From 97af764e926fcee99769db862fab58f1b3f4997d Mon Sep 17 00:00:00 2001 From: Soar Qin Date: Wed, 7 Dec 2022 17:04:53 +0800 Subject: [PATCH] Dustbin v1.2.1, fixes #2 --- Dustbin/Dustbin.csproj | 2 +- Dustbin/README.md | 3 +++ Dustbin/TankPatch.cs | 35 ++++++++++++++++++++++++++--------- Dustbin/package/manifest.json | 2 +- 4 files changed, 31 insertions(+), 11 deletions(-) diff --git a/Dustbin/Dustbin.csproj b/Dustbin/Dustbin.csproj index 3dad88c..f0e5ac2 100644 --- a/Dustbin/Dustbin.csproj +++ b/Dustbin/Dustbin.csproj @@ -4,7 +4,7 @@ net472 org.soardev.dustbin DSP MOD - Dustbin - 1.2.0 + 1.2.1 true latest Dustbin diff --git a/Dustbin/README.md b/Dustbin/README.md index c5bfe75..0ddf012 100644 --- a/Dustbin/README.md +++ b/Dustbin/README.md @@ -5,6 +5,9 @@ ## Updates +* 1.2.1 + * Fix dynamic array bug in codes, which causes various bugs and errors. + * 1.2.0 * Use [DSPModSave](https://dsp.thunderstore.io/package/CommonAPI/DSPModSave/) to save dustbin specified data now, which fixes [#1](https://github.com/soarqin/DSP_Mods/issues/1). * Fix issue for storages on multiple planets. diff --git a/Dustbin/TankPatch.cs b/Dustbin/TankPatch.cs index c521929..e315ec4 100644 --- a/Dustbin/TankPatch.cs +++ b/Dustbin/TankPatch.cs @@ -1,6 +1,7 @@ using System; using System.IO; using HarmonyLib; +using HarmonyLib.Tools; namespace Dustbin; @@ -8,7 +9,7 @@ using IsDustbinIndexer = DynamicObjectArray where T: struct { - private T[] _store = new T[16]; + private T[] _store = new T[64]; public T this[int index] { @@ -21,7 +22,12 @@ public class DynamicValueArray where T: struct { if (index >= _store.Length) { - Array.Resize(ref _store, _store.Length * 2); + var count = index | (index >> 1); + count |= count >> 2; + count |= count >> 4; + count |= count >> 8; + count |= count >> 16; + Array.Resize(ref _store, count + 1); } _store[index] = value; } @@ -51,13 +57,21 @@ public class DynamicObjectArray where T: class, new() { get { - if (index < 0 || index >= _store.Length) return null; - var result = _store[index]; - if (result == null) + if (index < 0) return null; + if (index >= _store.Length) { - result = new T(); - _store[index] = result; + var count = index | (index >> 1); + count |= count >> 2; + count |= count >> 4; + count |= count >> 8; + count |= count >> 16; + Array.Resize(ref _store, count + 1); } + T result = _store[index]; + if (result != null) + return result; + result = new T(); + _store[index] = result; return result; } } @@ -129,10 +143,13 @@ public static class TankPatch { r.ReadByte(); var planetId = r.ReadInt32(); - var data = tankIsDustbin[planetId / 100][planetId % 100]; + var data = tankIsDustbin[15]; + data[0][0] = true; + data = tankIsDustbin[20]; + data[0][0] = true; for (var count = r.ReadInt32(); count > 0; count--) { - data[r.ReadInt32()] = true; + tankIsDustbin[planetId / 100][planetId % 100][r.ReadInt32()] = true; } } } diff --git a/Dustbin/package/manifest.json b/Dustbin/package/manifest.json index fbc9f5c..64177a5 100644 --- a/Dustbin/package/manifest.json +++ b/Dustbin/package/manifest.json @@ -1,6 +1,6 @@ { "name": "Dustbin", - "version_number": "1.2.0", + "version_number": "1.2.1", "website_url": "https://github.com/soarqin/DSP_Mods/tree/master/Dustbin", "description": "Can turn Storages and Tanks into Dustbin(Destroy incoming items) / 储物仓和储液罐可以转变为垃圾桶(销毁送进的物品)", "dependencies": [