ModLoader now support calling entry functions.

模组加载器现在支持调用入口函数了。
This commit is contained in:
Cold-Mint 2024-07-27 22:06:10 +08:00
parent a7a5a309a2
commit d95515fdb7
Signed by: Cold-Mint
GPG Key ID: C5A9BF8A98E0CE99
3 changed files with 57 additions and 35 deletions

View File

@ -109,3 +109,4 @@ 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_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_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ファイルにはパラメータのないコンストラクタがありません。 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}個のタイプが含まれています。
1 id zh en ja
109 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}を登録していません。
110 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インターフェースを実装していません。
111 log_dll_no_parameterless_constructor 位于{0}的dll文件,没有无参构造函数。 The DLL file located at {0} does not have a parameterless constructor. {0}にあるDLLファイルにはパラメータのないコンストラクタがありません。
112 log_dll_type_length 位于{0}的dll文件内,共包含{1}个类型。 The DLL file located at {0} contains a total of {1} types. {0}にあるDLLファイルには合計{1}個のタイプが含まれています。

View File

@ -34,6 +34,7 @@ radius = 172.29
collision_layer = 64 collision_layer = 64
collision_mask = 38 collision_mask = 38
script = ExtResource("1_ubaid") script = ExtResource("1_ubaid")
InitWeaponRes = null
LootListId = "test" LootListId = "test"
metadata/CampId = "Mazoku" metadata/CampId = "Mazoku"
metadata/MaxHp = 50 metadata/MaxHp = 50

View File

@ -1,7 +1,10 @@
using System; using System;
using System.Data; using System.Data;
using System.IO; using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.Loader; using System.Runtime.Loader;
using System.Text;
using ColdMint.scripts.debug; using ColdMint.scripts.debug;
using ColdMint.scripts.utils; using ColdMint.scripts.utils;
using Godot; using Godot;
@ -98,52 +101,50 @@ public class ModLoader
} }
LogCat.LogWithFormat("dll_name", LogCat.LogLabel.ModLoader, true, assemblyName); 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. if (assemblyName == Config.SolutionName)
// //如果加载的不是自身的Dll文件. {
// } return;
}
//Call the method of the entry class. //Call the method of the entry class.
//调用入口类的方法 //调用入口类的方法
var modLifecycleHandlerFullName = assemblyName + "." + Config.ModLifecycleHandlerName; var exportedTypes = assembly.GetExportedTypes();
var modLifecycleHandlerType = assembly.GetType(modLifecycleHandlerFullName); LogCat.LogWarningWithFormat("dll_type_length", LogCat.LogLabel.ModLoader, LogCat.UploadFormat, dllPath,
exportedTypes.Length);
var modLifecycleHandlerType =
FindTypeInTypeArray(exportedTypes, Config.ModLifecycleHandlerName);
if (modLifecycleHandlerType == null) if (modLifecycleHandlerType == null)
{ {
//The module does not register a lifecycle processor. //The module does not register a lifecycle processor.
//模组没有注册生命周期处理器。 //模组没有注册生命周期处理器。
LogCat.LogWarningWithFormat("dll_does_not_register_lifecycle_processor", LogCat.LogLabel.ModLoader, LogCat.LogWarningWithFormat("dll_does_not_register_lifecycle_processor", LogCat.LogLabel.ModLoader,
LogCat.UploadFormat, LogCat.UploadFormat,
dllPath, modLifecycleHandlerFullName); dllPath, Config.ModLifecycleHandlerName);
return;
} }
else var constructor = modLifecycleHandlerType.GetConstructor(Type.EmptyTypes);
if (constructor == null)
{ {
var constructor = modLifecycleHandlerType.GetConstructor(Type.EmptyTypes); //No parameterless constructor found.
if (constructor == null) //未找到无参构造方法。
{ LogCat.LogWarningWithFormat("dll_no_parameterless_constructor", LogCat.LogLabel.ModLoader,
//No parameterless constructor found. LogCat.UploadFormat,
//未找到无参构造方法。 dllPath);
LogCat.LogWarningWithFormat("dll_no_parameterless_constructor", LogCat.LogLabel.ModLoader, return;
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();
}
}
} }
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) catch (ArgumentNullException argumentNullException)
{ {
@ -184,6 +185,25 @@ public class ModLoader
LogCat.LogWithFormat("load_dll_success", LogCat.LogLabel.ModLoader, true, dllPath); LogCat.LogWithFormat("load_dll_success", LogCat.LogLabel.ModLoader, true, dllPath);
} }
/// <summary>
/// <para>Find a specific type by class name</para>
/// <para>通过类名查找特定的类型</para>
/// </summary>
/// <param name="types">
///<para>TypeArray</para>
///<para>类型数组</para>
/// </param>
/// <param name="className">
///<para>ClassName</para>
///<para>类名</para>
/// </param>
/// <returns></returns>
public static Type? FindTypeInTypeArray(Type[] types, string className)
{
return types.FirstOrDefault(type => type.Name == className);
}
/// <summary> /// <summary>
/// <para>Load all mods</para> /// <para>Load all mods</para>
/// <para>加载全部模组</para> /// <para>加载全部模组</para>