| | | 1 | | using System; |
| | | 2 | | using Microsoft.CodeAnalysis; |
| | | 3 | | |
| | | 4 | | namespace Hertzole.ScriptableValues.Generator; |
| | | 5 | | |
| | | 6 | | internal enum ScriptableType |
| | | 7 | | { |
| | | 8 | | None = 0, |
| | | 9 | | Event = 1, |
| | | 10 | | GenericEvent = 2, |
| | | 11 | | Value = 3, |
| | | 12 | | Pool = 4, |
| | | 13 | | List = 5, |
| | | 14 | | Dictionary = 6 |
| | | 15 | | } |
| | | 16 | | |
| | | 17 | | internal static class ScriptableValueHelper |
| | | 18 | | { |
| | | 19 | | public static bool TryGetScriptableType(in SemanticModel semanticModel, |
| | | 20 | | INamedTypeSymbol? type, |
| | | 21 | | out ScriptableType scriptableType, |
| | | 22 | | out ITypeSymbol? genericType) |
| | | 23 | | { |
| | 25 | 24 | | genericType = null; |
| | | 25 | | |
| | 25 | 26 | | if (type != null) |
| | | 27 | | { |
| | 25 | 28 | | if (TryGetScriptableTypeFromType(in semanticModel, type, out scriptableType, out genericType)) |
| | | 29 | | { |
| | 25 | 30 | | return true; |
| | | 31 | | } |
| | | 32 | | |
| | 0 | 33 | | if (TryGetScriptableTypeFromAddressable(in semanticModel, type, out scriptableType, out genericType)) |
| | | 34 | | { |
| | 0 | 35 | | return true; |
| | | 36 | | } |
| | | 37 | | } |
| | | 38 | | |
| | 0 | 39 | | scriptableType = ScriptableType.None; |
| | 0 | 40 | | return false; |
| | | 41 | | } |
| | | 42 | | |
| | | 43 | | private static bool TryGetScriptableTypeFromType(in SemanticModel semanticModel, |
| | | 44 | | INamedTypeSymbol type, |
| | | 45 | | out ScriptableType scriptableType, |
| | | 46 | | out ITypeSymbol? genericType) |
| | | 47 | | { |
| | 50 | 48 | | if (type.ConstructedFrom.StringEquals(Types.SCRIPTABLE_VALUE) || |
| | 50 | 49 | | type.ConstructedFrom.StringEquals(Types.VALUE_REFERENCE)) |
| | | 50 | | { |
| | 25 | 51 | | genericType = type.TypeArguments[0]; |
| | 25 | 52 | | scriptableType = ScriptableType.Value; |
| | 25 | 53 | | return true; |
| | | 54 | | } |
| | | 55 | | |
| | 25 | 56 | | if (type.ConstructedFrom.StringEquals(Types.SCRIPTABLE_EVENT)) |
| | | 57 | | { |
| | 0 | 58 | | genericType = null; |
| | 0 | 59 | | scriptableType = ScriptableType.Event; |
| | 0 | 60 | | return true; |
| | | 61 | | } |
| | | 62 | | |
| | 25 | 63 | | if (type.ConstructedFrom.StringEquals(Types.GENERIC_SCRIPTABLE_EVENT)) |
| | | 64 | | { |
| | 0 | 65 | | genericType = type.TypeArguments[0]; |
| | 0 | 66 | | scriptableType = ScriptableType.GenericEvent; |
| | 0 | 67 | | return true; |
| | | 68 | | } |
| | | 69 | | |
| | 25 | 70 | | if (type.ConstructedFrom.StringEquals(Types.SCRIPTABLE_POOL)) |
| | | 71 | | { |
| | 0 | 72 | | genericType = type.TypeArguments[0]; |
| | 0 | 73 | | scriptableType = ScriptableType.Pool; |
| | 0 | 74 | | return true; |
| | | 75 | | } |
| | | 76 | | |
| | 25 | 77 | | if (type.ConstructedFrom.StringEquals(Types.SCRIPTABLE_LIST)) |
| | | 78 | | { |
| | 0 | 79 | | genericType = type.TypeArguments[0]; |
| | 0 | 80 | | scriptableType = ScriptableType.List; |
| | 0 | 81 | | return true; |
| | | 82 | | } |
| | | 83 | | |
| | 25 | 84 | | if (type.ConstructedFrom.StringEquals(Types.SCRIPTABLE_DICTIONARY)) |
| | | 85 | | { |
| | | 86 | | // Get the key and value types from ScriptableDictionary<TKey, TValue> |
| | 0 | 87 | | ITypeSymbol keyType = type.TypeArguments[0]; |
| | 0 | 88 | | ITypeSymbol valueType = type.TypeArguments[1]; |
| | | 89 | | |
| | | 90 | | // Get the KeyValuePair<,> generic type definition |
| | 0 | 91 | | INamedTypeSymbol? keyValuePairType = semanticModel.Compilation.GetTypeByMetadataName("System.Collections.Gen |
| | | 92 | | |
| | 0 | 93 | | if (keyValuePairType != null) |
| | | 94 | | { |
| | | 95 | | // Construct the KeyValuePair<TKey, TValue> with the specific type arguments |
| | 0 | 96 | | genericType = keyValuePairType.Construct(keyType, valueType); |
| | 0 | 97 | | scriptableType = ScriptableType.Dictionary; |
| | 0 | 98 | | return true; |
| | | 99 | | } |
| | | 100 | | } |
| | | 101 | | |
| | 25 | 102 | | if (type.BaseType != null) |
| | | 103 | | { |
| | 25 | 104 | | return TryGetScriptableTypeFromType(in semanticModel, type.BaseType, out scriptableType, out genericType); |
| | | 105 | | } |
| | | 106 | | |
| | 0 | 107 | | scriptableType = ScriptableType.None; |
| | 0 | 108 | | genericType = null; |
| | 0 | 109 | | return false; |
| | | 110 | | } |
| | | 111 | | |
| | | 112 | | private static bool TryGetScriptableTypeFromAddressable(in SemanticModel semanticModel, |
| | | 113 | | INamedTypeSymbol type, |
| | | 114 | | out ScriptableType scriptableType, |
| | | 115 | | out ITypeSymbol? genericType) |
| | | 116 | | { |
| | | 117 | | while (true) |
| | | 118 | | { |
| | 0 | 119 | | if (type.ConstructedFrom.StringEquals(Types.ASSET_REFERENCE_T)) |
| | | 120 | | { |
| | 0 | 121 | | if (type.TypeArguments[0] is not INamedTypeSymbol namedTypeSymbol) |
| | | 122 | | { |
| | 0 | 123 | | scriptableType = ScriptableType.None; |
| | 0 | 124 | | genericType = null; |
| | 0 | 125 | | return false; |
| | | 126 | | } |
| | | 127 | | |
| | 0 | 128 | | return TryGetScriptableTypeFromType(in semanticModel, namedTypeSymbol, out scriptableType, out genericTy |
| | | 129 | | } |
| | | 130 | | |
| | 0 | 131 | | if (type.BaseType != null) |
| | | 132 | | { |
| | 0 | 133 | | type = type.BaseType; |
| | 0 | 134 | | continue; |
| | | 135 | | } |
| | | 136 | | |
| | 0 | 137 | | scriptableType = ScriptableType.None; |
| | 0 | 138 | | genericType = null; |
| | 0 | 139 | | return false; |
| | | 140 | | } |
| | | 141 | | } |
| | | 142 | | |
| | | 143 | | public static bool IsSupportedType(in CallbackType callbackType, in ScriptableType scriptableType) |
| | | 144 | | { |
| | 0 | 145 | | switch (callbackType) |
| | | 146 | | { |
| | | 147 | | case CallbackType.Value: |
| | 0 | 148 | | return scriptableType == ScriptableType.Value; |
| | | 149 | | case CallbackType.Event: |
| | 0 | 150 | | return scriptableType == ScriptableType.Event || scriptableType == ScriptableType.GenericEvent; |
| | | 151 | | case CallbackType.Collection: |
| | 0 | 152 | | return scriptableType == ScriptableType.List || scriptableType == ScriptableType.Dictionary; |
| | | 153 | | case CallbackType.Pool: |
| | 0 | 154 | | return scriptableType == ScriptableType.Pool; |
| | | 155 | | default: |
| | 0 | 156 | | throw new ArgumentOutOfRangeException(); |
| | | 157 | | } |
| | | 158 | | } |
| | | 159 | | |
| | | 160 | | public static bool DoesTypeSupportCallbacks(in ITypeSymbol symbol, out NotSupportedReason reason) |
| | | 161 | | { |
| | 25 | 162 | | if (symbol.IsValueType && symbol.IsReadOnly) |
| | | 163 | | { |
| | 0 | 164 | | reason = NotSupportedReason.ReadOnlyStruct; |
| | 0 | 165 | | return false; |
| | | 166 | | } |
| | | 167 | | |
| | 25 | 168 | | if (symbol.IsStatic) |
| | | 169 | | { |
| | 1 | 170 | | reason = NotSupportedReason.StaticClass; |
| | 1 | 171 | | return false; |
| | | 172 | | } |
| | | 173 | | |
| | 24 | 174 | | reason = NotSupportedReason.None; |
| | 24 | 175 | | return true; |
| | | 176 | | } |
| | | 177 | | } |
| | | 178 | | |
| | | 179 | | public enum NotSupportedReason |
| | | 180 | | { |
| | | 181 | | None = 0, |
| | | 182 | | ReadOnlyStruct = 1, |
| | | 183 | | StaticClass = 2 |
| | | 184 | | } |