Skip to content

Commit 0481cd4

Browse files
committed
Add command line argument that allows going straight to the host window
1 parent 12f0188 commit 0481cd4

File tree

4 files changed

+83
-59
lines changed

4 files changed

+83
-59
lines changed

Source/Client/MultiplayerStatic.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ public static class MultiplayerStatic
4242
public static readonly Texture2D GiftModeIcon = ContentFinder<Texture2D>.Get("UI/Buttons/GiftMode");
4343
public static readonly Texture2D TradeModeIcon = ContentFinder<Texture2D>.Get("UI/Buttons/TradeMode");
4444

45+
public const string MpHostReplayCmdLineArg = "mphostreplay";
46+
4547
static MultiplayerStatic()
4648
{
4749
Native.InitLmfPtr(
@@ -263,6 +265,11 @@ void TickBatch()
263265
DirectXmlSaver.SaveDataObject(new SyncContainer(), "SyncHandlers.xml");
264266
ExtendDirectXmlSaver.extend = false;
265267
}
268+
269+
if (GenCommandLine.TryGetCommandLineArg(MpHostReplayCmdLineArg, out var path))
270+
{
271+
DoubleLongEvent(() => HostWindow.VerifyAndOpen(path), "Loading");
272+
}
266273
}
267274

268275
public class SyncContainer

Source/Client/Windows/HostWindow.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using RimWorld;
33
using System;
44
using System.Collections.Generic;
5+
using System.IO;
56
using System.Linq;
67
using UnityEngine;
78
using Verse;
@@ -34,6 +35,16 @@ enum Tab
3435

3536
private ServerSettings serverSettings;
3637

38+
public static void VerifyAndOpen(string path)
39+
{
40+
FileInfo fileInfo = new(path);
41+
var saveFile = SaveFile.ReadMpSave(fileInfo);
42+
ServerBrowser.CheckGameVersionAndMods(
43+
saveFile,
44+
() => { Find.WindowStack.Add(new HostWindow(saveFile) { returnToServerBrowser = false }); }
45+
);
46+
}
47+
3748
public HostWindow(SaveFile file = null)
3849
{
3950
closeOnAccept = false;

Source/Client/Windows/SaveFileReader.cs

Lines changed: 64 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using System.Linq;
88
using System.Threading.Tasks;
99
using System.Xml;
10+
using JetBrains.Annotations;
1011
using UnityEngine;
1112
using Verse;
1213

@@ -45,10 +46,7 @@ private void ReadSpSave(FileInfo file)
4546
{
4647
try
4748
{
48-
var saveFile = new SaveFile(Path.GetFileNameWithoutExtension(file.Name), false, file);
49-
using var stream = file.OpenRead();
50-
ReadSaveInfo(stream, saveFile);
51-
data[file] = saveFile;
49+
data[file] = SaveFile.ReadSpSave(file);
5250
}
5351
catch (Exception ex)
5452
{
@@ -60,57 +58,14 @@ private void ReadMpSave(FileInfo file)
6058
{
6159
try
6260
{
63-
var displayName = Path.ChangeExtension(Path.GetRelativePath(Multiplayer.ReplaysDir, file.FullName), null);
64-
var saveFile = new SaveFile(displayName, true, file);
65-
66-
var replay = Replay.ForLoading(file);
67-
if (!replay.LoadInfo()) return;
68-
69-
saveFile.gameName = replay.info.name;
70-
saveFile.protocol = replay.info.protocol;
71-
saveFile.replaySections = replay.info.sections.Count;
72-
73-
if (!replay.info.rwVersion.NullOrEmpty())
74-
{
75-
saveFile.rwVersion = replay.info.rwVersion;
76-
saveFile.modIds = replay.info.modIds.ToArray();
77-
saveFile.modNames = replay.info.modNames.ToArray();
78-
saveFile.asyncTime = replay.info.asyncTime;
79-
saveFile.multifaction = replay.info.multifaction;
80-
}
81-
else
82-
{
83-
using var zip = replay.OpenZipRead();
84-
using var stream = zip.GetEntry("world/000_save")!.Open();
85-
ReadSaveInfo(stream, saveFile);
86-
}
87-
88-
data[file] = saveFile;
61+
data[file] = SaveFile.ReadMpSave(file);
8962
}
9063
catch (Exception ex)
9164
{
9265
Log.Warning($"Exception loading replay info of {file.Name}: {ex}");
9366
}
9467
}
9568

96-
private void ReadSaveInfo(Stream stream, SaveFile save)
97-
{
98-
using var reader = new XmlTextReader(stream);
99-
reader.ReadToNextElement(); // savedGame
100-
reader.ReadToNextElement(); // meta
101-
102-
if (reader.Name != "meta") return;
103-
104-
reader.ReadToDescendant("gameVersion");
105-
save.rwVersion = VersionControl.VersionStringWithoutRev(reader.ReadString());
106-
107-
reader.ReadToNextSibling("modIds");
108-
save.modIds = reader.ReadStrings();
109-
110-
reader.ReadToNextSibling("modNames");
111-
save.modNames = reader.ReadStrings();
112-
}
113-
11469
public SaveFile GetData(FileInfo file)
11570
{
11671
data.TryGetValue(file, out var saveFile);
@@ -124,18 +79,18 @@ public void RemoveFile(FileInfo info)
12479
}
12580
}
12681

127-
public class SaveFile
82+
public class SaveFile(string displayName, bool replay, FileInfo file)
12883
{
129-
public string displayName;
130-
public bool replay;
84+
public string displayName = displayName;
85+
public bool replay = replay;
13186
public int replaySections;
132-
public FileInfo file;
87+
public FileInfo file = file;
13388

13489
public string gameName;
13590

13691
public string rwVersion;
137-
public string[] modNames = Array.Empty<string>();
138-
public string[] modIds = Array.Empty<string>();
92+
public string[] modNames = [];
93+
public string[] modIds = [];
13994

14095
public int protocol;
14196
public bool asyncTime;
@@ -164,11 +119,62 @@ public Color VersionColor
164119
}
165120
}
166121

167-
public SaveFile(string displayName, bool replay, FileInfo file)
122+
[CanBeNull]
123+
public static SaveFile ReadSpSave(FileInfo file)
124+
{
125+
var saveFile = new SaveFile(Path.GetFileNameWithoutExtension(file.Name), false, file);
126+
using var stream = file.OpenRead();
127+
ReadSaveInfo(stream, saveFile);
128+
return saveFile;
129+
}
130+
131+
[CanBeNull]
132+
public static SaveFile ReadMpSave(FileInfo file)
168133
{
169-
this.displayName = displayName;
170-
this.replay = replay;
171-
this.file = file;
134+
var displayName = Path.ChangeExtension(Path.GetRelativePath(Multiplayer.ReplaysDir, file.FullName), null);
135+
var saveFile = new SaveFile(displayName, true, file);
136+
137+
var replay = Replay.ForLoading(file);
138+
if (!replay.LoadInfo()) return null;
139+
140+
saveFile.gameName = replay.info.name;
141+
saveFile.protocol = replay.info.protocol;
142+
saveFile.replaySections = replay.info.sections.Count;
143+
144+
if (!replay.info.rwVersion.NullOrEmpty())
145+
{
146+
saveFile.rwVersion = replay.info.rwVersion;
147+
saveFile.modIds = replay.info.modIds.ToArray();
148+
saveFile.modNames = replay.info.modNames.ToArray();
149+
saveFile.asyncTime = replay.info.asyncTime;
150+
saveFile.multifaction = replay.info.multifaction;
151+
}
152+
else
153+
{
154+
using var zip = replay.OpenZipRead();
155+
using var stream = zip.GetEntry("world/000_save")!.Open();
156+
ReadSaveInfo(stream, saveFile);
157+
}
158+
159+
return saveFile;
160+
}
161+
162+
private static void ReadSaveInfo(Stream stream, SaveFile save)
163+
{
164+
using var reader = new XmlTextReader(stream);
165+
reader.ReadToNextElement(); // savedGame
166+
reader.ReadToNextElement(); // meta
167+
168+
if (reader.Name != "meta") return;
169+
170+
reader.ReadToDescendant("gameVersion");
171+
save.rwVersion = VersionControl.VersionStringWithoutRev(reader.ReadString());
172+
173+
reader.ReadToNextSibling("modIds");
174+
save.modIds = reader.ReadStrings();
175+
176+
reader.ReadToNextSibling("modNames");
177+
save.modNames = reader.ReadStrings();
172178
}
173179
}
174180
}

Source/Client/Windows/ServerBrowser.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ private void DrawFileButtons(SaveFile file, ref float width)
271271
width += 120;
272272
}
273273

274-
private static void CheckGameVersionAndMods(SaveFile file, Action action)
274+
public static void CheckGameVersionAndMods(SaveFile file, Action action)
275275
{
276276
ScribeMetaHeaderUtility.lastMode = ScribeMetaHeaderUtility.ScribeHeaderMode.Map;
277277
ScribeMetaHeaderUtility.loadedGameVersion = file.rwVersion;

0 commit comments

Comments
 (0)