using System.Collections; using System.Reflection; using Milimoe.FunGame.Core.Entity; using Milimoe.FunGame.Core.Library.Common.Addon; using Milimoe.FunGame.Core.Library.Constant; namespace Milimoe.FunGame.Core.Service { internal class AddonManager { /// /// 从plugins目录加载所有插件 /// /// /// /// /// internal static Dictionary LoadPlugins(Dictionary plugins, Hashtable delegates, params object[] otherobjs) { if (!Directory.Exists(ReflectionSet.PluginFolderPath)) return plugins; string[] dlls = Directory.GetFiles(ReflectionSet.PluginFolderPath, "*.dll"); foreach (string dll in dlls) { // 加载目录下所有的DLL Assembly assembly = Assembly.LoadFrom(dll); // 遍历DLL中继承了Plugin的类型 foreach (Type type in assembly.GetTypes().AsEnumerable().Where(type => type.IsSubclassOf(typeof(Plugin)))) { Plugin? instance = (Plugin?)Activator.CreateInstance(type); if (instance != null && instance.Load(otherobjs) && instance.Name.Trim() != "") { instance.Controller = new(instance, delegates); plugins.TryAdd(instance.Name, instance); } } } return plugins; } /// /// 从modules目录加载所有模组 /// /// /// /// /// /// /// /// internal static Dictionary LoadGameModules(Dictionary modules, List characters, List skills, List items, Hashtable delegates, params object[] otherobjs) { if (!Directory.Exists(ReflectionSet.GameModuleFolderPath)) return modules; string[] dlls = Directory.GetFiles(ReflectionSet.GameModuleFolderPath, "*.dll"); foreach (string dll in dlls) { Assembly assembly = Assembly.LoadFrom(dll); foreach (Type type in assembly.GetTypes().AsEnumerable().Where(type => type.IsSubclassOf(typeof(GameModule)))) { GameModule? instance = (GameModule?)Activator.CreateInstance(type); if (instance != null && instance.Load(otherobjs) && instance.Name.Trim() != "") { instance.Controller = new(instance, delegates); modules.TryAdd(instance.Name, instance); } } foreach (Type type in assembly.GetTypes().AsEnumerable().Where(type => type.IsSubclassOf(typeof(Character)))) { Character? instance = (Character?)Activator.CreateInstance(type); if (instance != null && instance.Name.Trim() != "" && !characters.Contains(instance)) { characters.Add(instance); } } foreach (Type type in assembly.GetTypes().AsEnumerable().Where(type => type.IsSubclassOf(typeof(Skill)))) { Skill? instance = (Skill?)Activator.CreateInstance(type); if (instance != null && instance.Name.Trim() != "" && !skills.Contains(instance)) { skills.Add(instance); } } foreach (Type type in assembly.GetTypes().AsEnumerable().Where(type => type.IsSubclassOf(typeof(Item)))) { Item? instance = (Item?)Activator.CreateInstance(type); if (instance != null && instance.Name.Trim() != "" && !items.Contains(instance)) { items.Add(instance); } } } return modules; } /// /// 从modules目录加载所有适用于服务器的模组 /// /// /// /// /// /// /// /// internal static Dictionary LoadGameModulesForServer(Dictionary modules, List characters, List skills, List items, Hashtable delegates, params object[] otherobjs) { if (!Directory.Exists(ReflectionSet.GameModuleFolderPath)) return modules; string[] dlls = Directory.GetFiles(ReflectionSet.GameModuleFolderPath, "*.dll"); foreach (string dll in dlls) { Assembly assembly = Assembly.LoadFrom(dll); foreach (Type type in assembly.GetTypes().AsEnumerable().Where(type => type.IsSubclassOf(typeof(GameModuleServer)))) { GameModuleServer? instance = (GameModuleServer?)Activator.CreateInstance(type); if (instance != null && instance.Load(otherobjs) && instance.Name.Trim() != "") { instance.Controller = new(instance, delegates); modules.TryAdd(instance.Name, instance); } } foreach (Type type in assembly.GetTypes().AsEnumerable().Where(type => type.IsSubclassOf(typeof(Character)))) { Character? instance = (Character?)Activator.CreateInstance(type); if (instance != null && instance.Name.Trim() != "" && !characters.Contains(instance)) { characters.Add(instance); } } foreach (Type type in assembly.GetTypes().AsEnumerable().Where(type => type.IsSubclassOf(typeof(Skill)))) { Skill? instance = (Skill?)Activator.CreateInstance(type); if (instance != null && instance.Name.Trim() != "" && !skills.Contains(instance)) { skills.Add(instance); } } foreach (Type type in assembly.GetTypes().AsEnumerable().Where(type => type.IsSubclassOf(typeof(Item)))) { Item? instance = (Item?)Activator.CreateInstance(type); if (instance != null && instance.Name.Trim() != "" && !items.Contains(instance)) { items.Add(instance); } } } return modules; } /// /// 从maps目录加载所有地图 /// /// /// /// internal static Dictionary LoadGameMaps(Dictionary maps, params object[] objs) { if (!Directory.Exists(ReflectionSet.GameMapFolderPath)) return maps; string[] dlls = Directory.GetFiles(ReflectionSet.GameMapFolderPath, "*.dll"); foreach (string dll in dlls) { Assembly assembly = Assembly.LoadFrom(dll); foreach (Type type in assembly.GetTypes().AsEnumerable().Where(type => type.IsSubclassOf(typeof(GameMap)))) { GameMap? instance = (GameMap?)Activator.CreateInstance(type); if (instance != null && instance.Load(objs) && instance.Name.Trim() != "") { maps.TryAdd(instance.Name, instance); } } } return maps; } } }