예제 #1
0
        public override bool WriteBinary(BinaryWriter writer, GData data, GObject inObj)
        {
            // todo: check if template field name is defined in type config.

            for (int i = 0; i < FieldCount; ++i)
            {
                GStructField field     = GetField(i);
                GData        fieldData = data.GetFieldWithInheritance(field.Name);
                if (fieldData != null)
                {
                    fieldData.WriteBinary(writer, inObj);
                }
                else
                {
                    // write default
                    GType fieldTp = GTypeManager.Instance.GetType(field.Type);
                    if (fieldTp != null)
                    {
                        fieldTp.WriteDefault(writer, field.Default);
                    }
                    else
                    {
                        GLog.LogError("Can't find type definition of " + field.Type + data.debugInfo);
                    }
                }
            }
            return(true);
        }
예제 #2
0
        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);
        }
예제 #3
0
        public override GData ReadData(string strData, GStructField field, DataDebugInfo ownerDebugInfo)
        {
            GData data = new GData(Name);

            data.debugInfo = ownerDebugInfo;

            data.Enum = strData;
            if (!this.Contains(data.Enum))
            {
                GLog.LogError(data.Enum + " is not in " + Name + data.debugInfo);
                return(null);
            }
            return(data);
        }
예제 #4
0
        public override GData ReadData(JsonData jsonData, bool inherit, GStructField field, DataDebugInfo ownerDebugInfo)
        {
            GData data = new GData(Name);

            data.inheritParent = inherit;
            data.debugInfo     = ownerDebugInfo;

            data.fieldInfo = field;
            if (!ReadData_Impl(data, jsonData, field))
            {
                return(null);
            }
            return(data);
        }
예제 #5
0
        public override GData ReadData(JsonData jsonData, bool inherit, GStructField field, DataDebugInfo ownerDebugInfo)
        {
            GData data = new GData(Name);

            data.inheritParent = inherit;
            data.debugInfo     = ownerDebugInfo;

            data.Enum = (string)jsonData;
            if (!this.Contains(data.Enum))
            {
                GLog.LogError(data.Enum + " is not in " + Name + data.debugInfo);
                return(null);
            }
            return(data);
        }
예제 #6
0
        public override GData ReadData(string strData, GStructField field, DataDebugInfo ownerDebugInfo)
        {
            GData data = new GData(Name);

            data.debugInfo = ownerDebugInfo;

            data.BitField = strData;
            if (!string.IsNullOrEmpty(data.BitField))
            {
                if (GetVal(data.BitField, data.debugInfo) == 0)
                {
                    return(null);
                }
            }
            return(data);
        }
예제 #7
0
        public override GData ReadData(JsonData jsonData, bool inherit, GStructField field, DataDebugInfo ownerDebugInfo)
        {
            GData data = new GData(Name);

            data.inheritParent = inherit;
            data.debugInfo     = ownerDebugInfo;

            data.BitField = (string)jsonData;
            if (!string.IsNullOrEmpty(data.BitField))
            {
                if (GetVal(data.BitField, data.debugInfo) == 0)
                {
                    return(null);
                }
            }
            return(data);
        }
예제 #8
0
        public override GData ReadData(JsonData jsonData, bool inherit, GStructField field, DataDebugInfo ownerDebugInfo)
        {
            GData data = new GData(GType.Map);

            data.inheritParent = inherit;
            data.debugInfo     = ownerDebugInfo;
            data.fieldInfo     = field;
            data.Map           = new List <MapKV>();

            GType keyType = GTypeManager.Instance.GetType(field.SubTypes[0]);
            GType valType = GTypeManager.Instance.GetType(field.SubTypes[1]);

            if (jsonData.IsObject)
            {
                var keys = jsonData.Keys;
                var e    = keys.GetEnumerator();
                while (e.MoveNext())
                {
                    GData key = keyType.ReadData(e.Current, field, data.debugInfo);
                    if (key == null)
                    {
                        return(null);
                    }
                    GData val = valType.ReadData(jsonData[e.Current], false, field, data.debugInfo);
                    if (val == null)
                    {
                        return(null);
                    }

                    data.Map.Add(new MapKV()
                    {
                        key = key, val = val
                    });
                }

                return(data);
            }
            else
            {
                GLog.LogError("Field '{0}' expect array data.{1}", field.Name, data.debugInfo);
                return(null);
            }
        }
예제 #9
0
        public override bool WriteJson(ref JsonData jsonData, GData data)
        {
            if (jsonData == null)
            {
                jsonData = new JsonData();
            }
            jsonData.SetJsonType(JsonType.Object);

            for (int i = 0; i < this.FieldCount; ++i)
            {
                GStructField desc = this.GetField(i);
                if (!data.Obj.Keys.Contains(desc.Name))
                {
                    continue;
                }

                JsonData subJsonData = new JsonData();
                data.GetField(desc.Name).WriteJson(ref subJsonData);
                jsonData.Add(desc.Name, subJsonData);
            }
            return(true);
        }
예제 #10
0
        public virtual GData ReadData(string strData, GStructField field, DataDebugInfo ownerDebugInfo)
        {
            GData data = new GData(Name);

            data.debugInfo = ownerDebugInfo;

            if (Name.Equals(GType.Bool))
            {
                bool ret;
                if (bool.TryParse(strData, out ret))
                {
                    data.Bool = bool.Parse(strData);
                }
                else
                {
                    GLog.LogError("Parse Bool failed: {0}", strData);
                    return(null);
                }
            }
            else if (Name.Equals(GType.Byte) ||
                     Name.Equals(GType.Short) ||
                     Name.Equals(GType.Int))
            {
                int intVal = 0;
                int ret;
                if (int.TryParse(strData, out ret))
                {
                    intVal = ret;
                }
                else
                {
                    string[] splits = strData.Split('.');
                    if (splits.Length >= 2)
                    {
                        GType tp = GTypeManager.Instance.GetType(splits[0]);
                        if (tp.IsEnum())
                        {
                            GTypeEnum enumTp = tp as GTypeEnum;
                            int       val    = (int)enumTp.GetVal(splits[1]);
                            if (val != -1)
                            {
                                intVal = val;
                            }
                            else
                            {
                                GLog.LogError("Can't parse {0} while assigning to int field {1}{2}", strData, field.Name, data.debugInfo);
                            }
                        }
                        else
                        {
                            GLog.LogError("Can't parse {0} while assigning to int field {1}{2}", strData, field.Name, data.debugInfo);
                        }
                    }
                    else
                    {
                        GLog.LogError("Can't parse {0} while assigning to int field {1}{2}", strData, field.Name, data.debugInfo);
                    }
                }

                if (Name.Equals(GType.Int))
                {
                    data.Int = intVal;
                }
                else if (Name.Equals(GType.Short))
                {
                    data.Short = (short)intVal;
                    if (data.Short != intVal)
                    {
                        GLog.LogError("{0} is cast to short {1}", intVal, data.Short);
                    }
                }
                else if (Name.Equals(GType.Byte))
                {
                    data.Byte = (byte)intVal;
                    if (data.Byte != intVal)
                    {
                        GLog.LogError("{0} is cast to byte {1}", intVal, data.Byte);
                    }
                }
            }
            else if (Name.Equals(GType.Float))
            {
                float ret;
                if (float.TryParse(strData, out ret))
                {
                    data.Float = ret;
                }
                else
                {
                    GLog.LogError("(" + strData + ") is not a number" + data.debugInfo);
                    return(null);
                }
            }
            else if (Name.Equals(GType.String))
            {
                data.String = strData;
            }
            else if (Name.Equals(GType.TID))
            {
                if (string.IsNullOrEmpty(field.Category))
                {
                    data.TID = strData;
                }
                else
                {
                    data.TID = field.Category + "." + strData;
                }
            }

            return(data);
        }
예제 #11
0
        public virtual GData ReadData(JsonData jsonData, bool inherit, GStructField field, DataDebugInfo ownerDebugInfo)
        {
            GData data = new GData(Name);

            data.inheritParent = inherit;
            data.debugInfo     = ownerDebugInfo;

            if (Name.Equals(GType.Bool))
            {
                data.Bool = (bool)jsonData;
            }
            else if (Name.Equals(GType.Byte) ||
                     Name.Equals(GType.Short) ||
                     Name.Equals(GType.Int))
            {
                int intVal = 0;
                if (jsonData.IsInt)
                {
                    intVal = (int)jsonData;
                }
                else if (jsonData.IsDouble)
                {
                    float tmp = (float)jsonData;
                    intVal = (int)tmp;
                    if (tmp > intVal)
                    {
                        GLog.LogError(jsonData + " is converted to int!" + data.debugInfo);
                    }
                }
                else if (jsonData.IsString)
                {
                    string[] splits = ((string)jsonData).Split('.');
                    if (splits.Length >= 2)
                    {
                        GType tp = GTypeManager.Instance.GetType(splits[0]);
                        if (tp.IsEnum())
                        {
                            GTypeEnum enumTp = tp as GTypeEnum;
                            int       val    = (int)enumTp.GetVal(splits[1]);
                            if (val != -1)
                            {
                                intVal = val;
                            }
                            else
                            {
                                GLog.LogError("Can't parse {0} while assigning to int field {1}{2}", (string)jsonData, field.Name, data.debugInfo);
                            }
                        }
                        else
                        {
                            GLog.LogError("Can't parse {0} while assigning to int field {1}{2}", (string)jsonData, field.Name, data.debugInfo);
                        }
                    }
                    else
                    {
                        GLog.LogError("Can't parse string \"{0}\" to enum while assigning to int field {1}{2}", (string)jsonData, field.Name, data.debugInfo);
                    }
                }
                else
                {
                    GLog.LogError("(" + jsonData.GetJsonType() + ") is not int" + data.debugInfo);
                }

                if (Name.Equals(GType.Int))
                {
                    data.Int = intVal;
                }
                else if (Name.Equals(GType.Short))
                {
                    data.Short = (short)intVal;
                    if (data.Short != intVal)
                    {
                        GLog.LogError("{0} is cast to short {1}", intVal, data.Short);
                    }
                }
                else if (Name.Equals(GType.Byte))
                {
                    data.Byte = (byte)intVal;
                    if (data.Byte != intVal)
                    {
                        GLog.LogError("{0} is cast to byte {1}", intVal, data.Byte);
                    }
                }
            }
            else if (Name.Equals(GType.Float))
            {
                if (jsonData.IsInt)
                {
                    data.Float = (int)jsonData;
                }
                else if (jsonData.IsDouble)
                {
                    data.Float = (float)jsonData;
                }
                else
                {
                    GLog.LogError("(" + jsonData.GetJsonType() + ") is not a number" + data.debugInfo);
                }
            }
            else if (Name.Equals(GType.String))
            {
                data.String = (string)jsonData;
            }
            else if (Name.Equals(GType.TID))
            {
                if (string.IsNullOrEmpty(field.Category))
                {
                    data.TID = (string)jsonData;
                }
                else
                {
                    // todo: ����Ƿ��Ѿ�����Ϊ Catagory.Name ��ʽ
                    data.TID = field.Category + "." + (string)jsonData;
                }
            }

            return(data);
        }
예제 #12
0
        public override GData ReadData(JsonData jsonData, bool inherit, GStructField field, DataDebugInfo ownerDebugInfo)
        {
            GObject obj = new GObject(Name);

            obj.inheritParent = inherit;
            obj.debugInfo     = ownerDebugInfo;

            // TODO: check errors
            // todo: 检查是否已经定义为 Category.Name 格式
            if (string.IsNullOrEmpty(this.Category))
            {
                obj.ID = (string)jsonData["ID"];
            }
            else
            {
                obj.ID = this.Category + "." + (string)jsonData["ID"];
            }

            obj.debugInfo.ownerTID = obj.ID;

            if (jsonData.Keys.Contains("RuntimeLink"))
            {
                obj.isRuntimeLink = (bool)jsonData["RuntimeLink"];
            }

            // todo: 检查是否已经定义为 Category.Name 格式
            if (jsonData.Keys.Contains("Parent"))
            {
                if (string.IsNullOrEmpty(this.Category))
                {
                    obj.Parent = (string)jsonData["Parent"];
                }
                else
                {
                    obj.Parent = this.Category + "." + (string)jsonData["Parent"];
                }
            }

            if (jsonData.Keys.Contains("Singleton"))
            {
                try {
                    obj.singletonType = (GObject.SingletonType)Enum.Parse(typeof(GObject.SingletonType), (string)jsonData["Singleton"]);
                }
                catch (Exception ex) {
                    GLog.LogError("Unrecognized SingletonType '{0}' {1}", (string)jsonData["Singleton"], obj.debugInfo);
                }
            }

            if (jsonData.Keys.Contains("CrcMask"))
            {
                obj.CrcMask = (uint)(int)jsonData["CrcMask"];
            }

            obj.crc = Crc32.Calc(obj.ID, obj.CrcMask);

            if (!ReadData_Impl(obj, jsonData, field))
            {
                return(null);
            }
            return(obj);
        }
예제 #13
0
        public override GData ReadData(JsonData jsonData, bool inherit, GStructField field, DataDebugInfo ownerDebugInfo)
        {
            GData data = new GData(GType.Array);

            data.inheritParent = inherit;
            data.debugInfo     = ownerDebugInfo;

            data.fieldInfo = field;
            data.Array     = new List <GData>();

            GType itemType = GTypeManager.Instance.GetType(field.SubTypes[0]);

            // 完整数组 //
            if (jsonData.IsArray)
            {
                for (int iSub = 0; iSub < jsonData.Count; ++iSub)
                {
                    GData arrayItem = itemType.ReadData(jsonData[iSub], false, field, data.debugInfo);
                    if (arrayItem == null)
                    {
                        return(null);
                    }
                    data.Array.Add(arrayItem);
                }
                return(data);
            }
            // 指定index的数组 //
            else if (jsonData.IsObject)
            {
                data.isKVArray = true;

                var keys      = jsonData.Keys;
                int lastIndex = -1;
                var e         = keys.GetEnumerator();
                while (e.MoveNext())
                {
                    int index = 0;
                    if (!int.TryParse(e.Current, out index))
                    {
                        GLog.LogError("Array index must be integer. '{0}' {1}", e.Current, data.debugInfo);
                        return(null);
                    }

                    if (index <= lastIndex)
                    {
                        GLog.LogError("Array index must be incremental. '{0}' {1}", e.Current, data.debugInfo);
                        return(null);
                    }

                    while (lastIndex++ < index - 1)
                    {
                        data.Array.Add(null);
                    }

                    GData arrayItem = itemType.ReadData(jsonData[e.Current], false, field, data.debugInfo);
                    if (arrayItem == null)
                    {
                        return(null);
                    }
                    data.Array.Add(arrayItem);
                }

                return(data);
            }
            else
            {
                GLog.LogError("Field '{0}' expect array data.{1}", field.Name, data.debugInfo);
                return(null);
            }
        }