|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.IO; |
| 4 | +using UnityEngine; |
| 5 | + |
| 6 | +namespace StansAssets.Plugins.Editor |
| 7 | +{ |
| 8 | + public static class GuidGenerator |
| 9 | + { |
| 10 | + const int k_CountCharGiudField = 6; |
| 11 | + |
| 12 | + public static void RegenerateGuid(string assetPath) |
| 13 | + { |
| 14 | + try |
| 15 | + { |
| 16 | + var path = $"{assetPath}.meta"; |
| 17 | + var metafile = File.ReadAllText(path); |
| 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 | + } |
| 31 | + } |
| 32 | + catch (Exception exception) |
| 33 | + { |
| 34 | + Debug.LogError(exception.Message); |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + public static void RegenerateGuid(IEnumerable<string> assetPaths) |
| 39 | + { |
| 40 | + foreach (var assetPath in assetPaths) |
| 41 | + { |
| 42 | + RegenerateGuid(assetPath); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + public static void RegenerateGuidsInFolder(string folderPath, bool recursive = false) |
| 47 | + { |
| 48 | + if (Directory.Exists(folderPath)) |
| 49 | + ProcessDirectory(folderPath, recursive); |
| 50 | + |
| 51 | + RegenerateGuid(folderPath); |
| 52 | + } |
| 53 | + |
| 54 | + public static void RegenerateGuidsInFolder(IEnumerable<string> folderPaths, bool recursive = false) |
| 55 | + { |
| 56 | + foreach (var assetPath in folderPaths) |
| 57 | + { |
| 58 | + RegenerateGuidsInFolder(assetPath, recursive); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + static void ProcessDirectory(string targetDirectory, bool recursive = true) |
| 63 | + { |
| 64 | + string[] fileEntries = Directory.GetFiles(targetDirectory); |
| 65 | + foreach (string fileName in fileEntries) |
| 66 | + if (!fileName.Contains(".meta")) |
| 67 | + RegenerateGuid(fileName); |
| 68 | + |
| 69 | + string[] subdirectoryEntries = Directory.GetDirectories(targetDirectory); |
| 70 | + foreach (string subdirectory in subdirectoryEntries) |
| 71 | + { |
| 72 | + RegenerateGuid(subdirectory); |
| 73 | + if (recursive) |
| 74 | + ProcessDirectory(subdirectory); |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | +} |
0 commit comments