2
2
using OpenLoco . Dat . FileParsing ;
3
3
using OpenLoco . Dat . Objects ;
4
4
using System . Collections . Concurrent ;
5
+ using System . Text ;
5
6
using System . Text . Json ;
6
7
7
8
namespace Dat
8
9
{
9
10
public class ObjectIndex
10
11
{
11
- public const int JsonVersion = 1 ; // change this every time this format changes
12
- public int Version => JsonVersion ;
13
-
14
12
public required IList < ObjectIndexEntry > Objects { get ; set ; } = [ ] ;
15
13
16
14
public required IList < ObjectIndexFailedEntry > ObjectsFailed { get ; set ; } = [ ] ;
@@ -63,13 +61,24 @@ public void SaveIndex(string indexFile)
63
61
public void SaveIndex ( string indexFile , JsonSerializerOptions options )
64
62
=> File . WriteAllText ( indexFile , JsonSerializer . Serialize ( this , options ) ) ;
65
63
66
- public static ObjectIndex LoadIndex ( string indexFile )
64
+ public static ObjectIndex ? LoadIndex ( string indexFile )
67
65
=> JsonSerializer . Deserialize < ObjectIndex > ( File . ReadAllText ( indexFile ) ) ;
68
66
69
- public static ObjectIndex LoadIndex ( string indexFile , JsonSerializerOptions options )
67
+ public static ObjectIndex ? LoadIndex ( string indexFile , JsonSerializerOptions options )
70
68
=> JsonSerializer . Deserialize < ObjectIndex > ( File . ReadAllText ( indexFile ) , options ) ;
71
69
72
- public static ObjectIndex LoadOrCreateIndex ( string directory )
70
+ public static async Task < ObjectIndex ? > LoadIndexAsync ( string indexFile )
71
+ {
72
+ await using ( var stream = new MemoryStream ( Encoding . UTF8 . GetBytes ( await File . ReadAllTextAsync ( indexFile ) ) ) )
73
+ {
74
+ return await JsonSerializer . DeserializeAsync < ObjectIndex > ( stream ) ;
75
+ }
76
+ }
77
+
78
+ public static ObjectIndex ? LoadOrCreateIndex ( string directory )
79
+ => LoadOrCreateIndexAsync ( directory ) . Result ;
80
+
81
+ public static async Task < ObjectIndex ? > LoadOrCreateIndexAsync ( string directory )
73
82
{
74
83
var indexPath = Path . Combine ( directory , "objectIndex.json" ) ;
75
84
ObjectIndex ? index ;
@@ -80,7 +89,7 @@ public static ObjectIndex LoadOrCreateIndex(string directory)
80
89
else
81
90
{
82
91
var fileArr = SawyerStreamUtils . GetDatFilesInDirectory ( directory ) . ToArray ( ) ;
83
- index = CreateIndexAsync ( directory , fileArr , null ) . Result ;
92
+ index = await CreateIndexAsync ( directory , fileArr , null ) ;
84
93
index . SaveIndex ( indexPath ) ;
85
94
}
86
95
0 commit comments