11#nullable enable
22using System ;
33using System . Collections . Generic ;
4+ using System . IO ;
45using System . Linq ;
56using System . Threading . Tasks ;
67using Newtonsoft . Json ;
@@ -12,12 +13,15 @@ namespace NuclearBand
1213{
1314 public static class SODatabase
1415 {
16+ public static string SavePath => Application . persistentDataPath + @"/save.txt" ;
17+
1518 private static FolderHolder root = null ! ;
1619
1720 public static async void Init ( Action < float > ? onProgress , Action ? onComplete )
1821 {
1922 await InitAsync ( onProgress , onComplete ) ;
2023 }
24+
2125 public static async Task InitAsync ( Action < float > ? onProgress , Action ? onComplete )
2226 {
2327 var loadHandler = Addressables . LoadResourceLocationsAsync ( SODatabaseSettings . Label ) ;
@@ -26,16 +30,11 @@ public static async Task InitAsync(Action<float>? onProgress, Action? onComplete
2630 {
2731 while ( ! loadHandler . IsDone )
2832 {
29- CallAction ( ( ) =>
30- {
31- onProgress ? . Invoke ( loadHandler . PercentComplete ) ;
32- } ) ;
33+ CallAction ( ( ) => { onProgress ? . Invoke ( loadHandler . PercentComplete ) ; } ) ;
3334 await Task . Delay ( 50 ) ;
3435 }
35- CallAction ( ( ) =>
36- {
37- onProgress ? . Invoke ( loadHandler . PercentComplete ) ;
38- } ) ;
36+
37+ CallAction ( ( ) => { onProgress ? . Invoke ( loadHandler . PercentComplete ) ; } ) ;
3938 } ) ;
4039#pragma warning restore 4014
4140 var resourceLocations = await loadHandler . Task ;
@@ -67,6 +66,7 @@ public static async Task InitAsync(Action<float>? onProgress, Action? onComplete
6766 static async void CallAction ( Action ? action )
6867 {
6968 action ? . Invoke ( ) ;
69+ await Task . CompletedTask ;
7070 }
7171
7272 public static T GetModel < T > ( string path ) where T : DataNode
@@ -87,14 +87,10 @@ public static List<T> GetModels<T>(string path, bool includeSubFolders = false)
8787 if ( path != string . Empty )
8888 for ( var i = 0 ; i < pathElements . Length ; i ++ )
8989 {
90- try
91- {
90+ if ( curFolder . FolderHolders . ContainsKey ( pathElements [ i ] ) )
9291 curFolder = curFolder . FolderHolders [ pathElements [ i ] ] ;
93- }
94- catch ( Exception e )
95- {
96-
97- }
92+ else
93+ break ;
9894 }
9995
10096 var res = curFolder . DataNodes . Values . OfType < T > ( ) . ToList ( ) ;
@@ -111,12 +107,16 @@ public static List<T> GetModels<T>(string path, bool includeSubFolders = false)
111107 return res ;
112108 }
113109
114- public static void Save ( )
110+ public static async void Save ( )
111+ {
112+ await SaveAsync ( ) ;
113+ }
114+
115+ public static async Task SaveAsync ( )
115116 {
116117 var res = SaveFolderHolder ( root , string . Empty ) ;
117- foreach ( var pair in res )
118- PlayerPrefs . SetString ( pair . Key , pair . Value ) ;
119- PlayerPrefs . Save ( ) ;
118+ using var fileStream = new StreamWriter ( SavePath ) ;
119+ await fileStream . WriteAsync ( JsonConvert . SerializeObject ( res ) ) ;
120120 }
121121
122122 static Dictionary < string , string > SaveFolderHolder ( FolderHolder folderHolder , string path )
@@ -143,19 +143,31 @@ static Dictionary<string, string> SaveFolderHolder(FolderHolder folderHolder, st
143143 return res ;
144144 }
145145
146- public static void Load ( )
146+ public static async void Load ( )
147147 {
148- LoadFolderHolder ( root , string . Empty ) ;
148+ await LoadAsync ( ) ;
149149 }
150150
151- static void LoadFolderHolder ( FolderHolder folderHolder , string path )
151+ public static async Task LoadAsync ( )
152+ {
153+ if ( ! File . Exists ( SavePath ) )
154+ return ;
155+
156+ using var fileStream = new StreamReader ( SavePath ) ;
157+ var serializedDictionary = await fileStream . ReadToEndAsync ( ) ;
158+ var dict = JsonConvert . DeserializeObject < Dictionary < string , string > > ( serializedDictionary ) ;
159+ LoadFolderHolder ( root , string . Empty , dict ) ;
160+ }
161+
162+
163+ static void LoadFolderHolder ( FolderHolder folderHolder , string path , Dictionary < string , string > data )
152164 {
153165 foreach ( var dataNodePair in folderHolder . DataNodes )
154166 {
155167 var fullPath = dataNodePair . Key ;
156168 if ( ! string . IsNullOrEmpty ( path ) )
157169 fullPath = path + '/' + fullPath ;
158- var json = PlayerPrefs . GetString ( fullPath ) ;
170+ var json = data . ContainsKey ( fullPath ) ? data [ fullPath ] : string . Empty ;
159171 if ( string . IsNullOrEmpty ( json ) )
160172 continue ;
161173 JsonConvert . PopulateObject ( json , dataNodePair . Value ) ;
@@ -166,7 +178,7 @@ static void LoadFolderHolder(FolderHolder folderHolder, string path)
166178 var fullPath = folderHolderPair . Key ;
167179 if ( ! string . IsNullOrEmpty ( path ) )
168180 fullPath = path + '/' + fullPath ;
169- LoadFolderHolder ( folderHolderPair . Value , fullPath ) ;
181+ LoadFolderHolder ( folderHolderPair . Value , fullPath , data ) ;
170182 }
171183 }
172184 }
0 commit comments