A game about forced loneliness, made by TACStudios
at master 216 lines 7.8 kB view raw
1#if UNITY_EDITOR 2 3using System; 4using System.IO; 5using UnityEditor; 6 7namespace UnityEngine.InputSystem 8{ 9 /// Provides convenience functions for creating and managing assets for test purposes. 10 /// Note that all returned paths are converted to Unix paths when running on Windows 11 /// for consistency and to avoid mixed path names. 12 public static class AssetDatabaseUtils 13 { 14 private const string kAssetPath = "Assets"; 15 private const string kTestPath = "TestFiles"; 16 private const string kMetaExtension = ".meta"; 17 private const string kDefaultAssetExtension = "asset"; 18 19 // Perform an operation equivalent to a file delete operation outside of Unity Editor. 20 // Note that meta file is also removed to avoid generating warnings about non-clean delete. 21 public static void ExternalDeleteFileOrDirectory(string path) 22 { 23 FileUtil.DeleteFileOrDirectory(path); 24 FileUtil.DeleteFileOrDirectory(path + kMetaExtension); 25 } 26 27 // Perform an operation equivalent to a file move operation outside of Unity Editor. 28 // Note that meta file is also moved to avoid generating warnings about non-clean move. 29 public static void ExternalMoveFileOrDirectory(string source, string dest) 30 { 31 FileUtil.MoveFileOrDirectory(source, dest); 32 FileUtil.MoveFileOrDirectory(source + kMetaExtension, dest + kMetaExtension); 33 } 34 35 // Create an asset at the given path containing the given text content. 36 private static T CreateAssetAtPath<T>(string path, string content) where T : UnityEngine.Object 37 { 38 Debug.Assert(!File.Exists(path)); 39 40 T obj; 41 try 42 { 43 CreateDirectories(Path.GetDirectoryName(path)); 44 45 File.WriteAllText(path, content); 46 AssetDatabase.ImportAsset(path); 47 obj = AssetDatabase.LoadAssetAtPath<T>(path); 48 if (obj == null) 49 throw new Exception($"Failed to create asset at \"{path}\""); 50 } 51 catch (Exception) 52 { 53 AssetDatabase.DeleteAsset(path); 54 throw; 55 } 56 57 return obj; 58 } 59 60 private static string SanitizePath(string path) 61 { 62 return path?.Replace("\\", "/"); 63 } 64 65 private static void CreateRootDirectory() 66 { 67 CreateDirectories(RootPath()); 68 } 69 70 // Creates all directories (including intermediate) defined in path. 71 private static string CreateDirectories(string path) 72 { 73 if (Directory.Exists(path)) 74 return SanitizePath(path); 75 76 var parentFolder = kAssetPath; 77 path = path.Replace("\\", "/"); // Make sure we only get '/' separators. 78 var directories = path.Split('/'); 79 if (directories[0] != kAssetPath) 80 throw new ArgumentException(path); 81 for (var i = 1; i < directories.Length; ++i) 82 { 83 var guid = AssetDatabase.CreateFolder(parentFolder, directories[i]); 84 if (guid == string.Empty) 85 throw new Exception("Failed to create path \"" + path + "\""); 86 parentFolder = SanitizePath(Path.Combine(parentFolder, directories[i])); 87 } 88 89 AssetDatabase.Refresh(); 90 91 return SanitizePath(path); 92 } 93 94 // Creates a random test directory within asset folder that is automatically removed after test run. 95 public static string CreateDirectory() 96 { 97 return CreateDirectories(RandomDirectoryPath()); 98 } 99 100 // Creates an asset in the given directory path with an explicit or random file name containing the 101 // given content or the default content based on type. 102 public static T CreateAsset<T>(string directoryPath, string filename = null, string content = null) where T : UnityEngine.Object 103 { 104 Debug.Assert(directoryPath == null || directoryPath.Contains(RootPath())); 105 Debug.Assert(filename == null || !filename.Contains("/")); 106 107 if (directoryPath == null) 108 directoryPath = RootPath(); 109 string path; 110 if (filename != null) 111 { 112 path = SanitizePath(Path.Combine(directoryPath, filename)); 113 if (File.Exists(path)) 114 throw new Exception($"File already exists: {path}"); 115 } 116 else 117 { 118 path = RandomAssetFilePath(directoryPath, AssetFileExtensionFromType(typeof(T))); 119 } 120 121 return CreateAsset<T>(path: path, content: content); 122 } 123 124 // Creates an asset at the given path containing the specified content. 125 // If path is null, a unique random file name is assigned, if content is null the default content based 126 // on type (extension) is used. 127 public static T CreateAsset<T>(string path = null, string content = null) where T : UnityEngine.Object 128 { 129 if (path == null) 130 path = RandomAssetFilePath(RootPath(), AssetFileExtensionFromType(typeof(T))); 131 if (content == null) 132 content = DefaultContentFromType(typeof(T)); 133 return CreateAssetAtPath<T>(path, content); 134 } 135 136 public static void Restore() 137 { 138 var root = RootPath(); 139 140 // Delete all files in test folder 141 if (!Directory.Exists(root)) 142 return; 143 144 foreach (var asset in AssetDatabase.FindAssets("", new string[] { root })) 145 { 146 var path = AssetDatabase.GUIDToAssetPath(asset); 147 AssetDatabase.DeleteAsset(path); 148 } 149 150 AssetDatabase.DeleteAsset(root); 151 } 152 153 private static string RandomName() 154 { 155 const double scale = int.MaxValue; 156 double r = UnityEngine.Random.value; 157 return "Test_" + (int)(Math.Floor(r * scale)); 158 } 159 160 private static string RandomAssetFilePath<T>(string directoryPath = null) 161 { 162 return RandomAssetFilePath(directoryPath, AssetFileExtensionFromType(typeof(T))); 163 } 164 165 private static string RandomAssetFilePath(string directoryPath = null, string extension = null) 166 { 167 // Default to using test files root path 168 if (directoryPath == null) 169 directoryPath = RootPath(); 170 171 // Default to default extension 172 if (extension == null) 173 extension = kDefaultAssetExtension; 174 175 string path; 176 do 177 { 178 path = SanitizePath(Path.Combine(directoryPath, RandomName() + "." + extension)); // EDIT 179 } 180 while (File.Exists(path)); 181 return path; 182 } 183 184 private static string RootPath() 185 { 186 return SanitizePath(Path.Combine(kAssetPath, kTestPath)); 187 } 188 189 public static string RandomDirectoryPath() 190 { 191 string path; 192 do 193 { 194 path = Path.Combine(RootPath(), RandomName()); 195 } 196 while (File.Exists(path)); 197 return SanitizePath(path); 198 } 199 200 private static string AssetFileExtensionFromType(Type type) 201 { 202 if (type == typeof(InputActionAsset)) 203 return InputActionAsset.Extension; 204 return kDefaultAssetExtension; 205 } 206 207 private static string DefaultContentFromType(Type type) 208 { 209 if (type == typeof(InputActionAsset)) 210 return "{}"; 211 return string.Empty; 212 } 213 } 214} 215 216#endif // UNITY_EDITOR