void ParseConst(string name, ScriptMap table)
    {
        var consts = new PackageConst();

        foreach (var pair in table)
        {
            var fieldName = pair.Key as string;
            if (string.IsNullOrEmpty(fieldName))
            {
                throw new Exception($"Const:{name} Field:{fieldName} 参数出错");
            }
            var value = pair.Value;
            var field = new FieldConst()
            {
                Name = fieldName
            };
            switch (value.valueType)
            {
            case ScriptValue.longValueType:
                field.Type  = BasicEnum.INT64;
                field.Value = value.longValue.ToString() + "L";
                break;

            case ScriptValue.stringValueType:
                field.Type  = BasicEnum.STRING;
                field.Value = "\"" + value.stringValue + "\"";
                break;

            case ScriptValue.doubleValueType:
            case ScriptValue.objectValueType:
                field.Type  = BasicEnum.INT32;
                field.Value = value.ToString();
                break;

            default: throw new Exception("不支持此常量类型 " + value.ValueTypeName);
            }
            consts.Fields.Add(field);
        }
        name         = name.Substring(CONST_KEYWORD.Length);
        consts.Name  = name;
        Consts[name] = consts;
    }
Exemple #2
0
    public static void ParseStructure(string dir,
                                      Dictionary <string, List <PackageField> > customClass,         //自定义类
                                      Dictionary <string, List <PackageEnum> > customEnum,           //自定义枚举
                                      Dictionary <string, List <PackageField> > customTable,         //excel表自定义类
                                      Dictionary <string, PackageDatabase> customDatabase,           //单个数据库
                                      Dictionary <string, List <PackageField> > customDatabaseClass, //数据库类
                                      Dictionary <string, List <PackageConst> > customConst)         //自定义常量
    {
        if (customClass != null)
        {
            customClass.Clear();
        }
        if (customEnum != null)
        {
            customEnum.Clear();
        }
        if (customTable != null)
        {
            customTable.Clear();
        }
        if (customDatabase != null)
        {
            customDatabase.Clear();
        }
        if (customDatabaseClass != null)
        {
            customDatabaseClass.Clear();
        }
        if (customConst != null)
        {
            customConst.Clear();
        }
        dir    = Path.Combine(WorkspaceDirectory, dir);
        Script = new Script();
        Script.LoadLibrary();
        List <ScriptObject> GlobalBasic = new List <ScriptObject>();

        {
            var itor = Script.GetGlobalTable().GetIterator();
            while (itor.MoveNext())
            {
                GlobalBasic.Add(itor.Current.Value);
            }
        }
        string[] files = System.IO.Directory.Exists(dir) ? System.IO.Directory.GetFiles(dir, "*.sco", SearchOption.AllDirectories) :  new string[0];
        foreach (var file in files)
        {
            Script.LoadFile(file);
        }
        {
            var itor = Script.GetGlobalTable().GetIterator();
            while (itor.MoveNext())
            {
                if (GlobalBasic.Contains(itor.Current.Value))
                {
                    continue;
                }
                string      name  = itor.Current.Key as string;
                ScriptTable table = itor.Current.Value as ScriptTable;
                if (name == null || table == null)
                {
                    continue;
                }
                //枚举类型
                if (name.StartsWith(ENUM_KEYWORD))
                {
                    if (customEnum != null)
                    {
                        List <PackageEnum> enums = new List <PackageEnum>();
                        var tItor = table.GetIterator();
                        while (tItor.MoveNext())
                        {
                            string       fieldName = tItor.Current.Key as string;
                            ScriptNumber val       = tItor.Current.Value as ScriptNumber;
                            if (string.IsNullOrEmpty(fieldName) || val == null)
                            {
                                throw new Exception(string.Format("Enum:{0} Field:{1} 参数出错", name, fieldName));
                            }
                            enums.Add(new PackageEnum()
                            {
                                Index = Convert.ToInt32(val.ObjectValue),
                                Name  = fieldName,
                            });
                        }
                        enums.Sort((m1, m2) => { return(m1.Index.CompareTo(m2.Index)); });
                        customEnum[name.Substring(ENUM_KEYWORD.Length)] = enums;
                    }
                    //常量类型
                }
                else if (name.StartsWith(CONST_KEYWORD))
                {
                    if (customConst != null)
                    {
                        List <PackageConst> consts = new List <PackageConst>();
                        var tItor = table.GetIterator();
                        while (tItor.MoveNext())
                        {
                            string fieldName = tItor.Current.Key as string;
                            if (string.IsNullOrEmpty(fieldName))
                            {
                                throw new Exception(string.Format("Const:{0} Field:{1} 参数出错", name, fieldName));
                            }
                            PackageConst pack = new PackageConst();
                            pack.Name = fieldName;
                            ScriptObject value = tItor.Current.Value;
                            if (value is ScriptNumberDouble)
                            {
                                pack.Type  = BasicEnum.INT32;
                                pack.Value = ((ScriptNumberDouble)value).ToInt32().ToString();
                            }
                            else if (value is ScriptNumberLong)
                            {
                                pack.Type  = BasicEnum.INT64;
                                pack.Value = ((ScriptNumberLong)value).ToLong().ToString() + "L";
                            }
                            else if (value is ScriptString)
                            {
                                pack.Type  = BasicEnum.STRING;
                                pack.Value = "\"" + value.ToString() + "\"";
                            }
                            else
                            {
                                throw new Exception("不支持此常量类型 " + value.Type);
                            }
                            consts.Add(pack);
                        }
                        customConst[name.Substring(CONST_KEYWORD.Length)] = consts;
                    }
                    //数据库类
                }
                else if (name.StartsWith(DATABASE_KEYWORD))
                {
                    if (customDatabase != null)
                    {
                        PackageDatabase package = new PackageDatabase();
                        var             tables  = table.GetValue("tables") as ScriptTable;
                        if (tables != null)
                        {
                            var tItor = tables.GetIterator();
                            while (tItor.MoveNext())
                            {
                                string tableName = tItor.Current.Key as string;
                                if (tableName.StartsWith(DATABASE_KEYWORD))
                                {
                                    continue;
                                }
                                package.tables.Add(tableName, JsonUtil.JsonToObject <DatabaseTable>(tItor.Current.Value.ToJson()));
                            }
                        }
                        customDatabase[name.Substring(DATABASE_KEYWORD.Length)] = package;
                    }
                }
                else
                {
                    List <PackageField> fields = new List <PackageField>();
                    var tItor = table.GetIterator();
                    while (tItor.MoveNext())
                    {
                        string       fieldName = tItor.Current.Key as string;
                        ScriptString val       = tItor.Current.Value as ScriptString;
                        if (string.IsNullOrEmpty(fieldName) || val == null)
                        {
                            throw new Exception(string.Format("Class:{0} Field:{1} 参数出错 参数模版 \"索引,类型,是否数组=false,注释\"", name, fieldName));
                        }
                        string[] infos = val.Value.Split(',');
                        if (infos.Length < 2)
                        {
                            throw new Exception(string.Format("Class:{0} Field:{1} 参数出错 参数模版 \"索引,类型,是否数组=false,注释\"", name, fieldName));
                        }
                        bool   array        = infos.Length > 2 && infos[2] == "true";
                        string note         = infos.Length > 3 ? infos[3] : "";
                        var    packageField = new PackageField()
                        {
                            Index   = int.Parse(infos[0]),
                            Type    = infos[1],
                            Name    = fieldName,
                            Array   = array,
                            Comment = note,
                        };
                        if (!packageField.IsBasic)
                        {
                            if (Script.HasValue(ENUM_KEYWORD + packageField.Type))
                            {
                                packageField.Enum = true;
                            }
                            else if (!Script.HasValue(packageField.Type) &&                                 //判断网络协议自定义类
                                     !Script.HasValue(TABLE_KEYWORD + packageField.Type) &&                 //判断Table内嵌类
                                     !Script.HasValue(DATABASE_CLASS_KEYWORD + packageField.Type)           //判断数据库内嵌类
                                     )
                            {
                                throw new Exception(string.Format("Class:{0} Field:{1} 未知类型:{2}", name, fieldName, packageField.Type));
                            }
                        }
                        fields.Add(packageField);
                    }
                    fields.Sort((m1, m2) => { return(m1.Index.CompareTo(m2.Index)); });
                    if (name.StartsWith(DATABASE_CLASS_KEYWORD))        //数据库内使用类
                    {
                        if (customDatabaseClass != null)
                        {
                            customDatabaseClass[name.Substring(DATABASE_CLASS_KEYWORD.Length)] = fields;
                        }
                    }
                    else if (name.StartsWith(TABLE_KEYWORD))            //excel表使用类
                    {
                        if (customTable != null)
                        {
                            customTable[name.Substring(TABLE_KEYWORD.Length)] = fields;
                        }
                    }
                    else
                    {
                        if (customClass != null)
                        {
                            customClass[name] = fields;
                        }
                    }
                }
            }
        }
    }