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.

509 lines
16KB

  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 += ExceptionUtil.WrapHandler(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 += ExceptionUtil.WrapHandler(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 += ExceptionUtil.WrapHandler(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 += ExceptionUtil.WrapHandler(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. /// The provided getter function is called each frame so make sure it returns quickly.
  364. /// </summary>
  365. /// <param name="id">Debug info identifier.</param>
  366. /// <param name="contentGetter">A function that returns the current information.</param>
  367. public void AddDebugInfo(string id, Func<string> contentGetter)
  368. {
  369. if (!VerifyMode()) return;
  370. if (menuMode)
  371. {
  372. throw new GameNotFoundException("Game object references a menu item but AddDebugInfo only works on the currently-loaded game");
  373. }
  374. debugOverlayEngine.SetInfo("game_" + id, contentGetter);
  375. debugIds.Add(id);
  376. }
  377. /// <summary>
  378. /// Remove information from the in-game debug display.
  379. /// </summary>
  380. /// <returns><c>true</c>, if debug info was removed, <c>false</c> otherwise.</returns>
  381. /// <param name="id">Debug info identifier.</param>
  382. public bool RemoveDebugInfo(string id)
  383. {
  384. if (!VerifyMode()) return false;
  385. if (menuMode)
  386. {
  387. throw new GameNotFoundException("Game object references a menu item but RemoveDebugInfo only works on the currently-loaded game");
  388. }
  389. if (!debugIds.Contains(id)) return false;
  390. debugOverlayEngine.RemoveInfo("game_" + id);
  391. return debugIds.Remove(id);
  392. }
  393. /// <summary>
  394. /// Add information to the in-game debug display.
  395. /// This debug info will be present for all games until it is manually removed.
  396. /// The provided getter function is called each frame so make sure it returns quickly.
  397. /// </summary>
  398. /// <param name="id">Debug info identifier.</param>
  399. /// <param name="contentGetter">A function that returns the current information.</param>
  400. public static void AddPersistentDebugInfo(string id, Func<string> contentGetter)
  401. {
  402. debugOverlayEngine.SetInfo("persistent_" + id, contentGetter);
  403. }
  404. /// <summary>
  405. /// Remove persistent information from the in-game debug display.
  406. /// </summary>
  407. /// <returns><c>true</c>, if debug info was removed, <c>false</c> otherwise.</returns>
  408. /// <param name="id">Debug info identifier.</param>
  409. public static bool RemovePersistentDebugInfo(string id)
  410. {
  411. return debugOverlayEngine.RemoveInfo("persistent_" + id);
  412. }
  413. /// <summary>
  414. /// Gets the blocks in the game.
  415. /// This returns null when in a loading state, and throws AppStateException when in menu.
  416. /// </summary>
  417. /// <returns>The blocks in game.</returns>
  418. /// <param name="filter">The block to search for. BlockIDs.Invalid will return all blocks.</param>
  419. public Block[] GetBlocksInGame(BlockIDs filter = BlockIDs.Invalid)
  420. {
  421. if (!VerifyMode()) return null;
  422. if (menuMode)
  423. {
  424. throw new AppStateException("Game object references a menu item but GetBlocksInGame only works on the currently-loaded game");
  425. }
  426. EGID[] blockEGIDs = gameEngine.GetAllBlocksInGame(filter);
  427. Block[] blocks = new Block[blockEGIDs.Length];
  428. for (int b = 0; b < blockEGIDs.Length; b++)
  429. {
  430. blocks[b] = Block.New(blockEGIDs[b]);
  431. }
  432. return blocks;
  433. }
  434. ~Game()
  435. {
  436. foreach (string id in debugIds)
  437. {
  438. debugOverlayEngine.RemoveInfo(id);
  439. }
  440. }
  441. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  442. private bool VerifyMode()
  443. {
  444. if (menuMode && (!menuEngine.IsInMenu || gameEngine.IsInGame))
  445. {
  446. // either game loading or API is broken
  447. return false;
  448. }
  449. if (!menuMode && (menuEngine.IsInMenu || !gameEngine.IsInGame))
  450. {
  451. // either game loading or API is broken
  452. return false;
  453. }
  454. return true;
  455. }
  456. internal static void Init()
  457. {
  458. GameEngineManager.AddGameEngine(gameEngine);
  459. GameEngineManager.AddGameEngine(debugOverlayEngine);
  460. GameEngineManager.AddGameEngine(buildSimEventEngine);
  461. MenuEngineManager.AddMenuEngine(menuEngine);
  462. }
  463. }
  464. }