forked from TheCherno/Hazel
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Added mono binaries/includes - Basic assembly loading and object creation/method calling in ScriptEngine
- Loading branch information
Showing
613 changed files
with
44,955 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
using System; | ||
|
||
namespace Hazel | ||
{ | ||
|
||
public class Main | ||
{ | ||
|
||
public float FloatVar { get; set; } | ||
|
||
public Main() | ||
{ | ||
Console.WriteLine("Main constructor!"); | ||
} | ||
|
||
public void PrintMessage() | ||
{ | ||
Console.WriteLine("Hello World from C#!"); | ||
} | ||
|
||
public void PrintInt(int value) | ||
{ | ||
Console.WriteLine($"C# says: {value}"); | ||
} | ||
|
||
public void PrintInts(int value1, int value2) | ||
{ | ||
Console.WriteLine($"C# says: {value1} and {value2}"); | ||
} | ||
|
||
public void PrintCustomMessage(string message) | ||
{ | ||
Console.WriteLine($"C# says: {message}"); | ||
} | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
project "Hazel-ScriptCore" | ||
kind "SharedLib" | ||
language "C#" | ||
dotnetframework "4.7.2" | ||
|
||
targetdir ("%{wks.location}/Hazelnut/Resources/Scripts") | ||
objdir ("%{wks.location}/Hazelnut/Resources/Scripts/Intermediates") | ||
|
||
files | ||
{ | ||
"Source/**.cs", | ||
"Properties/**.cs" | ||
} | ||
|
||
filter "configurations:Debug" | ||
optimize "Off" | ||
symbols "Default" | ||
|
||
filter "configurations:Release" | ||
optimize "On" | ||
symbols "Default" | ||
|
||
filter "configurations:Dist" | ||
optimize "Full" | ||
symbols "Off" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,169 @@ | ||
#include "hzpch.h" | ||
#include "ScriptEngine.h" | ||
|
||
#include "mono/jit/jit.h" | ||
#include "mono/metadata/assembly.h" | ||
#include "mono/metadata/object.h" | ||
|
||
namespace Hazel { | ||
|
||
struct ScriptEngineData | ||
{ | ||
MonoDomain* RootDomain = nullptr; | ||
MonoDomain* AppDomain = nullptr; | ||
|
||
MonoAssembly* CoreAssembly = nullptr; | ||
}; | ||
|
||
static ScriptEngineData* s_Data = nullptr; | ||
|
||
void ScriptEngine::Init() | ||
{ | ||
s_Data = new ScriptEngineData(); | ||
|
||
InitMono(); | ||
} | ||
|
||
void ScriptEngine::Shutdown() | ||
{ | ||
ShutdownMono(); | ||
delete s_Data; | ||
} | ||
|
||
char* ReadBytes(const std::string& filepath, uint32_t* outSize) | ||
{ | ||
std::ifstream stream(filepath, std::ios::binary | std::ios::ate); | ||
|
||
if (!stream) | ||
{ | ||
// Failed to open the file | ||
return nullptr; | ||
} | ||
|
||
std::streampos end = stream.tellg(); | ||
stream.seekg(0, std::ios::beg); | ||
uint32_t size = end - stream.tellg(); | ||
|
||
if (size == 0) | ||
{ | ||
// File is empty | ||
return nullptr; | ||
} | ||
|
||
char* buffer = new char[size]; | ||
stream.read((char*)buffer, size); | ||
stream.close(); | ||
|
||
*outSize = size; | ||
return buffer; | ||
} | ||
|
||
MonoAssembly* LoadCSharpAssembly(const std::string& assemblyPath) | ||
{ | ||
uint32_t fileSize = 0; | ||
char* fileData = ReadBytes(assemblyPath, &fileSize); | ||
|
||
// NOTE: We can't use this image for anything other than loading the assembly because this image doesn't have a reference to the assembly | ||
MonoImageOpenStatus status; | ||
MonoImage* image = mono_image_open_from_data_full(fileData, fileSize, 1, &status, 0); | ||
|
||
if (status != MONO_IMAGE_OK) | ||
{ | ||
const char* errorMessage = mono_image_strerror(status); | ||
// Log some error message using the errorMessage data | ||
return nullptr; | ||
} | ||
|
||
MonoAssembly* assembly = mono_assembly_load_from_full(image, assemblyPath.c_str(), &status, 0); | ||
mono_image_close(image); | ||
|
||
// Don't forget to free the file data | ||
delete[] fileData; | ||
|
||
return assembly; | ||
} | ||
|
||
void PrintAssemblyTypes(MonoAssembly* assembly) | ||
{ | ||
MonoImage* image = mono_assembly_get_image(assembly); | ||
const MonoTableInfo* typeDefinitionsTable = mono_image_get_table_info(image, MONO_TABLE_TYPEDEF); | ||
int32_t numTypes = mono_table_info_get_rows(typeDefinitionsTable); | ||
|
||
for (int32_t i = 0; i < numTypes; i++) | ||
{ | ||
uint32_t cols[MONO_TYPEDEF_SIZE]; | ||
mono_metadata_decode_row(typeDefinitionsTable, i, cols, MONO_TYPEDEF_SIZE); | ||
|
||
const char* nameSpace = mono_metadata_string_heap(image, cols[MONO_TYPEDEF_NAMESPACE]); | ||
const char* name = mono_metadata_string_heap(image, cols[MONO_TYPEDEF_NAME]); | ||
|
||
HZ_CORE_TRACE("{}.{}", nameSpace, name); | ||
} | ||
} | ||
|
||
void ScriptEngine::InitMono() | ||
{ | ||
mono_set_assemblies_path("mono/lib"); | ||
|
||
MonoDomain* rootDomain = mono_jit_init("HazelJITRuntime"); | ||
HZ_CORE_ASSERT(rootDomain); | ||
|
||
// Store the root domain pointer | ||
s_Data->RootDomain = rootDomain; | ||
|
||
// Create an App Domain | ||
s_Data->AppDomain = mono_domain_create_appdomain("HazelScriptRuntime", nullptr); | ||
mono_domain_set(s_Data->AppDomain, true); | ||
|
||
// Move this maybe | ||
s_Data->CoreAssembly = LoadCSharpAssembly("Resources/Scripts/Hazel-ScriptCore.dll"); | ||
PrintAssemblyTypes(s_Data->CoreAssembly); | ||
|
||
MonoImage* assemblyImage = mono_assembly_get_image(s_Data->CoreAssembly); | ||
MonoClass* monoClass = mono_class_from_name(assemblyImage, "Hazel", "Main"); | ||
|
||
// 1. create an object (and call constructor) | ||
MonoObject* instance = mono_object_new(s_Data->AppDomain, monoClass); | ||
mono_runtime_object_init(instance); | ||
|
||
// 2. call function | ||
MonoMethod* printMessageFunc = mono_class_get_method_from_name(monoClass, "PrintMessage", 0); | ||
mono_runtime_invoke(printMessageFunc, instance, nullptr, nullptr); | ||
|
||
// 3. call function with param | ||
MonoMethod* printIntFunc = mono_class_get_method_from_name(monoClass, "PrintInt", 1); | ||
|
||
int value = 5; | ||
void* param = &value; | ||
|
||
mono_runtime_invoke(printIntFunc, instance, ¶m, nullptr); | ||
|
||
MonoMethod* printIntsFunc = mono_class_get_method_from_name(monoClass, "PrintInts", 2); | ||
int value2 = 508; | ||
void* params[2] = | ||
{ | ||
&value, | ||
&value2 | ||
}; | ||
mono_runtime_invoke(printIntsFunc, instance, params, nullptr); | ||
|
||
MonoString* monoString = mono_string_new(s_Data->AppDomain, "Hello World from C++!"); | ||
MonoMethod* printCustomMessageFunc = mono_class_get_method_from_name(monoClass, "PrintCustomMessage", 1); | ||
void* stringParam = monoString; | ||
mono_runtime_invoke(printCustomMessageFunc, instance, &stringParam, nullptr); | ||
|
||
// HZ_CORE_ASSERT(false); | ||
} | ||
|
||
void ScriptEngine::ShutdownMono() | ||
{ | ||
// NOTE(Yan): mono is a little confusing to shutdown, so maybe come back to this | ||
|
||
// mono_domain_unload(s_Data->AppDomain); | ||
s_Data->AppDomain = nullptr; | ||
|
||
// mono_jit_cleanup(s_Data->RootDomain); | ||
s_Data->RootDomain = nullptr; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#pragma once | ||
|
||
namespace Hazel { | ||
|
||
class ScriptEngine | ||
{ | ||
public: | ||
static void Init(); | ||
static void Shutdown(); | ||
private: | ||
static void InitMono(); | ||
static void ShutdownMono(); | ||
}; | ||
|
||
} |
Oops, something went wrong.