From f0f954f10c83ba364305f879b6eae574a09cb718 Mon Sep 17 00:00:00 2001 From: Cold-Mint Date: Wed, 24 Jul 2024 23:29:22 +0800 Subject: [PATCH] =?UTF-8?q?Support=20to=20print=20color=20logs,=20join=20M?= =?UTF-8?q?od=20lifecycle=20processor.=20=E6=94=AF=E6=8C=81=E6=89=93?= =?UTF-8?q?=E5=8D=B0=E5=BD=A9=E8=89=B2=E6=97=A5=E5=BF=97=EF=BC=8C=E5=8A=A0?= =?UTF-8?q?=E5=85=A5Mod=E7=94=9F=E5=91=BD=E5=91=A8=E6=9C=9F=E5=A4=84?= =?UTF-8?q?=E7=90=86=E5=99=A8=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- locals/Log.csv | 6 +- scripts/Config.cs | 8 ++- scripts/debug/LogCat.cs | 9 ++- scripts/loader/uiLoader/SplashScreenLoader.cs | 20 +++--- scripts/map/MapGenerator.cs | 2 +- scripts/mod/IModLifecycleHandler.cs | 14 +++++ scripts/mod/ModLoader.cs | 62 ++++++++++++++++++- scripts/utils/NodeUtils.cs | 2 +- 8 files changed, 104 insertions(+), 19 deletions(-) create mode 100644 scripts/mod/IModLifecycleHandler.cs diff --git a/locals/Log.csv b/locals/Log.csv index c4bf743..3fcfc49 100644 --- a/locals/Log.csv +++ b/locals/Log.csv @@ -104,4 +104,8 @@ log_load_pck_success,成功加载位于{0}的PCK文件。,Successfully loaded th log_load_pck_failed,加载PCK文件{0}失败。,Failed to load the PCK file {0}.,PCKファイル{0}のロードに失敗しました。 log_projectile_container_is_null,抛射体容器为空。,Projectile container is null.,射出体コンテナが空です。 log_marker2d_is_null,Marker2D为空。,Marker2D is null.,Marker2Dが空です。 -log_owner_is_null,所有者为空。,Owner is null.,所有者が空です。 \ No newline at end of file +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 diff --git a/scripts/Config.cs b/scripts/Config.cs index 716823d..259f037 100644 --- a/scripts/Config.cs +++ b/scripts/Config.cs @@ -95,12 +95,18 @@ public static class Config /// 公司/创作者名字 /// public const string CompanyName = "ColdMint"; + + /// + /// Module life handler name + /// 模组生命周期处理器名称 + /// + public const string ModLifecycleHandlerName = "ModLifecycleHandler"; /// /// Solution Name /// 解决方案名称 /// - public static string SolutionName = "ColdMint.Traveler"; + public const string SolutionName = "ColdMint.Traveler"; /// /// How many item slots are there on the shortcut bar diff --git a/scripts/debug/LogCat.cs b/scripts/debug/LogCat.cs index 607da7a..3506362 100644 --- a/scripts/debug/LogCat.cs +++ b/scripts/debug/LogCat.cs @@ -249,11 +249,14 @@ public static class LogCat switch (level) { + case WarningLogLevel: + GD.PrintRich("[color=#FFDE66]"+concreteLog+"[/color]"); + break; case ErrorLogLevel: - GD.PrintErr(concreteLog); + GD.PrintRich("[color=#FF4E39]"+concreteLog+"[/color]"); break; default: - GD.Print(concreteLog); + GD.PrintRich("[color=#CCCED1]"+concreteLog+"[/color]"); break; } } @@ -293,7 +296,7 @@ public static class LogCat upload); } - public static void LogWarningWithFormat(string message, bool upload, string label, params object?[] args) + public static void LogWarningWithFormat(string message, string label, bool upload, params object?[] args) { PrintLog(WarningLogLevel, string.Format(HandleMessage(WarningLogLevel, message, label).ToString(), args), label, upload); diff --git a/scripts/loader/uiLoader/SplashScreenLoader.cs b/scripts/loader/uiLoader/SplashScreenLoader.cs index 464eb90..4f6a0dc 100644 --- a/scripts/loader/uiLoader/SplashScreenLoader.cs +++ b/scripts/loader/uiLoader/SplashScreenLoader.cs @@ -89,21 +89,14 @@ public partial class SplashScreenLoader : UiLoaderTemplate //Register the corresponding encoding provider to solve the problem of garbled Chinese path of the compressed package //注册对应的编码提供程序,解决压缩包中文路径乱码问题 Encoding.RegisterProvider(CodePagesEncodingProvider.Instance); + //Create a game data folder //创建游戏数据文件夹 var dataPath = Config.GetGameDataDirectory(); if (!Directory.Exists(dataPath)) { Directory.CreateDirectory(dataPath); } - - ModLoader.Init(); - var modPath = Config.GetModDataDirectory(); - if (!Directory.Exists(modPath)) - { - Directory.CreateDirectory(modPath); - } - - ModLoader.LoadAllMods(modPath); + //Registered camp //注册阵营 var defaultCamp = new Camp(Config.CampId.Default) @@ -123,6 +116,15 @@ public partial class SplashScreenLoader : UiLoaderTemplate ItemTypeRegister.StaticRegister(); //静态注册掉落表 LootRegister.StaticRegister(); + //Load mod + //加载模组 + var modPath = Config.GetModDataDirectory(); + if (!Directory.Exists(modPath)) + { + Directory.CreateDirectory(modPath); + } + ModLoader.Init(); + ModLoader.LoadAllMods(modPath); await Task.Delay(TimeSpan.FromMilliseconds(500)); } } \ No newline at end of file diff --git a/scripts/map/MapGenerator.cs b/scripts/map/MapGenerator.cs index a2b6b44..623012c 100644 --- a/scripts/map/MapGenerator.cs +++ b/scripts/map/MapGenerator.cs @@ -330,7 +330,7 @@ public static class MapGenerator if (!await _roomPlacementStrategy.PlaceRoom(_mapRoot, roomPlacementData)) { - LogCat.LogWarningWithFormat("room_placement_failed", LogCat.UploadFormat, LogCat.LogLabel.Default, + LogCat.LogWarningWithFormat("room_placement_failed", LogCat.LogLabel.Default, LogCat.UploadFormat, roomNodeDataId); return false; } diff --git a/scripts/mod/IModLifecycleHandler.cs b/scripts/mod/IModLifecycleHandler.cs new file mode 100644 index 0000000..4f0725e --- /dev/null +++ b/scripts/mod/IModLifecycleHandler.cs @@ -0,0 +1,14 @@ +namespace ColdMint.scripts.mod; + +/// +/// Mod life cycle handler +/// 模组生命周期处理器 +/// +public interface IModLifecycleHandler +{ + /// + /// When loading the Mod + /// 当加载Mod时 + /// + void OnModLoaded(); +} \ No newline at end of file diff --git a/scripts/mod/ModLoader.cs b/scripts/mod/ModLoader.cs index 173005f..8f65d16 100644 --- a/scripts/mod/ModLoader.cs +++ b/scripts/mod/ModLoader.cs @@ -1,6 +1,7 @@ using System; using System.Data; using System.IO; +using System.Reflection; using System.Runtime.Loader; using ColdMint.scripts.debug; using ColdMint.scripts.utils; @@ -90,7 +91,60 @@ public class ModLoader LogCat.LogWithFormat("load_dll", LogCat.LogLabel.ModLoader, true, dllPath); try { - _assemblyLoadContext.LoadFromAssemblyPath(dllPath); + var assembly = _assemblyLoadContext.LoadFromAssemblyPath(dllPath); + var assemblyName = assembly.GetName().Name; + if (assemblyName == null) + { + return; + } + + LogCat.LogWithFormat("dll_name", LogCat.LogLabel.ModLoader, true, assemblyName); + // if (assemblyName != Config.SolutionName) + // { + // //If the load is not its own Dll file. + // //如果加载的不是自身的Dll文件. + // } + + //Call the method of the entry class. + //调用入口类的方法 + var modLifecycleHandlerFullName = assemblyName + "." + Config.ModLifecycleHandlerName; + var modLifecycleHandlerType = assembly.GetType(modLifecycleHandlerFullName); + 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); + } + else + { + 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(); + } + } + } } catch (ArgumentNullException argumentNullException) { @@ -201,7 +255,8 @@ public class ModLoader { //The module does not contain a dll file. //模组不包含dll文件。 - LogCat.LogWarningWithFormat("mod_not_contain_dll", true, LogCat.LogLabel.ModLoader, modFolderPath); + LogCat.LogWarningWithFormat("mod_not_contain_dll", LogCat.LogLabel.ModLoader, LogCat.UploadFormat, + modFolderPath); } else { @@ -219,7 +274,8 @@ public class ModLoader { //The module does not contain a pck file. //模组不包含pck文件。 - LogCat.LogWarningWithFormat("mod_not_contain_pck", true, LogCat.LogLabel.ModLoader, modFolderPath); + LogCat.LogWarningWithFormat("mod_not_contain_pck", LogCat.LogLabel.ModLoader, LogCat.UploadFormat, + modFolderPath); } else { diff --git a/scripts/utils/NodeUtils.cs b/scripts/utils/NodeUtils.cs index 3a24ddb..c0514f8 100644 --- a/scripts/utils/NodeUtils.cs +++ b/scripts/utils/NodeUtils.cs @@ -284,7 +284,7 @@ public static class NodeUtils if (node is T result) return result; // If the transformation fails, release the created node //如果转型失败,释放所创建的节点 - LogCat.LogWarningWithFormat("warning_node_cannot_cast_to", LogCat.UploadFormat, LogCat.LogLabel.Default, node, + LogCat.LogWarningWithFormat("warning_node_cannot_cast_to", LogCat.LogLabel.Default,LogCat.UploadFormat, node, nameof(T)); node.QueueFree(); return null;