Terraria ModLoader  0.10.1.5
A framework for Terraria mods
Terraria.ModLoader.IO.ItemIO Class Reference

Static Public Member Functions

static Item FromBase64 (string base64)
 
static void Load (Item item, TagCompound tag)
 
static Item Load (TagCompound tag)
 
static void LoadLegacy (Item item, BinaryReader reader, bool readStack=false, bool readFavorite=false)
 
static void LoadLegacyInventory (Item[] inv, BinaryReader reader, bool readStack=false, bool readFavorite=false)
 
static void Receive (Item item, BinaryReader reader, bool readStack=false, bool readFavorite=false)
 
static Item Receive (BinaryReader reader, bool readStack=false, bool readFavorite=false)
 
static void ReceiveModData (Item item, BinaryReader reader)
 
static TagCompound Save (Item item)
 
static void Send (Item item, BinaryWriter writer, bool writeStack=false, bool writeFavourite=false)
 
static void SendModData (Item item, BinaryWriter writer)
 
static string ToBase64 (Item item)
 

Detailed Description

Definition at line 10 of file ItemIO.cs.

Member Function Documentation

static Item Terraria.ModLoader.IO.ItemIO.FromBase64 ( string  base64)
static

Definition at line 303 of file ItemIO.cs.

References Terraria.ModLoader.IO.TagIO.FromStream(), Terraria.ModLoader.Item, and Terraria.ModLoader.IO.ItemIO.Load().

303  {
304  MemoryStream ms = new MemoryStream(Convert.FromBase64String(base64));
305  return ItemIO.Load(TagIO.FromStream(ms, true));
306  }

+ Here is the call graph for this function:

static void Terraria.ModLoader.IO.ItemIO.Load ( Item  item,
TagCompound  tag 
)
static

Definition at line 53 of file ItemIO.cs.

References Terraria.ModLoader.IO.TagCompound.ContainsKey(), Terraria.ModLoader.IO.TagCompound.Count, Terraria.ModLoader.IO.TagCompound.GetBool(), Terraria.ModLoader.IO.TagCompound.GetByte(), Terraria.ModLoader.IO.TagCompound.GetByteArray(), Terraria.ModLoader.IO.TagCompound.GetCompound(), Terraria.ModLoader.IO.TagCompound.GetInt(), Terraria.ModLoader.ModLoader.GetMod(), Terraria.ModLoader.IO.TagCompound.GetString(), Terraria.ModLoader.Mod.ItemType(), and Terraria.ModLoader.Mod.PrefixType().

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

53  {
54  if (tag.Count == 0) {
55  item.netDefaults(0);
56  return;
57  }
58 
59  string modName = tag.GetString("mod");
60  if (modName == "Terraria") {
61  item.netDefaults(tag.GetInt("id"));
62  if (tag.ContainsKey("legacyData"))
63  LoadLegacyModData(item, tag.GetByteArray("legacyData"), tag.GetBool("hasGlobalSaving"));
64  }
65  else {
66  int type = ModLoader.GetMod(modName)?.ItemType(tag.GetString("name")) ?? 0;
67  if (type > 0) {
68  item.netDefaults(type);
69  if (tag.ContainsKey("legacyData"))
70  LoadLegacyModData(item, tag.GetByteArray("legacyData"), tag.GetBool("hasGlobalSaving"));
71  else
72  item.modItem.Load(tag.GetCompound("data"));
73  }
74  else {
75  item.netDefaults(ModLoader.GetMod("ModLoader").ItemType("MysteryItem"));
76  ((MysteryItem)item.modItem).Setup(tag);
77  }
78  }
79 
80  item.Prefix(tag.GetByte("prefix"));
81  if (tag.ContainsKey("modPrefixMod") && tag.ContainsKey("modPrefixName")) {
82  string prefixMod = tag.GetString("modPrefixMod");
83  string prefixName = tag.GetString("modPrefixName");
84  item.Prefix(ModLoader.GetMod(prefixMod)?.PrefixType(prefixName) ?? 0);
85  }
86  item.stack = tag.Get<int?>("stack") ?? 1;
87  item.favorited = tag.GetBool("fav");
88 
89  if (!(item.modItem is MysteryItem))
90  LoadGlobals(item, tag.GetList<TagCompound>("globalData"));
91  }

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

static Item Terraria.ModLoader.IO.ItemIO.Load ( TagCompound  tag)
static

Definition at line 93 of file ItemIO.cs.

References Terraria.ModLoader.Mod.GetGlobalItem(), Terraria.ModLoader.ModLoader.GetMod(), Terraria.ModLoader.GlobalItem.Instance(), Terraria.ModLoader.Item, and Terraria.ModLoader.GlobalItem.Load().

93  {
94  var item = new Item();
95  Load(item, tag);
96  return item;
97  }
static void Load(Item item, TagCompound tag)
Definition: ItemIO.cs:53

+ Here is the call graph for this function:

static void Terraria.ModLoader.IO.ItemIO.LoadLegacy ( Item  item,
BinaryReader  reader,
bool  readStack = false,
bool  readFavorite = false 
)
static

Definition at line 186 of file ItemIO.cs.

References Terraria.ModLoader.Mod.GetGlobalItem(), Terraria.ModLoader.ModLoader.GetMod(), Terraria.ModLoader.Item, Terraria.ModLoader.Mod.ItemType(), Terraria.ModLoader.GlobalItem.LoadLegacy(), Terraria.ModLoader.GlobalItem.mod, and Terraria.ModLoader.Mod.Name.

186  {
187  string modName = reader.ReadString();
188  bool hasGlobalSaving = false;
189  if (modName.Length == 0) {
190  hasGlobalSaving = true;
191  modName = reader.ReadString();
192  }
193  if (modName == "Terraria") {
194  item.netDefaults(reader.ReadInt32());
195  LoadLegacyModData(item, LegacyModData(item.type, reader, hasGlobalSaving), hasGlobalSaving);
196  }
197  else {
198  string itemName = reader.ReadString();
199  int type = ModLoader.GetMod(modName)?.ItemType(itemName) ?? 0;
200  byte[] data = LegacyModData(type == 0 ? int.MaxValue : type, reader, hasGlobalSaving);
201  if (type != 0) {
202  item.netDefaults(type);
203  LoadLegacyModData(item, data, hasGlobalSaving);
204  }
205  else {
206  item.netDefaults(ModLoader.GetMod("ModLoader").ItemType("MysteryItem"));
207  var tag = new TagCompound {
208  ["mod"] = modName,
209  ["name"] = itemName,
210  ["hasGlobalSaving"] = hasGlobalSaving,
211  ["legacyData"] = data
212  };
213  ((MysteryItem)item.modItem).Setup(tag);
214  }
215  }
216 
217  item.Prefix(reader.ReadByte());
218 
219  if (readStack)
220  item.stack = reader.ReadInt32();
221 
222  if (readFavorite)
223  item.favorited = reader.ReadBoolean();
224  }

+ Here is the call graph for this function:

static void Terraria.ModLoader.IO.ItemIO.LoadLegacyInventory ( Item[]  inv,
BinaryReader  reader,
bool  readStack = false,
bool  readFavorite = false 
)
static

Definition at line 290 of file ItemIO.cs.

290  {
291  int count = reader.ReadUInt16();
292  for (int k = 0; k < count; k++) {
293  LoadLegacy(inv[reader.ReadUInt16()], reader, readStack, readFavorite);
294  }
295  }
static void LoadLegacy(Item item, BinaryReader reader, bool readStack=false, bool readFavorite=false)
Definition: ItemIO.cs:186
static void Terraria.ModLoader.IO.ItemIO.Receive ( Item  item,
BinaryReader  reader,
bool  readStack = false,
bool  readFavorite = false 
)
static

Definition at line 146 of file ItemIO.cs.

146  {
147  item.netDefaults(reader.ReadInt16());
148  item.Prefix(reader.ReadByte());
149  if (readStack) item.stack = reader.ReadInt16();
150  if (readFavorite) item.favorited = reader.ReadBoolean();
151  ReceiveModData(item, reader);
152  }
static void ReceiveModData(Item item, BinaryReader reader)
Definition: ItemIO.cs:167
static Item Terraria.ModLoader.IO.ItemIO.Receive ( BinaryReader  reader,
bool  readStack = false,
bool  readFavorite = false 
)
static

Definition at line 154 of file ItemIO.cs.

References Terraria.ModLoader.Item.

154  {
155  var item = new Item();
156  Receive(item, reader, readStack, readFavorite);
157  return item;
158  }
static void Receive(Item item, BinaryReader reader, bool readStack=false, bool readFavorite=false)
Definition: ItemIO.cs:146
static void Terraria.ModLoader.IO.ItemIO.ReceiveModData ( Item  item,
BinaryReader  reader 
)
static

Definition at line 167 of file ItemIO.cs.

167  {
168  if (item.IsAir) return;
169  try {
170  reader.SafeRead(r => item.modItem?.NetRecieve(r));
171  }
172  catch (IOException) {
173  //TODO inform modder/user
174  }
175 
176  foreach (var globalItem in ItemLoader.NetGlobals) {
177  try {
178  reader.SafeRead(r => globalItem.Instance(item).NetReceive(item, r));
179  }
180  catch (IOException) {
181  //TODO inform modder/user
182  }
183  }
184  }
static TagCompound Terraria.ModLoader.IO.ItemIO.Save ( Item  item)
static

Definition at line 18 of file ItemIO.cs.

References Terraria.ModLoader.ModPrefix.GetPrefix(), Terraria.ModLoader.ModPrefix.mod, Terraria.ModLoader.Mod.Name, and Terraria.ModLoader.ModPrefix.Name.

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

18  {
19  var tag = new TagCompound();
20  if (item.type <= 0)
21  return tag;
22 
23  if (item.modItem == null) {
24  tag.Set("mod", "Terraria");
25  tag.Set("id", item.netID);
26  }
27  else {
28  tag.Set("mod", item.modItem.mod.Name);
29  tag.Set("name", item.modItem.Name);
30  tag.Set("data", item.modItem.Save());
31  }
32 
33  if (item.prefix != 0 && item.prefix < PrefixID.Count)
34  tag.Set("prefix", item.prefix);
35 
36  if (item.prefix >= PrefixID.Count) {
37  ModPrefix modPrefix = ModPrefix.GetPrefix(item.prefix);
38  tag.Set("modPrefixMod", modPrefix.mod.Name);
39  tag.Set("modPrefixName", modPrefix.Name);
40  }
41 
42  if (item.stack > 1)
43  tag.Set("stack", item.stack);
44 
45  if (item.favorited)
46  tag.Set("fav", true);
47 
48  tag.Set("globalData", SaveGlobals(item));
49 
50  return tag;
51  }

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

static void Terraria.ModLoader.IO.ItemIO.Send ( Item  item,
BinaryWriter  writer,
bool  writeStack = false,
bool  writeFavourite = false 
)
static

Definition at line 138 of file ItemIO.cs.

138  {
139  writer.Write((short)item.netID);
140  writer.Write(item.prefix);
141  if (writeStack) writer.Write((short)item.stack);
142  if (writeFavourite) writer.Write(item.favorited);
143  SendModData(item, writer);
144  }
static void SendModData(Item item, BinaryWriter writer)
Definition: ItemIO.cs:160
static void Terraria.ModLoader.IO.ItemIO.SendModData ( Item  item,
BinaryWriter  writer 
)
static

Definition at line 160 of file ItemIO.cs.

160  {
161  if (item.IsAir) return;
162  writer.SafeWrite(w => item.modItem?.NetSend(w));
163  foreach (var globalItem in ItemLoader.NetGlobals)
164  writer.SafeWrite(w => globalItem.Instance(item).NetSend(item, w));
165  }
static string Terraria.ModLoader.IO.ItemIO.ToBase64 ( Item  item)
static

Definition at line 297 of file ItemIO.cs.

References Terraria.ModLoader.IO.ItemIO.Save(), and Terraria.ModLoader.IO.TagIO.ToStream().

297  {
298  MemoryStream ms = new MemoryStream();
299  TagIO.ToStream(ItemIO.Save(item), ms, true);
300  return Convert.ToBase64String(ms.ToArray());
301  }

+ Here is the call graph for this function: