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.

454 lines
13KB

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