public static string WriteToLua(PBMessageConfig config) { StringBuilder builder = new StringBuilder(); builder.AppendLine($"local {config.ClassName} = {{"); int indent = 0; foreach (var group in config.Groups) { indent++; { builder.AppendLine($"{GetIndent(indent)}--Begin {group.Name}--"); foreach (var message in group.Messages) { AppendMessage(builder, indent, message); } builder.AppendLine($"{GetIndent(indent)}--End {group.Name}--"); builder.AppendLine(); } indent--; } builder.AppendLine("}"); builder.AppendLine($"return {config.ClassName}"); return(builder.ToString()); }
static void OnSuccess(Options options) { if (string.IsNullOrEmpty(options.InputFilePath)) { Console.WriteLine("The path of input is empty"); return; } if (!File.Exists(options.InputFilePath)) { Console.WriteLine($"The path({options.InputFilePath}) is not found"); return; } if (!Directory.Exists(options.OutputFileDir)) { var dInfo = Directory.CreateDirectory(options.OutputFileDir); if (dInfo == null || !dInfo.Exists) { Console.WriteLine($"Create directory for outputFile({options.OutputFileDir}) failed"); return; } } PBMessageConfig config = XMLSerializeUtility.ReadFromFile <PBMessageConfig>(options.InputFilePath, false); if (config == null) { PrintLog(LogType.Error, "read config failed"); return; } if (string.IsNullOrEmpty(config.ClassName)) { PrintLog(LogType.Error, "the class-name of the config is empty"); return; } string content = null; if (options.FormatType == TargetFormatType.Lua) { content = PBConfigLuaWriter.WriteToLua(config); } if (string.IsNullOrEmpty(content)) { PrintLog(LogType.Error, "The content of the config is empty"); return; } string contentFilePath = $"{options.OutputFileDir}/{config.ClassName}{options.OutputFileExtension}"; File.WriteAllText(contentFilePath, content); }