Support to print color logs, join Mod lifecycle processor.

支持打印彩色日志,加入Mod生命周期处理器。
This commit is contained in:
Cold-Mint 2024-07-24 23:29:22 +08:00
parent 549248cf6b
commit f0f954f10c
Signed by: Cold-Mint
GPG Key ID: C5A9BF8A98E0CE99
8 changed files with 104 additions and 19 deletions

View File

@ -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.,所有者が空です。
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ファイルにはパラメータのないコンストラクタがありません。
1 id zh en ja
104 log_load_pck_failed 加载PCK文件{0}失败。 Failed to load the PCK file {0}. PCKファイル{0}のロードに失敗しました。
105 log_projectile_container_is_null 抛射体容器为空。 Projectile container is null. 射出体コンテナが空です。
106 log_marker2d_is_null Marker2D为空。 Marker2D is null. Marker2Dが空です。
107 log_owner_is_null 所有者为空。 Owner is null. 所有者が空です。
108 log_dll_name 已加载{0}。 Loaded {0}. {0}をロードしました。
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ファイルにはパラメータのないコンストラクタがありません。

View File

@ -95,12 +95,18 @@ public static class Config
/// <para>公司/创作者名字</para>
/// </summary>
public const string CompanyName = "ColdMint";
/// <summary>
/// <para>Module life handler name</para>
/// <para>模组生命周期处理器名称</para>
/// </summary>
public const string ModLifecycleHandlerName = "ModLifecycleHandler";
/// <summary>
/// <para>Solution Name</para>
/// <para>解决方案名称</para>
/// </summary>
public static string SolutionName = "ColdMint.Traveler";
public const string SolutionName = "ColdMint.Traveler";
/// <summary>
/// <para>How many item slots are there on the shortcut bar</para>

View File

@ -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);

View File

@ -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));
}
}

View File

@ -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;
}

View File

@ -0,0 +1,14 @@
namespace ColdMint.scripts.mod;
/// <summary>
/// <para>Mod life cycle handler</para>
/// <para>模组生命周期处理器</para>
/// </summary>
public interface IModLifecycleHandler
{
/// <summary>
/// <para>When loading the Mod</para>
/// <para>当加载Mod时</para>
/// </summary>
void OnModLoaded();
}

View File

@ -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
{

View File

@ -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;