Protobuf-net serializer used in Unity gives Invalid IL code IL_0035: call 0x06000001

2

Using Unity 5.0.1f1, protobuf-net r668.

I've set up a protobuf serializer dll using Visual Studio 2013, set to framework "Unity .Net 3.5 Subset Base Class Libraries" (I've also tried .Net 3.5 with same result). I have 4 parent data types to serialize: two rather simple ones, and two a tad more complex with some generics. The two simple ones serialize without issue, while the two others give me the following error:

Exception saving savegame: Invalid IL code in NVSavegameSerializer:Write (CityBuilder.CBMap/InventoryEntryArray,ProtoBuf.ProtoWriter): IL_0035: call      0x06000001
   at NVSavegameSerializer.Write (CityBuilder.Buildings , ProtoBuf.ProtoWriter ) [0x00000] in <filename unknown>:0 
   at NVSavegameSerializer.Write (CityBuilder.CBMap , ProtoBuf.ProtoWriter ) [0x00000] in <filename unknown>:0 
   at NVSavegameSerializer.Serialize (Int32 , System.Object , ProtoBuf.ProtoWriter ) [0x00000] in <filename unknown>:0 
   at ProtoBuf.Meta.TypeModel.SerializeCore (ProtoBuf.ProtoWriter writer, System.Object value) [0x00000] in <filename unknown>:0 
   at ProtoBuf.Meta.TypeModel.Serialize (System.IO.Stream dest, System.Object value, ProtoBuf.SerializationContext context) [0x00000] in <filename unknown>:0 
   at ProtoBuf.Meta.TypeModel.Serialize (System.IO.Stream dest, System.Object value) [0x00000] in <filename unknown>:0 
   at Nova.NVBSavegame.DoSave () [0x001ed] in C:\...\NVBSavegame.cs:118

I've tried using compiler options to target .Net1 instead of .Net2 by setting MetaDataVersion = 0x10000; but the result is the same.

Here's a snippet of the place mentioned in the exception (the InventoryEntryArray exists only because Unity can't serialize arrays of arrays):

[Serializable][ProtoContract(ImplicitFields = ImplicitFields.AllFields)]
public class CBMap
{
    ...
    [Serializable][ProtoContract(ImplicitFields = ImplicitFields.AllFields)]
    public struct InventoryEntryArray
    {
        public CBMap.InventoryEntry[] a;
        public CBMap.InventoryEntry this[int i] { get { return a[i]; } set { a[i] = value; } }
        public InventoryEntryArray(int count) { a = new CBMap.InventoryEntry[count]; }
    }
    ...
    [Serializable][ProtoContract(ImplicitFields = ImplicitFields.AllFields)]
    public struct InventoryEntry : CXPoolIndex
    {
        public int _index;
        public int Index { get { return _index; } set { _index = value; } }
        public static bool operator==(InventoryEntry a, InventoryEntry b) { return a._index == b._index; }
        public static bool operator!=(InventoryEntry a, InventoryEntry b) { return !(a == b); }
        public override bool Equals(System.Object o) { if (!(o is InventoryEntry)) return false; return this == (InventoryEntry)o; }
        public override int GetHashCode() { return _index; }

        public InventoryEntry(int ix) { _index = ix; }
        ... [some properties, a Reset() function]
    }
    ...
    [Serializable][ProtoContract(ImplicitFields = ImplicitFields.AllFields)]
    public class Buildings
    {
        public CXPool<Building> pool;
        ... [no constructor, other variables, CXPool is an interface]
        public float[] repairFactor;
        [CXSoAFieldConstructor(5)]
        public CBMap.InventoryEntryArray[] inventory;
        [^ here's the field mentioned in the exception]
    }
}

And here's how the dll is generated:

        var model = RuntimeTypeModel.Create();
        model.AutoCompile = false;
        model.AllowParseableTypes = true;
        model.AutoAddMissingTypes = true;

        model.Add(typeof(CXPoolIndex), true)
            .AddSubType(1000, typeof(CBMap.Building))
            .AddSubType(1001, typeof(CBMap.Captain))
            .AddSubType(1002, typeof(CBMap.InventoryEntry))
            .AddSubType(1003, typeof(CBMap.Tile))
        ;
        model.Add(typeof(CXPool<CBMap.Building>), true).AddSubType(1000, typeof(CXPoolSwap<CBMap.Building>));
        model.Add(typeof(CXPool<CBMap.Captain>), true).AddSubType(1001, typeof(CXPoolSwap<CBMap.Captain>));
        model.Add(typeof(CXPool<CBMap.InventoryEntry>), true).AddSubType(1002, typeof(CXPoolSwap<CBMap.InventoryEntry>));
        model.Add(typeof(CXPool<CBMap.Tile>), true).AddSubType(1003, typeof(CXPoolSwap<CBMap.Tile>));
        model.Add(typeof(CBMap), true);
        model.Add(typeof(CBSimulation), true);
        model.Add(typeof(NVSavegame.Data), true);
        model.Add(typeof(NVSavegame.BuildingSave), true);

        string typename = "NVSavegameSerializer";
        var opt = new RuntimeTypeModel.CompilerOptions();
        opt.TypeName = typename;
        opt.OutputPath = typename + ".dll";
        //opt.MetaDataVersion = 0x10000;
        model.Compile(opt);

Any idea of the issue ?

Thank you.

Edit:

Here's the result of peverify:

>peverify NVSavegameSerializer.dll

Microsoft (R) .NET Framework PE Verifier.  Version  4.0.30319.33440
Copyright (c) Microsoft Corporation.  All rights reserved.

[IL]: Error: [C:\data\projects\innervative\nova\unity\Assets\Plugins\NVSavegameS
erializer.dll : NVSavegameSerializer::Write][offset 0x000000B7][found ref 'CityB
uilder.CBMap+Tile'][expected value 'CityBuilder.CBMap+Tile'] Unexpected type on
the stack.
[IL]: Error: [C:\data\projects\innervative\nova\unity\Assets\Plugins\NVSavegameS
erializer.dll : NVSavegameSerializer::Write][offset 0x00000085][found ref 'CityB
uilder.CBMap+InventoryEntry'][expected value 'CityBuilder.CBMap+InventoryEntry']
 Unexpected type on the stack.
[IL]: Error: [C:\data\projects\innervative\nova\unity\Assets\Plugins\NVSavegameS
erializer.dll : NVSavegameSerializer::Write][offset 0x00000053][found ref 'CityB
uilder.CBMap+Captain'][expected value 'CityBuilder.CBMap+Captain'] Unexpected ty
pe on the stack.
[IL]: Error: [C:\data\projects\innervative\nova\unity\Assets\Plugins\NVSavegameS
erializer.dll : NVSavegameSerializer::Write][offset 0x00000021][found ref 'CityB
uilder.CBMap+Building'][expected value 'CityBuilder.CBMap+Building'] Unexpected
type on the stack.
[IL]: Error: [C:\data\projects\innervative\nova\unity\Assets\Plugins\NVSavegameS
erializer.dll : NVSavegameSerializer::Read][offset 0x00000166][found ref 'CityBu
ilder.CBMap+Tile'][expected value 'CityBuilder.CBMap+Tile'] Unexpected type on t
he stack.
[IL]: Error: [C:\data\projects\innervative\nova\unity\Assets\Plugins\NVSavegameS
erializer.dll : NVSavegameSerializer::Read][offset 0x00000172][found value 'City
Builder.CBMap+Tile'][expected ref 'Cuborph.CXPoolIndex'] Unexpected type on the
stack.
[IL]: Error: [C:\data\projects\innervative\nova\unity\Assets\Plugins\NVSavegameS
erializer.dll : NVSavegameSerializer::Read][offset 0x00000109][found ref 'CityBu
ilder.CBMap+InventoryEntry'][expected value 'CityBuilder.CBMap+InventoryEntry']
Unexpected type on the stack.
[IL]: Error: [C:\data\projects\innervative\nova\unity\Assets\Plugins\NVSavegameS
erializer.dll : NVSavegameSerializer::Read][offset 0x00000115][found value 'City
Builder.CBMap+InventoryEntry'][expected ref 'Cuborph.CXPoolIndex'] Unexpected ty
pe on the stack.
[IL]: Error: [C:\data\projects\innervative\nova\unity\Assets\Plugins\NVSavegameS
erializer.dll : NVSavegameSerializer::Read][offset 0x000000AC][found ref 'CityBu
ilder.CBMap+Captain'][expected value 'CityBuilder.CBMap+Captain'] Unexpected typ
e on the stack.
[IL]: Error: [C:\data\projects\innervative\nova\unity\Assets\Plugins\NVSavegameS
erializer.dll : NVSavegameSerializer::Read][offset 0x000000B8][found value 'City
Builder.CBMap+Captain'][expected ref 'Cuborph.CXPoolIndex'] Unexpected type on t
he stack.
[IL]: Error: [C:\data\projects\innervative\nova\unity\Assets\Plugins\NVSavegameS
erializer.dll : NVSavegameSerializer::Read][offset 0x0000004F][found ref 'CityBu
ilder.CBMap+Building'][expected value 'CityBuilder.CBMap+Building'] Unexpected t
ype on the stack.
[IL]: Error: [C:\data\projects\innervative\nova\unity\Assets\Plugins\NVSavegameS
erializer.dll : NVSavegameSerializer::Read][offset 0x0000005B][found value 'City
Builder.CBMap+Building'][expected ref 'Cuborph.CXPoolIndex'] Unexpected type on
the stack.
[IL]: Error: [C:\data\projects\innervative\nova\unity\Assets\Plugins\NVSavegameS
erializer.dll : NVSavegameSerializer::Write][offset 0x000000FF][found value 'Cit
yBuilder.CBMap+Building'][expected ref 'Cuborph.CXPoolIndex'] Unexpected type on
 the stack.
[IL]: Error: [C:\data\projects\innervative\nova\unity\Assets\Plugins\NVSavegameS
erializer.dll : NVSavegameSerializer::Write][offset 0x0000005B][found value 'Cit
yBuilder.CBMap+Building'][expected ref 'Cuborph.CXPoolIndex'] Unexpected type on
 the stack.
[IL]: Error: [C:\data\projects\innervative\nova\unity\Assets\Plugins\NVSavegameS
erializer.dll : NVSavegameSerializer::Read][offset 0x0000018B][found value 'City
Builder.CBMap+Building'][expected ref 'Cuborph.CXPoolIndex'] Unexpected type on
the stack.
[IL]: Error: [C:\data\projects\innervative\nova\unity\Assets\Plugins\NVSavegameS
erializer.dll : NVSavegameSerializer::Read][offset 0x0000019D][found ref 'CityBu
ilder.CBMap+Building'][expected value 'CityBuilder.CBMap+Building'] Unexpected t
ype on the stack.
[IL]: Error: [C:\data\projects\innervative\nova\unity\Assets\Plugins\NVSavegameS
erializer.dll : NVSavegameSerializer::Read][offset 0x0000004B][found value 'City
Builder.CBMap+Building'][expected ref 'Cuborph.CXPoolIndex'] Unexpected type on
the stack.
[IL]: Error: [C:\data\projects\innervative\nova\unity\Assets\Plugins\NVSavegameS
erializer.dll : NVSavegameSerializer::Read][offset 0x0000005D][found ref 'CityBu
ilder.CBMap+Building'][expected value 'CityBuilder.CBMap+Building'] Unexpected t
ype on the stack.
[IL]: Error: [C:\data\projects\innervative\nova\unity\Assets\Plugins\NVSavegameS
erializer.dll : NVSavegameSerializer::Write][offset 0x000000FF][found value 'Cit
yBuilder.CBMap+Captain'][expected ref 'Cuborph.CXPoolIndex'] Unexpected type on
the stack.
[IL]: Error: [C:\data\projects\innervative\nova\unity\Assets\Plugins\NVSavegameS
erializer.dll : NVSavegameSerializer::Write][offset 0x0000005B][found value 'Cit
yBuilder.CBMap+Captain'][expected ref 'Cuborph.CXPoolIndex'] Unexpected type on
the stack.
[IL]: Error: [C:\data\projects\innervative\nova\unity\Assets\Plugins\NVSavegameS
erializer.dll : NVSavegameSerializer::Read][offset 0x0000018B][found value 'City
Builder.CBMap+Captain'][expected ref 'Cuborph.CXPoolIndex'] Unexpected type on t
he stack.
[IL]: Error: [C:\data\projects\innervative\nova\unity\Assets\Plugins\NVSavegameS
erializer.dll : NVSavegameSerializer::Read][offset 0x0000019D][found ref 'CityBu
ilder.CBMap+Captain'][expected value 'CityBuilder.CBMap+Captain'] Unexpected typ
e on the stack.
[IL]: Error: [C:\data\projects\innervative\nova\unity\Assets\Plugins\NVSavegameS
erializer.dll : NVSavegameSerializer::Read][offset 0x0000004B][found value 'City
Builder.CBMap+Captain'][expected ref 'Cuborph.CXPoolIndex'] Unexpected type on t
he stack.
[IL]: Error: [C:\data\projects\innervative\nova\unity\Assets\Plugins\NVSavegameS
erializer.dll : NVSavegameSerializer::Read][offset 0x0000005D][found ref 'CityBu
ilder.CBMap+Captain'][expected value 'CityBuilder.CBMap+Captain'] Unexpected typ
e on the stack.
[IL]: Error: [C:\data\projects\innervative\nova\unity\Assets\Plugins\NVSavegameS
erializer.dll : NVSavegameSerializer::Write][offset 0x000000FF][found value 'Cit
yBuilder.CBMap+InventoryEntry'][expected ref 'Cuborph.CXPoolIndex'] Unexpected t
ype on the stack.
[IL]: Error: [C:\data\projects\innervative\nova\unity\Assets\Plugins\NVSavegameS
erializer.dll : NVSavegameSerializer::Write][offset 0x0000005B][found value 'Cit
yBuilder.CBMap+InventoryEntry'][expected ref 'Cuborph.CXPoolIndex'] Unexpected t
ype on the stack.
[IL]: Error: [C:\data\projects\innervative\nova\unity\Assets\Plugins\NVSavegameS
erializer.dll : NVSavegameSerializer::Read][offset 0x0000018B][found value 'City
Builder.CBMap+InventoryEntry'][expected ref 'Cuborph.CXPoolIndex'] Unexpected ty
pe on the stack.
[IL]: Error: [C:\data\projects\innervative\nova\unity\Assets\Plugins\NVSavegameS
erializer.dll : NVSavegameSerializer::Read][offset 0x0000019D][found ref 'CityBu
ilder.CBMap+InventoryEntry'][expected value 'CityBuilder.CBMap+InventoryEntry']
Unexpected type on the stack.
[IL]: Error: [C:\data\projects\innervative\nova\unity\Assets\Plugins\NVSavegameS
erializer.dll : NVSavegameSerializer::Read][offset 0x0000004B][found value 'City
Builder.CBMap+InventoryEntry'][expected ref 'Cuborph.CXPoolIndex'] Unexpected ty
pe on the stack.
[IL]: Error: [C:\data\projects\innervative\nova\unity\Assets\Plugins\NVSavegameS
erializer.dll : NVSavegameSerializer::Read][offset 0x0000005D][found ref 'CityBu
ilder.CBMap+InventoryEntry'][expected value 'CityBuilder.CBMap+InventoryEntry']
Unexpected type on the stack.
[IL]: Error: [C:\data\projects\innervative\nova\unity\Assets\Plugins\NVSavegameS
erializer.dll : NVSavegameSerializer::Write][offset 0x000000FF][found value 'Cit
yBuilder.CBMap+Tile'][expected ref 'Cuborph.CXPoolIndex'] Unexpected type on the
 stack.
[IL]: Error: [C:\data\projects\innervative\nova\unity\Assets\Plugins\NVSavegameS
erializer.dll : NVSavegameSerializer::Write][offset 0x0000005B][found value 'Cit
yBuilder.CBMap+Tile'][expected ref 'Cuborph.CXPoolIndex'] Unexpected type on the
 stack.
[IL]: Error: [C:\data\projects\innervative\nova\unity\Assets\Plugins\NVSavegameS
erializer.dll : NVSavegameSerializer::Read][offset 0x0000018B][found value 'City
Builder.CBMap+Tile'][expected ref 'Cuborph.CXPoolIndex'] Unexpected type on the
stack.
[IL]: Error: [C:\data\projects\innervative\nova\unity\Assets\Plugins\NVSavegameS
erializer.dll : NVSavegameSerializer::Read][offset 0x0000019D][found ref 'CityBu
ilder.CBMap+Tile'][expected value 'CityBuilder.CBMap+Tile'] Unexpected type on t
he stack.
[IL]: Error: [C:\data\projects\innervative\nova\unity\Assets\Plugins\NVSavegameS
erializer.dll : NVSavegameSerializer::Read][offset 0x0000004B][found value 'City
Builder.CBMap+Tile'][expected ref 'Cuborph.CXPoolIndex'] Unexpected type on the
stack.
[IL]: Error: [C:\data\projects\innervative\nova\unity\Assets\Plugins\NVSavegameS
erializer.dll : NVSavegameSerializer::Read][offset 0x0000005D][found ref 'CityBu
ilder.CBMap+Tile'][expected value 'CityBuilder.CBMap+Tile'] Unexpected type on t
he stack.
[IL]: Error: [C:\data\projects\innervative\nova\unity\Assets\Plugins\NVSavegameS
erializer.dll : NVSavegameSerializer::Write][offset 0x00000195][found value 'Cit
yBuilder.CBMap+Building'][expected ref 'Cuborph.CXPoolIndex'] Unexpected type on
 the stack.
[IL]: Error: [C:\data\projects\innervative\nova\unity\Assets\Plugins\NVSavegameS
erializer.dll : NVSavegameSerializer::Read][offset 0x0000024D][found value 'City
Builder.CBMap+Building'][expected ref 'Cuborph.CXPoolIndex'] Unexpected type on
the stack.
[IL]: Error: [C:\data\projects\innervative\nova\unity\Assets\Plugins\NVSavegameS
erializer.dll : NVSavegameSerializer::Read][offset 0x0000025E][found ref 'CityBu
ilder.CBMap+Building'][expected value 'CityBuilder.CBMap+Building'] Unexpected t
ype on the stack.
[IL]: Error: [C:\data\projects\innervative\nova\unity\Assets\Plugins\NVSavegameS
erializer.dll : NVSavegameSerializer::Write][offset 0x00000090][found value 'Cit
yBuilder.CBMap+Building'][expected ref 'Cuborph.CXPoolIndex'] Unexpected type on
 the stack.
[IL]: Error: [C:\data\projects\innervative\nova\unity\Assets\Plugins\NVSavegameS
erializer.dll : NVSavegameSerializer::Read][offset 0x00000113][found value 'City
Builder.CBMap+Building'][expected ref 'Cuborph.CXPoolIndex'] Unexpected type on
the stack.
[IL]: Error: [C:\data\projects\innervative\nova\unity\Assets\Plugins\NVSavegameS
erializer.dll : NVSavegameSerializer::Read][offset 0x00000125][found ref 'CityBu
ilder.CBMap+Building'][expected value 'CityBuilder.CBMap+Building'] Unexpected t
ype on the stack.
[IL]: Error: [C:\data\projects\innervative\nova\unity\Assets\Plugins\NVSavegameS
erializer.dll : NVSavegameSerializer::Write][offset 0x0000005B][found value 'Cit
yBuilder.CBMap+Building'][expected ref 'Cuborph.CXPoolIndex'] Unexpected type on
 the stack.
[IL]: Error: [C:\data\projects\innervative\nova\unity\Assets\Plugins\NVSavegameS
erializer.dll : NVSavegameSerializer::Read][offset 0x0000004B][found value 'City
Builder.CBMap+Building'][expected ref 'Cuborph.CXPoolIndex'] Unexpected type on
the stack.
[IL]: Error: [C:\data\projects\innervative\nova\unity\Assets\Plugins\NVSavegameS
erializer.dll : NVSavegameSerializer::Read][offset 0x0000005D][found ref 'CityBu
ilder.CBMap+Building'][expected value 'CityBuilder.CBMap+Building'] Unexpected t
ype on the stack.
[IL]: Error: [C:\data\projects\innervative\nova\unity\Assets\Plugins\NVSavegameS
erializer.dll : NVSavegameSerializer::Read][offset 0x0000002B] Cannot change ini
tonly field outside its .ctor.
[IL]: Error: [C:\data\projects\innervative\nova\unity\Assets\Plugins\NVSavegameS
erializer.dll : NVSavegameSerializer::Write][offset 0x00000035][found value 'Cit
yBuilder.CBMap+InventoryEntry'][expected ref 'Cuborph.CXPoolIndex'] Unexpected t
ype on the stack.
[IL]: Error: [C:\data\projects\innervative\nova\unity\Assets\Plugins\NVSavegameS
erializer.dll : NVSavegameSerializer::Read][offset 0x00000038][found value 'City
Builder.CBMap+InventoryEntry'][expected ref 'Cuborph.CXPoolIndex'] Unexpected ty
pe on the stack.
[IL]: Error: [C:\data\projects\innervative\nova\unity\Assets\Plugins\NVSavegameS
erializer.dll : NVSavegameSerializer::Read][offset 0x0000004A][found ref 'CityBu
ilder.CBMap+InventoryEntry'][expected value 'CityBuilder.CBMap+InventoryEntry']
Unexpected type on the stack.
49 Error(s) Verifying NVSavegameSerializer.dll
c#
unity3d
protobuf-net
asked on Stack Overflow May 26, 2015 by g012 • edited May 27, 2015 by Scott Chamberlain

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0