Remove Profiles.FileName and use FilePath correctly

This commit is contained in:
Flamefire committed 2014-10-27 20:02:06 +01:00
1 parent af88bb2161
commit 93a415205e
5 files changed
+29 -43

No files matched your search

+10 -22
View File
@@ -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
+7 -5
View File
@@ -401,9 +401,8 @@ namespace VocaluxeLib.Xml
}
#endregion
private T _Deserialize<T>(XmlReader reader) where T : new()
private T _Deserialize<T>(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<T>(reader);
return _Deserialize<T>(reader, new T());
}
finally
{
@@ -433,8 +432,11 @@ namespace VocaluxeLib.Xml
}
}
public T Deserialize<T>(string filePath) where T : new()
public T Deserialize<T>(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<T>(reader);
return _Deserialize<T>(reader, o);
}
finally
{