public static void Main(string[] args) { DslLogDelegation logCallback = (string msg) => { Console.WriteLine("{0}", msg); }; Dictionary<string, string> encodeTable = new Dictionary<string, string>(); Dictionary<string, string> decodeTable = new Dictionary<string, string>(); AddCrypto("skill", encodeTable, decodeTable); AddCrypto("section", encodeTable, decodeTable); AddCrypto("foreach", encodeTable, decodeTable); AddCrypto("looplist", encodeTable, decodeTable); AddCrypto("loop", encodeTable, decodeTable); AddCrypto("wait", encodeTable, decodeTable); AddCrypto("sleep", encodeTable, decodeTable); AddCrypto("while", encodeTable, decodeTable); AddCrypto("if", encodeTable, decodeTable); AddCrypto("log", encodeTable, decodeTable); AddCrypto("=", encodeTable, decodeTable); AddCrypto("+", encodeTable, decodeTable); AddCrypto("-", encodeTable, decodeTable); AddCrypto("*", encodeTable, decodeTable); AddCrypto("/", encodeTable, decodeTable); AddCrypto("%", encodeTable, decodeTable); AddCrypto(">", encodeTable, decodeTable); AddCrypto(">=", encodeTable, decodeTable); AddCrypto("==", encodeTable, decodeTable); AddCrypto("!=", encodeTable, decodeTable); AddCrypto("<", encodeTable, decodeTable); AddCrypto("<=", encodeTable, decodeTable); AddCrypto("&&", encodeTable, decodeTable); AddCrypto("||", encodeTable, decodeTable); AddCrypto("!", encodeTable, decodeTable); DslFile file = new DslFile(); file.Load("test.txt", logCallback); #if FULL_VERSION file.Save("copy.txt"); string code1 = file.GenerateBinaryCode(File.ReadAllText("test.txt"), encodeTable, logCallback); File.WriteAllText("binary.txt", code1); #endif file.DslInfos.Clear(); string code = File.ReadAllText("binary.txt"); file.LoadBinaryCode(code, decodeTable); #if FULL_VERSION file.Save("unbinary.txt"); #endif long t1 = GetLocalMilliseconds(); for (int i = 0; i < 1000; ++i) { file.LoadBinaryCode(code, decodeTable); } long t2 = GetLocalMilliseconds(); Console.WriteLine("time:{0}", t2 - t1); }
internal bool Init(string dslFile) { try { DslFile file = new DslFile(); if (!file.Load(dslFile, LogSystem.Log)) { return false; } bool haveError = false; foreach (DslInfo info in file.DslInfos) { if (info.GetId() == "tool") { if (info.Functions.Count == 1) { FunctionData funcData = info.First; if (null != funcData && funcData.HaveExternScript()) { string toolFile; CallData callData = funcData.Call; if (null != callData && callData.GetParamNum() == 1) { toolFile = callData.GetParamId(0); } else { toolFile = m_DefToolCsFileName; } List<string> codes; if (!m_ToolCodes.TryGetValue(toolFile, out codes)) { codes = new List<string>(); m_ToolCodes.Add(toolFile, codes); } codes.Add(funcData.GetExternScript()); } } else { LogSystem.Error("tool {0} must end with ';' ! line {1}", info.ToScriptString(), info.GetLine()); haveError = true; } } else if (info.GetId() == "global") { if (info.Functions.Count == 1) { FunctionData funcData = info.First; if (null != funcData && funcData.HaveExternScript()) { string globalFile; CallData callData = funcData.Call; if (null != callData && callData.GetParamNum() == 1) { globalFile = callData.GetParamId(0); } else { globalFile = m_DefGlobalCsFileName; } List<string> codes; if (!m_GlobalCodes.TryGetValue(globalFile, out codes)) { codes = new List<string>(); m_GlobalCodes.Add(globalFile, codes); } codes.Add(funcData.GetExternScript()); } } else { LogSystem.Error("global {0} must end with ';' ! line {1}", info.ToScriptString(), info.GetLine()); haveError = true; } } else if (info.GetId() == "typedef") { if (info.Functions.Count == 1) { FunctionData funcData = info.First; if (null != funcData) { CallData callData = funcData.Call; if (null != callData && callData.GetParamNum() == 1) { string typeName = callData.GetParamId(0); TypeDef typeDef = new TypeDef(); typeDef.m_TypeName = typeName; if (m_Types.ContainsKey(typeName)) { m_Types[typeName] = typeDef; } else { m_Types.Add(typeName, typeDef); } foreach (ISyntaxComponent comp in funcData.Statements) { FunctionData item = comp as FunctionData; if (null != item) { if (item.HaveExternScript()) { if (item.GetId() == "binary") { typeDef.m_BinaryCode = item.GetExternScript(); } else if (item.GetId() == "record") { typeDef.m_RecordCode = item.GetExternScript(); } } else { LogSystem.Error("typedef {0} must contains code ! line {1}", comp.ToScriptString(), comp.GetLine()); haveError = true; } } else { CallData call = comp as CallData; if (null != call && call.GetParamNum() == 1) { if (call.GetId() == "type") { typeDef.m_TypeCode = call.GetParamId(0); } } else { LogSystem.Error("typedef {0} must have params or end with ';' ! line {1}", comp.ToScriptString(), comp.GetLine()); haveError = true; } } } } else { LogSystem.Error("typedef {0} must have 1 params ! line {1}", info.ToScriptString(), info.GetLine()); haveError = true; } } } else { LogSystem.Error("typedef {0} must end with ';' ! line {1}", info.ToScriptString(), info.GetLine()); haveError = true; } } else if (info.GetId() == "tabledef") { if (info.Functions.Count == 1) { FunctionData funcData = info.First; if (null != funcData) { CallData callData = funcData.Call; if (null != callData && callData.GetParamNum() == 3) { TableFileTypeEnum fileType = TableFileTypeEnum.PUBLIC; string tableName, tableType, recordName, providerName; tableName = callData.GetParamId(0); tableType = callData.GetParamId(1); recordName = tableName; providerName = tableName + "Provider"; string fileTypeStr = callData.GetParamId(2); if (0 == fileTypeStr.CompareTo("client")) { fileType = TableFileTypeEnum.CLIENT; } else if (0 == fileTypeStr.CompareTo("server")) { fileType = TableFileTypeEnum.SERVER; } else if (0 == fileTypeStr.CompareTo("multifile")) { fileType = TableFileTypeEnum.MULTIFILE; } else { fileType = TableFileTypeEnum.PUBLIC; } TableDef tableDef = new TableDef(); tableDef.m_TableName = tableName; tableDef.m_Type = tableType; tableDef.m_FileType = fileType; tableDef.m_RecordName = recordName; tableDef.m_ProviderName = providerName; if (m_Tables.ContainsKey(tableName)) { m_Tables[tableName] = tableDef; } else { m_Tables.Add(tableName, tableDef); } foreach (ISyntaxComponent comp in funcData.Statements) { CallData field = comp as CallData; if (null != field) { if (field.GetId() == "recordname") { tableDef.m_RecordName = field.GetParamId(0); tableDef.m_ProviderName = tableDef.m_RecordName + "Provider"; } else if (field.GetId() == "providername") { tableDef.m_ProviderName = field.GetParamId(0); } else if (field.GetId() == "filename") { tableDef.m_CsFileName = field.GetParamId(0); } else if (field.GetId() == "recordmodifier") { tableDef.m_RecordModifier = field.GetParamId(0); if (tableDef.m_RecordModifier.Length > 0 && tableDef.m_RecordModifier[0] != ' ') { tableDef.m_RecordModifier = " " + tableDef.m_RecordModifier; } } else if (field.GetId() == "providermodifier") { tableDef.m_ProviderModifier = field.GetParamId(0); if (tableDef.m_ProviderModifier.Length > 0 && tableDef.m_ProviderModifier[0] != ' ') { tableDef.m_ProviderModifier = " " + tableDef.m_ProviderModifier; } } else if (field.GetId() == "fielddef" && field.GetParamNum() >= 3) { FieldDef fieldDef = new FieldDef(); fieldDef.m_MemberName = field.GetParamId(0); fieldDef.m_FieldName = field.GetParamId(1); fieldDef.m_Type = field.GetParamId(2); if (field.GetParamNum() >= 4) { ISyntaxComponent param = field.GetParam(3); if (null != param) { fieldDef.m_Default = param.GetId(); } } else { if (0 == fieldDef.m_Type.CompareTo("string")) { fieldDef.m_Default = ""; } else if (fieldDef.m_Type.Contains("_list") || fieldDef.m_Type.Contains("_array")) { fieldDef.m_Default = "null"; } else if (0 == fieldDef.m_Type.CompareTo("bool")) { fieldDef.m_Default = "false"; } else { fieldDef.m_Default = "0"; } } if (field.GetParamNum() >= 5) { fieldDef.m_Access = field.GetParamId(4); } else { fieldDef.m_Access = "public"; } tableDef.m_Fields.Add(fieldDef); } else { LogSystem.Error("field {0} must have name (member and field) and type ! line {1}", comp.ToScriptString(), comp.GetLine()); haveError = true; } } else { LogSystem.Error("field {0} must have name (member and field) and type and end with ';' ! line {1}", comp.ToScriptString(), comp.GetLine()); haveError = true; } } } else { LogSystem.Error("tabledef {0} must have 3 params ! line {1}", info.ToScriptString(), info.GetLine()); haveError = true; } } } else { LogSystem.Error("tabledef {0} must end with ';' ! line {1}", info.ToScriptString(), info.GetLine()); haveError = true; } } else { LogSystem.Error("Unknown part {0}, line {1}", info.GetId(), info.GetLine()); haveError = true; } } m_Types.Add("string", new TypeDef("string", "string", "{0} = DataRecordUtility.ExtractString(node, \"{1}\", \"{2}\");", "{0} = DataRecordUtility.ExtractString(table, record.{0}, \"{1}\");")); m_Types.Add("bool", new TypeDef("bool", "bool", "{0} = DataRecordUtility.ExtractBool(node, \"{1}\", {2});", "{0} = DataRecordUtility.ExtractBool(table, record.{0}, {1});")); m_Types.Add("int", new TypeDef("int", "int", "{0} = DataRecordUtility.ExtractNumeric<int>(node, \"{1}\", {2});", "{0} = DataRecordUtility.ExtractInt(table, record.{0}, {1});")); m_Types.Add("int32", new TypeDef("int", "int", "{0} = DataRecordUtility.ExtractNumeric<int>(node, \"{1}\", {2});", "{0} = DataRecordUtility.ExtractInt(table, record.{0}, {1});")); m_Types.Add("float", new TypeDef("float", "float", "{0} = DataRecordUtility.ExtractNumeric<float>(node, \"{1}\", {2});", "{0} = DataRecordUtility.ExtractFloat(table, record.{0}, {1});")); m_Types.Add("long", new TypeDef("long", "long", "{0} = DataRecordUtility.ExtractNumeric<long>(node, \"{1}\", {2});", "{0} = DataRecordUtility.ExtractLong(table, record.{0}, {1});")); m_Types.Add("int64", new TypeDef("long", "long", "{0} = DataRecordUtility.ExtractNumeric<long>(node, \"{1}\", {2});", "{0} = DataRecordUtility.ExtractLong(table, record.{0}, {1});")); m_Types.Add("string_list", new TypeDef("string_list", "List<string>", "{0} = DataRecordUtility.ExtractStringList(node, \"{1}\", {2});", "{0} = DataRecordUtility.ExtractStringList(table, record.{0}, {1});")); m_Types.Add("string_array", new TypeDef("string_array", "string[]", "{0} = DataRecordUtility.ExtractStringArray(node, \"{1}\", {2});", "{0} = DataRecordUtility.ExtractStringArray(table, record.{0}, {1});")); m_Types.Add("int_list", new TypeDef("int_list", "List<int>", "{0} = DataRecordUtility.ExtractNumericList<int>(node, \"{1}\", {2});", "{0} = DataRecordUtility.ExtractIntList(table, record.{0}, {1});")); m_Types.Add("int_array", new TypeDef("int_array", "int[]", "{0} = DataRecordUtility.ExtractNumericArray<int>(node, \"{1}\", {2});", "{0} = DataRecordUtility.ExtractIntArray(table, record.{0}, {1});")); m_Types.Add("float_list", new TypeDef("float_list", "List<float>", "{0} = DataRecordUtility.ExtractNumericList<float>(node, \"{1}\", {2});", "{0} = DataRecordUtility.ExtractFloatList(table, record.{0}, {1});")); m_Types.Add("float_array", new TypeDef("float_array", "float[]", "{0} = DataRecordUtility.ExtractNumericArray<float>(node, \"{1}\", {2});", "{0} = DataRecordUtility.ExtractFloatArray(table, record.{0}, {1});")); m_Types.Add("int32_list", new TypeDef("int_list", "List<int>", "{0} = DataRecordUtility.ExtractNumericList<int>(node, \"{1}\", {2});", "{0} = DataRecordUtility.ExtractIntList(table, record.{0}, {1});")); m_Types.Add("int32_array", new TypeDef("int_array", "int[]", "{0} = DataRecordUtility.ExtractNumericArray<int>(node, \"{1}\", {2});", "{0} = DataRecordUtility.ExtractIntArray(table, record.{0}, {1});")); foreach (var pair in m_Tables) { TableDef tableDef = pair.Value; if (0 == tableDef.m_Type.CompareTo("dictionary")) { if (tableDef.m_Fields.Count > 0) { tableDef.m_KeyFieldMemberName = tableDef.m_Fields[0].m_MemberName; TypeDef keyTypeDef; if (m_Types.TryGetValue(tableDef.m_Fields[0].m_Type, out keyTypeDef)) { tableDef.m_KeyFieldTypeCode = keyTypeDef.m_TypeCode; tableDef.m_ContainerType = string.Format("DataDictionaryMgr<{0},{1}>", tableDef.m_TableName, tableDef.m_KeyFieldTypeCode); } else { Console.WriteLine("Can't find type {0}'s definition ! table {1}", tableDef.m_KeyFieldTypeCode, pair.Key); haveError = true; } } } else { tableDef.m_ContainerType = string.Format("DataListMgr<{0}>", tableDef.m_TableName); } } return !haveError; } catch (Exception ex) { Console.WriteLine(ex); } return false; }
internal bool Init(string dslFile) { m_DslFile = dslFile; try { DslFile file = new DslFile(); if (!file.Load(dslFile, LogSystem.Log)) { LogSystem.Error("DSL {0} load failed !", dslFile); return false; } bool haveError = false; foreach (DslInfo info in file.DslInfos) { if (info.GetId() == "explanationfile") { if (info.Functions.Count == 1) { FunctionData funcData = info.First; CallData call = funcData.Call; if (null != call && call.GetParamNum() >= 1) { string commentFile = call.GetParamId(0); string path = Path.Combine(Path.GetDirectoryName(m_DslFile), commentFile); try { string[] lines = File.ReadAllLines(path); m_Explanation.AddRange(lines); } catch(Exception fileEx) { LogSystem.Error("File.ReadAllLines({0}) throw exception {1}, line {2} file {3}", fileEx.Message, path, info.GetLine(), dslFile); haveError = true; } } else { LogSystem.Error("explanationfile must have param file , line {0} file {1}", info.GetLine(), dslFile); haveError = true; } } else { LogSystem.Error("explanationfile must end with ';' , line {0} file {1}", info.GetLine(), dslFile); haveError = true; } } else if (info.GetId() == "explanation") { if (info.Functions.Count == 1) { FunctionData funcData = info.First; CallData call = funcData.Call; if (null != call && call.GetParamNum() >= 1) { string line = call.GetParamId(0); m_Explanation.Add(line); } else { LogSystem.Error("explanation must have param explanation , line {0} file {1}", info.GetLine(), dslFile); haveError = true; } } else { LogSystem.Error("explanation must end with ';' , line {0} file {1}", info.GetLine(), dslFile); haveError = true; } } else if (info.GetId() == "version") { if (info.Functions.Count == 1) { FunctionData funcData = info.First; CallData call = funcData.Call; if (null != call && call.GetParamNum() >= 1) { m_Version = call.GetParamId(0); } else { LogSystem.Error("version must have param version , line {0} file {1}", info.GetLine(), dslFile); haveError = true; } } else { LogSystem.Error("version must end with ';' , line {0} file {1}", info.GetLine(), dslFile); haveError = true; } } else if (info.GetId() == "package") { if (info.Functions.Count == 1) { FunctionData funcData = info.First; CallData call = funcData.Call; if (null != call && call.GetParamNum() >= 1) { m_Package = call.GetParamId(0); } else { LogSystem.Error("package must have param package , line {0} file {1}", info.GetLine(), dslFile); haveError = true; } } else { LogSystem.Error("package must end with ';' , line {0} file {1}", info.GetLine(), dslFile); haveError = true; } } else if (info.GetId() == "defaultsize") { if (info.Functions.Count == 1) { FunctionData funcData = info.First; CallData call = funcData.Call; if (null != call && call.GetParamNum() >= 1) { m_DefVarcharSize = int.Parse(call.GetParamId(0)); } else { LogSystem.Error("defaultsize must have param size , line {0} file {1}", info.GetLine(), dslFile); haveError = true; } } else { LogSystem.Error("defaultsize must end with ';' , line {0} file {1}", info.GetLine(), dslFile); haveError = true; } } else if (info.GetId() == "import") { if (info.Functions.Count == 1) { FunctionData funcData = info.First; CallData call = funcData.Call; if (null != call && call.GetParamNum() >= 1) { string importDslFile = call.GetParamId(0); if (string.IsNullOrEmpty(importDslFile)) { LogSystem.Error("import must have param dslfile, line {0} file {1}", info.GetLine(), dslFile); haveError = true; } else { string filePath = Path.Combine(Path.GetDirectoryName(dslFile), importDslFile); MessageDslParser parser = new MessageDslParser(); if (parser.Init(filePath)) { if (m_Explanation.Count <= 0 && parser.m_Explanation.Count > 0) { m_Explanation.AddRange(parser.m_Explanation); } if (string.IsNullOrEmpty(m_Version) && !string.IsNullOrEmpty(parser.m_Version)) { m_Version = parser.m_Version; } if (string.IsNullOrEmpty(m_Package) && !string.IsNullOrEmpty(parser.m_Package)) { m_Package = parser.m_Package; } if (m_DefVarcharSize == c_DefVarcharSize && parser.m_DefVarcharSize != c_DefVarcharSize) { m_DefVarcharSize = parser.m_DefVarcharSize; } foreach (var pair in parser.m_TypeConverters) { m_TypeConverters.Add(pair.Key, pair.Value); } foreach (var pair in parser.m_EnumTypes) { m_EnumTypes.Add(pair.Key, pair.Value); } foreach (var pair in parser.m_Messages) { m_Messages.Add(pair.Key, pair.Value); } foreach (string name in parser.m_SortedMessageEnums) { m_SortedMessageEnums.Add(name); } } } } } else { LogSystem.Error("import must end with ';' , line {0} file {1}", info.GetLine(), dslFile); haveError = true; } } else if (info.GetId() == "typeconverter") { if (info.Functions.Count == 1) { FunctionData funcData = info.First; if (null != funcData) { CallData callData = funcData.Call; if (null != callData && callData.GetParamNum() >= 1) { string logicTypeName = callData.GetParamId(0); TypeConverterDef typeConverter = new TypeConverterDef(); typeConverter.m_LogicType = logicTypeName; typeConverter.m_IsSpecial = false; if (m_TypeConverters.ContainsKey(logicTypeName)) { m_TypeConverters[logicTypeName] = typeConverter; } else { m_TypeConverters.Add(logicTypeName, typeConverter); } foreach (ISyntaxComponent comp in funcData.Statements) { FunctionData item = comp as FunctionData; if (null != item) { if (item.HaveExternScript()) { if (item.GetId() == "message2logic") { typeConverter.m_Message2LogicCode = item.GetExternScript(); } else if (item.GetId() == "logic2message") { typeConverter.m_Logic2MessageCode = item.GetExternScript(); } else if (item.GetId() == "crudcode") { typeConverter.m_IsSpecial = true; typeConverter.m_CrudCode = item.GetExternScript(); } } else { LogSystem.Error("typeconverter {0} member must contains code ! line {1} file {2}", comp.ToScriptString(), comp.GetLine(), dslFile); haveError = true; } } else { CallData converterData = comp as CallData; if (null != converterData && converterData.GetParamNum() == 1) { string id = converterData.GetId(); string param = converterData.GetParamId(0); if (id == "messagetype") { typeConverter.m_MessageType = param; } } else { if (comp is ValueData || null != converterData) { LogSystem.Error("typeconverter {0} member must have params ! line {1} file {2}", comp.ToScriptString(), comp.GetLine(), dslFile); haveError = true; } else { LogSystem.Error("typeconverter {0} member must end with ';' ! line {1} file {2}", comp.ToScriptString(), comp.GetLine(), dslFile); haveError = true; } } } } if (string.IsNullOrEmpty(typeConverter.m_LogicType) || string.IsNullOrEmpty(typeConverter.m_MessageType) || string.IsNullOrEmpty(typeConverter.m_Message2LogicCode) || string.IsNullOrEmpty(typeConverter.m_Logic2MessageCode)) { LogSystem.Error("typeconverter {0} error, logictype or messagetype or message2logic or logic2message is null or empty ! line {1} file {2}", funcData.ToScriptString(), funcData.GetLine(), dslFile); haveError = true; } if (typeConverter.m_IsSpecial && string.IsNullOrEmpty(typeConverter.m_CrudCode)) { LogSystem.Error("typeconverter {0} error, special type must have crudcode ! line {1} file {2}", funcData.ToScriptString(), funcData.GetLine(), dslFile); haveError = true; } } else { LogSystem.Error("typeconverter {0} must have 1 params ! line {1} file {2}", funcData.ToScriptString(), funcData.GetLine(), dslFile); haveError = true; } } else { LogSystem.Error("typeconverter {0} must have function body ! line {1} file {2}", info.ToScriptString(), info.GetLine(), dslFile); haveError = true; } } else { LogSystem.Error("typeconverter {0} must end with ';' ! line {1} file {2}", info.ToScriptString(), info.GetLine(), dslFile); haveError = true; } } else if (info.GetId() == "enum") { if (info.Functions.Count == 1) { FunctionData funcData = info.First; ParseEnum(dslFile, funcData, string.Empty, m_EnumTypes, ref haveError); } else { LogSystem.Error("enum must end with ';' , line {0} file {1}", info.GetLine(), dslFile); haveError = true; } } else if (info.GetId() == "message") { if (info.Functions.Count == 1) { FunctionData funcData = info.First; string typeName = ParseMessage(dslFile, funcData, string.Empty, false, m_Messages, ref haveError); if (!string.IsNullOrEmpty(typeName) && !haveError) { m_SortedMessageEnums.Add(typeName); } } else { LogSystem.Error("message must end with ';' , line {0} file {1}", info.GetLine(), dslFile); haveError = true; } } else { LogSystem.Error("Unknown part {0}, line {1} file {2}", info.GetId(), info.GetLine(), dslFile); haveError = true; } } return !haveError; } catch (Exception ex) { Console.WriteLine(ex); } return false; }