20
20
21
21
namespace AvaGui . Models
22
22
{
23
- public class VersionCheckBody
24
- {
25
- [ JsonPropertyName ( "tag_name" ) ]
26
- public string TagName { get ; set ; }
27
- }
28
-
29
- public class ObjectEditorModel : ReactiveObject // todo: only viewmodels should be reactive
23
+ public class ObjectEditorModel
30
24
{
31
25
public EditorSettings Settings { get ; private set ; }
32
26
@@ -36,8 +30,6 @@ public class ObjectEditorModel : ReactiveObject // todo: only viewmodels should
36
30
37
31
public HeaderIndex HeaderIndex { get ; private set ; } = [ ] ;
38
32
39
- public ObjectCache ObjectCache { get ; private set ; } = [ ] ;
40
-
41
33
public PaletteMap PaletteMap { get ; set ; }
42
34
43
35
public G1Dat ? G1 { get ; set ; }
@@ -134,73 +126,57 @@ public void SaveSettings()
134
126
File . WriteAllText ( SettingsFilePath , text ) ;
135
127
}
136
128
137
- public bool TryGetObject ( string path , out UiLocoFile ? uiLocoFile , bool reload = false )
129
+ public bool TryLoadObject ( string filename , out UiLocoFile ? uiLocoFile )
138
130
{
139
- if ( ObjectCache . TryGetValue ( path , out var obj ) && ! reload )
131
+ if ( string . IsNullOrEmpty ( filename ) )
140
132
{
141
- uiLocoFile = obj ;
142
- return true ;
133
+ uiLocoFile = null ;
134
+ return false ;
143
135
}
144
- else if ( File . Exists ( path ) )
136
+
137
+ ( var fileInfo , var locoObject ) = SawyerStreamReader . LoadFullObjectFromFile ( filename , logger : Logger ) ;
138
+
139
+ if ( locoObject == null )
145
140
{
146
- var loadResult = LoadSingleObjectFile ( path , HeaderIndex , ObjectCache , out var _ ) ;
147
- if ( loadResult )
148
- {
149
- uiLocoFile = ObjectCache [ path ] ;
150
- return true ;
151
- }
141
+ Logger ? . Error ( $ "Unable to load { filename } . FileInfo={ fileInfo } ") ;
142
+ uiLocoFile = null ;
143
+ return false ;
152
144
}
153
145
154
- uiLocoFile = null ;
155
- return false ;
146
+ uiLocoFile = new UiLocoFile ( ) { DatFileInfo = fileInfo , LocoObject = locoObject } ;
147
+ return true ;
156
148
}
157
149
158
150
// this method loads every single object entirely. it takes a long time to run
159
151
void CreateIndex ( string [ ] allFiles , IProgress < float > ? progress )
160
152
{
153
+ Logger ? . Info ( $ "Creating index on { allFiles . Length } files") ;
154
+
161
155
ConcurrentDictionary < string , IndexObjectHeader > ccHeaderIndex = new ( ) ; // key is full path/filename
162
- ConcurrentDictionary < string , UiLocoFile > ccObjectCache = new ( ) ; // key is full path/filename
163
156
164
157
var count = 0 ;
165
-
166
158
ConcurrentDictionary < string , TimeSpan > timePerFile = new ( ) ;
167
159
168
- Logger ? . Info ( $ "Creating index on { allFiles . Length } files") ;
169
160
var sw = new Stopwatch ( ) ;
170
161
sw . Start ( ) ;
171
162
172
- _ = Parallel . ForEach ( allFiles , new ParallelOptions ( ) { MaxDegreeOfParallelism = 16 } , ( file ) =>
173
- //foreach (var file in allFiles)
174
- {
175
- try
176
- {
177
- var startTime = sw . Elapsed ;
178
- _ = LoadSingleObjectFile ( file , ccHeaderIndex , ccObjectCache , out var fileInfo ) ;
179
- var elapsed = sw . Elapsed - startTime ;
180
-
181
- if ( fileInfo != null )
182
- {
183
- _ = timePerFile . TryAdd ( fileInfo . S5Header . Name , elapsed ) ;
184
- }
185
- }
186
- catch ( Exception ex )
187
- {
188
- Logger ? . Error ( $ "Failed to load \" { file } \" ", ex ) ;
163
+ var fileCount = allFiles . Length ;
164
+ var parallelise = false ;
189
165
190
- //var obj = SawyerStreamReader.LoadS5HeaderFromFile(file);
191
- //var indexObjectHeader = new IndexObjectHeader(obj.Name, obj.ObjectType, obj.SourceGame, obj.Checksum, null);
192
- //_ = ccHeaderIndex.TryAdd(file, indexObjectHeader);
193
- }
194
- finally
166
+ if ( parallelise )
167
+ {
168
+ _ = Parallel . ForEach ( allFiles , new ParallelOptions ( ) { MaxDegreeOfParallelism = 16 } , ( filename )
169
+ => count = LoadAndIndexFile ( count , filename ) ) ;
170
+ }
171
+ else
172
+ {
173
+ foreach ( var filename in allFiles )
195
174
{
196
- _ = Interlocked . Increment ( ref count ) ;
197
- progress ? . Report ( count / ( float ) allFiles . Length ) ;
175
+ count = LoadAndIndexFile ( count , filename ) ;
198
176
}
199
- //}
200
- } ) ;
177
+ }
201
178
202
179
HeaderIndex = ccHeaderIndex . OrderBy ( kvp => kvp . Key ) . ToDictionary ( kvp => kvp . Key , kvp => kvp . Value ) ;
203
- ObjectCache = ccObjectCache . OrderBy ( kvp => kvp . Key ) . ToDictionary ( kvp => kvp . Key , kvp => kvp . Value ) ;
204
180
205
181
sw . Stop ( ) ;
206
182
Logger ? . Info ( "Finished creating index" ) ;
@@ -219,39 +195,33 @@ void CreateIndex(string[] allFiles, IProgress<float>? progress)
219
195
220
196
var median = timePerFile . OrderBy ( x => x . Value ) . Skip ( timePerFile . Count / 2 ) . Take ( 1 ) . Single ( ) ;
221
197
Logger ? . Debug ( $ "Median time={ median . Value } ms") ;
222
- }
223
198
224
- private bool LoadSingleObjectFile ( string file , IDictionary < string , IndexObjectHeader > ccHeaderIndex , IDictionary < string , UiLocoFile > ccObjectCache , out DatFileInfo ? fileInfo )
225
- {
226
- ( fileInfo , var locoObject ) = SawyerStreamReader . LoadFullObjectFromFile ( file , logger : Logger ) ;
227
-
228
- if ( locoObject == null )
229
- {
230
- Logger ? . Error ( $ "Unable to load { file } . FileInfo={ fileInfo } ") ;
231
- return false ;
232
- }
233
-
234
- var newUiLocoFile = new UiLocoFile { DatFileInfo = fileInfo , LocoObject = locoObject } ;
235
- if ( ! ccObjectCache . TryAdd ( file , newUiLocoFile ) )
199
+ int LoadAndIndexFile ( int count , string filename )
236
200
{
237
- // replace the old file
238
- ccObjectCache [ file ] = newUiLocoFile ;
239
- }
201
+ var startTime = sw . Elapsed ;
202
+ var loadResult = TryLoadObject ( filename , out var uiLocoFile ) ;
203
+ var elapsed = sw . Elapsed - startTime ;
240
204
241
- VehicleType ? veh = null ;
242
- if ( locoObject . Object is VehicleObject vo )
243
- {
244
- veh = vo . Type ;
245
- }
205
+ if ( loadResult && uiLocoFile != null )
206
+ {
207
+ _ = ccHeaderIndex . TryAdd ( filename , new IndexObjectHeader (
208
+ uiLocoFile . DatFileInfo . S5Header . Name ,
209
+ uiLocoFile . DatFileInfo . S5Header . ObjectType ,
210
+ uiLocoFile . DatFileInfo . S5Header . SourceGame ,
211
+ uiLocoFile . DatFileInfo . S5Header . Checksum ,
212
+ uiLocoFile . LocoObject . Object is VehicleObject veh ? veh . Type : null ) ) ;
213
+
214
+ _ = timePerFile . TryAdd ( uiLocoFile . DatFileInfo . S5Header . Name , elapsed ) ;
215
+ }
216
+ else
217
+ {
218
+ Logger ? . Error ( $ "Failed to load \" { filename } \" ") ;
219
+ }
246
220
247
- var indexObjectHeader = new IndexObjectHeader ( fileInfo . S5Header . Name , fileInfo . S5Header . ObjectType , fileInfo . S5Header . SourceGame , fileInfo . S5Header . Checksum , veh ) ;
248
- if ( ! ccHeaderIndex . TryAdd ( file , indexObjectHeader ) )
249
- {
250
- // replace the old file
251
- ccHeaderIndex [ file ] = indexObjectHeader ;
221
+ _ = Interlocked . Increment ( ref count ) ;
222
+ progress ? . Report ( ( float ) count / fileCount ) ;
223
+ return count ;
252
224
}
253
-
254
- return true ;
255
225
}
256
226
257
227
public void SaveFile ( string path , UiLocoFile obj )
@@ -384,25 +354,5 @@ static void SerialiseHeaderIndexToFile(string filename, HeaderIndex headerIndex,
384
354
385
355
return JsonSerializer . Deserialize < HeaderIndex > ( json , GetOptions ( ) ) ?? [ ] ;
386
356
}
387
-
388
- public UiLocoFile ? LoadAndCacheObject ( string filename )
389
- {
390
- if ( string . IsNullOrEmpty ( filename ) || ! filename . EndsWith ( ".dat" , StringComparison . InvariantCultureIgnoreCase ) || ! File . Exists ( filename ) )
391
- {
392
- return null ;
393
- }
394
-
395
- if ( ObjectCache . TryGetValue ( filename , out var value ) )
396
- {
397
- return value ;
398
- }
399
- else
400
- {
401
- var obj = SawyerStreamReader . LoadFullObjectFromFile ( filename , logger : Logger ) ;
402
- var uiObj = new UiLocoFile { DatFileInfo = obj . DatFileInfo , LocoObject = obj . LocoObject } ;
403
- _ = ObjectCache . TryAdd ( filename , uiObj ) ;
404
- return uiObj ;
405
- }
406
- }
407
357
}
408
358
}
0 commit comments