PageRenderTime 34ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/contrib/ScriptsConverter/ScriptConverter/Program.cs

https://bitbucket.org/CatrinaEmu/core/
C# | 226 lines | 216 code | 9 blank | 1 comment | 36 complexity | a31f0d8e70eae996b3495cf63779fb01 MD5 | raw file
Possible License(s): GPL-2.0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Text.RegularExpressions;
  7. using System.IO;
  8. namespace ScriptConverter {
  9. class Program {
  10. static void Main(string[] args) {
  11. if (args.Length != 1) {
  12. Console.WriteLine("Usage: ScriptsConverter.exe [path_to_dir|path_to_file]");
  13. } else {
  14. string path = args[0];
  15. if (File.Exists(path)) {
  16. ProcessFile(path);
  17. } else if (Directory.Exists(path)) {
  18. ProcessDirectory(path);
  19. } else {
  20. Console.WriteLine("Invalid file or directory specified.\r\n\r\nUsage: ScriptsConverter.exe [path_to_dir|path_to_file]");
  21. }
  22. }
  23. }
  24. static void ProcessDirectory(string path) {
  25. string[] files = Directory.GetFiles(path, "*.cpp");
  26. foreach (string file in files) {
  27. ProcessFile(file);
  28. }
  29. string[] dirs = Directory.GetDirectories(path);
  30. foreach (string dir in dirs) {
  31. ProcessDirectory(dir);
  32. }
  33. }
  34. class ScriptData {
  35. public int type = 0;
  36. public string name;
  37. public ArrayList methods = new ArrayList();
  38. public string instanceName = null;
  39. public string aiName = null;
  40. public string[] special = new string[] { "GetAI_", "GetInstance_", "GetInstanceData_" };
  41. public void AddMethod(string method) {
  42. methods.Add(method);
  43. int i = 0;
  44. foreach (string s in special) {
  45. ++i;
  46. int pos = method.IndexOf(s);
  47. if (pos != -1) {
  48. type = i;
  49. string name = method.Substring(pos + s.Length);
  50. if (i == 1) {
  51. aiName = name + "AI";
  52. }
  53. if (i == 2 || i == 3)
  54. instanceName = name;
  55. }
  56. }
  57. }
  58. public override string ToString() {
  59. StringBuilder sb = new StringBuilder();
  60. sb.AppendFormat("Script: {0}\n", name);
  61. foreach (string method in methods)
  62. sb.Append(" ").Append(method).Append("\n");
  63. sb.Append("\n");
  64. return sb.ToString();
  65. }
  66. }
  67. static string GetMethod(string method, ref string txt, ref int minPos) {
  68. string res = null;
  69. Regex r = new Regex(method + "(\\s|:|[(])");
  70. Match m = r.Match(txt);
  71. if (m.Success) {
  72. int pos = m.Index;
  73. while (pos-- >= 0 && pos < txt.Length) {
  74. if (txt[pos] == '\n') break;
  75. }
  76. //pos++;
  77. int lastPos = txt.IndexOf("\n}", pos);
  78. if (lastPos != -1) {
  79. lastPos += 2;
  80. while (lastPos++ >= 0 && lastPos < txt.Length) {
  81. if (txt[lastPos] == '\n') break;
  82. }
  83. res = txt.Substring(pos, lastPos - pos);
  84. txt = txt.Remove(pos, lastPos - pos);
  85. }
  86. if (pos < minPos)
  87. minPos = pos;
  88. }
  89. return res;
  90. }
  91. static void ProcessFile(string filePath) {
  92. Console.WriteLine(filePath);
  93. string txt = File.ReadAllText(filePath);
  94. string[] lines = File.ReadAllLines(filePath);
  95. Array.Reverse(lines);
  96. ArrayList scripts = new ArrayList();
  97. ScriptData data = null;
  98. bool scriptStart = false;
  99. foreach (string line in lines) {
  100. if (line.IndexOf("Script *") != -1) {
  101. break;
  102. }
  103. if (line.IndexOf("->RegisterSelf();") != -1) {
  104. scriptStart = true;
  105. data = new ScriptData();
  106. continue;
  107. }
  108. if (scriptStart) {
  109. if (line.IndexOf("= new Script") != -1) {
  110. scriptStart = false;
  111. scripts.Add(data);
  112. data = null;
  113. continue;
  114. }
  115. Regex r = new Regex("newscript->([a-zA-Z]+) *= *&?([\"_a-zA-Z0-9]+);");
  116. Match m = r.Match(line);
  117. if (m.Success) {
  118. if (m.Groups[1].Value.Equals("Name")) {
  119. data.name = m.Groups[2].Value.Trim(new char[] { '"' });
  120. } else {
  121. data.AddMethod(m.Groups[2].Value);
  122. }
  123. }
  124. continue;
  125. }
  126. }
  127. if (scripts.Count != 0) {
  128. string register = "";
  129. foreach (ScriptData sd in scripts) {
  130. string ss = "";
  131. Console.WriteLine(sd);
  132. int minPos = txt.Length;
  133. foreach (string method in sd.methods) {
  134. string s = GetMethod(method, ref txt, ref minPos);
  135. ss += s + "\n";
  136. }
  137. if (sd.instanceName != null) {
  138. string s = GetMethod("struct " + sd.instanceName, ref txt, ref minPos);
  139. ss += s + "\n";
  140. }
  141. if (sd.aiName != null) {
  142. string ai = GetMethod("struct " + sd.aiName, ref txt, ref minPos);
  143. if (ai != null) {
  144. string sm = null;
  145. Regex r = new Regex("\\S+ " + sd.aiName + "::([^( ]+)");
  146. while (r.IsMatch(txt)) {
  147. Match m = r.Match(txt);
  148. int startPos = m.Index;
  149. int endPos = txt.IndexOf("\n}", startPos);
  150. if (endPos != -1)
  151. endPos += 2;
  152. while (endPos++ >= 0 && endPos < txt.Length) {
  153. if (txt[endPos] == '\n') break;
  154. }
  155. sm = txt.Substring(startPos, endPos - startPos);
  156. txt = txt.Remove(startPos, endPos - startPos);
  157. if (sm != null) {
  158. sm = sm.Replace("\n", "\n ");
  159. Regex r1 = new Regex("\\S+ " + m.Groups[1] + " *\\([^)]*\\) *;");
  160. Match m1 = r1.Match(ai);
  161. if (m1.Success) {
  162. ai = r1.Replace(ai, sm);
  163. }
  164. }
  165. }
  166. ai = ai.Replace(sd.aiName + "::", "");
  167. ss += ai + "\n";
  168. }
  169. }
  170. if (ss.Length != 0) {
  171. string typeName = "UnknownScript";
  172. switch (sd.type) {
  173. case 1: typeName = "CreatureScript"; break;
  174. case 2: typeName = "InstanceMapScript"; break;
  175. default:
  176. if (sd.name.IndexOf("npc") == 0)
  177. typeName = "CreatureScript";
  178. else if (sd.name.IndexOf("mob") == 0)
  179. typeName = "CreatureScript";
  180. else if (sd.name.IndexOf("boss_") == 0)
  181. typeName = "CreatureScript";
  182. else if (sd.name.IndexOf("item_") == 0)
  183. typeName = "ItemScript";
  184. else if (sd.name.IndexOf("go_") == 0)
  185. typeName = "GameObjectScript";
  186. else if (sd.name.IndexOf("at_") == 0)
  187. typeName = "AreaTriggerScript";
  188. else if (sd.name.IndexOf("instance_") == 0)
  189. typeName = "InstanceMapScript";
  190. break;
  191. }
  192. if (sd.instanceName != null)
  193. ss = ss.Replace(sd.instanceName, sd.instanceName + "_InstanceMapScript");
  194. ss = ss.Replace("\n", "\n ");
  195. ss = "class " + sd.name + " : public " + typeName + "\n{\npublic:\n " +
  196. sd.name + "() : " + typeName + "(\"" + sd.name + "\") { }\n" + ss + "\n};";
  197. ss = ss.Replace("_" + sd.name, "");
  198. ss = ss.Replace("AIAI", "AI");
  199. ss = ss.Replace(" \r\n", "\r\n");
  200. ss = ss.Replace(" \n", "\n");
  201. txt = txt.Insert(minPos, ss);
  202. register = " new " + sd.name + "();\n" + register;
  203. }
  204. }
  205. Regex r2 = new Regex("void +AddSC_([_a-zA-Z0-9]+)");
  206. Match m2 = r2.Match(txt);
  207. if (m2.Success) {
  208. txt = txt.Remove(m2.Index);
  209. txt += "void AddSC_" + m2.Groups[1].Value + "()\n{\n" + register + "}\n";
  210. }
  211. // File.Copy(filePath, filePath + ".bkp");
  212. txt = txt.Replace("\r\n", "\n");
  213. File.WriteAllText(filePath, txt);
  214. }
  215. }
  216. }
  217. }