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
24 changes: 4 additions & 20 deletions EXILED/Exiled.API/Features/Items/MicroHid.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,6 @@ public MicroHid(MicroHIDItem itemBase)
: base(itemBase)
{
Base = itemBase;

EnergyManager = Base.EnergyManager;
BrokenModule = Base.BrokenSync;
InputModule = Base.InputSync;
CycleController = Base.CycleController;
}

/// <summary>
Expand All @@ -48,22 +43,22 @@ internal MicroHid()
/// <summary>
/// Gets the <see cref="EnergyManagerModule"/> of the MicroHID.
/// </summary>
public EnergyManagerModule EnergyManager { get; }
public EnergyManagerModule EnergyManager => Base.EnergyManager;

/// <summary>
/// Gets the <see cref="BrokenSyncModule"/> of the MicroHID.
/// </summary>
public BrokenSyncModule BrokenModule { get; }
public BrokenSyncModule BrokenModule => Base.BrokenSync;

/// <summary>
/// Gets the <see cref="InputSyncModule"/> of the MicroHID.
/// </summary>
public InputSyncModule InputModule { get; }
public InputSyncModule InputModule => Base.InputSync;

/// <summary>
/// Gets the <see cref="CycleController"/> of the MicroHID.
/// </summary>
public CycleController CycleController { get; }
public CycleController CycleController => Base.CycleController;

/// <summary>
/// Gets or sets the remaining energy in the MicroHID.
Expand Down Expand Up @@ -219,16 +214,5 @@ 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 ReadPickupInfo(Pickup pickup)
{
base.ReadPickupInfo(pickup);

if (pickup.Is(out Pickups.MicroHIDPickup microHidPickup))
{
Energy = microHidPickup.Energy;
}
}
}
}
17 changes: 5 additions & 12 deletions EXILED/Exiled.API/Features/Pickups/MicroHIDPickup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,11 @@ internal MicroHIDPickup()
/// <summary>
/// Gets or sets the MicroHID Energy Level.
/// </summary>
public float Energy { get; set; }
public float Energy
{
get => EnergyManagerModule.GetEnergy(Serial);
set => EnergyManagerModule.SyncEnergy[Serial] = value;
}

/// <summary>
/// Gets or sets the <see cref="MicroHidPhase"/>.
Expand Down Expand Up @@ -144,16 +148,5 @@ public bool TryGetFireController<T>(MicroHidFiringMode firingMode, out T module)
/// </summary>
/// <returns>A string containing MicroHIDPickup related data.</returns>
public override string ToString() => $"{Type} ({Serial}) [{Weight}] *{Scale}* |{Energy}|";

/// <inheritdoc/>
internal override void ReadItemInfo(Item item)
{
base.ReadItemInfo(item);

if (item.Is(out MicroHid microHid))
{
Energy = microHid.Energy;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,16 @@ public class UsingMicroHIDEnergyEventArgs : IDeniableEvent, IItemEvent
/// <param name="microHIDitem">
/// <inheritdoc cref="MicroHID" />
/// </param>
/// <param name="drain">
/// <param name="newEnergy">
/// <inheritdoc cref="Drain" />
/// </param>
/// <param name="isAllowed">
/// <inheritdoc cref="IsAllowed" />
/// </param>
public UsingMicroHIDEnergyEventArgs(MicroHIDItem microHIDitem, float drain, bool isAllowed = true)
public UsingMicroHIDEnergyEventArgs(MicroHIDItem microHIDitem, float newEnergy, bool isAllowed = true)
{
MicroHID = Item.Get<MicroHid>(microHIDitem);
Drain = MicroHID.Energy - drain;
Drain = MicroHID.Energy - newEnergy;
IsAllowed = isAllowed;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstructi
new(OpCodes.Brfalse_S, returnLabel),

// value = ev.NewPhase;
new(OpCodes.Ldarg_S, ev.LocalIndex),
new(OpCodes.Ldloc_S, ev.LocalIndex),
new(OpCodes.Callvirt, PropertyGetter(typeof(ChangingMicroHIDStateEventArgs), nameof(ChangingMicroHIDStateEventArgs.NewPhase))),
new(OpCodes.Starg_S, 1),
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstructi
{
List<CodeInstruction> newInstructions = ListPool<CodeInstruction>.Pool.Get(instructions);

int index = newInstructions.FindLastIndex(instruction => instruction.opcode == OpCodes.Ldloc_0);
int index = newInstructions.FindLastIndex(instruction => instruction.opcode == OpCodes.Ldloc_1);

newInstructions.InsertRange(
index,
Expand Down Expand Up @@ -64,7 +64,7 @@ private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstructi
new(OpCodes.Callvirt, PropertyGetter(typeof(InteractingLockerEventArgs), nameof(InteractingLockerEventArgs.IsAllowed))),
new(OpCodes.Ldc_I4_0),
new(OpCodes.Ceq),
new(OpCodes.Stloc_0),
new(OpCodes.Stloc_1),
});

for (int z = 0; z < newInstructions.Count; z++)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,22 +37,23 @@ private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstructi

LocalBuilder ev = generator.DeclareLocal(typeof(UsingMicroHIDEnergyEventArgs));

int offset = -1;
int index = newInstructions.FindLastIndex(instruction => instruction.opcode == OpCodes.Ldarg_0) + offset;
int offset = -4;
int index = newInstructions.FindLastIndex(i => i.Calls(Method(typeof(EnergyManagerModule), nameof(EnergyManagerModule.ServerSetEnergy)))) + offset;

newInstructions.InsertRange(index, new CodeInstruction[]
{
// this.MicroHID
new(OpCodes.Ldarg_0),
new CodeInstruction(OpCodes.Ldarg_0).MoveLabelsFrom(newInstructions[index]),
new(OpCodes.Callvirt, PropertyGetter(typeof(EnergyManagerModule), nameof(EnergyManagerModule.MicroHid))),

// energy
// newEnergy
new(OpCodes.Ldloc_0),

// true
new(OpCodes.Ldc_I4_1),

// UsingMicroHIDEnergyEventArgs ev = new(this.MicroHID, energy, true);
// calculate drain by delta between old energy and new energy
// UsingMicroHIDEnergyEventArgs ev = new(this.MicroHID, newEnergy, true);
new(OpCodes.Newobj, GetDeclaredConstructors(typeof(UsingMicroHIDEnergyEventArgs))[0]),
new(OpCodes.Dup),
new(OpCodes.Dup),
Expand All @@ -66,7 +67,7 @@ private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstructi
new(OpCodes.Callvirt, PropertyGetter(typeof(UsingMicroHIDEnergyEventArgs), nameof(UsingMicroHIDEnergyEventArgs.IsAllowed))),
new(OpCodes.Brfalse_S, returnLabel),

// energy = this.Energy - ev.Drain;
// newEnergy = this.Energy - ev.Drain;
new(OpCodes.Ldarg_0),
new(OpCodes.Callvirt, PropertyGetter(typeof(EnergyManagerModule), nameof(EnergyManagerModule.Energy))),
new(OpCodes.Ldloc_S, ev.LocalIndex),
Expand All @@ -83,4 +84,4 @@ private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstructi
ListPool<CodeInstruction>.Pool.Return(newInstructions);
}
}
}
}