Terraria ModLoader  0.11.4
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 307 of file ItemIO.cs.

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

307  {
308  MemoryStream ms = new MemoryStream(Convert.FromBase64String(base64));
309  return ItemIO.Load(TagIO.FromStream(ms, true));
310  }

+ Here is the call graph for this function:

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

Definition at line 55 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().

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

+ 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 97 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().

97  {
98  var item = new Item();
99  Load(item, tag);
100  return item;
101  }
static void Load(Item item, TagCompound tag)
Definition: ItemIO.cs:55

+ 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 190 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.

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

+ 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 294 of file ItemIO.cs.

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

Definition at line 150 of file ItemIO.cs.

150  {
151  item.netDefaults(reader.ReadInt16());
152  item.Prefix(reader.ReadByte());
153  if (readStack) item.stack = reader.ReadInt16();
154  if (readFavorite) item.favorited = reader.ReadBoolean();
155  ReceiveModData(item, reader);
156  }
static void ReceiveModData(Item item, BinaryReader reader)
Definition: ItemIO.cs:171
static Item Terraria.ModLoader.IO.ItemIO.Receive ( BinaryReader  reader,
bool  readStack = false,
bool  readFavorite = false 
)
static

Definition at line 158 of file ItemIO.cs.

References Terraria.ModLoader.Item.

158  {
159  var item = new Item();
160  Receive(item, reader, readStack, readFavorite);
161  return item;
162  }
static void Receive(Item item, BinaryReader reader, bool readStack=false, bool readFavorite=false)
Definition: ItemIO.cs:150
static void Terraria.ModLoader.IO.ItemIO.ReceiveModData ( Item  item,
BinaryReader  reader 
)
static

Definition at line 171 of file ItemIO.cs.

171  {
172  if (item.IsAir) return;
173  try {
174  reader.SafeRead(r => item.modItem?.NetRecieve(r));
175  }
176  catch (IOException) {
177  Logging.tML.Error($"Above IOException error caused by {item.modItem.Name} from the {item.modItem.mod.Name} mod.");
178  }
179 
180  foreach (var globalItem in ItemLoader.NetGlobals) {
181  try {
182  reader.SafeRead(r => globalItem.Instance(item).NetReceive(item, r));
183  }
184  catch (IOException) {
185  Logging.tML.Error($"Above IOException error caused by {globalItem.Name} from the {globalItem.mod.Name} mod while reading {item.Name}.");
186  }
187  }
188  }
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  if (modPrefix != null) {
39  tag.Set("modPrefixMod", modPrefix.mod.Name);
40  tag.Set("modPrefixName", modPrefix.Name);
41  }
42  }
43 
44  if (item.stack > 1)
45  tag.Set("stack", item.stack);
46 
47  if (item.favorited)
48  tag.Set("fav", true);
49 
50  tag.Set("globalData", SaveGlobals(item));
51 
52  return tag;
53  }

+ 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 142 of file ItemIO.cs.

142  {
143  writer.Write((short)item.netID);
144  writer.Write(item.prefix);
145  if (writeStack) writer.Write((short)item.stack);
146  if (writeFavourite) writer.Write(item.favorited);
147  SendModData(item, writer);
148  }
static void SendModData(Item item, BinaryWriter writer)
Definition: ItemIO.cs:164
static void Terraria.ModLoader.IO.ItemIO.SendModData ( Item  item,
BinaryWriter  writer 
)
static

Definition at line 164 of file ItemIO.cs.

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

Definition at line 301 of file ItemIO.cs.

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

301  {
302  MemoryStream ms = new MemoryStream();
303  TagIO.ToStream(ItemIO.Save(item), ms, true);
304  return Convert.ToBase64String(ms.ToArray());
305  }

+ Here is the call graph for this function: