Process.State.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Reflection;
  4. using NTERA.Core;
  5. using NTERA.EmuEra.Game.EraEmu.GameData.Expression;
  6. using NTERA.EmuEra.Game.EraEmu.GameData.Variable;
  7. using NTERA.EmuEra.Game.EraEmu.Sub;
  8. namespace NTERA.EmuEra.Game.EraEmu.GameProc
  9. {
  10. //1756 インナークラス解除して一般に開放
  11. //難読化用属性。enum.ToString()やenum.Parse()を行うなら(Exclude=true)にすること。
  12. [Obfuscation(Exclude = false)]
  13. internal enum SystemStateCode
  14. {
  15. __CAN_SAVE__ = 0x10000,//セーブロード画面を呼び出し可能か?
  16. __CAN_BEGIN__ = 0x20000,//BEGIN命令を呼び出し可能か?
  17. Title_Begin = 0,//初期状態
  18. Openning = 1,//最初の入力待ち
  19. Train_Begin = 0x10,//BEGIN TRAINから。
  20. Train_CallEventTrain = 0x11,//@EVENTTRAINの呼び出し中。スキップ可能
  21. Train_CallShowStatus = 0x12,//@SHOW_STATUSの呼び出し中
  22. Train_CallComAbleXX = 0x13,//@COM_ABLExxの呼び出し中。スキップの場合、RETURN 1とする。
  23. Train_CallShowUserCom = 0x14,//@SHOW_USERCOMの呼び出し中
  24. Train_WaitInput = 0x15,//入力待ち状態。選択が実行可能ならEVENTCOMからCOMxx、そうでなければ@USERCOMにRESULTを渡す
  25. Train_CallEventCom = 0x16 | __CAN_BEGIN__,//@EVENTCOMの呼び出し中
  26. Train_CallComXX = 0x17 | __CAN_BEGIN__,//@COMxxの呼び出し中
  27. Train_CallSourceCheck = 0x18 | __CAN_BEGIN__,//@SOURCE_CHECKの呼び出し中
  28. Train_CallEventComEnd = 0x19 | __CAN_BEGIN__,//@EVENTCOMENDの呼び出し中。スキップ可能。Train_CallEventTrainへ帰る。@USERCOMの呼び出し中もここ
  29. Train_DoTrain = 0x1A,
  30. AfterTrain_Begin = 0x20 | __CAN_BEGIN__,//BEGIN AFTERTRAINから。@EVENTENDを呼び出してNormalへ。
  31. Ablup_Begin = 0x30,//BEGIN ABLUPから。
  32. Ablup_CallShowJuel = 0x31,//@SHOW_JUEL
  33. Ablup_CallShowAblupSelect = 0x32,//@SHOW_ABLUP_SELECT
  34. Ablup_WaitInput = 0x33,//
  35. Ablup_CallAblupXX = 0x34 | __CAN_BEGIN__,//@ABLUPxxがない場合は、@USERABLUPにRESULTを渡す。Ablup_CallShowJuelへ戻る。
  36. Turnend_Begin = 0x40 | __CAN_BEGIN__,//BEGIN TURNENDから。@EVENTTURNENDを呼び出してNormalへ。
  37. Shop_Begin = 0x50 | __CAN_SAVE__,//BEGIN SHOPから
  38. Shop_CallEventShop = 0x51 | __CAN_BEGIN__ | __CAN_SAVE__,//@EVENTSHOPの呼び出し中。スキップ可能
  39. Shop_CallShowShop = 0x52 | __CAN_SAVE__,//@SHOW_SHOPの呼び出し中
  40. Shop_WaitInput = 0x53 | __CAN_SAVE__,//入力待ち状態。アイテムが存在するならEVENTBUYにBOUGHT、そうでなければ@USERSHOPにRESULTを渡す
  41. Shop_CallEventBuy = 0x54 | __CAN_BEGIN__ | __CAN_SAVE__,//@USERSHOPまた@EVENTBUYはの呼び出し中
  42. SaveGame_Begin = 0x100,//SAVEGAMEから
  43. SaveGame_WaitInput = 0x101,//入力待ち
  44. SaveGame_WaitInputOverwrite = 0x102,//上書きの許可待ち
  45. SaveGame_CallSaveInfo = 0x103,//@SAVEINFO呼び出し中。20回。
  46. LoadGame_Begin = 0x110,//LOADGAMEから
  47. LoadGame_WaitInput = 0x111,//入力待ち
  48. LoadGameOpenning_Begin = 0x120,//最初に[1]を選択したとき。
  49. LoadGameOpenning_WaitInput = 0x121,//入力待ち
  50. //AutoSave_Begin = 0x200,
  51. AutoSave_CallSaveInfo = 0x201,
  52. AutoSave_CallUniqueAutosave = 0x202,
  53. AutoSave_Skipped = 0x203,
  54. LoadData_DataLoaded = 0x210,//データロード直後
  55. LoadData_CallSystemLoad = 0x211 | __CAN_BEGIN__,//データロード直後
  56. LoadData_CallEventLoad = 0x212 | __CAN_BEGIN__,//@EVENTLOADの呼び出し中。スキップ可能
  57. Openning_TitleLoadgame = 0x220,
  58. System_Reloaderb = 0x230,
  59. First_Begin = 0x240,
  60. Normal = 0xFFFF | __CAN_BEGIN__ | __CAN_SAVE__//特に何でもないとき。ScriptEndに達したらエラー
  61. }
  62. //難読化用属性。enum.ToString()やenum.Parse()を行うなら(Exclude=true)にすること。
  63. [Obfuscation(Exclude = false)]
  64. internal enum BeginType
  65. {
  66. NULL = 0,
  67. SHOP = 2,
  68. TRAIN = 3,
  69. AFTERTRAIN = 4,
  70. ABLUP = 5,
  71. TURNEND = 6,
  72. FIRST = 7,
  73. TITLE = 8
  74. }
  75. internal sealed class ProcessState
  76. {
  77. public ProcessState(IConsole console)
  78. {
  79. if (Program.DebugMode)//DebugModeでなければ知らなくて良い
  80. this.console = console;
  81. }
  82. readonly IConsole console;
  83. readonly List<CalledFunction> functionList = new List<CalledFunction>();
  84. private LogicalLine currentLine;
  85. //private LogicalLine nextLine;
  86. public int lineCount;
  87. public int currentMin = 0;
  88. //private bool sequential;
  89. public bool ScriptEnd => functionList.Count == currentMin;
  90. public int functionCount => functionList.Count;
  91. SystemStateCode sysStateCode = SystemStateCode.Title_Begin;
  92. BeginType begintype = BeginType.NULL;
  93. public bool isBegun => (begintype != BeginType.NULL) ? true : false;
  94. public LogicalLine CurrentLine { get => currentLine;
  95. set { currentLine = value; } }
  96. public LogicalLine ErrorLine => currentLine;
  97. //IF文中でELSEIF文の中身をチェックするなどCurrentLineと作業中のLineが違う時にセットする
  98. //public LogicalLine RunningLine { get; set; }
  99. //1755a 呼び出し元消滅
  100. //public bool Sequential { get { return sequential; } }
  101. public CalledFunction CurrentCalled => functionList[functionList.Count - 1];
  102. public SystemStateCode SystemState
  103. {
  104. get => sysStateCode;
  105. set { sysStateCode = value; }
  106. }
  107. public void ShiftNextLine()
  108. {
  109. currentLine = currentLine.NextLine;
  110. //nextLine = nextLine.NextLine;
  111. //RunningLine = null;
  112. //sequential = true;
  113. //GlobalStatic.Process.lineCount++;
  114. lineCount++;
  115. }
  116. /// <summary>
  117. /// 関数内の移動。JUMPではなくGOTOやIF文など
  118. /// </summary>
  119. /// <param name="line"></param>
  120. public void JumpTo(LogicalLine line)
  121. {
  122. currentLine = line;
  123. lineCount++;
  124. //sequential = false;
  125. //ShfitNextLine();
  126. }
  127. public void SetBegin(string keyword)
  128. {//TrimとToUpper済みのはず
  129. switch (keyword)
  130. {
  131. case "SHOP":
  132. SetBegin(BeginType.SHOP); return;
  133. case "TRAIN":
  134. SetBegin(BeginType.TRAIN); return;
  135. case "AFTERTRAIN":
  136. SetBegin(BeginType.AFTERTRAIN); return;
  137. case "ABLUP":
  138. SetBegin(BeginType.ABLUP); return;
  139. case "TURNEND":
  140. SetBegin(BeginType.TURNEND); return;
  141. case "FIRST":
  142. SetBegin(BeginType.FIRST); return;
  143. case "TITLE":
  144. SetBegin(BeginType.TITLE); return;
  145. }
  146. throw new CodeEE("BEGINのキーワード\"" + keyword + "\"は未定義です");
  147. }
  148. public void SetBegin(BeginType type)
  149. {
  150. string errmes = "";
  151. switch (type)
  152. {
  153. case BeginType.SHOP:
  154. case BeginType.TRAIN:
  155. case BeginType.AFTERTRAIN:
  156. case BeginType.ABLUP:
  157. case BeginType.TURNEND:
  158. case BeginType.FIRST:
  159. if ((sysStateCode & SystemStateCode.__CAN_BEGIN__) != SystemStateCode.__CAN_BEGIN__)
  160. {
  161. errmes = "BEGIN";
  162. goto err;
  163. }
  164. break;
  165. //1.729 BEGIN TITLEはどこでも使えるように
  166. case BeginType.TITLE:
  167. break;
  168. //BEGINの処理中でチェック済み
  169. //default:
  170. // throw new ExeEE("不適当なBEGIN呼び出し");
  171. }
  172. begintype = type;
  173. return;
  174. err:
  175. CalledFunction func = functionList[0];
  176. string funcName = func.FunctionName;
  177. throw new CodeEE("@" + funcName + "中で" + errmes + "命令を実行することはできません");
  178. }
  179. public void SaveLoadData(bool saveData)
  180. {
  181. if (saveData)
  182. sysStateCode = SystemStateCode.SaveGame_Begin;
  183. else
  184. sysStateCode = SystemStateCode.LoadGame_Begin;
  185. //ClearFunctionList();
  186. }
  187. public void ClearFunctionList()
  188. {
  189. if (Program.DebugMode && !isClone && GlobalStatic.Process.MethodStack() == 0)
  190. console.DebugClearTraceLog();
  191. foreach (CalledFunction called in functionList)
  192. if (called.CurrentLabel.hasPrivDynamicVar)
  193. called.CurrentLabel.Out();
  194. functionList.Clear();
  195. begintype = BeginType.NULL;
  196. }
  197. public bool calledWhenNormal = true;
  198. /// <summary>
  199. /// BEGIN命令によるプログラム状態の変化
  200. /// </summary>
  201. /// <param name="key"></param>
  202. /// <returns></returns>
  203. public void Begin()
  204. {
  205. //@EVENTSHOPからの呼び出しは一旦破棄
  206. if (sysStateCode == SystemStateCode.Shop_CallEventShop)
  207. return;
  208. switch (begintype)
  209. {
  210. case BeginType.SHOP:
  211. if (sysStateCode == SystemStateCode.Normal)
  212. calledWhenNormal = true;
  213. else
  214. calledWhenNormal = false;
  215. sysStateCode = SystemStateCode.Shop_Begin;
  216. break;
  217. case BeginType.TRAIN:
  218. sysStateCode = SystemStateCode.Train_Begin;
  219. break;
  220. case BeginType.AFTERTRAIN:
  221. sysStateCode = SystemStateCode.AfterTrain_Begin;
  222. break;
  223. case BeginType.ABLUP:
  224. sysStateCode = SystemStateCode.Ablup_Begin;
  225. break;
  226. case BeginType.TURNEND:
  227. sysStateCode = SystemStateCode.Turnend_Begin;
  228. break;
  229. case BeginType.FIRST:
  230. sysStateCode = SystemStateCode.First_Begin;
  231. break;
  232. case BeginType.TITLE:
  233. sysStateCode = SystemStateCode.Title_Begin;
  234. break;
  235. //セット時に判定してるので、ここには来ないはず
  236. //default:
  237. // throw new ExeEE("不適当なBEGIN呼び出し");
  238. }
  239. if (Program.DebugMode)
  240. {
  241. console.DebugClearTraceLog();
  242. console.DebugAddTraceLog("BEGIN:" + begintype);
  243. }
  244. foreach (CalledFunction called in functionList)
  245. if (called.CurrentLabel.hasPrivDynamicVar)
  246. called.CurrentLabel.Out();
  247. functionList.Clear();
  248. begintype = BeginType.NULL;
  249. }
  250. /// <summary>
  251. /// システムによる強制的なBEGIN
  252. /// </summary>
  253. /// <param name="type"></param>
  254. public void Begin(BeginType type)
  255. {
  256. begintype = type;
  257. sysStateCode = SystemStateCode.Title_Begin;
  258. Begin();
  259. }
  260. public LogicalLine GetCurrentReturnAddress
  261. {
  262. get
  263. {
  264. if (functionList.Count == currentMin)
  265. return null;
  266. return functionList[functionList.Count - 1].ReturnAddress;
  267. }
  268. }
  269. public LogicalLine GetReturnAddressSequensial(int curerntDepth)
  270. {
  271. if (functionList.Count == currentMin)
  272. return null;
  273. return functionList[functionList.Count - curerntDepth - 1].ReturnAddress;
  274. }
  275. public string Scope
  276. {
  277. get
  278. {
  279. //スクリプトの実行中処理からしか呼び出されないので、ここはない…はず
  280. //if (functionList.Count == 0)
  281. //{
  282. // throw new ExeEE("実行中の関数が存在しません");
  283. //}
  284. if (functionList.Count == 0)
  285. return null;//1756 デバッグコマンドから呼び出されるようになったので
  286. return functionList[functionList.Count - 1].FunctionName;
  287. }
  288. }
  289. public void Return(Int64 ret)
  290. {
  291. if (IsFunctionMethod)
  292. {
  293. ReturnF(null);
  294. return;
  295. }
  296. //sequential = false;//いずれにしろ順列ではない。
  297. //呼び出し元は全部スクリプト処理
  298. //if (functionList.Count == 0)
  299. //{
  300. // throw new ExeEE("実行中の関数が存在しません");
  301. //}
  302. CalledFunction called = functionList[functionList.Count - 1];
  303. if (called.IsJump)
  304. {//JUMPした場合。即座にRETURN RESULTする。
  305. if (called.TopLabel.hasPrivDynamicVar)
  306. called.TopLabel.Out();
  307. functionList.Remove(called);
  308. if (Program.DebugMode)
  309. console.DebugRemoveTraceLog();
  310. Return(ret);
  311. return;
  312. }
  313. if (!called.IsEvent)
  314. {
  315. if (called.TopLabel.hasPrivDynamicVar)
  316. called.TopLabel.Out();
  317. currentLine = null;
  318. }
  319. else
  320. {
  321. if (called.CurrentLabel.hasPrivDynamicVar)
  322. called.CurrentLabel.Out();
  323. //#Singleフラグ付き関数で1が返された。
  324. //1752 非0ではなく1と等価であることを見るように修正
  325. //1756 全てを終了ではなく#PRIや#LATERのグループごとに修正
  326. if (called.IsOnly)
  327. called.FinishEvent();
  328. else if ((called.HasSingleFlag) && (ret == 1))
  329. called.ShiftNextGroup();
  330. else
  331. called.ShiftNext();//次の同名関数に進む。
  332. currentLine = called.CurrentLabel;//関数の始点(@~~)へ移動。呼ぶべき関数が無ければnull
  333. if (called.CurrentLabel != null)
  334. {
  335. lineCount++;
  336. if (called.CurrentLabel.hasPrivDynamicVar)
  337. called.CurrentLabel.In();
  338. }
  339. }
  340. if (Program.DebugMode)
  341. console.DebugRemoveTraceLog();
  342. //関数終了
  343. if (currentLine == null)
  344. {
  345. currentLine = called.ReturnAddress;
  346. functionList.RemoveAt(functionList.Count - 1);
  347. if (currentLine == null)
  348. {
  349. //この時点でfunctionListは空のはず
  350. //functionList.Clear();//全て終了。stateEndProcessに処理を返す
  351. if (begintype != BeginType.NULL)//BEGIN XXが行なわれていれば
  352. {
  353. Begin();
  354. }
  355. return;
  356. }
  357. lineCount++;
  358. //ShfitNextLine();
  359. return;
  360. }
  361. if (Program.DebugMode)
  362. {
  363. FunctionLabelLine label = called.CurrentLabel;
  364. console.DebugAddTraceLog("CALL :@" + label.LabelName + ":" + label.Position + "行目");
  365. }
  366. lineCount++;
  367. //ShfitNextLine();
  368. }
  369. public void IntoFunction(CalledFunction call, UserDefinedFunctionArgument srcArgs, ExpressionMediator exm)
  370. {
  371. if (call.IsEvent)
  372. {
  373. foreach (CalledFunction called in functionList)
  374. {
  375. if (called.IsEvent)
  376. throw new CodeEE("EVENT関数の解決前にCALLEVENT命令が行われました");
  377. }
  378. }
  379. if (Program.DebugMode)
  380. {
  381. FunctionLabelLine label = call.CurrentLabel;
  382. if (call.IsJump)
  383. console.DebugAddTraceLog("JUMP :@" + label.LabelName + ":" + label.Position + "行目");
  384. else
  385. console.DebugAddTraceLog("CALL :@" + label.LabelName + ":" + label.Position + "行目");
  386. }
  387. if (srcArgs != null)
  388. {
  389. //引数の値を確定させる
  390. srcArgs.SetTransporter(exm);
  391. //プライベート変数更新
  392. if (call.TopLabel.hasPrivDynamicVar)
  393. call.TopLabel.In();
  394. //更新した変数へ引数を代入
  395. for (int i = 0; i < call.TopLabel.Arg.Length; i++)
  396. {
  397. if (srcArgs.Arguments[i] != null)
  398. {
  399. if (call.TopLabel.Arg[i].Identifier.IsReference)
  400. ((ReferenceToken)(call.TopLabel.Arg[i].Identifier)).SetRef(srcArgs.TransporterRef[i]);
  401. else if (srcArgs.Arguments[i].GetOperandType() == typeof(Int64))
  402. call.TopLabel.Arg[i].SetValue(srcArgs.TransporterInt[i], exm);
  403. else
  404. call.TopLabel.Arg[i].SetValue(srcArgs.TransporterStr[i], exm);
  405. }
  406. }
  407. }
  408. else//こっちに来るのはシステムからの呼び出し=引数は存在しない関数のみ ifネストの外に出していい気もしないでもないがはてさて
  409. {
  410. //プライベート変数更新
  411. if (call.TopLabel.hasPrivDynamicVar)
  412. call.TopLabel.In();
  413. }
  414. functionList.Add(call);
  415. //sequential = false;
  416. currentLine = call.CurrentLabel;
  417. lineCount++;
  418. //ShfitNextLine();
  419. }
  420. #region userdifinedmethod
  421. public bool IsFunctionMethod => functionList[currentMin].TopLabel.IsMethod;
  422. public SingleTerm MethodReturnValue;
  423. public void ReturnF(SingleTerm ret)
  424. {
  425. //読み込み時のチェック済みのはず
  426. //if (!IsFunctionMethod)
  427. // throw new ExeEE("ReturnFと#FUNCTIONのチェックがおかしい");
  428. //sequential = false;//いずれにしろ順列ではない。
  429. //呼び出し元はRETURNFコマンドか関数終了時のみ
  430. //if (functionList.Count == 0)
  431. // throw new ExeEE("実行中の関数が存在しません");
  432. //非イベント呼び出しなので、これは起こりえない
  433. //else if (functionList.Count != 1)
  434. // throw new ExeEE("関数が複数ある");
  435. if (Program.DebugMode)
  436. {
  437. console.DebugRemoveTraceLog();
  438. }
  439. //OutはGetValue側で行う
  440. //functionList[0].TopLabel.Out();
  441. currentLine = functionList[functionList.Count - 1].ReturnAddress;
  442. functionList.RemoveAt(functionList.Count - 1);
  443. //nextLine = null;
  444. MethodReturnValue = ret;
  445. }
  446. #endregion
  447. bool isClone;
  448. public bool IsClone { get => isClone;
  449. set { isClone = value; } }
  450. // functionListのコピーを必要とする呼び出し元が無かったのでコピーしないことにする。
  451. public ProcessState Clone()
  452. {
  453. ProcessState ret = new ProcessState(console);
  454. ret.isClone = true;
  455. //どうせ消すからコピー不要
  456. //foreach (CalledFunction func in functionList)
  457. // ret.functionList.Add(func.Clone());
  458. ret.currentLine = currentLine;
  459. //ret.nextLine = this.nextLine;
  460. //ret.sequential = this.sequential;
  461. ret.sysStateCode = sysStateCode;
  462. ret.begintype = begintype;
  463. //ret.MethodReturnValue = this.MethodReturnValue;
  464. return ret;
  465. }
  466. //public ProcessState CloneForFunctionMethod()
  467. //{
  468. // ProcessState ret = new ProcessState(console);
  469. // ret.isClone = true;
  470. // //どうせ消すからコピー不要
  471. // //foreach (CalledFunction func in functionList)
  472. // // ret.functionList.Add(func.Clone());
  473. // ret.currentLine = this.currentLine;
  474. // ret.nextLine = this.nextLine;
  475. // //ret.sequential = this.sequential;
  476. // ret.sysStateCode = this.sysStateCode;
  477. // ret.begintype = this.begintype;
  478. // //ret.MethodReturnValue = this.MethodReturnValue;
  479. // return ret;
  480. //}
  481. }
  482. }