Even More Modifiers  1.0.0.0
A mod for rolling various bonus stats on items
ModifierPoolIO.cs
Go to the documentation of this file.
1 using System;
2 using System.Collections.Generic;
3 using System.IO;
4 using Loot.Api.Core;
5 using Loot.Api.Loaders;
6 using Terraria;
7 using Terraria.ModLoader.IO;
8 
9 namespace Loot.IO
10 {
11  internal static class ModifierPoolIO
12  {
13  public const int SAVE_VERSION = 14;
14 
15  public static FiniteModifierPool NetReceive(Item item, BinaryReader reader)
16  {
17  string type = reader.ReadString();
18  string modName = reader.ReadString();
19 
20  ModifierPool pool = ContentLoader.ModifierPool.GetContent(modName, type);
21  if (pool == null)
22  {
23  throw new Exception($"Modifier _NetReceive error for {modName}");
24  }
25 
26  int activeModifiersSize = reader.ReadInt32();
27  var list = new List<Modifier>();
28  for (int i = 0; i < activeModifiersSize; ++i)
29  {
30  list.Add(ModifierIO.NetReceive(item, reader));
31  }
32 
33  var finitePool = new FiniteModifierPool(list);
34  finitePool.NetReceive(item, reader);
35  return finitePool;
36  }
37 
38  public static void NetSend(FiniteModifierPool pool, Item item, BinaryWriter writer)
39  {
40  writer.Write(pool.Name);
41  writer.Write(pool.Mod.Name);
42 
43  writer.Write(pool.Modifiers.Count);
44  foreach (var modifier in pool.Modifiers)
45  {
46  ModifierIO.NetSend(modifier, item, writer);
47  }
48 
49  pool.NetSend(item, writer);
50  }
51 
52  public static FiniteModifierPool Load(Item item, TagCompound tag)
53  {
54  if (tag == null || tag.ContainsKey("EMMErr:PoolNullErr"))
55  {
57  }
58 
59  var saveVersion = tag.ContainsKey("SaveVersion") ? tag.GetInt("SaveVersion") : 1;
60 
61  if (saveVersion < 14)
62  {
64  }
65 
66  var list = new List<Modifier>();
67  int count = tag.GetAsInt("Count");
68  for (int i = 0; i < count; ++i)
69  {
70  var loaded = ModifierIO.Load(item, tag.Get<TagCompound>($"Modifier{i}"));
71  if (loaded != null)
72  {
73  list.Add(loaded);
74  }
75  }
76 
77  return list.Count > 0 ? new FiniteModifierPool(list) : NullModifierPool.INSTANCE;
78  }
79 
80  public static TagCompound Save(Item item, FiniteModifierPool pool)
81  {
82  if (pool == null)
83  {
84  return new TagCompound { { "EMMErr:PoolNullErr", "ModifierPool was null err" } };
85  }
86 
87  var tag = new TagCompound
88  {
89  {"SaveVersion", SAVE_VERSION},
90  {"Count", pool.Modifiers.Count}
91  };
92 
93  for (int i = 0; i < pool.Modifiers.Count; ++i)
94  {
95  tag.Add($"Modifier{i}", ModifierIO.Save(item, pool.Modifiers[i]));
96  }
97 
98  pool.Save(item, tag);
99  return tag;
100  }
101  }
102 }
Defines a "Null" modifier which represents a pool with no modifiers Cannot be rolled normally ...
static NullModifierPool INSTANCE
virtual void NetSend(Item item, BinaryWriter writer)
Allows modder to do custom NetSend here
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)
static ModifierPoolContent ModifierPool
virtual 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...
Defines a modifier pool. A modifier pool holds a certain amount of effects in an array It allows to r...
Definition: ModifierPool.cs:37
readonly List< Modifier > Modifiers
Definition: ModifierPool.cs:19
This class holds all Content holders of this mod You can use this to access content loaded into the m...