using System; using System.Collections.Generic; using System.Text; using System.IO; using CommonLang; using CommonLang.IO; using CommonLang.IO.Attribute; using System.Runtime.Serialization; using System.Xml.Serialization; using CommonAI.Zone.Instance; using CommonAI.Zone.Formula; using CommonLang.Property; using CommonLang.Log; using CommonLang.Xml; using CommonAI.Zone.Helper; using CommonLang.Concurrent; using System.Xml; using CommonLang.Protocol; using CommonAI.Zone.Simple; namespace CommonAI.Zone { public class TemplateManager { //---------------------------------------------------------------------------------------------- #region _STATIC_ private static bool s_IsEditor = false; private static InstanceZoneFactory s_Factory = new SimpleInstanceZoneFactory(); private static MessageFactoryGenerator s_Codec = new MessageFactoryGenerator(); public static bool IsEditor { get { return s_IsEditor; } set { s_IsEditor = value; } } public static IFormula Formula { get { return s_Factory.Formula; } } public static MessageFactoryGenerator MessageCodec { get { return s_Codec; } } public static InstanceZoneFactory Factory { get { return s_Factory; } } public static void setFactory(InstanceZoneFactory factory) { TemplateManager.s_Factory = factory; TemplateManager.s_Codec.RegistAssembly(AppDomain.CurrentDomain.GetAssemblies()); ZoneEditor.EventTrigger.GameFields.InitFiledManager(); } public static InstanceZoneFactory LoadFactory(string plugin) { Type type = ReflectionUtil.GetType(plugin); InstanceZoneFactory factory = (InstanceZoneFactory)ReflectionUtil.CreateInstance(type); TemplateManager.setFactory(factory); return factory; } #endregion //---------------------------------------------------------------------------------------------- private Config mConfig; private ICommonConfig mExtConfig; private TerrainDefinitionMap mTerrainDefinition; private UnitActionDefinitionMap mUnitActionDefinition; private HashMap mUnitActionMap = new HashMap(); private HashMap mUnits = new HashMap(); private HashMap mSkills = new HashMap(); private HashMap mSpells = new HashMap(); private HashMap mBuffs = new HashMap(); private HashMap mItems = new HashMap(); private HashMap mUnitTriggers = new HashMap(); private HashMap mUnitEvents = new HashMap(); //---------------------------------------------------------------------------------------------- public TemplateManager() { mConfig = new Config(); this.mExtConfig = s_Factory.CreateCommonCFG(); mTerrainDefinition = new TerrainDefinitionMap(); mUnitActionDefinition = new UnitActionDefinitionMap(); } public string ResourceVersion { get; internal set; } public Config CFG { get { return mConfig; } internal set { mConfig = value; } } public ICommonConfig ExtConfig { get { return mExtConfig; } internal set { mExtConfig = value; } } public TerrainDefinitionMap TerrainDefinition { get { return mTerrainDefinition; } internal set { mTerrainDefinition = value; } } public UnitActionDefinitionMap UnitActionDefinition { get { return mUnitActionDefinition; } internal set { mUnitActionDefinition = value; mUnitActionMap.Clear(); foreach (var a in value.ActionMap) { mUnitActionMap.Put(a.Action, a); } } } public UnitActionDefinitionMap.UnitAction GetDefinedUnitAction(UnitActionStatus act) { UnitActionDefinitionMap.UnitAction ret; mUnitActionMap.TryGetValue(act, out ret); return ret; } //---------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------- #region UNITS public List getAllUnits() { return new List(mUnits.Values); } public HashMap getUnits() { return mUnits; } internal void addUnit(UnitInfo st) { if (st.ID == 0) { throw new Exception("ID=0不是一个有效的ID:" + st); } mUnits.Put(st.ID, st); } public UnitInfo getUnit(int id) { UnitInfo st; if (mUnits.TryGetValue(id, out st)) { return st; } return null; } public Dictionary getUnits(List units) { Dictionary ret = new Dictionary(); foreach (int uid in units) { UnitInfo st = getUnit(uid); if (st != null && !ret.ContainsKey(uid)) { ret.Add(uid, st); } } return ret; } #endregion //---------------------------------------------------------------------------------------------- #region SKILLS public List getAllSkills() { return new List(mSkills.Values); } public HashMap getAllSkillData() { return mSkills; } internal void addSkill(SkillTemplate st) { if (st.ID == 0) { throw new Exception("ID=0不是一个有效的ID:" + st); } mSkills.Put(st.ID, st); } public SkillTemplate getSkill(int id) { SkillTemplate st; if (mSkills.TryGetValue(id, out st)) { return st; } return null; } public Dictionary getSkills(List skills) { Dictionary ret = new Dictionary(); foreach (int skillID in skills) { SkillTemplate st = getSkill(skillID); if (st != null && !ret.ContainsKey(skillID)) { ret.Add(skillID, st); } } return ret; } #endregion //---------------------------------------------------------------------------------------------- #region SPELLS public List getAllSpells() { return new List(mSpells.Values); } internal void addSpell(SpellTemplate st) { if (st.ID == 0) { throw new Exception("ID=0不是一个有效的ID:" + st); } mSpells.Put(st.ID, st); } public SpellTemplate getSpell(int id) { SpellTemplate st; if (mSpells.TryGetValue(id, out st)) { return st; } return null; } public Dictionary getSpells(List spells) { Dictionary ret = new Dictionary(); foreach (int sid in spells) { SpellTemplate st = getSpell(sid); if (st != null && !ret.ContainsKey(sid)) { ret.Add(sid, st); } } return ret; } #endregion //---------------------------------------------------------------------------------------------- #region BUFFS public List getAllBuffs() { return new List(mBuffs.Values); } internal void addBuff(BuffTemplate st) { if (st.ID == 0) { throw new Exception("ID=0不是一个有效的ID:" + st); } mBuffs.Put(st.ID, st); } public BuffTemplate getBuff(int id) { BuffTemplate st; if (mBuffs.TryGetValue(id, out st)) { return st; } return null; } public Dictionary getBuffs(List spells) { Dictionary ret = new Dictionary(); foreach (int sid in spells) { BuffTemplate st = getBuff(sid); if (st != null && !ret.ContainsKey(sid)) { ret.Add(sid, st); } } return ret; } #endregion //---------------------------------------------------------------------------------------------- #region ITEMS public List getAllItems() { return new List(mItems.Values); } internal void addItem(ItemTemplate st) { if (st.ID == 0) { throw new Exception("ID=0不是一个有效的ID:" + st); } mItems.Put(st.ID, st); } public ItemTemplate getItem(int id) { ItemTemplate st; if (mItems.TryGetValue(id, out st)) { return st; } return null; } public Dictionary getItems(List items) { Dictionary ret = new Dictionary(); foreach (int sid in items) { ItemTemplate st = getItem(sid); if (st != null && !ret.ContainsKey(sid)) { ret.Add(sid, st); } } return ret; } #endregion //---------------------------------------------------------------------------------------------- #region UNIT_TRIGGERS public List getAllUnitTriggers() { return new List(mUnitTriggers.Values); } internal void addUnitTrigger(UnitTriggerTemplate st) { if (st.ID == 0) { throw new Exception("ID=0不是一个有效的ID:" + st); } mUnitTriggers.Put(st.ID, st); } public UnitTriggerTemplate getUnitTrigger(int id) { UnitTriggerTemplate st; if (mUnitTriggers.TryGetValue(id, out st)) { return st; } return null; } public Dictionary getUnitTriggers(List items) { Dictionary ret = new Dictionary(); foreach (int sid in items) { UnitTriggerTemplate st = getUnitTrigger(sid); if (st != null && !ret.ContainsKey(sid)) { ret.Add(sid, st); } } return ret; } #endregion //---------------------------------------------------------------------------------------------- #region UNIT_EVENTS public List getAllUnitEvents() { return new List(mUnitEvents.Values); } internal void addUnitEvent(UnitEventTemplate st) { if (st.ID == 0) { throw new Exception("ID=0不是一个有效的ID:" + st); } mUnitEvents.Put(st.ID, st); } public UnitEventTemplate getUnitEvent(int id) { UnitEventTemplate st; if (mUnitEvents.TryGetValue(id, out st)) { return st; } return null; } public Dictionary getUnitEvents(List items) { Dictionary ret = new Dictionary(); foreach (int sid in items) { UnitEventTemplate st = getUnitEvent(sid); if (st != null && !ret.ContainsKey(sid)) { ret.Add(sid, st); } } return ret; } #endregion //---------------------------------------------------------------------------------------------- private HashMap> mSnDataMap = new HashMap>(); private HashMap mCurrentSceneSnDataMap = new HashMap(); private int Rehash(object data) { int count = 0; List datas = new List(); PropertyUtil.CollectFieldTypeValues(data, datas); foreach (ISNData sn in datas) { Type type = sn.GetType(); HashMap map = mSnDataMap.Get(type); if (map == null) { map = new HashMap(); mSnDataMap.Add(type, map); } if (!map.ContainsKey(sn.SerialNumber)) { map.Add(sn.SerialNumber, sn); count++; } else { throw new Exception(string.Format("SerialNumber {0} already in use ! {1}", sn.SerialNumber, sn)); } } return count; } public int RehashAll() { mSnDataMap.Clear(); int count = 0; count += Rehash(mUnits); count += Rehash(mSkills); count += Rehash(mSpells); count += Rehash(mBuffs); count += Rehash(mItems); count += Rehash(mUnitTriggers); count += Rehash(mUnitEvents); return count; } internal int RehashAllScene(object scenes) { return Rehash(scenes); } internal int RehashScene(object scene) { int count = 0; mCurrentSceneSnDataMap.Clear(); List datas = new List(); PropertyUtil.CollectFieldTypeValues(scene, datas); foreach (ISNData sn in datas) { if (!mCurrentSceneSnDataMap.ContainsKey(sn.SerialNumber)) { mCurrentSceneSnDataMap.Add(sn.SerialNumber, sn); count++; } else { throw new Exception(string.Format("SerialNumber {0} already in use ! {1} @ {2}", sn.SerialNumber, sn, scene)); } } return count; } public T GetSnData(uint sn) where T : ISNData { Type type = typeof(T); HashMap map = mSnDataMap.Get(type); if (map != null) { T ret = map.Get(sn) as T; if (ret != null) { return ret; } } T ret2 = mCurrentSceneSnDataMap.Get(sn) as T; if(ret2 == null) { //打一些Log,以免某些效果出不来,没头脑的找半天 ClientLog.LogError("找不到SerialNumber={0}的数据,某些效果可能丢失,需要等待服务器更新", sn); } return ret2; } } public interface ITemplateData : IExternalizable { int TemplateID { get; } string EditorPath { get; set; } } public abstract class ISNData : IExternalizable { private static IDGenerator idgen = new IDGenerator(); private uint mSN = idgen.NextID(); [XmlSerializableAttribute(XmlSerializableProperty.IgnoreClone)] public uint SerialNumber { get { return mSN; } set { mSN = idgen.Regist(value); } } public void RegenSerialNumber(uint value) { mSN = idgen.Regist(value); } public virtual void WriteExternal(IOutputStream output) { output.PutU32(mSN); } public virtual void ReadExternal(IInputStream input) { this.mSN = input.GetU32(); } } }