Skip to content

Commit

Permalink
Sync "spawn" command for all entities and restrict "sub" and "spawn" …
Browse files Browse the repository at this point in the history
…to at least moderator perms
  • Loading branch information
tornac1234 committed Feb 16, 2024
1 parent 43fc3c8 commit d1cc4a7
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 41 deletions.
2 changes: 1 addition & 1 deletion NitroxClient/GameLogic/MobileVehicleBay.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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));

Expand Down
16 changes: 4 additions & 12 deletions NitroxClient/GameLogic/NitroxConsole.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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}");
}

/// <summary>
/// Spawns a Pickupable item
/// </summary>
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)
Expand Down
2 changes: 1 addition & 1 deletion NitroxClient/GameLogic/Vehicles.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

/// <summary>
/// Prevents local player from using "spawn" command without at least the <see cref="Perms.MODERATOR"/> permissions.
/// </summary>
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<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> 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<NitroxConsole>().Spawn(gameObject);
if (Resolve<LocalPlayer>().Permissions < Perms.MODERATOR)
{
Log.InGame(Language.main.Get("Nitrox_MissingPermission").Replace("{PERMISSION}", Perms.MODERATOR.ToString()));
return false;
}
return true;
}
}
Original file line number Diff line number Diff line change
@@ -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;

/// <summary>
/// Syncs "spawn" command.
/// </summary>
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<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> 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<NitroxConsole>().Spawn(gameObject);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/// <summary>
/// Prevents local player from using "sub" command without at least the <see cref="Perms.MODERATOR"/> permissions.
/// Once they have the permissions, sync this command.
/// </summary>
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<LocalPlayer>().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));
Expand Down

0 comments on commit d1cc4a7

Please sign in to comment.