Terraria ModLoader  0.10.1.5
A framework for Terraria mods
Terraria.ModLoader.IO.TagIO Class Reference
+ Collaboration diagram for Terraria.ModLoader.IO.TagIO:

Classes

class  ClassPayloadHandler
 
class  PayloadHandler
 
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

static T Terraria.ModLoader.IO.TagIO.Clone< T > ( o)
static
static object Terraria.ModLoader.IO.TagIO.Deserialize ( Type  type,
object  tag 
)
static

Definition at line 213 of file TagIO.cs.

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

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

+ Here is the caller graph for this function:

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  }
static object Deserialize(Type type, object tag)
Definition: TagIO.cs:213
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
static TagCompound Terraria.ModLoader.IO.TagIO.FromStream ( Stream  stream,
bool  compressed = true 
)
static

Definition at line 292 of file TagIO.cs.

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

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

+ Here is the caller graph for this function:

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
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
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
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  }
static PayloadHandler< string > StringHandler
Definition: TagIO.cs:162
static readonly PayloadHandler[] PayloadHandlers
Definition: TagIO.cs:77
static object Terraria.ModLoader.IO.TagIO.Serialize ( object  value)
static

Definition at line 182 of file TagIO.cs.

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

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 int GetPayloadId(Type t)
Definition: TagIO.cs:171
static object Serialize(object value)
Definition: TagIO.cs:182

+ Here is the caller graph for this function:

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
static void Terraria.ModLoader.IO.TagIO.ToStream ( TagCompound  root,
Stream  stream,
bool  compress = true 
)
static

Definition at line 316 of file TagIO.cs.

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

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)

+ Here is the caller graph for this function:

static void Terraria.ModLoader.IO.TagIO.Write ( TagCompound  root,
BinaryWriter  writer 
)
static
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  }
static int GetPayloadId(Type t)
Definition: TagIO.cs:171
static PayloadHandler< string > StringHandler
Definition: TagIO.cs:162
static readonly PayloadHandler[] PayloadHandlers
Definition: TagIO.cs:77

Member Data Documentation

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

Definition at line 77 of file TagIO.cs.

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.

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

Definition at line 162 of file TagIO.cs.