tModLoader v0.11.8.9
A mod to make and play Terraria mods
Terraria.ModLoader.IO.TagIO Class Reference
+ Collaboration diagram for Terraria.ModLoader.IO.TagIO:

Classes

class  ClassPayloadHandler
 
class  PayloadHandler
 

Static Public Member Functions

static T Clone< T > (T o)
 
static object Deserialize (Type type, object tag)
 
static T Deserialize< T > (object tag)
 
static TagCompound FromFile (string path, bool compressed=true)
 
static TagCompound FromStream (Stream stream, bool compressed=true)
 
static TagCompound Read (BinaryReader reader)
 
static object ReadTag (BinaryReader r, out string name)
 
static object Serialize (object value)
 
static void ToFile (TagCompound root, string path, bool compress=true)
 
static void ToStream (TagCompound root, Stream stream, bool compress=true)
 
static void Write (TagCompound root, BinaryWriter writer)
 
static void WriteTag (string name, object tag, BinaryWriter w)
 

Static Private Member Functions

static PayloadHandler GetHandler (int id)
 
static int GetPayloadId (Type t)
 

Static Private Attributes

static readonly PayloadHandler[] PayloadHandlers
 
static readonly Dictionary< Type, int > PayloadIDs
 
static PayloadHandler< string > StringHandler = (PayloadHandler<string>)PayloadHandlers[8]
 

Detailed Description

Definition at line 11 of file TagIO.cs.

Member Function Documentation

◆ Clone< T >()

static T Terraria.ModLoader.IO.TagIO.Clone< T > ( o)
static

◆ Deserialize()

static object Terraria.ModLoader.IO.TagIO.Deserialize ( Type  type,
object  tag 
)
static

Definition at line 213 of file TagIO.cs.

213 {
214 if (type.IsInstanceOfType(tag))
215 return tag;
216
217 TagSerializer serializer;
218 if (TagSerializer.TryGetSerializer(type, out serializer)) {
219 if (tag == null)
220 tag = Deserialize(serializer.TagType, null);
221
222 return serializer.Deserialize(tag);
223 }
224
225 //normal nbt type with missing value
226 if (tag == null) {
227 if (type.GetGenericArguments().Length == 0)
228 return GetHandler(GetPayloadId(type)).Default();
229
230 if (type.GetGenericTypeDefinition() == typeof(Nullable<>))
231 return null;
232 }
233
234 //list conversion required
235 if ((tag == null || tag is IList) &&
236 type.GetGenericArguments().Length == 1) {
237 var elemType = type.GetGenericArguments()[0];
238 var newListType = typeof(List<>).MakeGenericType(elemType);
239 if (type.IsAssignableFrom(newListType)) {//if the desired type is a superclass of List<elemType>
240 if (tag == null)
241 return newListType.GetConstructor(new Type[0]).Invoke(new object[0]);
242
243 if (TagSerializer.TryGetSerializer(elemType, out serializer))
244 return serializer.DeserializeList((IList)tag);
245
246 //create a strongly typed nested list
247 var oldList = (IList)tag;
248 var newList = (IList)newListType.GetConstructor(new[] { typeof(int) }).Invoke(new object[] { oldList.Count });
249 foreach (var elem in oldList)
250 newList.Add(Deserialize(elemType, elem));
251
252 return newList;
253 }
254 }
255
256 if (tag == null)//unable to create an empty list subclassing the desired type
257 throw new IOException($"Invalid NBT payload type '{type}'");
258
259 throw new InvalidCastException($"Unable to cast object of type '{tag.GetType()}' to type '{type}'");
260 }
static int GetPayloadId(Type t)
Definition: TagIO.cs:171
static object Deserialize(Type type, object tag)
Definition: TagIO.cs:213
static PayloadHandler GetHandler(int id)
Definition: TagIO.cs:164

References Terraria.ModLoader.IO.TagSerializer< T, S >.Deserialize(), Terraria.ModLoader.IO.TagSerializer< T, S >.TagType, and Terraria.ModLoader.IO.TagSerializer< T, S >.TryGetSerializer().

Referenced by Terraria.ModLoader.IO.TagCompound.Get< T >().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ Deserialize< T >()

static T Terraria.ModLoader.IO.TagIO.Deserialize< T > ( object  tag)
static

Definition at line 208 of file TagIO.cs.

208 {
209 if (tag is T) return (T)tag;
210 return (T)Deserialize(typeof(T), tag);
211 }

◆ FromFile()

static TagCompound Terraria.ModLoader.IO.TagIO.FromFile ( string  path,
bool  compressed = true 
)
static

Definition at line 282 of file TagIO.cs.

282 {
283 try {
284 using (Stream fs = new FileStream(path, FileMode.Open, FileAccess.Read))
285 return FromStream(fs, compressed);
286 }
287 catch (IOException e) {
288 throw new IOException("Failed to read NBT file: " + path, e);
289 }
290 }
static TagCompound FromStream(Stream stream, bool compressed=true)
Definition: TagIO.cs:292

◆ FromStream()

static TagCompound Terraria.ModLoader.IO.TagIO.FromStream ( Stream  stream,
bool  compressed = true 
)
static

Definition at line 292 of file TagIO.cs.

292 {
293 if (compressed) stream = new GZipStream(stream, CompressionMode.Decompress);
294 return Read(new BigEndianReader(stream));
295 }
static TagCompound Read(BinaryReader reader)
Definition: TagIO.cs:297

Referenced by Terraria.ModLoader.IO.ItemIO.FromBase64().

+ Here is the caller graph for this function:

◆ GetHandler()

static PayloadHandler Terraria.ModLoader.IO.TagIO.GetHandler ( int  id)
staticprivate

Definition at line 164 of file TagIO.cs.

164 {
165 if (id < 1 || id >= PayloadHandlers.Length)
166 throw new IOException("Invalid NBT payload id: " + id);
167
168 return PayloadHandlers[id];
169 }
static readonly PayloadHandler[] PayloadHandlers
Definition: TagIO.cs:77

◆ GetPayloadId()

static int Terraria.ModLoader.IO.TagIO.GetPayloadId ( Type  t)
staticprivate

Definition at line 171 of file TagIO.cs.

171 {
172 int id;
173 if (PayloadIDs.TryGetValue(t, out id))
174 return id;
175
176 if (typeof(IList).IsAssignableFrom(t))
177 return 9;
178
179 throw new IOException($"Invalid NBT payload type '{t}'");
180 }
static readonly Dictionary< Type, int > PayloadIDs
Definition: TagIO.cs:159

◆ Read()

static TagCompound Terraria.ModLoader.IO.TagIO.Read ( BinaryReader  reader)
static

Definition at line 297 of file TagIO.cs.

297 {
298 string name;
299 var tag = ReadTag(reader, out name);
300 if (!(tag is TagCompound))
301 throw new IOException("Root tag not a TagCompound");
302
303 return (TagCompound)tag;
304 }
static object ReadTag(BinaryReader r, out string name)
Definition: TagIO.cs:264

◆ ReadTag()

static object Terraria.ModLoader.IO.TagIO.ReadTag ( BinaryReader  r,
out string  name 
)
static

Definition at line 264 of file TagIO.cs.

264 {
265 int id = r.ReadByte();
266 if (id == 0) {
267 name = null;
268 return null;
269 }
270
271 name = StringHandler.reader(r);
272 return PayloadHandlers[id].Read(r);
273 }
abstract object Read(BinaryReader r)
static PayloadHandler< string > StringHandler
Definition: TagIO.cs:162

◆ Serialize()

static object Terraria.ModLoader.IO.TagIO.Serialize ( object  value)
static

Definition at line 182 of file TagIO.cs.

182 {
183 var type = value.GetType();
184
185 TagSerializer serializer;
186 if (TagSerializer.TryGetSerializer(type, out serializer))
187 return serializer.Serialize(value);
188
189 //does a base level typecheck with throw
190 if (GetPayloadId(type) != 9)
191 return value;
192
193 var elemType = type.GetGenericArguments()[0];
194 if (TagSerializer.TryGetSerializer(elemType, out serializer))
195 return serializer.SerializeList((IList)value);
196
197 if (GetPayloadId(elemType) != 9)
198 return value;//already a valid NBT list type
199
200 //list of lists conversion
201 var list = value as IList<IList> ?? ((IList)value).Cast<IList>().ToList();
202 for (int i = 0; i < list.Count; i++)
203 list[i] = (IList)Serialize(list[i]);
204
205 return list;
206 }
static object Serialize(object value)
Definition: TagIO.cs:182

References Terraria.ModLoader.IO.TagSerializer< T, S >.Serialize(), Terraria.ModLoader.IO.TagSerializer< T, S >.SerializeList(), and Terraria.ModLoader.IO.TagSerializer< T, S >.TryGetSerializer().

Referenced by Terraria.ModLoader.IO.TagCompound.Set().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ ToFile()

static void Terraria.ModLoader.IO.TagIO.ToFile ( TagCompound  root,
string  path,
bool  compress = true 
)
static

Definition at line 306 of file TagIO.cs.

306 {
307 try {
308 using (Stream fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write))
309 ToStream(root, fs, compress);
310 }
311 catch (IOException e) {
312 throw new IOException("Failed to read NBT file: " + path, e);
313 }
314 }
static void ToStream(TagCompound root, Stream stream, bool compress=true)
Definition: TagIO.cs:316

◆ ToStream()

static void Terraria.ModLoader.IO.TagIO.ToStream ( TagCompound  root,
Stream  stream,
bool  compress = true 
)
static

Definition at line 316 of file TagIO.cs.

316 {
317 if (compress) stream = new GZipStream(stream, CompressionMode.Compress, true);
318 Write(root, new BigEndianWriter(stream));
319 if (compress) stream.Close();
320 }
static void Write(TagCompound root, BinaryWriter writer)

Referenced by Terraria.ModLoader.IO.ItemIO.ToBase64().

+ Here is the caller graph for this function:

◆ Write()

static void Terraria.ModLoader.IO.TagIO.Write ( TagCompound  root,
BinaryWriter  writer 
)
static

◆ WriteTag()

static void Terraria.ModLoader.IO.TagIO.WriteTag ( string  name,
object  tag,
BinaryWriter  w 
)
static

Definition at line 275 of file TagIO.cs.

275 {
276 int id = GetPayloadId(tag.GetType());
277 w.Write((byte)id);
278 StringHandler.writer(w, name);
279 PayloadHandlers[id].Write(w, tag);
280 }
abstract void Write(BinaryWriter w, object v)

Member Data Documentation

◆ PayloadHandlers

readonly PayloadHandler [] Terraria.ModLoader.IO.TagIO.PayloadHandlers
staticprivate

Definition at line 77 of file TagIO.cs.

◆ PayloadIDs

readonly Dictionary<Type, int> Terraria.ModLoader.IO.TagIO.PayloadIDs
staticprivate
Initial value:
=
Enumerable.Range(1, PayloadHandlers.Length - 1).ToDictionary(i => PayloadHandlers[i].PayloadType)

Definition at line 159 of file TagIO.cs.

◆ StringHandler

PayloadHandler<string> Terraria.ModLoader.IO.TagIO.StringHandler = (PayloadHandler<string>)PayloadHandlers[8]
staticprivate

Definition at line 162 of file TagIO.cs.