tModLoader v0.11.8.9
A mod to make and play 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 Item Receive (BinaryReader reader, bool readStack=false, bool readFavorite=false)
 
static void Receive (Item item, 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

◆ FromBase64()

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

Definition at line 316 of file ItemIO.cs.

316 {
317 MemoryStream ms = new MemoryStream(Convert.FromBase64String(base64));
318 return ItemIO.Load(TagIO.FromStream(ms, true));
319 }

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

+ Here is the call graph for this function:

◆ Load() [1/2]

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

Definition at line 55 of file ItemIO.cs.

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 }

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(), and Terraria.ModLoader.IO.ItemIO.Load().

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

◆ Load() [2/2]

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

Definition at line 98 of file ItemIO.cs.

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

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

+ Here is the call graph for this function:

◆ LoadLegacy()

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

Definition at line 199 of file ItemIO.cs.

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

References Terraria.ModLoader.ModLoader.GetMod(), and Terraria.ModLoader.Mod.ItemType().

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

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

◆ LoadLegacyInventory()

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

Definition at line 303 of file ItemIO.cs.

303 {
304 int count = reader.ReadUInt16();
305 for (int k = 0; k < count; k++) {
306 LoadLegacy(inv[reader.ReadUInt16()], reader, readStack, readFavorite);
307 }
308 }
static void LoadLegacy(Item item, BinaryReader reader, bool readStack=false, bool readFavorite=false)
Definition: ItemIO.cs:199

References Terraria.ModLoader.IO.ItemIO.LoadLegacy().

+ Here is the call graph for this function:

◆ Receive() [1/2]

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

Definition at line 159 of file ItemIO.cs.

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

References Terraria.ModLoader.Item, and Terraria.ModLoader.IO.ItemIO.Receive().

+ Here is the call graph for this function:

◆ Receive() [2/2]

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

References Terraria.ModLoader.IO.ItemIO.ReceiveModData().

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

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

◆ ReceiveModData()

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 e) {
178 if (FrameworkVersion.Framework == Framework.Mono) {
179 Logging.tML.Error(e);
180 }
181
182 Logging.tML.Error($"Above IOException error caused by {item.modItem.Name} from the {item.modItem.mod.Name} mod.");
183 }
184
185 foreach (var globalItem in ItemLoader.NetGlobals) {
186 try {
187 reader.SafeRead(r => globalItem.Instance(item).NetReceive(item, r));
188 }
189 catch (IOException e) {
190 if (FrameworkVersion.Framework == Framework.Mono) {
191 Logging.tML.Error(e);
192 }
193
194 Logging.tML.Error($"Above IOException error caused by {globalItem.Name} from the {globalItem.mod.Name} mod while reading {item.Name}.");
195 }
196 }
197 }

References Terraria.ModLoader.FrameworkVersion.Framework.

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

+ Here is the caller graph for this function:

◆ Save()

static TagCompound Terraria.ModLoader.IO.ItemIO.Save ( Item  item)
static

Definition at line 18 of file ItemIO.cs.

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 }

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

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

◆ Send()

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

References Terraria.ModLoader.IO.ItemIO.SendModData().

+ Here is the call graph for this function:

◆ SendModData()

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 }

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

+ Here is the caller graph for this function:

◆ ToBase64()

static string Terraria.ModLoader.IO.ItemIO.ToBase64 ( Item  item)
static

Definition at line 310 of file ItemIO.cs.

310 {
311 MemoryStream ms = new MemoryStream();
312 TagIO.ToStream(ItemIO.Save(item), ms, true);
313 return Convert.ToBase64String(ms.ToArray());
314 }

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

+ Here is the call graph for this function: