Load cartridge

This commit is contained in:
Fabien Freling 2019-05-15 14:03:53 +02:00
parent 2f31756be8
commit d197159f5a
7 changed files with 40 additions and 9 deletions

View file

@ -67,6 +67,8 @@ let get_RAM_size = function
let read_cartridge file =
Printf.printf "Read cartridge: %s" file;
print_newline ();
try
let ic = open_in_bin file in

View file

@ -1,3 +1,4 @@
let () =
Callback.register "oboy_name" Version.name;
Callback.register "oboy_version" Version.version;
Callback.register "oboy_load" State.load_cartridge;

View file

@ -1,9 +1,16 @@
(* State of the world, contains all the required information at any point *)
type t = {
cartridge : option Cartridge.t;
mutable cartridge : Cartridge.t option;
}
(* Global reference *)
let state = ref {
cartridge = None
}
let load_cartridge file =
let cartridge = Cartridge.read_cartridge file in
!state.cartridge <- cartridge;
match cartridge with
| Some _ -> true
| None -> false

View file

@ -1,6 +1,5 @@
import QtQuick 2.4
import QtQuick.Window 2.0
import com.oboy.oboy 1.0
Window {
id: window
@ -12,10 +11,6 @@ Window {
maximumHeight: height
minimumHeight: height
OBoy {
id: oboy
}
Text {
id: name
text: oboy.name

View file

@ -1,6 +1,7 @@
import QtQuick 2.5
import QtQuick.Controls 1.4
import QtQuick.Dialogs 1.2
import com.oboy.oboy 1.0
ApplicationWindow {
id: root
@ -49,6 +50,10 @@ ApplicationWindow {
}
OBoy {
id: oboy
}
MainForm {
anchors.fill: parent
openButton.onClicked: fileDialog.open()
@ -70,7 +75,8 @@ ApplicationWindow {
title: "Please choose a file"
folder: shortcuts.home
onAccepted: {
console.log("You chose: " + fileDialog.fileUrls)
console.log("You chose: " + fileDialog.fileUrl)
oboy.load(fileDialog.fileUrl)
}
onRejected: {
console.log("Canceled")

View file

@ -2,6 +2,9 @@
#include <caml/mlvalues.h>
#include <caml/callback.h>
#include <caml/alloc.h>
#include <caml/misc.h>
#include <caml/osdeps.h>
OBoy::OBoy(QObject *parent) : QObject(parent)
{
@ -29,3 +32,19 @@ QString OBoy::version() const
const char *str = String_val(caml_callback(*closure_f, Val_unit));
return QString(str);
}
bool OBoy::load(const QString &path)
{
value * closure_f = caml_named_value("oboy_load");
if (closure_f == nullptr) {
return false;
}
QString truncated(path);
truncated.remove(0, 7); // remove file://
QByteArray ba = truncated.toLocal8Bit();
value ocaml_path = caml_copy_string_of_os(ba.data());
const bool success = Bool_val(caml_callback(*closure_f, ocaml_path));
return success;
}

View file

@ -5,11 +5,12 @@
class OBoy : public QObject
{
Q_OBJECT
Q_PROPERTY(QString name READ name)
Q_PROPERTY(QString version READ version)
Q_PROPERTY(QString name READ name CONSTANT)
Q_PROPERTY(QString version READ version CONSTANT)
public:
explicit OBoy(QObject *parent = nullptr);
QString name() const;
QString version() const;
Q_INVOKABLE bool load(const QString &path);
};