c# - unable to de-serialize data -
i started created unity game visual studio project. created side project (winforms) testing don't know unit testing. there, of code works perfectly, serialization class works flawlessly when protobuf-net dll used 1 unity. now, when started work on unity, copied on of code , dlls protobuf-net , mysql.data. reason, cannot (basically)same code work. exception when de-serializing, this:
invalid field in source data: 0
and this
unexpected end-group in source data; means source data corrupt
this stack trace last exception:
protobuf.protoexception: unexpected end-group in source data; means source data corrupt @ protobuf.protoreader.readfieldheader () @ (wrapper dynamic-method) neohumansoftware.aon.userinformation.user.proto_2 (object,protobuf.protoreader) @ protobuf.serializers.compiledserializer.protobuf.serializers.iprotoserializer.read (object,protobuf.protoreader) @ protobuf.meta.runtimetypemodel.deserialize (int,object,protobuf.protoreader) @ protobuf.meta.typemodel.deserializecore (protobuf.protoreader,system.type,object,bool) @ protobuf.meta.typemodel.deserialize (system.io.stream,object,system.type,protobuf.serializationcontext) @ protobuf.meta.typemodel.deserialize (system.io.stream,object,system.type) @ protobuf.serializer.deserialize (system.io.stream) <0x000ae> @ neohumansoftware.aon.storage.serializationhandler.deserialize (string) <0x00140> @ neohumansoftware.aon.storage.datahandler.deserializeuser (string)
i omitted last part of stack trace it's not relevant. not understand why works in visual studio not in unity. serializer:
using (memorystream ms = new memorystream()) { serializer.serialize(ms, toserialize); return ms.toarray(); }
and de-serializer:
using (memorystream ms = new memorystream(todeserialize)) { return serializer.deserialize<t>(ms); }
and object serialize "heart" of game comes user.user:
[protocontract] internal class user { internal static user user { get; set; } [protomember(1)] internal usomething { get; private set; } [protomember(2)] internal bool usomething2 { get; set; } [protomember(3)] internal something3 usomething3 { get; set; } [protomember(4)] internal something4 usomething4 { get; private set; } [protomember(5)] internal something5 usomething5 { get; private set; }
all of these custom classes have [protocontract] attribute along @ least 1 [protomember], , not use other attribute apart these 2. flow of game:
- data created.
- data serialized.
- serialized data passed on encryption (rijndaelmanaged).
- encrypted , serialized data written file.
- game ends
- data loaded file.
- data unencrypted using exact same key , iv (randomly generated , stored)
- data de-serialized.
- game starts
edit: trying use visual studio's project de-serialize unity's data not work. trying use unity de-serialize visual studio's data not work. seems unity not protobuf-net @ all...
edit2: changed serializer method to:
byte[] b; using (memorystream ms = new memorystream()) { serializer.serialize(ms, toxml); b = new byte[ms.position]; var fullb = ms.getbuffer(); array.copy(fullb, b, b.length); }
and got:
protobuf.protoexception: invalid wire-type; means have over-written file without truncating or setting length; see using protobuf-net, got exception unknown wire-type @ protobuf.protoreader.skipfield () @ (wrapper dynamic-method) neohumansoftware.aon.userinformation.user.proto_2 (object,protobuf.protoreader) @ protobuf.serializers.compiledserializer.protobuf.serializers.iprotoserializer.read (object,protobuf.protoreader) @ protobuf.meta.runtimetypemodel.deserialize (int,object,protobuf.protoreader) @ protobuf.meta.typemodel.deserializecore (protobuf.protoreader,system.type,object,bool) @ protobuf.meta.typemodel.deserialize (system.io.stream,object,system.type,protobuf.serializationcontext) @ protobuf.meta.typemodel.deserialize (system.io.stream,object,system.type) @ protobuf.serializer.deserialize (system.io.stream) <0x000ae>
if helps, these read , write file methods:
internal static void saveuser(string userserialized) { file.writealltext(somepath, userserialized); } internal static void loaduser(string userserialized) { file.readalltext(somepath); }
edit 3: in unity's project, had these did not have in visual studio's project:
internal static coordinates fromvector3(vector3 vector) { return new coordinates(vector.x, vector.y, vector.z); } internal static vector3 tovector3(coordinates c) { return new vector3(c.x, c.y, c.z); }
these in coordinates class, has [protocontract] , [protomember] x, y , z. commenting these 2 methods out stops me receiving exceptions serialization still doesn't work (the user properties "new")
solved
after giving hope on protobuf-net switched on datacotractserializer somehow worked on unity. still got many issues decided turn off encryption , read xml using visual studio; the file ok. still had lot of issues 3 event handlers (2 hooked on game start, other each time special object created), had remove event handlers , operations manually (what pain in a$$). after of this, managed add protobuf-net , work should (note protobuf-net gives me 89 bytes file agains dcs' 3.5 kb file)
so, final solution extremely weird issue was:
- disable functional rijndaelmanaged implementation
- disable event handlers
if can me understand why rijdnaelmanaged did work on visual studio not unity, please leave comment.
Comments
Post a Comment