diff --git a/src/glue.cpp b/src/glue.cpp index 52035af..eb5049d 100644 --- a/src/glue.cpp +++ b/src/glue.cpp @@ -25,11 +25,6 @@ bool Core_IsPluginLoaded(MonoString* name, int version, bool minimum) { return g_monolm.GetProvider()->IsPluginLoaded(MonoStringToUTF8(name), requiredVersion, minimum); } -MonoObject* Plugin_FindPluginByName(MonoString* name) { - ScriptInstance* script = g_monolm.FindScript(MonoStringToUTF8(name)); - return script ? script->GetManagedObject() : nullptr; -} - MonoString* Plugin_FindResource(MonoString* name, MonoString* path) { ScriptInstance* script = g_monolm.FindScript(MonoStringToUTF8(name)); if (script) { @@ -41,10 +36,15 @@ MonoString* Plugin_FindResource(MonoString* name, MonoString* path) { return nullptr; } +MonoObject* Plugin_FindPluginByName(MonoString* name) { + ScriptInstance* script = g_monolm.FindScript(MonoStringToUTF8(name)); + return script ? script->GetManagedObject() : nullptr; +} + void Glue::RegisterFunctions() { PLUG_ADD_INTERNAL_CALL(Core_GetBaseDirectory); PLUG_ADD_INTERNAL_CALL(Core_IsModuleLoaded); PLUG_ADD_INTERNAL_CALL(Core_IsPluginLoaded); - PLUG_ADD_INTERNAL_CALL(Plugin_FindPluginByName); PLUG_ADD_INTERNAL_CALL(Plugin_FindResource); + PLUG_ADD_INTERNAL_CALL(Plugin_FindPluginByName); } \ No newline at end of file diff --git a/src/module.cpp b/src/module.cpp index 149be12..b884f48 100644 --- a/src/module.cpp +++ b/src/module.cpp @@ -615,7 +615,7 @@ void* CSharpLanguageModule::MonoDelegateToArg(MonoDelegate* source, const plugif void CSharpLanguageModule::CleanupFunctionCache() { for (auto it = _cachedFunctions.begin(); it != _cachedFunctions.end();) { - if (mono_gchandle_get_target(std::get(*it)) == nullptr) { + if (mono_gchandle_get_target(it->first) == nullptr) { it = _cachedFunctions.erase(it); } else { ++it; @@ -692,7 +692,7 @@ void CSharpLanguageModule::ExternalCall(const Method* method, void* addr, const break; case ValueType::Vector2: ag = dcNewAggr(2, sizeof(Vector2)); - for (int i = 0; i < 2; ++i) + for (size_t i = 0; i < 2; ++i) dcAggrField(ag, DC_SIGCHAR_FLOAT, static_cast(sizeof(float) * i), 1); dcCloseAggr(ag); dcBeginCallAggr(vm, ag); @@ -700,7 +700,7 @@ void CSharpLanguageModule::ExternalCall(const Method* method, void* addr, const break; case ValueType::Vector3: ag = dcNewAggr(3, sizeof(Vector3)); - for (int i = 0; i < 3; ++i) + for (size_t i = 0; i < 3; ++i) dcAggrField(ag, DC_SIGCHAR_FLOAT, static_cast(sizeof(float) * i), 1); dcCloseAggr(ag); dcBeginCallAggr(vm, ag); @@ -708,7 +708,7 @@ void CSharpLanguageModule::ExternalCall(const Method* method, void* addr, const break; case ValueType::Vector4: ag = dcNewAggr(4, sizeof(Vector4)); - for (int i = 0; i < 4; ++i) + for (size_t i = 0; i < 4; ++i) dcAggrField(ag, DC_SIGCHAR_FLOAT, static_cast(sizeof(float) * i), 1); dcCloseAggr(ag); dcBeginCallAggr(vm, ag); @@ -716,7 +716,7 @@ void CSharpLanguageModule::ExternalCall(const Method* method, void* addr, const break; case ValueType::Matrix4x4: ag = dcNewAggr(16, sizeof(Matrix4x4)); - for (int i = 0; i < 16; ++i) + for (size_t i = 0; i < 16; ++i) dcAggrField(ag, DC_SIGCHAR_FLOAT, static_cast(sizeof(float) * i), 1); dcCloseAggr(ag); dcBeginCallAggr(vm, ag); @@ -1169,96 +1169,91 @@ void CSharpLanguageModule::ExternalCall(const Method* method, void* addr, const break; } - // Pull back references into provided arguments - - PullReferences(method, p, count, hasRet, hasRefs, args); - - if (!args.empty()) { - uint8_t j = 0; + size_t argsCount = args.size(); + if (argsCount != 0) { + if (hasRefs) { + size_t j = hasRet; // skip first param if has return + + if (j < argsCount) { + for (uint8_t i = 0; i < count; ++i) { + const auto& param = method->paramTypes[i]; + if (param.ref) { + switch (param.type) { + case ValueType::String: + p->SetArgumentAt(i, g_monolm.CreateString(*reinterpret_cast(args[j++]))); + break; + case ValueType::ArrayBool: + p->SetArgumentAt(i, g_monolm.CreateArrayT(*reinterpret_cast*>(args[j++]), mono_get_byte_class())); + break; + case ValueType::ArrayChar8: + p->SetArgumentAt(i, g_monolm.CreateArrayT(*reinterpret_cast*>(args[j++]), mono_get_char_class())); + break; + case ValueType::ArrayChar16: + p->SetArgumentAt(i, g_monolm.CreateArrayT(*reinterpret_cast*>(args[j++]), mono_get_char_class())); + break; + case ValueType::ArrayInt8: + p->SetArgumentAt(i, g_monolm.CreateArrayT(*reinterpret_cast*>(args[j++]), mono_get_sbyte_class())); + break; + case ValueType::ArrayInt16: + p->SetArgumentAt(i, g_monolm.CreateArrayT(*reinterpret_cast*>(args[j++]), mono_get_int16_class())); + break; + case ValueType::ArrayInt32: + p->SetArgumentAt(i, g_monolm.CreateArrayT(*reinterpret_cast*>(args[j++]), mono_get_int32_class())); + break; + case ValueType::ArrayInt64: + p->SetArgumentAt(i, g_monolm.CreateArrayT(*reinterpret_cast*>(args[j++]), mono_get_int64_class())); + break; + case ValueType::ArrayUInt8: + p->SetArgumentAt(i, g_monolm.CreateArrayT(*reinterpret_cast*>(args[j++]), mono_get_byte_class())); + break; + case ValueType::ArrayUInt16: + p->SetArgumentAt(i, g_monolm.CreateArrayT(*reinterpret_cast*>(args[j++]), mono_get_uint16_class())); + break; + case ValueType::ArrayUInt32: + p->SetArgumentAt(i, g_monolm.CreateArrayT(*reinterpret_cast*>(args[j++]), mono_get_uint32_class())); + break; + case ValueType::ArrayUInt64: + p->SetArgumentAt(i, g_monolm.CreateArrayT(*reinterpret_cast*>(args[j++]), mono_get_uint64_class())); + break; + case ValueType::ArrayPointer: + p->SetArgumentAt(i, g_monolm.CreateArrayT(*reinterpret_cast*>(args[j++]), mono_get_intptr_class())); + break; + case ValueType::ArrayFloat: + p->SetArgumentAt(i, g_monolm.CreateArrayT(*reinterpret_cast*>(args[j++]), mono_get_single_class())); + break; + case ValueType::ArrayDouble: + p->SetArgumentAt(i, g_monolm.CreateArrayT(*reinterpret_cast*>(args[j++]), mono_get_double_class())); + break; + case ValueType::ArrayString: + p->SetArgumentAt(i, g_monolm.CreateStringArray(*reinterpret_cast*>(args[j++]))); + break; + default: + break; + } + } + if (j == argsCount) + break; + } + } + } + + size_t j = 0; if (hasRet) { DeleteReturn(args, j, method->retType.type); } - if (j < args.size()) { + if (j < argsCount) { for (uint8_t i = 0; i < count; ++i) { DeleteParam(args, j, method->paramTypes[i].type); - if (j == args.size()) - break; - } - } - } -} - -void CSharpLanguageModule::PullReferences(const Method* method, const Parameters* p, uint8_t count, bool hasRet, bool hasRefs, const ArgumentList& args) { - if (hasRefs) { - uint8_t j = hasRet; // skip first param if has return - - if (j < args.size()) { - for (uint8_t i = 0; i < count; ++i) { - const auto& param = method->paramTypes[i]; - if (param.ref) { - switch (param.type) { - case ValueType::String: - p->SetArgumentAt(i, g_monolm.CreateString(*reinterpret_cast(args[j++]))); - break; - case ValueType::ArrayBool: - p->SetArgumentAt(i, g_monolm.CreateArrayT(*reinterpret_cast*>(args[j++]), mono_get_byte_class())); - break; - case ValueType::ArrayChar8: - p->SetArgumentAt(i, g_monolm.CreateArrayT(*reinterpret_cast*>(args[j++]), mono_get_char_class())); - break; - case ValueType::ArrayChar16: - p->SetArgumentAt(i, g_monolm.CreateArrayT(*reinterpret_cast*>(args[j++]), mono_get_char_class())); - break; - case ValueType::ArrayInt8: - p->SetArgumentAt(i, g_monolm.CreateArrayT(*reinterpret_cast*>(args[j++]), mono_get_sbyte_class())); - break; - case ValueType::ArrayInt16: - p->SetArgumentAt(i, g_monolm.CreateArrayT(*reinterpret_cast*>(args[j++]), mono_get_int16_class())); - break; - case ValueType::ArrayInt32: - p->SetArgumentAt(i, g_monolm.CreateArrayT(*reinterpret_cast*>(args[j++]), mono_get_int32_class())); - break; - case ValueType::ArrayInt64: - p->SetArgumentAt(i, g_monolm.CreateArrayT(*reinterpret_cast*>(args[j++]), mono_get_int64_class())); - break; - case ValueType::ArrayUInt8: - p->SetArgumentAt(i, g_monolm.CreateArrayT(*reinterpret_cast*>(args[j++]), mono_get_byte_class())); - break; - case ValueType::ArrayUInt16: - p->SetArgumentAt(i, g_monolm.CreateArrayT(*reinterpret_cast*>(args[j++]), mono_get_uint16_class())); - break; - case ValueType::ArrayUInt32: - p->SetArgumentAt(i, g_monolm.CreateArrayT(*reinterpret_cast*>(args[j++]), mono_get_uint32_class())); - break; - case ValueType::ArrayUInt64: - p->SetArgumentAt(i, g_monolm.CreateArrayT(*reinterpret_cast*>(args[j++]), mono_get_uint64_class())); - break; - case ValueType::ArrayPointer: - p->SetArgumentAt(i, g_monolm.CreateArrayT(*reinterpret_cast*>(args[j++]), mono_get_intptr_class())); - break; - case ValueType::ArrayFloat: - p->SetArgumentAt(i, g_monolm.CreateArrayT(*reinterpret_cast*>(args[j++]), mono_get_single_class())); - break; - case ValueType::ArrayDouble: - p->SetArgumentAt(i, g_monolm.CreateArrayT(*reinterpret_cast*>(args[j++]), mono_get_double_class())); - break; - case ValueType::ArrayString: - p->SetArgumentAt(i, g_monolm.CreateStringArray(*reinterpret_cast*>(args[j++]))); - break; - default: - break; - } - } - if (j == args.size()) + if (j == argsCount) break; } } } } -void CSharpLanguageModule::DeleteParam(const ArgumentList& args, uint8_t& i, ValueType type) { +void CSharpLanguageModule::DeleteParam(const ArgumentList& args, size_t& i, ValueType type) { switch (type) { case ValueType::String: delete reinterpret_cast(args[i++]); @@ -1313,7 +1308,7 @@ void CSharpLanguageModule::DeleteParam(const ArgumentList& args, uint8_t& i, Val } } -void CSharpLanguageModule::DeleteReturn(const ArgumentList& args, uint8_t& i, ValueType type) { +void CSharpLanguageModule::DeleteReturn(const ArgumentList& args, size_t& i, ValueType type) { switch (type) { case ValueType::String: FreeMemory(args[i++]); diff --git a/src/module.h b/src/module.h index 795fb15..cb9f9ac 100644 --- a/src/module.h +++ b/src/module.h @@ -124,12 +124,11 @@ namespace monolm { static void InternalCall(const plugify::Method* method, void* data, const plugify::Parameters* params, uint8_t count, const plugify::ReturnValue* ret); static void DelegateCall(const plugify::Method* method, void* data, const plugify::Parameters* params, uint8_t count, const plugify::ReturnValue* ret); - static void DeleteParam(const std::vector& args, uint8_t& i, plugify::ValueType type); - static void DeleteReturn(const std::vector& args, uint8_t& i, plugify::ValueType type); + static void DeleteParam(const ArgumentList& args, size_t& i, plugify::ValueType type); + static void DeleteReturn(const ArgumentList& args, size_t& i, plugify::ValueType type); static void SetReturn(const plugify::Method* method, const plugify::Parameters* p, const plugify::ReturnValue* ret, MonoObject* result); static void SetParams(const plugify::Method* method, const plugify::Parameters* p, uint8_t count, bool hasRet, bool& hasRefs, ArgumentList& args); static void SetReferences(const plugify::Method* method, const plugify::Parameters* p, uint8_t count, bool hasRet, bool hasRefs, const ArgumentList& args); - static void PullReferences(const plugify::Method* method, const plugify::Parameters* p, uint8_t count, bool hasRet, bool hasRefs, const ArgumentList& args); template static void* MonoStructToArg(ArgumentList& args);