Terraria ModLoader  0.11.6.2
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 308 of file ItemIO.cs.

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

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

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

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

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

98  {
99  var item = new Item();
100  Load(item, tag);
101  return item;
102  }
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 191 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.

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

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

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

Definition at line 151 of file ItemIO.cs.

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

Definition at line 159 of file ItemIO.cs.

References Terraria.ModLoader.Item.

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

Definition at line 172 of file ItemIO.cs.

172  {
173  if (item.IsAir) return;
174  try {
175  reader.SafeRead(r => item.modItem?.NetRecieve(r));
176  }
177  catch (IOException) {
178  Logging.tML.Error($"Above IOException error caused by {item.modItem.Name} from the {item.modItem.mod.Name} mod.");
179  }
180 
181  foreach (var globalItem in ItemLoader.NetGlobals) {
182  try {
183  reader.SafeRead(r => globalItem.Instance(item).NetReceive(item, r));
184  }
185  catch (IOException) {
186  Logging.tML.Error($"Above IOException error caused by {globalItem.Name} from the {globalItem.mod.Name} mod while reading {item.Name}.");
187  }
188  }
189  }
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 143 of file ItemIO.cs.

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

Definition at line 165 of file ItemIO.cs.

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

Definition at line 302 of file ItemIO.cs.

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

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

+ Here is the call graph for this function: