using System.Data; using Milimoe.FunGame.Core.Api.EntityFactory; using Milimoe.FunGame.Core.Api.OpenEntityAdapter; using Milimoe.FunGame.Core.Api.Transmittal; using Milimoe.FunGame.Core.Entity; using Milimoe.FunGame.Core.Library.Constant; using Milimoe.FunGame.Core.Library.Exception; using Milimoe.FunGame.Core.Library.SQLScript.Entity; namespace Milimoe.FunGame.Core.Api.Utility { public class Factory { /// /// 支持动态扩展的工厂实例 /// public static Factory OpenFactory { get; } = new(); private Factory() { } internal HashSet> CharacterFactories { get; } = []; internal HashSet> InventoryFactories { get; } = []; internal HashSet> SkillFactories { get; } = []; internal HashSet> EffectFactories { get; } = []; internal HashSet> ItemFactories { get; } = []; internal HashSet> RoomFactories { get; } = []; internal HashSet> UserFactories { get; } = []; public delegate T? EntityFactoryDelegate(long id, string name, Dictionary args); /// /// 注册工厂方法 /// /// /// public void RegisterFactory(EntityFactoryDelegate d) { if (typeof(T) == typeof(Character) && d is EntityFactoryDelegate character) { CharacterFactories.Add(character); } if (typeof(T) == typeof(Inventory) && d is EntityFactoryDelegate inventory) { InventoryFactories.Add(inventory); } if (typeof(T) == typeof(Skill) && d is EntityFactoryDelegate skill) { SkillFactories.Add(skill); } if (typeof(T) == typeof(Effect) && d is EntityFactoryDelegate effect) { EffectFactories.Add(effect); } if (typeof(T) == typeof(Item) && d is EntityFactoryDelegate item) { ItemFactories.Add(item); } if (typeof(T) == typeof(Room) && d is EntityFactoryDelegate room) { RoomFactories.Add(room); } if (typeof(T) == typeof(User) && d is EntityFactoryDelegate user) { UserFactories.Add(user); } } /// /// 构造一个实体实例 /// /// /// /// /// /// /// public T GetInstance(long id, string name, Dictionary args) { if (typeof(T) == typeof(Character)) { foreach (EntityFactoryDelegate d in CharacterFactories) { try { if (d.Invoke(id, name, args) is T character) { return character; } } catch (Exception e) { TXTHelper.AppendErrorLog(e.GetErrorInfo()); } } return (T)(object)GetCharacter(); } if (typeof(T) == typeof(Inventory)) { foreach (EntityFactoryDelegate d in InventoryFactories) { try { if (d.Invoke(id, name, args) is T inventory) { return inventory; } } catch (Exception e) { TXTHelper.AppendErrorLog(e.GetErrorInfo()); } } return (T)(object)GetInventory(); } if (typeof(T) == typeof(Skill)) { foreach (EntityFactoryDelegate d in SkillFactories) { try { if (d.Invoke(id, name, args) is T skill) { return skill; } } catch (Exception e) { TXTHelper.AppendErrorLog(e.GetErrorInfo()); } } Skill openSkill = new OpenSkill(id, name, args); if (args.TryGetValue("values", out object? value) && value is Dictionary dict) { foreach (string key in dict.Keys) { openSkill.Values[key] = dict[key]; } } return (T)(object)openSkill; } if (typeof(T) == typeof(Effect)) { foreach (EntityFactoryDelegate d in EffectFactories) { try { if (d.Invoke(id, name, args) is T effect) { return effect; } } catch (Exception e) { TXTHelper.AppendErrorLog(e.GetErrorInfo()); } } return (T)(object)GetEffect(); } if (typeof(T) == typeof(Item)) { foreach (EntityFactoryDelegate d in ItemFactories) { try { if (d.Invoke(id, name, args) is T item) { return item; } } catch (Exception e) { TXTHelper.AppendErrorLog(e.GetErrorInfo()); } } return (T)(object)new OpenItem(id, name, args); } if (typeof(T) == typeof(Room)) { foreach (EntityFactoryDelegate d in RoomFactories) { try { if (d.Invoke(id, name, args) is T room) { return room; } } catch (Exception e) { TXTHelper.AppendErrorLog(e.GetErrorInfo()); } } return (T)(object)GetRoom(); } if (typeof(T) == typeof(User)) { foreach (EntityFactoryDelegate d in UserFactories) { try { if (d.Invoke(id, name, args) is T user) { return user; } } catch (Exception e) { TXTHelper.AppendErrorLog(e.GetErrorInfo()); } } return (T)(object)GetUser(); } throw new NotSupportedInstanceClassException(); } /// /// 此方法使用 取得一个实体字典 /// /// /// /// /// public static Dictionary GetGameModuleInstances(string module_name, string file_name) where T : BaseEntity { EntityModuleConfig config = new(module_name, file_name); config.LoadConfig(); if (typeof(T) == typeof(Skill)) { OpenSkillAdapter.Adaptation(config); } if (typeof(T) == typeof(Item)) { OpenItemAdapter.Adaptation(config); } return config; } /// /// 使用 构造一个实体字典并保存 /// /// /// /// /// /// public static void CreateGameModuleEntityConfig(string module_name, string file_name, Dictionary dict) where T : BaseEntity { EntityModuleConfig config = new(module_name, file_name); foreach (string key in dict.Keys) { config[key] = dict[key]; } config.SaveConfig(); } internal HashSet SQLHelperFactories { get; } = []; public delegate SQLHelper? SQLHelperFactoryDelegate(); /// /// 注册工厂方法 [SQLHelper] /// /// public void RegisterFactory(SQLHelperFactoryDelegate d) { SQLHelperFactories.Add(d); } /// /// 构造一个 SQLHelper 实例 /// /// public SQLHelper? GetSQLHelper() { foreach (SQLHelperFactoryDelegate d in SQLHelperFactories) { try { if (d.Invoke() is SQLHelper helper) { return helper; } } catch (Exception e) { TXTHelper.AppendErrorLog(e.GetErrorInfo()); } } return null; } internal HashSet MailSenderFactories { get; } = []; public delegate MailSender? MailSenderFactoryDelegate(); /// /// 注册工厂方法 [MailSender] /// /// public void RegisterFactory(MailSenderFactoryDelegate d) { MailSenderFactories.Add(d); } /// /// 构造一个 MailSender 实例 /// /// public MailSender? GetMailSender() { foreach (MailSenderFactoryDelegate d in MailSenderFactories) { try { if (d.Invoke() is MailSender sender) { return sender; } } catch (Exception e) { TXTHelper.AppendErrorLog(e.GetErrorInfo()); } } return null; } private readonly static CharacterFactory CharacterFactory = new(); private readonly static InventoryFactory InventoryFactory = new(); private readonly static SkillFactory SkillFactory = new(); private readonly static EffectFactory EffectFactory = new(); private readonly static ItemFactory ItemFactory = new(); private readonly static RoomFactory RoomFactory = new(); private readonly static UserFactory UserFactory = new(); /// /// 获取角色实例 /// /// public static Character GetCharacter() { return CharacterFactory.Create(); } /// /// 获取库存实例 /// /// public static Inventory GetInventory() { return InventoryFactory.Create(); } /// /// 获取技能实例 /// /// public static Skill GetSkill() { return SkillFactory.Create(); } /// /// 获取技能特效实例 /// /// public static Effect GetEffect() { return EffectFactory.Create(); } /// /// 获取物品实例 /// /// public static Item GetItem() { return ItemFactory.Create(); } /// /// 获取房间实例 /// /// 房间内部序列号 /// 房间号 /// 创建时间 /// 房主 /// 房间类型 /// 游戏模组 /// /// 房间状态 /// /// 房间密码 /// 人数上限 /// public static Room GetRoom(long id = 0, string roomid = "-1", DateTime? createTime = null, User? roomMaster = null, RoomType roomType = RoomType.All, string gameModule = "", string gameMap = "", RoomState roomState = RoomState.Created, bool isRank = false, string password = "", int maxUsers = 4) { return RoomFactory.Create(id, roomid, createTime, roomMaster, roomType, gameModule, gameMap, roomState, isRank, password, maxUsers); } /// /// 通过DataSet获取房间实例 /// /// /// /// public static Room GetRoom(DataRow drRoom, User user) { Room room = General.HallInstance; if (drRoom != null) { long id = (long)drRoom[RoomQuery.Column_ID]; string roomid = (string)drRoom[RoomQuery.Column_RoomID]; DateTime createTime = (DateTime)drRoom[RoomQuery.Column_CreateTime]; User roomMaster = user; RoomType roomType = (RoomType)Convert.ToInt32(drRoom[RoomQuery.Column_RoomType]); string gameModule = (string)drRoom[RoomQuery.Column_GameModule]; string gameMap = (string)drRoom[RoomQuery.Column_GameMap]; RoomState roomState = (RoomState)Convert.ToInt32(drRoom[RoomQuery.Column_RoomState]); bool isRank = Convert.ToInt32(drRoom[RoomQuery.Column_IsRank]) == 1; string password = (string)drRoom[RoomQuery.Column_Password]; int maxUsers = (int)drRoom[RoomQuery.Column_MaxUsers]; room = GetRoom(id, roomid, createTime, roomMaster, roomType, gameModule, gameMap, roomState, isRank, password, maxUsers); } return room; } /// /// 通过DataSet获取房间列表 /// /// /// /// public static List GetRooms(DataSet dsRoom, DataSet dsUser) { List list = [ General.HallInstance ]; if (dsRoom != null && dsRoom.Tables[0].Rows.Count > 0) { foreach (DataRow drRoom in dsRoom.Tables[0].Rows) { long Id = (long)drRoom[RoomQuery.Column_ID]; string Roomid = (string)drRoom[RoomQuery.Column_RoomID]; DateTime createTime = (DateTime)drRoom[RoomQuery.Column_CreateTime]; User roomMaster = General.UnknownUserInstance; if (dsUser != null && dsUser.Tables.Count > 0) { DataRow[] rows = dsUser.Tables[0].Select($"{UserQuery.Column_UID} = {(long)drRoom[RoomQuery.Column_RoomMaster]}"); if (rows.Length > 0) { roomMaster = GetUser(rows[0]); } } RoomType roomType = (RoomType)Convert.ToInt32(drRoom[RoomQuery.Column_RoomType]); string gameModule = (string)drRoom[RoomQuery.Column_GameModule]; string gameMap = (string)drRoom[RoomQuery.Column_GameMap]; RoomState roomState = (RoomState)Convert.ToInt32(drRoom[RoomQuery.Column_RoomState]); bool isRank = Convert.ToInt32(drRoom[RoomQuery.Column_IsRank]) == 1; string password = (string)drRoom[RoomQuery.Column_Password]; list.Add(GetRoom(Id, Roomid, createTime, roomMaster, roomType, gameModule, gameMap, roomState, isRank, password)); } } return list; } /// /// 获取大厅(-1号房) /// /// public static Room GetHall() { return RoomFactory.Create(); } /// /// 获取用户实例 /// /// public static User GetUser() { return UserFactory.Create(); } /// /// 获取用户实例 /// /// /// /// /// /// /// /// /// /// /// /// /// public static User GetUser(long Id = 0, string Username = "", DateTime? RegTime = null, DateTime? LastTime = null, string Email = "", string NickName = "", bool IsAdmin = false, bool IsOperator = false, bool IsEnable = true, double GameTime = 0, string AutoKey = "") { return UserFactory.Create(Id, Username, RegTime, LastTime, Email, NickName, IsAdmin, IsOperator, IsEnable, GameTime, AutoKey); } /// /// 获取用户实例 /// /// /// public static User GetUser(DataSet ds) { if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0) { return GetUser(ds.Tables[0].Rows[0]); } return UserFactory.Create(); } /// /// 获取用户实例 /// /// /// public static User GetUser(DataRow dr) { if (dr != null) { long Id = (long)dr[UserQuery.Column_UID]; string Username = (string)dr[UserQuery.Column_Username]; DateTime RegTime = (DateTime)dr[UserQuery.Column_RegTime]; DateTime LastTime = (DateTime)dr[UserQuery.Column_LastTime]; string Email = (string)dr[UserQuery.Column_Email]; string NickName = (string)dr[UserQuery.Column_Nickname]; bool IsAdmin = Convert.ToInt32(dr[UserQuery.Column_IsAdmin]) == 1; bool IsOperator = Convert.ToInt32(dr[UserQuery.Column_IsOperator]) == 1; bool IsEnable = Convert.ToInt32(dr[UserQuery.Column_IsEnable]) == 1; double GameTime = Convert.ToDouble(dr[UserQuery.Column_GameTime]); string AutoKey = (string)dr[UserQuery.Column_AutoKey]; return UserFactory.Create(Id, Username, RegTime, LastTime, Email, NickName, IsAdmin, IsOperator, IsEnable, GameTime, AutoKey); } return UserFactory.Create(); } } }