42 lines
1.6 KiB
C#
42 lines
1.6 KiB
C#
using System.Runtime.CompilerServices;
|
|
using BD2.GameNames;
|
|
using BDNetwork;
|
|
using Google.Protobuf;
|
|
using HarmonyLib;
|
|
using Proto.Design.common;
|
|
using Proto.Net;
|
|
|
|
namespace Bd2BetterExperience.Life;
|
|
|
|
internal sealed class LifeMinimumBuildings : IModule
|
|
{
|
|
private const int RemoveMinimumField = 100;
|
|
private static readonly ConditionalWeakTable<LifeWorldObjectUnplaceSaveRequest, object> marked = [];
|
|
|
|
public string Id => "LifeMinimumBuildings";
|
|
public string Description => "Allow farm buildings to be removed below their minimum count. Requires server support.";
|
|
|
|
public void Install(Harmony harmony)
|
|
{
|
|
harmony.Patch(Game.Getter<LifeObjectGroupTable, int>(table => table.MinCount),
|
|
postfix: new HarmonyMethod(typeof(LifeMinimumBuildings), nameof(Minimum)));
|
|
harmony.Patch(Game.Method<NetworkManager>(network => network.Send(null, null, true, null, false, null)),
|
|
prefix: new HarmonyMethod(typeof(LifeMinimumBuildings), nameof(BeforeSend)));
|
|
}
|
|
|
|
private static void Minimum(ref int __result) => __result = 0;
|
|
|
|
private static void BeforeSend(IMessage __0)
|
|
{
|
|
if (__0 is not LifeWorldObjectUnplaceSaveRequest request || marked.TryGetValue(request, out _)) return;
|
|
byte[] envelope = new byte[CodedOutputStream.ComputeTagSize(RemoveMinimumField) + 1];
|
|
using (var output = new CodedOutputStream(envelope))
|
|
{
|
|
output.WriteTag(RemoveMinimumField, WireFormat.WireType.Varint);
|
|
output.WriteBool(true);
|
|
}
|
|
request.MergeFrom(envelope);
|
|
marked.Add(request, new object());
|
|
}
|
|
}
|