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 4.0KB

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