Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions EXILED/Exiled.API/Features/Items/Firearm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ namespace Exiled.API.Features.Items
using Extensions;
using InventorySystem;
using InventorySystem.Items;
using InventorySystem.Items.Autosync;
using InventorySystem.Items.Firearms;
using InventorySystem.Items.Firearms.Attachments;
using InventorySystem.Items.Firearms.Attachments.Components;
Expand Down Expand Up @@ -690,6 +691,7 @@ public override Item Clone()
/// <param name="newOwner">new <see cref="Firearm"/> owner.</param>
internal override void ChangeOwner(Player oldOwner, Player newOwner)
{
Base.InstantiationStatus = newOwner == Server.Host ? AutosyncInstantiationStatus.SimulatedInstance : AutosyncInstantiationStatus.InventoryInstance;
Base.Owner = newOwner.ReferenceHub;
Base._footprintCacheSet = false;
foreach (ModuleBase module in Base.Modules)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public override bool IsCocked

set
{
PumpBarrel.SyncCocked = MaxAmmo;
PumpBarrel.SyncCocked = value ? MaxAmmo : 0;
Resync();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,16 @@ public override int MaxAmmo
set => MagazineModule._defaultCapacity = value;
}

/// <inheritdoc/>
public override int Ammo
{
set
{
MagazineModule.SyncData[MagazineModule.ItemSerial] = Math.Max(value, 0) + 1;
Resync();
}
}

/// <inheritdoc/>
public override int ConstantMaxAmmo => MagazineModule._defaultCapacity;

Expand Down
2 changes: 2 additions & 0 deletions EXILED/Exiled.API/Features/Items/Item.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ public Item(ItemBase itemBase)
ushort serial = ItemSerialGenerator.GenerateNext();
Serial = serial;
itemBase.OnAdded(null);
if (Base is ModularAutosyncItem syncItem)
syncItem.InstantiationStatus = AutosyncInstantiationStatus.SimulatedInstance;
#if DEBUG
Log.Debug($"{nameof(Item)}.ctor: Generating new serial number. Serial should now be: {serial}. // {Serial}");
#endif
Expand Down
16 changes: 16 additions & 0 deletions EXILED/Exiled.API/Features/Items/MicroHid.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ namespace Exiled.API.Features.Items

using Exiled.API.Features.Pickups;
using Exiled.API.Interfaces;

using InventorySystem.Items.Autosync;
using InventorySystem.Items.Firearms.Modules;
using InventorySystem.Items.MicroHID;
using InventorySystem.Items.MicroHID.Modules;

Expand Down Expand Up @@ -214,5 +217,18 @@ public bool TryGetFireController<T>(MicroHidFiringMode firingMode, out T module)
/// </summary>
/// <returns>A string containing MicroHid-related data.</returns>
public override string ToString() => $"{Type} ({Serial}) [{Weight}] *{Scale}* |{Energy}| -{State}-";

/// <inheritdoc/>
internal override void ChangeOwner(Player oldOwner, Player newOwner)
{
Base.Owner = newOwner.ReferenceHub;

for (int i = 0; i < Base.AllSubcomponents.Length; i++)
{
Base.AllSubcomponents[i].OnAdded();
}

Base.InstantiationStatus = newOwner == Server.Host ? AutosyncInstantiationStatus.SimulatedInstance : AutosyncInstantiationStatus.InventoryInstance;
}
}
}
4 changes: 2 additions & 2 deletions EXILED/Exiled.API/Features/Room.cs
Original file line number Diff line number Diff line change
Expand Up @@ -401,8 +401,8 @@ public void UnlockAll()
/// Factory method to create and add a <see cref="Room"/> component to a Transform.
/// We can add parameters to be set privately here.
/// </summary>
/// <param name="roomIdentifier">The Game Object to attach the Room component to.</param>
internal static void CreateComponent(RoomIdentifier roomIdentifier) => roomIdentifier.gameObject.AddComponent<Room>();
/// <param name="baseRoom">The Game Object to attach the Room component to.</param>
internal static void CreateComponent(GameObject baseRoom) => baseRoom.AddComponent<Room>().InternalCreate();

/// <summary>
/// Factory method to complete all element inside a Room.
Expand Down
32 changes: 32 additions & 0 deletions EXILED/Exiled.Events/Patches/Fixes/ServerHubMicroHidFix.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// -----------------------------------------------------------------------
// <copyright file="ServerHubMicroHidFix.cs" company="ExMod Team">
// Copyright (c) ExMod Team. All rights reserved.
// Licensed under the CC BY-SA 3.0 license.
// </copyright>
// -----------------------------------------------------------------------

namespace Exiled.Events.Patches.Fixes
{
#pragma warning disable SA1313
using API.Features.Items;
using Exiled.API.Features;

using HarmonyLib;

using InventorySystem.Items.Autosync;
using InventorySystem.Items.MicroHID.Modules;
using InventorySystem.Items.Pickups;
using InventorySystem.Items.Usables.Scp330;

/// <summary>
/// Patches <see cref="CycleSyncModule.Update()"/> to fix phantom <see cref="MicroHid"/> for <see cref="Item.Create(ItemType, API.Features.Player)"/>.
/// </summary>
[HarmonyPatch(typeof(CycleSyncModule), nameof(CycleSyncModule.Update))]
internal static class ServerHubMicroHidFix
{
private static bool Prefix(CycleSyncModule __instance)
{
return __instance.MicroHid.InstantiationStatus == AutosyncInstantiationStatus.InventoryInstance;
}
}
}
33 changes: 25 additions & 8 deletions EXILED/Exiled.Events/Patches/Generic/AmmoDrain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ namespace Exiled.Events.Patches.Generic

using HarmonyLib;

using InventorySystem;
using InventorySystem.Items;
using InventorySystem.Items.Firearms;
using InventorySystem.Items.Firearms.Modules;
Expand Down Expand Up @@ -66,6 +67,22 @@ internal static IEnumerable<CodeInstruction> GetInstructions(LocalBuilder firear
yield return new CodeInstruction(OpCodes.Nop).WithLabels(continueLabel);
}

/// <summary>
/// Calculates modified ammo limit.
/// </summary>
/// <param name="ammoLimit">Current ammo limit for reloading.</param>
/// <param name="ammoDrain">Target <see cref="API.Features.Items.Firearm.AmmoDrain"/>.</param>
/// <returns>Modified ammo limit for reloading.</returns>
internal static int GetAmmoDrainLimit(int ammoLimit, int ammoDrain)
{
if (ammoDrain == 0)
{
return int.MaxValue;
}

return ammoLimit / ammoDrain;
}

// that patch for open bolted firearms
[HarmonyPatch(nameof(AutomaticActionModule.ServerShoot))]
[HarmonyTranspiler]
Expand All @@ -89,7 +106,7 @@ private static IEnumerable<CodeInstruction> First(IEnumerable<CodeInstruction> c
newInstructions.InsertRange(index, new CodeInstruction[]
{
new(OpCodes.Ldloc_S, ammoDrain.LocalIndex),
new(OpCodes.Div),
new(OpCodes.Call, Method(typeof(AmmoDrainAutomatic), nameof(AmmoDrainAutomatic.GetAmmoDrainLimit))),
});

offset = 0;
Expand Down Expand Up @@ -131,7 +148,7 @@ private static IEnumerable<CodeInstruction> Second(IEnumerable<CodeInstruction>
newInstructions.InsertRange(index, new CodeInstruction[]
{
new(OpCodes.Ldloc_S, ammoDrain.LocalIndex),
new(OpCodes.Div),
new(OpCodes.Call, Method(typeof(AmmoDrainAutomatic), nameof(AmmoDrainAutomatic.GetAmmoDrainLimit))),
});

offset = 0;
Expand Down Expand Up @@ -177,7 +194,7 @@ private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstructi
newInstructions.InsertRange(index, new CodeInstruction[]
{
new(OpCodes.Ldloc_S, ammoDrain.LocalIndex),
new(OpCodes.Div),
new(OpCodes.Call, Method(typeof(AmmoDrainAutomatic), nameof(AmmoDrainAutomatic.GetAmmoDrainLimit))),
});

offset = 0;
Expand All @@ -200,7 +217,7 @@ private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstructi
/// <summary>
/// Patch for adding <see cref="API.Features.Items.Firearm.AmmoDrain"/> support for revolvers.
/// </summary>
[HarmonyPatch(typeof(RevolverClipReloaderModule), nameof(RevolverClipReloaderModule.InsertAmmoFromClip))]
[HarmonyPatch(typeof(RevolverClipReloaderModule), nameof(RevolverClipReloaderModule.ServerWithholdAmmo))]
internal class AmmoDrainRevolver
{
private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> codeInstructions, ILGenerator generator)
Expand All @@ -224,17 +241,17 @@ private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstructi
newInstructions.InsertRange(index, new CodeInstruction[]
{
new(OpCodes.Ldloc_S, ammoDrain.LocalIndex),
new(OpCodes.Mul),
new(OpCodes.Call, Method(typeof(AmmoDrainAutomatic), nameof(AmmoDrainAutomatic.GetAmmoDrainLimit))),
});

offset = 0;
index = newInstructions.FindIndex(i => i.Calls(Method(typeof(IPrimaryAmmoContainerModule), nameof(IPrimaryAmmoContainerModule.ServerModifyAmmo)))) + offset;
index = newInstructions.FindIndex(i => i.Calls(Method(typeof(InventoryExtensions), nameof(InventoryExtensions.ServerAddAmmo)))) + offset;

// and divide ammo that are inserting in magazine, to implement AmmoDrain
// and multiply ammo that are inserting in magazine, to implement AmmoDrain
newInstructions.InsertRange(index, new CodeInstruction[]
{
new(OpCodes.Ldloc_S, ammoDrain.LocalIndex),
new(OpCodes.Div),
new(OpCodes.Mul),
});

for (int z = 0; z < newInstructions.Count; z++)
Expand Down
13 changes: 0 additions & 13 deletions EXILED/Exiled.Events/Patches/Generic/RoomLightControllersList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ namespace Exiled.Events.Patches.Generic
{
using Exiled.API.Features;
#pragma warning disable SA1313
#pragma warning disable SA1402

using HarmonyLib;

Expand All @@ -24,16 +23,4 @@ private static void Postfix(RoomLightController __instance)
Room.Get(__instance.Room).RoomLightControllersValue.Add(__instance);
}
}

/// <summary>
/// Patch for removing <see cref="RoomLightController"/> to list.
/// </summary>
[HarmonyPatch(typeof(RoomLightController), nameof(RoomLightController.OnDestroy))]
internal class RoomLightControllersList2
{
private static void Postfix(RoomLightController __instance)
{
Room.Get(__instance.Room).RoomLightControllersValue.Remove(__instance);
}
}
}
15 changes: 1 addition & 14 deletions EXILED/Exiled.Events/Patches/Generic/RoomList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstructi
{
new CodeInstruction(OpCodes.Ldarg_0).MoveLabelsFrom(newInstructions[index]),
new(OpCodes.Callvirt, PropertyGetter(typeof(Component), nameof(Component.gameObject))),
new(OpCodes.Callvirt, Method(typeof(Room), nameof(Room.FindParentRoom), new System.Type[] { typeof(GameObject) })),
new(OpCodes.Callvirt, Method(typeof(Room), nameof(Room.InternalCreate))),
new(OpCodes.Callvirt, Method(typeof(Room), nameof(Room.CreateComponent))),
});

for (int z = 0; z < newInstructions.Count; z++)
Expand All @@ -55,18 +54,6 @@ private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstructi
}
}

/// <summary>
/// Patches <see cref="RoomIdentifier.Awake"/>.
/// </summary>
[HarmonyPatch(typeof(RoomIdentifier), nameof(RoomIdentifier.Awake))]
internal class RoomListAdd
{
private static void Postfix(RoomIdentifier __instance)
{
Room.CreateComponent(__instance);
}
}

/// <summary>
/// Patches <see cref="RoomIdentifier.OnDestroy"/>.
/// </summary>
Expand Down