Files
Quentin Bray 6ffa63b923 Apply R# clean-up for reliability (#877)
* Apply R# clean-up for reliability

- Migrate to NUnit4 Assert.That
- Update dependencise versions
- Fix LangVersion to 9.0 (until net core migration)
- Reformat code
- Apply 'var' style (easier for refactoring)
- Remove redundant parentheses
- Add braces on every condition blocks (1 line)
- Uses new null checking patterns
- Uses AutoProperties when possible
- Uses string scalar type instead of class
- Clean usings
- Add .orig files in git ignore
- Rename some properties to respect defined conventions and US format definition
	- ID -> Id
	- BPM -> Bpm
	- Finish -> End

* Update R# settings according to the clean-up made
2026-06-22 19:52:06 +02:00

180 lines
4.9 KiB
C#

#region license
// This file is part of Vocaluxe.
//
// Vocaluxe is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Vocaluxe is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Vocaluxe. If not, see <http://www.gnu.org/licenses/>.
#endregion
using System;
using System.ComponentModel;
using System.IO;
using System.Xml.Serialization;
using VocaluxeLib.Log;
using VocaluxeLib.Xml;
namespace VocaluxeLib.Profile
{
[Flags]
public enum EProfileChangedFlags
{
None = 1,
Avatar = 2,
Profile = 4
}
public delegate void ProfileChangedCallback(EProfileChangedFlags typeChanged);
public class CProfile
{
private struct SOldXmlProfile
{
#pragma warning disable 649
public CProfile Info;
#pragma warning restore 649
}
[DefaultValue(null)]
public Guid Id;
public string PlayerName;
[XmlIgnore]
public string FilePath;
[DefaultValue(null)]
public byte[] PasswordHash;
[DefaultValue(null)]
public byte[] PasswordSalt;
public EGameDifficulty Difficulty;
[XmlIgnore]
public CAvatar Avatar { get; set; }
public EUserRole UserRole;
public EOffOn Active;
[XmlElement("Avatar")]
// ReSharper disable UnusedMember.Global
public string AvatarFileName
// ReSharper restore UnusedMember.Global
{
get { return Path.GetFileName(Avatar.FileName); }
set
{
Avatar = CBase.Profiles.GetAvatarByFilename(value);
if (Avatar == null)
{
CLog.Error("Avatar '" + value + "' not found");
}
}
}
public CProfile()
{
Id = Guid.NewGuid();
PlayerName = string.Empty;
Difficulty = EGameDifficulty.TR_CONFIG_EASY;
UserRole = EUserRole.TR_USERROLE_NORMAL;
Active = EOffOn.TR_CONFIG_ON;
}
public bool LoadProfile(string filePath)
{
FilePath = filePath;
return LoadProfile();
}
public bool LoadProfile()
{
var xml = new CXmlDeserializer();
try
{
xml.Deserialize(FilePath, this);
//If Id couldn't be loaded, generate a new one and save it
if (Id == Guid.Empty)
{
Id = Guid.NewGuid();
SaveProfile();
}
}
catch (Exception e)
{
if (_ConvertProfile(ref e))
{
return true;
}
CLog.Error("Error loading profile file " + Path.GetFileName(FilePath) + ": " + e.Message);
return false;
}
return true;
}
private bool _ConvertProfile(ref Exception e)
{
var xml = new CXmlDeserializer();
var ser = new CXmlSerializer();
try
{
var old = xml.Deserialize<SOldXmlProfile>(FilePath);
var newXml = ser.Serialize(old.Info);
xml.DeserializeString(newXml, this);
if (Id == null)
{
Id = Guid.NewGuid();
}
ser.Serialize(FilePath, this);
}
catch (Exception e2)
{
if (!(e2 is CXmlException))
{
e = e2;
}
return false;
}
return true;
}
public void SaveProfile()
{
if (string.IsNullOrEmpty(FilePath))
{
var filename = string.Empty;
// ReSharper disable LoopCanBeConvertedToQuery
foreach (var chr in PlayerName)
// ReSharper restore LoopCanBeConvertedToQuery
{
if (char.IsLetter(chr))
{
filename += chr.ToString();
}
}
if (filename == "")
{
filename = "1";
}
FilePath = CHelper.GetUniqueFileName(Path.Combine(CBase.Settings.GetDataPath(), CBase.Settings.GetFolderProfiles()), filename + ".xml");
}
var xml = new CXmlSerializer();
xml.Serialize(FilePath, this);
}
}
}