/// <summary> /// Build this instance. /// 在所有LoadJson()调用完成后调用 /// </summary> public void Build() { // build parent-children inheritance foreach (var tp in mTypeList) { if (tp.IsClass()) { GTypeClass tpClass = tp as GTypeClass; tpClass.InheritParentFields(); } } }
public override bool WriteBinary(BinaryWriter writer, GObject inObject) { GTypeClass tp = (GTypeClass)GTypeManager.Instance.GetType(this.Type); #if DEBUG_INFO GLog.Log("Start Write: {0} at pos {1}", this.ID, writer.BaseStream.Position); #endif writer.Write(tp.crc); writer.Write(isRuntimeLink); writer.Write(GTIDGenerator.Instance.GetID(this.ID)); if (isRuntimeLink) { return(true); } writer.Write((byte)this.singletonType); return(base.WriteBinary(writer, this)); }
public static GType CreateFromJson(JsonData data) { GType ret = null; if (data.Keys.Contains("Class")) { ret = new GTypeClass(); } else if (data.Keys.Contains("Struct")) { ret = new GTypeStruct(); } else if (data.Keys.Contains("Enum")) { ret = new GTypeEnum(); } else if (data.Keys.Contains("BitField")) { ret = new GTypeBitField(); } else { GLog.LogError("Type must be declared as 'Class' or 'Struct' or 'Enum' or 'BitField'. \n" + data.ToJson()); return(null); } if (ret != null) { if (data.Keys.Contains("CompileTo")) { ret.CompileTo = (CompileTarget)Enum.Parse(typeof(CompileTarget), (string)data["CompileTo"]); } else { ret.CompileTo = CompileTarget.CSharp; } if (ret.ParseJson(data)) { return(ret); } } return(null); }
// get base type inherited from TBase // public string GetRootType(string type) { GTypeClass tmp = (GTypeClass)this.GetType(type); while (tmp != null && !string.IsNullOrEmpty(tmp.Parent)) { tmp = (GTypeClass)this.GetType(tmp.Parent); } if (tmp == null) { GLog.LogError("Cant find Root Type of " + type); return(null); } return(tmp.Name); }
public bool isDerived(GType subType, GType baseType) { if (subType == null || !subType.IsClass() || baseType == null || !baseType.IsClass()) { return(false); } GTypeClass subClassType = (GTypeClass)subType; GTypeClass tmp; do { tmp = (GTypeClass)this.GetType(subClassType.Parent); }while (tmp != null && !tmp.isEqual(baseType)); return(tmp != null); }
public void PrepareTIDForJsonTmpls(string jsonPath) { if (!File.Exists(jsonPath)) { return; } string jsonStr = File.ReadAllText(jsonPath); LitJson.JsonData jsonData; try { jsonData = LitJson.JsonMapper.ToObject(jsonStr); } catch (LitJson.JsonException ex) { GLog.LogError("Exception catched while parsing : " + jsonPath + "\n" + ex.Message); return; } if (!jsonData.IsArray) { return; } for (int i = 0; i < jsonData.Count; ++i) { if (!jsonData[i].Keys.Contains("Type") || !jsonData[i].Keys.Contains("ID")) { return; } string type = (string)jsonData[i]["Type"]; // check is class GTypeClass tp = GTypeManager.Instance.GetType(type) as GTypeClass; if (tp == null) { GLog.LogError("LoadJson. '" + type + "' is not defined or is not class\n"); return; } string finalTID = string.IsNullOrEmpty(tp.Category) ? (string)jsonData[i]["ID"] : (tp.Category + "." + (string)jsonData[i]["ID"]); GetID(finalTID, true); } }
public static GType CreateFromDesc(TypeDesc desc) { GType ret = null; if (desc.Tt == TypeDesc.TT.Class) { ret = new GTypeClass(); } else if (desc.Tt == TypeDesc.TT.Struct) { ret = new GTypeStruct(); } else if (desc.Tt == TypeDesc.TT.Enum) { ret = new GTypeEnum(); } else if (desc.Tt == TypeDesc.TT.BitField) { ret = new GTypeBitField(); } else { GLog.LogError("Type must be declared as 'Class' or 'Struct' or 'Enum' or 'BitField'. \n" + desc.Tt); return(null); } ret.Name = desc.Name; ret.Namespace = desc.Namespace; ret.CompileTo = desc.CompileTo; ret.Gen_Head = desc.Gen_Head; ret.Gen_Serialize = desc.Gen_Serialize; ret.Gen_Deserialize = desc.Gen_Deserialize; if (ret.Parse(desc)) { return(ret); } return(null); }
public bool InheritParentFields() { if (inherited) { return(true); } if (this.Parent == null) { return(true); } // check GTypeClass parentType = (GTypeClass)GTypeManager.Instance.GetType(this.Parent); if (parentType == null || !parentType.IsClass()) { GLog.LogError("'" + this.Parent + "' is not defined or is not a 'Class'"); return(false); } parentType.InheritParentFields(); // inherit category this.Category = parentType.Category; // inherit fiedls this.mInheritedFields = new GStructField[parentType.FieldCount]; for (int i = 0; i < this.mInheritedFields.Length; ++i) { // TODO: copy or ref ? // this.mInheritedFields[i] = parentType.GetField(i); } inherited = true; return(true); }
// string cache // List<string> mStrings = new List<string>(); public bool LoadJson(string jsonStr, string dir, string fileName, string patchName) { GLog.Log("Load json: " + Path.Combine(dir, fileName)); LitJson.JsonData jsonData; try { jsonData = LitJson.JsonMapper.ToObject(jsonStr); } catch (LitJson.JsonException ex) { GLog.LogError("Exception catched while parsing : " + Path.Combine(dir, fileName) + "\n" + ex.Message); return(false); } for (int i = 0; i < jsonData.Count; ++i) { if (!jsonData[i].Keys.Contains("Type")) { GLog.LogError("GDataManager.LoadJson: " + jsonData.ToJson() + " does not contain 'Type' definition"); return(false); } string type = (string)jsonData[i]["Type"]; // check is class GTypeClass tp = GTypeManager.Instance.GetType(type) as GTypeClass; if (tp == null) { GLog.LogError("LoadJson. '" + type + "' is not defined or is not class\n"); return(false); } DataDebugInfo info = new DataDebugInfo(); info.fileName = Path.Combine(dir, fileName); GObject obj = tp.ReadData(jsonData[i], false, null, info) as GObject; if (obj == null) { continue; } obj.DirPath = dir; obj.FileName = Path.GetFileNameWithoutExtension(fileName); obj.patch = patchName; GObject oldObj; if (mIDMap.TryGetValue(obj.ID, out oldObj)) { if (oldObj.patch.Equals((obj.patch))) { GLog.LogError("GDataManager.LoadJson: Multi definition of " + oldObj.ID); return(false); } else { mIDMap[obj.ID] = obj; List <GObject> typeObjs; if (!mTypeMap.TryGetValue(type, out typeObjs)) { typeObjs = new List <GObject>(); mTypeMap.Add(type, typeObjs); } typeObjs.Remove(oldObj); typeObjs.Add(obj); } } else { mIDMap[obj.ID] = obj; List <GObject> typeObjs; if (!mTypeMap.TryGetValue(type, out typeObjs)) { typeObjs = new List <GObject>(); mTypeMap.Add(type, typeObjs); } typeObjs.Add(obj); } } return(true); }
public bool GenCode_CS_Binding(out string code, BatchInfo batch) { StringWriter writer = new StringWriter(); CodeGenerator gen = new CodeGenerator(writer); gen.Line("// ============================================================================================= //"); gen.Line("// This is generated by tool. Don't edit this manually."); gen.Line("// Encoding: " + writer.Encoding.EncodingName); gen.Line("// ============================================================================================= //"); gen.Line(); gen.Line(); gen.Line("using System.IO;"); gen.Line("using zf.util;"); gen.Line(); gen.Line("#pragma warning disable 0108"); gen.Line(); // classes foreach (var tp in mTypeList) { if ((tp.batch == null && batch == null) || (tp.batch != null && tp.batch.Equals(batch))) { if (tp.CompileTo == CompileTarget.CSharp) { if (tp.IsClass()) { GTypeClass classTp = tp as GTypeClass; if (!string.IsNullOrEmpty(classTp.BindingClass)) { string spaceName = null; string bindclassName = null; if (classTp.BindingClass.Contains(".")) { bindclassName = classTp.BindingClass.Substring(classTp.BindingClass.LastIndexOf('.') + 1); spaceName = classTp.BindingClass.Substring(0, classTp.BindingClass.LastIndexOf('.')); } else { bindclassName = classTp.BindingClass; } if (!string.IsNullOrEmpty(classTp.BindingClassMacro)) { gen.Line("#if {0}", classTp.BindingClassMacro); } if (!string.IsNullOrEmpty(spaceName)) { gen.Line("namespace {0}", spaceName); gen.AddIndent("{"); } gen.Line("public partial class {0}", bindclassName); gen.AddIndent("{"); gen.Line("public {0} template;", tp.Name); gen.Line("public override void InitTemplate(BaseTemplate tmpl)"); gen.AddIndent("{"); gen.Line("base.InitTemplate(tmpl);"); gen.Line("template = tmpl as {0};", tp.Name); gen.RemIndent("}"); gen.RemIndent("}"); if (!string.IsNullOrEmpty(spaceName)) { gen.RemIndent("}"); } if (!string.IsNullOrEmpty(classTp.BindingClassMacro)) { gen.Line("#endif"); } } } } } } // partial TemplateManager gen.Line("// ----------------------------------------------------------------------------"); GenCode_CS_Mgr(gen, batch); gen.Line(); writer.Flush(); code = writer.ToString(); return(true); }
void GenCode_CS_Mgr(CodeGenerator gen, BatchInfo batch) { if (batch == null) { gen.Line("namespace zf.util"); gen.AddIndent("{"); gen.Line("public partial class TemplateManager"); } else { gen.Line("public class {0}", "Gen_" + batch.name); } gen.AddIndent("{"); // Function CreateTemplate if (batch == null) { gen.Line("static partial void CreateTemplate_Inner(uint typeId, ref BaseTemplate ret)"); } else { gen.Line("public static void CreateTemplate(uint typeId, ref BaseTemplate ret)"); } gen.AddIndent("{"); gen.Line("switch (typeId)"); gen.AddIndent("{"); foreach (var tp in this.mTypeList) { if ((tp.batch == null && batch == null) || (tp.batch != null && tp.batch.Equals(batch))) { if (tp.CompileTo == CompileTarget.CSharp) { if (tp.IsClass()) { GTypeClass classTp = tp as GTypeClass; if (classTp.isTemplateClass) { gen.Line("case {0}.TYPE: ret = new {0}(); break;", tp.FullName); } } } } } gen.Line("default:"); gen.AddIndent(); //gen.Line("GBGenLog.LogError(\"Can't find template type id: \" + typeId);"); gen.Line("break;"); gen.RemIndent(); gen.RemIndent("}"); gen.RemIndent("}"); // Function CreateObject if (batch == null) { gen.Line("static partial void CreateObject_Inner(uint typeId, TID tid, ref BaseObject ret)"); } else { gen.Line("public static void CreateObject(uint typeId, TID tid, ref BaseObject ret)"); } gen.AddIndent("{"); gen.Line("switch (typeId)"); gen.AddIndent("{"); foreach (var tp in this.mTypeList) { if ((tp.batch == null && batch == null) || (tp.batch != null && tp.batch.Equals(batch))) { if (tp.CompileTo == CompileTarget.CSharp) { if (tp.IsClass()) { GTypeClass classTp = tp as GTypeClass; if (!string.IsNullOrEmpty(classTp.BindingClass)) { if (!string.IsNullOrEmpty(classTp.BindingClassMacro)) { gen.Line("#if {0}", classTp.BindingClassMacro); } gen.Line("case {0}.TYPE: ret = new {1}(); break;", tp.Name, classTp.BindingClass); if (!string.IsNullOrEmpty(classTp.BindingClassMacro)) { gen.Line("#endif"); } } } } } } gen.Line("default:"); gen.AddIndent(); //gen.Line("GBGenLog.LogError(\"BindingClass is not defiend! type: \" + typeId);"); gen.Line("break;"); gen.RemIndent(); gen.RemIndent("}"); gen.RemIndent("}"); gen.RemIndent("}"); if (batch == null) { gen.RemIndent("}"); } }