2using System.Collections.Generic;
5using Terraria.ModLoader.Default;
15 writer.Write(item.modItem !=
null ? 0 : item.netID);
23 if (item.modItem ==
null) {
24 tag.Set(
"mod",
"Terraria");
25 tag.Set(
"id", item.netID);
28 tag.Set(
"mod", item.modItem.mod.Name);
29 tag.Set(
"name", item.modItem.Name);
30 tag.Set(
"data", item.modItem.Save());
33 if (item.prefix != 0 && item.prefix < PrefixID.Count)
34 tag.Set(
"prefix", item.prefix);
36 if (item.prefix >= PrefixID.Count) {
38 if (modPrefix !=
null) {
39 tag.Set(
"modPrefixMod", modPrefix.
mod.
Name);
40 tag.Set(
"modPrefixName", modPrefix.
Name);
45 tag.Set(
"stack", item.stack);
50 tag.Set(
"globalData", SaveGlobals(item));
63 if (modName ==
"Terraria") {
64 item.netDefaults(tag.
GetInt(
"id"));
71 item.netDefaults(type);
78 item.netDefaults(
ModContent.ItemType<MysteryItem>());
79 ((MysteryItem)item.modItem).Setup(tag);
84 string prefixMod = tag.
GetString(
"modPrefixMod");
85 string prefixName = tag.
GetString(
"modPrefixName");
89 item.Prefix(tag.
GetByte(
"prefix"));
91 item.stack = tag.Get<
int?>(
"stack") ?? 1;
92 item.favorited = tag.
GetBool(
"fav");
94 if (!(item.modItem is MysteryItem))
95 LoadGlobals(item, tag.GetList<
TagCompound>(
"globalData"));
99 var item =
new Item();
104 internal static List<TagCompound> SaveGlobals(Item item) {
105 if (item.modItem is MysteryItem)
108 var list =
new List<TagCompound>();
109 foreach (var globalItem
in ItemLoader.globalItems) {
110 var globalItemInstance = globalItem.Instance(item);
111 if (!globalItemInstance.NeedsSaving(item))
115 [
"mod"] = globalItemInstance.mod.Name,
116 [
"name"] = globalItemInstance.Name,
117 [
"data"] = globalItemInstance.Save(item)
120 return list.Count > 0 ? list :
null;
123 internal static void LoadGlobals(Item item, IList<TagCompound> list) {
124 foreach (var tag
in list) {
127 if (globalItem !=
null) {
128 var globalItemInstance = globalItem.
Instance(item);
130 globalItemInstance.
Load(item, tag.GetCompound(
"data"));
134 "Error in reading custom player data for " + mod.Name, e);
138 item.GetGlobalItem<MysteryGlobalItem>().data.Add(tag);
143 public static void Send(Item item,
BinaryWriter writer,
bool writeStack =
false,
bool writeFavourite =
false) {
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);
151 public static void Receive(Item item,
BinaryReader reader,
bool readStack =
false,
bool readFavorite =
false) {
152 item.netDefaults(reader.ReadInt16());
153 item.Prefix(reader.ReadByte());
154 if (readStack) item.stack = reader.ReadInt16();
155 if (readFavorite) item.favorited = reader.ReadBoolean();
160 var item =
new Item();
161 Receive(item, reader, readStack, readFavorite);
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));
173 if (item.IsAir)
return;
175 reader.SafeRead(r => item.modItem?.NetRecieve(r));
182 Logging.tML.Error($
"Above IOException error caused by {item.modItem.Name} from the {item.modItem.mod.Name} mod.");
185 foreach (var globalItem
in ItemLoader.NetGlobals) {
187 reader.SafeRead(r => globalItem.Instance(item).NetReceive(item, r));
194 Logging.tML.Error($
"Above IOException error caused by {globalItem.Name} from the {globalItem.mod.Name} mod while reading {item.Name}.");
200 string modName = reader.ReadString();
201 bool hasGlobalSaving =
false;
202 if (modName.Length == 0) {
203 hasGlobalSaving =
true;
204 modName = reader.ReadString();
206 if (modName ==
"Terraria") {
207 item.netDefaults(reader.ReadInt32());
208 LoadLegacyModData(item, LegacyModData(item.type, reader, hasGlobalSaving), hasGlobalSaving);
211 string itemName = reader.ReadString();
213 byte[] data = LegacyModData(type == 0 ?
int.MaxValue : type, reader, hasGlobalSaving);
215 item.netDefaults(type);
216 LoadLegacyModData(item, data, hasGlobalSaving);
219 item.netDefaults(
ModContent.ItemType<MysteryItem>());
223 [
"hasGlobalSaving"] = hasGlobalSaving,
224 [
"legacyData"] = data
226 ((MysteryItem)item.modItem).Setup(tag);
230 item.Prefix(reader.ReadByte());
233 item.stack = reader.ReadInt32();
236 item.favorited = reader.ReadBoolean();
239 internal static byte[] LegacyModData(
int type,
BinaryReader reader,
bool hasGlobalSaving =
true) {
240 using (MemoryStream memoryStream =
new MemoryStream()) {
242 if (type >= ItemID.Count) {
243 ushort length = reader.ReadUInt16();
244 writer.Write(length);
245 writer.Write(reader.ReadBytes(length));
247 if (hasGlobalSaving) {
248 ushort count = reader.ReadUInt16();
250 for (
int k = 0; k < count; k++) {
251 writer.Write(reader.ReadString());
252 writer.Write(reader.ReadString());
253 ushort length = reader.ReadUInt16();
254 writer.Write(length);
255 writer.Write(reader.ReadBytes(length));
259 return memoryStream.ToArray();
263 internal static void LoadLegacyModData(Item item,
byte[] data,
bool hasGlobalSaving =
true) {
265 if (item.modItem !=
null) {
266 byte[] modData = reader.ReadBytes(reader.ReadUInt16());
267 if (modData.Length > 0) {
270 item.modItem.LoadLegacy(customReader);
274 "Error in reading custom item data for " + item.modItem.mod.Name, e);
279 if (hasGlobalSaving) {
280 int count = reader.ReadUInt16();
281 for (
int k = 0; k < count; k++) {
282 string modName = reader.ReadString();
283 string globalName = reader.ReadString();
284 byte[] globalData = reader.ReadBytes(reader.ReadUInt16());
287 if (globalItem !=
null && globalData.Length > 0) {
294 "Error in reading custom global item data for " + globalItem.
mod.
Name, e);
304 int count = reader.ReadUInt16();
305 for (
int k = 0; k < count; k++) {
306 LoadLegacy(inv[reader.ReadUInt16()], reader, readStack, readFavorite);
311 MemoryStream ms =
new MemoryStream();
313 return Convert.ToBase64String(ms.ToArray());
317 MemoryStream ms =
new MemoryStream(Convert.FromBase64String(base64));
static readonly Framework Framework
This class allows you to modify and use hooks for all items, including vanilla items....
virtual void Load(Item item, TagCompound tag)
Allows you to load custom data that you have saved for the given item.
virtual void LoadLegacy(Item item, BinaryReader reader)
Allows you to load pre-v0.9 custom data that you have saved for the given item.
GlobalItem Instance(Item item)
Mod mod
The mod to which this GlobalItem belongs.
static void LoadLegacy(Item item, BinaryReader reader, bool readStack=false, bool readFavorite=false)
static TagCompound Save(Item item)
static void Receive(Item item, BinaryReader reader, bool readStack=false, bool readFavorite=false)
static void Load(Item item, TagCompound tag)
static Item Load(TagCompound tag)
static string ToBase64(Item item)
static void Send(Item item, BinaryWriter writer, bool writeStack=false, bool writeFavourite=false)
static void LoadLegacyInventory(Item[] inv, BinaryReader reader, bool readStack=false, bool readFavorite=false)
static void SendModData(Item item, BinaryWriter writer)
static Item FromBase64(string base64)
static Item Receive(BinaryReader reader, bool readStack=false, bool readFavorite=false)
static void ReceiveModData(Item item, BinaryReader reader)
byte[] GetByteArray(string key)
TagCompound GetCompound(string key)
string GetString(string key)
bool ContainsKey(string key)
static void ToStream(TagCompound root, Stream stream, bool compress=true)
static TagCompound FromStream(Stream stream, bool compressed=true)
This serves as the central class from which item-related functions are carried out....
Manages content added by mods. Liasons between mod content and Terraria's arrays and oversees the Loa...
byte PrefixType(string name)
Gets the internal ID / type of the ModPrefix corresponding to the name. Returns 0 if no ModPrefix wit...
GlobalItem GetGlobalItem(string name)
Gets the GlobalItem instance with the given name from this mod.
int ItemType(string name)
Gets the internal ID / type of the ModItem corresponding to the name. Returns 0 if no ModItem with th...
virtual string Name
Stores the name of the mod. This name serves as the mod's identification, and also helps with saving ...
This serves as the central class which loads mods. It contains many static fields and methods related...
static Mod GetMod(string name)
Gets the instance of the Mod with the specified name.
static ModPrefix GetPrefix(byte type)
Returns the ModPrefix associated with specified type If not a ModPrefix, returns null.