From c1c226ef2a4221d6f35f9f45f3c5da181a576747 Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Fri, 4 Jun 2021 23:07:06 +0200 Subject: [PATCH] Added support for setting default color/material and static blocks Also fixed setting game name/description When setting the block color or material to default it will look up the default value to use Blocks can now be set to be static so they don't fall or move at all --- TechbloxModdingAPI/App/GameMenuEngine.cs | 25 +++++++---------- TechbloxModdingAPI/Block.cs | 27 +++++++++++++++---- .../Blocks/Engines/PlacementEngine.cs | 6 ----- .../Interface/IMGUI/Constants.cs | 2 +- .../Tests/TechbloxModdingAPIPluginTest.cs | 9 +++++++ 5 files changed, 41 insertions(+), 28 deletions(-) diff --git a/TechbloxModdingAPI/App/GameMenuEngine.cs b/TechbloxModdingAPI/App/GameMenuEngine.cs index 3357984..4738507 100644 --- a/TechbloxModdingAPI/App/GameMenuEngine.cs +++ b/TechbloxModdingAPI/App/GameMenuEngine.cs @@ -92,7 +92,7 @@ namespace TechbloxModdingAPI.App { if (!ExistsGameInfo(id)) return false; GetGameInfo(id).GameName.Set(name); - //GetGameViewInfo(id).MyGamesSlotComponent.GameName = StringUtil.SanitiseString(name); - TODO: Input field struct + GetGameViewInfo(id).MyGamesSlotComponent.GameName = StringUtil.SanitiseString(name); return true; } @@ -100,7 +100,7 @@ namespace TechbloxModdingAPI.App { if (!ExistsGameInfo(id)) return false; GetGameInfo(id).GameDescription.Set(name); - //GetGameViewInfo(id).MyGamesSlotComponent.GameDescription = StringUtil.SanitiseString(name); - TODO + GetGameViewInfo(id).MyGamesSlotComponent.GameDescription = StringUtil.SanitiseString(name); return true; } @@ -114,21 +114,14 @@ namespace TechbloxModdingAPI.App return ref GetComponent(id); } - /*public ref MyGamesSlotEntityViewStruct GetGameViewInfo(EGID id) + public dynamic GetGameViewInfo(EGID id) { - EntityCollection entities = - entitiesDB.QueryEntities(MyGamesScreenExclusiveGroups.GameSlotGuiEntities); - var entitiesB = entities.ToBuffer().buffer; - for (int i = 0; i < entities.count; i++) - { - if (entitiesB[i].ID.entityID == id.entityID) - { - return ref entitiesB[i]; - } - } - MyGamesSlotEntityViewStruct[] defRef = new MyGamesSlotEntityViewStruct[1]; - return ref defRef[0]; - TODO: The struct is internal now - }*/ + dynamic structOptional = AccessTools.Method("TechbloxModdingAPI.Utility.NativeApiExtensions:QueryEntityOptional", new []{typeof(EntitiesDB), typeof(EGID)}) + .MakeGenericMethod(AccessTools.TypeByName("RobocraftX.GUI.MyGamesScreen.MyGamesSlotEntityViewStruct")) + .Invoke(null, new object[] {entitiesDB, new EGID(id.entityID, MyGamesScreenExclusiveGroups.GameSlotGuiEntities)}); + if (structOptional == null) throw new Exception("Could not get game slot entity"); + return structOptional ? structOptional : null; + } public ref T GetComponent(EGID id) where T: unmanaged, IEntityComponent { diff --git a/TechbloxModdingAPI/Block.cs b/TechbloxModdingAPI/Block.cs index 9d71d24..c264c17 100644 --- a/TechbloxModdingAPI/Block.cs +++ b/TechbloxModdingAPI/Block.cs @@ -248,11 +248,14 @@ namespace TechbloxModdingAPI } set { - //TODO: Check if setting to 255 works + if (value.Color == BlockColors.Default) + value = new BlockColor(FullGameFields._dataDb.TryGetValue((int) Type, out CubeListData cld) + ? cld.DefaultColour + : throw new BlockTypeException("Unknown block type! Could not set default color.")); ref var color = ref BlockEngine.GetBlockInfo(this); color.indexInPalette = value.Index; color.hasNetworkChange = true; - color.paletteColour = BlockEngine.ConvertBlockColor(color.indexInPalette); + color.paletteColour = BlockEngine.ConvertBlockColor(color.indexInPalette); //Setting to 255 results in black } } @@ -282,10 +285,15 @@ namespace TechbloxModdingAPI } set { - if (!FullGameFields._dataDb.ContainsKey((byte) value)) + byte val = (byte) value; + if (value == BlockMaterial.Default) + val = FullGameFields._dataDb.TryGetValue((int) Type, out CubeListData cld) + ? cld.DefaultMaterialID + : throw new BlockTypeException("Unknown block type! Could not set default material."); + if (!FullGameFields._dataDb.ContainsKey(val)) throw new BlockException($"Block material {value} does not exist!"); - BlockEngine.GetBlockInfo(this).materialId = (byte) value; - BlockEngine.UpdatePrefab(this, (byte) value, Flipped); //TODO: Test default + BlockEngine.GetBlockInfo(this).materialId = val; + BlockEngine.UpdatePrefab(this, val, Flipped); //The default causes the screen to go black } } @@ -336,6 +344,15 @@ namespace TechbloxModdingAPI } } + /// + /// Whether the block should be static in simulation. If set, it cannot be moved. The effect is temporary, it will not be saved with the block. + /// + public bool Static + { + get => BlockEngine.GetBlockInfo(this).staticIfUnconnected; + set => BlockEngine.GetBlockInfo(this).staticIfUnconnected = value; + } + /// /// Whether the block exists. The other properties will return a default value if the block doesn't exist. /// If the block was just placed, then this will also return false but the properties will work correctly. diff --git a/TechbloxModdingAPI/Blocks/Engines/PlacementEngine.cs b/TechbloxModdingAPI/Blocks/Engines/PlacementEngine.cs index 196df83..b2e5659 100644 --- a/TechbloxModdingAPI/Blocks/Engines/PlacementEngine.cs +++ b/TechbloxModdingAPI/Blocks/Engines/PlacementEngine.cs @@ -77,12 +77,6 @@ namespace TechbloxModdingAPI.Blocks.Engines triggerAutoWiring = autoWire && structInitializer.Has() }); - - /*structInitializer.Init(new OverrideStaticComponent() - { //TODO - staticIfUnconnected = true - });*/ - int nextFilterId = BlockGroupUtility.NextFilterId; structInitializer.Init(new BlockGroupEntityComponent { diff --git a/TechbloxModdingAPI/Interface/IMGUI/Constants.cs b/TechbloxModdingAPI/Interface/IMGUI/Constants.cs index 02564a6..ecb292a 100644 --- a/TechbloxModdingAPI/Interface/IMGUI/Constants.cs +++ b/TechbloxModdingAPI/Interface/IMGUI/Constants.cs @@ -24,7 +24,7 @@ namespace TechbloxModdingAPI.Interface.IMGUI /// /// Best-effort imitation of Techblox's UI style. /// - public static GUISkin Default //TODO: Update to Techblox style + public static GUISkin Default { get { diff --git a/TechbloxModdingAPI/Tests/TechbloxModdingAPIPluginTest.cs b/TechbloxModdingAPI/Tests/TechbloxModdingAPIPluginTest.cs index d938482..319ec76 100644 --- a/TechbloxModdingAPI/Tests/TechbloxModdingAPIPluginTest.cs +++ b/TechbloxModdingAPI/Tests/TechbloxModdingAPIPluginTest.cs @@ -194,6 +194,15 @@ namespace TechbloxModdingAPI.Tests Game.CurrentGame().ToggleTimeMode(); }).Build(); + CommandBuilder.Builder("testColorBlock", "Tests coloring a block to default color") + .Action(() => Player.LocalPlayer.GetBlockLookedAt().Color = BlockColors.Default).Build(); + CommandBuilder.Builder("testMaterialBlock", "Tests materialing a block to default material") + .Action(() => Player.LocalPlayer.GetBlockLookedAt().Material = BlockMaterial.Default).Build(); + CommandBuilder.Builder("testGameName", "Tests changing the game name") + .Action(() => Game.CurrentGame().Name = "Test").Build(); + CommandBuilder.Builder("makeBlockStatic", "Makes a block you look at static") + .Action(() => Player.LocalPlayer.GetBlockLookedAt().Static = true).Build(); + Game.AddPersistentDebugInfo("InstalledMods", InstalledMods); Block.Placed += (sender, args) => Logging.MetaDebugLog("Placed block " + args.Block);