-
-
Notifications
You must be signed in to change notification settings - Fork 100
/
Copy pathSaveFileReader.cs
174 lines (143 loc) · 5.57 KB
/
SaveFileReader.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
using Multiplayer.Common;
using RimWorld;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Xml;
using UnityEngine;
using Verse;
namespace Multiplayer.Client
{
public class SaveFileReader
{
public List<FileInfo> SpSaves { get; private set; }
public List<FileInfo> MpSaves { get; private set; }
private ConcurrentDictionary<FileInfo, SaveFile> data = new();
private Task spTask, mpTask;
public void StartReading()
{
SpSaves = GenFilePaths.AllSavedGameFiles.OrderByDescending(f => f.LastWriteTime).ToList();
var replaysDir = new DirectoryInfo(GenFilePaths.FolderUnderSaveData("MpReplays"));
var toRead = replaysDir.GetFiles("*.zip", MpVersion.IsDebug ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly);
MpSaves = toRead.OrderByDescending(f => f.LastWriteTime).ToList();
spTask = Task.Run(() => SpSaves.ForEach(ReadSpSave));
// Loading Replays currently uses DirectXmlToObject which isn't thread-safe.
// Running the whole task on one thread is fine, though it can't be parallelized any further
mpTask = Task.Run(() => MpSaves.ForEach(ReadMpSave));
}
public void WaitTasks()
{
spTask.Wait();
mpTask.Wait();
}
private void ReadSpSave(FileInfo file)
{
try
{
var saveFile = new SaveFile(Path.GetFileNameWithoutExtension(file.Name), false, file);
using var stream = file.OpenRead();
ReadSaveInfo(stream, saveFile);
data[file] = saveFile;
}
catch (Exception ex)
{
Log.Warning($"Exception loading save info of {file.Name}: {ex}");
}
}
private void ReadMpSave(FileInfo file)
{
try
{
var displayName = Path.ChangeExtension(file.FullName.Substring(Multiplayer.ReplaysDir.Length + 1), null);
var saveFile = new SaveFile(displayName, true, file);
var replay = Replay.ForLoading(file);
if (!replay.LoadInfo()) return;
saveFile.gameName = replay.info.name;
saveFile.protocol = replay.info.protocol;
saveFile.replaySections = replay.info.sections.Count;
if (!replay.info.rwVersion.NullOrEmpty())
{
saveFile.rwVersion = replay.info.rwVersion;
saveFile.modIds = replay.info.modIds.ToArray();
saveFile.modNames = replay.info.modNames.ToArray();
saveFile.asyncTime = replay.info.asyncTime;
saveFile.multifaction = replay.info.multifaction;
}
else
{
using var zip = replay.OpenZipRead();
using var stream = zip.GetEntry("world/000_save")!.Open();
ReadSaveInfo(stream, saveFile);
}
data[file] = saveFile;
}
catch (Exception ex)
{
Log.Warning($"Exception loading replay info of {file.Name}: {ex}");
}
}
private void ReadSaveInfo(Stream stream, SaveFile save)
{
using var reader = new XmlTextReader(stream);
reader.ReadToNextElement(); // savedGame
reader.ReadToNextElement(); // meta
if (reader.Name != "meta") return;
reader.ReadToDescendant("gameVersion");
save.rwVersion = VersionControl.VersionStringWithoutRev(reader.ReadString());
reader.ReadToNextSibling("modIds");
save.modIds = reader.ReadStrings();
reader.ReadToNextSibling("modNames");
save.modNames = reader.ReadStrings();
}
public SaveFile GetData(FileInfo file)
{
data.TryGetValue(file, out var saveFile);
return saveFile;
}
public void RemoveFile(FileInfo info)
{
SpSaves.Remove(info);
MpSaves.Remove(info);
}
}
public class SaveFile
{
public string displayName;
public bool replay;
public int replaySections;
public FileInfo file;
public string gameName;
public string rwVersion;
public string[] modNames = Array.Empty<string>();
public string[] modIds = Array.Empty<string>();
public int protocol;
public bool asyncTime;
public bool multifaction;
public bool HasRwVersion => rwVersion != null;
public bool MajorAndMinorVerEqualToCurrent =>
VersionControl.MajorFromVersionString(rwVersion) == VersionControl.CurrentMajor &&
VersionControl.MinorFromVersionString(rwVersion) == VersionControl.CurrentMinor;
public Color VersionColor
{
get
{
if (rwVersion == null)
return Color.red;
if (MajorAndMinorVerEqualToCurrent)
return new Color(0.6f, 0.6f, 0.6f);
if (BackCompatibility.IsSaveCompatibleWith(rwVersion))
return Color.yellow;
return Color.red;
}
}
public SaveFile(string displayName, bool replay, FileInfo file)
{
this.displayName = displayName;
this.replay = replay;
this.file = file;
}
}
}