forked from tylercamp/palcalc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCompressedSAV.cs
143 lines (121 loc) · 5.01 KB
/
CompressedSAV.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
using ICSharpCode.SharpZipLib.Zip;
using ICSharpCode.SharpZipLib.Zip.Compression.Streams;
using Serilog;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.IO.Compression;
using System.Linq;
using System.Reflection.PortableExecutable;
using System.Text;
using System.Threading.Tasks;
// palsav.py
namespace PalCalc.SaveReader
{
public static class CompressedSAV
{
private static ILogger logger = Log.ForContext(typeof(CompressedSAV));
// file operations can take a while, and they can happen while Palworld is running + saving file contents.
// save files are relatively small (typically ~10MB at most), prefer loading the whole file into memory up
// front so we can close the handle ASAP.
private static Stream ReadFileNonLocking(string filePath)
{
// since the file can be moved or changed during the read op, auto-retry any failed attempts
byte[] fileBytes;
const int maxAttempts = 3;
for (int i = 1; i <= maxAttempts; i++)
{
try
{
using (var fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite | FileShare.Delete))
using (var ms = new MemoryStream())
{
fs.CopyTo(ms);
fileBytes = ms.ToArray();
}
return new MemoryStream(fileBytes);
}
catch (Exception e) when (i != maxAttempts)
{
logger.Warning(e, "Error while reading {File}, retrying", filePath);
}
}
// (shouldn't happen, we should have returned a stream or thrown an exception by now)
throw new NotImplementedException();
}
// don't know what this is, but I've seen this in newer palworld saves from Game Pass. seems
// to clear itself up (back to normal format) after the game has been closed for a little while.
// likely an indicator of unsynced save or something like that.
static byte[] WRAPPER_MAGIC_BYTES = Encoding.ASCII.GetBytes("CNK");
static byte[] MAGIC_BYTES = Encoding.ASCII.GetBytes("PlZ");
public static void WithDecompressedSave(string filePath, Action<Stream> action)
{
logger.Information("Loading {file} as GVAS", filePath);
using (var fs = ReadFileNonLocking(filePath))
using (var binaryReader = new BinaryReader(fs))
{
// unused
var uncompressedLength = binaryReader.ReadInt32();
var compressedLen = binaryReader.ReadInt32();
var magicBytes = binaryReader.ReadBytes(3);
if (WRAPPER_MAGIC_BYTES.SequenceEqual(magicBytes))
{
// unknown content
binaryReader.ReadBytes(9);
magicBytes = binaryReader.ReadBytes(3);
}
if (!MAGIC_BYTES.SequenceEqual(magicBytes))
{
throw new Exception("Magic bytes mismatch");
}
var saveType = binaryReader.ReadByte();
switch (saveType)
{
case 0x31: break;
case 0x32: break;
default: throw new Exception("Unrecognized compression type");
}
using (var decompressed = new InflaterInputStream(fs))
{
if (saveType == 0x32)
{
using (var doubleDecompressed = new InflaterInputStream(decompressed))
action(doubleDecompressed);
}
else
{
action(decompressed);
}
}
}
logger.Information("done");
}
public static bool IsValidSave(string filePath)
{
using (var fs = ReadFileNonLocking(filePath))
using (var binaryReader = new BinaryReader(fs))
{
// unused
var uncompressedLength = binaryReader.ReadInt32();
var compressedLen = binaryReader.ReadInt32();
var magicBytes = binaryReader.ReadBytes(3);
if (WRAPPER_MAGIC_BYTES.SequenceEqual(magicBytes))
{
binaryReader.ReadBytes(9);
magicBytes = binaryReader.ReadBytes(3);
}
if (!MAGIC_BYTES.SequenceEqual(magicBytes))
return false;
var saveType = binaryReader.ReadByte();
switch (saveType)
{
case 0x31: return true;
case 0x32: return true;
default: return false;
}
}
}
}
}