From d95515fdb739e74e715488db2725c556bf70c0ba Mon Sep 17 00:00:00 2001 From: Cold-Mint Date: Sat, 27 Jul 2024 22:06:10 +0800 Subject: [PATCH] =?UTF-8?q?ModLoader=20now=20support=20calling=20entry=20f?= =?UTF-8?q?unctions.=20=E6=A8=A1=E7=BB=84=E5=8A=A0=E8=BD=BD=E5=99=A8?= =?UTF-8?q?=E7=8E=B0=E5=9C=A8=E6=94=AF=E6=8C=81=E8=B0=83=E7=94=A8=E5=85=A5?= =?UTF-8?q?=E5=8F=A3=E5=87=BD=E6=95=B0=E4=BA=86=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- locals/Log.csv | 3 +- prefab/entitys/DelivererOfDarkMagic.tscn | 1 + scripts/mod/ModLoader.cs | 88 +++++++++++++++--------- 3 files changed, 57 insertions(+), 35 deletions(-) diff --git a/locals/Log.csv b/locals/Log.csv index 3fcfc49..7d4251f 100644 --- a/locals/Log.csv +++ b/locals/Log.csv @@ -108,4 +108,5 @@ log_owner_is_null,所有者为空。,Owner is null.,所有者が空です。 log_dll_name,已加载{0}。,Loaded {0}.,{0}をロードしました。 log_dll_does_not_register_lifecycle_processor,位于{0}的dll文件,未注册生命周期处理器{1}。,"The DLL file located at {0} does not register the lifecycle processor {1}.",{0}にあるDLLファイルは、ライフサイクルプロセッサ{1}を登録していません。 log_mod_lifecycle_handler_not_implement_interface,位于{0}的dll文件,生命周期处理器未实现IModLifecycleHandler接口。,"The DLL file located at {0}, the lifecycle processor does not implement the IModLifecycleHandler interface.",{0}にあるDLLファイル、ライフサイクルプロセッサはIModLifecycleHandlerインターフェースを実装していません。 -log_dll_no_parameterless_constructor,位于{0}的dll文件,没有无参构造函数。,"The DLL file located at {0} does not have a parameterless constructor.",{0}にあるDLLファイルにはパラメータのないコンストラクタがありません。 \ No newline at end of file +log_dll_no_parameterless_constructor,位于{0}的dll文件,没有无参构造函数。,"The DLL file located at {0} does not have a parameterless constructor.",{0}にあるDLLファイルにはパラメータのないコンストラクタがありません。 +log_dll_type_length,位于{0}的dll文件内,共包含{1}个类型。,"The DLL file located at {0} contains a total of {1} types.",{0}にあるDLLファイルには合計{1}個のタイプが含まれています。 \ No newline at end of file diff --git a/prefab/entitys/DelivererOfDarkMagic.tscn b/prefab/entitys/DelivererOfDarkMagic.tscn index 2df9169..1362234 100644 --- a/prefab/entitys/DelivererOfDarkMagic.tscn +++ b/prefab/entitys/DelivererOfDarkMagic.tscn @@ -34,6 +34,7 @@ radius = 172.29 collision_layer = 64 collision_mask = 38 script = ExtResource("1_ubaid") +InitWeaponRes = null LootListId = "test" metadata/CampId = "Mazoku" metadata/MaxHp = 50 diff --git a/scripts/mod/ModLoader.cs b/scripts/mod/ModLoader.cs index eb7a1dc..0539d9b 100644 --- a/scripts/mod/ModLoader.cs +++ b/scripts/mod/ModLoader.cs @@ -1,7 +1,10 @@ using System; using System.Data; using System.IO; +using System.Linq; +using System.Reflection; using System.Runtime.Loader; +using System.Text; using ColdMint.scripts.debug; using ColdMint.scripts.utils; using Godot; @@ -98,52 +101,50 @@ public class ModLoader } LogCat.LogWithFormat("dll_name", LogCat.LogLabel.ModLoader, true, assemblyName); - // if (assemblyName != Config.SolutionName) - // { - // //If the load is not its own Dll file. - // //如果加载的不是自身的Dll文件. - // } + //If the load is not its own Dll file. + //如果加载的不是自身的Dll文件. + if (assemblyName == Config.SolutionName) + { + return; + } //Call the method of the entry class. //调用入口类的方法 - var modLifecycleHandlerFullName = assemblyName + "." + Config.ModLifecycleHandlerName; - var modLifecycleHandlerType = assembly.GetType(modLifecycleHandlerFullName); + var exportedTypes = assembly.GetExportedTypes(); + LogCat.LogWarningWithFormat("dll_type_length", LogCat.LogLabel.ModLoader, LogCat.UploadFormat, dllPath, + exportedTypes.Length); + var modLifecycleHandlerType = + FindTypeInTypeArray(exportedTypes, Config.ModLifecycleHandlerName); if (modLifecycleHandlerType == null) { //The module does not register a lifecycle processor. //模组没有注册生命周期处理器。 LogCat.LogWarningWithFormat("dll_does_not_register_lifecycle_processor", LogCat.LogLabel.ModLoader, LogCat.UploadFormat, - dllPath, modLifecycleHandlerFullName); + dllPath, Config.ModLifecycleHandlerName); + return; } - else + var constructor = modLifecycleHandlerType.GetConstructor(Type.EmptyTypes); + if (constructor == null) { - var constructor = modLifecycleHandlerType.GetConstructor(Type.EmptyTypes); - if (constructor == null) - { - //No parameterless constructor found. - //未找到无参构造方法。 - LogCat.LogWarningWithFormat("dll_no_parameterless_constructor", LogCat.LogLabel.ModLoader, - LogCat.UploadFormat, - dllPath); - } - else - { - var modLifecycleHandler = constructor.Invoke(null) as IModLifecycleHandler; - if (modLifecycleHandler == null) - { - //The ModLifecycleHandler class in the dll does not yet implement the IModLifecycleHandler interface. - //dll内的ModLifecycleHandler类尚未实现IModLifecycleHandler接口。 - LogCat.LogWarningWithFormat("mod_lifecycle_handler_not_implement_interface", - LogCat.LogLabel.ModLoader, - LogCat.UploadFormat, dllPath); - } - else - { - modLifecycleHandler.OnModLoaded(); - } - } + //No parameterless constructor found. + //未找到无参构造方法。 + LogCat.LogWarningWithFormat("dll_no_parameterless_constructor", LogCat.LogLabel.ModLoader, + LogCat.UploadFormat, + dllPath); + return; } + var modLifecycleHandler = constructor.Invoke(null); + var methodInfo = + modLifecycleHandlerType.GetMethod(nameof(IModLifecycleHandler.OnModLoaded)); + if (methodInfo == null) + { + LogCat.LogWarningWithFormat("mod_lifecycle_handler_not_implement_interface", + LogCat.LogLabel.ModLoader, + LogCat.UploadFormat, dllPath); + return; + } + methodInfo.Invoke(modLifecycleHandler, null); } catch (ArgumentNullException argumentNullException) { @@ -184,6 +185,25 @@ public class ModLoader LogCat.LogWithFormat("load_dll_success", LogCat.LogLabel.ModLoader, true, dllPath); } + + /// + /// Find a specific type by class name + /// 通过类名查找特定的类型 + /// + /// + ///TypeArray + ///类型数组 + /// + /// + ///ClassName + ///类名 + /// + /// + public static Type? FindTypeInTypeArray(Type[] types, string className) + { + return types.FirstOrDefault(type => type.Name == className); + } + /// /// Load all mods /// 加载全部模组