diff --git a/NitroxClient/GameLogic/MobileVehicleBay.cs b/NitroxClient/GameLogic/MobileVehicleBay.cs index 6a33c16586..b48f5a61a1 100644 --- a/NitroxClient/GameLogic/MobileVehicleBay.cs +++ b/NitroxClient/GameLogic/MobileVehicleBay.cs @@ -40,7 +40,7 @@ public void BeginCrafting(ConstructorInput constructor, GameObject constructedOb NitroxId constructedObjectId = NitroxEntity.GenerateNewId(constructedObject); - VehicleWorldEntity vehicleEntity = Vehicles.MakeVehicleEntity(constructedObject, constructedObjectId, techType, constructorId); + VehicleWorldEntity vehicleEntity = Vehicles.BuildVehicleWorldEntity(constructedObject, constructedObjectId, techType, constructorId); packetSender.Send(new EntitySpawnedByClient(vehicleEntity)); diff --git a/NitroxClient/GameLogic/NitroxConsole.cs b/NitroxClient/GameLogic/NitroxConsole.cs index acfb93ae40..ab866cb729 100644 --- a/NitroxClient/GameLogic/NitroxConsole.cs +++ b/NitroxClient/GameLogic/NitroxConsole.cs @@ -35,8 +35,7 @@ public void Spawn(GameObject gameObject) } else { - SpawnItem(gameObject); - //TODO: Add support for no AI creature that need to be spawned as well + DefaultSpawn(gameObject); } } catch (Exception ex) @@ -52,23 +51,16 @@ private void SpawnVehicle(GameObject gameObject, TechType techType) { NitroxId id = NitroxEntity.GetIdOrGenerateNew(gameObject); - VehicleWorldEntity vehicleEntity = Vehicles.MakeVehicleEntity(gameObject, id, techType); + VehicleWorldEntity vehicleEntity = Vehicles.BuildVehicleWorldEntity(gameObject, id, techType); packetSender.Send(new EntitySpawnedByClient(vehicleEntity)); Log.Debug($"Spawning vehicle {techType} with id {id} at {gameObject.transform.position}"); } - /// - /// Spawns a Pickupable item - /// - private void SpawnItem(GameObject gameObject) + private void DefaultSpawn(GameObject gameObject) { - if (gameObject.TryGetComponent(out Pickupable pickupable)) - { - Log.Debug($"Spawning item {pickupable.GetTechName()} at {gameObject.transform.position}"); - item.Dropped(gameObject, pickupable.GetTechType()); - } + item.Dropped(gameObject); } private static TechType GetObjectTechType(GameObject gameObject) diff --git a/NitroxClient/GameLogic/Vehicles.cs b/NitroxClient/GameLogic/Vehicles.cs index ba31024c32..3132b8b7b3 100644 --- a/NitroxClient/GameLogic/Vehicles.cs +++ b/NitroxClient/GameLogic/Vehicles.cs @@ -235,7 +235,7 @@ public static void RemoveNitroxEntitiesTagging(GameObject constructedObject) } } - public static VehicleWorldEntity MakeVehicleEntity(GameObject constructedObject, NitroxId constructedObjectId, TechType techType, NitroxId constructorId = null) + public static VehicleWorldEntity BuildVehicleWorldEntity(GameObject constructedObject, NitroxId constructedObjectId, TechType techType, NitroxId constructorId = null) { VehicleWorldEntity vehicleEntity = new(constructorId, DayNightCycle.main.timePassedAsFloat, constructedObject.transform.ToLocalDto(), string.Empty, false, constructedObjectId, techType.ToDto(), null); VehicleChildEntityHelper.PopulateChildren(constructedObjectId, constructedObject.GetFullHierarchyPath(), vehicleEntity.ChildEntities, constructedObject); diff --git a/NitroxPatcher/Patches/Dynamic/SpawnConsoleCommand_OnConsoleCommand_Patch.cs b/NitroxPatcher/Patches/Dynamic/SpawnConsoleCommand_OnConsoleCommand_Patch.cs index c7133fd69b..4381696cc4 100644 --- a/NitroxPatcher/Patches/Dynamic/SpawnConsoleCommand_OnConsoleCommand_Patch.cs +++ b/NitroxPatcher/Patches/Dynamic/SpawnConsoleCommand_OnConsoleCommand_Patch.cs @@ -1,39 +1,24 @@ -using System.Collections.Generic; using System.Reflection; -using System.Reflection.Emit; -using HarmonyLib; using NitroxClient.GameLogic; +using NitroxModel.DataStructures.GameLogic; using NitroxModel.Helper; -using UnityEngine; namespace NitroxPatcher.Patches.Dynamic; +/// +/// Prevents local player from using "spawn" command without at least the permissions. +/// public sealed partial class SpawnConsoleCommand_OnConsoleCommand_Patch : NitroxPatch, IDynamicPatch { - internal static readonly MethodInfo TARGET_METHOD = AccessTools.EnumeratorMoveNext(Reflect.Method((SpawnConsoleCommand t) => t.SpawnAsync(default))); + internal static readonly MethodInfo TARGET_METHOD = Reflect.Method((SpawnConsoleCommand t) => t.OnConsoleCommand_spawn(default)); - /* - * GameObject gameObject = global::Utils.CreatePrefab(prefabForTechType, maxDist, i > 0); - * -> SpawnConsoleCommand_OnConsoleCommand_Patch.Callback(gameObject); - * LargeWorldEntity.Register(gameObject); - * CrafterLogic.NotifyCraftEnd(gameObject, techType); - * gameObject.SendMessage("StartConstruction", SendMessageOptions.DontRequireReceiver); - */ - public static IEnumerable Transpiler(IEnumerable instructions) + public static bool Prefix(NotificationCenter.Notification n) { - return new CodeMatcher(instructions).MatchStartForward([ - new CodeMatch(OpCodes.Call, Reflect.Method(() => Utils.CreatePrefab(default, default, default))) - ]) - .Advance(1) - .Insert([ - new CodeInstruction(OpCodes.Dup), - new CodeInstruction(OpCodes.Call, Reflect.Method(() => Callback(default))) - ]) - .InstructionEnumeration(); - } - - public static void Callback(GameObject gameObject) - { - Resolve().Spawn(gameObject); + if (Resolve().Permissions < Perms.MODERATOR) + { + Log.InGame(Language.main.Get("Nitrox_MissingPermission").Replace("{PERMISSION}", Perms.MODERATOR.ToString())); + return false; + } + return true; } } diff --git a/NitroxPatcher/Patches/Dynamic/SpawnConsoleCommand_SpawnAsync_Patch.cs b/NitroxPatcher/Patches/Dynamic/SpawnConsoleCommand_SpawnAsync_Patch.cs new file mode 100644 index 0000000000..750de7a5be --- /dev/null +++ b/NitroxPatcher/Patches/Dynamic/SpawnConsoleCommand_SpawnAsync_Patch.cs @@ -0,0 +1,43 @@ +using System.Collections.Generic; +using System.Reflection; +using System.Reflection.Emit; +using HarmonyLib; +using NitroxClient.GameLogic; +using NitroxModel.Helper; +using UnityEngine; + +namespace NitroxPatcher.Patches.Dynamic; + +/// +/// Syncs "spawn" command. +/// +public sealed partial class SpawnConsoleCommand_SpawnAsync_Patch : NitroxPatch, IDynamicPatch +{ + internal static readonly MethodInfo TARGET_METHOD = AccessTools.EnumeratorMoveNext(Reflect.Method((SpawnConsoleCommand t) => t.SpawnAsync(default))); + + /* + * MODIFIED: + * GameObject gameObject = global::Utils.CreatePrefab(prefabForTechType, maxDist, i > 0); + * SpawnConsoleCommand_OnConsoleCommand_Patch.WrappedCallback(gameObject); <---- INSERTED LINE + * LargeWorldEntity.Register(gameObject); + * CrafterLogic.NotifyCraftEnd(gameObject, techType); + * gameObject.SendMessage("StartConstruction", SendMessageOptions.DontRequireReceiver); + */ + public static IEnumerable Transpiler(IEnumerable instructions) + { + return new CodeMatcher(instructions).MatchStartForward([ + new CodeMatch(OpCodes.Call, Reflect.Method(() => Utils.CreatePrefab(default, default, default))) + ]) + .Advance(1) + .Insert([ + new CodeInstruction(OpCodes.Dup), + new CodeInstruction(OpCodes.Call, Reflect.Method(() => Callback(default))) + ]) + .InstructionEnumeration(); + } + + public static void Callback(GameObject gameObject) + { + Resolve().Spawn(gameObject); + } +} diff --git a/NitroxPatcher/Patches/Dynamic/SubConsoleCommand_OnConsoleCommand_sub_Patch.cs b/NitroxPatcher/Patches/Dynamic/SubConsoleCommand_OnConsoleCommand_sub_Patch.cs index eb9fb89d6e..8c7714329b 100644 --- a/NitroxPatcher/Patches/Dynamic/SubConsoleCommand_OnConsoleCommand_sub_Patch.cs +++ b/NitroxPatcher/Patches/Dynamic/SubConsoleCommand_OnConsoleCommand_sub_Patch.cs @@ -3,15 +3,37 @@ using System.Reflection.Emit; using HarmonyLib; using NitroxClient.GameLogic; +using NitroxModel.DataStructures.GameLogic; using NitroxModel.Helper; using UnityEngine; namespace NitroxPatcher.Patches.Dynamic; +/// +/// Prevents local player from using "sub" command without at least the permissions. +/// Once they have the permissions, sync this command. +/// public sealed partial class SubConsoleCommand_OnConsoleCommand_sub_Patch : NitroxPatch, IDynamicPatch { internal static readonly MethodInfo TARGET_METHOD = Reflect.Method((SubConsoleCommand t) => t.OnConsoleCommand_sub(default)); + public static bool Prefix(NotificationCenter.Notification n) + { + if (Resolve().Permissions < Perms.MODERATOR) + { + Log.InGame(Language.main.Get("Nitrox_MissingPermission").Replace("{PERMISSION}", Perms.MODERATOR.ToString())); + return false; + } + + string text = (string)n.data[0]; + if (!string.IsNullOrEmpty(text) && !text.ToLowerInvariant().Equals("cyclops")) + { + Log.InGame(Language.main.Get("Nitrox_CommandNotAvailable")); + return false; + } + return true; + } + /* * REPLACE: * LightmappedPrefabs.main.RequestScenePrefab(text, new LightmappedPrefabs.OnPrefabLoaded(this.OnSubPrefabLoaded));