|
|
|
@ -4,13 +4,107 @@
|
|
|
|
|
|
|
|
|
|
#include <cstdarg>
|
|
|
|
|
#include <mono/metadata/appdomain.h>
|
|
|
|
|
#include <mono/metadata/object-forward.h>
|
|
|
|
|
#include <mono/jit/jit.h>
|
|
|
|
|
#include <mono/metadata/assembly.h>
|
|
|
|
|
#include <mono/metadata/mono-config.h>
|
|
|
|
|
#include <apps/openmw-mp/Script/ScriptFunctions.hpp>
|
|
|
|
|
#include <apps/openmw-mp/Script/API/TimerAPI.hpp>
|
|
|
|
|
#include "LangMono.hpp"
|
|
|
|
|
|
|
|
|
|
static MonoDomain *domain = nullptr; // shared domain
|
|
|
|
|
|
|
|
|
|
std::string monoStringToStdString(MonoString *monoString)
|
|
|
|
|
{
|
|
|
|
|
char *utf8 = mono_string_to_utf8(monoString);
|
|
|
|
|
std::string str = utf8;
|
|
|
|
|
mono_free(utf8);
|
|
|
|
|
return str;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
|
T unbox(MonoObject *obj)
|
|
|
|
|
{
|
|
|
|
|
return *(T *) mono_object_unbox(obj);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
boost::any monoObjectToAny(MonoObject *obj)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
MonoClass *klass = mono_object_get_class(obj);
|
|
|
|
|
MonoType *rawType = mono_class_get_type(klass);
|
|
|
|
|
|
|
|
|
|
switch ((MonoTypeEnum) mono_type_get_type(rawType))
|
|
|
|
|
{
|
|
|
|
|
case MONO_TYPE_END:
|
|
|
|
|
case MONO_TYPE_VOID:
|
|
|
|
|
break;
|
|
|
|
|
case MONO_TYPE_BOOLEAN:
|
|
|
|
|
return (bool) unbox<MonoBoolean>(obj);
|
|
|
|
|
case MONO_TYPE_CHAR:
|
|
|
|
|
return unbox<uint16_t>(obj);
|
|
|
|
|
case MONO_TYPE_I1:
|
|
|
|
|
return unbox<int8_t>(obj);
|
|
|
|
|
case MONO_TYPE_U1:
|
|
|
|
|
return unbox<uint8_t>(obj);
|
|
|
|
|
case MONO_TYPE_I2:
|
|
|
|
|
return unbox<int16_t>(obj);
|
|
|
|
|
case MONO_TYPE_U2:
|
|
|
|
|
return unbox<uint16_t>(obj);
|
|
|
|
|
case MONO_TYPE_I4:
|
|
|
|
|
return unbox<int32_t>(obj);
|
|
|
|
|
case MONO_TYPE_U4:
|
|
|
|
|
return unbox<uint32_t>(obj);
|
|
|
|
|
case MONO_TYPE_I8:
|
|
|
|
|
return unbox<int64_t>(obj);
|
|
|
|
|
case MONO_TYPE_U8:
|
|
|
|
|
return unbox<uint64_t>(obj);
|
|
|
|
|
case MONO_TYPE_R4:
|
|
|
|
|
return unbox<float>(obj);
|
|
|
|
|
case MONO_TYPE_R8:
|
|
|
|
|
return unbox<double>(obj);
|
|
|
|
|
case MONO_TYPE_STRING:
|
|
|
|
|
return monoStringToStdString((MonoString *) obj);
|
|
|
|
|
case MONO_TYPE_ARRAY:
|
|
|
|
|
case MONO_TYPE_SZARRAY:
|
|
|
|
|
{
|
|
|
|
|
MonoArrayType *arrayType = mono_type_get_array_type(rawType);
|
|
|
|
|
}
|
|
|
|
|
default:
|
|
|
|
|
return boost::any();
|
|
|
|
|
}
|
|
|
|
|
return boost::any();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int LangMono::CreateTimerEx(MonoObject *delegate, long msec, MonoString *monoStr, MonoArray *monoArgs)
|
|
|
|
|
{
|
|
|
|
|
size_t argsLength = mono_array_length(monoArgs);
|
|
|
|
|
std::vector<boost::any> params (argsLength);
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
for(size_t i = 0; i < argsLength; ++i)
|
|
|
|
|
params[i] = monoObjectToAny(mono_array_get(monoArgs, MonoObject*, i));
|
|
|
|
|
|
|
|
|
|
char *types = mono_string_to_utf8(monoStr);
|
|
|
|
|
int id = mwmp::TimerAPI::CreateTimerMono(delegate, msec, types, params);
|
|
|
|
|
mono_free(types);
|
|
|
|
|
return id;
|
|
|
|
|
}
|
|
|
|
|
catch (...)
|
|
|
|
|
{
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void LangMono::MakePublic(MonoObject *delegate, const char *name) noexcept
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
MonoObject *LangMono::CallPublic(const char *name, MonoArray *args)
|
|
|
|
|
{
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
lib_t LangMono::GetInterface()
|
|
|
|
|
{
|
|
|
|
@ -40,6 +134,9 @@ void LangMono::Init()
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
domain = mono_jit_init("TES3MP Mono VM"); // will leak :P
|
|
|
|
|
mono_add_internal_call("TES3MPSharp.TES3MP::CreateTimerEx", (void*) &LangMono::CreateTimerEx);
|
|
|
|
|
mono_add_internal_call("TES3MPSharp.TES3MP::MakePublic", (void*) &LangMono::MakePublic);
|
|
|
|
|
mono_add_internal_call("TES3MPSharp.TES3MP::CallPublic", (void*) &LangMono::CallPublic);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::vector<MonoClass *> getInstanceClassList(MonoImage *image, const std::string &parentName)
|
|
|
|
@ -109,8 +206,7 @@ boost::any LangMono::Call(const char *name, const char *argl, int buf, ...)
|
|
|
|
|
|
|
|
|
|
int n_args = (int) (strlen(argl));
|
|
|
|
|
|
|
|
|
|
std::vector<void *> vec;
|
|
|
|
|
vec.reserve(n_args);
|
|
|
|
|
std::vector<void *> vec(n_args);
|
|
|
|
|
|
|
|
|
|
for (int index = 0; index < n_args; index++)
|
|
|
|
|
{
|
|
|
|
@ -119,48 +215,48 @@ boost::any LangMono::Call(const char *name, const char *argl, int buf, ...)
|
|
|
|
|
case 'i':
|
|
|
|
|
{
|
|
|
|
|
auto val = va_arg(vargs, unsigned int);
|
|
|
|
|
vec.push_back((void *) &val);
|
|
|
|
|
vec[index] = (void *) &val;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case 'q':
|
|
|
|
|
{
|
|
|
|
|
auto val = va_arg(vargs, signed int);
|
|
|
|
|
vec.push_back((void *) &val);
|
|
|
|
|
vec[index] = (void *) &val;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case 'l':
|
|
|
|
|
{
|
|
|
|
|
auto val = va_arg(vargs, unsigned long long);
|
|
|
|
|
vec.push_back((void *) &val);
|
|
|
|
|
vec[index] = (void *) &val;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case 'w':
|
|
|
|
|
{
|
|
|
|
|
auto val = va_arg(vargs, signed long long);
|
|
|
|
|
vec.push_back((void *) &val);
|
|
|
|
|
vec[index] = (void *) &val;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case 'f':
|
|
|
|
|
{
|
|
|
|
|
auto val = va_arg(vargs, double);
|
|
|
|
|
vec.push_back((void *) &val);
|
|
|
|
|
vec[index] = (void *) &val;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case 'p':
|
|
|
|
|
{
|
|
|
|
|
auto val = va_arg(vargs, void*);
|
|
|
|
|
vec.push_back((void *) &val);
|
|
|
|
|
vec[index] = (void *) &val;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case 's':
|
|
|
|
|
{
|
|
|
|
|
vec.push_back(mono_string_new(mono_domain_get(), va_arg(vargs, const char*)));
|
|
|
|
|
vec[index] = (mono_string_new(mono_domain_get(), va_arg(vargs, const char*)));
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case 'b':
|
|
|
|
|
{
|
|
|
|
|
auto val = va_arg(vargs, int);
|
|
|
|
|
vec.push_back((void *) &val);
|
|
|
|
|
vec[index] = (void *) &val;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -182,6 +278,9 @@ boost::any LangMono::Call(const char *name, const char *argl, int buf, ...)
|
|
|
|
|
methodsCache[{name, n_args}] = method;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (method == nullptr)
|
|
|
|
|
return boost::any();
|
|
|
|
|
|
|
|
|
|
MonoObject *ret = mono_runtime_invoke(method, instance->object, vec.data(), nullptr);
|
|
|
|
|
if (ret)
|
|
|
|
|
return boost::any(mono_object_unbox(ret));
|
|
|
|
|