Inheritance: ModelBase
Beispiel #1
0
        /// <summary>
        /// Attempts to encrypt and save a player object.
        /// </summary>
        /// <param name="player"></param>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public bool SaveProfile(Player player, string fileName)
        {
            FileStream fStream = null;
            BinaryWriter bWriter = null;

            try
            {
                // Open or create temp file to write to..
                fStream = new FileStream(fileName + ".tmp", FileMode.OpenOrCreate, FileAccess.Write);
                bWriter = new BinaryWriter(fStream);

                // Write the game version..
                bWriter.Write(player.GameVersion);

                // Write basic player info..
                bWriter.Write(player.Name);
                bWriter.Write(player.Difficulty);
                bWriter.Write(player.Hair);

                // Write hair dye and hide visual flag..
                bWriter.Write((byte)player.HairDye);
                bWriter.Write((byte)player.HideVisual);

                bWriter.Write(player.IsMale);
                bWriter.Write(player.Health);
                bWriter.Write(player.HealthMax);
                bWriter.Write(player.Mana);
                bWriter.Write(player.ManaMax);

                // Write player colors..
                bWriter.Write(player.HairColor.ToBytes());
                bWriter.Write(player.SkinColor.ToBytes());
                bWriter.Write(player.EyeColor.ToBytes());
                bWriter.Write(player.ShirtColor.ToBytes());
                bWriter.Write(player.UndershirtColor.ToBytes());
                bWriter.Write(player.PantsColor.ToBytes());
                bWriter.Write(player.ShoesColor.ToBytes());

                // Write player armor..
                for (var x = 0; x < 3; x++)
                {
                    bWriter.Write(player.Armor[x].NetID);
                    bWriter.Write(player.Armor[x].Prefix);
                }

                // Write player accessories..
                for (var x = 0; x < 5; x++)
                {
                    bWriter.Write(player.Accessories[x].NetID);
                    bWriter.Write(player.Accessories[x].Prefix);
                }

                // Write player vanity items..
                for (var x = 0; x < 3; x++)
                {
                    bWriter.Write(player.Vanity[x].NetID);
                    bWriter.Write(player.Vanity[x].Prefix);
                }

                // Write social accessories items..
                for (var x = 0; x < 5; x++)
                {
                    bWriter.Write(player.SocialAccessories[x].NetID);
                    bWriter.Write(player.SocialAccessories[x].Prefix);
                }

                // Write player dye..
                for (var x = 0; x < 8; x++)
                {
                    bWriter.Write(player.Dye[x].NetID);
                    bWriter.Write(player.Dye[x].Prefix);
                }

                // Write player inventory..
                for (var x = 0; x < 58; x++)
                {
                    bWriter.Write(player.Inventory[x].NetID);
                    bWriter.Write(player.Inventory[x].Count);
                    bWriter.Write(player.Inventory[x].Prefix);
                }

                // Write player bank1..
                for (var x = 0; x < 40; x++)
                {
                    bWriter.Write(player.Bank1[x].NetID);
                    bWriter.Write(player.Bank1[x].Count);
                    bWriter.Write(player.Bank1[x].Prefix);
                }

                // Write player bank2..
                for (var x = 0; x < 40; x++)
                {
                    bWriter.Write(player.Bank2[x].NetID);
                    bWriter.Write(player.Bank2[x].Count);
                    bWriter.Write(player.Bank2[x].Prefix);
                }

                // Write player buffs..
                var buffCount = (player.GameVersion < 74) ? 10 : 22;
                for (var x = 0; x < buffCount; x++)
                {
                    bWriter.Write(player.Buffs[x].Id);
                    bWriter.Write(player.Buffs[x].Duration);
                }

                // Write server entries..
                for (var x = 0; x < 200; x++)
                {
                    if (player.ServerEntries[x].SpawnX == -1)
                    {
                        bWriter.Write(player.ServerEntries[x].SpawnX);
                        break;
                    }

                    bWriter.Write(player.ServerEntries[x].SpawnX);
                    bWriter.Write(player.ServerEntries[x].SpawnY);
                    bWriter.Write(player.ServerEntries[x].ServerAddress);
                    bWriter.Write(player.ServerEntries[x].ServerName);
                }

                // Write hotbar locked flag..
                bWriter.Write(player.IsHotbarLocked);
                bWriter.Write(player.AnglerQuestsFinished);

                // Cleanup..
                bWriter.Close();
                fStream.Close();

                // Encrypt the file and save it to real path..
                var ret = EncryptProfile(fileName);

                // Delete the temp file..
                File.Delete(fileName + ".tmp");
                return ret;
            }
            catch (Exception ex)
            {
                MessageBox.Show(string.Format("Failed to save profile; error was:\r\n\r\n{0}", ex), "Error!");
                return false;
            }
            finally
            {
                // Ensure streams are cleaned up..
                if (bWriter != null)
                    bWriter.Close();
                if (fStream != null)
                    fStream.Close();
            }
        }
Beispiel #2
0
        /// <summary>
        /// Attempts to decrypt and load a player file.
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public Player LoadProfile(string fileName)
        {
            try
            {
                // Attempt to decrypt the player file..
                var playerData = DecryptProfile(fileName);
                if (playerData == null)
                    return default(Player);

                // Attempt to read the player file..
                using (var mStream = new MemoryStream(playerData))
                {
                    using (var bReader = new BinaryReader(mStream))
                    {
                        // Create the new player object..
                        var p = new Player { GameVersion = bReader.ReadInt32() };

                        // Ensure game version is valid..
                        if (p.GameVersion < Terraria.GameVersions.Min() || p.GameVersion > Terraria.GameVersions.Max())
                            throw new InvalidDataException("GameVersion does not match the supported value!");

                        // Read basic player info..
                        p.Name = bReader.ReadString();
                        p.Difficulty = bReader.ReadByte();
                        p.Hair = bReader.ReadInt32();

                        // Hair Dye
                        if (p.GameVersion >= 82)
                            p.HairDye = bReader.ReadByte();

                        // Hide Visual
                        if (p.GameVersion >= 83)
                            p.HideVisual = bReader.ReadByte();

                        p.IsMale = bReader.ReadBoolean();
                        p.Health = bReader.ReadInt32();
                        p.HealthMax = bReader.ReadInt32();
                        p.Mana = bReader.ReadInt32();
                        p.ManaMax = bReader.ReadInt32();

                        // Read character colors..
                        p.HairColor = new Color().FromBytes(bReader.ReadBytes(3));
                        p.SkinColor = new Color().FromBytes(bReader.ReadBytes(3));
                        p.EyeColor = new Color().FromBytes(bReader.ReadBytes(3));
                        p.ShirtColor = new Color().FromBytes(bReader.ReadBytes(3));
                        p.UndershirtColor = new Color().FromBytes(bReader.ReadBytes(3));
                        p.PantsColor = new Color().FromBytes(bReader.ReadBytes(3));
                        p.ShoesColor = new Color().FromBytes(bReader.ReadBytes(3));

                        // Read Armor..
                        for (var x = 0; x < 3; x++)
                        {
                            p.Armor[x].SetItem(bReader.ReadInt32());
                            p.Armor[x].Prefix = bReader.ReadByte();
                        }

                        // Read accessories..
                        for (var x = 0; x < 5; x++)
                        {
                            p.Accessories[x].SetItem(bReader.ReadInt32());
                            p.Accessories[x].Prefix = bReader.ReadByte();
                        }

                        // Read vanity items..
                        for (var x = 0; x < 3; x++)
                        {
                            p.Vanity[x].SetItem(bReader.ReadInt32());
                            p.Vanity[x].Prefix = bReader.ReadByte();
                        }

                        // Read social accessories..
                        if (p.GameVersion >= 81)
                        {
                            for (var x = 0; x < 5; x++)
                            {
                                p.SocialAccessories[x].SetItem(bReader.ReadInt32());
                                p.SocialAccessories[x].Prefix = bReader.ReadByte();
                            }
                        }

                        // Read dye items..
                        if (p.GameVersion >= 47)
                        {
                            var dyeCount = (p.GameVersion >= 81) ? 8 : 3;
                            for (var x = 0; x < dyeCount; x++)
                            {
                                p.Dye[x].SetItem(bReader.ReadInt32());
                                p.Dye[x].Prefix = bReader.ReadByte();
                            }
                        }

                        // Read inventory..
                        for (var x = 0; x < ((p.GameVersion >= 58) ? 58 : 48); x++)
                        {
                            var temp = bReader.ReadInt32();
                            if (temp >= Terraria.MaxItemCount)
                                p.Inventory[x].SetItem(0);
                            else
                            {
                                p.Inventory[x].SetItem(temp);
                                p.Inventory[x].Count = bReader.ReadInt32();
                                p.Inventory[x].Prefix = bReader.ReadByte();
                            }
                        }

                        // Read Bank1..
                        for (var x = 0; x < ((p.GameVersion >= 58) ? 40 : 20); x++)
                        {
                            p.Bank1[x].SetItem(bReader.ReadInt32());
                            p.Bank1[x].Count = bReader.ReadInt32();
                            p.Bank1[x].Prefix = bReader.ReadByte();
                        }

                        // Read Bank2..
                        for (var x = 0; x < ((p.GameVersion >= 58) ? 40 : 20); x++)
                        {
                            p.Bank2[x].SetItem(bReader.ReadInt32());
                            p.Bank2[x].Count = bReader.ReadInt32();
                            p.Bank2[x].Prefix = bReader.ReadByte();
                        }

                        // Read Buffs..
                        var buffCount = (p.GameVersion < 74) ? 10 : 22;
                        for (var x = 0; x < buffCount; x++)
                        {
                            p.Buffs[x].SetBuff(bReader.ReadInt32());
                            p.Buffs[x].Duration = bReader.ReadInt32();
                        }

                        // Read Server Entries..
                        for (var x = 0; x < 200; x++)
                        {
                            var temp = bReader.ReadInt32();
                            if (temp == -1) break;

                            p.ServerEntries[x].SpawnX = temp;
                            p.ServerEntries[x].SpawnY = bReader.ReadInt32();
                            p.ServerEntries[x].ServerAddress = bReader.ReadInt32();
                            p.ServerEntries[x].ServerName = bReader.ReadString();
                        }

                        // Read Hotbar Locked Flag..
                        p.IsHotbarLocked = bReader.ReadBoolean();

                        // Read angler quest data..
                        if (p.GameVersion >= 98)
                            p.AnglerQuestsFinished = bReader.ReadInt32();

                        // Force the new profile to latest version..
                        p.GameVersion = Terraria.LatestVersion;
                        return p;
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(string.Format("Failed to load profile; error was:\r\n\r\n{0}", ex), "Error!");
                return default(Player);
            }
        }