Skip to content

Commit

Permalink
Removed all instances of legacy (pre 0.9) data loading.
Browse files Browse the repository at this point in the history
  • Loading branch information
Mirsario committed Jul 12, 2020
1 parent 8966c02 commit 991c437
Show file tree
Hide file tree
Showing 13 changed files with 29 additions and 492 deletions.
15 changes: 0 additions & 15 deletions patches/tModLoader/Terraria/ModLoader/Default/MysteryItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,21 +55,6 @@ public override void Load(TagCompound tag) {
}
}

public override void LoadLegacy(BinaryReader reader) {
string modName = reader.ReadString();
bool hasGlobal = false;
if (modName.Length == 0) {
hasGlobal = true;
modName = reader.ReadString();
}
Load(new TagCompound {
["mod"] = modName,
["name"] = reader.ReadString(),
["hasGlobalSaving"] = hasGlobal,
["legacyData"] = ItemIO.LegacyModData(int.MaxValue, reader, hasGlobal)
});
}

public override void NetSend(BinaryWriter writer) {
TagIO.Write(data ?? new TagCompound(), writer);
}
Expand Down
13 changes: 0 additions & 13 deletions patches/tModLoader/Terraria/ModLoader/Default/MysteryPlayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,6 @@ public override void Load(TagCompound tag) {
PlayerIO.LoadModData(player, tag.GetList<TagCompound>("list"));
}

public override void LoadLegacy(BinaryReader reader) {
var list = new List<TagCompound>();
int count = reader.ReadUInt16();
for (int k = 0; k < count; k++) {
list.Add(new TagCompound {
["mod"] = reader.ReadString(),
["name"] = reader.ReadString(),
["legacyData"] = reader.ReadBytes(reader.ReadUInt16())
});
}
Load(new TagCompound { ["list"] = list });
}

public override void SetupStartInventory(IList<Item> items, bool mediumcoreDeath) {
if (AprilFools.CheckAprilFools()) {
Item item = new Item();
Expand Down
23 changes: 0 additions & 23 deletions patches/tModLoader/Terraria/ModLoader/Default/MysteryTilesWorld.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,29 +57,6 @@ public override void Load(TagCompound tag) {
}
}

public override void LoadLegacy(BinaryReader reader) {
var list = new List<TagCompound>();
int count = reader.ReadInt32();
for (int k = 0; k < count; k++) {
string modName = reader.ReadString();
if (modName.Length == 0) {
list.Add(new TagCompound());
}
else {
var tag = new TagCompound {
["mod"] = modName,
["name"] = reader.ReadString(),
};
if (reader.ReadBoolean()) {
tag.Set("frameX", reader.ReadInt16());
tag.Set("frameY", reader.ReadInt16());
}
list.Add(tag);
}
}
Load(new TagCompound { ["list"] = list });
}

private void RestoreTiles(List<ushort> canRestore) {
ushort mysteryType = (ushort)ModContent.GetInstance<ModLoaderMod>().TileType("MysteryTile");
for (int x = 0; x < Main.maxTilesX; x++) {
Expand Down
13 changes: 0 additions & 13 deletions patches/tModLoader/Terraria/ModLoader/Default/MysteryWorld.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,5 @@ public override void Load(TagCompound tag) {
WorldIO.LoadNPCs(tag.GetList<TagCompound>("mysteryNPCs"));
WorldIO.LoadNPCKillCounts(tag.GetList<TagCompound>("mysteryKillCounts"));
}

public override void LoadLegacy(BinaryReader reader) {
var list = new List<TagCompound>();
int count = reader.ReadUInt16();
for (int k = 0; k < count; k++) {
list.Add(new TagCompound {
["mod"] = reader.ReadString(),
["name"] = reader.ReadString(),
["legacyData"] = reader.ReadBytes(reader.ReadUInt16())
});
}
Load(new TagCompound { ["list"] = list });
}
}
}
9 changes: 0 additions & 9 deletions patches/tModLoader/Terraria/ModLoader/Default/StartBag.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,5 @@ public override TagCompound Save() {
public override void Load(TagCompound tag) {
items = tag.Get<List<Item>>("items");
}

public override void LoadLegacy(BinaryReader reader) {
int count = reader.ReadUInt16();
for (int k = 0; k < count; k++) {
var item = new Item();
ItemIO.LoadLegacy(item, reader, true);
AddItem(item);
}
}
}
}
6 changes: 0 additions & 6 deletions patches/tModLoader/Terraria/ModLoader/GlobalItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -905,12 +905,6 @@ public virtual TagCompound Save(Item item) {
public virtual void Load(Item item, TagCompound tag) {
}

/// <summary>
/// Allows you to load pre-v0.9 custom data that you have saved for the given item.
/// </summary>
public virtual void LoadLegacy(Item item, BinaryReader reader) {
}

/// <summary>
/// Allows you to send custom data for the given item between client and server.
/// </summary>
Expand Down
95 changes: 2 additions & 93 deletions patches/tModLoader/Terraria/ModLoader/IO/ItemIO.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,17 +62,13 @@ public static void Load(Item item, TagCompound tag)

if (modName == "Terraria") {
item.netDefaults(tag.GetInt("id"));
if (tag.ContainsKey("legacyData"))
LoadLegacyModData(item, tag.GetByteArray("legacyData"), tag.GetBool("hasGlobalSaving"));
}
else {
int type = ModLoader.GetMod(modName)?.ItemType(tag.GetString("name")) ?? 0;
if (type > 0) {
item.netDefaults(type);
if (tag.ContainsKey("legacyData"))
LoadLegacyModData(item, tag.GetByteArray("legacyData"), tag.GetBool("hasGlobalSaving"));
else
item.modItem.Load(tag.GetCompound("data"));

item.modItem.Load(tag.GetCompound("data"));
}
else {
item.netDefaults(ModContent.ItemType<MysteryItem>());
Expand Down Expand Up @@ -188,46 +184,6 @@ public static void ReceiveModData(Item item, BinaryReader reader) {
}
}

public static void LoadLegacy(Item item, BinaryReader reader, bool readStack = false, bool readFavorite = false) {
string modName = reader.ReadString();
bool hasGlobalSaving = false;
if (modName.Length == 0) {
hasGlobalSaving = true;
modName = reader.ReadString();
}
if (modName == "Terraria") {
item.netDefaults(reader.ReadInt32());
LoadLegacyModData(item, LegacyModData(item.type, reader, hasGlobalSaving), hasGlobalSaving);
}
else {
string itemName = reader.ReadString();
int type = ModLoader.GetMod(modName)?.ItemType(itemName) ?? 0;
byte[] data = LegacyModData(type == 0 ? int.MaxValue : type, reader, hasGlobalSaving);
if (type != 0) {
item.netDefaults(type);
LoadLegacyModData(item, data, hasGlobalSaving);
}
else {
item.netDefaults(ModContent.ItemType<MysteryItem>());
var tag = new TagCompound {
["mod"] = modName,
["name"] = itemName,
["hasGlobalSaving"] = hasGlobalSaving,
["legacyData"] = data
};
((MysteryItem)item.modItem).Setup(tag);
}
}

item.Prefix(reader.ReadByte());

if (readStack)
item.stack = reader.ReadInt32();

if (readFavorite)
item.favorited = reader.ReadBoolean();
}

internal static byte[] LegacyModData(int type, BinaryReader reader, bool hasGlobalSaving = true) {
using (MemoryStream memoryStream = new MemoryStream()) {
using (BinaryWriter writer = new BinaryWriter(memoryStream)) {
Expand All @@ -252,53 +208,6 @@ internal static byte[] LegacyModData(int type, BinaryReader reader, bool hasGlob
}
}

internal static void LoadLegacyModData(Item item, byte[] data, bool hasGlobalSaving = true) {
using (BinaryReader reader = new BinaryReader(new MemoryStream(data))) {
if (item.modItem != null) {
byte[] modData = reader.ReadBytes(reader.ReadUInt16());
if (modData.Length > 0) {
using (BinaryReader customReader = new BinaryReader(new MemoryStream(modData))) {
try {
item.modItem.LoadLegacy(customReader);
}
catch (Exception e) {
throw new CustomModDataException(item.modItem.mod,
"Error in reading custom item data for " + item.modItem.mod.Name, e);
}
}
}
}
if (hasGlobalSaving) {
int count = reader.ReadUInt16();
for (int k = 0; k < count; k++) {
string modName = reader.ReadString();
string globalName = reader.ReadString();
byte[] globalData = reader.ReadBytes(reader.ReadUInt16());
GlobalItem globalItem = ModLoader.GetMod(modName)?.GetGlobalItem(globalName);
//could support legacy global data in mystery globals but eh...
if (globalItem != null && globalData.Length > 0) {
using (BinaryReader customReader = new BinaryReader(new MemoryStream(globalData))) {
try {
globalItem.LoadLegacy(item, customReader);
}
catch (Exception e) {
throw new CustomModDataException(globalItem.mod,
"Error in reading custom global item data for " + globalItem.mod.Name, e);
}
}
}
}
}
}
}

public static void LoadLegacyInventory(Item[] inv, BinaryReader reader, bool readStack = false, bool readFavorite = false) {
int count = reader.ReadUInt16();
for (int k = 0; k < count; k++) {
LoadLegacy(inv[reader.ReadUInt16()], reader, readStack, readFavorite);
}
}

public static string ToBase64(Item item) {
MemoryStream ms = new MemoryStream();
TagIO.ToStream(ItemIO.Save(item), ms, true);
Expand Down
130 changes: 5 additions & 125 deletions patches/tModLoader/Terraria/ModLoader/IO/PlayerIO.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,14 @@ internal static void Save(Player player, string path, bool isCloudSave) {
//add near end of Terraria.Player.LoadPlayer before accessory check
internal static void Load(Player player, string path, bool isCloudSave) {
path = Path.ChangeExtension(path, ".tplr");

if (!FileUtilities.Exists(path, isCloudSave))
return;

var buf = FileUtilities.ReadAllBytes(path, isCloudSave);
byte[] buf = FileUtilities.ReadAllBytes(path, isCloudSave);

if (buf[0] != 0x1F || buf[1] != 0x8B) {
LoadLegacy(player, buf);
//LoadLegacy(player, buf);
return;
}

Expand Down Expand Up @@ -132,10 +134,7 @@ internal static void LoadModData(Player player, IList<TagCompound> list) {
var modPlayer = mod == null ? null : player.GetModPlayer(mod, tag.GetString("name"));
if (modPlayer != null) {
try {
if (tag.ContainsKey("legacyData"))
modPlayer.LoadLegacy(new BinaryReader(new MemoryStream(tag.GetByteArray("legacyData"))));
else
modPlayer.Load(tag.GetCompound("data"));
modPlayer.Load(tag.GetCompound("data"));
}
catch (Exception e) {
throw new CustomModDataException(mod,
Expand Down Expand Up @@ -213,125 +212,6 @@ internal static void LoadModBuffs(Player player, IList<TagCompound> list) {
}
}

private static void LoadLegacy(Player player, byte[] buffer) {
const int numFlagBytes = 2;
RijndaelManaged rijndaelManaged = new RijndaelManaged();
rijndaelManaged.Padding = PaddingMode.None;
using (MemoryStream stream = new MemoryStream(buffer)) {
using (CryptoStream cryptoStream = new CryptoStream(stream, rijndaelManaged.CreateDecryptor(Player.ENCRYPTION_KEY, Player.ENCRYPTION_KEY), CryptoStreamMode.Read)) {
using (BinaryReader reader = new BinaryReader(cryptoStream)) {
byte limit = reader.ReadByte();
if (limit == 0) {
return;
}
byte[] flags = reader.ReadBytes(limit);
if (flags.Length < numFlagBytes) {
Array.Resize(ref flags, numFlagBytes);
}
LoadLegacyModPlayer(player, flags, reader);
}
}
}
}

private static void LoadLegacyModPlayer(Player player, byte[] flags, BinaryReader reader) {
if ((flags[0] & 1) == 1) {
ItemIO.LoadLegacyInventory(player.armor, reader);
}
if ((flags[0] & 2) == 2) {
ItemIO.LoadLegacyInventory(player.dye, reader);
}
if ((flags[0] & 4) == 4) {
ItemIO.LoadLegacyInventory(player.inventory, reader, true, true);
}
if ((flags[0] & 8) == 8) {
ItemIO.LoadLegacyInventory(player.miscEquips, reader);
}
if ((flags[0] & 16) == 16) {
ItemIO.LoadLegacyInventory(player.miscDyes, reader);
}
if ((flags[0] & 32) == 32) {
ItemIO.LoadLegacyInventory(player.bank.item, reader, true);
}
if ((flags[0] & 64) == 64) {
ItemIO.LoadLegacyInventory(player.bank2.item, reader, true);
}
if ((flags[0] & 128) == 128) {
LoadLegacyModData(player, reader);
}
if ((flags[1] & 1) == 1) {
LoadLegacyModBuffs(player, reader);
}
}

private static void LoadLegacyModData(Player player, BinaryReader reader) {
int count = reader.ReadUInt16();
for (int k = 0; k < count; k++) {
string modName = reader.ReadString();
string name = reader.ReadString();
byte[] data = reader.ReadBytes(reader.ReadUInt16());
Mod mod = ModLoader.GetMod(modName);
ModPlayer modPlayer = mod == null ? null : player.GetModPlayer(mod, name);
if (modPlayer != null) {
using (MemoryStream stream = new MemoryStream(data)) {
using (BinaryReader customReader = new BinaryReader(stream)) {
try {
modPlayer.LoadLegacy(customReader);
}
catch (Exception e) {
throw new CustomModDataException(mod,
"Error in reading custom player data for " + mod.Name, e);
}
}
}
}
else {
var tag = new TagCompound {
["mod"] = modName,
["name"] = name,
["legacyData"] = data
};
player.GetModPlayer<MysteryPlayer>().data.Add(tag);
}
}
}

private static void LoadLegacyModBuffs(Player player, BinaryReader reader) {
int num = reader.ReadByte();
int minusIndex = 0;
for (int k = 0; k < num; k++) {
int index = reader.ReadByte() - minusIndex;
string modName = reader.ReadString();
string name = reader.ReadString();
int time = reader.ReadInt32();
Mod mod = ModLoader.GetMod(modName);
int type = mod == null ? 0 : mod.BuffType(name);
if (type > 0) {
for (int j = Player.MaxBuffs - 1; j > index; j--) {
player.buffType[j] = player.buffType[j - 1];
player.buffTime[j] = player.buffTime[j - 1];
}
player.buffType[index] = type;
player.buffTime[index] = time;
}
else {
minusIndex++;
}
}
for (int k = 1; k < Player.MaxBuffs; k++) {
if (player.buffType[k] > 0) {
int j = k - 1;
while (player.buffType[j] == 0) {
player.buffType[j] = player.buffType[j + 1];
player.buffTime[j] = player.buffTime[j + 1];
player.buffType[j + 1] = 0;
player.buffTime[j + 1] = 0;
j--;
}
}
}
}

internal static void LoadUsedMods(Player player, IList<string> usedMods) {
player.usedMods = usedMods;
}
Expand Down
Loading

0 comments on commit 991c437

Please sign in to comment.