/// <summary>
        /// Sends a CharacterCreate packet to a CityServer.
        /// </summary>
        /// <param name="LoginArgs">Arguments used to log onto the CityServer.</param>
        /// <param name="Character">The character to create on the CityServer.</param>
        public static void SendCharacterCreateCity(LoginArgsContainer LoginArgs, TSOClient.VM.Sim Character)
        {
            PacketStream Packet = new PacketStream((byte)PacketType.CHARACTER_CREATE_CITY, 0);

            Packet.WriteHeader();

            byte[]       EncryptionKey = LoginArgs.Enc.GetDecryptionArgsContainer().ARC4DecryptArgs.EncryptionKey;
            MemoryStream PacketData    = new MemoryStream();
            BinaryWriter Writer        = new BinaryWriter(PacketData);

            Writer.Write((byte)LoginArgs.Username.Length);
            Writer.Write(Encoding.ASCII.GetBytes(LoginArgs.Username), 0, Encoding.ASCII.GetBytes(LoginArgs.Username).Length);

            //LoginArgs password is set to PlayerAccount.Hash for relogging to the cityserver.
            byte[] HashBuf = Convert.FromBase64String(LoginArgs.Password);
            Writer.Write((byte)HashBuf.Length);
            Writer.Write(HashBuf, 0, HashBuf.Length);

            Writer.Write((byte)EncryptionKey.Length);
            Writer.Write(EncryptionKey);
            Writer.Write(PlayerAccount.CityToken);
            Writer.Write(Character.Timestamp);
            Writer.Write(Character.Name);
            Writer.Write(Character.Sex);
            Writer.Write(Character.Description);
            Writer.Write((ulong)Character.HeadOutfitID);
            Writer.Write((ulong)Character.BodyOutfitID);
            Writer.Write((byte)Character.AppearanceType);

            Packet.WriteUInt16((ushort)((ushort)PacketHeaders.UNENCRYPTED + PacketData.Length));
            Packet.WriteBytes(PacketData.ToArray());
            Writer.Close();

            LoginArgs.Client.Send(Packet.ToArray());
        }
 /// <summary>
 /// Requests a token from the LoginServer, that can be used to log into a CityServer.
 /// </summary>
 /// <param name="Client">A NetworkClient instance.</param>
 public static void RequestCityToken(NetworkClient Client, Sim SelectedCharacter)
 {
     PacketStream Packet = new PacketStream((byte)PacketType.REQUEST_CITY_TOKEN, 0);
     Packet.WritePascalString(Client.ClientEncryptor.Username);
     Packet.WritePascalString(SelectedCharacter.ResidingCity.UUID);
     Packet.WritePascalString(SelectedCharacter.GUID.ToString());
     Client.SendEncrypted((byte)PacketType.REQUEST_CITY_TOKEN, Packet.ToArray());
 }
        /// <summary>
        /// LoginServer sent information about the player's characters.
        /// </summary>
        /// <param name="Packet">The packet that was received.</param>
        public static void OnCharacterInfoResponse(PacketStream Packet, NetworkClient Client)
        {
            byte Opcode = (byte)Packet.ReadByte();
            byte Length = (byte)Packet.ReadByte();
            byte DecryptedLength = (byte)Packet.ReadByte();

            Packet.DecryptPacket(PlayerAccount.EncKey, new DESCryptoServiceProvider(), DecryptedLength);

            //If the decrypted length == 1, it means that there were 0
            //characters that needed to be updated, or that the user
            //hasn't created any characters on his/her account yet.
            //Since the Packet.Length property is equal to the length
            //of the encrypted data, it cannot be used to get the length
            //of the decrypted data.
            if (DecryptedLength > 1)
            {
                byte NumCharacters = (byte)Packet.ReadByte();
                List<Sim> FreshSims = new List<Sim>();

                for (int i = 0; i < NumCharacters; i++)
                {
                    int CharacterID = Packet.ReadInt32();

                    Sim FreshSim = new Sim(Packet.ReadString());
                    FreshSim.CharacterID = CharacterID;
                    FreshSim.Timestamp = Packet.ReadString();
                    FreshSim.Name = Packet.ReadString();
                    FreshSim.Sex = Packet.ReadString();

                    FreshSims.Add(FreshSim);
                }

                NetworkFacade.Avatars = FreshSims;
                CacheSims(FreshSims);
            }

            PacketStream CityInfoRequest = new PacketStream(0x06, 0);
            CityInfoRequest.WriteByte(0x00); //Dummy

            Client.SendEncrypted(0x06, CityInfoRequest.ToArray());
        }
        /// <summary>
        /// Sends a CharacterCreate packet to the LoginServer.
        /// </summary>
        /// <param name="Character">The character to create.</param>
        /// <param name="TimeStamp">The timestamp of when this character was created.</param>
        public static void SendCharacterCreate(TSOClient.VM.Sim Character, string TimeStamp)
        {
            PacketStream Packet = new PacketStream((byte)PacketType.CHARACTER_CREATE, 0);

            Packet.WritePascalString(PlayerAccount.Client.ClientEncryptor.Username);
            Packet.WritePascalString(TimeStamp);
            Packet.WritePascalString(Character.Name);
            Packet.WritePascalString(Character.Sex);
            Packet.WritePascalString(Character.Description);
            Packet.WriteUInt64(Character.HeadOutfitID);
            Packet.WriteUInt64(Character.BodyOutfitID);
            Packet.WriteByte((byte)Character.AppearanceType);

            Packet.WritePascalString(Character.ResidingCity.Name);
            Packet.WriteUInt64(Character.ResidingCity.Thumbnail);
            Packet.WritePascalString(Character.ResidingCity.UUID);
            Packet.WriteUInt64(Character.ResidingCity.Map);
            Packet.WritePascalString(Character.ResidingCity.IP);
            Packet.WriteInt32(Character.ResidingCity.Port);

            byte[] PacketData = Packet.ToArray();
            PlayerAccount.Client.SendEncrypted((byte)PacketType.CHARACTER_CREATE, PacketData);
        }
        /// <summary>
        /// Caches sims received from the LoginServer to the disk.
        /// </summary>
        /// <param name="FreshSims">A list of the sims received by the LoginServer.</param>
        private static void CacheSims(List<Sim> FreshSims)
        {
            if (!Directory.Exists("CharacterCache"))
                Directory.CreateDirectory("CharacterCache");

            BinaryWriter Writer = new BinaryWriter(File.Create("CharacterCache\\Sims.tempcache"));

            Writer.Write(FreshSims.Count);

            foreach (Sim S in FreshSims)
            {
                //Length of the current entry, so its skippable...
                Writer.Write((int)4 + S.GUID.Length + S.Timestamp.Length + S.Name.Length + S.Sex.Length);
                Writer.Write(S.CharacterID);
                Writer.Write(S.GUID);
                Writer.Write(S.Timestamp);
                Writer.Write(S.Name);
                Writer.Write(S.Sex);
            }

            if (File.Exists("CharacterCache\\Sims.cache"))
            {
                BinaryReader Reader = new BinaryReader(File.Open("CharacterCache\\Sims.cache", FileMode.Open));
                int NumSims = Reader.ReadInt32();

                List<Sim> UnchangedSims = new List<Sim>();

                if (NumSims > FreshSims.Count)
                {
                    if (NumSims == 2)
                    {
                        //Skips the first entry.
                        Reader.BaseStream.Position = Reader.ReadInt32();

                        Reader.ReadInt32(); //Length of second entry.
                        string GUID = Reader.ReadString();

                        Sim S = new Sim(GUID);

                        S.CharacterID = Reader.ReadInt32();
                        S.Timestamp = Reader.ReadString();
                        S.Name = Reader.ReadString();
                        S.Sex = Reader.ReadString();
                        UnchangedSims.Add(S);
                    }
                    else if (NumSims == 3)
                    {
                        //Skips the first entry.
                        Reader.BaseStream.Position = Reader.ReadInt32();

                        Reader.ReadInt32(); //Length of second entry.
                        string GUID = Reader.ReadString();

                        Sim S = new Sim(GUID);

                        S.CharacterID = Reader.ReadInt32();
                        S.Timestamp = Reader.ReadString();
                        S.Name = Reader.ReadString();
                        S.Sex = Reader.ReadString();
                        UnchangedSims.Add(S);

                        Reader.ReadInt32(); //Length of third entry.
                        S.CharacterID = Reader.ReadInt32();
                        S.Timestamp = Reader.ReadString();
                        S.Name = Reader.ReadString();
                        S.Sex = Reader.ReadString();
                        UnchangedSims.Add(S);
                    }

                    Reader.Close();

                    foreach (Sim S in UnchangedSims)
                    {
                        //Length of the current entry, so its skippable...
                        Writer.Write((int)4 + S.Timestamp.Length + S.Name.Length + S.Sex.Length);
                        Writer.Write(S.CharacterID);
                        Writer.Write(S.Timestamp);
                        Writer.Write(S.Name);
                        Writer.Write(S.Sex);
                    }
                }
            }

            Writer.Close();

            if (File.Exists("CharacterCache\\Sims.cache"))
                File.Delete("CharacterCache\\Sims.cache");

            File.Move("CharacterCache\\Sims.tempcache", "CharacterCache\\Sims.cache");
        }
        public PersonSelection()
        {
            UIScript ui = null;
            if (GlobalSettings.Default.ScaleUI)
            {
                ui = this.RenderScript("personselection.uis");
                this.Scale800x600 = true;
            }
            else
            {
                ui = this.RenderScript("personselection" + (ScreenWidth == 1024 ? "1024" : "") + ".uis");
            }

            m_ExitButton = (UIButton)ui["ExitButton"];

            var numSlots = 3;
            m_PersonSlots = new List<PersonSlot>();

            for (var i = 0; i < numSlots; i++)
            {
                var index = (i + 1).ToString();

                /** Tab Background **/
                var tabBackground = ui.Create<UIImage>("TabBackgroundImage" + index);
                this.Add(tabBackground);

                var enterTabImage = ui.Create<UIImage>("EnterTabImage" + index);
                this.Add(enterTabImage);

                var descTabImage = ui.Create<UIImage>("DescriptionTabImage" + index);
                this.Add(descTabImage);

                var descTabBgImage = ui.Create<UIImage>("DescriptionTabBackgroundImage" + index);
                var enterIcons = ui.Create<UIImage>("EnterTabBackgroundImage" + index);

                var personSlot = new PersonSlot(this)
                {
                    AvatarButton = (UIButton)ui["AvatarButton" + index],
                    CityButton = (UIButton)ui["CityButton" + index],
                    HouseButton = (UIButton)ui["HouseButton" + index],
                    EnterTabButton = (UIButton)ui["EnterTabButton" + index],
                    DescTabButton = (UIButton)ui["DescriptionTabButton" + index],
                    NewAvatarButton = (UIButton)ui["NewAvatarButton" + index],
                    DeleteAvatarButton = (UIButton)ui["DeleteAvatarButton" + index],
                    PersonNameText = (UILabel)ui["PersonNameText" + index],
                    PersonDescriptionScrollUpButton = (UIButton)ui["PersonDescriptionScrollUpButton" + index],
                    PersonDescriptionScrollDownButton = (UIButton)ui["PersonDescriptionScrollDownButton" + index],
                    PersonDescriptionSlider = (UISlider)ui["PersonDescriptionSlider" + index],
                    CityNameText = (UILabel)ui["CityNameText" + index],
                    HouseNameText = (UILabel)ui["HouseNameText" + index],
                    PersonDescriptionText = (UITextEdit)ui["PersonDescriptionText" + index],
                    DescriptionTabBackgroundImage = descTabBgImage,
                    EnterTabBackgroundImage = enterIcons,

                    TabBackground = tabBackground,
                    TabEnterBackground = enterTabImage,
                    TabDescBackground = descTabImage
                };

                this.AddBefore(descTabBgImage, personSlot.PersonDescriptionText);
                this.AddBefore(enterIcons, personSlot.CityButton);

                personSlot.Init();
                personSlot.SetSlotAvaliable(true);
                m_PersonSlots.Add(personSlot);

                if (i < NetworkFacade.Avatars.Count)
                {
                    personSlot.DisplayAvatar(NetworkFacade.Avatars[i]);
                    personSlot.AvatarButton.OnButtonClick += new ButtonClickDelegate(AvatarButton_OnButtonClick);

                    var simBox = new UISim();
                    var sim = new Sim(Guid.NewGuid().ToString());

                    sim.HeadOutfitID = NetworkFacade.Avatars[i].HeadOutfitID;
                    sim.BodyOutfitID = NetworkFacade.Avatars[i].BodyOutfitID;

                    sim.AppearanceType = NetworkFacade.Avatars[i].AppearanceType;

                    simBox.Avatar.Body = Content.Get().AvatarOutfits.Get(sim.BodyOutfitID);
                    simBox.Avatar.Head = Content.Get().AvatarOutfits.Get(sim.HeadOutfitID);
                    simBox.Avatar.Appearance = sim.AppearanceType;

                    simBox.Position = m_PersonSlots[i].AvatarButton.Position + new Vector2(70, (m_PersonSlots[i].AvatarButton.Size.Y - 35));
                    simBox.Size = m_PersonSlots[i].AvatarButton.Size;

                    this.Add(simBox);
                }
            }

            this.AddAt(0, new UIImage(BackgroundImage));
            if (BackgroundImageDialog != null)
            {
                this.AddAt(1, new UIImage(BackgroundImageDialog)
                {
                    X = 112,
                    Y = 84
                });
            }

            /**
             * Button plumbing
             */
            CreditsButton.OnButtonClick += new ButtonClickDelegate(CreditsButton_OnButtonClick);
            m_ExitButton.OnButtonClick += new ButtonClickDelegate(m_ExitButton_OnButtonClick);

            /**
             * Music
             */
            var tracks = new string[]{
                GlobalSettings.Default.StartupPath + "\\music\\modes\\select\\tsosas1_v2.mp3",
                GlobalSettings.Default.StartupPath + "\\music\\modes\\select\\tsosas2_v2.mp3",
                GlobalSettings.Default.StartupPath + "\\music\\modes\\select\\tsosas3.mp3",
                GlobalSettings.Default.StartupPath + "\\music\\modes\\select\\tsosas4.mp3",
                GlobalSettings.Default.StartupPath + "\\music\\modes\\select\\tsosas5.mp3"
            };
            PlayBackgroundMusic(
                tracks
            );

            NetworkFacade.Controller.OnCityToken += new OnCityTokenDelegate(Controller_OnCityToken);
        }
        /// <summary>
        /// Loads a head mesh.
        /// </summary>
        /// <param name="MeshID">The ID of the mesh to load.</param>
        /// <param name="TexID">The ID of the texture to load.</param>
        public void LoadHeadMesh(Sim Character, Outfit Outf, int SkinColor)
        {
            Appearance Apr;

            switch (SkinColor)
            {
                case 0:
                    Apr = new Appearance(ContentManager.GetResourceFromLongID(Outf.LightAppearanceID));
                    break;
                case 1:
                    Apr = new Appearance(ContentManager.GetResourceFromLongID(Outf.MediumAppearanceID));
                    break;
                case 2:
                    Apr = new Appearance(ContentManager.GetResourceFromLongID(Outf.DarkAppearanceID));
                    break;
                default:
                    Apr = new Appearance(ContentManager.GetResourceFromLongID(Outf.LightAppearanceID));
                    break;
            }

            Binding Bnd = new Binding(ContentManager.GetResourceFromLongID(Apr.BindingIDs[0]));

            if (m_CurrentSims.Count > 0)
            {
                if (!m_SingleRenderer)
                {
                    m_Effects.Add(new BasicEffect(m_Scene.SceneMgr.Device, null));
                    m_CurrentSims.Add(Character);

                    Skeleton SimSkeleton = m_CurrentSims[m_CurrentSims.Count - 1].SimSkeleton;

                    //m_CurrentSims[m_CurrentSims.Count - 1].HeadMesh = new Mesh();
                    //m_CurrentSims[m_CurrentSims.Count - 1].HeadMesh.
                    //    Read(ContentManager.GetResourceFromLongID(Bnd.MeshAssetID));
                    //m_CurrentSims[m_CurrentSims.Count - 1].SimSkeleton.ComputeBonePositions(SimSkeleton.RootBone,
                    //    GameFacade.Scenes.WorldMatrix);
                    //m_CurrentSims[m_CurrentSims.Count - 1].HeadMesh.ProcessMesh();

                    //m_CurrentSims[m_CurrentSims.Count - 1].HeadTexture = Texture2D.FromFile(m_Scene.SceneMgr.Device,
                    //    new MemoryStream(ContentManager.GetResourceFromLongID(Bnd.TextureAssetID)));
                }
                else
                {
                    Skeleton SimSkeleton = m_CurrentSims[0].SimSkeleton;

                    //m_Effects[0] = new BasicEffect(m_Scene.SceneMgr.Device, null);
                    //m_CurrentSims[0].HeadMesh = new Mesh();
                    //m_CurrentSims[0].HeadMesh.Read(ContentManager.GetResourceFromLongID(Bnd.MeshAssetID));
                    //m_CurrentSims[0].SimSkeleton.ComputeBonePositions(SimSkeleton.RootBone,
                    //    GameFacade.Scenes.WorldMatrix);
                    //m_CurrentSims[0].HeadMesh.ProcessMesh();

                    //m_CurrentSims[0].HeadTexture = Texture2D.FromFile(m_Scene.SceneMgr.Device,
                    //    new MemoryStream(ContentManager.GetResourceFromLongID(Bnd.TextureAssetID)));
                }
            }
            else
            {
                m_Effects.Add(new BasicEffect(m_Scene.SceneMgr.Device, null));
                m_CurrentSims.Add(Character);

                Skeleton SimSkeleton = m_CurrentSims[0].SimSkeleton;

                //m_CurrentSims[0].HeadMesh = new Mesh();
                //m_CurrentSims[0].HeadMesh.Read(ContentManager.GetResourceFromLongID(Bnd.MeshAssetID));
                //m_CurrentSims[0].SimSkeleton.ComputeBonePositions(SimSkeleton.RootBone,
                //    GameFacade.Scenes.WorldMatrix);
                //m_CurrentSims[0].HeadMesh.ProcessMesh();

                //m_CurrentSims[0].HeadTexture = Texture2D.FromFile(m_Scene.SceneMgr.Device,
                //    new MemoryStream(ContentManager.GetResourceFromLongID(Bnd.TextureAssetID)));
            }
        }
        /// <summary>
        /// LoginServer sent information about the player's characters.
        /// </summary>
        /// <param name="Packet">The packet that was received.</param>
        public static void OnCharacterInfoResponse(ProcessedPacket Packet, NetworkClient Client)
        {
            //If the decrypted length == 1, it means that there were 0
            //characters that needed to be updated, or that the user
            //hasn't created any characters on his/her account yet.
            //Since the Packet.Length property is equal to the length
            //of the encrypted data, it cannot be used to get the length
            //of the decrypted data.
            if (Packet.DecryptedLength > 1)
            {
                byte NumCharacters = (byte)Packet.ReadByte();
                List<Sim> FreshSims = new List<Sim>();

                for (int i = 0; i < NumCharacters; i++)
                {
                    int CharacterID = Packet.ReadInt32();

                    Sim FreshSim = new Sim(Packet.ReadString());
                    FreshSim.CharacterID = CharacterID;
                    FreshSim.Timestamp = Packet.ReadString();
                    FreshSim.Name = Packet.ReadString();
                    FreshSim.Sex = Packet.ReadString();
                    FreshSim.Description = Packet.ReadString();
                    FreshSim.HeadOutfitID = Packet.ReadUInt64();
                    FreshSim.BodyOutfitID = Packet.ReadUInt64();
                    FreshSim.AppearanceType = (SimsLib.ThreeD.AppearanceType)Packet.ReadByte();
                    FreshSim.ResidingCity = new CityInfo(Packet.ReadString(), "", Packet.ReadUInt64(), Packet.ReadString(),
                        Packet.ReadUInt64(), Packet.ReadString(), Packet.ReadInt32());

                    FreshSims.Add(FreshSim);
                }

                NetworkFacade.Avatars = FreshSims;
                Cache.CacheSims(FreshSims);
            }

            PacketStream CityInfoRequest = new PacketStream(0x06, 0);
            CityInfoRequest.WriteByte(0x00); //Dummy

            Client.SendEncrypted((byte)PacketType.CITY_LIST, CityInfoRequest.ToArray());
        }
        public UIGizmo()
        {
            var ui = this.RenderScript("gizmo.uis");

            BackgroundImageGizmo = ui.Create<UIImage>("BackgroundImageGizmo");
            this.AddAt(0, BackgroundImageGizmo);

            BackgroundImageGizmoPanel = ui.Create<UIImage>("BackgroundImageGizmoPanel");
            this.AddAt(0, BackgroundImageGizmoPanel);

            BackgroundImagePanel = ui.Create<UIImage>("BackgroundImagePanel");
            this.AddAt(0, BackgroundImagePanel);

            UIUtils.MakeDraggable(BackgroundImageGizmo, this);
            UIUtils.MakeDraggable(BackgroundImageGizmoPanel, this);
            UIUtils.MakeDraggable(BackgroundImagePanel, this);

            ButtonContainer = new UIContainer();
            this.Remove(ExpandButton);
            ButtonContainer.Add(ExpandButton);
            this.Remove(ContractButton);
            ButtonContainer.Add(ContractButton);
            this.Remove(FiltersButton);
            ButtonContainer.Add(FiltersButton);
            this.Remove(SearchButton);
            ButtonContainer.Add(SearchButton);
            this.Remove(Top100ListsButton);
            ButtonContainer.Add(Top100ListsButton);
            this.Add(ButtonContainer);

            FiltersProperty = new UIGizmoPropertyFilters(ui, this);
            FiltersProperty.Visible = false;
            this.Add(FiltersProperty);

            Search = new UIGizmoSearch(ui, this);
            Search.Visible = false;
            this.Add(Search);

            Top100 = new UIGizmoTop100(ui, this);
            Top100.Visible = false;
            this.Add(Top100);

            ExpandButton.OnButtonClick += new ButtonClickDelegate(ExpandButton_OnButtonClick);
            ContractButton.OnButtonClick += new ButtonClickDelegate(ContractButton_OnButtonClick);

            PeopleTabButton.OnButtonClick += new ButtonClickDelegate(PeopleTabButton_OnButtonClick);
            HousesTabButton.OnButtonClick += new ButtonClickDelegate(HousesTabButton_OnButtonClick);

            FiltersButton.OnButtonClick += new ButtonClickDelegate(FiltersButton_OnButtonClick);
            SearchButton.OnButtonClick += new ButtonClickDelegate(SearchButton_OnButtonClick);
            Top100ListsButton.OnButtonClick += new ButtonClickDelegate(Top100ListsButton_OnButtonClick);

            SimBox = new UISim();
            var sim = new Sim(Guid.NewGuid().ToString());
            var maleHeads = new Collection(ContentManager.GetResourceFromLongID((ulong)FileIDs.CollectionsFileIDs.ea_male_heads));
            SimCatalog.LoadSim3D(sim, SimCatalog.GetOutfit(4462471020557), AppearanceType.Light);

            SimBox.Sim = sim;
            SimBox.SimScale = 0.4f;
            SimBox.Position = new Microsoft.Xna.Framework.Vector2(60, 60);

            this.Add(SimBox);

            SetOpen(false);
        }
        public static void LoadSimHead(Sim sim)
        {
            var outfit = SimCatalog.GetOutfit(sim.HeadOutfitID)
                            .GetAppearanceObject(sim.AppearanceType);

            sim.HeadBindings = outfit.BindingIDs.Select(
                x => new SimModelBinding(x)
            ).ToList();
        }
        /// <summary>
        /// Display an avatar
        /// </summary>
        /// <param name="avatar"></param>
        public void DisplayAvatar(Sim avatar)
        {
            this.Avatar = avatar;
            SetSlotAvaliable(false);

            PersonNameText.Caption = avatar.Name;
            PersonDescriptionText.CurrentText = avatar.Description;
            AvatarButton.Texture = Screen.SimSelectButtonImage;

            CityNameText.Caption = avatar.ResidingCity.Name;

            String gamepath = GameFacade.GameFilePath("");
            int CityNum = GameFacade.GetCityNumber(avatar.ResidingCity.Name);
            string CityStr = gamepath + "cities\\" + ((CityNum >= 10) ? "city_00" + CityNum.ToString() : "city_000" + CityNum.ToString());

            Texture2D cityThumbTex = TextureUtils.Resize(GameFacade.GraphicsDevice, Texture2D.FromFile(
                GameFacade.Game.GraphicsDevice, CityStr + "\\Thumbnail.bmp"), 78, 58);
            TextureUtils.CopyAlpha(ref cityThumbTex, Screen.CityHouseButtonAlpha);
            CityThumb.Texture = cityThumbTex;

            SetTab(PersonSlotTab.EnterTab);
        }
        public static void LoadSim3D(Sim sim, Outfit OutfHead, AppearanceType skin)
        {
            var Apr = new Appearance(ContentManager.GetResourceFromLongID(OutfHead.GetAppearance(skin)));
            var Bnd = new Binding(ContentManager.GetResourceFromLongID(Apr.BindingIDs[0]));

            sim.HeadTexture = GetOutfitTexture(Bnd.TextureAssetID);
            sim.HeadMesh = GetOutfitMesh(sim.SimSkeleton, Bnd.MeshAssetID);
        }
        public static void LoadSimBody(Sim sim)
        {
            var appearance = SimCatalog.GetOutfit(sim.BodyOutfitID)
                            .GetAppearanceObject(sim.AppearanceType);

            sim.BodyBindings = appearance.Bindings.Select(
                x => new SimModelBinding(x.ID)
            ).ToList();
        }
        public static void LoadSimHands(Sim sim)
        {
            ulong ID = SimCatalog.GetOutfit(sim.BodyOutfitID).HandGroup;

            Hag HandGrp = new Hag(new MemoryStream(ContentManager.GetResourceFromLongID(ID)));
            Appearance Apr;

            //This is UGLY, there must be a better way of doing this. :\
            switch (sim.AppearanceType)
            {
                case AppearanceType.Light:
                    if (HandGrp.LightSkin.LeftHand.FistGesture != 0)
                    {
                        Apr = new Appearance();
                        Apr.Read(new MemoryStream(ContentManager.GetResourceFromLongID(
                            HandGrp.LightSkin.LeftHand.FistGesture)));

                        sim.LeftHandBindings.FistBindings = Apr.Bindings.Select(x => new SimModelBinding(x.ID)).ToList();

                        Apr = new Appearance();
                        Apr.Read(new MemoryStream(ContentManager.GetResourceFromLongID(
                            HandGrp.LightSkin.RightHand.FistGesture)));

                        sim.RightHandBindings.FistBindings = Apr.Bindings.Select(x => new SimModelBinding(x.ID)).ToList();
                    }

                    if (HandGrp.LightSkin.LeftHand.IdleGesture != 0)
                    {
                        Apr = new Appearance();
                        Apr.Read(new MemoryStream(ContentManager.GetResourceFromLongID(
                            HandGrp.LightSkin.LeftHand.IdleGesture)));

                        sim.LeftHandBindings.IdleBindings = Apr.Bindings.Select(x => new SimModelBinding(x.ID)).ToList();

                        Apr = new Appearance();
                        Apr.Read(new MemoryStream(ContentManager.GetResourceFromLongID(
                            HandGrp.LightSkin.RightHand.IdleGesture)));

                        sim.RightHandBindings.IdleBindings = Apr.Bindings.Select(x => new SimModelBinding(x.ID)).ToList();
                    }

                    if (HandGrp.LightSkin.LeftHand.PointingGesture != 0)
                    {
                        Apr = new Appearance();
                        Apr.Read(new MemoryStream(ContentManager.GetResourceFromLongID(
                            HandGrp.LightSkin.LeftHand.PointingGesture)));

                        sim.LeftHandBindings.PointingBindings = Apr.Bindings.Select(x => new SimModelBinding(x.ID)).ToList();

                        Apr = new Appearance();
                        Apr.Read(new MemoryStream(ContentManager.GetResourceFromLongID(
                            HandGrp.LightSkin.RightHand.PointingGesture)));

                        sim.RightHandBindings.PointingBindings = Apr.Bindings.Select(x => new SimModelBinding(x.ID)).ToList();
                    }
                    break;
                case AppearanceType.Medium:
                    if (HandGrp.MediumSkin.LeftHand.FistGesture != 0)
                    {
                        Apr = new Appearance();
                        Apr.Read(new MemoryStream(ContentManager.GetResourceFromLongID(
                            HandGrp.MediumSkin.LeftHand.FistGesture)));

                        sim.LeftHandBindings.FistBindings = Apr.Bindings.Select(x => new SimModelBinding(x.ID)).ToList();

                        Apr = new Appearance();
                        Apr.Read(new MemoryStream(ContentManager.GetResourceFromLongID(
                            HandGrp.MediumSkin.RightHand.FistGesture)));

                        sim.RightHandBindings.FistBindings = Apr.Bindings.Select(x => new SimModelBinding(x.ID)).ToList();
                    }

                    if (HandGrp.MediumSkin.LeftHand.IdleGesture != 0)
                    {
                        Apr = new Appearance();
                        Apr.Read(new MemoryStream(ContentManager.GetResourceFromLongID(
                            HandGrp.MediumSkin.LeftHand.IdleGesture)));

                        sim.LeftHandBindings.IdleBindings = Apr.Bindings.Select(x => new SimModelBinding(x.ID)).ToList();

                        Apr = new Appearance();
                        Apr.Read(new MemoryStream(ContentManager.GetResourceFromLongID(
                            HandGrp.MediumSkin.RightHand.IdleGesture)));

                        sim.RightHandBindings.IdleBindings = Apr.Bindings.Select(x => new SimModelBinding(x.ID)).ToList();
                    }

                    if (HandGrp.MediumSkin.LeftHand.PointingGesture != 0)
                    {
                        Apr = new Appearance();
                        Apr.Read(new MemoryStream(ContentManager.GetResourceFromLongID(
                            HandGrp.MediumSkin.LeftHand.PointingGesture)));

                        sim.LeftHandBindings.PointingBindings = Apr.Bindings.Select(x => new SimModelBinding(x.ID)).ToList();

                        Apr = new Appearance();
                        Apr.Read(new MemoryStream(ContentManager.GetResourceFromLongID(
                            HandGrp.MediumSkin.RightHand.PointingGesture)));

                        sim.RightHandBindings.PointingBindings = Apr.Bindings.Select(x => new SimModelBinding(x.ID)).ToList();
                    }
                    break;
                case AppearanceType.Dark:
                    if (HandGrp.DarkSkin.LeftHand.FistGesture != 0)
                    {
                        Apr = new Appearance();
                        Apr.Read(new MemoryStream(ContentManager.GetResourceFromLongID(
                            HandGrp.DarkSkin.LeftHand.FistGesture)));

                        sim.LeftHandBindings.FistBindings = Apr.Bindings.Select(x => new SimModelBinding(x.ID)).ToList();

                        Apr = new Appearance();
                        Apr.Read(new MemoryStream(ContentManager.GetResourceFromLongID(
                            HandGrp.DarkSkin.RightHand.FistGesture)));

                        sim.RightHandBindings.FistBindings = Apr.Bindings.Select(x => new SimModelBinding(x.ID)).ToList();
                    }

                    if (HandGrp.DarkSkin.LeftHand.IdleGesture != 0)
                    {
                        Apr = new Appearance();
                        Apr.Read(new MemoryStream(ContentManager.GetResourceFromLongID(
                            HandGrp.DarkSkin.LeftHand.IdleGesture)));

                        sim.LeftHandBindings.IdleBindings = Apr.Bindings.Select(x => new SimModelBinding(x.ID)).ToList();

                        Apr = new Appearance();
                        Apr.Read(new MemoryStream(ContentManager.GetResourceFromLongID(
                            HandGrp.DarkSkin.RightHand.IdleGesture)));

                        sim.RightHandBindings.IdleBindings = Apr.Bindings.Select(x => new SimModelBinding(x.ID)).ToList();
                    }

                    if (HandGrp.DarkSkin.LeftHand.PointingGesture != 0)
                    {
                        Apr = new Appearance();
                        Apr.Read(new MemoryStream(ContentManager.GetResourceFromLongID(
                            HandGrp.DarkSkin.LeftHand.PointingGesture)));

                        sim.LeftHandBindings.PointingBindings = Apr.Bindings.Select(x => new SimModelBinding(x.ID)).ToList();

                        Apr = new Appearance();
                        Apr.Read(new MemoryStream(ContentManager.GetResourceFromLongID(
                            HandGrp.DarkSkin.RightHand.PointingGesture)));

                        sim.RightHandBindings.PointingBindings = Apr.Bindings.Select(x => new SimModelBinding(x.ID)).ToList();
                    }
                    break;
            }
        }
        public PersonSelection()
        {
            UIScript ui = null;
            if (GlobalSettings.Default.ScaleUI)
            {
                ui = this.RenderScript("personselection.uis");
                this.Scale800x600 = true;
            }
            else
            {
                ui = this.RenderScript("personselection" + (ScreenWidth == 1024 ? "1024" : "") + ".uis");
            }

            var numSlots = 3;
            PersonSlots = new List<PersonSlot>();

            for (var i = 0; i < numSlots; i++)
            {
                var index = (i + 1).ToString();

                /** Tab Background **/
                var tabBackground = ui.Create<UIImage>("TabBackgroundImage" + index);
                this.Add(tabBackground);

                var enterTabImage = ui.Create<UIImage>("EnterTabImage" + index);
                this.Add(enterTabImage);

                var descTabImage = ui.Create<UIImage>("DescriptionTabImage" + index);
                this.Add(descTabImage);

                var descTabBgImage = ui.Create<UIImage>("DescriptionTabBackgroundImage" + index);
                var enterIcons = ui.Create<UIImage>("EnterTabBackgroundImage" + index);

                var personSlot = new PersonSlot(this)
                {
                    AvatarButton = (UIButton)ui["AvatarButton" + index],
                    CityButton = (UIButton)ui["CityButton" + index],
                    HouseButton = (UIButton)ui["HouseButton" + index],
                    EnterTabButton = (UIButton)ui["EnterTabButton" + index],
                    DescTabButton = (UIButton)ui["DescriptionTabButton" + index],
                    NewAvatarButton = (UIButton)ui["NewAvatarButton" + index],
                    DeleteAvatarButton = (UIButton)ui["DeleteAvatarButton" + index],
                    PersonNameText = (UILabel)ui["PersonNameText" + index],
                    PersonDescriptionScrollUpButton = (UIButton)ui["PersonDescriptionScrollUpButton" + index],
                    PersonDescriptionScrollDownButton = (UIButton)ui["PersonDescriptionScrollDownButton" + index],
                    PersonDescriptionSlider = (UISlider)ui["PersonDescriptionSlider" + index],
                    CityNameText = (UILabel)ui["CityNameText" + index],
                    HouseNameText = (UILabel)ui["HouseNameText" + index],
                    PersonDescriptionText = (UITextEdit)ui["PersonDescriptionText" + index],
                    DescriptionTabBackgroundImage = descTabBgImage,
                    EnterTabBackgroundImage = enterIcons,

                    TabBackground = tabBackground,
                    TabEnterBackground = enterTabImage,
                    TabDescBackground = descTabImage
                };

                this.AddBefore(descTabBgImage, personSlot.PersonDescriptionText);
                this.AddBefore(enterIcons, personSlot.CityButton);

                personSlot.Init();
                personSlot.SetSlotAvaliable(true);
                PersonSlots.Add(personSlot);

                if (i < NetworkFacade.Avatars.Count)
                {
                    personSlot.DisplayAvatar(NetworkFacade.Avatars[i]);
                }
            }

            this.AddAt(0, new UIImage(BackgroundImage));
            if (BackgroundImageDialog != null)
            {
                this.AddAt(1, new UIImage(BackgroundImageDialog)
                {
                    X = 112,
                    Y = 84
                });
            }

            /**
             * Button plumbing
             */
            CreditsButton.OnButtonClick += new ButtonClickDelegate(CreditsButton_OnButtonClick);

            /**
             * Music
             */
            var tracks = new string[]{
                //"music\\modes\\select\\tsosas1_v2.mp3",
                "music\\modes\\select\\tsosas2_v2.mp3",
                //"music\\modes\\select\\tsosas3.mp3",
                //"music\\modes\\select\\tsosas4.mp3",
                //"music\\modes\\select\\tsosas5.mp3"
            };
            PlayBackgroundMusic(
                GameFacade.GameFilePath(tracks.RandomItem())
            );

            var simBox = new UISim();
            var sim = new Sim(Guid.NewGuid().ToString());
            var maleHeads = new Collection(ContentManager.GetResourceFromLongID((ulong)FileIDs.CollectionsFileIDs.ea_male_heads));

            SimCatalog.LoadSim3D(sim, SimCatalog.GetOutfit(4462471020557), AppearanceType.Light);

            simBox.Sim = sim;
            simBox.Position = PersonSlots[0].AvatarButton.Position + new Vector2(70, 40);
            simBox.Size = PersonSlots[0].AvatarButton.Size;

            this.Add(simBox);
        }
        /// <summary>
        /// Display an avatar
        /// </summary>
        /// <param name="avatar"></param>
        public void DisplayAvatar(Sim avatar)
        {
            this.Avatar = avatar;
            SetSlotAvaliable(false);

            PersonNameText.Caption = avatar.Name;
            PersonDescriptionText.CurrentText = avatar.Description;
            AvatarButton.Texture = Screen.SimSelectButtonImage;

            var myCity = NetworkFacade.Cities.First(x => x.ID == avatar.CityID);
            CityNameText.Caption = myCity.Name;

            var cityThumbTex = TextureUtils.Resize(GameFacade.GraphicsDevice, Texture2D.FromFile(GameFacade.GraphicsDevice, new MemoryStream(ContentManager.GetResourceFromLongID(myCity.Thumbnail))), 78, 58);
            TextureUtils.CopyAlpha(ref cityThumbTex, Screen.CityHouseButtonAlpha);
            CityThumb.Texture = cityThumbTex;

            SetTab(PersonSlotTab.EnterTab);
        }
        private void AcceptButton_OnButtonClick(UIElement button)
        {
            var sim = new Sim(Guid.NewGuid());

            sim.Name = NameTextEdit.CurrentText;
            sim.Sex = System.Enum.GetName(typeof(Gender), Gender);
            sim.Description = DescriptionTextEdit.CurrentText;
            sim.Timestamp = DateTime.Now.ToString("yyyy.MM.dd hh:mm:ss");
            sim.ResidingCity = SelectedCity;

            var selectedHead = (CollectionItem)((UIGridViewerItem)m_HeadSkinBrowser.SelectedItem).Data;
            var selectedBody = (CollectionItem)((UIGridViewerItem)m_BodySkinBrowser.SelectedItem).Data;
            var headPurchasable = Content.Get().AvatarPurchasables.Get(selectedHead.PurchasableOutfitId);
            var bodyPurchasable = Content.Get().AvatarPurchasables.Get(selectedBody.PurchasableOutfitId);

            sim.HeadOutfitID = headPurchasable.OutfitID;
            sim.BodyOutfitID = bodyPurchasable.OutfitID;
            sim.AppearanceType = this.AppearanceType;

            //GameFacade.Controller.ShowCity();
            PlayerAccount.CurrentlyActiveSim = sim;

            if (PlayerAccount.Sims.Count == 0)
                PlayerAccount.Sims.Add(sim);
            else if (PlayerAccount.Sims.Count == 2)
                PlayerAccount.Sims[1] = sim;
            else if (PlayerAccount.Sims.Count == 3)
                PlayerAccount.Sims[2] = sim;

            UIPacketSenders.SendCharacterCreate(sim, DateTime.Now.ToString());
        }
        public PersonSelectionEdit()
        {
            /**
             * Data
             */
            MaleHeads = new Collection(ContentManager.GetResourceFromLongID((ulong)FileIDs.CollectionsFileIDs.ea_male_heads));
            MaleOutfits = new Collection(ContentManager.GetResourceFromLongID((ulong)FileIDs.CollectionsFileIDs.ea_male));

            FemaleHeads = new Collection(ContentManager.GetResourceFromLongID((ulong)FileIDs.CollectionsFileIDs.ea_female_heads));
            FemaleOutfits = new Collection(ContentManager.GetResourceFromLongID((ulong)FileIDs.CollectionsFileIDs.ea_female));

            /**
             * UI
             */

            UIScript ui = null;
            if (GlobalSettings.Default.ScaleUI)
            {
                ui = this.RenderScript("personselectionedit.uis");
                this.Scale800x600 = true;
            }
            else
            {
                ui = this.RenderScript("personselectionedit" + (ScreenWidth == 1024 ? "1024" : "") + ".uis");
            }

            DescriptionTextEdit.CurrentText = ui.GetString("DefaultAvatarDescription");
            DescriptionSlider.AttachButtons(DescriptionScrollUpButton, DescriptionScrollDownButton, 1);
            DescriptionTextEdit.AttachSlider(DescriptionSlider);
            NameTextEdit.OnChange += new ChangeDelegate(NameTextEdit_OnChange);

            AcceptButton.Disabled = true;
            AcceptButton.OnButtonClick += new ButtonClickDelegate(AcceptButton_OnButtonClick);

            /** Appearance **/
            SkinLightButton.OnButtonClick += new ButtonClickDelegate(SkinButton_OnButtonClick);
            SkinMediumButton.OnButtonClick += new ButtonClickDelegate(SkinButton_OnButtonClick);
            SkinDarkButton.OnButtonClick += new ButtonClickDelegate(SkinButton_OnButtonClick);
            SelectedAppearanceButton = SkinLightButton;
            SkinLightButton.Selected = true;

            HeadSkinBrowser = ui.Create<UICollectionViewer>("HeadSkinBrowser");
            HeadSkinBrowser.OnChange += new ChangeDelegate(HeadSkinBrowser_OnChange);
            HeadSkinBrowser.Init();
            this.Add(HeadSkinBrowser);

            BodySkinBrowser = ui.Create<UICollectionViewer>("BodySkinBrowser");
            BodySkinBrowser.Init();
            this.Add(BodySkinBrowser);

            FemaleButton.OnButtonClick += new ButtonClickDelegate(GenderButton_OnButtonClick);
            MaleButton.OnButtonClick += new ButtonClickDelegate(GenderButton_OnButtonClick);

            /** Backgrounds **/
            var bg = new UIImage(BackgroundImage);
            this.AddAt(0, bg);

            var offset = new Vector2(0, 0);
            if (BackgroundImageDialog != null)
            {
                offset = new Vector2(112, 84);

                this.AddAt(1, new UIImage(BackgroundImageDialog) {
                    X = 112,
                    Y = 84
                });
            }

            /**
             * Music
             */
            PlayBackgroundMusic(
                GameFacade.GameFilePath("music\\modes\\create\\tsocas1_v2.mp3")
            );

            SimBox = new UISim();
            SimBox.SimScale = 0.8f;
            SimBox.Position = new Microsoft.Xna.Framework.Vector2(offset.X + 140, offset.Y + 130);

            this.Add(SimBox);

            Sim = new Sim(new Guid().ToString());

            /**
             * Init state
             */
            RefreshCollections();

            HeadSkinBrowser.SelectedIndex = 0;
            BodySkinBrowser.SelectedIndex = 0;
            FemaleButton.Selected = true;
        }
        public static void LoadSim3D(Sim sim)
        {
            LoadSimHead(sim);
            LoadSimBody(sim);

            sim.Reposition();
        }
Beispiel #20
0
        /// <summary>
        /// Caches sims received from the LoginServer to the disk.
        /// </summary>
        /// <param name="FreshSims">A list of the sims received by the LoginServer.</param>
        public static void CacheSims(List<Sim> FreshSims)
        {
            if (!Directory.Exists(CacheDir))
                Directory.CreateDirectory(CacheDir);

            using(BinaryWriter Writer = new BinaryWriter(File.Create(CacheDir + "\\Sims.tempcache")))
            {
                //Last time these sims were cached.
                Writer.Write(DateTime.Now.ToString("yyyy.MM.dd hh:mm:ss"));

                Writer.Write(FreshSims.Count);

                foreach (Sim S in FreshSims)
                {
                    //Length of the current entry, so its skippable...
                    Writer.Write((int)4 + S.GUID.ToString().Length + 4 + S.Timestamp.Length + S.Name.Length + S.Sex.Length +
                        S.Description.Length + 17 + S.ResidingCity.Name.Length + 8 + S.ResidingCity.UUID.ToString().Length + 8 +
                        S.ResidingCity.IP.Length + 4);
                    Writer.Write(S.GUID.ToString());
                    Writer.Write(S.CharacterID);
                    Writer.Write(S.Timestamp);
                    Writer.Write(S.Name);
                    Writer.Write(S.Sex);
                    Writer.Write(S.Description);
                    Writer.Write(S.HeadOutfitID);
                    Writer.Write(S.BodyOutfitID);
                    Writer.Write((byte)S.AppearanceType);
                    Writer.Write(S.ResidingCity.Name);
                    Writer.Write(S.ResidingCity.Thumbnail);
                    Writer.Write(S.ResidingCity.UUID);
                    Writer.Write(S.ResidingCity.Map);
                    Writer.Write(S.ResidingCity.IP);
                    Writer.Write(S.ResidingCity.Port);
                }

                if (File.Exists(CacheDir + "\\Sims.cache"))
                {
                    using (BinaryReader Reader = new BinaryReader(File.Open(CacheDir + "\\Sims.cache", FileMode.Open)))
                    {
                        //Last time these sims were cached.
                        Reader.ReadString();
                        int NumSims = Reader.ReadInt32();

                        List<Sim> UnchangedSims = new List<Sim>();

                        if (NumSims > FreshSims.Count)
                        {
                            if (NumSims == 2)
                            {
                                //Skips the first entry.
                                Reader.BaseStream.Position = Reader.ReadInt32();

                                Reader.ReadInt32(); //Length of second entry.
                                string GUID = Reader.ReadString();

                                Sim S = new Sim(GUID);

                                S.CharacterID = Reader.ReadInt32();
                                S.Timestamp = Reader.ReadString();
                                S.Name = Reader.ReadString();
                                S.Sex = Reader.ReadString();
                                S.Description = Reader.ReadString();
                                S.HeadOutfitID = Reader.ReadUInt64();
                                S.BodyOutfitID = Reader.ReadUInt64();
                                S.AppearanceType = (AppearanceType)Reader.ReadByte();
                                S.ResidingCity = new ProtocolAbstractionLibraryD.CityInfo(Reader.ReadString(), "",
                                    Reader.ReadUInt64(), Reader.ReadString(), Reader.ReadUInt64(), Reader.ReadString(),
                                    Reader.ReadInt32());
                                UnchangedSims.Add(S);
                            }
                            else if (NumSims == 3)
                            {
                                //Skips the first entry.
                                Reader.BaseStream.Position = Reader.ReadInt32();

                                Reader.ReadInt32(); //Length of second entry.
                                string GUID = Reader.ReadString();

                                Sim S = new Sim(GUID);

                                S.CharacterID = Reader.ReadInt32();
                                S.Timestamp = Reader.ReadString();
                                S.Name = Reader.ReadString();
                                S.Sex = Reader.ReadString();
                                S.Description = Reader.ReadString();
                                S.HeadOutfitID = Reader.ReadUInt64();
                                S.BodyOutfitID = Reader.ReadUInt64();
                                S.AppearanceType = (AppearanceType)Reader.ReadByte();
                                S.ResidingCity = new ProtocolAbstractionLibraryD.CityInfo(Reader.ReadString(), "",
                                    Reader.ReadUInt64(), Reader.ReadString(), Reader.ReadUInt64(), Reader.ReadString(),
                                    Reader.ReadInt32());
                                UnchangedSims.Add(S);

                                Reader.ReadInt32(); //Length of third entry.
                                S.CharacterID = Reader.ReadInt32();
                                S.Timestamp = Reader.ReadString();
                                S.Name = Reader.ReadString();
                                S.Sex = Reader.ReadString();
                                S.Description = Reader.ReadString();
                                S.HeadOutfitID = Reader.ReadUInt64();
                                S.BodyOutfitID = Reader.ReadUInt64();
                                S.AppearanceType = (AppearanceType)Reader.ReadByte();
                                S.ResidingCity = new ProtocolAbstractionLibraryD.CityInfo(Reader.ReadString(), "",
                                    Reader.ReadUInt64(), Reader.ReadString(), Reader.ReadUInt64(), Reader.ReadString(),
                                    Reader.ReadInt32());
                                UnchangedSims.Add(S);
                            }

                            Reader.Close();

                            foreach (Sim S in UnchangedSims)
                            {
                                //Length of the current entry, so its skippable...
                                Writer.Write((int)4 + S.GUID.ToString().Length + 4 + S.Timestamp.Length + S.Name.Length + S.Sex.Length +
                                    S.Description.Length + 17 + S.ResidingCity.Name.Length + 8 + S.ResidingCity.UUID.ToString().Length + 8 +
                                    S.ResidingCity.IP.Length + 4);
                                Writer.Write(S.GUID.ToString());
                                Writer.Write(S.CharacterID);
                                Writer.Write(S.Timestamp);
                                Writer.Write(S.Name);
                                Writer.Write(S.Sex);
                                Writer.Write(S.Description);
                                Writer.Write(S.HeadOutfitID);
                                Writer.Write(S.BodyOutfitID);
                                Writer.Write((byte)S.AppearanceType);
                                Writer.Write(S.ResidingCity.Name);
                                Writer.Write(S.ResidingCity.Thumbnail);
                                Writer.Write(S.ResidingCity.UUID);
                                Writer.Write(S.ResidingCity.Map);
                                Writer.Write(S.ResidingCity.IP);
                                Writer.Write(S.ResidingCity.Port);
                            }
                        }
                    }
                }

                Writer.Flush();
                Writer.Close();
            }

            if (File.Exists(CacheDir + "\\Sims.cache"))
            {
                File.Delete(CacheDir + "\\Sims.cache");
                File.Move(CacheDir + "\\Sims.tempcache", CacheDir + "\\Sims.cache");
            }
            else
                File.Move(CacheDir + "\\Sims.tempcache", CacheDir + "\\Sims.cache");
        }
Beispiel #21
0
        /// <summary>
        /// Caches sims received from the LoginServer to the disk.
        /// </summary>
        /// <param name="FreshSims">A list of the sims received by the LoginServer.</param>
        public static void CacheSims(List<Sim> FreshSims)
        {
            if (!Directory.Exists("CharacterCache"))
                Directory.CreateDirectory("CharacterCache");

            using(BinaryWriter Writer = new BinaryWriter(File.Create("CharacterCache\\Sims.tempcache")))
            {
                //Last time these sims were cached.
                Writer.Write(DateTime.Now.ToString("yyyy.MM.dd hh:mm:ss"));

                Writer.Write(FreshSims.Count);

                foreach (Sim S in FreshSims)
                {
                    //Length of the current entry, so its skippable...
                    Writer.Write((int)4 + S.GUID.ToString().Length + S.Timestamp.Length + S.Name.Length + S.Sex.Length +
                        S.Description.Length + 16 + S.CityID.ToString().Length);
                    Writer.Write(S.CharacterID);
                    Writer.Write(S.GUID.ToString());
                    Writer.Write(S.Timestamp);
                    Writer.Write(S.Name);
                    Writer.Write(S.Sex);
                    Writer.Write(S.Description);
                    Writer.Write(S.HeadOutfitID);
                    Writer.Write(S.BodyOutfitID);
                    Writer.Write(S.CityID.ToString());
                }

                if (File.Exists("CharacterCache\\Sims.cache"))
                {
                    using (BinaryReader Reader = new BinaryReader(File.Open("CharacterCache\\Sims.cache", FileMode.Open)))
                    {
                        //Last time these sims were cached.
                        Reader.ReadString();
                        int NumSims = Reader.ReadInt32();

                        List<Sim> UnchangedSims = new List<Sim>();

                        if (NumSims > FreshSims.Count)
                        {
                            if (NumSims == 2)
                            {
                                //Skips the first entry.
                                Reader.BaseStream.Position = Reader.ReadInt32();

                                Reader.ReadInt32(); //Length of second entry.
                                string GUID = Reader.ReadString();

                                Sim S = new Sim(GUID);

                                S.CharacterID = Reader.ReadInt32();
                                S.Timestamp = Reader.ReadString();
                                S.Name = Reader.ReadString();
                                S.Sex = Reader.ReadString();
                                S.Description = Reader.ReadString();
                                S.HeadOutfitID = Reader.ReadUInt64();
                                S.BodyOutfitID = Reader.ReadUInt64();
                                S.CityID = new Guid(Reader.ReadString());
                                UnchangedSims.Add(S);
                            }
                            else if (NumSims == 3)
                            {
                                //Skips the first entry.
                                Reader.BaseStream.Position = Reader.ReadInt32();

                                Reader.ReadInt32(); //Length of second entry.
                                string GUID = Reader.ReadString();

                                Sim S = new Sim(GUID);

                                S.CharacterID = Reader.ReadInt32();
                                S.Timestamp = Reader.ReadString();
                                S.Name = Reader.ReadString();
                                S.Sex = Reader.ReadString();
                                S.Description = Reader.ReadString();
                                S.HeadOutfitID = Reader.ReadUInt64();
                                S.BodyOutfitID = Reader.ReadUInt64();
                                S.CityID = new Guid(Reader.ReadString());
                                UnchangedSims.Add(S);

                                Reader.ReadInt32(); //Length of third entry.
                                S.CharacterID = Reader.ReadInt32();
                                S.Timestamp = Reader.ReadString();
                                S.Name = Reader.ReadString();
                                S.Sex = Reader.ReadString();
                                S.Description = Reader.ReadString();
                                S.HeadOutfitID = Reader.ReadUInt64();
                                S.BodyOutfitID = Reader.ReadUInt64();
                                S.CityID = new Guid(Reader.ReadString());
                                UnchangedSims.Add(S);
                            }

                            Reader.Close();

                            foreach (Sim S in UnchangedSims)
                            {
                                //Length of the current entry, so its skippable...
                                Writer.Write((int)4 + S.GUID.ToString().Length + S.Timestamp.Length + S.Name.Length + S.Sex.Length +
                                    S.Description.Length + 16 + S.CityID.ToString().Length);
                                Writer.Write(S.CharacterID);
                                Writer.Write(S.Timestamp);
                                Writer.Write(S.Name);
                                Writer.Write(S.Sex);
                                Writer.Write(S.Description);
                                Writer.Write(S.HeadOutfitID);
                                Writer.Write(S.BodyOutfitID);
                                Writer.Write(S.CityID.ToString());
                            }
                        }
                    }
                }

                Writer.Flush();
                Writer.Close();
            }

            if (File.Exists("CharacterCache\\Sims.cache"))
            {
                //File.Replace("CharacterCache\\Sims.tempcache", "CharacterCache\\Sims.cache", "CaracterCache\\cache.backup");
                File.Delete("CharacterCache\\Sims.cache");
                File.Move("CharacterCache\\Sims.tempcache", "CharacterCache\\Sims.cache");
            }
            else
                File.Move("CharacterCache\\Sims.tempcache", "CharacterCache\\Sims.cache");
        }