@@ -318,5 +318,51 @@ public static string EnsureTrailingSlash(this string path)
318318 {
319319 return path . TrimEnd ( Path . DirectorySeparatorChar ) + Path . DirectorySeparatorChar ;
320320 }
321+
322+ /// <summary>
323+ /// Validates a directory, creating it if it doesn't exist
324+ /// </summary>
325+ /// <param name="path"></param>
326+ public static void ValidateDirectory ( string path )
327+ {
328+ if ( ! Directory . Exists ( path ) )
329+ {
330+ Directory . CreateDirectory ( path ) ;
331+ }
332+ }
333+
334+ /// <summary>
335+ /// Validates a data directory, synchronizing it by ensuring all files from a bundled source directory exist in it.
336+ /// If files are missing or outdated, they are copied from the bundled directory to the data directory.
337+ /// </summary>
338+ /// <param name="bundledDataDirectory"></param>
339+ /// <param name="dataDirectory"></param>
340+ public static void ValidateDataDirectory ( string bundledDataDirectory , string dataDirectory )
341+ {
342+ if ( ! Directory . Exists ( dataDirectory ) )
343+ {
344+ Directory . CreateDirectory ( dataDirectory ) ;
345+ }
346+
347+ foreach ( var bundledDataPath in Directory . GetFiles ( bundledDataDirectory ) )
348+ {
349+ var data = Path . GetFileName ( bundledDataPath ) ;
350+ if ( data == null ) continue ;
351+ var dataPath = Path . Combine ( dataDirectory , data ) ;
352+ if ( ! File . Exists ( dataPath ) )
353+ {
354+ File . Copy ( bundledDataPath , dataPath ) ;
355+ }
356+ else
357+ {
358+ var time1 = new FileInfo ( bundledDataPath ) . LastWriteTimeUtc ;
359+ var time2 = new FileInfo ( dataPath ) . LastWriteTimeUtc ;
360+ if ( time1 != time2 )
361+ {
362+ File . Copy ( bundledDataPath , dataPath , true ) ;
363+ }
364+ }
365+ }
366+ }
321367 }
322368}
0 commit comments