/// <summary> /// Load templates from stream. /// </summary> public void LoadTemplates(Stream stream) { if (loadFinished) { Logger.Error("Try load templates while TemplateMgr is read-only."); return; } if (stream == null) { Logger.Warning("LoadTemplates from null stream, ignored"); return; } // BinaryReader little endian BinaryReader reader = new BinaryReader(stream); int count = reader.ReadInt32(); for (int i = 0; i < count; ++i) { #if DEBUG_INFO Logger.Log(string.Format("Start Read at offset: {0}", reader.BaseStream.Position)); #endif uint typeId = reader.ReadUInt32(); bool isRuntimeLink = reader.ReadBoolean(); if (isRuntimeLink) { int tid = reader.ReadInt32(); tmpls[tid] = null; } else { BaseTemplate tmpl = CreateTemplate(typeId); if (tmpl == null) { continue; } tmpl.TypeID = typeId; tmpl.Deserialize(reader); #if DEBUG_INFO Logger.Log(string.Format("Start Read: {0}", tmpl.tid)); #endif if (tmpls.ContainsKey(tmpl.tid)) { Logger.Warning("{0} is already loaded, the old one will be discarded.", tmpl.tid); } tmpls[tmpl.tid] = tmpl; } } }
static partial void CreateTemplate_Inner(uint typeId, ref BaseTemplate ret) { switch (typeId) { case zf.util.TGameApp.TYPE: ret = new zf.util.TGameApp(); break; case zf.util.TMod.TYPE: ret = new zf.util.TMod(); break; case zf.util.TRunEnv.TYPE: ret = new zf.util.TRunEnv(); break; default: break; } }
/// <summary> /// Create binding class instance with tid. if exists. /// </summary> public BaseObject CreateObject(TID tid) { BaseObject obj; if (tidInsts.TryGetValue(tid, out obj)) { return(obj); } BaseTemplate tmpl = GetTemplate(tid); if (tmpl == null) { Logger.Error("CreateObject failed, tmpl of {0} is null", tid); return(null); } CreateObject_Inner(tmpl.TypeID, tid, ref obj); if (obj == null) { for (int i = 0; i < createObjFuncs.Count; ++i) { createObjFuncs[i](tmpl.TypeID, tid, ref obj); if (obj != null) { break; } } } if (obj == null) { Logger.Error("CreateObject failed, can't create Object of {0}", tid); return(null); } if (tmpl.singletonType == SingletonType.TemplateMgr) { tidInsts.Add(tid, obj); } obj.Tid = tid; obj.InitTemplate(tmpl); obj.OnCreated(); return(obj); }
/// <summary> /// Creates the template. /// </summary> /// <returns>The template.</returns> /// <param name="typeId">Type identifier.</param> public BaseTemplate CreateTemplate(uint typeId) { BaseTemplate ret = null; CreateTemplate_Inner(typeId, ref ret); if (ret != null) { return(ret); } for (int i = 0; i < createTmplFuncs.Count; ++i) { createTmplFuncs[i](typeId, ref ret); if (ret != null) { return(ret); } } Logger.Error("Create Template failed, typeid: {0}", typeId); return(null); }
public virtual void InitTemplate(BaseTemplate tmpl) { }
/// <summary> /// Creates the template. /// Auto generate the implement in Templates_impl.cs /// </summary> static partial void CreateTemplate_Inner(uint id, ref BaseTemplate ret);