Even More Modifiers  1.0.0.0
A mod for rolling various bonus stats on items
ModifierRarityIO.cs
Go to the documentation of this file.
1 using System;
2 using System.IO;
3 using Loot.Api.Core;
4 using Loot.Api.Loaders;
5 using Terraria;
6 using Terraria.ModLoader.IO;
7 
8 namespace Loot.IO
9 {
10  internal static class ModifierRarityIO
11  {
12  public const int SAVE_VERSION = 14;
13 
14  public static ModifierRarity NetReceive(Item item, BinaryReader reader)
15  {
16  string type = reader.ReadString();
17  string modName = reader.ReadString();
18 
19  ModifierRarity rarity = ContentLoader.ModifierRarity.GetContent(modName, type);
20  if (rarity == null)
21  {
22  throw new Exception($"ModifierRarity _NetReceive error for {modName}");
23  }
24 
25  rarity.NetReceive(item, reader);
26  return rarity;
27  }
28 
29  public static void NetSend(ModifierRarity rarity, Item item, BinaryWriter writer)
30  {
31  writer.Write(rarity.Name);
32  writer.Write(rarity.Mod.Name);
33  rarity.NetSend(item, writer);
34  }
35 
36  public static ModifierRarity Load(Item item, TagCompound tag)
37  {
38  if (tag == null || tag.ContainsKey("EMMErr:RarityNullErr"))
39  {
41  }
42 
43  string modName = tag.GetString("ModName");
44 
45  if (RegistryLoader.Mods.TryGetValue(modName, out var assembly))
46  {
47  var saveVersion = tag.ContainsKey("SaveVersion") ? tag.GetInt("SaveVersion") : 1;
48 
49  if (saveVersion < 14)
50  {
52  }
53 
54  string rarityTypeName = tag.GetString("Type");
55  var rarity = ContentLoader.ModifierRarity.GetContent(modName, rarityTypeName);
56  if (rarity == null)
57  {
59  }
60 
61  rarity.Load(item, tag);
62  return rarity;
63  }
64 
65  Loot.Logger.ErrorFormat("There was a load error for modifierrarity, TC: {0}", tag);
67  }
68 
69  public static TagCompound Save(Item item, ModifierRarity rarity)
70  {
71  if (rarity == null)
72  {
73  // Should NEVER be null, instead be NullModifierRarity
74  return new TagCompound { { "EMMErr:RarityNullErr", "ModifierRarity was null err" } };
75  }
76 
77  var tag = new TagCompound
78  {
79  {"Type", rarity.GetType().Name},
80  {"ModName", rarity.Mod.Name},
81  {"SaveVersion", SAVE_VERSION}
82  };
83  rarity.Save(item, tag);
84  return tag;
85  }
86  }
87 }
static NullModifierRarity INSTANCE
virtual void NetReceive(Item item, BinaryReader reader)
Allows the modder to do custom NetReceive
Defines the rarity of a modifier
virtual void Save(Item item, TagCompound tag)
Allows modder to do custom saving here Use the given TC to put data you want to save, which can be loaded using Load(Item,TagCompound)
virtual void Load(Item item, TagCompound tag)
Allows modder to do custom loading here Use the given TagCompound to pull data you saved using Save(I...
static ModifierRarityContent ModifierRarity
This class holds all Content holders of this mod You can use this to access content loaded into the m...
Defines a "Null" rarity which represents no rarity safely Cannot be rolled normally ...
virtual void NetSend(Item item, BinaryWriter writer)
Allows modder to do custom NetSend here
This class is responsible for loading mods into Even More Modifiers Mods can be registered and trigge...