[ModAPI] SerializeToBinary() doesn't work for grid OB

Digi shared this bug 13 months ago
Considered

Objectbuilder for grids can't be reliably serialized to binary because many block types aren't in the protobuf thing that the modAPI variant uses.

The game's protobuf has them (they were added after this API method existed), therefore SerializeTo/FromBinary() needs to be updated to support that, or at the very least add a different one.


Code for replicating:

using Sandbox.Common.ObjectBuilders;
using Sandbox.ModAPI;
using VRage.Game;
using VRage.Game.Components;
using VRage.ObjectBuilders;
using VRage.Utils;

[MySessionComponentDescriptor(MyUpdateOrder.NoUpdate)]
public class Bugreport_ToBinaryGrid : MySessionComponentBase
{
    public override void BeforeStart()
    {
        MyLog.Default.WriteLine("###TEST: Creating grid OB and serializing...");
        MyLog.Default.Flush();

        var gridObj = MyObjectBuilderSerializer.CreateNewObject<MyObjectBuilder_CubeGrid>();

        // this is causing the issue, and many other block types
        gridObj.CubeBlocks.Add(MyObjectBuilderSerializer.CreateNewObject<MyObjectBuilder_Reactor>("LargeBlockSmallGenerator"));

        var bytes = MyAPIGateway.Utilities.SerializeToBinary(gridObj);

        MyLog.Default.WriteLine($"###TEST: Done! bytes: {bytes.Length}");
        MyLog.Default.Flush();
    }
}
Exception:
2018-08-02 06:54:42.652 - Thread:   1 ->  ###TEST: Creating grid OB and serializing...
2018-08-02 06:54:42.652 - Thread:   1 ->  ERROR: Loading screen failed
2018-08-02 06:54:42.652 - Thread:   1 ->  Exception occured: System.InvalidOperationException: Unexpected sub-type: Sandbox.Common.ObjectBuilders.MyObjectBuilder_Reactor
   at ProtoBuf.Meta.TypeModel.ThrowUnexpectedSubtype(Type expected, Type actual)
   at proto_1345(Object , ProtoWriter )
   at ProtoBuf.ProtoWriter.WriteObject(Object value, Int32 key, ProtoWriter writer)
   at proto_1343(Object , ProtoWriter )
   at ProtoBuf.Meta.TypeModel.SerializeCore(ProtoWriter writer, Object value)
   at ProtoBuf.Meta.TypeModel.Serialize(Stream dest, Object value, SerializationContext context)
   at ProtoBuf.Serializer.Serialize[T](Stream destination, T instance)
   at Sandbox.ModAPI.MyAPIUtilities.VRage.Game.ModAPI.IMyUtilities.SerializeToBinary[T](T obj)
   at Bugreport_ToBinaryGrid.BeforeStart()

Comments (1)

photo
1

Hello, Engineer!


Thank you for your feedback! Your topic has been added between considered issues.

Please keep voting for the issue as it will help us to identify the most serious bugs.


We really appreciate your patience.


Kind Regards

Keen Software House: QA Department