mirror of
https://github.com/soarqin/DSP_Mods.git
synced 2025-12-09 04:13:32 +08:00
Dustbin: Save dustbin state using DSPModSave
This commit is contained in:
@@ -1,47 +1,62 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections;
|
||||||
|
using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Reflection.Emit;
|
|
||||||
using BepInEx;
|
using BepInEx;
|
||||||
|
using crecheng.DSPModSave;
|
||||||
using HarmonyLib;
|
using HarmonyLib;
|
||||||
using UnityEngine;
|
|
||||||
|
|
||||||
namespace Dustbin;
|
namespace Dustbin;
|
||||||
|
|
||||||
class IsDusbinIndexer
|
public class IsDusbinIndexer
|
||||||
{
|
{
|
||||||
private bool[] store = new bool[256];
|
private bool[] _store = new bool[256];
|
||||||
|
|
||||||
public bool this[int index]
|
public bool this[int index]
|
||||||
{
|
{
|
||||||
get
|
get
|
||||||
{
|
{
|
||||||
if (index < 0 || index >= store.Length) return false;
|
if (index < 0 || index >= _store.Length) return false;
|
||||||
return store[index];
|
return _store[index];
|
||||||
}
|
}
|
||||||
set
|
set
|
||||||
{
|
{
|
||||||
if (index >= store.Length)
|
if (index >= _store.Length)
|
||||||
{
|
{
|
||||||
var oldLen = store.Length;
|
var oldLen = _store.Length;
|
||||||
var newLen = oldLen * 2;
|
var newLen = oldLen * 2;
|
||||||
var oldArr = store;
|
var oldArr = _store;
|
||||||
store = new bool[newLen];
|
_store = new bool[newLen];
|
||||||
Array.Copy(oldArr, store, oldLen);
|
Array.Copy(oldArr, _store, oldLen);
|
||||||
}
|
}
|
||||||
store[index] = value;
|
_store[index] = value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Reset()
|
public void Reset()
|
||||||
{
|
{
|
||||||
store = new bool[256];
|
_store = new bool[256];
|
||||||
|
}
|
||||||
|
|
||||||
|
public delegate void ForEachFunc(int id);
|
||||||
|
public void ForEachIsDustbin(ForEachFunc func)
|
||||||
|
{
|
||||||
|
var len = _store.Length;
|
||||||
|
for (var i = 0; i < len; i++)
|
||||||
|
{
|
||||||
|
if (_store[i])
|
||||||
|
{
|
||||||
|
func(i);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
[BepInPlugin(PluginInfo.PLUGIN_GUID, PluginInfo.PLUGIN_NAME, PluginInfo.PLUGIN_VERSION)]
|
[BepInPlugin(PluginInfo.PLUGIN_GUID, PluginInfo.PLUGIN_NAME, PluginInfo.PLUGIN_VERSION)]
|
||||||
public class Dustbin : BaseUnityPlugin
|
[BepInDependency(DSPModSavePlugin.MODGUID)]
|
||||||
|
public class Dustbin : BaseUnityPlugin, IModCanSave
|
||||||
{
|
{
|
||||||
|
private const ushort ModSaveVersion = 1;
|
||||||
private new static readonly BepInEx.Logging.ManualLogSource Logger =
|
private new static readonly BepInEx.Logging.ManualLogSource Logger =
|
||||||
BepInEx.Logging.Logger.CreateLogSource(PluginInfo.PLUGIN_NAME);
|
BepInEx.Logging.Logger.CreateLogSource(PluginInfo.PLUGIN_NAME);
|
||||||
|
|
||||||
@@ -79,4 +94,25 @@ public class Dustbin : BaseUnityPlugin
|
|||||||
foreach (var id in ItemProto.fluids)
|
foreach (var id in ItemProto.fluids)
|
||||||
IsFluid[id] = true;
|
IsFluid[id] = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void Export(BinaryWriter w)
|
||||||
|
{
|
||||||
|
w.Write(ModSaveVersion);
|
||||||
|
StoragePatch.Export(w);
|
||||||
|
TankPatch.Export(w);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Import(BinaryReader r)
|
||||||
|
{
|
||||||
|
var version = r.ReadUInt16();
|
||||||
|
if (version > 0)
|
||||||
|
{
|
||||||
|
StoragePatch.Import(r);
|
||||||
|
TankPatch.Import(r);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void IntoOtherSave()
|
||||||
|
{
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,11 +15,12 @@
|
|||||||
<PackageReference Include="BepInEx.Core" Version="5.*" />
|
<PackageReference Include="BepInEx.Core" Version="5.*" />
|
||||||
<PackageReference Include="BepInEx.PluginInfoProps" Version="1.*" />
|
<PackageReference Include="BepInEx.PluginInfoProps" Version="1.*" />
|
||||||
<PackageReference Include="DysonSphereProgram.GameLibs" Version="*-r.*" />
|
<PackageReference Include="DysonSphereProgram.GameLibs" Version="*-r.*" />
|
||||||
|
<PackageReference Include="DysonSphereProgram.Modding.DSPModSave" Version="1.*" />
|
||||||
<PackageReference Include="UnityEngine.Modules" Version="2018.4.12" IncludeAssets="compile" />
|
<PackageReference Include="UnityEngine.Modules" Version="2018.4.12" IncludeAssets="compile" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup Condition="'$(TargetFramework.TrimEnd(`0123456789`))' == 'net'">
|
<ItemGroup Condition="'$(TargetFramework.TrimEnd(`0123456789`))' == 'net'">
|
||||||
<PackageReference Include="Microsoft.NETFramework.ReferenceAssemblies" Version="1.0.2" PrivateAssets="all" />
|
<PackageReference Include="Microsoft.NETFramework.ReferenceAssemblies" Version="1.0.3" PrivateAssets="all" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<Target Name="PostBuild" AfterTargets="PostBuildEvent">
|
<Target Name="PostBuild" AfterTargets="PostBuildEvent">
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
using HarmonyLib;
|
using System.IO;
|
||||||
|
using HarmonyLib;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
|
|
||||||
namespace Dustbin;
|
namespace Dustbin;
|
||||||
@@ -16,6 +17,32 @@ public static class StoragePatch
|
|||||||
lastStorageId = 0;
|
lastStorageId = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void Export(BinaryWriter w)
|
||||||
|
{
|
||||||
|
var tempStream = new MemoryStream();
|
||||||
|
var tempWriter = new BinaryWriter(tempStream);
|
||||||
|
int count = 0;
|
||||||
|
storageIsDustbin.ForEachIsDustbin(i =>
|
||||||
|
{
|
||||||
|
tempWriter.Write(i);
|
||||||
|
count++;
|
||||||
|
});
|
||||||
|
w.Write(count);
|
||||||
|
tempStream.Position = 0;
|
||||||
|
/* FixMe: May BinaryWriter not sync with its BaseStream while subclass overrides Write()? */
|
||||||
|
tempStream.CopyTo(w.BaseStream);
|
||||||
|
tempWriter.Dispose();
|
||||||
|
tempStream.Dispose();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void Import(BinaryReader r)
|
||||||
|
{
|
||||||
|
for (var count = r.ReadInt32(); count > 0; count--)
|
||||||
|
{
|
||||||
|
storageIsDustbin[r.ReadInt32()] = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
[HarmonyPostfix]
|
[HarmonyPostfix]
|
||||||
[HarmonyPatch(typeof(UIStorageWindow), "_OnCreate")]
|
[HarmonyPatch(typeof(UIStorageWindow), "_OnCreate")]
|
||||||
private static void UIStorageWindow__OnCreate_Postfix(UIStorageWindow __instance)
|
private static void UIStorageWindow__OnCreate_Postfix(UIStorageWindow __instance)
|
||||||
@@ -81,38 +108,24 @@ public static class StoragePatch
|
|||||||
}
|
}
|
||||||
|
|
||||||
[HarmonyPrefix]
|
[HarmonyPrefix]
|
||||||
[HarmonyPatch(typeof(StorageComponent), "Export")]
|
[HarmonyPatch(typeof(FactoryStorage), "RemoveStorageComponent")]
|
||||||
private static void StorageComponent_Export_Prefix(StorageComponent __instance, out int __state)
|
private static void FactoryStorage_RemoveStorageComponent_Prefix(FactoryStorage __instance, int id)
|
||||||
{
|
{
|
||||||
if (storageIsDustbin[__instance.id])
|
var storage = __instance.storagePool[id];
|
||||||
|
if (storage != null && storage.id != 0)
|
||||||
{
|
{
|
||||||
__state = __instance.bans;
|
storageIsDustbin[id] = false;
|
||||||
__instance.bans = -__instance.bans - 1;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
__state = -1;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
[HarmonyPostfix]
|
/* We keep this to make MOD compatible with older version */
|
||||||
[HarmonyPatch(typeof(StorageComponent), "Export")]
|
|
||||||
private static void StorageComponent_Export_Postfix(StorageComponent __instance, int __state)
|
|
||||||
{
|
|
||||||
if (__state < 0) return;
|
|
||||||
__instance.bans = __state;
|
|
||||||
}
|
|
||||||
|
|
||||||
[HarmonyPostfix]
|
[HarmonyPostfix]
|
||||||
[HarmonyPatch(typeof(StorageComponent), "Import")]
|
[HarmonyPatch(typeof(StorageComponent), "Import")]
|
||||||
private static void StorageComponent_Import_Postfix(StorageComponent __instance)
|
private static void StorageComponent_Import_Postfix(StorageComponent __instance)
|
||||||
{
|
{
|
||||||
if ((__instance.bans & 0x8000) == 0)
|
if (__instance.bans >= 0)
|
||||||
storageIsDustbin[__instance.id] = false;
|
return;
|
||||||
else
|
storageIsDustbin[__instance.id] = true;
|
||||||
{
|
__instance.bans = -__instance.bans - 1;
|
||||||
storageIsDustbin[__instance.id] = true;
|
|
||||||
__instance.bans ^= 0x8000;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
using HarmonyLib;
|
using System.IO;
|
||||||
using UnityEngine;
|
using HarmonyLib;
|
||||||
|
|
||||||
namespace Dustbin;
|
namespace Dustbin;
|
||||||
|
|
||||||
@@ -16,6 +16,32 @@ public static class TankPatch
|
|||||||
lastTankId = 0;
|
lastTankId = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void Export(BinaryWriter w)
|
||||||
|
{
|
||||||
|
var tempStream = new MemoryStream();
|
||||||
|
var tempWriter = new BinaryWriter(tempStream);
|
||||||
|
int count = 0;
|
||||||
|
tankIsDustbin.ForEachIsDustbin(i =>
|
||||||
|
{
|
||||||
|
tempWriter.Write(i);
|
||||||
|
count++;
|
||||||
|
});
|
||||||
|
w.Write(count);
|
||||||
|
tempStream.Position = 0;
|
||||||
|
/* FixMe: May BinaryWriter not sync with its BaseStream while subclass overrides Write()? */
|
||||||
|
tempStream.CopyTo(w.BaseStream);
|
||||||
|
tempWriter.Dispose();
|
||||||
|
tempStream.Dispose();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void Import(BinaryReader r)
|
||||||
|
{
|
||||||
|
for (var count = r.ReadInt32(); count > 0; count--)
|
||||||
|
{
|
||||||
|
tankIsDustbin[r.ReadInt32()] = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
[HarmonyPostfix]
|
[HarmonyPostfix]
|
||||||
[HarmonyPatch(typeof(UITankWindow), "_OnCreate")]
|
[HarmonyPatch(typeof(UITankWindow), "_OnCreate")]
|
||||||
private static void UITankWindow__OnCreate_Postfix(UITankWindow __instance)
|
private static void UITankWindow__OnCreate_Postfix(UITankWindow __instance)
|
||||||
@@ -69,38 +95,22 @@ public static class TankPatch
|
|||||||
}
|
}
|
||||||
|
|
||||||
[HarmonyPrefix]
|
[HarmonyPrefix]
|
||||||
[HarmonyPatch(typeof(TankComponent), "Export")]
|
[HarmonyPatch(typeof(FactoryStorage), "RemoveTankComponent")]
|
||||||
private static void TankComponent_Export_Prefix(ref TankComponent __instance, out int __state)
|
private static void FactoryStorage_RemoveTankComponent_Prefix(FactoryStorage __instance, int id)
|
||||||
{
|
{
|
||||||
if (tankIsDustbin[__instance.id])
|
if (__instance.tankPool[id].id != 0)
|
||||||
{
|
{
|
||||||
__state = __instance.fluidInc;
|
tankIsDustbin[id] = false;
|
||||||
__instance.fluidInc = -__instance.fluidInc - 1;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
__state = -1;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
[HarmonyPostfix]
|
/* We keep this to make MOD compatible with older version */
|
||||||
[HarmonyPatch(typeof(TankComponent), "Export")]
|
|
||||||
private static void TankComponent_Export_Postfix(ref TankComponent __instance, int __state)
|
|
||||||
{
|
|
||||||
if (__state < 0) return;
|
|
||||||
__instance.fluidInc = __state;
|
|
||||||
}
|
|
||||||
|
|
||||||
[HarmonyPostfix]
|
[HarmonyPostfix]
|
||||||
[HarmonyPatch(typeof(TankComponent), "Import")]
|
[HarmonyPatch(typeof(TankComponent), "Import")]
|
||||||
private static void TankComponent_Import_Postfix(TankComponent __instance)
|
private static void TankComponent_Import_Postfix(TankComponent __instance)
|
||||||
{
|
{
|
||||||
if (__instance.fluidInc >= 0)
|
if (__instance.fluidInc >= 0) return;
|
||||||
tankIsDustbin[__instance.id] = false;
|
tankIsDustbin[__instance.id] = true;
|
||||||
else
|
__instance.fluidInc = -__instance.fluidInc - 1;
|
||||||
{
|
|
||||||
tankIsDustbin[__instance.id] = true;
|
|
||||||
__instance.fluidInc = -__instance.fluidInc - 1;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,6 +4,7 @@
|
|||||||
"website_url": "https://github.com/soarqin/DSP_Mods/tree/master/Dustbin",
|
"website_url": "https://github.com/soarqin/DSP_Mods/tree/master/Dustbin",
|
||||||
"description": "Can turn Storages and Tanks into Dustbin(Destroy incoming items) / 储物仓和储液罐可以转变为垃圾桶(销毁送进的物品)",
|
"description": "Can turn Storages and Tanks into Dustbin(Destroy incoming items) / 储物仓和储液罐可以转变为垃圾桶(销毁送进的物品)",
|
||||||
"dependencies": [
|
"dependencies": [
|
||||||
"xiaoye97-BepInEx-5.4.17"
|
"xiaoye97-BepInEx-5.4.17",
|
||||||
|
"CommonAPI-DSPModSave-1.1.4"
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user