SmartlyDressedGames / Unturned-3.x-Community

Community portion of the Unturned-3.x repo. If you have access to the source code you can find it here:
https://github.com/SmartlyDressedGames/Unturned-3.x/
84 stars 18 forks source link

Warn for more than 8 rubble sections #4322

Closed Axodouble closed 4 months ago

Axodouble commented 5 months ago

After some frustrating headaches assuming I had made a mistake somewhere, I happened to find that that was indeed not the case.

If an object is given more than 8 sections or above, the game client will freeze, this error only occurs when more than 8 sections are added.

Below: image Works fine without any crashing, freezing or similar.

However, an exact same copy of the above prefab which works fine on its own: image Does not work.

Upon loading this the following error appears in the client.log file.

[2024-01-22 14:09:35] IndexOutOfRangeException: Index was outside the bounds of the array.
[2024-01-22 14:09:35] SDG.Unturned.InteractableObjectRubble.updateState (SDG.Unturned.Asset asset, System.Byte[] state) (at <9c401a3808344e96b2b815240a0d52d3>:0)
SDG.Unturned.LevelObject..ctor (UnityEngine.Vector3 newPoint, UnityEngine.Quaternion newRotation, UnityEngine.Vector3 newScale, System.UInt16 newID, System.Guid newGUID, SDG.Unturned.ELevelObjectPlacementOrigin newPlacementOrigin, System.UInt32 newInstanceID, SDG.Unturned.AssetReference`1[T] customMaterialOverride, System.Int32 materialIndexOverride, SDG.Unturned.NetId netId) (at <9c401a3808344e96b2b815240a0d52d3>:0)
SDG.Unturned.LevelObjects.load () (at <9c401a3808344e96b2b815240a0d52d3>:0)
SDG.Unturned.Level+<init>d__148.MoveNext () (at <9c401a3808344e96b2b815240a0d52d3>:0)
UnityEngine.SetupCoroutine.InvokeMoveNext (System.Collections.IEnumerator enumerator, System.IntPtr returnValueAddress) (at <fcaf289839f24125a059795f150fce14>:0)

A fix would be lovely, thanks in advance.

GazziFX commented 5 months ago

More than 8 sections isn't supported because their state stored in 1 byte (8 bits) and accessing to Types.SHIFTS[8] cause IndexOutOfRangeException

SDGNelson commented 5 months ago

Sorry about this. As GazziFX mentions it is a limit currently, but the game should at least warn about this rather than breaking. I'll see about adding a warning instead.

DanielWillett commented 5 months ago

(based off your response in #3159) Wouldn't it be possible to allow more segments without messing up states as long as you only add more bytes to the end? Since all states are read as byte arrays and not individual components you should just be able to check the length.

SDGNelson commented 5 months ago

Hmmm I suppose so. I'll take a look at whether it would be a bigger(/more likely to break) change when I see about adding the warning.

SDGNelson commented 5 months ago

Added the warning for the next update.

@DanielWillett sorry to disappoint, but while your proposal is quite reasonable, I am quite hesitant to make that change with the existing object state storage. It would be the change in InteractableObjectRubble.updateState and some changes in ObjectManager where it assumes it can just shift the last byte by the section index, but most importantly not only would it need vanilla save and state compatibility but any plugins using that code may easily run into out-of-bounds exceptions. It's a similarly awful situation to item state where any changes are very fragile. Ideally perhaps we can share some sort of "persistent state" and "networked state" code between items, objects, barricades, structures, etc., in the future to eliminate this problem.