//add near end of Terraria.IO.WorldFile.saveWorld before releasing locks internal static void Save(string path, bool isCloudSave) { path = Path.ChangeExtension(path, ".twld"); if (FileUtilities.Exists(path, isCloudSave)) { FileUtilities.Copy(path, path + ".bak", isCloudSave); } var tag = new TagCompound { ["chests"] = SaveChests(), ["tiles"] = TileIO.SaveTiles(), ["containers"] = TileIO.SaveContainers(), ["tileEntities"] = TileIO.SaveTileEntities(), ["killCounts"] = SaveNPCKillCounts(), ["anglerQuest"] = SaveAnglerQuest(), ["modData"] = SaveModData() }; var stream = new MemoryStream(); TagIO.ToStream(tag, stream); var data = stream.ToArray(); FileUtilities.Write(path, data, data.Length, isCloudSave); }
public static string ToBase64(Item item) { MemoryStream ms = new MemoryStream(); TagIO.ToStream(ItemIO.Save(item), ms, true); return(Convert.ToBase64String(ms.ToArray())); }
//if value is null, calls RemoveTag, also performs type checking public void Set(string key, object value, bool replace = false) { if (value == null) { Remove(key); return; } object serialized; try { serialized = TagIO.Serialize(value); } catch (IOException e) { var valueInfo = "value=" + value; if (value.GetType().ToString() != value.ToString()) { valueInfo = "type=" + value.GetType() + "," + valueInfo; } throw new IOException($"NBT Serialization (key={key},{valueInfo})", e); } if (replace) { dict[key] = serialized; } else { dict.Add(key, serialized); } }
//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); if (buf[0] != 0x1F || buf[1] != 0x8B) { LoadLegacy(player, buf); return; } var tag = TagIO.FromStream(new MemoryStream(buf)); LoadInventory(player.armor, tag.GetList <TagCompound>("armor")); LoadInventory(player.dye, tag.GetList <TagCompound>("dye")); LoadInventory(player.inventory, tag.GetList <TagCompound>("inventory")); LoadInventory(player.miscEquips, tag.GetList <TagCompound>("miscEquips")); LoadInventory(player.miscDyes, tag.GetList <TagCompound>("miscDyes")); LoadInventory(player.bank.item, tag.GetList <TagCompound>("bank")); LoadInventory(player.bank2.item, tag.GetList <TagCompound>("bank2")); LoadInventory(player.bank3.item, tag.GetList <TagCompound>("bank3")); LoadModData(player, tag.GetList <TagCompound>("modData")); LoadModBuffs(player, tag.GetList <TagCompound>("modBuffs")); LoadUsedMods(player, tag.GetList <string>("usedMods")); }
//make Terraria.Player.ENCRYPTION_KEY internal //add to end of Terraria.Player.SavePlayer internal static void Save(Player player, string path, bool isCloudSave) { path = Path.ChangeExtension(path, ".tplr"); if (FileUtilities.Exists(path, isCloudSave)) { FileUtilities.Copy(path, path + ".bak", isCloudSave); } var tag = new TagCompound { ["armor"] = SaveInventory(player.armor), ["dye"] = SaveInventory(player.dye), ["inventory"] = SaveInventory(player.inventory), ["miscEquips"] = SaveInventory(player.miscEquips), ["miscDyes"] = SaveInventory(player.miscDyes), ["bank"] = SaveInventory(player.bank.item), ["bank2"] = SaveInventory(player.bank2.item), ["bank3"] = SaveInventory(player.bank3.item), ["modData"] = SaveModData(player), ["modBuffs"] = SaveModBuffs(player), ["usedMods"] = SaveUsedMods(player) }; using (Stream stream = isCloudSave ? (Stream) new MemoryStream() : (Stream) new FileStream(path, FileMode.Create)) { TagIO.ToStream(tag, stream); if (isCloudSave && SocialAPI.Cloud != null) { SocialAPI.Cloud.Write(path, ((MemoryStream)stream).ToArray()); } } }
public object Clone() { var copy = new TagCompound(); foreach (var entry in this) { copy.Set(entry.Key, TagIO.Clone(entry.Value)); } return(copy); }
public T Get <T>(string key) { dict.TryGetValue(key, out object tag); try { return(TagIO.Deserialize <T>(tag)); } catch (Exception e) { throw new IOException( $"NBT Deserialization (type={typeof(T)}," + $"entry={TagPrinter.Print(new KeyValuePair<string, object>(key, tag))})", e); } }
//if value is null, calls RemoveTag, also performs type checking public void SetTag(string key, object value) { if (value == null) { RemoveTag(key); } else { TagIO.TypeCheck(value.GetType()); dict.Add(key, value); } }
//add near end of Terraria.IO.WorldFile.loadWorld before setting failure and success internal static void Load(string path, bool isCloudSave) { customDataFail = null; path = Path.ChangeExtension(path, ".twld"); if (!FileUtilities.Exists(path, isCloudSave)) { return; } var buf = FileUtilities.ReadAllBytes(path, isCloudSave); if (buf[0] != 0x1F || buf[1] != 0x8B) { LoadLegacy(buf); return; } var tag = TagIO.FromStream(new MemoryStream(buf)); LoadChests(tag.GetList <TagCompound>("chests")); TileIO.LoadTiles(tag.GetCompound("tiles")); TileIO.LoadContainers(tag.GetCompound("containers")); LoadNPCs(tag.GetList <TagCompound>("npcs")); try { TileIO.LoadTileEntities(tag.GetList <TagCompound>("tileEntities")); } catch (CustomModDataException e) { customDataFail = e; throw; } LoadNPCKillCounts(tag.GetList <TagCompound>("killCounts")); LoadAnglerQuest(tag.GetCompound("anglerQuest")); LoadTownManager(tag.GetList <TagCompound>("townManager")); try { LoadModData(tag.GetList <TagCompound>("modData")); } catch (CustomModDataException e) { customDataFail = e; throw; } }
//if value is null, calls RemoveTag, also performs type checking public void Set(string key, object value) { if (value == null) { Remove(key); return; } try { dict.Add(key, TagIO.Serialize(value)); } catch (IOException e) { var valueInfo = "value=" + value; if (value.GetType().ToString() != value.ToString()) { valueInfo = "type=" + value.GetType() + "," + valueInfo; } throw new IOException($"NBT Serialization (key={key},{valueInfo})", e); } }
public static Item FromBase64(string base64) { MemoryStream ms = new MemoryStream(Convert.FromBase64String(base64)); return(ItemIO.Load(TagIO.FromStream(ms, true))); }