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 JsonRef<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 }
22
23 public void OnAfterDeserialize()
24 {
25 if (MultiJsonInternal.isDeserializing)
26 {
27 try
28 {
29 if (MultiJsonInternal.valueMap.TryGetValue(m_Id, out var value))
30 {
31 m_Value = value.CastTo<T>();
32 m_Id = m_Value.objectId;
33 }
34 else
35 {
36 m_Value = null;
37 m_Id = null;
38 }
39 }
40 catch (Exception e)
41 {
42 Debug.LogException(e);
43 }
44 }
45 }
46
47 public static implicit operator T(JsonRef<T> jsonRef)
48 {
49 return jsonRef.m_Value;
50 }
51
52 public static implicit operator JsonRef<T>(T value)
53 {
54 return new JsonRef<T> { m_Value = value, m_Id = value?.objectId };
55 }
56
57 public bool Equals(JsonRef<T> other)
58 {
59 return EqualityComparer<T>.Default.Equals(m_Value, other.m_Value);
60 }
61
62 public bool Equals(T other)
63 {
64 return EqualityComparer<T>.Default.Equals(m_Value, other);
65 }
66
67 public override bool Equals(object obj)
68 {
69 return obj is JsonRef<T> other && Equals(other) || obj is T otherValue && Equals(otherValue);
70 }
71
72 public override int GetHashCode()
73 {
74 return EqualityComparer<T>.Default.GetHashCode(m_Value);
75 }
76
77 public static bool operator ==(JsonRef<T> left, JsonRef<T> right)
78 {
79 return left.value == right.value;
80 }
81
82 public static bool operator !=(JsonRef<T> left, JsonRef<T> right)
83 {
84 return left.value != right.value;
85 }
86
87 public static bool operator ==(JsonRef<T> left, T right)
88 {
89 return left.value == right;
90 }
91
92 public static bool operator !=(JsonRef<T> left, T right)
93 {
94 return left.value != right;
95 }
96
97 public static bool operator ==(T left, JsonRef<T> right)
98 {
99 return left == right.value;
100 }
101
102 public static bool operator !=(T left, JsonRef<T> right)
103 {
104 return left != right.value;
105 }
106 }
107}