Esempio n. 1
0
        public static GType CreateBasicType(string type)
        {
            // TODO:error check
            GType tp = new GType();

            tp.Name = type;
            return(tp);
        }
Esempio n. 2
0
 public virtual bool isEqual(GType b)
 {
     if (b == null)
     {
         return(false);
     }
     return(this.Name.Equals(b.Name));
 }
Esempio n. 3
0
        public override bool WriteDefault(BinaryWriter writer, JsonData defaultVal)
        {
            for (int i = 0; i < this.FieldCount; ++i)
            {
                GType fieldTp = GTypeManager.Instance.GetType(this.GetField(i).Type);
                fieldTp.WriteDefault(writer, null);
            }

            return(true);
        }
Esempio n. 4
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);
        }
Esempio n. 5
0
        public virtual bool WriteJson(ref JsonData jsonData)
        {
            if (Validate() == false)
            {
                return(false);
            }

            GType tp = GTypeManager.Instance.GetType(mType);

            return(tp == null ? false : tp.WriteJson(ref jsonData, this));
        }
Esempio n. 6
0
        public override void GenCode_CS_FieldSerialize(CodeGenerator gen, string varName = null, string[] subTypes = null)
        {
            GType subTp1 = GTypeManager.Instance.GetType(subTypes[0]);
            GType subTp2 = GTypeManager.Instance.GetType(subTypes[1]);

            gen.Line("writer.Write({0}.Count);", varName);
            gen.Line("var iter_{0} = {0}.GetEnumerator();", varName);
            gen.Line("while (iter_{0}.MoveNext())", varName);
            gen.AddIndent("{");
            subTp1.GenCode_CS_FieldSerialize(gen, string.Format("iter_{0}.Current.Key", varName));
            subTp2.GenCode_CS_FieldSerialize(gen, string.Format("iter_{0}.Current.Value", varName));
            gen.RemIndent("}");
        }
Esempio n. 7
0
 void AddBasicTypes()
 {
     mTypeMap.Add(GType.Bool, GType.CreateBasicType(GType.Bool));
     mTypeMap.Add(GType.Byte, GType.CreateBasicType(GType.Byte));
     mTypeMap.Add(GType.Short, GType.CreateBasicType(GType.Short));
     mTypeMap.Add(GType.Int, GType.CreateBasicType(GType.Int));
     mTypeMap.Add(GType.Float, GType.CreateBasicType(GType.Float));
     mTypeMap.Add(GType.String, GType.CreateBasicType(GType.String));
     mTypeMap.Add(GType.TID, GType.CreateBasicType(GType.TID));
     mTypeMap.Add(GType.List, new GTypeArray(true));
     mTypeMap.Add(GType.Array, new GTypeArray());
     mTypeMap.Add(GType.Map, new GTypeMap());
     mTypeMap.Add(GType.Handle, new GTypeHandle());
 }
Esempio n. 8
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);
        }
Esempio n. 9
0
        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);
        }
Esempio n. 10
0
        /*        public bool LoadYaml(string content)
         *      {
         *          try
         *          {
         *              var deserializer = new YamlDotNet.Serialization.DeserializerBuilder().Build();
         *              var yamlObject = deserializer.Deserialize(new StringReader(content));
         *              var serializer = new YamlDotNet.Serialization.SerializerBuilder()
         *                  .JsonCompatible()
         *                  .Build();
         *          }
         *          catch (Exception ex)
         *          {
         *              GLog.LogError("YAML parse error: " + ex.Message);
         *          }
         *
         *          var json = serializer.Serialize(yamlObject);
         *          return LoadJson(json);
         *      }
         */

        /// <summary>
        /// Load types json.
        /// 可以多次Load多个json并合并
        /// 最终必须调用Build()
        /// </summary>
        public bool LoadJson(string content, string fileName, BatchInfo batch = null)
        {
            LitJson.JsonData typeList;
            try {
                typeList = LitJson.JsonMapper.ToObject(content);
            }
            catch (Exception ex) {
                GLog.LogError("Exception catched while parsing : " + fileName + "\n" + ex.Message);
                return(false);
            }

            for (int i = 0; i < typeList.Count; ++i)
            {
                GType tp = GType.CreateFromJson(typeList[i]);

                if (tp == null)
                {
                    return(false);
                }

                if (mTypeMap.ContainsKey(tp.Name))
                {
                    GLog.LogError("GTypeManager.Load: Multi definition of " + tp.Name);
                    return(false);
                }
                tp.batch = batch;

                if (tp.Namespace == null)
                {
                    if (batch != null)
                    {
                        tp.Namespace = batch.code_namespace;
                    }
                    else
                    {
                        tp.Namespace = "zf.util";
                    }
                }

                mTypeMap.Add(tp.Name, tp);
                mTypeList.Add(tp);
                //GLog.Log("Finish " + tp.Name + "  isEnum:" + tp.IsEnum() + "\n");
            }
            return(true);
        }
Esempio n. 11
0
        public override void GenCode_CS_FieldSerialize(CodeGenerator gen, string varName = null, string[] subTypes = null)
        {
            GType subTp1 = GTypeManager.Instance.GetType(subTypes[0]);

            if (isList)
            {
                gen.Line("int len_{0} = {0}.Count;", varName);
            }
            else
            {
                gen.Line("int len_{0} = {0}.Length;", varName);
            }

            gen.Line("writer.Write(len_{0});", varName);
            gen.Line("for (int i_{0} = 0; i_{0} < len_{0}; ++i_{0})", varName);
            gen.AddIndent("{");
            subTp1.GenCode_CS_FieldSerialize(gen, string.Format("{0}[i_{0}]", varName));
            gen.RemIndent("}");
        }
Esempio n. 12
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);
            }
        }
Esempio n. 13
0
        public override void GenCode_CS_FieldDeserialize(CodeGenerator gen, string left, string varName = null, string[] subTypes = null)
        {
            if (varName == null)
            {
                varName = left;
            }

            GType subTp1 = GTypeManager.Instance.GetType(subTypes[0]);
            GType subTp2 = GTypeManager.Instance.GetType(subTypes[1]);

            gen.Line("int len_{0} = reader.ReadInt32();", varName);
            gen.Line("{0} = new Dictionary<{1}, {2}>();", varName, subTp1.GetCSName(), subTp2.GetCSName());

            gen.Line("for (int i_{0} = 0; i_{0} < len_{0}; ++i_{0})", varName);
            gen.AddIndent("{");
            subTp1.GenCode_CS_FieldDeserialize(gen, string.Format("{0} key", subTp1.GetCSName()), "key");
            subTp2.GenCode_CS_FieldDeserialize(gen, string.Format("{0} val", subTp2.GetCSName()), "val");
            gen.Line("{0}.Add(key, val);", varName);
            gen.RemIndent("}");
        }
Esempio n. 14
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);
        }
Esempio n. 15
0
        /// <summary>
        /// Load Types from C# Assembly
        /// </summary>
        public bool LoadFromAssembly(string fileName, BatchInfo batch = null, string[] namespaceFilter = null)
        {
            Assembly asm = Assembly.Load(File.ReadAllBytes(fileName));

            foreach (var desc in GCSAssembly_Convertor.Convert(asm, namespaceFilter))
            {
                GType tp = GType.CreateFromDesc(desc);
                if (tp == null)
                {
                    return(false);
                }

                if (mTypeMap.ContainsKey(tp.Name))
                {
                    GLog.LogError("GTypeManager.Load: Multi definition of " + tp.Name);
                    return(false);
                }
                tp.batch = batch;
                mTypeMap.Add(tp.Name, tp);
                mTypeList.Add(tp);
            }
            return(true);
        }
Esempio n. 16
0
        public override void GenCode_CS_FieldDeserialize(CodeGenerator gen, string left, string varName = null, string[] subTypes = null)
        {
            if (varName == null)
            {
                varName = left;
            }
            GType subTp1 = GTypeManager.Instance.GetType(subTypes[0]);

            gen.Line("int len_{0} = reader.ReadInt32();", varName);

            if (isList)
            {
                gen.Line("{0} = new List<{1}>();", left, subTp1.GetCSName());
            }
            else
            {
                gen.Line("{0} = new {1}[len_{0}];", left, subTp1.GetCSName());
            }

            gen.Line("for (int i_{0} = 0; i_{0} < len_{0}; ++i_{0})", varName);
            gen.AddIndent("{");
            subTp1.GenCode_CS_FieldDeserialize(gen, string.Format("{0}[i_{0}]", varName));
            gen.RemIndent("}");
        }
Esempio n. 17
0
        public void GenCode_CS_Serialize(CodeGenerator gen)
        {
            GType tp = GTypeManager.Instance.GetType(Type);

            tp.GenCode_CS_FieldSerialize(gen, this.Name, this.SubTypes);
        }
Esempio n. 18
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);
            }
        }
Esempio n. 19
0
        public virtual bool WriteBinary(BinaryWriter writer, GObject inObj)
        {
            GType tp = GTypeManager.Instance.GetType(mType);

            return(tp.WriteBinary(writer, this, inObj));
        }
Esempio n. 20
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);
        }
Esempio n. 21
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);
        }