Even More Modifiers  1.0.0.0
A mod for rolling various bonus stats on items
ModifierIO.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 ModifierIO
11  {
12  public const int SAVE_VERSION = 14;
13 
14  public static Modifier NetReceive(Item item, BinaryReader reader)
15  {
16  string type = reader.ReadString();
17  string modName = reader.ReadString();
18  ModifierProperties properties = ModifierPropertiesIO.NetReceive(item, reader);
19 
20  Modifier m = ContentLoader.Modifier.GetContent(modName, type);
21  if (m != null)
22  {
23  m.Properties = m.GetModifierProperties(item).Build();
24  m.Properties.Magnitude = properties.Magnitude;
25  m.Properties.Power = properties.Power;
26  m.NetReceive(item, reader);
27  return m;
28  }
29 
30  throw new Exception($"Modifier _NetReceive error for {modName}");
31  }
32 
33  public static void NetSend(Modifier modifier, Item item, BinaryWriter writer)
34  {
35  writer.Write(modifier.GetType().Name);
36  writer.Write(modifier.Mod.Name);
37  ModifierPropertiesIO.NetSend(item, modifier.Properties, writer);
38  modifier.NetSend(item, writer);
39  }
40 
41  public static Modifier Load(Item item, TagCompound tag)
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  // adapt by save version
50  if (saveVersion < 14)
51  {
52  return NullModifier.INSTANCE;
53  }
54 
55  string modifierTypeName = tag.GetString("Type");
56  var modifier = ContentLoader.Modifier.GetContent(modName, modifierTypeName);
57  if (modifier == null)
58  {
59  return NullModifier.INSTANCE;
60  }
61 
62  var p = ModifierPropertiesIO.Load(item, tag.GetCompound("ModifierProperties"));
63  modifier.Properties = modifier.GetModifierProperties(item).Build();
64  modifier.Properties.Magnitude = p.Magnitude;
65  modifier.Properties.Power = p.Power;
66  modifier.Load(item, tag);
67  return modifier;
68 
69  }
70 
71  Loot.Logger.ErrorFormat("There was a load error for modifier, TC: {0}", tag);
72  return NullModifier.INSTANCE;
73  }
74 
75  public static TagCompound Save(Item item, Modifier modifier)
76  {
77  var tag = new TagCompound
78  {
79  {"Type", modifier.GetType().Name},
80  {"ModName", modifier.Mod.Name},
81  {"ModifierProperties", ModifierPropertiesIO.Save(item, modifier.Properties)},
82  {"SaveVersion", SAVE_VERSION}
83  };
84  modifier.Save(item, tag);
85  return tag;
86  }
87  }
88 }
virtual void NetReceive(Item item, BinaryReader reader)
Defines a modifier, which is an unloaded GlobalItem Making it a GlobalItem gives easy access to all h...
Definition: Modifier.cs:21
static ModifierContent Modifier
ModifierProperties Properties
Definition: Modifier.cs:44
Defines the properties of a modifier
static NullModifier INSTANCE
Definition: NullModifier.cs:14
virtual ModifierProperties.ModifierPropertiesBuilder GetModifierProperties(Item item)
Defines a "Null" modifier which represents no modifier safely Cannot be rolled normally ...
Definition: NullModifier.cs:10
This class holds all Content holders of this mod You can use this to access content loaded into the m...
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)
Definition: Modifier.cs:127
virtual new void Load(Item item, TagCompound tag)
Allows modder to do custom loading here Use the given TC to pull data you saved using Save(Item...
Definition: Modifier.cs:119
This class is responsible for loading mods into Even More Modifiers Mods can be registered and trigge...