public override bool ParseJson(JsonData data) { this.Gen_Head = true; this.Name = (string)data["Macro"]; if (data.Keys.Contains("Fields")) { JsonData fieldsJson = data["Fields"]; if (!fieldsJson.IsArray || fieldsJson.Count == 0) { GLog.LogError("'Fields' is invalid.\n" + data.ToJson()); return(false); } this.Fields = new Field[fieldsJson.Count]; int lastEnumValue = -1; for (int i = 0; i < fieldsJson.Count; ++i) { this.Fields[i] = new Field(); this.Fields[i].Parse(fieldsJson[i]); if (!fieldsJson[i].Keys.Contains("Key")) { GLog.LogError("GType.Parse: " + this.Name + " Enums " + i + " 'Key' is not defined"); return(false); } this.Fields[i].Key = (string)fieldsJson[i]["Key"]; if (fieldsJson[i].Keys.Contains("Value")) { if (!fieldsJson[i]["Value"].IsInt) { GLog.LogError(this.Name + " Enums " + i + " 'Value' is not an interger"); return(false); } this.Fields[i].Val = (int)fieldsJson[i]["Value"]; if (this.Fields[i].Val <= lastEnumValue) { GLog.LogError(this.Name + " Enums " + i + " 'Value' must be bigger than " + lastEnumValue); return(false); } lastEnumValue = this.Fields[i].Val; } else { this.Fields[i].Val = ++lastEnumValue; } GLog.Log("AddNumItem: " + this.Fields[i].Key + " " + this.Fields[i].Val); } return(true); } else { this.Fields = new Field[0]; return(true); } }
public void InheriteParent(GData parent) { if (parent == null) { return; } if (this.mType.Equals(GType.Array)) { GLog.Log("inherit array '{0}' {1}", this.fieldInfo.Name, debugInfo); if (!this.isKVArray) { int count = parent.inst_array.Count; for (int i = 0; i < count; ++i) { inst_array.Insert(i, parent.inst_array[i].Clone()); } } else { int count = parent.inst_array.Count; for (int i = 0; i < count; ++i) { if (inst_array.Count <= i) { inst_array.Add(parent.inst_array[i].Clone()); } else if (inst_array[i] == null) { inst_array[i] = parent.inst_array[i].Clone(); } } } for (int i = 0; i < inst_array.Count; ++i) { if (inst_array[i] == null) { GLog.LogError("Array item can't be null. Index '{0}' of '{1}' {2}", i, this.fieldInfo.Name, debugInfo); } } } else if (mType.Equals(GType.Map)) { GLog.Log("inherit map '{0}' {1}", this.fieldInfo.Name, debugInfo); IList <MapKV> parentData = parent.inst_map; for (int i = 0; i < parentData.Count; ++i) { if (GetMapItem(parentData[i].key) == null) { inst_map.Insert(0, new MapKV() { key = parentData[i].key.Clone(), val = parentData[i].val.Clone() }); } } } }
protected bool ReadData_Impl(GData data, JsonData jsonData, GStructField field) { data.Obj = new Dictionary <string, GData>(); if (!jsonData.IsObject) { GLog.LogError("JsonData of {0}({1}) is not a struct {2}", field.Name, this.Name, data.debugInfo); return(false); } bool inheritParent = false; string fieldName; // check if field name is not defined. foreach (var key in jsonData.Keys) { if (key.Equals("ID") || key.Equals("Type") || key.Equals("Parent") || key.Equals("Singleton") || key.Equals("RuntimeLink") ) { continue; } if (key[0] == '+') { GLog.Log("inheritParent " + key); inheritParent = true; fieldName = key.Substring(1, key.Length - 1); } else { fieldName = key; } GStructField desc = GetField(fieldName); if (desc == null) { GLog.LogError("{0} is not a field of {1} {2}", fieldName, this.Name, data.debugInfo); return(false); } GType tp = GTypeManager.Instance.GetType(desc.Type); GData subData = tp.ReadData(jsonData[key], inheritParent, desc, data.debugInfo); if (subData == null) { return(false); } data.Obj.Add(desc.Name, subData); } return(true); }
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 bool PreBuild() { if (prebuilded) { return(true); } prebuilded = true; if (!string.IsNullOrEmpty(Parent)) { GLog.Log("Build: " + this.ID); GObject parentObj = GDataManager.Instance.GetObj(Parent); if (parentObj == null) { GLog.LogError("Parent '{0}' can't be found.{1}", Parent, debugInfo); return(false); } // 0. parent prebuild parentObj.PreBuild(); // 1. Inherite Array、Map ... var e = inst_object.GetEnumerator(); while (e.MoveNext()) { if (e.Current.Value.inheritParent) { GLog.Log("inheriteParent true"); GData parentData = parentObj.GetField(e.Current.Key); e.Current.Value.InheriteParent(parentData); } } } return(true); }
/// <summary> /// Replaces the macro. /// </summary> /// <returns>new string</returns> public string ReplaceMacro(string str, DataDebugInfo debugInfo) { if (string.IsNullOrEmpty(str)) { return(str); } int index = 0; int numOfMacro = 0; string newStr = ""; while (index < str.Length) { int leftIndex = str.IndexOf("##", index, StringComparison.Ordinal); if (leftIndex < 0) { if (index > 0) { newStr += str.Substring(index); } break; } int rightIndex = str.IndexOf("##", leftIndex + 2, StringComparison.Ordinal); if (rightIndex < 0) { GLog.LogError("Can't parse macro in string : " + str + debugInfo); return(str); } numOfMacro += 1; newStr += str.Substring(index, leftIndex - index); string macroName = str.Substring(leftIndex + 2, rightIndex - leftIndex - 2); // todo: 目前仅支持ID宏 其他的后续看情况添加 // if (macroName.Equals("ID")) { if (this.ID.IndexOf('.') < 0) { newStr += this.ID; } else { string[] splits = this.ID.Split('.'); newStr += splits[1]; } } else { GLog.LogError("Unknown macro '{0}' in string '{1}'{2}", macroName, str, debugInfo); return(str); } index = rightIndex + 2; } if (numOfMacro > 0) { GLog.Log("ReplaceMacro '{0}' to '{1}' {2}", str, newStr, debugInfo); } else { newStr = str; } return(newStr); }
// 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); }