77using System . Linq ;
88using System . Threading . Tasks ;
99using System . Xml ;
10+ using JetBrains . Annotations ;
1011using UnityEngine ;
1112using 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}
0 commit comments