A game about forced loneliness, made by TACStudios
1using System;
2using System.Collections.Generic;
3using UnityEngine;
4
5namespace UnityEditor.ShaderGraph.Serialization
6{
7 [Serializable]
8 struct JsonData<T> : ISerializationCallbackReceiver
9 where T : JsonObject
10 {
11 [NonSerialized]
12 T m_Value;
13
14 [SerializeField]
15 string m_Id;
16
17 public T value => m_Value;
18
19 public void OnBeforeSerialize()
20 {
21 if (MultiJsonInternal.isSerializing && m_Value != null && MultiJsonInternal.serializedSet.Add(m_Id))
22 {
23 MultiJsonInternal.serializationQueue.Add(m_Value);
24 }
25 }
26
27 public void OnAfterDeserialize()
28 {
29 if (MultiJsonInternal.isDeserializing)
30 {
31 try
32 {
33 if (MultiJsonInternal.valueMap.TryGetValue(m_Id, out var value))
34 {
35 m_Value = value.CastTo<T>();
36
37 // cast may fail for unknown types, but we can still grab the id from the original UnknownType
38 m_Id = value.objectId;
39 }
40 else
41 {
42 }
43 }
44 catch (Exception e)
45 {
46 // TODO: Allow custom logging function
47 Debug.LogException(e);
48 }
49 }
50 }
51
52 public static implicit operator T(JsonData<T> jsonRef)
53 {
54 return jsonRef.m_Value;
55 }
56
57 public static implicit operator JsonData<T>(T value)
58 {
59 return new JsonData<T> { m_Value = value, m_Id = value.objectId };
60 }
61
62 public bool Equals(JsonData<T> other)
63 {
64 return EqualityComparer<T>.Default.Equals(m_Value, other.m_Value);
65 }
66
67 public bool Equals(T other)
68 {
69 return EqualityComparer<T>.Default.Equals(m_Value, other);
70 }
71
72 public override bool Equals(object obj)
73 {
74 return obj is JsonData<T> other && Equals(other) || obj is T otherValue && Equals(otherValue);
75 }
76
77 public override int GetHashCode()
78 {
79 return EqualityComparer<T>.Default.GetHashCode(m_Value);
80 }
81
82 public static bool operator ==(JsonData<T> left, JsonData<T> right)
83 {
84 return left.value == right.value;
85 }
86
87 public static bool operator !=(JsonData<T> left, JsonData<T> right)
88 {
89 return left.value != right.value;
90 }
91
92 public static bool operator ==(JsonData<T> left, T right)
93 {
94 return left.value == right;
95 }
96
97 public static bool operator !=(JsonData<T> left, T right)
98 {
99 return left.value != right;
100 }
101
102 public static bool operator ==(T left, JsonData<T> right)
103 {
104 return left == right.value;
105 }
106
107 public static bool operator !=(T left, JsonData<T> right)
108 {
109 return left != right.value;
110 }
111
112 public static bool operator ==(JsonData<T> left, JsonRef<T> right)
113 {
114 return left.value == right.value;
115 }
116
117 public static bool operator !=(JsonData<T> left, JsonRef<T> right)
118 {
119 return left.value != right.value;
120 }
121
122 public static bool operator ==(JsonRef<T> left, JsonData<T> right)
123 {
124 return left.value == right.value;
125 }
126
127 public static bool operator !=(JsonRef<T> left, JsonData<T> right)
128 {
129 return left.value != right.value;
130 }
131 }
132}