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.

GameMenuEngine.cs 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. using System;
  2. using HarmonyLib;
  3. using RobocraftX;
  4. using RobocraftX.Common;
  5. using RobocraftX.GUI;
  6. using RobocraftX.GUI.MyGamesScreen;
  7. using Svelto.ECS;
  8. using Svelto.ECS.Experimental;
  9. using GamecraftModdingAPI.Engines;
  10. using GamecraftModdingAPI.Utility;
  11. namespace GamecraftModdingAPI.App
  12. {
  13. public class GameMenuEngine : IFactoryEngine
  14. {
  15. public IEntityFactory Factory { set; private get; }
  16. public string Name => "GamecraftModdingAPIGameInfoGameEngine";
  17. public bool isRemovable => false;
  18. public EntitiesDB entitiesDB { set; private get; }
  19. public void Dispose()
  20. {
  21. IsInMenu = false;
  22. }
  23. public void Ready()
  24. {
  25. IsInMenu = true;
  26. }
  27. // game functionality
  28. public bool IsInMenu
  29. {
  30. get;
  31. private set;
  32. } = false;
  33. public bool CreateMyGame(EGID id, string path = "", uint thumbnailId = 0, string gameName = "", string creatorName = "", string description = "", long createdDate = 0L)
  34. {
  35. EntityComponentInitializer eci = Factory.BuildEntity<MyGameDataEntityDescriptor_DamnItFJWhyDidYouMakeThisInternal>(id);
  36. eci.Init(new MyGameDataEntityStruct
  37. {
  38. SavedGamePath = new ECSString(path),
  39. ThumbnailId = thumbnailId,
  40. GameName = new ECSString(gameName),
  41. CreatorName = new ECSString(creatorName),
  42. GameDescription = new ECSString(description),
  43. CreatedDate = createdDate,
  44. });
  45. // entitiesDB.PublishEntityChange<MyGameDataEntityStruct>(id); // this will always fail
  46. return true;
  47. }
  48. public uint HighestID()
  49. {
  50. EntityCollection<MyGameDataEntityStruct> games = entitiesDB.QueryEntities<MyGameDataEntityStruct>(MyGamesScreenExclusiveGroups.MyGames);
  51. uint max = 0;
  52. for (int i = 0; i < games.count; i++)
  53. {
  54. if (games[i].ID.entityID > max)
  55. {
  56. max = games[i].ID.entityID;
  57. }
  58. }
  59. return max;
  60. }
  61. public bool EnterGame(EGID id)
  62. {
  63. if (!ExistsGameInfo(id)) return false;
  64. ref MyGameDataEntityStruct mgdes = ref GetGameInfo(id);
  65. return EnterGame(mgdes.GameName, mgdes.SavedGamePath);
  66. }
  67. public bool EnterGame(string gameName, string path, ulong workshopId = 0uL, bool autoEnterSim = false)
  68. {
  69. GameMode.CurrentMode = autoEnterSim ? RCXMode.Play : RCXMode.Build;
  70. GameMode.SaveGameDetails = new SaveGameDetails(gameName, path, workshopId);
  71. // the private FullGameCompositionRoot.SwitchToGame() method gets passed to menu items for this reason
  72. AccessTools.Method(typeof(FullGameCompositionRoot), "SwitchToGame").Invoke(FullGameFields.Instance, new object[0]);
  73. return true;
  74. }
  75. public bool SetGameName(EGID id, string name)
  76. {
  77. if (!ExistsGameInfo(id)) return false;
  78. GetGameInfo(id).GameName.Set(name);
  79. GetGameViewInfo(id).MyGamesSlotComponent.GameName = StringUtil.SanitiseString(name);
  80. return true;
  81. }
  82. public bool SetGameDescription(EGID id, string name)
  83. {
  84. if (!ExistsGameInfo(id)) return false;
  85. GetGameInfo(id).GameDescription.Set(name);
  86. GetGameViewInfo(id).MyGamesSlotComponent.GameDescription = StringUtil.SanitiseString(name);
  87. return true;
  88. }
  89. public bool ExistsGameInfo(EGID id)
  90. {
  91. return entitiesDB.Exists<MyGameDataEntityStruct>(id);
  92. }
  93. public ref MyGameDataEntityStruct GetGameInfo(EGID id)
  94. {
  95. return ref GetComponent<MyGameDataEntityStruct>(id);
  96. }
  97. public ref MyGamesSlotEntityViewStruct GetGameViewInfo(EGID id)
  98. {
  99. return ref GetComponent<MyGamesSlotEntityViewStruct>(new EGID(id.entityID, MyGamesScreenExclusiveGroups.GameSlotGuiEntities));
  100. }
  101. public ref T GetComponent<T>(EGID id) where T: struct, IEntityComponent
  102. {
  103. return ref entitiesDB.QueryEntity<T>(id);
  104. }
  105. }
  106. internal class MyGameDataEntityDescriptor_DamnItFJWhyDidYouMakeThisInternal : GenericEntityDescriptor<MyGameDataEntityStruct> { }
  107. }