0

I'm converting some python code to C# and was successful so far, but there's this one part that I don't understand and is arguably the most important part as it involves file decompression and turning into JSON and human readable format.

file_data: bytes = file.read() 'I understand this
file_data: bytes = struct.pack('B' * len(file_data), *file_data[::-1]) 'This is the part that I do not understand, everything after the 'B'
file_data: bytes = zlib.decompress(file_data) 'should be fine, c# should have same library
file_data: tuple = pickle.loads(file_data, encoding='windows-1251')
CruleD
  • 1,153
  • 2
  • 7
  • 15
  • I understand that SO is not a code conversion service, but I've been looking for hours and don't feel like I'm getting any closer to a solution, hence the question. – CruleD Jun 22 '21 at 02:40
  • You haven't come to the trickiest part yet. `Pickle` is a Python-specific way of encoding a data structure to file so it can be loaded on other computers. It's not intended to be language-independent. You will have to implement the whole pickle protocol, and there are some concepts that won't cross over to C#. – Tim Roberts Jun 22 '21 at 03:31
  • @TimRoberts I used razorvine.pickler, just gotta figure out how to fix the following error for it now "Razorvine.Pickle.PickleException: 'expected zero arguments for construction of ClassDict (for copy_reg._reconstructor). This happens when an unsupported/unregistered class is being unpickled that requires construction arguments. Fix it by registering a custom IObjectConstructor for this class.'", the python file has class TypeInfo(object): pass class GPData(object): pass class GameParams: pass for pickler. – CruleD Jun 22 '21 at 13:30

1 Answers1

0

That python code line simply reverses the code and removes the header.

Here's the c# code that does the same.

        Hashtable UnpickledGP;
        byte[] FileBytes = File.ReadAllBytes("GameParams.data").Reverse().ToArray();
        byte[] ModdedFileBytes = new byte[FileBytes.Length - 2]; //remove file header
        Array.Copy(FileBytes, 2, ModdedFileBytes, 0, ModdedFileBytes.Length);

        using (Stream StreamTemp = new MemoryStream(ModdedFileBytes))
        {
            using (MemoryStream MemoryStreamTemp = new MemoryStream())
            {
                using (DeflateStream DeflateStreamTemp = new DeflateStream(StreamTemp, CompressionMode.Decompress))
                {
                    DeflateStreamTemp.CopyTo(MemoryStreamTemp);
                }
                ModdedFileBytes = MemoryStreamTemp.ToArray();
            }
        }
        using (Unpickler UnpicklerTemp = new Unpickler())
        {
            object[] UnpickledObjectTemp = (object[])UnpicklerTemp.loads(ModdedFileBytes);
            UnpickledGP = (Hashtable)UnpickledObjectTemp[0];
            UnpicklerTemp.close();
        }
CruleD
  • 1,153
  • 2
  • 7
  • 15