diff --git a/.gitignore b/.gitignore index edc45cb..bdff2ad 100644 --- a/.gitignore +++ b/.gitignore @@ -20,4 +20,4 @@ temp/ logs/ log/ *.log -mono/ +./mono/ diff --git a/.vscode/launch.json b/.vscode/launch.json index 42c5cc2..0c287a0 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -11,6 +11,7 @@ "stopAtEntry": false, "cwd": "${workspaceFolder}/bin", "externalConsole": false, + "internalConsoleOptions": "neverOpen", "MIMode": "gdb", "miDebuggerPath": "gdb", "setupCommands": [ diff --git a/src/GUI/MainWindow.cpp b/src/GUI/MainWindow.cpp index 40d342c..6237bd0 100644 --- a/src/GUI/MainWindow.cpp +++ b/src/GUI/MainWindow.cpp @@ -2,7 +2,7 @@ #include #include #include "MainWindow.hpp" -#include "../exceptions.hpp" +#include "exceptions.hpp" #include "../format.hpp" #include "../Resources/fonts.hpp" #include "../Resources/textures.hpp" diff --git a/src/exceptions.cpp b/src/GUI/exceptions.cpp similarity index 100% rename from src/exceptions.cpp rename to src/GUI/exceptions.cpp diff --git a/src/exceptions.hpp b/src/GUI/exceptions.hpp similarity index 93% rename from src/exceptions.hpp rename to src/GUI/exceptions.hpp index 79d68ce..ef86f66 100644 --- a/src/exceptions.hpp +++ b/src/GUI/exceptions.hpp @@ -1,6 +1,6 @@ #pragma once -#include "UsefulException.hpp" +#include "../UsefulException.hpp" namespace ougge { diff --git a/src/Mono/Mono.cpp b/src/Mono/Mono.cpp new file mode 100644 index 0000000..3fe21f2 --- /dev/null +++ b/src/Mono/Mono.cpp @@ -0,0 +1,80 @@ +#include "Mono.hpp" +#include "../UsefulException.hpp" +#include "../format.hpp" +#include +#include +#include +#include + +namespace Mono { + +RuntimeJIT::RuntimeJIT(const std::string& domain_name){ + mono_set_dirs("mono-libs", "mono-libs"); + mono_set_assemblies_path("mono-libs"); + mono_config_parse("mono-libs/config.xml"); + + domain = mono_jit_init(domain_name.c_str()); + if(!domain) + throw UsefulException("can't initialize mono domain"); +} + +RuntimeJIT::~RuntimeJIT(){ + mono_jit_cleanup(domain); +} + +std::shared_ptr RuntimeJIT::loadAssembly(const std::string &name){ + MonoAssembly* ptr = mono_domain_assembly_open(domain, "app.exe"); + if(!ptr) + throw UsefulException(format("can't load assembly '%s'", name.c_str())); + return std::make_shared(ptr); +} + +Assembly::Assembly(MonoAssembly *ptr) + : ptr(ptr), image(mono_assembly_get_image(ptr)) +{ +} + +Assembly::~Assembly(){ + // Tt causes SEGFAULT. + // Is it even possible to unload assembly from domain? + // mono_assembly_close(ptr); +} + +std::shared_ptr Assembly::getClass(const std::string &name_space, const std::string &name){ + MonoClass* ptr = mono_class_from_name(image, name_space.c_str(), name.c_str()); + if(!ptr) + throw UsefulException(format("can't find class '%s.%s'", name_space.c_str(), name.c_str())); + return std::make_shared(ptr); +} + +Class::Class(MonoClass *ptr) + : ptr(ptr) +{ + void* iter = nullptr; + MonoMethod* m = nullptr; + while( (m = mono_class_get_methods(ptr, &iter)) ){ + auto name = std::string_view(mono_method_get_name(m)); + methods.emplace(std::piecewise_construct, + std::forward_as_tuple(name), + std::forward_as_tuple(name, m)); + } +} + +template +std::shared_ptr> getMethod(const std::string& name){ + + return std::make_shared>(); +} + +Method::Method(const std::string_view &name, MonoMethod *ptr) + : ptr(ptr), signature(mono_method_signature(ptr)), name(name) +{ + void* iter = nullptr; + MonoType* rt = mono_signature_get_return_type(signature); + MonoType* pt = nullptr; + while( (pt = mono_signature_get_params(signature, &iter)) ){ + std::cout<<"name: "< +#include +#include +#include +#include +#include +#include +#include + +namespace Mono { + +typedef i8 SByte; +typedef u8 Byte; +typedef i16 Short; +typedef u16 UShort; +typedef i32 Int; +typedef i32 UInt; +typedef i64 Long; +typedef i64 ULong; +typedef f32 Float; +typedef f64 Double; +typedef MonoString* String; + +class Method { + MonoMethod* ptr; + MonoMethodSignature* signature; + +public: + const std::string_view name; + Method(const std::string_view& name, MonoMethod* ptr); +}; + +class Class { + std::multimap methods; + MonoClass* ptr; + +public: + Class(MonoClass* ptr); + Class(const Class&) = delete; + + template + std::shared_ptr> getMethod(const std::string& name); +}; + +class Assembly { + MonoAssembly* ptr; + MonoImage* image; + +public: + Assembly(MonoAssembly* ptr); + Assembly(const Assembly&) = delete; + ~Assembly(); + + std::shared_ptr getClass(const std::string& name_space, const std::string& name); +}; + +///LINUX: `config.xml`, `mscorelib.dll`, `libmono-native.so` in `mono-libs` directory +/// +///WINDOWS: `config.xml`, `mscorelib.dll` in `mono-libs` directory +class RuntimeJIT { + MonoDomain* domain; +public: + RuntimeJIT(const std::string& domain_name = "MonoApp"); + RuntimeJIT(const RuntimeJIT&) = delete; + ~RuntimeJIT(); + + std::shared_ptr loadAssembly(const std::string& name); +}; + +} diff --git a/src/Resources/Resources.cpp b/src/Resources/Resources.cpp index d49dcd3..5dd3c15 100644 --- a/src/Resources/Resources.cpp +++ b/src/Resources/Resources.cpp @@ -1,6 +1,6 @@ #include #include -#include "../exceptions.hpp" +#include "../UsefulException.hpp" #include "../format.hpp" #include "Resources.hpp" #include "embedded_resources.h" diff --git a/src/Resources/textures.cpp b/src/Resources/textures.cpp index 321c125..1174fa3 100644 --- a/src/Resources/textures.cpp +++ b/src/Resources/textures.cpp @@ -1,6 +1,6 @@ #include "textures.hpp" #include -#include "../exceptions.hpp" +#include "../GUI/exceptions.hpp" namespace ougge::Resources { diff --git a/src/main.cpp b/src/main.cpp index 449740b..d4be044 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -4,10 +4,8 @@ #include "Resources/Resources.hpp" #include "Game/Scene.hpp" #include "format.hpp" -#include "exceptions.hpp" -#include -#include -#include +#include "UsefulException.hpp" +#include "Mono/Mono.hpp" using namespace ougge; @@ -18,29 +16,16 @@ void update(f64 deltaTime){ int main(int argc, const char** argv){ try { Resources::init(); - mono_set_dirs("mono-libs", "mono-libs"); - mono_set_assemblies_path("mono-libs"); - mono_config_parse("mono-libs/config.xml"); - std::cout<<"mono_root_dir: "<getClass("", "A"); + std::cout<<"initialized mono jit runtime"<