using Milimoe.FunGame.Core.Library.Common.Addon;
using Milimoe.FunGame.Core.Library.Constant;
using Milimoe.FunGame.Core.Service;
namespace Milimoe.FunGame.Core.Api.Utility
{
public class GameModuleLoader
{
///
/// 适用于客户端的模组集
///
public Dictionary Modules { get; } = [];
///
/// 适用于服务器的模组集
///
public Dictionary ModuleServers { get; } = [];
///
/// 游戏地图集
///
public Dictionary Maps { get; } = [];
///
/// 角色表
///
public Dictionary Characters { get; } = [];
///
/// 技能表
///
public Dictionary Skills { get; } = [];
///
/// 物品表
///
public Dictionary Items { get; } = [];
///
/// 已加载的模组DLL名称对应的路径
///
public Dictionary ModuleFilePaths => IsHotLoadMode ? new(HotLoadAddonManager.ModuleFilePaths) : new(AddonManager.ModuleFilePaths);
///
/// 使用可热更新的加载项模式
///
public bool IsHotLoadMode { get; } = false;
private GameModuleLoader(bool hotMode = false)
{
IsHotLoadMode = hotMode;
}
///
/// 传入 类型来创建指定端的模组读取器
/// runtime = 时,仅读取
/// runtime = 时,仅读取
/// 都会读取
///
/// 传入 类型来创建指定端的模组读取器
/// 用于构建
/// 其他需要传入给插件初始化的对象
///
public static GameModuleLoader LoadGameModules(FunGameInfo.FunGame runtime, Dictionary delegates, params object[] otherobjs)
{
GameModuleLoader loader = new();
if (runtime == FunGameInfo.FunGame.FunGame_Desktop)
{
AddonManager.LoadGameMaps(loader.Maps, otherobjs);
foreach (GameMap map in loader.Maps.Values.ToList())
{
map.ModuleLoader = loader;
map.AfterLoad(loader, otherobjs);
}
AddonManager.LoadGameModules(loader.Modules, loader.Characters, loader.Skills, loader.Items, delegates, otherobjs);
foreach (GameModule module in loader.Modules.Values.ToList())
{
module.ModuleLoader = loader;
// 读取模组的依赖集合
module.GameModuleDepend.GetDependencies(loader);
// 如果模组加载后需要执行代码,请重写AfterLoad方法
module.AfterLoad(loader, otherobjs);
}
}
else if (runtime == FunGameInfo.FunGame.FunGame_Server)
{
AddonManager.LoadGameMaps(loader.Maps, otherobjs);
foreach (GameMap map in loader.Maps.Values.ToList())
{
map.ModuleLoader = loader;
map.AfterLoad(loader, otherobjs);
}
AddonManager.LoadGameModulesForServer(loader.ModuleServers, loader.Characters, loader.Skills, loader.Items, delegates, otherobjs);
foreach (GameModuleServer server in loader.ModuleServers.Values.ToList())
{
server.ModuleLoader = loader;
server.GameModuleDepend.GetDependencies(loader);
server.AfterLoad(loader, otherobjs);
}
}
return loader;
}
///
/// 传入 类型来创建指定端的模组读取器 [ 可热更新模式 ]
/// runtime = 时,仅读取
/// runtime = 时,仅读取
/// 都会读取
///
/// 传入 类型来创建指定端的模组读取器
/// 用于构建
/// 其他需要传入给插件初始化的对象
///
public static GameModuleLoader LoadGameModulesByHotLoadMode(FunGameInfo.FunGame runtime, Dictionary delegates, params object[] otherobjs)
{
GameModuleLoader loader = new(true);
if (runtime == FunGameInfo.FunGame.FunGame_Desktop)
{
List updated = HotLoadAddonManager.LoadGameMaps(loader.Maps, otherobjs);
foreach (GameMap map in updated)
{
map.ModuleLoader = loader;
map.AfterLoad(loader, otherobjs);
}
List updatedModule = HotLoadAddonManager.LoadGameModules(loader.Modules, loader.Characters, loader.Skills, loader.Items, delegates, otherobjs);
foreach (GameModule module in updatedModule)
{
module.ModuleLoader = loader;
// 读取模组的依赖集合
module.GameModuleDepend.GetDependencies(loader);
// 如果模组加载后需要执行代码,请重写AfterLoad方法
module.AfterLoad(loader, otherobjs);
}
}
else if (runtime == FunGameInfo.FunGame.FunGame_Server)
{
List updated = HotLoadAddonManager.LoadGameMaps(loader.Maps, otherobjs);
foreach (GameMap map in updated)
{
map.ModuleLoader = loader;
map.AfterLoad(loader, otherobjs);
}
List updatedServer = HotLoadAddonManager.LoadGameModulesForServer(loader.ModuleServers, loader.Characters, loader.Skills, loader.Items, delegates, otherobjs);
foreach (GameModuleServer server in updatedServer)
{
server.ModuleLoader = loader;
server.GameModuleDepend.GetDependencies(loader);
server.AfterLoad(loader, otherobjs);
}
}
return loader;
}
///
/// 热更新
///
///
///
///
public void HotReload(FunGameInfo.FunGame runtime, Dictionary delegates, params object[] otherobjs)
{
if (!IsHotLoadMode) return;
if (runtime == FunGameInfo.FunGame.FunGame_Desktop)
{
List updated = HotLoadAddonManager.LoadGameMaps(Maps, otherobjs);
foreach (GameMap map in updated)
{
map.ModuleLoader = this;
map.AfterLoad(this, otherobjs);
}
List updatedModule = HotLoadAddonManager.LoadGameModules(Modules, Characters, Skills, Items, delegates, otherobjs);
foreach (GameModule module in updatedModule)
{
module.ModuleLoader = this;
// 读取模组的依赖集合
module.GameModuleDepend.GetDependencies(this);
// 如果模组加载后需要执行代码,请重写AfterLoad方法
module.AfterLoad(this, otherobjs);
}
}
else if (runtime == FunGameInfo.FunGame.FunGame_Server)
{
List updated = HotLoadAddonManager.LoadGameMaps(Maps, otherobjs);
foreach (GameMap map in updated)
{
map.ModuleLoader = this;
map.AfterLoad(this, otherobjs);
}
List updatedServer = HotLoadAddonManager.LoadGameModulesForServer(ModuleServers, Characters, Skills, Items, delegates, otherobjs);
foreach (GameModuleServer server in updatedServer)
{
server.ModuleLoader = this;
server.GameModuleDepend.GetDependencies(this);
server.AfterLoad(this, otherobjs);
}
}
}
///
/// 获取对应名称的模组实例
/// 如果需要取得服务器模组的实例,请调用
///
///
///
public GameModule this[string name]
{
get
{
return Modules[name];
}
set
{
Modules.TryAdd(name, value);
}
}
///
/// 获取对应名称的服务器模组实例
///
///
///
public GameModuleServer GetServerMode(string name)
{
return ModuleServers[name];
}
///
/// 获取对应名称的游戏地图
///
///
///
public GameMap GetGameMap(string name)
{
return Maps[name];
}
}
}