jefffhaynes / BinarySerializer

A declarative serialization framework for controlling formatting of data at the byte and bit level using field bindings, converters, and code.
MIT License
292 stars 62 forks source link

Serialize to file output zeros with a valid populated class #152

Closed sn4k3 closed 4 years ago

sn4k3 commented 4 years ago

I'm having a trouble and spent many hours trying to debug, but no matter what i try when i read back the pervious write it output a empty class.

The encoding starts at: https://github.com/sn4k3/PrusaSL1Viewer/blob/master/PrusaSL1Reader/CbddlpFile.cs -> public override void BeginEncode(string fileFullPath)

The Decode method under a valid previous made file works well, but if i try to recreate the binary file on my program it ouput empty classes, just the first class gets ok (Header) HxD_2020-04-11_21-25-22 ) Files.zip

How to recreate the problem using the GUI: open the .sl1 file -> Go to Edit menu - Convert - Save the file.

PS: There are missing objects to enconde but that doesn't matter since first 3 classes should return valid content which doesn't happen.

Debug output from Write operation

Encode Results: Magic: 318570521, Version: 2, BedSizeX: 120,96, BedSizeY: 68,04, BedSizeZ: 150, Unknown1: 0, Unknown2: 0, Unknown3: 0, LayerHeightMilimeter: 0,1, LayerExposureSeconds: 10, BottomExposureSeconds: 35, LayerOffTime: 0, BottomLayersCount: 10, ResolutionX: 2560, ResolutionY: 1440, PreviewOneOffsetAddress: 0, LayersDefinitionOffsetAddress: 266, LayerCount: 559, PreviewTwoOffsetAddress: 0, PrintTime: 8585, ProjectorType: 1, PrintParametersOffsetAddress: 112, PrintParametersSize: 60, AntiAliasLevel: 1, LightPWM: 255, BottomLightPWM: 255, Padding1: 0, Padding2: 0, Padding3: 0 BottomLiftHeight: 5, BottomLiftSpeed: 60, LiftHeight: 5, LiftingSpeed: 60, RetractSpeed: 150, VolumeMl: 33,95821, WeightG: 33,95821, CostDollars: 0, BottomLightOffDelay: 0, LightOffDelay: 0, BottomLayerCount: 10, P1: 0, P2: 0, P3: 0, P4: 0 d1: 0, d2: 0, d3: 0, d4: 0, d5: 0, d6: 0, d7: 0, MachineNameAddress: 248, MachineNameSize: 18, d8: 0, d9: 0, d10: 0, d11: 0, d12: 0, d13: 0, d14: 0, d15: 0, d16: 0, d17: 0, MachineName: Original Prusa SL1 -End-

Debug from Read back operation

Header -> Magic: 318570521, Version: 2, BedSizeX: 120,96, BedSizeY: 68,04, BedSizeZ: 150, Unknown1: 0, Unknown2: 0, Unknown3: 0, LayerHeightMilimeter: 0,1, LayerExposureSeconds: 10, BottomExposureSeconds: 35, LayerOffTime: 0, BottomLayersCount: 10, ResolutionX: 2560, ResolutionY: 1440, PreviewOneOffsetAddress: 0, LayersDefinitionOffsetAddress: 266, LayerCount: 559, PreviewTwoOffsetAddress: 0, PrintTime: 8585, ProjectorType: 1, PrintParametersOffsetAddress: 112, PrintParametersSize: 60, AntiAliasLevel: 1, LightPWM: 255, BottomLightPWM: 255, Padding1: 0, Padding2: 0, Padding3: 0 Print Parameters -> BottomLiftHeight: 0, BottomLiftSpeed: 0, LiftHeight: 0, LiftingSpeed: 0, RetractSpeed: 0, VolumeMl: 0, WeightG: 0, CostDollars: 0, BottomLightOffDelay: 0, LightOffDelay: 0, BottomLayerCount: 0, P1: 0, P2: 0, P3: 0, P4: 0 Machine Info -> d1: 0, d2: 0, d3: 0, d4: 0, d5: 0, d6: 0, d7: 0, MachineNameAddress: 0, MachineNameSize: 0, d8: 0, d9: 0, d10: 0, d11: 0, d12: 0, d13: 0, d14: 0, d15: 0, d16: 0, d17: 0, MachineName:

As can be observed after Header, everything comes empty

EDIT 1: I found part of the problem, it seens serializer sizeof is returning diferent values from encoded byte count:

CurrentOffset = Helpers.SerializeWriteFileStream(OutputFile, HeaderSettings); // 256
uint size = (uint) Helpers.Serializer.SizeOf(HeaderSettings); // 4*28=112

Giving that when i set the position address it will be in wrong position due the use of SizeOf When analising original file, the header is 112 bytes, why write operation is outputing more?

EDIT 2: Found the problem, i was using stream.GetBuffer() instead of stream.ToArray() which cause wrong output

sn4k3 commented 4 years ago

Found the problem, i was using stream.GetBuffer() instead of stream.ToArray() which cause wrong output.

jefffhaynes commented 4 years ago

GetBuffer is the worst