1
0
mirror of https://github.com/soarqin/DSP_Mods.git synced 2025-12-09 09:33:36 +08:00

Dustbin 1.3.0 WIP

This commit is contained in:
2023-08-15 21:19:22 +08:00
parent 7dcd7b56a3
commit 078af5bc59
13 changed files with 501 additions and 270 deletions

View File

@@ -1,23 +1,15 @@
using System.Collections.Generic;
using System.IO;
using System.Reflection.Emit;
using System.IO;
using System.Reflection;
using HarmonyLib;
using NebulaAPI;
using UnityEngine;
namespace Dustbin;
public class StorageComponentWithDustbin : StorageComponent
{
public bool IsDusbin;
public StorageComponentWithDustbin(int size): base(size)
{
IsDusbin = false;
}
}
[HarmonyPatch]
public static class StoragePatch
{
public static readonly FieldInfo IsDustbinField = AccessTools.Field(typeof(StorageComponent), "IsDustbin");
private static MyCheckBox _storageDustbinCheckBox;
private static int _lastStorageId;
@@ -43,7 +35,7 @@ public static class StoragePatch
for (var j = 1; j < cursor; j++)
{
if (storagePool[j] == null || storagePool[j].id != j) continue;
if (storagePool[j] is not StorageComponentWithDustbin { IsDusbin: true }) continue;
if (!(bool)IsDustbinField.GetValue(storagePool[j])) continue;
tempWriter.Write(j);
count++;
}
@@ -81,10 +73,9 @@ public static class StoragePatch
for (var count = r.ReadInt32(); count > 0; count--)
{
var id = r.ReadInt32();
if (id > 0 && id < storagePool.Length && storagePool[id] != null && storagePool[id].id == id &&
storagePool[id] is StorageComponentWithDustbin comp)
if (id > 0 && id < storagePool.Length && storagePool[id] != null && storagePool[id].id == id)
{
comp.IsDusbin = true;
IsDustbinField.SetValue(storagePool[id], true);
}
}
}
@@ -102,8 +93,11 @@ public static class StoragePatch
if (storageId <= 0) return;
var storagePool = window.factoryStorage.storagePool;
if (storagePool[storageId].id != storageId) return;
if (storagePool[storageId] is not StorageComponentWithDustbin comp) return;
comp.IsDusbin = _storageDustbinCheckBox.Checked;
var enabled = _storageDustbinCheckBox.Checked;
IsDustbinField.SetValue(storagePool[storageId], enabled);
if (!NebulaModAPI.IsMultiplayerActive) return;
var planetId = window.factory.planetId;
NebulaModAPI.MultiplayerSession.Network.SendPacketToLocalStar(new NebulaSupport.Packet.ToggleEvent(planetId, storageId, enabled));
};
}
@@ -117,8 +111,7 @@ public static class StoragePatch
if (storageId <= 0) return;
var storagePool = __instance.factoryStorage.storagePool;
if (storagePool[storageId].id != storageId) return;
if (storagePool[storageId] is not StorageComponentWithDustbin comp) return;
_storageDustbinCheckBox.Checked = comp.IsDusbin;
_storageDustbinCheckBox.Checked = (bool)IsDustbinField.GetValue(storagePool[storageId]);
if (__instance.transform is RectTransform rectTrans)
{
_storageDustbinCheckBox.rectTrans.anchoredPosition3D = new Vector3(50, 58 - rectTrans.sizeDelta.y, 0);
@@ -144,7 +137,7 @@ public static class StoragePatch
private static bool StorageComponent_AddItem_Prefix(ref int __result, StorageComponent __instance, int itemId, int count, int inc,
ref int remainInc, bool useBan = false)
{
if (__instance is not StorageComponentWithDustbin { IsDusbin: true }) return true;
if (!(bool)IsDustbinField.GetValue(__instance)) return true;
remainInc = inc;
__result = count;
var fluidArr = Dustbin.IsFluid;
@@ -162,9 +155,6 @@ public static class StoragePatch
var addCount = count * sandsPerItem;
player.sandCount += addCount;
GameMain.history.OnSandCountChange(player.sandCount, addCount);
/* Following line crashes game, seems that it should not be called in this working thread:
* UIRoot.instance.uiGame.OnSandCountChanged(player.sandCount, addCount);
*/
return false;
}
@@ -173,29 +163,9 @@ public static class StoragePatch
[HarmonyPatch(typeof(StorageComponent), "Import")]
private static void StorageComponent_Import_Postfix(StorageComponent __instance)
{
if (__instance.bans >= 0 || __instance is not StorageComponentWithDustbin comp)
if (__instance.bans >= 0)
return;
__instance.bans = -__instance.bans - 1;
comp.IsDusbin = true;
}
/* Replace: new StorageComponent(int) => new StorageComponentWithDustbin(int) */
[HarmonyTranspiler]
[HarmonyPatch(typeof(FactoryStorage), "Import")]
[HarmonyPatch(typeof(FactoryStorage), "NewStorageComponent")]
private static IEnumerable<CodeInstruction> FactoryStorage_NewStorageComponent_Transpiler(
IEnumerable<CodeInstruction> instructions)
{
foreach (var instr in instructions)
{
if (instr.opcode == OpCodes.Newobj && instr.OperandIs(AccessTools.Constructor(typeof(StorageComponent), new [] { typeof(int) })))
{
yield return new CodeInstruction(OpCodes.Newobj, AccessTools.Constructor(typeof(StorageComponentWithDustbin), new [] { typeof(int) }));
}
else
{
yield return instr;
}
}
IsDustbinField.SetValue(__instance, true);
}
}