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.

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