-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathISavesLocation.cs
235 lines (195 loc) · 10.2 KB
/
ISavesLocation.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
using PalCalc.Model;
using PalCalc.SaveReader.SaveFile;
using PalCalc.SaveReader.SaveFile.Xbox;
using Serilog;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection.Metadata.Ecma335;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using Windows.ApplicationModel;
using Windows.Management.Deployment;
using Windows.Storage;
namespace PalCalc.SaveReader
{
public interface ISavesLocation
{
public string FolderPath { get; }
public string FolderName => FolderPath == null ? "None" : Path.GetFileName(FolderPath);
public IEnumerable<ISaveGame> AllSaveGames { get; }
public IEnumerable<ISaveGame> ValidSaveGames => AllSaveGames.Where(g => g.IsValid);
}
public class DirectSavesLocation : ISavesLocation
{
private static ILogger logger = Log.ForContext<DirectSavesLocation>();
public DirectSavesLocation(string folderPath)
{
FolderPath = folderPath;
}
public string FolderPath { get; }
public string FolderName => Path.GetFileName(FolderPath);
public IEnumerable<ISaveGame> AllSaveGames => Directory.EnumerateDirectories(FolderPath).Select(d => new StandardSaveGame(d));
public IEnumerable<ISaveGame> ValidSaveGames => AllSaveGames.Where(g => g.IsValid);
private static List<DirectSavesLocation> FindAllForWindows()
{
var mainSavePath = Environment.ExpandEnvironmentVariables("%LOCALAPPDATA%/Pal/Saved/SaveGames");
logger.Information("checking for save game folders in {mainSavePath}", mainSavePath);
// this file has multiple ID-like sub-folders, I assume there's one for each steam/etc. user, but
// it also has a `UserOption.sav` which seems to store things like joystick sensitivity?
if (Directory.Exists(mainSavePath))
{
return Directory.EnumerateDirectories(mainSavePath).Select(d => new DirectSavesLocation(d)).ToList();
}
else
{
logger.Information("folder does not exist");
return new List<DirectSavesLocation>();
}
}
public static List<DirectSavesLocation> AllLocal
{
get
{
logger.Information("listing all local saves from known file locations");
if (OperatingSystem.IsWindows()) return FindAllForWindows();
else
{
logger.Warning("unsupported platform");
return new List<DirectSavesLocation>();
}
}
}
}
// possible thanks to https://github.com/Tom60chat/Xbox-Live-Save-Exporter/
// (where that was possible thanks to "HunterStanton" and "snoozbuster", see that repo's README)
public class XboxSavesLocation : ISavesLocation
{
private static ILogger logger = Log.ForContext<XboxSavesLocation>();
private class XboxSaveFile
{
public string FilePath { get; set; }
public string FileName { get; set; }
}
public static List<XboxSavesLocation> FindAll()
{
var LocalPackagesPath = Path.Combine(UserDataPaths.GetDefault().LocalAppData, "Packages");
var pm = new PackageManager();
string pkgDir = null;
Package palworldPkg = null;
// Logo = {file:///C:/Program Files/WindowsApps/Resources/PocketpairInc.Palworld_0.0.51499.0_x64__ad4psfrxyesvt/Resources/StoreLogo.png}
foreach (var packageDir in Directory.EnumerateDirectories(LocalPackagesPath))
{
if (!Directory.Exists(Path.Join(packageDir, "SystemAppData\\wgs"))) continue;
// note: Xbox-Live-Save-Exporter checks `package.DisplayName`, which is a more accurate way of checking the app name,
// but the `.DisplayName` call seems to take forever. The directory for palworld data should also contain the
// "Palworld" string, so this should be fine
if (!Path.GetFileName(packageDir).Contains("Palworld", StringComparison.InvariantCultureIgnoreCase)) continue;
var package = pm.FindPackagesForUser(null, Path.GetFileName(packageDir))?.FirstOrDefault();
palworldPkg = package;
pkgDir = packageDir;
break;
}
if (palworldPkg == null) return new List<XboxSavesLocation>();
var result = new List<XboxSavesLocation>();
foreach (var userFolder in Directory.EnumerateDirectories(Path.Combine(pkgDir, "SystemAppData\\wgs")))
{
try
{
var dataContainer = Container.TryParse(Path.Combine(userFolder, "containers.index"));
if (dataContainer == null) continue;
// save files that are part of a single save are grouped by the first part of their name
var collectedSaveFiles = new List<XboxSaveFile>();
foreach (var saveFileFolder in dataContainer.Folders.Where(f => f.Name.Count(c => c == '-') != 0))
{
var saveGameFiles = ContainerFile.TryParse(saveFileFolder);
foreach (var saveFile in saveGameFiles.Where(f => File.Exists(f.Path)))
{
// all of the files are stored in their own folders, where the "real" file name is always just "Data"
if (saveFile.Name != "Data") continue;
collectedSaveFiles.Add(new XboxSaveFile() { FilePath = saveFile.Path, FileName = saveFileFolder.Name });
}
}
var saveGames = collectedSaveFiles
.GroupBy(xsf => xsf.FileName.Split('-')[0])
.Select(g =>
{
LevelSaveFile level = null;
LevelMetaSaveFile levelMeta = null;
LocalDataSaveFile localData = null;
WorldOptionSaveFile worldOption = null;
List<PlayersSaveFile> players = new List<PlayersSaveFile>();
var filesByType = g.GroupBy(f => f.FileName.Split('-')[1]).ToDictionary(g => g.Key, g => g.ToList());
// there can be multiple `Level` files, supposedly when there's a new file format and the latest file
// get `-1` appended (or the next number after that). sort by this last part and take the highest number
//
// TODO - not sure if this is the right approach, or if the `Level.sav` file is always the latest
var levelFile = filesByType.GetValueOrDefault("Level")
?.OrderByDescending(l => int.Parse(l.FileName.Split('-').Skip(2).FirstOrDefault() ?? "0"))
?.FirstOrDefault();
var watchers = new List<FileSystemWatcher>();
if (levelFile != null)
{
level = new LevelSaveFile(levelFile.FilePath);
watchers.Add(new FileSystemWatcher(Path.GetDirectoryName(levelFile.FilePath)));
}
var levelMetaFile = filesByType.GetValueOrDefault("LevelMeta")?.FirstOrDefault();
if (levelMetaFile != null)
{
levelMeta = new LevelMetaSaveFile(levelMetaFile.FilePath);
watchers.Add(new FileSystemWatcher(Path.GetDirectoryName(levelMetaFile.FilePath)));
}
var localDataFile = filesByType.GetValueOrDefault("LocalData")?.FirstOrDefault();
if (localDataFile != null)
{
localData = new LocalDataSaveFile(localDataFile.FilePath);
watchers.Add(new FileSystemWatcher(Path.GetDirectoryName(localDataFile.FilePath)));
}
var worldOptionFile = filesByType.GetValueOrDefault("WorldOption")?.FirstOrDefault();
if (worldOptionFile != null)
{
worldOption = new WorldOptionSaveFile(worldOptionFile.FilePath);
watchers.Add(new FileSystemWatcher(Path.GetDirectoryName(worldOptionFile.FilePath)));
}
players = filesByType
.GetValueOrElse("Players", new List<XboxSaveFile>())
.Select(f =>
{
watchers.Add(new FileSystemWatcher(Path.GetDirectoryName(f.FilePath)));
return new PlayersSaveFile(f.FilePath);
})
.ToList();
return new XboxSaveGame(userFolder, g.Key, level, levelMeta, localData, worldOption, players, watchers);
})
.ToList();
result.Add(new XboxSavesLocation(userFolder, saveGames));
}
catch (Exception ex)
{
logger.Warning(ex, "error reading Xbox save data from user folder {path}", userFolder);
}
}
return result;
}
private XboxSavesLocation(string userFolderPath, List<XboxSaveGame> saves)
{
try
{
FolderPath = userFolderPath;
AllSaveGames = saves;
}
catch
{
AllSaveGames = new List<ISaveGame>();
}
}
public XboxSavesLocation()
{
AllSaveGames = new List<ISaveGame>();
FolderPath = null;
}
public string FolderPath { get; }
public IEnumerable<ISaveGame> AllSaveGames { get; }
}
}