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.

478 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 GamecraftModdingAPI;
  9. using GamecraftModdingAPI.Blocks;
  10. using GamecraftModdingAPI.Tasks;
  11. using GamecraftModdingAPI.Utility;
  12. namespace GamecraftModdingAPI.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:GamecraftModdingAPI.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:GamecraftModdingAPI.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:GamecraftModdingAPI.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, Gamecraft 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. /// 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. /// </summary>
  353. /// <param name="id">Debug info identifier.</param>
  354. /// <param name="contentGetter">Content getter.</param>
  355. public void AddDebugInfo(string id, Func<string> contentGetter)
  356. {
  357. if (!VerifyMode()) return;
  358. if (menuMode)
  359. {
  360. throw new GameNotFoundException("Game object references a menu item but AddDebugInfo only works on the currently-loaded game");
  361. }
  362. debugOverlayEngine.SetInfo(id, contentGetter);
  363. debugIds.Add(id);
  364. }
  365. /// <summary>
  366. /// Remove information from the in-game debug display.
  367. /// </summary>
  368. /// <returns><c>true</c>, if debug info was removed, <c>false</c> otherwise.</returns>
  369. /// <param name="id">Debug info identifier.</param>
  370. public bool RemoveDebugInfo(string id)
  371. {
  372. if (!VerifyMode()) return false;
  373. if (menuMode)
  374. {
  375. throw new GameNotFoundException("Game object references a menu item but RemoveDebugInfo only works on the currently-loaded game");
  376. }
  377. if (!debugIds.Contains(id)) return false;
  378. debugOverlayEngine.RemoveInfo(id);
  379. return debugIds.Remove(id);
  380. }
  381. /// <summary>
  382. /// Gets the blocks in the game.
  383. /// This returns null when in a loading state, and throws AppStateException when in menu.
  384. /// </summary>
  385. /// <returns>The blocks in game.</returns>
  386. /// <param name="filter">The block to search for. BlockIDs.Invalid will return all blocks.</param>
  387. public Block[] GetBlocksInGame(BlockIDs filter = BlockIDs.Invalid)
  388. {
  389. if (!VerifyMode()) return null;
  390. if (menuMode)
  391. {
  392. throw new AppStateException("Game object references a menu item but GetBlocksInGame only works on the currently-loaded game");
  393. }
  394. EGID[] blockEGIDs = gameEngine.GetAllBlocksInGame(filter);
  395. Block[] blocks = new Block[blockEGIDs.Length];
  396. for (int b = 0; b < blockEGIDs.Length; b++)
  397. {
  398. blocks[b] = new Block(blockEGIDs[b]);
  399. }
  400. return blocks;
  401. }
  402. ~Game()
  403. {
  404. foreach (string id in debugIds)
  405. {
  406. debugOverlayEngine.RemoveInfo(id);
  407. }
  408. }
  409. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  410. private bool VerifyMode()
  411. {
  412. if (menuMode && (!menuEngine.IsInMenu || gameEngine.IsInGame))
  413. {
  414. // either game loading or API is broken
  415. return false;
  416. }
  417. if (!menuMode && (menuEngine.IsInMenu || !gameEngine.IsInGame))
  418. {
  419. // either game loading or API is broken
  420. return false;
  421. }
  422. return true;
  423. }
  424. internal static void Init()
  425. {
  426. GameEngineManager.AddGameEngine(gameEngine);
  427. GameEngineManager.AddGameEngine(debugOverlayEngine);
  428. MenuEngineManager.AddMenuEngine(menuEngine);
  429. }
  430. internal static void InitDeterministic(StateSyncRegistrationHelper stateSyncReg)
  431. {
  432. stateSyncReg.AddDeterministicEngine(buildSimEventEngine);
  433. }
  434. }
  435. }