| | 1 | | // Copyright (c) 2020-2024 dotBunny Inc. |
| | 2 | | // dotBunny licenses this file to you under the BSL-1.0 license. |
| | 3 | | // See the LICENSE file in the project root for more information. |
| | 4 | |
|
| | 5 | | using System.Runtime.CompilerServices; |
| | 6 | |
|
| | 7 | | namespace GDX |
| | 8 | | { |
| | 9 | | public static class Serializable |
| | 10 | | { |
| | 11 | | public enum SerializableTypes |
| | 12 | | { |
| | 13 | | Invalid = -1, |
| | 14 | | String, |
| | 15 | | Char, |
| | 16 | | Bool, |
| | 17 | | SByte, |
| | 18 | | Byte, |
| | 19 | | Short, |
| | 20 | | UShort, |
| | 21 | | Int, |
| | 22 | | UInt, |
| | 23 | | Long, |
| | 24 | | ULong, |
| | 25 | | Float, |
| | 26 | | Double, |
| | 27 | | Vector2, |
| | 28 | | Vector3, |
| | 29 | | Vector4, |
| | 30 | | Vector2Int, |
| | 31 | | Vector3Int, |
| | 32 | | Quaternion, |
| | 33 | | Rect, |
| | 34 | | RectInt, |
| | 35 | | Color, |
| | 36 | | LayerMask, |
| | 37 | | Bounds, |
| | 38 | | BoundsInt, |
| | 39 | | Hash128, |
| | 40 | | Gradient, |
| | 41 | | AnimationCurve, |
| | 42 | | Object, |
| | 43 | | EnumInt |
| | 44 | | } |
| | 45 | |
|
| | 46 | | const string k_Invalid = "Invalid"; |
| | 47 | | const string k_String = "String"; |
| | 48 | | const string k_Char = "Character"; |
| | 49 | | const string k_Bool = "Boolean"; |
| | 50 | | const string k_SByte = "Signed Byte"; |
| | 51 | | const string k_Byte = "Unsigned Byte"; |
| | 52 | | const string k_Short = "Short"; |
| | 53 | | const string k_UShort = "Unsigned Short"; |
| | 54 | | const string k_Int = "Integer"; |
| | 55 | | const string k_UInt = "UInt"; |
| | 56 | | const string k_Long = "Long"; |
| | 57 | | const string k_ULong = "Unsigned Long"; |
| | 58 | | const string k_Float = "Float"; |
| | 59 | | const string k_Double = "Double"; |
| | 60 | | const string k_Vector2 = "Vector2"; |
| | 61 | | const string k_Vector3 = "Vector3"; |
| | 62 | | const string k_Vector4 = "Vector4"; |
| | 63 | | const string k_Vector2Int = "Vector2 (Integer)"; |
| | 64 | | const string k_Vector3Int = "Vector3 (Integer)"; |
| | 65 | | const string k_Quaternion = "Quaternion"; |
| | 66 | | const string k_Rect = "Rectangle"; |
| | 67 | | const string k_RectInt = "Rectangle (Integer)"; |
| | 68 | | const string k_Color = "Color"; |
| | 69 | | const string k_LayerMask = "Layer (Mask)"; |
| | 70 | | const string k_Bounds = "Bounds"; |
| | 71 | | const string k_BoundsInt = "Bounds (Integer)"; |
| | 72 | | const string k_Hash128 = "Hash128"; |
| | 73 | | const string k_Gradient = "Gradient"; |
| | 74 | | const string k_AnimationCurve = "AnimationCurve"; |
| | 75 | | const string k_Object = "Object"; |
| | 76 | | const string k_EnumInt = "Enum (Integer)"; |
| | 77 | |
|
| | 78 | | public const int SerializableTypesCount = 30; |
| 1 | 79 | | static readonly int k_InvalidHashCode = k_Invalid.GetStableHashCode(); |
| 1 | 80 | | static readonly int k_StringHashCode = k_String.GetStableHashCode(); |
| 1 | 81 | | static readonly int k_CharHashCode = k_Char.GetStableHashCode(); |
| 1 | 82 | | static readonly int k_BoolHashCode = k_Bool.GetStableHashCode(); |
| 1 | 83 | | static readonly int k_SByteHashCode = k_SByte.GetStableHashCode(); |
| 1 | 84 | | static readonly int k_ByteHashCode = k_Byte.GetStableHashCode(); |
| 1 | 85 | | static readonly int k_ShortHashCode = k_Short.GetStableHashCode(); |
| 1 | 86 | | static readonly int k_UShortHashCode = k_UShort.GetStableHashCode(); |
| 1 | 87 | | static readonly int k_IntHashCode = k_Int.GetStableHashCode(); |
| 1 | 88 | | static readonly int k_UIntHashCode = k_UInt.GetStableHashCode(); |
| 1 | 89 | | static readonly int k_LongHashCode = k_Long.GetStableHashCode(); |
| 1 | 90 | | static readonly int k_ULongHashCode = k_ULong.GetStableHashCode(); |
| 1 | 91 | | static readonly int k_FloatHashCode = k_Float.GetStableHashCode(); |
| 1 | 92 | | static readonly int k_DoubleHashCode = k_Double.GetStableHashCode(); |
| 1 | 93 | | static readonly int k_Vector2HashCode = k_Vector2.GetStableHashCode(); |
| 1 | 94 | | static readonly int k_Vector3HashCode = k_Vector3.GetStableHashCode(); |
| 1 | 95 | | static readonly int k_Vector4HashCode = k_Vector4.GetStableHashCode(); |
| 1 | 96 | | static readonly int k_Vector2IntHashCode = k_Vector2Int.GetStableHashCode(); |
| 1 | 97 | | static readonly int k_Vector3IntHashCode = k_Vector3Int.GetStableHashCode(); |
| 1 | 98 | | static readonly int k_QuaternionHashCode = k_Quaternion.GetStableHashCode(); |
| 1 | 99 | | static readonly int k_RectHashCode = k_Rect.GetStableHashCode(); |
| 1 | 100 | | static readonly int k_RectIntHashCode = k_RectInt.GetStableHashCode(); |
| 1 | 101 | | static readonly int k_ColorHashCode = k_Color.GetStableHashCode(); |
| 1 | 102 | | static readonly int k_LayerMaskHashCode = k_LayerMask.GetStableHashCode(); |
| 1 | 103 | | static readonly int k_BoundsHashCode = k_Bounds.GetStableHashCode(); |
| 1 | 104 | | static readonly int k_BoundsIntHashCode = k_BoundsInt.GetStableHashCode(); |
| 1 | 105 | | static readonly int k_Hash128HashCode = k_Hash128.GetStableHashCode(); |
| 1 | 106 | | static readonly int k_GradientHashCode = k_Gradient.GetStableHashCode(); |
| 1 | 107 | | static readonly int k_AnimationCurveHashCode = k_AnimationCurve.GetStableHashCode(); |
| 1 | 108 | | static readonly int k_ObjectHashCode = k_Object.GetStableHashCode(); |
| 1 | 109 | | static readonly int k_EnumIntHashCode = k_EnumInt.GetStableHashCode(); |
| | 110 | |
|
| | 111 | | public static int GetHashCode(this SerializableTypes serializableTypes) |
| 0 | 112 | | { |
| 0 | 113 | | switch (serializableTypes) |
| | 114 | | { |
| | 115 | | case SerializableTypes.Invalid: |
| 0 | 116 | | return k_InvalidHashCode; |
| | 117 | | case SerializableTypes.String: |
| 0 | 118 | | return k_StringHashCode; |
| | 119 | | case SerializableTypes.Char: |
| 0 | 120 | | return k_CharHashCode; |
| | 121 | | case SerializableTypes.Bool: |
| 0 | 122 | | return k_BoolHashCode; |
| | 123 | | case SerializableTypes.SByte: |
| 0 | 124 | | return k_SByteHashCode; |
| | 125 | | case SerializableTypes.Byte: |
| 0 | 126 | | return k_ByteHashCode; |
| | 127 | | case SerializableTypes.Short: |
| 0 | 128 | | return k_ShortHashCode; |
| | 129 | | case SerializableTypes.UShort: |
| 0 | 130 | | return k_UShortHashCode; |
| | 131 | | case SerializableTypes.Int: |
| 0 | 132 | | return k_IntHashCode; |
| | 133 | | case SerializableTypes.UInt: |
| 0 | 134 | | return k_UIntHashCode; |
| | 135 | | case SerializableTypes.Long: |
| 0 | 136 | | return k_LongHashCode; |
| | 137 | | case SerializableTypes.ULong: |
| 0 | 138 | | return k_ULongHashCode; |
| | 139 | | case SerializableTypes.Float: |
| 0 | 140 | | return k_FloatHashCode; |
| | 141 | | case SerializableTypes.Double: |
| 0 | 142 | | return k_DoubleHashCode; |
| | 143 | | case SerializableTypes.Vector2: |
| 0 | 144 | | return k_Vector2HashCode; |
| | 145 | | case SerializableTypes.Vector3: |
| 0 | 146 | | return k_Vector3HashCode; |
| | 147 | | case SerializableTypes.Vector4: |
| 0 | 148 | | return k_Vector4HashCode; |
| | 149 | | case SerializableTypes.Vector2Int: |
| 0 | 150 | | return k_Vector2IntHashCode; |
| | 151 | | case SerializableTypes.Vector3Int: |
| 0 | 152 | | return k_Vector3IntHashCode; |
| | 153 | | case SerializableTypes.Quaternion: |
| 0 | 154 | | return k_QuaternionHashCode; |
| | 155 | | case SerializableTypes.Rect: |
| 0 | 156 | | return k_RectHashCode; |
| | 157 | | case SerializableTypes.RectInt: |
| 0 | 158 | | return k_RectIntHashCode; |
| | 159 | | case SerializableTypes.Color: |
| 0 | 160 | | return k_ColorHashCode; |
| | 161 | | case SerializableTypes.LayerMask: |
| 0 | 162 | | return k_LayerMaskHashCode; |
| | 163 | | case SerializableTypes.Bounds: |
| 0 | 164 | | return k_BoundsHashCode; |
| | 165 | | case SerializableTypes.BoundsInt: |
| 0 | 166 | | return k_BoundsIntHashCode; |
| | 167 | | case SerializableTypes.Hash128: |
| 0 | 168 | | return k_Hash128HashCode; |
| | 169 | | case SerializableTypes.Gradient: |
| 0 | 170 | | return k_GradientHashCode; |
| | 171 | | case SerializableTypes.AnimationCurve: |
| 0 | 172 | | return k_AnimationCurveHashCode; |
| | 173 | | case SerializableTypes.Object: |
| 0 | 174 | | return k_ObjectHashCode; |
| | 175 | | case SerializableTypes.EnumInt: |
| 0 | 176 | | return k_EnumIntHashCode; |
| | 177 | | } |
| | 178 | |
|
| 0 | 179 | | return k_InvalidHashCode; |
| 0 | 180 | | } |
| | 181 | |
|
| | 182 | | public static string GetLabel(this SerializableTypes serializableType) |
| 38 | 183 | | { |
| 38 | 184 | | switch (serializableType) |
| | 185 | | { |
| | 186 | | case SerializableTypes.Invalid: |
| 0 | 187 | | return k_Invalid; |
| | 188 | | case SerializableTypes.String: |
| 9 | 189 | | return k_String; |
| | 190 | | case SerializableTypes.Char: |
| 1 | 191 | | return k_Char; |
| | 192 | | case SerializableTypes.Bool: |
| 1 | 193 | | return k_Bool; |
| | 194 | | case SerializableTypes.SByte: |
| 1 | 195 | | return k_SByte; |
| | 196 | | case SerializableTypes.Byte: |
| 1 | 197 | | return k_Byte; |
| | 198 | | case SerializableTypes.Short: |
| 1 | 199 | | return k_Short; |
| | 200 | | case SerializableTypes.UShort: |
| 1 | 201 | | return k_UShort; |
| | 202 | | case SerializableTypes.Int: |
| 1 | 203 | | return k_Int; |
| | 204 | | case SerializableTypes.UInt: |
| 1 | 205 | | return k_UInt; |
| | 206 | | case SerializableTypes.Long: |
| 1 | 207 | | return k_Long; |
| | 208 | | case SerializableTypes.ULong: |
| 1 | 209 | | return k_ULong; |
| | 210 | | case SerializableTypes.Float: |
| 1 | 211 | | return k_Float; |
| | 212 | | case SerializableTypes.Double: |
| 1 | 213 | | return k_Double; |
| | 214 | | case SerializableTypes.Vector2: |
| 1 | 215 | | return k_Vector2; |
| | 216 | | case SerializableTypes.Vector3: |
| 1 | 217 | | return k_Vector3; |
| | 218 | | case SerializableTypes.Vector4: |
| 1 | 219 | | return k_Vector4; |
| | 220 | | case SerializableTypes.Vector2Int: |
| 1 | 221 | | return k_Vector2Int; |
| | 222 | | case SerializableTypes.Vector3Int: |
| 1 | 223 | | return k_Vector3Int; |
| | 224 | | case SerializableTypes.Quaternion: |
| 1 | 225 | | return k_Quaternion; |
| | 226 | | case SerializableTypes.Rect: |
| 1 | 227 | | return k_Rect; |
| | 228 | | case SerializableTypes.RectInt: |
| 1 | 229 | | return k_RectInt; |
| | 230 | | case SerializableTypes.Color: |
| 1 | 231 | | return k_Color; |
| | 232 | | case SerializableTypes.LayerMask: |
| 1 | 233 | | return k_LayerMask; |
| | 234 | | case SerializableTypes.Bounds: |
| 1 | 235 | | return k_Bounds; |
| | 236 | | case SerializableTypes.BoundsInt: |
| 1 | 237 | | return k_BoundsInt; |
| | 238 | | case SerializableTypes.Hash128: |
| 1 | 239 | | return k_Hash128; |
| | 240 | | case SerializableTypes.Gradient: |
| 1 | 241 | | return k_Gradient; |
| | 242 | | case SerializableTypes.AnimationCurve: |
| 1 | 243 | | return k_AnimationCurve; |
| | 244 | | case SerializableTypes.Object: |
| 1 | 245 | | return k_Object; |
| | 246 | | case SerializableTypes.EnumInt: |
| 1 | 247 | | return k_EnumInt; |
| | 248 | | } |
| | 249 | |
|
| 0 | 250 | | return k_Invalid; |
| 38 | 251 | | } |
| | 252 | |
|
| | 253 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 254 | | public static string GetLabelFromTypeValue(int typeValue) |
| 8 | 255 | | { |
| 8 | 256 | | return GetLabel((SerializableTypes)typeValue); |
| 8 | 257 | | } |
| | 258 | |
|
| | 259 | | public static SerializableTypes GetTypeFromHashCode(int hashCode) |
| 0 | 260 | | { |
| 0 | 261 | | if (hashCode == k_StringHashCode) |
| 0 | 262 | | { |
| 0 | 263 | | return SerializableTypes.String; |
| | 264 | | } |
| | 265 | |
|
| 0 | 266 | | if (hashCode == k_CharHashCode) |
| 0 | 267 | | { |
| 0 | 268 | | return SerializableTypes.Char; |
| | 269 | | } |
| | 270 | |
|
| 0 | 271 | | if (hashCode == k_BoolHashCode) |
| 0 | 272 | | { |
| 0 | 273 | | return SerializableTypes.Bool; |
| | 274 | | } |
| | 275 | |
|
| 0 | 276 | | if (hashCode == k_SByteHashCode) |
| 0 | 277 | | { |
| 0 | 278 | | return SerializableTypes.SByte; |
| | 279 | | } |
| | 280 | |
|
| 0 | 281 | | if (hashCode == k_ByteHashCode) |
| 0 | 282 | | { |
| 0 | 283 | | return SerializableTypes.Byte; |
| | 284 | | } |
| | 285 | |
|
| 0 | 286 | | if (hashCode == k_ShortHashCode) |
| 0 | 287 | | { |
| 0 | 288 | | return SerializableTypes.Short; |
| | 289 | | } |
| | 290 | |
|
| 0 | 291 | | if (hashCode == k_UShortHashCode) |
| 0 | 292 | | { |
| 0 | 293 | | return SerializableTypes.UShort; |
| | 294 | | } |
| | 295 | |
|
| 0 | 296 | | if (hashCode == k_IntHashCode) |
| 0 | 297 | | { |
| 0 | 298 | | return SerializableTypes.Int; |
| | 299 | | } |
| | 300 | |
|
| 0 | 301 | | if (hashCode == k_UIntHashCode) |
| 0 | 302 | | { |
| 0 | 303 | | return SerializableTypes.UInt; |
| | 304 | | } |
| | 305 | |
|
| 0 | 306 | | if (hashCode == k_LongHashCode) |
| 0 | 307 | | { |
| 0 | 308 | | return SerializableTypes.Long; |
| | 309 | | } |
| | 310 | |
|
| 0 | 311 | | if (hashCode == k_ULongHashCode) |
| 0 | 312 | | { |
| 0 | 313 | | return SerializableTypes.ULong; |
| | 314 | | } |
| | 315 | |
|
| 0 | 316 | | if (hashCode == k_FloatHashCode) |
| 0 | 317 | | { |
| 0 | 318 | | return SerializableTypes.Float; |
| | 319 | | } |
| | 320 | |
|
| 0 | 321 | | if (hashCode == k_DoubleHashCode) |
| 0 | 322 | | { |
| 0 | 323 | | return SerializableTypes.Double; |
| | 324 | | } |
| | 325 | |
|
| 0 | 326 | | if (hashCode == k_Vector2HashCode) |
| 0 | 327 | | { |
| 0 | 328 | | return SerializableTypes.Vector2; |
| | 329 | | } |
| | 330 | |
|
| 0 | 331 | | if (hashCode == k_Vector3HashCode) |
| 0 | 332 | | { |
| 0 | 333 | | return SerializableTypes.Vector3; |
| | 334 | | } |
| | 335 | |
|
| 0 | 336 | | if (hashCode == k_Vector4HashCode) |
| 0 | 337 | | { |
| 0 | 338 | | return SerializableTypes.Vector4; |
| | 339 | | } |
| | 340 | |
|
| 0 | 341 | | if (hashCode == k_Vector2IntHashCode) |
| 0 | 342 | | { |
| 0 | 343 | | return SerializableTypes.Vector2Int; |
| | 344 | | } |
| | 345 | |
|
| 0 | 346 | | if (hashCode == k_Vector3IntHashCode) |
| 0 | 347 | | { |
| 0 | 348 | | return SerializableTypes.Vector3Int; |
| | 349 | | } |
| | 350 | |
|
| 0 | 351 | | if (hashCode == k_QuaternionHashCode) |
| 0 | 352 | | { |
| 0 | 353 | | return SerializableTypes.Quaternion; |
| | 354 | | } |
| | 355 | |
|
| 0 | 356 | | if (hashCode == k_RectHashCode) |
| 0 | 357 | | { |
| 0 | 358 | | return SerializableTypes.Rect; |
| | 359 | | } |
| | 360 | |
|
| 0 | 361 | | if (hashCode == k_RectIntHashCode) |
| 0 | 362 | | { |
| 0 | 363 | | return SerializableTypes.RectInt; |
| | 364 | | } |
| | 365 | |
|
| 0 | 366 | | if (hashCode == k_ColorHashCode) |
| 0 | 367 | | { |
| 0 | 368 | | return SerializableTypes.Color; |
| | 369 | | } |
| | 370 | |
|
| 0 | 371 | | if (hashCode == k_LayerMaskHashCode) |
| 0 | 372 | | { |
| 0 | 373 | | return SerializableTypes.LayerMask; |
| | 374 | | } |
| | 375 | |
|
| 0 | 376 | | if (hashCode == k_BoundsHashCode) |
| 0 | 377 | | { |
| 0 | 378 | | return SerializableTypes.Bounds; |
| | 379 | | } |
| | 380 | |
|
| 0 | 381 | | if (hashCode == k_BoundsIntHashCode) |
| 0 | 382 | | { |
| 0 | 383 | | return SerializableTypes.BoundsInt; |
| | 384 | | } |
| | 385 | |
|
| 0 | 386 | | if (hashCode == k_Hash128HashCode) |
| 0 | 387 | | { |
| 0 | 388 | | return SerializableTypes.Hash128; |
| | 389 | | } |
| | 390 | |
|
| 0 | 391 | | if (hashCode == k_GradientHashCode) |
| 0 | 392 | | { |
| 0 | 393 | | return SerializableTypes.Gradient; |
| | 394 | | } |
| | 395 | |
|
| 0 | 396 | | if (hashCode == k_AnimationCurveHashCode) |
| 0 | 397 | | { |
| 0 | 398 | | return SerializableTypes.AnimationCurve; |
| | 399 | | } |
| | 400 | |
|
| 0 | 401 | | if (hashCode == k_ObjectHashCode) |
| 0 | 402 | | { |
| 0 | 403 | | return SerializableTypes.Object; |
| | 404 | | } |
| | 405 | |
|
| 0 | 406 | | if (hashCode == k_EnumIntHashCode) |
| 0 | 407 | | { |
| 0 | 408 | | return SerializableTypes.EnumInt; |
| | 409 | | } |
| | 410 | |
|
| 0 | 411 | | return SerializableTypes.Invalid; |
| 0 | 412 | | } |
| | 413 | |
|
| | 414 | | public static SerializableTypes GetTypeFromLabel(string label) |
| 0 | 415 | | { |
| 0 | 416 | | return GetTypeFromHashCode(label.GetStableHashCode()); |
| 0 | 417 | | } |
| | 418 | |
|
| | 419 | | public static bool IsReferenceType(this SerializableTypes type) |
| 0 | 420 | | { |
| 0 | 421 | | switch (type) |
| | 422 | | { |
| | 423 | | case SerializableTypes.Gradient: |
| | 424 | | case SerializableTypes.AnimationCurve: |
| | 425 | | case SerializableTypes.Object: |
| 0 | 426 | | return true; |
| | 427 | | default: |
| 0 | 428 | | return false; |
| | 429 | | } |
| 0 | 430 | | } |
| | 431 | | } |
| | 432 | | } |