SAMP.NET/sampdotnethook/Mono.cpp

66 lines
2.0 KiB
C++
Raw Permalink Normal View History

2022-08-27 17:10:32 +00:00
#include "Mono.h"
2022-08-27 17:16:44 +00:00
#include <iostream>
2022-08-27 17:10:32 +00:00
Mono *g_Mono;
void Mono::init() {
// Set up domain
2022-08-27 17:16:44 +00:00
this->domain = mono_jit_init_version ("sampdotnet", "v2.0.50727");
2022-08-27 17:10:32 +00:00
// Set up assembly, image and class for Script.dll invoke
2022-08-27 17:16:44 +00:00
this->assembly = mono_domain_assembly_open (this->domain, "Script.dll");
this->image = mono_assembly_get_image (this->assembly);
this->klass = mono_class_from_name (this->image, "Script", "Script");
2022-08-27 17:10:32 +00:00
// Invoke the loading method for Script.dll
2022-08-27 17:16:44 +00:00
this->callMethod("Script:Load", NULL);
2022-08-27 17:10:32 +00:00
// That last method invoke was the last time we'll be calling to Script.dll
2022-08-27 17:16:44 +00:00
this->assembly = mono_domain_assembly_open (this->domain, "sampdotnet.dll");
this->image = mono_assembly_get_image (this->assembly);
this->klass = NULL; // We don't need this anymore
2022-08-27 17:10:32 +00:00
}
MonoString* Mono::createString(const char* string) {
return mono_string_new(this->domain, string);
}
int Mono::callReturn(const char* name, void* args[]) {
// Set method parameters
2022-08-27 17:16:44 +00:00
MonoMethodDesc* desc = mono_method_desc_new(name, false);
MonoMethod* method = NULL;
2022-08-27 17:10:32 +00:00
// Determine if we're using the Class model or Image model
2022-08-27 17:16:44 +00:00
if(this->klass != NULL) {
2022-08-27 17:10:32 +00:00
method = mono_method_desc_search_in_class(desc, this->klass);
} else {
method = mono_method_desc_search_in_image(desc, this->image);
}
// Free MonoMethodDesc object
mono_method_desc_free(desc);
// Invoke and return the value
2022-08-27 17:16:44 +00:00
MonoObject* result = mono_runtime_invoke(method, this->klass, args, NULL);
int int_result = NULL;
2022-08-27 17:10:32 +00:00
int_result = *(int*)mono_object_unbox(result);
return int_result;
}
void Mono::callMethod(const char* name, void* args[]) {
// Set method parameters
2022-08-27 17:16:44 +00:00
MonoMethodDesc* desc = mono_method_desc_new(name, false);
MonoMethod* method = NULL;
2022-08-27 17:10:32 +00:00
// Determine if we're using the Class model or Image model
2022-08-27 17:16:44 +00:00
if(this->klass != NULL) {
2022-08-27 17:10:32 +00:00
method = mono_method_desc_search_in_class(desc, this->klass);
} else {
method = mono_method_desc_search_in_image(desc, this->image);
}
// Free MonoMethodDesc object
mono_method_desc_free(desc);
// Invoke
2022-08-27 17:16:44 +00:00
mono_runtime_invoke(method, this->klass, args, NULL);
}