55using System ;
66using System . Collections . Concurrent ;
77using System . Collections . Generic ;
8- using System . Diagnostics ;
98using System . Dynamic ;
109using System . Globalization ;
11- using System . IO ;
1210using System . Linq ;
1311using System . Threading ;
1412using System . Threading . Tasks ;
@@ -71,7 +69,7 @@ public DataStore(string path, bool useLowerCamelCase = true, string keyProperty
7169 _decryptJson = ( json => json ) ;
7270 }
7371
74- _jsonData = JObject . Parse ( ReadJsonFromFile ( path ) ) ;
72+ _jsonData = GetJsonObjectFromFile ( ) ;
7573
7674 // Run updates on a background thread and use BlockingCollection to prevent multiple updates to run simultaneously
7775 Task . Run ( ( ) =>
@@ -103,8 +101,8 @@ public DataStore(string path, bool useLowerCamelCase = true, string keyProperty
103101 // Ignore this and exit
104102 break ;
105103 }
106-
107- var jsonText = ReadJsonFromFile ( _filePath ) ;
104+
105+ var jsonText = GetJsonTextFromFile ( ) ;
108106
109107 foreach ( var action in batch )
110108 {
@@ -121,7 +119,7 @@ public DataStore(string path, bool useLowerCamelCase = true, string keyProperty
121119
122120 try
123121 {
124- writeSuccess = WriteJsonToFile ( _filePath , jsonText ) ;
122+ writeSuccess = FileAccess . WriteJsonToFile ( _filePath , _encryptJson , jsonText ) ;
125123
126124 lock ( _jsonData )
127125 {
@@ -165,14 +163,14 @@ public void UpdateAll(string jsonData)
165163 _jsonData = JObject . Parse ( jsonData ) ;
166164 }
167165
168- WriteJsonToFile ( _filePath , jsonData ) ;
166+ FileAccess . WriteJsonToFile ( _filePath , _encryptJson , jsonData ) ;
169167 }
170168
171169 public void Reload ( )
172170 {
173171 lock ( _jsonData )
174172 {
175- _jsonData = JObject . Parse ( ReadJsonFromFile ( _filePath ) ) ;
173+ _jsonData = GetJsonObjectFromFile ( ) ;
176174 }
177175 }
178176
@@ -181,7 +179,7 @@ public T GetItem<T>(string key)
181179 if ( _reloadBeforeGetCollection )
182180 {
183181 // This might be a bad idea especially if the file is in use, as this can take a long time
184- _jsonData = JObject . Parse ( ReadJsonFromFile ( _filePath ) ) ;
182+ _jsonData = GetJsonObjectFromFile ( ) ;
185183 }
186184
187185 var token = _jsonData [ key ] ;
@@ -204,7 +202,7 @@ public dynamic GetItem(string key)
204202 if ( _reloadBeforeGetCollection )
205203 {
206204 // This might be a bad idea especially if the file is in use, as this can take a long time
207- _jsonData = JObject . Parse ( ReadJsonFromFile ( _filePath ) ) ;
205+ _jsonData = GetJsonObjectFromFile ( ) ;
208206 }
209207
210208 var token = _jsonData [ key ] ;
@@ -358,7 +356,7 @@ private IDocumentCollection<T> GetCollection<T>(string path, Func<JToken, T> rea
358356 if ( _reloadBeforeGetCollection )
359357 {
360358 // This might be a bad idea especially if the file is in use, as this can take a long time
361- _jsonData = JObject . Parse ( ReadJsonFromFile ( _filePath ) ) ;
359+ _jsonData = GetJsonObjectFromFile ( ) ;
362360 }
363361
364362 return _jsonData [ path ] ?
@@ -469,59 +467,9 @@ private dynamic SingleDynamicItemReadConverter(JToken e)
469467 }
470468 }
471469
472- private string ReadJsonFromFile ( string path )
473- {
474- Stopwatch sw = null ;
475- string json = "{}" ;
476-
477- while ( true )
478- {
479- try
480- {
481- json = File . ReadAllText ( path ) ;
482- break ;
483- }
484- catch ( FileNotFoundException )
485- {
486- json = _encryptJson ( json ) ;
487- File . WriteAllText ( path , json ) ;
488- break ;
489- }
490- catch ( IOException e ) when ( e . Message . Contains ( "because it is being used by another process" ) )
491- {
492- // If some other process is using this file, retry operation unless elapsed times is greater than 10sec
493- sw = sw ?? Stopwatch . StartNew ( ) ;
494- if ( sw . ElapsedMilliseconds > 10000 )
495- throw ;
496- }
497- }
470+ private string GetJsonTextFromFile ( ) => FileAccess . ReadJsonFromFile ( _filePath , _encryptJson , _decryptJson ) ;
498471
499- return _decryptJson ( json ) ;
500- }
501-
502- private bool WriteJsonToFile ( string path , string content )
503- {
504- Stopwatch sw = null ;
505- while ( true )
506- {
507- try
508- {
509- File . WriteAllText ( path , _encryptJson ( content ) ) ;
510- return true ;
511- }
512- catch ( IOException e ) when ( e . Message . Contains ( "because it is being used by another process" ) )
513- {
514- // If some other process is using this file, retry operation unless elapsed times is greater than 10sec
515- sw = sw ?? Stopwatch . StartNew ( ) ;
516- if ( sw . ElapsedMilliseconds > 10000 )
517- return false ;
518- }
519- catch ( Exception )
520- {
521- return false ;
522- }
523- }
524- }
472+ private JObject GetJsonObjectFromFile ( ) => JObject . Parse ( GetJsonTextFromFile ( ) ) ;
525473
526474 private class CommitAction
527475 {
0 commit comments