Serialized Save File Format
I have used a text editor to read a save file created with:
BinaryFormatter.Serialize(fileStream, saveData);
and the format doesn't appear to written in binary when interpreted by Notepad. What format is this binary data being written in here?
Answer by Bunny83 · May 20, 2020 at 01:02 AM
I'm not sure what you're asking. The BinaryFormatter uses a very verbose binary format. It's actually the remoting protocol that is used. I always recommend to not use the BinaryFormatter for save game files because:
The files get quite big due to the verbose information stored. The remoting protocol essentially stores the whole class / type descriptions of the objects saved.
Many just use it because it's simple to use and they think the data is somewhat protected against user manipulations. Though since the format is a well known format it's quite trivial to read / modify any value stored.
The BinaryFormatter is extremely sensitive to any kind of structural changes to the objects saved. So adding or removing any field to the class you save will break all previous save game files.
I can asure you that the data is stored in binary. However you might have the wrong idea what binary means. Text is also stored in binary. Notepad can't really view, let alone editing binary formats since it interprets everything as text. Of course actual texts within the binary format would be viewed correctly. This is true for any binary formats. To view the file for debug purposes I would recommend to use a hexeditor. If you don't have any hex editor installed you can use an online editor like this one. It also has a nice data inspector.
If you have any further questions regarding the format, feel free to get more concrete in your question. I can read the format pretty much directly. (I spend quite some time analysing the format ^^).
So say i wanted to produce the same hexidecimal stream in a way where the data was accessible in exactly the same format that binaryformatter produces, text bits and all. Any thoughts on how that might be done?
Answer by The_Kakaze · May 20, 2020 at 12:04 PM
I'm interested in passing the data that has been written into a filestream format to a JSLIB method that will download it to a file. This is a formatted filestream written into a file by the binary formatter using a custom saveData struct:
ÿÿÿÿ FAssembly-CSharp, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null PStructs.savedProblemRun name inputChangesprintRunInt PStructs.problemRunData[] adamTest PStructs.problemRunData ûÿÿÿPStructs.problemRunData isEngagedoperatorChangedriveChangeregistrationLRChangeregistrationUDChange newAnilox €? €? ffæ?úÿÿÿûÿÿÿ ? ? ? ? Aùÿÿÿûÿÿÿ ? ? ? ?ffæ?øÿÿÿûÿÿÿ ? ? ? ?ffæ?÷ÿÿÿûÿÿÿ ? ? ? ?ffæ?öÿÿÿûÿÿÿ ? ? ? ?ffæ?õÿÿÿûÿÿÿ ? ? ? ?ffæ?
As you can see, there is plain text in this file. The following was created using a memorystream, the same saveData struct, and the binary formatter:
t3.crf # ««««««««««««««««) á ‘ Ãx›4à €¿ €¿ €? ÿ „4? y> Ëhº‘ Ãjó0C €¿ €¿ €? ÿ „4? øœ> €ÿCËhºÔ7Cjó0C €¿ €¿ €? ÿ øÿIËhºÔ7Cx›4à €¿ €¿ €? ÿ €ÿIËhº €¿ €¿ €?ÿÿÿÿ €¿ €¿ €?ÿÿÿÿ €¿ €¿ €?ÿÿÿÿ €¿ €¿ €?ÿÿÿÿ 1 s ™ 9 9 7
WebGL has been a bit sensitive to the type of data i have been attempting to pass it. When I use File.ReadAllBytes on a freshly written formatted filestream I was getting all sorts of JS errors so I attempted to switch methods for creating the byte[].
I suppose my question then becomes, how do I write a byte array with all of the verbosity to a byte array?
Well, first of all you should have mentioned your actual issue in your question. Second it's completely unclear what you're actually doing to "pass it to a JSLib". What do you actually want to do in the JSLib with the data? If you just want to initiate a download in the browser, I can recommend my download helper class for WebGL. It essentially encodes the data as a base64 string which is send to a JSLIB which will create a temporary anchor element with a data URL and simulate a click on it to initiate the download of the data.
Note that the script contains some editor code that will extract the JSLIB into the plugin folder. However for that to happen you have to click run at least once in the editor before building your application.
Thank you so much! I'm going to write this up in more detail on the other thread where I asked this question directly, but it turns out that all i was missing was two things: the byte array sent to JS as a Base64 string, and the back half of your $$anonymous$$I$$anonymous$$E type: 'charset=utf-8;base64,'
The reason why I wrote the question with the phrasing I did was that I believed I didn't understand how to write the output file in JavaScript: the 'format' of the $$anonymous$$I$$anonymous$$E type and of the BinaryFormatter output. $$anonymous$$y understanding of Java and computer science terms in general is pretty low, sadly