示例#1
0
        /// <summary>
        /// Gets the value.
        /// </summary>
        /// <param name="desc">Desc. Array of bitfield items, split with ','</param>
        public long GetVal(string desc, DataDebugInfo debugInfo)
        {
            string[] splits = desc.Split(',');

            long retVal = 0;

            foreach (var item in splits)
            {
                if (item.Equals("All"))
                {
                    return(-1); // 0xFFFFFFFFFFFFFFFF
                }
                else
                {
                    int index = 0;
                    for (; index < Fields.Length; ++index)
                    {
                        if (Fields[index].Equals(item))
                        {
                            break;
                        }
                    }

                    if (index >= Fields.Length)
                    {
                        GLog.LogError("BitField" + this.Name + " does't contain a '" + item + "'" + debugInfo);
                        return(0);
                    }
                    retVal |= ((long)1 << index);
                }
            }

            return(retVal);
        }
示例#2
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);
        }
示例#3
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);
        }
示例#4
0
        /// <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);
        }
示例#5
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);
            }
        }
示例#6
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);
        }
示例#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.fieldInfo = field;
            if (!ReadData_Impl(data, jsonData, field))
            {
                return(null);
            }
            return(data);
        }
示例#8
0
        // 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);
        }
示例#9
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);
        }
示例#10
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);
        }
示例#11
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);
        }
示例#12
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);
            }
        }
示例#13
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);
        }