func main() { flag.Parse() source_dir := *g_Source target_dir := *g_Target source_dir = strings.TrimSuffix(source_dir, "/") target_dir = strings.TrimSuffix(target_dir, "/") fmt.Println(source_dir, target_dir, *g_Language) if !help.IsExist(source_dir) { fmt.Printf("找不到描述文件目录[%s]\n", source_dir) return } cd, _ := os.Getwd() fmt.Println(cd) fs := help.NewFileSearch() fs.Dir = source_dir fs.KeyWord = "/*.epd" fs.SubDir = false fl, _ := fs.RegSearchToList() for _, key := range fl { file_path := source_dir + "/" + key fi, err := os.Open(file_path) if err != nil { fmt.Println("读文件失败: %s", err.Error()) return } fd, err := ioutil.ReadAll(fi) fi.Close() switch *g_Language { case "go": proto.ParseToNewGolang(string(fd), target_dir, key+".go") proto.WriteAllEpdGoCode() case "c#": proto.ParseToNewGolang(string(fd), target_dir, key+".cs") proto.WriteAllEpdCSharpCode() // case "cpp": // proto.ParseToCpplang(string(fd), target_dir, key+".cpp") } } }
func writeGoCode(table *EB_FileTable) { // 检查log目录 if !help.IsExist(table.FileDir) { os.MkdirAll(table.FileDir, os.ModeDir) } target_file := table.FileDir + "/" + table.FileName if help.IsExist(target_file) { os.Remove(target_file) } file, err := os.OpenFile(target_file, os.O_RDWR|os.O_CREATE, os.ModePerm) if err != nil { panic(err.Error()) } // 文件头 file.WriteString( `// easybuff // 不要修改本文件, 每次消息有变动, 请手动生成本文件 // easybuff -s 描述文件目录 -o 目标文件目录 -l 语言(go,cpp) package proto `) // 按照字母顺序 keys := make([]int, len(table.MyMsgIds)) i := 0 for key, _ := range table.MyMsgIds { keys[i] = key i++ } sort.Sort(sort.IntSlice(keys)) // 引用文件 writeInclude(file, table) // 枚举 file.WriteString("// ------ 枚举\n") file.WriteString("const(\n") for _, key := range keys { msg_name := table.MyMsgIds[key] switch g_ParseTable.Cells[msg_name].(type) { case *EB_Enum: d := g_ParseTable.Cells[msg_name].(*EB_Enum) comment_string := "" for k, _ := range d.Comment { if len(d.Comment[k]) > 0 { if len(comment_string) > 0 { comment_string += "," } comment_string += d.Comment[k] } } if len(comment_string) > 0 { file.WriteString(fmt.Sprintf("%s = %s // %s\n", d.Name, d.Value, comment_string)) } else { file.WriteString(fmt.Sprintf("%s = %s\n", d.Name, d.Value)) } } } file.WriteString(")\n\n") // 消息ID file.WriteString("// ------ 消息ID\n") file.WriteString("const(\n") for _, key := range keys { msg_name := table.MyMsgIds[key] switch g_ParseTable.Cells[msg_name].(type) { case *EB_Message: d := g_ParseTable.Cells[msg_name].(*EB_Message) comment_string := "" for k, _ := range d.Comment { if len(d.Comment[k]) > 0 { if len(comment_string) > 0 { comment_string += ", " } comment_string += d.Comment[k] } } if d.MsgId > 0 { if len(comment_string) > 0 { file.WriteString(fmt.Sprintf("%s_Id = %d// %s\n", d.Name, d.MsgId, comment_string)) } else { file.WriteString(fmt.Sprintf("%s_Id = %d\n", d.Name, d.MsgId)) } } else { panic(fmt.Sprintf("文件格式错误 : message [%s]的ID(%d)非法", d.Name, d.MsgId)) } } } file.WriteString(")\n\n") // 普通结构 writeStructs(file, table) // 消息结构 writeMessages(file, table) file.Close() cmd_data := exec.Command("gofmt", "-w", target_file) err = cmd_data.Run() if err != nil { fmt.Println(target_file + "," + err.Error()) } }
func writeCSharpCode(table *EB_FileTable) { // 检查log目录 if !help.IsExist(table.FileDir) { os.MkdirAll(table.FileDir, os.ModeDir) } target_file := table.FileDir + "/" + table.FileName if help.IsExist(target_file) { os.Remove(target_file) } file, err := os.OpenFile(target_file, os.O_RDWR|os.O_CREATE, os.ModePerm) if err != nil { panic(err.Error()) } // 文件名标志 reg := regexp.MustCompile(`[^/.]+`) file_flag := reg.FindString(table.FileName) // 文件头 WriteCSharpString(file, 0, "// easybuff") WriteCSharpString(file, 0, "// 不要修改本文件, 每次消息有变动, 请手动生成本文件") WriteCSharpString(file, 0, "// easybuff -s 描述文件目录 -o 目标文件目录 -l 语言(go,cpp,c#)") WriteCSharpString(file, 0, "") WriteCSharpString(file, 0, "using NetMsg;") WriteCSharpString(file, 0, "using System;") // 按照字母顺序 keys := make([]int, len(table.MyMsgIds)) i := 0 for key, _ := range table.MyMsgIds { keys[i] = key i++ } sort.Sort(sort.IntSlice(keys)) // 引用文件 writeIncludeCSharp(file, table) // 文件头 WriteCSharpString(file, 0, "namespace NetMsg") WriteCSharpString(file, 0, "{") // 枚举 WriteCSharpString(file, 1, "// ------ 枚举") // for _, key := range keys { // msg_name := table.MyMsgIds[key] // switch g_ParseTable.Cells[msg_name].(type) { // case *EB_Enum: // d := g_ParseTable.Cells[msg_name].(*EB_Enum) // comment_string := "" // for k, _ := range d.Comment { // if len(d.Comment[k]) > 0 { // if len(comment_string) > 0 { // comment_string += "," // } // comment_string += d.Comment[k] // } // } // if len(comment_string) > 0 { // file.WriteString(fmt.Sprintf("static int %s = %s; // %s", d.Name, d.Value, comment_string)) // } else { // file.WriteString(fmt.Sprintf("static int %s = %s;", d.Name, d.Value)) // } // } // } // 消息ID WriteCSharpString(file, 1, "// ------ 消息ID") WriteCSharpString(file, 1, fmt.Sprintf("enum MsgId_%s", file_flag)) WriteCSharpString(file, 1, fmt.Sprintf("{")) for _, key := range keys { msg_name := table.MyMsgIds[key] switch g_ParseTable.Cells[msg_name].(type) { case *EB_Message: d := g_ParseTable.Cells[msg_name].(*EB_Message) comment_string := "" for k, _ := range d.Comment { if len(d.Comment[k]) > 0 { if len(comment_string) > 0 { comment_string += ", " } comment_string += d.Comment[k] } } if d.MsgId > 0 { if len(comment_string) > 0 { WriteCSharpString(file, 2, fmt.Sprintf("%s_Id = %d, // %s", d.Name, d.MsgId, comment_string)) } else { WriteCSharpString(file, 2, fmt.Sprintf("%s_Id = %d,", d.Name, d.MsgId)) } } else { panic(fmt.Sprintf("文件格式错误 : message [%s]的ID(%d)非法", d.Name, d.MsgId)) } } } WriteCSharpString(file, 1, fmt.Sprintf("}")) // 普通结构 writeStructsCSharp(file, table) // 消息结构 writeMessagesCSharp(file, table, fmt.Sprintf("MsgId_%s", file_flag)) WriteCSharpString(file, 0, "}") file.Close() // cmd_data := exec.Command("gofmt", "-w", target_file) // err = cmd_data.Run() // if err != nil { // fmt.Println(target_file + "," + err.Error()) // } }