From 93a415205e1b11124d7162339905561e6b3c91d9 Mon Sep 17 00:00:00 2001 From: Flamefire Date: Mon, 27 Oct 2014 20:02:06 +0100 Subject: [PATCH] Remove Profiles.FileName and use FilePath correctly --- Vocaluxe/Base/CConfig.cs | 2 +- Vocaluxe/Base/CProfiles.cs | 24 ++++++++----------- Vocaluxe/Base/Server/CVocaluxeServer.cs | 2 +- VocaluxeLib/Profile/CProfile.cs | 32 ++++++++----------------- VocaluxeLib/Xml/CXmlSerializer.cs | 12 ++++++---- 5 files changed, 29 insertions(+), 43 deletions(-) diff --git a/Vocaluxe/Base/CConfig.cs b/Vocaluxe/Base/CConfig.cs index 98ef2d1d..5f87e326 100644 --- a/Vocaluxe/Base/CConfig.cs +++ b/Vocaluxe/Base/CConfig.cs @@ -693,7 +693,7 @@ namespace Vocaluxe.Base foreach (CProfile profile in profiles) { - if (Path.GetFileName(profile.FileName) == Config.Game.Players[j] && profile.Active == EOffOn.TR_CONFIG_ON) + if (Path.GetFileName(profile.FilePath) == Config.Game.Players[j] && profile.Active == EOffOn.TR_CONFIG_ON) { //Update Game-infos with player CGame.Players[j].ProfileID = profile.ID; diff --git a/Vocaluxe/Base/CProfiles.cs b/Vocaluxe/Base/CProfiles.cs index 1abbda41..1cf1d983 100644 --- a/Vocaluxe/Base/CProfiles.cs +++ b/Vocaluxe/Base/CProfiles.cs @@ -327,10 +327,10 @@ namespace Vocaluxe.Base { var profile = new CProfile { - FileName = fileName != "" ? Path.Combine(CConfig.ProfileFolders[0], fileName) : String.Empty + FilePath = fileName != "" ? Path.Combine(CConfig.ProfileFolders[0], fileName) : String.Empty }; - if (File.Exists(profile.FileName)) + if (File.Exists(profile.FilePath)) return -1; profile.ID = _ProfileIDs.Dequeue(); @@ -393,7 +393,7 @@ namespace Vocaluxe.Base if (!IsProfileIDValid(profileID)) return String.Empty; - return Path.GetFileName(_Profiles[profileID].FileName); + return Path.GetFileName(_Profiles[profileID].FilePath); } public static string AddGetPlayerName(int profileID, char chr) @@ -534,7 +534,7 @@ namespace Vocaluxe.Base foreach (int id in ids) { if (_Profiles[id].LoadProfile()) - knownFiles.Add(Path.GetFileName(_Profiles[id].FileName)); + knownFiles.Add(Path.GetFileName(_Profiles[id].FilePath)); else _Profiles.Remove(id); } @@ -550,13 +550,9 @@ namespace Vocaluxe.Base if (knownFiles.Contains(Path.GetFileName(file))) continue; - var profile = new CProfile - { - FileName = Path.GetFileName(file), - FilePath = Path.GetDirectoryName(file) - }; + var profile = new CProfile(); - if (profile.LoadProfile()) + if (profile.LoadProfile(file)) { profile.ID = _ProfileIDs.Dequeue(); _Profiles.Add(profile.ID, profile); @@ -605,7 +601,7 @@ namespace Vocaluxe.Base if (!IsProfileIDValid(profileID)) return; - if (_Profiles[profileID].FileName == "") + if (string.IsNullOrEmpty(_Profiles[profileID].FilePath)) { _RemoveProfile(profileID); return; @@ -616,13 +612,13 @@ namespace Vocaluxe.Base //Check if profile saved in config for (int i = 0; i < CSettings.MaxNumPlayer; i++) { - if (CConfig.Config.Game.Players[i] == _Profiles[profileID].FileName) + if (CConfig.Config.Game.Players[i] == GetProfileFileName(profileID)) { CConfig.Config.Game.Players[i] = string.Empty; CConfig.SaveConfig(); } } - File.Delete(Path.Combine(_Profiles[profileID].FilePath, _Profiles[profileID].FileName)); + File.Delete(_Profiles[profileID].FilePath); _RemoveProfile(profileID); //Check if profile is selected in game @@ -636,7 +632,7 @@ namespace Vocaluxe.Base } catch (Exception) { - CLog.LogError("Can't delete Profile File " + _Profiles[profileID].FileName + ".xml"); + CLog.LogError("Can't delete Profile File " + _Profiles[profileID].FilePath); } _ProfilesChanged = true; } diff --git a/Vocaluxe/Base/Server/CVocaluxeServer.cs b/Vocaluxe/Base/Server/CVocaluxeServer.cs index 32f87b2e..da781d27 100644 --- a/Vocaluxe/Base/Server/CVocaluxeServer.cs +++ b/Vocaluxe/Base/Server/CVocaluxeServer.cs @@ -205,7 +205,7 @@ namespace Vocaluxe.Base.Server newProfile = new CProfile { ID = existingProfile.ID, - FileName = existingProfile.FileName, + FilePath = existingProfile.FilePath, Active = existingProfile.Active, Avatar = existingProfile.Avatar, Difficulty = existingProfile.Difficulty, diff --git a/VocaluxeLib/Profile/CProfile.cs b/VocaluxeLib/Profile/CProfile.cs index 95ad5d4f..a6c74447 100644 --- a/VocaluxeLib/Profile/CProfile.cs +++ b/VocaluxeLib/Profile/CProfile.cs @@ -18,6 +18,7 @@ using System; using System.IO; using System.Xml; +using System.Xml.Serialization; using VocaluxeLib.Xml; namespace VocaluxeLib.Profile @@ -34,11 +35,10 @@ namespace VocaluxeLib.Profile public class CProfile { - public int ID; + [XmlIgnore] public int ID; public string PlayerName; public string FilePath; - public string FileName; public byte[] PasswordHash; public byte[] PasswordSalt; @@ -55,28 +55,18 @@ namespace VocaluxeLib.Profile Difficulty = EGameDifficulty.TR_CONFIG_EASY; UserRole = EUserRole.TR_USERROLE_NORMAL; Active = EOffOn.TR_CONFIG_ON; - - FileName = String.Empty; } - public bool LoadProfile(string pathName, string fileName) + public bool LoadProfile(string filePath) { - FilePath = pathName; - FileName = fileName; - return LoadProfile(); - } - - public bool LoadProfile(string file) - { - FileName = Path.GetFileName(file); - FilePath = Path.GetDirectoryName(file); + FilePath = filePath; return LoadProfile(); } public bool LoadProfile() { - CXMLReader xmlReader = CXMLReader.OpenFile(Path.Combine(FilePath, FileName)); + CXMLReader xmlReader = CXMLReader.OpenFile(FilePath); if (xmlReader == null) return false; @@ -98,19 +88,17 @@ namespace VocaluxeLib.Profile xmlReader.GetValue("//root/Info/PasswordSalt", out passwordSalt); PasswordSalt = !string.IsNullOrEmpty(passwordSalt) ? Convert.FromBase64String(passwordSalt) : null; if (!ok) - CBase.Log.LogError("Error loading profile file: " + FileName); + CBase.Log.LogError("Error loading profile file: " + FilePath); return ok; } - CBase.Log.LogError("Can't find PlayerName in Profile File: " + FileName); + CBase.Log.LogError("Can't find PlayerName in Profile File: " + FilePath); return false; } public void SaveProfile() { if (String.IsNullOrEmpty(FilePath)) - FilePath = Path.Combine(CBase.Settings.GetDataPath(), CBase.Settings.GetFolderProfiles()); - if (FileName == String.Empty) { string filename = string.Empty; // ReSharper disable LoopCanBeConvertedToQuery @@ -124,17 +112,17 @@ namespace VocaluxeLib.Profile if (filename == "") filename = "1"; - FileName = CHelper.GetUniqueFileName(FilePath, filename + ".xml", false); + FilePath = Path.Combine(CBase.Settings.GetDataPath(), CBase.Settings.GetFolderProfiles(), CHelper.GetUniqueFileName(FilePath, filename + ".xml", false)); } XmlWriter writer; try { - writer = XmlWriter.Create(Path.Combine(FilePath, FileName), CBase.Config.GetXMLSettings()); + writer = XmlWriter.Create(FilePath, CBase.Config.GetXMLSettings()); } catch (Exception e) { - CBase.Log.LogError("Error creating/opening Profile File " + FileName + ": " + e.Message); + CBase.Log.LogError("Error creating/opening Profile File " + FilePath + ": " + e.Message); return; } try diff --git a/VocaluxeLib/Xml/CXmlSerializer.cs b/VocaluxeLib/Xml/CXmlSerializer.cs index 07f09c91..f78717be 100644 --- a/VocaluxeLib/Xml/CXmlSerializer.cs +++ b/VocaluxeLib/Xml/CXmlSerializer.cs @@ -401,9 +401,8 @@ namespace VocaluxeLib.Xml } #endregion - private T _Deserialize(XmlReader reader) where T : new() + private T _Deserialize(XmlReader reader, object result) where T : new() { - object result = new T(); if (reader.IsEmptyElement) return (T)result; @@ -425,7 +424,7 @@ namespace VocaluxeLib.Xml }; try { - return _Deserialize(reader); + return _Deserialize(reader, new T()); } finally { @@ -433,8 +432,11 @@ namespace VocaluxeLib.Xml } } - public T Deserialize(string filePath) where T : new() + public T Deserialize(string filePath, object o = null) where T : new() { + Debug.Assert(o == null || o.GetType() is T); + if (o == null) + o = new T(); var reader = new XmlTextReader(filePath) { WhitespaceHandling = WhitespaceHandling.Significant, @@ -443,7 +445,7 @@ namespace VocaluxeLib.Xml }; try { - return _Deserialize(reader); + return _Deserialize(reader, o); } finally {