generator.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. package main
  2. import (
  3. "Server-Core/Server/Base"
  4. "fmt"
  5. "net"
  6. "os"
  7. )
  8. func WriteS(f *os.File, s []byte) bool {
  9. _, err := f.Write(s)
  10. if err != nil {
  11. fmt.Println(err)
  12. return false
  13. }
  14. return true
  15. }
  16. func GeneratorDef() bool {
  17. //file, err := os.OpenFile("../../Server/Scheme/SchemeDef.go", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0666)
  18. //files, _ = ioutil.ReadDir("E://ChatGameService2/Server/Scheme")
  19. file, err := os.OpenFile("../../Server/Scheme/SchemeDef.go", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0666)
  20. if err != nil {
  21. fmt.Println(err)
  22. return false
  23. }
  24. defer file.Close()
  25. var s = "package scheme\n\n"
  26. s += "type ID int32\n"
  27. s += "type G int32\n\n"
  28. s += "const (\n\t"
  29. s += "ID_Min = iota\n\t"
  30. for _, name := range FileList {
  31. s += "ID_" + name + "\n\t"
  32. }
  33. s += "ID_Max\n"
  34. s += ")\n\n"
  35. s += "const (\n"
  36. //records := base.LoadCsvCfg("../../Bin/Scp/Rule.csv").Records
  37. //records := base.LoadCsvCfg("./Scp/Rule.csv").Records
  38. var path string = ""
  39. if ip == "192.168.1.122" {
  40. path = "E://ChatGameService2/Bin/Scp/Role.csv"
  41. } else {
  42. // records := base.LoadCsvCfg("E://ChatGameService2/Bin/Scp/Level.csv").Records
  43. path = "../Scp/Level.csv"
  44. }
  45. records := base.LoadCsvCfg(path).Records
  46. //records := base.LoadCsvCfg("E://ChatGameService2/Bin/Scp/Rule.csv").Records
  47. for i := 4; i < len(records); i++ {
  48. s += "\tG_" + records[i].Record["name"] + " = " + records[i].Record["id"] + "\n"
  49. }
  50. s += ")\n\n"
  51. s += "type Scp interface {\n\t"
  52. s += "Load()\n\t"
  53. s += "Get(key int32) (interface{}, error)\n"
  54. s += "}\n\n"
  55. s += "func Register() {\n"
  56. for _, name := range FileList {
  57. s += "\tmgr.schemeList[ID_" + name + "] = new(" + name + ")\n"
  58. }
  59. s += "}"
  60. if !WriteS(file, []byte(s)) {
  61. return false
  62. }
  63. return true
  64. }
  65. var ip string = ""
  66. func getIp() string {
  67. addrList, err := net.InterfaceAddrs()
  68. if err != nil {
  69. panic(err)
  70. }
  71. for _, address := range addrList {
  72. if ipNet, ok := address.(*net.IPNet); ok && !ipNet.IP.IsLoopback() {
  73. if ipNet.IP.To4() != nil {
  74. fmt.Println(ipNet.IP.String())
  75. ip = ipNet.IP.String()
  76. break
  77. }
  78. }
  79. }
  80. return ip
  81. }
  82. func GeneratorScp() bool {
  83. for _, name := range FileList {
  84. fileName := "Scheme" + name + ".go"
  85. if SchemeMap[name] == true {
  86. fmt.Println(fileName + "已经存在, 忽略")
  87. continue
  88. }
  89. fmt.Println("开始生成" + fileName)
  90. file, err := os.OpenFile("../../Server/Scheme/"+fileName, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0666)
  91. if err != nil {
  92. fmt.Println(err)
  93. return false
  94. }
  95. var s = "package scheme\n\n"
  96. s += "import (\n\t"
  97. s += "\"Server-Core/Server/Base\"\n\t"
  98. s += "\"reflect\"\n\t"
  99. s += "\"strconv\"\n"
  100. s += ")\n\n"
  101. s += "type " + name + "Item struct {\n"
  102. //r := base.LoadCsvCfg("../../Bin/Scp/" + name + ".csv")
  103. var path string = ""
  104. if ip == "192.168.1.122" {
  105. path = "E://ChatGameService2/Bin/Scp/" + name + ".csv"
  106. } else {
  107. // records := base.LoadCsvCfg("E://ChatGameService2/Bin/Scp/Level.csv").Records
  108. path = "./Scp/" + name + ".csv"
  109. }
  110. if ip == "192.168.1.122" {
  111. path = "E://ChatGameService2/Bin/Scp/" + name + ".csv"
  112. } else {
  113. // records := base.LoadCsvCfg("E://ChatGameService2/Bin/Scp/Level.csv").Records
  114. path = "./Scp/" + name + ".csv"
  115. }
  116. r := base.LoadCsvCfg(path)
  117. // r := base.LoadCsvCfg("./Scp/" + name + ".csv")
  118. //r := base.LoadCsvCfg("E://ChatGameService2/Bin/Scp/" + name + ".csv")
  119. records := r.Records
  120. primaryKey := ""
  121. for k, v := range records[1].Record {
  122. key := base.StrFirstToUpper(k)
  123. if k == r.Key {
  124. primaryKey = key
  125. }
  126. var value string
  127. switch v {
  128. case "INT":
  129. value = "int32"
  130. case "FLOAT":
  131. value = "float64"
  132. case "STRING":
  133. value = "string"
  134. default:
  135. fmt.Println("类型异常: " + v)
  136. return false
  137. }
  138. s += "\t" + key + " " + value + "\n"
  139. }
  140. s += "}\n\n"
  141. s += "type " + name + " struct {\n\t"
  142. s += "dataList map[int32]*" + name + "Item\n"
  143. s += "}\n\n"
  144. s += "func (scp *" + name + ") Load() {\n\t"
  145. s += "scp.dataList = make(map[int32]*" + name + "Item)\n\t"
  146. s += "records := base.LoadCsvCfg(\"../Scp/" + name + ".csv\").Records\n\t"
  147. s += "for i := 4; i < len(records); i++ {\n\t\t"
  148. s += "item := new(" + name + "Item)\n\t\t"
  149. s += "cr := reflect.ValueOf(item).Elem()\n\t\t"
  150. s += "for k, v := range records[i].Record {\n\t\t\t"
  151. s += "k = base.StrFirstToUpper(k)\n\t\t\t"
  152. s += "a := cr.FieldByName(k)\n\t\t\t"
  153. s += "switch a.Type().String() {\n\t\t\t"
  154. s += "case \"string\":\n\t\t\t\t"
  155. s += "a.Set(reflect.ValueOf(v))\n\t\t\t"
  156. s += "case \"int32\":\n\t\t\t\t"
  157. s += "i, _ := strconv.Atoi(v)\n\t\t\t\t"
  158. s += "a.Set(reflect.ValueOf(int32(i)))\n\t\t\t"
  159. s += "case \"float64\":\n\t\t\t\t"
  160. s += "f, _ := strconv.ParseFloat(v, 64)\n\t\t\t\t"
  161. s += "a.Set(reflect.ValueOf(f))\n\t\t\t"
  162. s += "}\n\t\t"
  163. s += "}\n\t\t"
  164. s += "scp.dataList[item." + primaryKey + "] = item\n\t"
  165. s += "}\n"
  166. s += "}\n\n"
  167. s += "func (scp *" + name + ") Get(key int32) interface{} {\n\t"
  168. s += "return scp.dataList[key]\n"
  169. s += "}"
  170. if !WriteS(file, []byte(s)) {
  171. return false
  172. }
  173. file.Close()
  174. }
  175. return true
  176. }