FormatHelper.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. using System;
  2. using System.Globalization;
  3. namespace FF8
  4. {
  5. public static class FormatHelper
  6. {
  7. private static readonly String[] Separators = {"\r\n", "\n", "{Line}", "{Next}"};
  8. public static void FormatMonologue(ScriptWriter sw, String message)
  9. {
  10. if (!sw.HasWhiteLine)
  11. sw.AppendLine();
  12. foreach (String str in SplitMonologue(message))
  13. {
  14. if (String.IsNullOrEmpty(str))
  15. continue;
  16. sw.Append("// ");
  17. sw.AppendLine(str);
  18. }
  19. }
  20. public static void FormatAnswers(ScriptWriter sw, String message, IJsmExpression top, IJsmExpression bottom, IJsmExpression begin, IJsmExpression cancel)
  21. {
  22. if (!(top is IConstExpression t) || !(bottom is IConstExpression b))
  23. {
  24. FormatMonologue(sw, message);
  25. return;
  26. }
  27. if (!sw.HasWhiteLine)
  28. sw.AppendLine();
  29. // Question
  30. //☞ Answer 1
  31. // Answer 2
  32. // Answer 3
  33. //☜ Answer 4
  34. Int32 to = t.Int32();
  35. Int32 bo = b.Int32();
  36. Int32 be = -1;
  37. Int32 ca = -1;
  38. if (begin is IConstExpression beg)
  39. be = beg.Int32();
  40. if (cancel is IConstExpression can)
  41. ca = can.Int32();
  42. String[] lines = SplitMonologue(message);
  43. for (Int32 i = 0; i < lines.Length; i++)
  44. {
  45. sw.Append("//");
  46. if (i >= to && i <= bo)
  47. {
  48. if (i == be)
  49. sw.Append("☞ ");
  50. else if (i == ca)
  51. sw.Append("☜ ");
  52. else
  53. sw.Append(" ");
  54. }
  55. else
  56. {
  57. sw.Append(" ");
  58. }
  59. sw.AppendLine(lines[i]);
  60. }
  61. }
  62. public static String[] SplitMonologue(String message)
  63. {
  64. return message.Split(Separators, StringSplitOptions.None);
  65. }
  66. public static void FormatGlobalGet<T>(GlobalVariableId<T> globalVariable, Int32[] knownVariables, ScriptWriter sw, IScriptFormatterContext formatterContext, IServices executionContext) where T: unmanaged
  67. {
  68. if (knownVariables == null || Array.BinarySearch(knownVariables, globalVariable.VariableId) < 0)
  69. {
  70. sw.Append("(");
  71. sw.Append(GlobalVariableId<T>.TypeName);
  72. sw.Append(")");
  73. sw.Append("G");
  74. }
  75. else
  76. {
  77. sw.Append("G");
  78. sw.Append(GlobalVariableId<T>.TypeName);
  79. }
  80. sw.Append("[");
  81. sw.Append(globalVariable.VariableId.ToString(CultureInfo.InvariantCulture));
  82. sw.Append("]");
  83. }
  84. public static void FormatGlobalSet<T>(GlobalVariableId<T> globalVariable, IJsmExpression value, Int32[] knownVariables, ScriptWriter sw, IScriptFormatterContext formatterContext, IServices executionContext) where T: unmanaged
  85. {
  86. sw.Append("G");
  87. if (knownVariables == null || Array.BinarySearch(knownVariables, globalVariable.VariableId) < 0)
  88. {
  89. sw.Append("[");
  90. sw.Append(globalVariable.VariableId.ToString(CultureInfo.InvariantCulture));
  91. sw.Append("]");
  92. sw.Append(" = ");
  93. sw.Append("(");
  94. sw.Append(GlobalVariableId<T>.TypeName);
  95. sw.Append(")");
  96. }
  97. else
  98. {
  99. sw.Append(GlobalVariableId<T>.TypeName);
  100. sw.Append("[");
  101. sw.Append(globalVariable.VariableId.ToString(CultureInfo.InvariantCulture));
  102. sw.Append("]");
  103. sw.Append(" = ");
  104. }
  105. value.Format(sw, formatterContext, executionContext);
  106. sw.AppendLine(";");
  107. }
  108. public static Formatter Format(this ScriptWriter sw, IScriptFormatterContext formatterContext, IServices executionContext)
  109. {
  110. return new Formatter(sw, formatterContext, executionContext);
  111. }
  112. public sealed class Formatter
  113. {
  114. public ScriptWriter Sw { get; }
  115. public IScriptFormatterContext FormatterContext { get; }
  116. public IServices ExecutionContext { get; }
  117. public Formatter(ScriptWriter sw, IScriptFormatterContext formatterContext, IServices executionContext)
  118. {
  119. Sw = sw;
  120. FormatterContext = formatterContext;
  121. ExecutionContext = executionContext;
  122. }
  123. public Formatter Await()
  124. {
  125. Sw.Append("await ");
  126. return this;
  127. }
  128. public PropertyFormatter Property(String methodName)
  129. {
  130. Sw.Append("this.");
  131. Sw.Append(methodName);
  132. return new PropertyFormatter(this);
  133. }
  134. public MethodFormatter Method(String methodName)
  135. {
  136. Sw.Append("this.");
  137. Sw.Append(methodName);
  138. Sw.Append("(");
  139. return new MethodFormatter(this);
  140. }
  141. public StaticTypeFormatter StaticType(String typeName)
  142. {
  143. Sw.Append(typeName);
  144. return new StaticTypeFormatter(this);
  145. }
  146. public Formatter CommentLine(String text)
  147. {
  148. if (!Sw.HasWhiteLine)
  149. Sw.AppendLine();
  150. Sw.Append("// ");
  151. Sw.AppendLine(text);
  152. return this;
  153. }
  154. }
  155. public sealed class StaticTypeFormatter
  156. {
  157. public Formatter Formatter { get; }
  158. public StaticTypeFormatter(Formatter formatter)
  159. {
  160. Formatter = formatter;
  161. }
  162. public PropertyFormatter Property(String propertyName)
  163. {
  164. Formatter.Sw.Append(".");
  165. Formatter.Sw.Append(propertyName);
  166. return new PropertyFormatter(Formatter);
  167. }
  168. public MethodFormatter Method(String methodName)
  169. {
  170. ScriptWriter sw = Formatter.Sw;
  171. sw.Append(".");
  172. sw.Append(methodName);
  173. sw.Append("(");
  174. return new MethodFormatter(Formatter);
  175. }
  176. }
  177. public sealed class PropertyFormatter
  178. {
  179. public Formatter Formatter { get; }
  180. public PropertyFormatter(Formatter formatter)
  181. {
  182. Formatter = formatter;
  183. }
  184. public PropertyFormatter Property(String propertyName)
  185. {
  186. Formatter.Sw.Append(".");
  187. Formatter.Sw.Append(propertyName);
  188. return new PropertyFormatter(Formatter);
  189. }
  190. public MethodFormatter Method(String methodName)
  191. {
  192. ScriptWriter sw = Formatter.Sw;
  193. sw.Append(".");
  194. sw.Append(methodName);
  195. sw.Append("(");
  196. return new MethodFormatter(Formatter);
  197. }
  198. public PropertyFormatter Assign(Boolean value)
  199. {
  200. ScriptWriter sw = Formatter.Sw;
  201. sw.Append(" = ");
  202. sw.Append(value ? "true" : "false");
  203. sw.Append(";");
  204. return this;
  205. }
  206. public PropertyFormatter Assign(Int64 value)
  207. {
  208. ScriptWriter sw = Formatter.Sw;
  209. sw.Append(" = ");
  210. sw.Append(value.ToString(CultureInfo.InvariantCulture));
  211. sw.Append(";");
  212. return this;
  213. }
  214. public PropertyFormatter Assign(IJsmExpression assignExpression)
  215. {
  216. ScriptWriter sw = Formatter.Sw;
  217. sw.Append(" = ");
  218. assignExpression.Format(sw, Formatter.FormatterContext, Formatter.ExecutionContext);
  219. sw.Append(";");
  220. return this;
  221. }
  222. public void Comment(String nativeName)
  223. {
  224. Formatter.Sw.Append(" // ");
  225. Formatter.Sw.AppendLine(nativeName);
  226. }
  227. }
  228. public sealed class MethodFormatter
  229. {
  230. public Formatter Formatter { get; }
  231. public MethodFormatter(Formatter formatter)
  232. {
  233. Formatter = formatter;
  234. }
  235. private Boolean _hasArguments = false;
  236. public MethodFormatter Enum<T>(T argumentValue) where T : struct
  237. {
  238. return Argument(typeof(T).Name + '.' + argumentValue);
  239. }
  240. public MethodFormatter Enum<T>(IJsmExpression argumentExpression) where T : struct
  241. {
  242. if (argumentExpression is IConstExpression expr)
  243. return Argument(typeof(T).Name + '.' + (T)(Object)expr.Int32());
  244. return Argument<T>(null, argumentExpression);
  245. }
  246. public MethodFormatter Argument(String argumentName, Int32 argumentValue)
  247. {
  248. return Argument(argumentName, argumentValue.ToString(CultureInfo.InvariantCulture));
  249. }
  250. public MethodFormatter Argument(String argumentName, Boolean argumentValue)
  251. {
  252. return Argument(argumentName, argumentValue ? "true" : "false");
  253. }
  254. public MethodFormatter Argument(String argumentName, IJsmExpression argumentExpression)
  255. {
  256. ScriptWriter sw = Formatter.Sw;
  257. if (_hasArguments)
  258. sw.Append(", ");
  259. if (argumentName != null)
  260. {
  261. sw.Append(argumentName);
  262. sw.Append(": ");
  263. }
  264. argumentExpression.Format(sw, Formatter.FormatterContext, Formatter.ExecutionContext);
  265. _hasArguments = true;
  266. return this;
  267. }
  268. public MethodFormatter Argument<T>(String argumentName, IJsmExpression argumentExpression)
  269. {
  270. ScriptWriter sw = Formatter.Sw;
  271. if (_hasArguments)
  272. sw.Append(", ");
  273. if (argumentName != null)
  274. {
  275. sw.Append(argumentName);
  276. sw.Append(": ");
  277. }
  278. sw.Append("(");
  279. sw.Append(typeof(T).Name);
  280. sw.Append(")");
  281. argumentExpression.Format(sw, Formatter.FormatterContext, Formatter.ExecutionContext);
  282. _hasArguments = true;
  283. return this;
  284. }
  285. public MethodFormatter Argument(String argumentValue)
  286. {
  287. ScriptWriter sw = Formatter.Sw;
  288. if (_hasArguments)
  289. sw.Append(", ");
  290. sw.Append(argumentValue);
  291. _hasArguments = true;
  292. return this;
  293. }
  294. public MethodFormatter Argument(String argumentName, String argumentValue)
  295. {
  296. ScriptWriter sw = Formatter.Sw;
  297. if (_hasArguments)
  298. sw.Append(", ");
  299. sw.Append(argumentName);
  300. sw.Append(": ");
  301. sw.Append(argumentValue);
  302. _hasArguments = true;
  303. return this;
  304. }
  305. public void Comment(String nativeName)
  306. {
  307. Formatter.Sw.Append("); // ");
  308. Formatter.Sw.AppendLine(nativeName);
  309. }
  310. }
  311. }
  312. }