From 19339c6888822e383b045b747efde015bfb4ad5a Mon Sep 17 00:00:00 2001 From: Alexander Eckart Date: Tue, 7 May 2013 16:56:23 +0200 Subject: [PATCH] reconstructed CProfiles (preparing for async profile adding) --- Vocaluxe/Base/CConfig.cs | 8 +- Vocaluxe/Base/CMain.cs | 7 +- Vocaluxe/Base/CProfiles.cs | 349 +++++++++++++++++----------- Vocaluxe/Screens/CScreenNames.cs | 56 +++-- Vocaluxe/Screens/CScreenProfiles.cs | 85 ++++--- Vocaluxe/Screens/CScreenScore.cs | 2 +- Vocaluxe/Screens/CScreenSing.cs | 4 +- VocaluxeLib/IBasic.cs | 2 + VocaluxeLib/Menu/CNameSelection.cs | 10 +- VocaluxeLib/Profile/CAvatar.cs | 18 +- VocaluxeLib/Profile/CProfile.cs | 37 ++- 11 files changed, 353 insertions(+), 225 deletions(-) diff --git a/Vocaluxe/Base/CConfig.cs b/Vocaluxe/Base/CConfig.cs index 204c8f73..9bc011eb 100644 --- a/Vocaluxe/Base/CConfig.cs +++ b/Vocaluxe/Base/CConfig.cs @@ -30,6 +30,7 @@ using Vocaluxe.Lib.Sound; using Vocaluxe.Lib.Webcam; using VocaluxeLib; using VocaluxeLib.Menu; +using VocaluxeLib.Profile; namespace Vocaluxe.Base { @@ -810,12 +811,13 @@ namespace Vocaluxe.Base CGame.Players[j].ProfileID = -1; if (Players[j] == "") continue; - for (int i = 0; i < CProfiles.Profiles.Length; i++) + CProfile[] profiles = CProfiles.GetProfiles(); + for (int i = 0; i < profiles.Length; i++) { - if (Path.GetFileName(CProfiles.Profiles[i].FileName) == Players[j] && CProfiles.Profiles[i].Active == EOffOn.TR_CONFIG_ON) + if (Path.GetFileName(profiles[i].FileName) == Players[j] && profiles[i].Active == EOffOn.TR_CONFIG_ON) { //Update Game-infos with player - CGame.Players[j].ProfileID = i; + CGame.Players[j].ProfileID = profiles[i].ID; } } } diff --git a/Vocaluxe/Base/CMain.cs b/Vocaluxe/Base/CMain.cs index 02bc2092..5a55a92e 100644 --- a/Vocaluxe/Base/CMain.cs +++ b/Vocaluxe/Base/CMain.cs @@ -214,6 +214,11 @@ namespace Vocaluxe.Base { return CSettings.MedleyMinDuration; } + + public string GetFolderProfiles() + { + return CSettings.FolderProfiles; + } } class CBtheme : ITheme @@ -549,7 +554,7 @@ namespace Vocaluxe.Base { public CProfile[] GetProfiles() { - return CProfiles.Profiles; + return CProfiles.GetProfiles(); } public EGameDifficulty GetDifficulty(int profileID) diff --git a/Vocaluxe/Base/CProfiles.cs b/Vocaluxe/Base/CProfiles.cs index 51607c5f..2368532b 100644 --- a/Vocaluxe/Base/CProfiles.cs +++ b/Vocaluxe/Base/CProfiles.cs @@ -30,24 +30,21 @@ namespace Vocaluxe.Base { static class CProfiles { - private static List _Profiles; - private static readonly List _Avatars = new List(); + private static Dictionary _Profiles; + private static Queue _ProfileIDs; + private static Object _ProfileMutex = new Object(); - public static CProfile[] Profiles - { - get { return _Profiles.ToArray(); } - } + private static Dictionary _Avatars; + private static Queue _AvatarIDs; + private static Object _AvatarMutex = new Object(); + + private static List _Callbacks; public static int NumProfiles { get { return _Profiles.Count; } } - public static CAvatar[] Avatars - { - get { return _Avatars.ToArray(); } - } - public static int NumAvatars { get { return _Avatars.Count; } @@ -55,80 +52,175 @@ namespace Vocaluxe.Base public static void Init() { + _Avatars = new Dictionary(); + _AvatarIDs = new Queue(1000); + for (int i = 0; i < 1000; i++) + { + _AvatarIDs.Enqueue(i); + } + + _Profiles = new Dictionary(); + _ProfileIDs = new Queue(100); + for (int i = 0; i < 100; i++) + { + _ProfileIDs.Enqueue(i); + } + + _Callbacks = new List(); + LoadProfiles(); } + public static CProfile[] GetProfiles() + { + List list = new List(); + + lock (_ProfileMutex) + { + if (_Profiles.Count == 0) + return null; + + CProfile[] result = new CProfile[_Profiles.Count]; + _Profiles.Values.CopyTo(result, 0); + for (int i = 0; i < result.Length; i++) + { + list.Add(result[i]); + } + list.Sort(_CompareByPlayerName); + } + return list.ToArray(); + } + + public static CAvatar[] GetAvatars() + { + CAvatar[] result = null; + lock (_AvatarMutex) + { + if (_Avatars.Count == 0) + return null; + + result = new CAvatar[_Avatars.Count]; + _Avatars.Values.CopyTo(result, 0); + } + return result; + } + public static string GetPlayerName(int profileID, int playerNum = 0) { if (ICProfileIDValid(profileID)) return _Profiles[profileID].PlayerName; + string playerName = CLanguage.Translate("TR_SCREENNAMES_PLAYER"); if (playerNum > 0) playerName += " " + playerNum; return playerName; } + public static string GetProfileFileName(int profileID) + { + if (!ICProfileIDValid(profileID)) + return String.Empty; + + return Path.GetFileName(_Profiles[profileID].FileName); + } + public static int NewProfile(string fileName = "") { CProfile profile = new CProfile { - PlayerName = String.Empty, - Difficulty = EGameDifficulty.TR_CONFIG_EASY, - Avatar = new CAvatar(-1), - GuestProfile = EOffOn.TR_CONFIG_OFF, - Active = EOffOn.TR_CONFIG_ON, FileName = fileName != "" ? Path.Combine(CSettings.FolderProfiles, fileName) : String.Empty }; if (File.Exists(profile.FileName)) return -1; - _Profiles.Add(profile); - return _Profiles.Count - 1; + lock (_ProfileMutex) + { + profile.ID = _ProfileIDs.Dequeue(); + _Profiles.Add(profile.ID, profile); + } + + return profile.ID; } public static void SaveProfiles() { - for (int i = 0; i < _Profiles.Count; i++) - _SaveProfile(i); + foreach (int id in _Profiles.Keys) + { + _Profiles[id].SaveProfile(); + } + LoadProfiles(); } public static void LoadProfiles() { LoadAvatars(); - _Profiles = new List(); - List files = new List(); - files.AddRange(CHelper.ListFiles(CSettings.FolderProfiles, "*.xml", true, true)); - foreach (string file in files) - _LoadProfile(file); + lock (_ProfileMutex) + { + List knownFiles = new List(); + foreach (int id in _Profiles.Keys) + { + _Profiles[id].LoadProfile(); + knownFiles.Add(_Profiles[id].FileName); + } - _SortProfilesByName(); + List files = new List(); + files.AddRange(CHelper.ListFiles(CSettings.FolderProfiles, "*.xml", true, true)); + + foreach (string file in knownFiles) + { + files.Remove(file); + } + + foreach (string file in files) + { + CProfile profile = new CProfile + { + FileName = Path.Combine(CSettings.FolderProfiles, file) + }; + + if (profile.LoadProfile()) + { + profile.Avatar = _GetAvatar(profile.AvatarFileName); + profile.ID = _ProfileIDs.Dequeue(); + _Profiles.Add(profile.ID, profile); + } + } + } } public static void LoadAvatars() { - for (int i = 0; i < _Avatars.Count; i++) + lock (_AvatarMutex) { - STexture texture = _Avatars[i].Texture; - CDraw.RemoveTexture(ref texture); - } - _Avatars.Clear(); - - List files = new List(); - files.AddRange(CHelper.ListFiles(CSettings.FolderProfiles, "*.png", true, true)); - files.AddRange(CHelper.ListFiles(CSettings.FolderProfiles, "*.jpg", true, true)); - files.AddRange(CHelper.ListFiles(CSettings.FolderProfiles, "*.jpeg", true, true)); - files.AddRange(CHelper.ListFiles(CSettings.FolderProfiles, "*.bmp", true, true)); - - foreach (string file in files) - { - STexture tex = CDraw.AddTexture(file); - if (tex.Index != -1) + List knownFiles = new List(); + foreach (int id in _Avatars.Keys) { - CAvatar avatar = new CAvatar(-1) {Texture = tex, FileName = Path.GetFileName(file)}; - _Avatars.Add(avatar); + _Avatars[id].Reload(); + knownFiles.Add(_Avatars[id].FileName); + } + + List files = new List(); + files.AddRange(CHelper.ListFiles(CSettings.FolderProfiles, "*.png", true, true)); + files.AddRange(CHelper.ListFiles(CSettings.FolderProfiles, "*.jpg", true, true)); + files.AddRange(CHelper.ListFiles(CSettings.FolderProfiles, "*.jpeg", true, true)); + files.AddRange(CHelper.ListFiles(CSettings.FolderProfiles, "*.bmp", true, true)); + + foreach (string file in knownFiles) + { + files.Remove(file); + } + + foreach (string file in files) + { + CAvatar avatar = new CAvatar(-1); + if (avatar.LoadFromFile(file)) + { + avatar.ID = _AvatarIDs.Dequeue(); + _Avatars.Add(avatar.ID, avatar); + } } } } @@ -138,11 +230,8 @@ namespace Vocaluxe.Base if (!ICProfileIDValid(profileID)) return String.Empty; - CProfile profile = _Profiles[profileID]; - profile.PlayerName += chr; - _Profiles[profileID] = profile; - - return profile.PlayerName; + _Profiles[profileID].PlayerName += chr; + return _Profiles[profileID].PlayerName; } public static string GetDeleteCharInPlayerName(int profileID) @@ -153,14 +242,18 @@ namespace Vocaluxe.Base CProfile profile = _Profiles[profileID]; if (profile.PlayerName != "") profile.PlayerName = profile.PlayerName.Remove(profile.PlayerName.Length - 1); - _Profiles[profileID] = profile; return profile.PlayerName; } public static bool ICProfileIDValid(int profileID) { - return profileID >= 0 && profileID < _Profiles.Count; + return _Profiles.ContainsKey(profileID); + } + + public static bool ICAvatarIDValid(int avatarID) + { + return _Avatars.ContainsKey(avatarID); } public static EGameDifficulty GetDifficulty(int profileID) @@ -173,9 +266,7 @@ namespace Vocaluxe.Base if (!ICProfileIDValid(profileID)) return; - CProfile profile = _Profiles[profileID]; - profile.Difficulty = difficulty; - _Profiles[profileID] = profile; + _Profiles[profileID].Difficulty = difficulty; } public static EOffOn GetGuestProfile(int profileID) @@ -193,18 +284,14 @@ namespace Vocaluxe.Base if (!ICProfileIDValid(profileID)) return; - CProfile profile = _Profiles[profileID]; - profile.GuestProfile = option; - _Profiles[profileID] = profile; + _Profiles[profileID].GuestProfile = option; } public static void SetActive(int profileID, EOffOn option) { if (!ICProfileIDValid(profileID)) return; - CProfile profile = _Profiles[profileID]; - profile.Active = option; - _Profiles[profileID] = profile; + _Profiles[profileID].Active = option; } public static bool IsGuestProfile(int profileID) @@ -222,126 +309,106 @@ namespace Vocaluxe.Base return _Profiles[profileID].Active == EOffOn.TR_CONFIG_ON; } - public static void SetAvatar(int profileID, int avatarNr) + public static void SetAvatar(int profileID, int avatarID) { - if (!ICProfileIDValid(profileID) || avatarNr < 0 || avatarNr >= _Avatars.Count) + if (!ICProfileIDValid(profileID) || !ICAvatarIDValid(avatarID)) return; - CProfile profile = _Profiles[profileID]; - profile.Avatar = _Avatars[avatarNr]; - _Profiles[profileID] = profile; + _Profiles[profileID].Avatar = _Avatars[avatarID]; } - public static int GetAvatarNr(int profileID) + public static int GetAvatarID(int profileID) { if (!ICProfileIDValid(profileID)) - return 0; + return -1; - for (int i = 0; i < _Avatars.Count; i++) - { - if (_Profiles[profileID].Avatar.FileName == _Avatars[i].FileName) - return i; - } + return _Profiles[profileID].Avatar.ID; + } - return 0; + public static STexture GetAvatarTexture(int avatarID) + { + if (!ICAvatarIDValid(avatarID)) + return new STexture(-1); + + return _Avatars[avatarID].Texture; + } + + public static STexture GetAvatarTextureFromProfile(int profileID) + { + if (!ICProfileIDValid(profileID)) + return new STexture(-1); + + return _Profiles[profileID].Avatar.Texture; } public static void DeleteProfile(int profileID) { - if (!ICProfileIDValid(profileID)) - return; - if (_Profiles[profileID].FileName == "") - return; - try + lock (_ProfileMutex) { - //Check if profile saved in config - for (int i = 0; i < CConfig.Players.Length; i++) + if (!ICProfileIDValid(profileID)) + return; + + if (_Profiles[profileID].FileName == "") { - if (CConfig.Players[i] == _Profiles[profileID].FileName) + _RemoveProfile(profileID); + return; + } + + try + { + //Check if profile saved in config + for (int i = 0; i < CConfig.Players.Length; i++) { - CConfig.Players[i] = string.Empty; - CConfig.SaveConfig(); + if (CConfig.Players[i] == _Profiles[profileID].FileName) + { + CConfig.Players[i] = string.Empty; + CConfig.SaveConfig(); + } + } + File.Delete(_Profiles[profileID].FileName); + _RemoveProfile(profileID); + + //Check if profile is selected in game + for (int i = 0; i < CGame.Players.Length; i++) + { + if (CGame.Players[i].ProfileID > profileID) + CGame.Players[i].ProfileID--; + else if (CGame.Players[i].ProfileID == profileID) + CGame.Players[i].ProfileID = -1; } } - File.Delete(_Profiles[profileID].FileName); - _Profiles.RemoveAt(profileID); - //Check if profile is selected in game - for (int i = 0; i < CGame.Players.Length; i++) + catch (Exception) { - if (CGame.Players[i].ProfileID > profileID) - CGame.Players[i].ProfileID--; - else if (CGame.Players[i].ProfileID == profileID) - CGame.Players[i].ProfileID = -1; + CLog.LogError("Can't delete Profile File " + _Profiles[profileID].FileName + ".xml"); } } - catch (Exception) - { - CLog.LogError("Can't delete Profile File " + _Profiles[profileID].FileName + ".xml"); - } - } - - private static void _SortProfilesByName() - { - _Profiles.Sort(_CompareByPlayerName); } + #region private methods private static int _CompareByPlayerName(CProfile a, CProfile b) { return String.CompareOrdinal(a.PlayerName, b.PlayerName); } - #region private methods private static CAvatar _GetAvatar(string fileName) { - foreach (CAvatar avatar in _Avatars) + lock (_AvatarMutex) { - if (fileName == avatar.FileName) - return avatar; + foreach (int id in _Avatars.Keys) + { + if (Path.GetFileName(_Avatars[id].FileName) == fileName) + return _Avatars[id]; + } } return new CAvatar(-1); } - private static void _SaveProfile(int profileID) + private static void _RemoveProfile(int profileID) { if (!ICProfileIDValid(profileID)) return; - if (_Profiles[profileID].FileName == "") - { - string filename = string.Empty; - foreach (char chr in _Profiles[profileID].PlayerName) - { - if (char.IsLetter(chr)) - filename += chr.ToString(); - } - - if (filename == "") - filename = "1"; - - int i = 0; - while (File.Exists(Path.Combine(CSettings.FolderProfiles, filename + ".xml"))) - { - i++; - if (!File.Exists(Path.Combine(CSettings.FolderProfiles, filename + i + ".xml"))) - filename += i; - } - - CProfile profile = _Profiles[profileID]; - profile.FileName = Path.Combine(CSettings.FolderProfiles, filename + ".xml"); - _Profiles[profileID] = profile; - } - - _Profiles[profileID].SaveProfile(); - } - - private static void _LoadProfile(string fileName) - { - CProfile profile = new CProfile {FileName = Path.Combine(CSettings.FolderProfiles, fileName)}; - if (profile.LoadProfile()) - { - profile.Avatar = _GetAvatar(profile.AvatarFileName); - _Profiles.Add(profile); - } + _Profiles.Remove(profileID); } #endregion private methods } diff --git a/Vocaluxe/Screens/CScreenNames.cs b/Vocaluxe/Screens/CScreenNames.cs index 28efb832..8db82dcf 100644 --- a/Vocaluxe/Screens/CScreenNames.cs +++ b/Vocaluxe/Screens/CScreenNames.cs @@ -23,6 +23,7 @@ using System.Windows.Forms; using Vocaluxe.Base; using VocaluxeLib; using VocaluxeLib.Menu; +using VocaluxeLib.Profile; namespace Vocaluxe.Screens { @@ -166,15 +167,19 @@ namespace Vocaluxe.Screens //Check, if a player is selected if (_NameSelections[_NameSelection].Selection > -1) { + CProfile[] profiles = CProfiles.GetProfiles(); _SelectedPlayerNr = _NameSelections[_NameSelection].Selection; + if (profiles.Length <= _SelectedPlayerNr) + return true; + //Update Game-infos with new player - CGame.Players[_SelectingFastPlayerNr - 1].ProfileID = _SelectedPlayerNr; + CGame.Players[_SelectingFastPlayerNr - 1].ProfileID = profiles[_SelectedPlayerNr].ID; //Update config for default players. - CConfig.Players[_SelectingFastPlayerNr - 1] = CProfiles.Profiles[_SelectedPlayerNr].FileName; + CConfig.Players[_SelectingFastPlayerNr - 1] = profiles[_SelectedPlayerNr].FileName; CConfig.SaveConfig(); //Update texture and name - _Statics[_StaticPlayerAvatar[_SelectingFastPlayerNr - 1]].Texture = CProfiles.Profiles[_SelectedPlayerNr].Avatar.Texture; - _Texts[_TextPlayer[_SelectingFastPlayerNr - 1]].Text = CProfiles.Profiles[_SelectedPlayerNr].PlayerName; + _Statics[_StaticPlayerAvatar[_SelectingFastPlayerNr - 1]].Texture = profiles[_SelectedPlayerNr].Avatar.Texture; + _Texts[_TextPlayer[_SelectingFastPlayerNr - 1]].Text = profiles[_SelectedPlayerNr].PlayerName; //Update profile-warning _CheckPlayers(); //Update Tiles-List @@ -418,10 +423,10 @@ namespace Vocaluxe.Screens //Update Game-infos with new player CGame.Players[_SelectingSwitchNr].ProfileID = CGame.Players[i].ProfileID; //Update config for default players. - CConfig.Players[_SelectingSwitchNr] = CProfiles.Profiles[i].FileName; + CConfig.Players[_SelectingSwitchNr] = CProfiles.GetProfileFileName(CGame.Players[i].ProfileID); //Update texture and name - _Statics[_StaticPlayerAvatar[_SelectingSwitchNr]].Texture = CProfiles.Profiles[CGame.Players[i].ProfileID].Avatar.Texture; - _Texts[_TextPlayer[_SelectingSwitchNr]].Text = CProfiles.Profiles[CGame.Players[i].ProfileID].PlayerName; + _Statics[_StaticPlayerAvatar[_SelectingSwitchNr]].Texture = CProfiles.GetAvatarTextureFromProfile(CGame.Players[i].ProfileID); + _Texts[_TextPlayer[_SelectingSwitchNr]].Text = CProfiles.GetPlayerName(CGame.Players[i].ProfileID); } else if (_SelectingSwitchNr > -1) { @@ -433,14 +438,19 @@ namespace Vocaluxe.Screens _Statics[_StaticPlayerAvatar[_SelectingSwitchNr]].Texture = _OriginalPlayerAvatarTextures[_SelectingSwitchNr]; _Texts[_TextPlayer[_SelectingSwitchNr]].Text = CProfiles.GetPlayerName(-1, (_SelectingSwitchNr+1)); } + + CProfile[] profiles = CProfiles.GetProfiles(); + if (profiles.Length <= _SelectedPlayerNr) + return true; + //Update Game-infos with new player - CGame.Players[i].ProfileID = _SelectedPlayerNr; + CGame.Players[i].ProfileID = profiles[_SelectedPlayerNr].ID; //Update config for default players. - CConfig.Players[i] = CProfiles.Profiles[_SelectedPlayerNr].FileName; + CConfig.Players[i] = profiles[_SelectedPlayerNr].FileName; CConfig.SaveConfig(); //Update texture and name _Statics[_StaticPlayerAvatar[i]].Texture = _ChooseAvatarStatic.Texture; - _Texts[_TextPlayer[i]].Text = CProfiles.Profiles[_SelectedPlayerNr].PlayerName; + _Texts[_TextPlayer[i]].Text = profiles[_SelectedPlayerNr].PlayerName; //Update profile-warning _CheckPlayers(); //Update Tiles-List @@ -473,14 +483,18 @@ namespace Vocaluxe.Screens _SelectedPlayerNr = _NameSelections[_NameSelection].TilePlayerNr(mouseEvent); if (_SelectedPlayerNr != -1) { + CProfile[] profiles = CProfiles.GetProfiles(); + if (profiles.Length <= _SelectedPlayerNr) + return true; + //Update Game-infos with new player - CGame.Players[_SelectingFastPlayerNr - 1].ProfileID = _SelectedPlayerNr; + CGame.Players[_SelectingFastPlayerNr - 1].ProfileID = profiles[_SelectedPlayerNr].ID; //Update config for default players. - CConfig.Players[_SelectingFastPlayerNr - 1] = CProfiles.Profiles[_SelectedPlayerNr].FileName; + CConfig.Players[_SelectingFastPlayerNr - 1] = profiles[_SelectedPlayerNr].FileName; CConfig.SaveConfig(); //Update texture and name - _Statics[_StaticPlayerAvatar[_SelectingFastPlayerNr - 1]].Texture = CProfiles.Profiles[_SelectedPlayerNr].Avatar.Texture; - _Texts[_TextPlayer[_SelectingFastPlayerNr - 1]].Text = CProfiles.Profiles[_SelectedPlayerNr].PlayerName; + _Statics[_StaticPlayerAvatar[_SelectingFastPlayerNr - 1]].Texture = profiles[_SelectedPlayerNr].Avatar.Texture; + _Texts[_TextPlayer[_SelectingFastPlayerNr - 1]].Text = profiles[_SelectedPlayerNr].PlayerName; //Update profile-warning _CheckPlayers(); //Update Tiles-List @@ -516,14 +530,18 @@ namespace Vocaluxe.Screens { if (CGame.Players[i].ProfileID == -1) { + CProfile[] profiles = CProfiles.GetProfiles(); + if (profiles.Length <= _SelectedPlayerNr) + return true; + //Update Game-infos with new player - CGame.Players[i].ProfileID = _SelectedPlayerNr; + CGame.Players[i].ProfileID = profiles[_SelectedPlayerNr].ID; //Update config for default players. - CConfig.Players[i] = CProfiles.Profiles[_SelectedPlayerNr].FileName; + CConfig.Players[i] = profiles[_SelectedPlayerNr].FileName; CConfig.SaveConfig(); //Update texture and name - _Statics[_StaticPlayerAvatar[i]].Texture = CProfiles.Profiles[_SelectedPlayerNr].Avatar.Texture; - _Texts[_TextPlayer[i]].Text = CProfiles.Profiles[_SelectedPlayerNr].PlayerName; + _Statics[_StaticPlayerAvatar[i]].Texture = profiles[_SelectedPlayerNr].Avatar.Texture; + _Texts[_TextPlayer[i]].Text = profiles[_SelectedPlayerNr].PlayerName; //Update profile-warning _CheckPlayers(); //Update Tiles-List @@ -624,7 +642,7 @@ namespace Vocaluxe.Screens for (int i = 0; i < CSettings.MaxNumPlayer; i++) { _Statics[_StaticPlayerAvatar[i]].Texture = CProfiles.ICProfileIDValid(CGame.Players[i].ProfileID) ? - CProfiles.Profiles[CGame.Players[i].ProfileID].Avatar.Texture : + CProfiles.GetAvatarTextureFromProfile(CGame.Players[i].ProfileID) : _OriginalPlayerAvatarTextures[i]; _Texts[_TextPlayer[i]].Text = CProfiles.GetPlayerName(CGame.Players[i].ProfileID, i + 1); if (CGame.GetNumSongs() == 1 && CGame.GetSong(1).IsDuet) diff --git a/Vocaluxe/Screens/CScreenProfiles.cs b/Vocaluxe/Screens/CScreenProfiles.cs index bd637050..7a5ff289 100644 --- a/Vocaluxe/Screens/CScreenProfiles.cs +++ b/Vocaluxe/Screens/CScreenProfiles.cs @@ -18,6 +18,7 @@ #endregion using System; +using System.Collections.Generic; using System.Drawing; using System.Drawing.Imaging; using System.IO; @@ -25,6 +26,7 @@ using System.Windows.Forms; using Vocaluxe.Base; using VocaluxeLib; using VocaluxeLib.Menu; +using VocaluxeLib.Profile; namespace Vocaluxe.Screens { @@ -103,7 +105,7 @@ namespace Vocaluxe.Screens break; case EEditMode.PlayerName: _SelectSlides[_SelectSlideProfiles].RenameValue( - CProfiles.AddGetPlayerName(_SelectSlides[_SelectSlideProfiles].Selection, keyEvent.Unicode)); + CProfiles.AddGetPlayerName(_SelectSlides[_SelectSlideProfiles].ValueIndex, keyEvent.Unicode)); break; } } @@ -145,7 +147,7 @@ namespace Vocaluxe.Screens if (_EditMode == EEditMode.PlayerName) { _SelectSlides[_SelectSlideProfiles].RenameValue( - CProfiles.GetDeleteCharInPlayerName(_SelectSlides[_SelectSlideProfiles].Selection)); + CProfiles.GetDeleteCharInPlayerName(_SelectSlides[_SelectSlideProfiles].ValueIndex)); } else CGraphics.FadeTo(EScreens.ScreenMain); @@ -157,22 +159,22 @@ namespace Vocaluxe.Screens } if (_SelectSlides[_SelectSlideDifficulty].Selected) { - CProfiles.SetDifficulty(_SelectSlides[_SelectSlideProfiles].Selection, + CProfiles.SetDifficulty(_SelectSlides[_SelectSlideProfiles].ValueIndex, (EGameDifficulty)_SelectSlides[_SelectSlideDifficulty].Selection); } else if (_SelectSlides[_SelectSlideAvatars].Selected) { - CProfiles.SetAvatar(_SelectSlides[_SelectSlideProfiles].Selection, - _SelectSlides[_SelectSlideAvatars].Selection); + CProfiles.SetAvatar(_SelectSlides[_SelectSlideProfiles].ValueIndex, + _SelectSlides[_SelectSlideAvatars].ValueIndex); } else if (_SelectSlides[_SelectSlideGuestProfile].Selected) { - CProfiles.SetGuestProfile(_SelectSlides[_SelectSlideProfiles].Selection, + CProfiles.SetGuestProfile(_SelectSlides[_SelectSlideProfiles].ValueIndex, (EOffOn)_SelectSlides[_SelectSlideGuestProfile].Selection); } else if (_SelectSlides[_SelectSlideActive].Selected) { - CProfiles.SetActive(_SelectSlides[_SelectSlideProfiles].Selection, + CProfiles.SetActive(_SelectSlides[_SelectSlideProfiles].ValueIndex, (EOffOn)_SelectSlides[_SelectSlideActive].Selection); } } @@ -204,24 +206,24 @@ namespace Vocaluxe.Screens } else if (_SelectSlides[_SelectSlideDifficulty].Selected) { - CProfiles.SetDifficulty(_SelectSlides[_SelectSlideProfiles].Selection, + CProfiles.SetDifficulty(_SelectSlides[_SelectSlideProfiles].ValueIndex, (EGameDifficulty)_SelectSlides[_SelectSlideDifficulty].Selection); } else if (_SelectSlides[_SelectSlideAvatars].Selected) { - CProfiles.SetAvatar(_SelectSlides[_SelectSlideProfiles].Selection, - _SelectSlides[_SelectSlideAvatars].Selection); + CProfiles.SetAvatar(_SelectSlides[_SelectSlideProfiles].ValueIndex, + _SelectSlides[_SelectSlideAvatars].ValueIndex); if (CWebcam.IsDeviceAvailable() && _WebcamTexture.Index > 0) _OnDiscardSnapshot(); } else if (_SelectSlides[_SelectSlideGuestProfile].Selected) { - CProfiles.SetGuestProfile(_SelectSlides[_SelectSlideProfiles].Selection, + CProfiles.SetGuestProfile(_SelectSlides[_SelectSlideProfiles].ValueIndex, (EOffOn)_SelectSlides[_SelectSlideGuestProfile].Selection); } else if (_SelectSlides[_SelectSlideActive].Selected) { - CProfiles.SetActive(_SelectSlides[_SelectSlideProfiles].Selection, + CProfiles.SetActive(_SelectSlides[_SelectSlideProfiles].ValueIndex, (EOffOn)_SelectSlides[_SelectSlideActive].Selection); } else if (_Buttons[_ButtonWebcam].Selected && CWebcam.IsDeviceAvailable()) @@ -272,11 +274,12 @@ namespace Vocaluxe.Screens CWebcam.Stop(); CDraw.RemoveTexture(ref _WebcamTexture); - for (int j = 0; j < CProfiles.Avatars.Length; j++) + CAvatar[] avatars = CProfiles.GetAvatars(); + for (int j = 0; j < avatars.Length; j++) { - if (CProfiles.Avatars[j].FileName == (filename + i + ".png")) + if (Path.GetFileName(avatars[j].FileName) == (filename + i + ".png")) { - CProfiles.SetAvatar(_SelectSlides[_SelectSlideProfiles].Selection, j); + CProfiles.SetAvatar(_SelectSlides[_SelectSlideProfiles].ValueIndex, avatars[j].ID); break; } } @@ -302,17 +305,17 @@ namespace Vocaluxe.Screens { if (_SelectSlides[_SelectSlideProfiles].Selection > -1) { - _Buttons[_ButtonPlayerName].Text.Text = CProfiles.GetPlayerName(_SelectSlides[_SelectSlideProfiles].Selection); + _Buttons[_ButtonPlayerName].Text.Text = CProfiles.GetPlayerName(_SelectSlides[_SelectSlideProfiles].ValueIndex); if (_EditMode == EEditMode.PlayerName) _Buttons[_ButtonPlayerName].Text.Text += "|"; - _SelectSlides[_SelectSlideDifficulty].Selection = (int)CProfiles.GetDifficulty(_SelectSlides[_SelectSlideProfiles].Selection); - _SelectSlides[_SelectSlideGuestProfile].Selection = (int)CProfiles.GetGuestProfile(_SelectSlides[_SelectSlideProfiles].Selection); - _SelectSlides[_SelectSlideActive].Selection = (int)CProfiles.GetActive(_SelectSlides[_SelectSlideProfiles].Selection); + _SelectSlides[_SelectSlideDifficulty].Selection = (int)CProfiles.GetDifficulty(_SelectSlides[_SelectSlideProfiles].ValueIndex); + _SelectSlides[_SelectSlideGuestProfile].Selection = (int)CProfiles.GetGuestProfile(_SelectSlides[_SelectSlideProfiles].ValueIndex); + _SelectSlides[_SelectSlideActive].Selection = (int)CProfiles.GetActive(_SelectSlides[_SelectSlideProfiles].ValueIndex); - int avatarNr = CProfiles.GetAvatarNr(_SelectSlides[_SelectSlideProfiles].Selection); - _SelectSlides[_SelectSlideAvatars].Selection = avatarNr; - if (CWebcam.IsDeviceAvailable() && _WebcamTexture.Index > 0) + int avatarID = CProfiles.GetAvatarID(_SelectSlides[_SelectSlideProfiles].ValueIndex); + _SelectSlides[_SelectSlideAvatars].SetSelectionByValueIndex(avatarID); + if (CWebcam.IsDeviceAvailable() && _WebcamTexture.Index != -1) { if (_Snapshot == null) CWebcam.GetFrame(ref _WebcamTexture); @@ -320,7 +323,7 @@ namespace Vocaluxe.Screens _Statics[_StaticAvatar].Texture = _WebcamTexture; } else - _Statics[_StaticAvatar].Texture = CProfiles.Avatars[avatarNr].Texture; + _Statics[_StaticAvatar].Texture = CProfiles.GetAvatarTexture(avatarID); } return true; @@ -353,7 +356,7 @@ namespace Vocaluxe.Screens private void _DeleteProfile() { - CProfiles.DeleteProfile(_SelectSlides[_SelectSlideProfiles].Selection); + CProfiles.DeleteProfile(_SelectSlides[_SelectSlideProfiles].ValueIndex); _LoadProfiles(); UpdateGame(); } @@ -363,34 +366,44 @@ namespace Vocaluxe.Screens _EditMode = EEditMode.None; _SelectSlides[_SelectSlideProfiles].Clear(); - for (int i = 0; i < CProfiles.NumProfiles; i++) - _SelectSlides[_SelectSlideProfiles].AddValue(CProfiles.GetPlayerName(i)); + CProfile[] profiles = CProfiles.GetProfiles(); + for (int i = 0; i < profiles.Length; i++) + _SelectSlides[_SelectSlideProfiles].AddValue( + profiles[i].PlayerName, + new STexture(-1), + profiles[i].ID, + -1); if (CProfiles.NumProfiles > 0 && CProfiles.NumAvatars > 0) { - _SelectSlides[_SelectSlideDifficulty].Selection = (int)CProfiles.GetDifficulty(_SelectSlides[_SelectSlideProfiles].Selection); - _SelectSlides[_SelectSlideGuestProfile].Selection = (int)CProfiles.GetGuestProfile(_SelectSlides[_SelectSlideProfiles].Selection); - _SelectSlides[_SelectSlideActive].Selection = (int)CProfiles.GetActive(_SelectSlides[_SelectSlideProfiles].Selection); - _SelectSlides[_SelectSlideAvatars].Selection = CProfiles.GetAvatarNr(_SelectSlides[_SelectSlideProfiles].Selection); + _SelectSlides[_SelectSlideDifficulty].Selection = (int)CProfiles.GetDifficulty(_SelectSlides[_SelectSlideProfiles].ValueIndex); + _SelectSlides[_SelectSlideGuestProfile].Selection = (int)CProfiles.GetGuestProfile(_SelectSlides[_SelectSlideProfiles].ValueIndex); + _SelectSlides[_SelectSlideActive].Selection = (int)CProfiles.GetActive(_SelectSlides[_SelectSlideProfiles].ValueIndex); + _SelectSlides[_SelectSlideAvatars].SetSelectionByValueIndex(CProfiles.GetAvatarID(_SelectSlides[_SelectSlideProfiles].ValueIndex)); } } private void _LoadAvatars() { _SelectSlides[_SelectSlideAvatars].Clear(); - for (int i = 0; i < CProfiles.NumAvatars; i++) - _SelectSlides[_SelectSlideAvatars].AddValue(CProfiles.Avatars[i].FileName); + CAvatar[] avatars = CProfiles.GetAvatars(); + for (int i = 0; i < avatars.Length; i++) + _SelectSlides[_SelectSlideAvatars].AddValue( + Path.GetFileName(avatars[i].FileName), + new STexture(-1), + avatars[i].ID, + -1); } private void _NewProfile() { _EditMode = EEditMode.None; - CProfiles.NewProfile(); + int id = CProfiles.NewProfile(); _LoadProfiles(); - _SelectSlides[_SelectSlideProfiles].LastValue(); + _SelectSlides[_SelectSlideProfiles].SetSelectionByValueIndex(id); - CProfiles.SetAvatar(_SelectSlides[_SelectSlideProfiles].Selection, - _SelectSlides[_SelectSlideAvatars].Selection); + CProfiles.SetAvatar(_SelectSlides[_SelectSlideProfiles].ValueIndex, + _SelectSlides[_SelectSlideAvatars].ValueIndex); _SetInteractionToButton(_Buttons[_ButtonPlayerName]); _EditMode = EEditMode.PlayerName; diff --git a/Vocaluxe/Screens/CScreenScore.cs b/Vocaluxe/Screens/CScreenScore.cs index 192cac34..22756d7f 100644 --- a/Vocaluxe/Screens/CScreenScore.cs +++ b/Vocaluxe/Screens/CScreenScore.cs @@ -334,7 +334,7 @@ namespace Vocaluxe.Screens _Statics[_StaticPointsBar[p, CGame.NumPlayer - 1]].Rect.H; } if (CProfiles.ICProfileIDValid(players[p].ProfileID)) - _Statics[_StaticAvatar[p, CGame.NumPlayer - 1]].Texture = CProfiles.Profiles[players[p].ProfileID].Avatar.Texture; + _Statics[_StaticAvatar[p, CGame.NumPlayer - 1]].Texture = CProfiles.GetAvatarTextureFromProfile(players[p].ProfileID); } if (CConfig.ScoreAnimationTime < 1) diff --git a/Vocaluxe/Screens/CScreenSing.cs b/Vocaluxe/Screens/CScreenSing.cs index e34df75d..472037b2 100644 --- a/Vocaluxe/Screens/CScreenSing.cs +++ b/Vocaluxe/Screens/CScreenSing.cs @@ -1384,7 +1384,7 @@ namespace Vocaluxe.Screens for (int i = 0; i < CGame.NumPlayer; i++) { if (CProfiles.ICProfileIDValid(CGame.Players[i].ProfileID)) - _Statics[_StaticAvatars[i, CGame.NumPlayer - 1]].Texture = CProfiles.Profiles[CGame.Players[i].ProfileID].Avatar.Texture; + _Statics[_StaticAvatars[i, CGame.NumPlayer - 1]].Texture = CProfiles.GetAvatarTextureFromProfile(CGame.Players[i].ProfileID); else _Statics[_StaticAvatars[i, CGame.NumPlayer - 1]].Visible = false; } @@ -1395,7 +1395,7 @@ namespace Vocaluxe.Screens for (int i = 0; i < CGame.NumPlayer; i++) { if (CProfiles.ICProfileIDValid(CGame.Players[i].ProfileID)) - _Texts[_TextNames[i, CGame.NumPlayer - 1]].Text = CProfiles.Profiles[CGame.Players[i].ProfileID].PlayerName; + _Texts[_TextNames[i, CGame.NumPlayer - 1]].Text = CProfiles.GetPlayerName(CGame.Players[i].ProfileID); else _Texts[_TextNames[i, CGame.NumPlayer - 1]].Visible = false; } diff --git a/VocaluxeLib/IBasic.cs b/VocaluxeLib/IBasic.cs index 990dff45..fe92c5b8 100644 --- a/VocaluxeLib/IBasic.cs +++ b/VocaluxeLib/IBasic.cs @@ -70,6 +70,8 @@ namespace VocaluxeLib float GetDefaultMedleyFadeOutTime(); int GetMedleyMinSeriesLength(); float GetMedleyMinDuration(); + + string GetFolderProfiles(); } public interface ITheme diff --git a/VocaluxeLib/Menu/CNameSelection.cs b/VocaluxeLib/Menu/CNameSelection.cs index 2888d8ea..72fedb85 100644 --- a/VocaluxeLib/Menu/CNameSelection.cs +++ b/VocaluxeLib/Menu/CNameSelection.cs @@ -22,6 +22,8 @@ using System.Collections.Generic; using System.Windows.Forms; using System.Xml; +using VocaluxeLib.Profile; + namespace VocaluxeLib.Menu { struct SThemeNameSelection @@ -497,16 +499,18 @@ namespace VocaluxeLib.Menu private void _UpdateVisibleProfiles() { _VisibleProfiles.Clear(); - for (int i = 0; i < CBase.Profiles.GetProfiles().Length; i++) + CProfile[] profiles = CBase.Profiles.GetProfiles(); + + for (int i = 0; i < profiles.Length; i++) { - bool visible = CBase.Profiles.GetProfiles()[i].Active == EOffOn.TR_CONFIG_ON; + bool visible = profiles[i].Active == EOffOn.TR_CONFIG_ON; if (visible) { //Show profile only if active for (int p = 0; p < CBase.Game.GetNumPlayer(); p++) { //Don't show profile if is selected, but if selected and guest - if (CBase.Game.GetPlayers()[p].ProfileID == i && CBase.Profiles.GetProfiles()[i].GuestProfile == EOffOn.TR_CONFIG_OFF) + if (CBase.Game.GetPlayers()[p].ProfileID == profiles[i].ID && profiles[i].GuestProfile == EOffOn.TR_CONFIG_OFF) visible = false; } } diff --git a/VocaluxeLib/Profile/CAvatar.cs b/VocaluxeLib/Profile/CAvatar.cs index 466ceab8..3f7dd941 100644 --- a/VocaluxeLib/Profile/CAvatar.cs +++ b/VocaluxeLib/Profile/CAvatar.cs @@ -9,28 +9,23 @@ namespace VocaluxeLib.Profile { public class CAvatar { - private int _ID; - - public int ID - { - get { return _ID; } - } - + public int ID; public string FileName; public STexture Texture; public CAvatar(int ID) { - _ID = ID; + this.ID = ID; FileName = String.Empty; Texture = new STexture(-1); } - public void LoadFromFile(string NewFileName) + public bool LoadFromFile(string NewFileName) { FileName = NewFileName; CBase.Drawing.RemoveTexture(ref Texture); Texture = CBase.Drawing.AddTexture(FileName); + return (Texture.Index != -1); } public void Reload() @@ -38,5 +33,10 @@ namespace VocaluxeLib.Profile CBase.Drawing.RemoveTexture(ref Texture); Texture = CBase.Drawing.AddTexture(FileName); } + + public void Unload() + { + CBase.Drawing.RemoveTexture(ref Texture); + } } } diff --git a/VocaluxeLib/Profile/CProfile.cs b/VocaluxeLib/Profile/CProfile.cs index fc9f19b4..f4ed3efd 100644 --- a/VocaluxeLib/Profile/CProfile.cs +++ b/VocaluxeLib/Profile/CProfile.cs @@ -16,20 +16,14 @@ namespace VocaluxeLib.Profile ConformanceLevel = ConformanceLevel.Document }; - private int _ID; - private int _AvatarID; - - public int ID - { - get { return _ID; } - } - + public int ID; + public string PlayerName; public string FileName; public string AvatarFileName; public EGameDifficulty Difficulty; - public CAvatar Avatar; //should be removed! use avatar id + public CAvatar Avatar; public EOffOn GuestProfile; public EOffOn Active; @@ -78,6 +72,29 @@ namespace VocaluxeLib.Profile public void SaveProfile() { + if (FileName == String.Empty) + { + string filename = string.Empty; + foreach (char chr in PlayerName) + { + if (char.IsLetter(chr)) + filename += chr.ToString(); + } + + if (filename == "") + filename = "1"; + + int i = 0; + while (File.Exists(Path.Combine(CBase.Settings.GetFolderProfiles(), filename + ".xml"))) + { + i++; + if (!File.Exists(Path.Combine(CBase.Settings.GetFolderProfiles(), filename + i + ".xml"))) + filename += i; + } + + FileName = Path.Combine(Environment.CurrentDirectory, CBase.Settings.GetFolderProfiles(), filename + ".xml"); + } + if (FileName == String.Empty) return; @@ -99,7 +116,7 @@ namespace VocaluxeLib.Profile writer.WriteStartElement("Info"); writer.WriteElementString("PlayerName", PlayerName); writer.WriteElementString("Difficulty", Enum.GetName(typeof(EGameDifficulty), Difficulty)); - writer.WriteElementString("Avatar", Avatar.FileName); + writer.WriteElementString("Avatar", Path.GetFileName(Avatar.FileName)); writer.WriteElementString("GuestProfile", Enum.GetName(typeof(EOffOn), GuestProfile)); writer.WriteElementString("Active", Enum.GetName(typeof(EOffOn), Active)); writer.WriteEndElement();