Ejemplo n.º 1
0
        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);
        }
Ejemplo n.º 2
0
        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);
        }
Ejemplo n.º 3
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);
        }
Ejemplo n.º 4
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);
        }