// This is where we create the new player file private void CreateNewPlayerFile() { var filename = PlayerFileName; try { File.Delete(filename + ".invbak"); } catch (Exception ex) { // throw your hands in the air } File.Copy(filename, filename + ".invbak"); // before we go crazy, let's back our original up Player player = new Player(); bool flag; var str = filename + ".dec2"; // this will hold the decrypted data var strEdit = filename + ".edit"; // this will hold unencrypted data with our new items var strEditEnc = filename + ".editenc"; // this will hold the new items, encrypted flag = Player.DecryptFile(filename, str); if (!flag) { using (FileStream fileStream = new FileStream(str, FileMode.Open)) { using (BinaryReader binaryReader = new BinaryReader((Stream)fileStream)) { var newFile = new FileStream(strEdit, FileMode.Create); var bw = new BinaryWriter((Stream)newFile); bw.Write(binaryReader.ReadInt32()); bw.Write(binaryReader.ReadString()); bw.Write(binaryReader.ReadInt32()); bw.Write(binaryReader.ReadInt32()); bw.Write(binaryReader.ReadInt32()); bw.Write(binaryReader.ReadInt32()); bw.Write(binaryReader.ReadInt32()); bw.Write(binaryReader.ReadByte()); bw.Write(binaryReader.ReadByte()); bw.Write(binaryReader.ReadByte()); bw.Write(binaryReader.ReadByte()); bw.Write(binaryReader.ReadByte()); bw.Write(binaryReader.ReadByte()); bw.Write(binaryReader.ReadByte()); bw.Write(binaryReader.ReadByte()); bw.Write(binaryReader.ReadByte()); bw.Write(binaryReader.ReadByte()); bw.Write(binaryReader.ReadByte()); bw.Write(binaryReader.ReadByte()); bw.Write(binaryReader.ReadByte()); bw.Write(binaryReader.ReadByte()); bw.Write(binaryReader.ReadByte()); bw.Write(binaryReader.ReadByte()); bw.Write(binaryReader.ReadByte()); bw.Write(binaryReader.ReadByte()); bw.Write(binaryReader.ReadByte()); bw.Write(binaryReader.ReadByte()); bw.Write(binaryReader.ReadByte()); for (int index = 0; index < 8; ++index) { bw.Write(binaryReader.ReadString()); } if (listInventory.Items.Count != 44) { throw new Exception("Numbers not matching up"); } foreach (var i in listInventory.Items) { var oldItem = binaryReader.ReadString(); var oldQty = binaryReader.ReadInt32(); var selVal = i.ToString(); var selName = selVal.Substring(selVal.IndexOf(" - ") + 3, selVal.LastIndexOf(" (") - selVal.IndexOf(" - ") - 3); var selQty = selVal.Substring(selVal.LastIndexOf(" (") + 2, selVal.LastIndexOf(")") - selVal.LastIndexOf(" (") - 2); bw.Write(selName.ToString()); bw.Write(Convert.ToInt32(selQty)); } for (int index = 0; index < Chest.maxItems; ++index) { bw.Write(binaryReader.ReadString()); bw.Write(binaryReader.ReadInt32()); } for (int index = 0; index < 200; ++index) { int num = binaryReader.ReadInt32(); bw.Write(num); if (num != -1) { bw.Write(binaryReader.ReadInt32()); bw.Write(binaryReader.ReadInt32()); bw.Write(binaryReader.ReadString()); } else break; } binaryReader.Close(); bw.Close(); Player.EncryptFile(strEdit, strEditEnc); File.Delete(filename); File.Copy(strEditEnc, filename); // display information lblPlayerResults.Text = "Player: " + player.name; } } } }
// For choosing the player file private void button2_Click(object sender, EventArgs e) { var file = new OpenFileDialog(); file.Title = "Choose Character File"; file.Filter = "PLR Files|*.plr"; file.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) + "\\Documents\\My Games\\Terraria\\Players"; if (file.ShowDialog() == DialogResult.OK) { var filename = file.FileName.ToString(); PlayerFileName = filename; // save for later use Player player = new Player(); bool flag; //string str = playerPath + ".dat"; var str = filename + ".dec"; // this will hold the decrypted data flag = Player.DecryptFile(filename, str); if (!flag) { using (FileStream fileStream = new FileStream(str, FileMode.Open)) { using (BinaryReader binaryReader = new BinaryReader((Stream)fileStream)) { binaryReader.ReadInt32(); player.name = binaryReader.ReadString(); player.hair = binaryReader.ReadInt32(); player.statLife = binaryReader.ReadInt32(); player.statLifeMax = binaryReader.ReadInt32(); player.statMana = binaryReader.ReadInt32(); player.statManaMax = binaryReader.ReadInt32(); player.hairColor.R = binaryReader.ReadByte(); player.hairColor.G = binaryReader.ReadByte(); player.hairColor.B = binaryReader.ReadByte(); player.skinColor.R = binaryReader.ReadByte(); player.skinColor.G = binaryReader.ReadByte(); player.skinColor.B = binaryReader.ReadByte(); player.eyeColor.R = binaryReader.ReadByte(); player.eyeColor.G = binaryReader.ReadByte(); player.eyeColor.B = binaryReader.ReadByte(); player.shirtColor.R = binaryReader.ReadByte(); player.shirtColor.G = binaryReader.ReadByte(); player.shirtColor.B = binaryReader.ReadByte(); player.underShirtColor.R = binaryReader.ReadByte(); player.underShirtColor.G = binaryReader.ReadByte(); player.underShirtColor.B = binaryReader.ReadByte(); player.pantsColor.R = binaryReader.ReadByte(); player.pantsColor.G = binaryReader.ReadByte(); player.pantsColor.B = binaryReader.ReadByte(); player.shoeColor.R = binaryReader.ReadByte(); player.shoeColor.G = binaryReader.ReadByte(); player.shoeColor.B = binaryReader.ReadByte(); for (int index = 0; index < 8; ++index) { player.armor[index] = new Item(); player.armor[index].name = binaryReader.ReadString(); } listInventory.Items.Clear(); for (int index = 0; index < 44; ++index) { player.inventory[index] = new Item(); player.inventory[index].name = binaryReader.ReadString(); player.inventory[index].stack = binaryReader.ReadInt32(); listInventory.Items.Add((index+1).ToString() + " - " + player.inventory[index].name + " (" + player.inventory[index].stack + ")"); } for (int index = 0; index < Chest.maxItems; ++index) { player.bank[index] = new Item(); player.bank[index].name = binaryReader.ReadString(); player.bank[index].stack = binaryReader.ReadInt32(); } for (int index = 0; index < 200; ++index) { int num = binaryReader.ReadInt32(); if (num != -1) { player.spX[index] = num; player.spY[index] = binaryReader.ReadInt32(); player.spI[index] = binaryReader.ReadInt32(); player.spN[index] = binaryReader.ReadString(); } else break; } binaryReader.Close(); // display information lblPlayerResults.Text = "Player: " + player.name; } } } } }