A stable modding interface between Techblox and mods https://mod.exmods.org/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

490 lines
14KB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Runtime.CompilerServices;
  4. using RobocraftX.Common;
  5. using RobocraftX.GUI.MyGamesScreen;
  6. using RobocraftX.StateSync;
  7. using Svelto.ECS;
  8. using TechbloxModdingAPI;
  9. using TechbloxModdingAPI.Blocks;
  10. using TechbloxModdingAPI.Tasks;
  11. using TechbloxModdingAPI.Utility;
  12. namespace TechbloxModdingAPI.App
  13. {
  14. /// <summary>
  15. /// An in-game save.
  16. /// This can be a menu item for a local save or the currently loaded save.
  17. /// Support for Steam Workshop coming soon (hopefully).
  18. /// </summary>
  19. public class Game
  20. {
  21. // extensible engines
  22. protected static GameGameEngine gameEngine = new GameGameEngine();
  23. protected static GameMenuEngine menuEngine = new GameMenuEngine();
  24. protected static DebugInterfaceEngine debugOverlayEngine = new DebugInterfaceEngine();
  25. protected static GameBuildSimEventEngine buildSimEventEngine = new GameBuildSimEventEngine();
  26. private List<string> debugIds = new List<string>();
  27. private bool menuMode = true;
  28. private bool hasId = false;
  29. /// <summary>
  30. /// Initializes a new instance of the <see cref="T:TechbloxModdingAPI.App.Game"/> class.
  31. /// </summary>
  32. /// <param name="id">Menu identifier.</param>
  33. public Game(uint id) : this(new EGID(id, MyGamesScreenExclusiveGroups.MyGames))
  34. {
  35. }
  36. /// <summary>
  37. /// Initializes a new instance of the <see cref="T:TechbloxModdingAPI.App.Game"/> class.
  38. /// </summary>
  39. /// <param name="id">Menu identifier.</param>
  40. public Game(EGID id)
  41. {
  42. this.Id = id.entityID;
  43. this.EGID = id;
  44. this.hasId = true;
  45. menuMode = true;
  46. if (!VerifyMode()) throw new AppStateException("Game cannot be created while not in a game nor in a menu (is the game in a loading screen?)");
  47. }
  48. /// <summary>
  49. /// Initializes a new instance of the <see cref="T:TechbloxModdingAPI.App.Game"/> class without id.
  50. /// This is assumed to be the current game.
  51. /// </summary>
  52. public Game()
  53. {
  54. menuMode = false;
  55. if (!VerifyMode()) throw new AppStateException("Game cannot be created while not in a game nor in a menu (is the game in a loading screen?)");
  56. if (menuEngine.IsInMenu) throw new GameNotFoundException("Game not found.");
  57. }
  58. /// <summary>
  59. /// Returns the currently loaded game.
  60. /// If in a menu, manipulating the returned object may not work as intended.
  61. /// </summary>
  62. /// <returns>The current game.</returns>
  63. public static Game CurrentGame()
  64. {
  65. return new Game();
  66. }
  67. /// <summary>
  68. /// Creates a new game and adds it to the menu.
  69. /// If not in a menu, this will throw AppStateException.
  70. /// </summary>
  71. /// <returns>The new game.</returns>
  72. public static Game NewGame()
  73. {
  74. if (!menuEngine.IsInMenu) throw new AppStateException("New Game cannot be created while not in a menu.");
  75. uint nextId = menuEngine.HighestID() + 1;
  76. EGID egid = new EGID(nextId, MyGamesScreenExclusiveGroups.MyGames);
  77. menuEngine.CreateMyGame(egid);
  78. return new Game(egid);
  79. }
  80. /// <summary>
  81. /// An event that fires whenever a game is switched to simulation mode (time running mode).
  82. /// </summary>
  83. public static event EventHandler<GameEventArgs> Simulate
  84. {
  85. add => buildSimEventEngine.SimulationMode += value;
  86. remove => buildSimEventEngine.SimulationMode -= value;
  87. }
  88. /// <summary>
  89. /// An event that fires whenever a game is switched to edit mode (time stopped mode).
  90. /// This does not fire when a game is loaded.
  91. /// </summary>
  92. public static event EventHandler<GameEventArgs> Edit
  93. {
  94. add => buildSimEventEngine.BuildMode += value;
  95. remove => buildSimEventEngine.BuildMode -= value;
  96. }
  97. /// <summary>
  98. /// An event that fires right after a game is completely loaded.
  99. /// </summary>
  100. public static event EventHandler<GameEventArgs> Enter
  101. {
  102. add => gameEngine.EnterGame += value;
  103. remove => gameEngine.EnterGame -= value;
  104. }
  105. /// <summary>
  106. /// An event that fires right before a game returns to the main menu.
  107. /// At this point, Techblox is transitioning state so many things are invalid/unstable here.
  108. /// </summary>
  109. public static event EventHandler<GameEventArgs> Exit
  110. {
  111. add => gameEngine.ExitGame += value;
  112. remove => gameEngine.ExitGame -= value;
  113. }
  114. /// <summary>
  115. /// The game's unique menu identifier.
  116. /// </summary>
  117. /// <value>The identifier.</value>
  118. public uint Id
  119. {
  120. get;
  121. private set;
  122. }
  123. /// <summary>
  124. /// The game's unique menu EGID.
  125. /// </summary>
  126. /// <value>The egid.</value>
  127. public EGID EGID
  128. {
  129. get;
  130. private set;
  131. }
  132. /// <summary>
  133. /// Whether the game is a (valid) menu item.
  134. /// </summary>
  135. /// <value><c>true</c> if menu item; otherwise, <c>false</c>.</value>
  136. public bool MenuItem
  137. {
  138. get => menuMode && hasId;
  139. }
  140. /// <summary>
  141. /// The game's name.
  142. /// </summary>
  143. /// <value>The name.</value>
  144. public string Name
  145. {
  146. get
  147. {
  148. if (!VerifyMode()) return null;
  149. if (menuMode) return menuEngine.GetGameInfo(EGID).GameName;
  150. return GameMode.SaveGameDetails.Name;
  151. }
  152. set
  153. {
  154. if (!VerifyMode()) return;
  155. if (menuMode)
  156. {
  157. menuEngine.SetGameName(EGID, value);
  158. }
  159. else
  160. {
  161. GameMode.SaveGameDetails.Name = value;
  162. }
  163. }
  164. }
  165. /// <summary>
  166. /// The game's description.
  167. /// </summary>
  168. /// <value>The description.</value>
  169. public string Description
  170. {
  171. get
  172. {
  173. if (!VerifyMode()) return null;
  174. if (menuMode) return menuEngine.GetGameInfo(EGID).GameDescription;
  175. return "";
  176. }
  177. set
  178. {
  179. if (!VerifyMode()) return;
  180. if (menuMode)
  181. {
  182. menuEngine.SetGameDescription(EGID, value);
  183. }
  184. else
  185. {
  186. // No description exists in-game
  187. }
  188. }
  189. }
  190. /// <summary>
  191. /// The path to the game's save folder.
  192. /// </summary>
  193. /// <value>The path.</value>
  194. public string Path
  195. {
  196. get
  197. {
  198. if (!VerifyMode()) return null;
  199. if (menuMode) return menuEngine.GetGameInfo(EGID).SavedGamePath;
  200. return GameMode.SaveGameDetails.Folder;
  201. }
  202. set
  203. {
  204. if (!VerifyMode()) return;
  205. if (menuMode)
  206. {
  207. menuEngine.GetGameInfo(EGID).SavedGamePath.Set(value);
  208. }
  209. else
  210. {
  211. // this likely breaks things
  212. GameMode.SaveGameDetails = new SaveGameDetails(GameMode.SaveGameDetails.Name, value, GameMode.SaveGameDetails.WorkshopId);
  213. }
  214. }
  215. }
  216. /// <summary>
  217. /// The Steam Workshop Id of the game save.
  218. /// In most cases this is invalid and returns 0, so this can be ignored.
  219. /// </summary>
  220. /// <value>The workshop identifier.</value>
  221. public ulong WorkshopId
  222. {
  223. get
  224. {
  225. if (!VerifyMode()) return 0uL;
  226. if (menuMode) return 0uL; // MyGames don't have workshop IDs
  227. return GameMode.SaveGameDetails.WorkshopId;
  228. }
  229. set
  230. {
  231. VerifyMode();
  232. if (menuMode)
  233. {
  234. // MyGames don't have workshop IDs
  235. // menuEngine.GetGameInfo(EGID).GameName.Set(value);
  236. }
  237. else
  238. {
  239. // this likely breaks things
  240. GameMode.SaveGameDetails = new SaveGameDetails(GameMode.SaveGameDetails.Name, GameMode.SaveGameDetails.Folder, value);
  241. }
  242. }
  243. }
  244. /// <summary>
  245. /// Whether the game is in simulation mode.
  246. /// </summary>
  247. /// <value><c>true</c> if is simulating; otherwise, <c>false</c>.</value>
  248. public bool IsSimulating
  249. {
  250. get
  251. {
  252. if (!VerifyMode()) return false;
  253. return !menuMode && gameEngine.IsTimeRunningMode();
  254. }
  255. set
  256. {
  257. if (!VerifyMode()) return;
  258. if (!menuMode && gameEngine.IsTimeRunningMode() != value)
  259. gameEngine.ToggleTimeMode();
  260. }
  261. }
  262. /// <summary>
  263. /// Whether the game is in time-running mode.
  264. /// Alias of IsSimulating.
  265. /// </summary>
  266. /// <value><c>true</c> if is time running; otherwise, <c>false</c>.</value>
  267. public bool IsTimeRunning
  268. {
  269. get => IsSimulating;
  270. set
  271. {
  272. IsSimulating = value;
  273. }
  274. }
  275. /// <summary>
  276. /// Whether the game is in time-stopped mode.
  277. /// </summary>
  278. /// <value><c>true</c> if is time stopped; otherwise, <c>false</c>.</value>
  279. public bool IsTimeStopped
  280. {
  281. get
  282. {
  283. if (!VerifyMode()) return false;
  284. return !menuMode && gameEngine.IsTimeStoppedMode();
  285. }
  286. set
  287. {
  288. if (!VerifyMode()) return;
  289. if (!menuMode && gameEngine.IsTimeStoppedMode() != value)
  290. gameEngine.ToggleTimeMode();
  291. }
  292. }
  293. /// <summary>
  294. /// Toggles the time mode.
  295. /// </summary>
  296. public void ToggleTimeMode()
  297. {
  298. if (!VerifyMode()) return;
  299. if (menuMode || !gameEngine.IsInGame)
  300. {
  301. throw new AppStateException("Game menu item cannot toggle it's time mode");
  302. }
  303. gameEngine.ToggleTimeMode();
  304. }
  305. /// <summary>
  306. /// The mode of the game.
  307. /// </summary>
  308. public CurrentGameMode Mode
  309. {
  310. get
  311. {
  312. if (menuMode || !VerifyMode()) return CurrentGameMode.None;
  313. return (CurrentGameMode) GameMode.CurrentMode;
  314. }
  315. }
  316. /// <summary>
  317. /// Load the game save.
  318. /// This happens asynchronously, so when this method returns the game not loaded yet.
  319. /// Use the Game.Enter event to perform operations after the game has completely loaded.
  320. /// </summary>
  321. public void EnterGame()
  322. {
  323. if (!VerifyMode()) return;
  324. if (!hasId)
  325. {
  326. throw new GameNotFoundException("Game has an invalid ID");
  327. }
  328. ISchedulable task = new Once(() => { menuEngine.EnterGame(EGID); this.menuMode = false; });
  329. Scheduler.Schedule(task);
  330. }
  331. /// <summary>
  332. /// Return to the menu.
  333. /// Part of this always happens asynchronously, so when this method returns the game has not exited yet.
  334. /// Use the Client.EnterMenu event to perform operations after the game has completely exited.
  335. /// </summary>
  336. /// <param name="async">If set to <c>true</c>, do this async.</param>
  337. public void ExitGame(bool async = false)
  338. {
  339. if (!VerifyMode()) return;
  340. if (menuMode)
  341. {
  342. throw new GameNotFoundException("Cannot exit game using menu ID");
  343. }
  344. gameEngine.ExitCurrentGame(async);
  345. this.menuMode = true;
  346. }
  347. /// <summary>
  348. /// Saves the game.
  349. /// Part of this happens asynchronously, so when this method returns the game has not been saved yet.
  350. /// </summary>
  351. public void SaveGame()
  352. {
  353. if (!VerifyMode()) return;
  354. if (menuMode)
  355. {
  356. throw new GameNotFoundException("Cannot save game using menu ID");
  357. }
  358. gameEngine.SaveCurrentGame();
  359. }
  360. /// <summary>
  361. /// Add information to the in-game debug display.
  362. /// When this object is garbage collected, this debug info is automatically removed.
  363. /// </summary>
  364. /// <param name="id">Debug info identifier.</param>
  365. /// <param name="contentGetter">Content getter.</param>
  366. public void AddDebugInfo(string id, Func<string> contentGetter)
  367. {
  368. if (!VerifyMode()) return;
  369. if (menuMode)
  370. {
  371. throw new GameNotFoundException("Game object references a menu item but AddDebugInfo only works on the currently-loaded game");
  372. }
  373. debugOverlayEngine.SetInfo(id, contentGetter);
  374. debugIds.Add(id);
  375. }
  376. /// <summary>
  377. /// Remove information from the in-game debug display.
  378. /// </summary>
  379. /// <returns><c>true</c>, if debug info was removed, <c>false</c> otherwise.</returns>
  380. /// <param name="id">Debug info identifier.</param>
  381. public bool RemoveDebugInfo(string id)
  382. {
  383. if (!VerifyMode()) return false;
  384. if (menuMode)
  385. {
  386. throw new GameNotFoundException("Game object references a menu item but RemoveDebugInfo only works on the currently-loaded game");
  387. }
  388. if (!debugIds.Contains(id)) return false;
  389. debugOverlayEngine.RemoveInfo(id);
  390. return debugIds.Remove(id);
  391. }
  392. /// <summary>
  393. /// Gets the blocks in the game.
  394. /// This returns null when in a loading state, and throws AppStateException when in menu.
  395. /// </summary>
  396. /// <returns>The blocks in game.</returns>
  397. /// <param name="filter">The block to search for. BlockIDs.Invalid will return all blocks.</param>
  398. public Block[] GetBlocksInGame(BlockIDs filter = BlockIDs.Invalid)
  399. {
  400. if (!VerifyMode()) return null;
  401. if (menuMode)
  402. {
  403. throw new AppStateException("Game object references a menu item but GetBlocksInGame only works on the currently-loaded game");
  404. }
  405. EGID[] blockEGIDs = gameEngine.GetAllBlocksInGame(filter);
  406. Block[] blocks = new Block[blockEGIDs.Length];
  407. for (int b = 0; b < blockEGIDs.Length; b++)
  408. {
  409. blocks[b] = new Block(blockEGIDs[b]);
  410. }
  411. return blocks;
  412. }
  413. ~Game()
  414. {
  415. foreach (string id in debugIds)
  416. {
  417. debugOverlayEngine.RemoveInfo(id);
  418. }
  419. }
  420. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  421. private bool VerifyMode()
  422. {
  423. if (menuMode && (!menuEngine.IsInMenu || gameEngine.IsInGame))
  424. {
  425. // either game loading or API is broken
  426. return false;
  427. }
  428. if (!menuMode && (menuEngine.IsInMenu || !gameEngine.IsInGame))
  429. {
  430. // either game loading or API is broken
  431. return false;
  432. }
  433. return true;
  434. }
  435. internal static void Init()
  436. {
  437. GameEngineManager.AddGameEngine(gameEngine);
  438. GameEngineManager.AddGameEngine(debugOverlayEngine);
  439. MenuEngineManager.AddMenuEngine(menuEngine);
  440. }
  441. internal static void InitDeterministic(StateSyncRegistrationHelper stateSyncReg)
  442. {
  443. stateSyncReg.AddDeterministicEngine(buildSimEventEngine);
  444. }
  445. }
  446. }