Survivalcraft API 1.8.2.3 v1.8.2.3
Survivalcraft 2.4
载入中...
搜索中...
未找到
InputArchive.cs
浏览该文件的文档.
1namespace Engine.Serialization {
2 public abstract class InputArchive : Archive {
3 Dictionary<int, object> m_objectById = [];
4
5 DynamicArray<object> m_stack = new();
6
7 public ReadOnlyList<object> Stack => new(m_stack);
8
9 protected InputArchive(int version, object context) : base(version, context) { }
10
11 protected new void Reset(int version, object context) {
12 base.Reset(version, context);
13 m_objectById.Clear();
14 m_stack.Clear();
15 }
16
17 public abstract void Serialize(string name, ref sbyte value);
18
19 public abstract void Serialize(string name, ref byte value);
20
21 public abstract void Serialize(string name, ref short value);
22
23 public abstract void Serialize(string name, ref ushort value);
24
25 public abstract void Serialize(string name, ref int value);
26
27 public abstract void Serialize(string name, ref uint value);
28
29 public abstract void Serialize(string name, ref long value);
30
31 public abstract void Serialize(string name, ref ulong value);
32
33 public abstract void Serialize(string name, ref float value);
34
35 public abstract void Serialize(string name, ref double value);
36
37 public abstract void Serialize(string name, ref bool value);
38
39 public abstract void Serialize(string name, ref char value);
40
41 public abstract void Serialize(string name, ref string value);
42
43 public abstract void Serialize(string name, ref byte[] value);
44
45 public abstract void Serialize(string name, int length, ref byte[] value);
46
47 public abstract void SerializeCollection<T>(string name, ICollection<T> collection);
48
49 public abstract void SerializeDictionary<K, V>(string name, IDictionary<K, V> dictionary);
50
51 public void Serialize(string name, Type type, ref object value) {
52 ReadObject(name, GetSerializeData(type, true), ref value, true);
53 }
54
55 public void Serialize(string name, Type type, object value) {
56 if (value == null) {
57 throw new InvalidOperationException("Value cannot be null");
58 }
59 ReadObject(name, GetSerializeData(type, true), ref value, false);
60 }
61
62 public void Serialize<T>(string name, T value) where T : class {
63 ReadObject(name, GetSerializeData(typeof(T), true), ref value, false);
64 }
65
66 public void Serialize<T>(string name, ref T value) {
67 ReadObject(name, GetSerializeData(typeof(T), true), ref value, true);
68 }
69
70 public void Serialize<T>(string name, Action<T> setter) {
71 T value = default;
72 ReadObject(name, GetSerializeData(typeof(T), true), ref value, true);
73 setter(value);
74 }
75
76 public T Serialize<T>(string name) {
77 T value = default;
78 ReadObject(name, GetSerializeData(typeof(T), true), ref value, true);
79 return value;
80 }
81
82 public object Serialize(string name, Type type) {
83 object value = null;
84 Serialize(name, type, ref value);
85 return value;
86 }
87
88 public List<T> SerializeCollection<T>(string name) {
89 List<T> list = new();
90 SerializeCollection(name, list);
91 return list;
92 }
93
94 public void SerializeCollection<T>(string name, Action<T> adder) {
95 List<T> list = new();
96 SerializeCollection(name, list);
97 foreach (T item in list) {
98 adder(item);
99 }
100 }
101
102 public Dictionary<K, V> SerializeDictionary<K, V>(string name) {
103 Dictionary<K, V> dictionary = new();
104 SerializeDictionary(name, dictionary);
105 return dictionary;
106 }
107
108 public T FindParentObject<T>(bool throwIfNotFound = true) where T : class {
109 for (int num = m_stack.Count - 1; num >= 0; num--) {
110 if (m_stack[num] is T result) {
111 return result;
112 }
113 }
114 if (throwIfNotFound) {
115 throw new InvalidOperationException($"Required parent object of type {typeof(T).FullName} not found on serialization stack.");
116 }
117 return null;
118 }
119
120 public abstract void ReadObjectInfo(out int? objectId, out bool isReference, out Type runtimeType);
121
122 protected virtual void ReadObject(string name, SerializeData staticSerializeData, ref object value, bool allowOverwriteOfExistingObject) {
123 if (!staticSerializeData.UseObjectInfo
124 || !UseObjectInfos) {
125 ReadObjectWithoutObjectInfo(staticSerializeData, ref value);
126 }
127 else {
128 ReadObjectWithObjectInfo(staticSerializeData, ref value, allowOverwriteOfExistingObject);
129 }
130 }
131
132 protected virtual void ReadObject<T>(string name, SerializeData staticSerializeData, ref T value, bool allowOverwriteOfExistingObject) {
133 if (staticSerializeData.IsValueType) {
134 staticSerializeData.VerifySerializable();
135 ((SerializeData<T>)staticSerializeData).ReadGeneric(this, ref value);
136 }
137 else {
138 object value2 = value;
139 ReadObject(name, staticSerializeData, ref value2, allowOverwriteOfExistingObject);
140 value = (T)value2;
141 }
142 }
143
144 void ReadObjectWithoutObjectInfo(SerializeData staticSerializeData, ref object value) {
145 Type type = value?.GetType();
146 SerializeData serializeData = !(type == null) && !(staticSerializeData.Type == type)
147 ? GetSerializeData(type, false)
148 : staticSerializeData;
149 if (serializeData.AutoConstruct == AutoConstructMode.Yes
150 && value == null) {
151 value = Activator.CreateInstance(serializeData.Type, true);
152 }
153 serializeData.Read(this, ref value);
154 }
155
156 void ReadObjectWithObjectInfo(SerializeData staticSerializeData, ref object value, bool allowOverwriteOfExistingObject) {
157 ReadObjectInfo(out int? objectId, out bool isReference, out Type runtimeType);
158 if (objectId == 0) {
159 if (!allowOverwriteOfExistingObject
160 && value != null) {
161 throw new InvalidOperationException("Serializing null reference into an existing object.");
162 }
163 return;
164 }
165 if (isReference) {
166 if (!allowOverwriteOfExistingObject
167 && value != null) {
168 throw new InvalidOperationException("Serializing a reference into an existing object.");
169 }
170 value = m_objectById[objectId.Value];
171 return;
172 }
173 Type type = value?.GetType();
174 SerializeData serializeData;
175 if (!(type != null)) {
176 serializeData = !(runtimeType != null) ? staticSerializeData : GetSerializeData(runtimeType, false);
177 }
178 else {
179 if (runtimeType != null
180 && runtimeType != type) {
181 throw new InvalidOperationException("Serialized object has different type than existing object.");
182 }
183 serializeData = GetSerializeData(type, false);
184 }
185 if (serializeData.AutoConstruct == AutoConstructMode.Yes
186 && value == null) {
187 value = serializeData.CreateInstance();
188 }
189 serializeData.VerifySerializable();
190 m_stack.Add(value);
191 serializeData.Read(this, ref value);
192 m_stack.RemoveAtEnd();
193 if (objectId.HasValue) {
194 m_objectById.Add(objectId.Value, value);
195 }
196 }
197 }
198}
static SerializeData GetSerializeData(Type type, bool allowEmptySerializer)
Archive(int version, object context)
void Serialize(string name, ref long value)
void SerializeDictionary< K, V >(string name, IDictionary< K, V > dictionary)
void Serialize(string name, int length, ref byte[] value)
void ReadObjectInfo(out int? objectId, out bool isReference, out Type runtimeType)
virtual void ReadObject< T >(string name, SerializeData staticSerializeData, ref T value, bool allowOverwriteOfExistingObject)
virtual void ReadObject(string name, SerializeData staticSerializeData, ref object value, bool allowOverwriteOfExistingObject)
void SerializeCollection< T >(string name, ICollection< T > collection)
void Serialize(string name, ref uint value)
InputArchive(int version, object context)
T FindParentObject< T >(bool throwIfNotFound=true)
void Serialize(string name, ref ulong value)
new void Reset(int version, object context)
void Serialize(string name, ref string value)
void ReadObjectWithObjectInfo(SerializeData staticSerializeData, ref object value, bool allowOverwriteOfExistingObject)
void Serialize(string name, Type type, ref object value)
void Serialize(string name, ref bool value)
void Serialize(string name, ref ushort value)
void Serialize(string name, ref int value)
void Serialize(string name, ref double value)
void Serialize< T >(string name, T value)
void Serialize(string name, ref byte[] value)
void Serialize(string name, ref float value)
void Serialize(string name, ref sbyte value)
Dictionary< int, object > m_objectById
void Serialize(string name, ref char value)
void Serialize(string name, ref byte value)
object Serialize(string name, Type type)
void Serialize(string name, Type type, object value)
void ReadObjectWithoutObjectInfo(SerializeData staticSerializeData, ref object value)
void Serialize(string name, ref short value)