CodeSmile AssetDatabase 1.9
Unity's AssetDatabase in enjoyable, consistent, concise, convenient, comprehensible, safe, documented form.
Loading...
Searching...
No Matches
Asset.ThrowIf.cs
1// Copyright (C) 2021-2023 Steffen Itterheim
2// Refer to included LICENSE file for terms and conditions.
3
4using System;
5using System.IO;
6using UnityEditor;
7using UnityEngine;
8using Object = System.Object;
9
10namespace CodeSmileEditor
11{
12 public sealed partial class Asset
13 {
17 internal static class ThrowIf
18 {
19 public static void ArgumentIsNull(Object obj, String argName)
20 {
21 if (obj == null)
22 throw new ArgumentNullException(argName);
23 }
24
25 public static void DoesNotExistInFileSystem(Path path)
26 {
27 if (path.ExistsInFileSystem == false)
28 throw new FileNotFoundException($"path does not exist: '{path}'");
29 }
30
31 public static void AlreadyAnAsset(UnityEngine.Object obj)
32 {
33 if (Status.IsImported(obj))
34 throw new ArgumentException($"object already is an asset file: {obj}");
35 }
36
37 public static void AssetPathNotInDatabase(Path path)
38 {
39 if (path.Exists == false)
40 throw new ArgumentException($"path does not exist or not imported: {path}");
41 }
42
43 public static void NotInDatabase(UnityEngine.Object obj)
44 {
45 if (Status.IsImported(obj) == false)
46 throw new ArgumentException($"object is not an asset: {obj}");
47 }
48
49 public static void NotAnAssetGuid(GUID guid)
50 {
51 if (Path.Get(guid) == null)
52 throw new ArgumentException($"guid is not an asset: {guid}");
53 }
54
55 /*
56 public static void AssetDeleted(Asset asset)
57 {
58 if (asset.IsDeleted)
59 throw new InvalidOperationException("asset has been deleted");
60 }
61
62 public static void AssetNotImported(Path path, Type assetType)
63 {
64 if (assetType == null)
65 {
66 // path exists in file system yet ADB does not know its type - missing Import() ?
67 throw new AssetLoadException("cannot load asset - file exists but asset type is null, " +
68 $"most likely the asset has not been imported; path: {path}");
69 }
70 }
71
72 public static void AssetTypeMismatch<T>(Path path, Type assetType) where T : UnityEngine.Object
73 {
74 if (typeof(T).IsAssignableFrom(assetType) == false)
75 {
76 // types just don't match
77 throw new AssetLoadException($"cannot load asset, type mismatch: {typeof(T)} " +
78 $"not assignable from asset type: {assetType.FullName}; path: {path}");
79 }
80 }
81 */
82
83 public static void AssetLoadReturnedNull(UnityEngine.Object obj, Path path)
84 {
85 if (obj == null)
86 {
87 // path exists + type is known, yet load throws null?
88 // Probably ADB or asset in invalid state ...
89 throw new AssetLoadException("asset load returned null - this can occur if the AssetDatabase " +
90 "is currently initializing (eg static ctor) or when importing an asset " +
91 "async or while ADB is 'paused', or if the type does not math, or " +
92 $"some other reason (please report); path: {path}");
93 }
94 }
95
96 public static void PathIsNotValid(String path)
97 {
98 if (Path.IsValid(path) == false)
99 throw new ArgumentException($"invalid path: {GetLastErrorMessage()}");
100 }
101
102 public static void NotAProjectPath(String fullPath)
103 {
104 var rootPath = Path.FullProjectPath;
105 if (fullPath.StartsWith(rootPath) == false)
106 {
107 throw new ArgumentException(
108 $"invalid relative or project path: '{fullPath}' - relative paths must start with 'Assets', full paths must include the project's root directory");
109 }
110 }
111
112 public static void NullOrWhitespace(String param, String paramName)
113 {
114 if (param == null)
115 throw new ArgumentNullException($"{paramName} is null");
116 if (String.IsNullOrWhiteSpace(param))
117 throw new ArgumentException($"{paramName} is empty or whitespace");
118 }
119
120 public static void ContainsPathSeparators(String fileName, String paramName)
121 {
122 var normalized = fileName.ToForwardSlashes();
123 if (normalized.Contains('/'))
124 throw new ArgumentException($"filename contains path separators: '{fileName}'", paramName);
125 }
126
127 public static void SubObjectIsGameObject(UnityEngine.Object subObject)
128 {
129 if (subObject is GameObject go)
130 throw new ArgumentException($"sub assets must not be of type GameObject: {subObject}");
131 }
132
133 public static void ExtensionIsNotUnityPackage(Path path)
134 {
135 if (path.Extension.ToLower().Equals(".unitypackage") == false)
136 throw new ArgumentException($"file does not have .unitypackage extension: {path}");
137 }
138
139 public static void SourceAndDestPathAreEqual(Path sourcePath, Path destinationPath)
140 {
141 if (sourcePath.Equals(destinationPath))
142 throw new ArgumentException($"source and destination path are equal: {sourcePath}");
143 }
144
145 /*public static void NotAnAsset(UnityEngine.Object obj)
146 {
147 if (Database.Contains(obj) == false)
148 throw new ArgumentException($"{obj} is not an asset file");
149 }
150
151 public static void NotAnAsset(Int32 instanceId)
152 {
153 if (Database.Contains(instanceId) == false)
154 throw new ArgumentException($"{instanceId} is not an asset instance ID");
155 }*/
156 }
157 }
158}