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.

493 lines
15KB

  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 internal 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 gameEngine.GetGameData().saveName;
  151. }
  152. set
  153. {
  154. if (!VerifyMode()) return;
  155. if (menuMode)
  156. {
  157. menuEngine.SetGameName(EGID, value);
  158. } // Save details are directly saved from user input or not changed at all when in game
  159. }
  160. }
  161. /// <summary>
  162. /// The game's description.
  163. /// </summary>
  164. /// <value>The description.</value>
  165. public string Description
  166. {
  167. get
  168. {
  169. if (!VerifyMode()) return null;
  170. if (menuMode) return menuEngine.GetGameInfo(EGID).GameDescription;
  171. return "";
  172. }
  173. set
  174. {
  175. if (!VerifyMode()) return;
  176. if (menuMode)
  177. {
  178. menuEngine.SetGameDescription(EGID, value);
  179. } // No description exists in-game
  180. }
  181. }
  182. /// <summary>
  183. /// The path to the game's save folder.
  184. /// </summary>
  185. /// <value>The path.</value>
  186. public string Path
  187. {
  188. get
  189. {
  190. if (!VerifyMode()) return null;
  191. if (menuMode) return menuEngine.GetGameInfo(EGID).SavedGamePath;
  192. return gameEngine.GetGameData().gameID;
  193. }
  194. set
  195. {
  196. if (!VerifyMode()) return;
  197. if (menuMode)
  198. {
  199. menuEngine.GetGameInfo(EGID).SavedGamePath.Set(value);
  200. }
  201. }
  202. }
  203. /// <summary>
  204. /// The Steam Workshop Id of the game save.
  205. /// In most cases this is invalid and returns 0, so this can be ignored.
  206. /// </summary>
  207. /// <value>The workshop identifier.</value>
  208. [Obsolete]
  209. public ulong WorkshopId
  210. {
  211. get
  212. {
  213. return 0uL; // Not supported anymore
  214. }
  215. set
  216. {
  217. }
  218. }
  219. /// <summary>
  220. /// Whether the game is in simulation mode.
  221. /// </summary>
  222. /// <value><c>true</c> if is simulating; otherwise, <c>false</c>.</value>
  223. public bool IsSimulating
  224. {
  225. get
  226. {
  227. if (!VerifyMode()) return false;
  228. return !menuMode && gameEngine.IsTimeRunningMode();
  229. }
  230. set
  231. {
  232. if (!VerifyMode()) return;
  233. if (!menuMode && gameEngine.IsTimeRunningMode() != value)
  234. gameEngine.ToggleTimeMode();
  235. }
  236. }
  237. /// <summary>
  238. /// Whether the game is in time-running mode.
  239. /// Alias of IsSimulating.
  240. /// </summary>
  241. /// <value><c>true</c> if is time running; otherwise, <c>false</c>.</value>
  242. public bool IsTimeRunning
  243. {
  244. get => IsSimulating;
  245. set
  246. {
  247. IsSimulating = value;
  248. }
  249. }
  250. /// <summary>
  251. /// Whether the game is in time-stopped mode.
  252. /// </summary>
  253. /// <value><c>true</c> if is time stopped; otherwise, <c>false</c>.</value>
  254. public bool IsTimeStopped
  255. {
  256. get
  257. {
  258. if (!VerifyMode()) return false;
  259. return !menuMode && gameEngine.IsTimeStoppedMode();
  260. }
  261. set
  262. {
  263. if (!VerifyMode()) return;
  264. if (!menuMode && gameEngine.IsTimeStoppedMode() != value)
  265. gameEngine.ToggleTimeMode();
  266. }
  267. }
  268. /// <summary>
  269. /// Toggles the time mode.
  270. /// </summary>
  271. public void ToggleTimeMode()
  272. {
  273. if (!VerifyMode()) return;
  274. if (menuMode || !gameEngine.IsInGame)
  275. {
  276. throw new AppStateException("Game menu item cannot toggle it's time mode");
  277. }
  278. gameEngine.ToggleTimeMode();
  279. }
  280. /// <summary>
  281. /// The mode of the game.
  282. /// </summary>
  283. public CurrentGameMode Mode
  284. {
  285. get
  286. {
  287. if (menuMode || !VerifyMode()) return CurrentGameMode.None;
  288. return (CurrentGameMode) GameMode.CurrentMode;
  289. }
  290. }
  291. /// <summary>
  292. /// Load the game save.
  293. /// This happens asynchronously, so when this method returns the game not loaded yet.
  294. /// Use the Game.Enter event to perform operations after the game has completely loaded.
  295. /// </summary>
  296. public void EnterGame()
  297. {
  298. if (!VerifyMode()) return;
  299. if (!hasId)
  300. {
  301. throw new GameNotFoundException("Game has an invalid ID");
  302. }
  303. ISchedulable task = new Once(() => { menuEngine.EnterGame(EGID); this.menuMode = false; });
  304. Scheduler.Schedule(task);
  305. }
  306. /// <summary>
  307. /// Return to the menu.
  308. /// Part of this always happens asynchronously, so when this method returns the game has not exited yet.
  309. /// Use the Client.EnterMenu event to perform operations after the game has completely exited.
  310. /// </summary>
  311. /// <param name="async">If set to <c>true</c>, do this async.</param>
  312. public void ExitGame(bool async = false)
  313. {
  314. if (!VerifyMode()) return;
  315. if (menuMode)
  316. {
  317. throw new GameNotFoundException("Cannot exit game using menu ID");
  318. }
  319. gameEngine.ExitCurrentGame(async);
  320. this.menuMode = true;
  321. }
  322. /// <summary>
  323. /// Saves the game.
  324. /// Part of this happens asynchronously, so when this method returns the game has not been saved yet.
  325. /// </summary>
  326. public void SaveGame()
  327. {
  328. if (!VerifyMode()) return;
  329. if (menuMode)
  330. {
  331. throw new GameNotFoundException("Cannot save game using menu ID");
  332. }
  333. gameEngine.SaveCurrentGame();
  334. }
  335. /// <summary>
  336. /// Add information to the in-game debug display.
  337. /// When this object is garbage collected, this debug info is automatically removed.
  338. /// The provided getter function is called each frame so make sure it returns quickly.
  339. /// </summary>
  340. /// <param name="id">Debug info identifier.</param>
  341. /// <param name="contentGetter">A function that returns the current information.</param>
  342. public void AddDebugInfo(string id, Func<string> contentGetter)
  343. {
  344. if (!VerifyMode()) return;
  345. if (menuMode)
  346. {
  347. throw new GameNotFoundException("Game object references a menu item but AddDebugInfo only works on the currently-loaded game");
  348. }
  349. debugOverlayEngine.SetInfo("game_" + id, contentGetter);
  350. debugIds.Add(id);
  351. }
  352. /// <summary>
  353. /// Remove information from the in-game debug display.
  354. /// </summary>
  355. /// <returns><c>true</c>, if debug info was removed, <c>false</c> otherwise.</returns>
  356. /// <param name="id">Debug info identifier.</param>
  357. public bool RemoveDebugInfo(string id)
  358. {
  359. if (!VerifyMode()) return false;
  360. if (menuMode)
  361. {
  362. throw new GameNotFoundException("Game object references a menu item but RemoveDebugInfo only works on the currently-loaded game");
  363. }
  364. if (!debugIds.Contains(id)) return false;
  365. debugOverlayEngine.RemoveInfo("game_" + id);
  366. return debugIds.Remove(id);
  367. }
  368. /// <summary>
  369. /// Add information to the in-game debug display.
  370. /// This debug info will be present for all games until it is manually removed.
  371. /// The provided getter function is called each frame so make sure it returns quickly.
  372. /// </summary>
  373. /// <param name="id">Debug info identifier.</param>
  374. /// <param name="contentGetter">A function that returns the current information.</param>
  375. public static void AddPersistentDebugInfo(string id, Func<string> contentGetter)
  376. {
  377. debugOverlayEngine.SetInfo("persistent_" + id, contentGetter);
  378. }
  379. /// <summary>
  380. /// Remove persistent information from the in-game debug display.
  381. /// </summary>
  382. /// <returns><c>true</c>, if debug info was removed, <c>false</c> otherwise.</returns>
  383. /// <param name="id">Debug info identifier.</param>
  384. public static bool RemovePersistentDebugInfo(string id)
  385. {
  386. return debugOverlayEngine.RemoveInfo("persistent_" + id);
  387. }
  388. /// <summary>
  389. /// Gets the blocks in the game.
  390. /// This returns null when in a loading state, and throws AppStateException when in menu.
  391. /// </summary>
  392. /// <returns>The blocks in game.</returns>
  393. /// <param name="filter">The block to search for. BlockIDs.Invalid will return all blocks.</param>
  394. public Block[] GetBlocksInGame(BlockIDs filter = BlockIDs.Invalid)
  395. {
  396. if (!VerifyMode()) return null;
  397. if (menuMode)
  398. {
  399. throw new AppStateException("Game object references a menu item but GetBlocksInGame only works on the currently-loaded game");
  400. }
  401. EGID[] blockEGIDs = gameEngine.GetAllBlocksInGame(filter);
  402. Block[] blocks = new Block[blockEGIDs.Length];
  403. for (int b = 0; b < blockEGIDs.Length; b++)
  404. {
  405. blocks[b] = Block.New(blockEGIDs[b]);
  406. }
  407. return blocks;
  408. }
  409. /// <summary>
  410. /// Enable the screenshot taker for updating the game's screenshot. Breaks the pause menu in a new save.
  411. /// </summary>
  412. public void EnableScreenshotTaker()
  413. {
  414. if (!VerifyMode()) return;
  415. gameEngine.EnableScreenshotTaker();
  416. }
  417. ~Game()
  418. {
  419. foreach (string id in debugIds)
  420. {
  421. debugOverlayEngine.RemoveInfo(id);
  422. }
  423. }
  424. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  425. private bool VerifyMode()
  426. {
  427. if (menuMode && (!menuEngine.IsInMenu || gameEngine.IsInGame))
  428. {
  429. // either game loading or API is broken
  430. return false;
  431. }
  432. if (!menuMode && (menuEngine.IsInMenu || !gameEngine.IsInGame))
  433. {
  434. // either game loading or API is broken
  435. return false;
  436. }
  437. return true;
  438. }
  439. internal static void Init()
  440. {
  441. GameEngineManager.AddGameEngine(gameEngine);
  442. GameEngineManager.AddGameEngine(debugOverlayEngine);
  443. GameEngineManager.AddGameEngine(buildSimEventEngine);
  444. MenuEngineManager.AddMenuEngine(menuEngine);
  445. }
  446. }
  447. }