@@ -7,31 +7,73 @@ namespace StansAssets.Plugins.Editor
77{
88 public static class GuidGenerator
99 {
10- public static void RegenerateGuid ( string assetPath )
10+ const int k_CountCharGiudField = 6 ;
11+
12+ public static void RegenerateGuid ( string assetPath )
1113 {
1214 try
1315 {
1416 var path = $ "{ assetPath } .meta";
1517 var metafile = File . ReadAllText ( path ) ;
16- var startGuid = metafile . IndexOf ( "guid:" ) + 6 ;
17- var endGuid = metafile . Substring ( startGuid ) . IndexOf ( "\n " ) ;
18- var oldGuid = metafile . Substring ( startGuid , endGuid ) ;
19- metafile = metafile . Replace ( oldGuid , Guid . NewGuid ( ) . ToString ( "N" ) ) ;
20- File . WriteAllText ( path , metafile ) ;
18+ var startGuid = metafile . IndexOf ( "guid:" ) ;
19+ if ( startGuid > 0 )
20+ {
21+ startGuid += k_CountCharGiudField ;
22+ var endGuid = metafile . Substring ( startGuid ) . IndexOf ( "\n " ) ;
23+ var oldGuid = metafile . Substring ( startGuid , endGuid ) ;
24+ metafile = metafile . Replace ( oldGuid , Guid . NewGuid ( ) . ToString ( "N" ) ) ;
25+ File . WriteAllText ( path , metafile ) ;
26+ }
27+ else
28+ {
29+ Debug . LogError ( "Does not contain guid in the metafile" ) ;
30+ }
2131 }
2232 catch ( Exception exception )
2333 {
2434 Debug . LogError ( exception . Message ) ;
2535 }
2636 }
2737
28- public static void RegenerateGuids ( IEnumerable < string > assetPaths )
38+ public static void RegenerateGuid ( IEnumerable < string > assetPaths )
2939 {
3040 foreach ( var assetPath in assetPaths )
3141 {
3242 RegenerateGuid ( assetPath ) ;
3343 }
44+ }
45+
46+ public static void RegenerateGuidsInFolder ( string folderPath , bool recursive = false , bool changeGuidFolder = true )
47+ {
48+ if ( Directory . Exists ( folderPath ) )
49+ ProcessDirectory ( folderPath , recursive ) ;
3450
51+ if ( changeGuidFolder )
52+ RegenerateGuid ( folderPath ) ;
53+ }
54+
55+ public static void RegenerateGuidsInFolder ( IEnumerable < string > folderPaths , bool recursive = false )
56+ {
57+ foreach ( var assetPath in folderPaths )
58+ {
59+ RegenerateGuidsInFolder ( assetPath , recursive ) ;
60+ }
61+ }
62+
63+ static void ProcessDirectory ( string targetDirectory , bool recursive = true )
64+ {
65+ string [ ] fileEntries = Directory . GetFiles ( targetDirectory ) ;
66+ foreach ( string fileName in fileEntries )
67+ if ( ! fileName . Contains ( ".meta" ) )
68+ RegenerateGuid ( fileName ) ;
69+
70+ string [ ] subdirectoryEntries = Directory . GetDirectories ( targetDirectory ) ;
71+ foreach ( string subdirectory in subdirectoryEntries )
72+ {
73+ RegenerateGuid ( subdirectory ) ;
74+ if ( recursive )
75+ ProcessDirectory ( subdirectory ) ;
76+ }
3577 }
3678 }
3779}
0 commit comments