diff --git a/Vocaluxe/Base/CCover.cs b/Vocaluxe/Base/CCover.cs index 9c7a33ce..033a1184 100644 --- a/Vocaluxe/Base/CCover.cs +++ b/Vocaluxe/Base/CCover.cs @@ -62,7 +62,7 @@ namespace Vocaluxe.Base { get { - List coverThemes = new List(); + var coverThemes = new List(); for (int i = 0; i < _CoverThemes.Count; i++) coverThemes.Add(_CoverThemes[i].Name); @@ -154,7 +154,7 @@ namespace Vocaluxe.Base if (xmlReader != null) { - SCoverTheme coverTheme = new SCoverTheme(); + var coverTheme = new SCoverTheme(); xmlReader.GetValue("//root/Info/Name", out coverTheme.Name, String.Empty); xmlReader.GetValue("//root/Info/Folder", out coverTheme.Folder, String.Empty); @@ -179,7 +179,7 @@ namespace Vocaluxe.Base if (String.IsNullOrEmpty(coverTheme.Name)) return; - List ignoreList = new List(); + var ignoreList = new List(); CXMLReader xmlReader = CXMLReader.OpenFile(Path.Combine(CSettings.FolderCover, coverTheme.File)); if (xmlReader != null) { @@ -206,7 +206,7 @@ namespace Vocaluxe.Base } } - List files = new List(); + var files = new List(); string coverPath = Path.Combine(CSettings.FolderCover, coverTheme.Folder); files.AddRange(CHelper.ListFiles(coverPath, "*.png", true, true)); @@ -222,19 +222,17 @@ namespace Vocaluxe.Base } } - private static void _AddCover(string Name, string File) + private static void _AddCover(string name, string file) { - CTexture texture = CDraw.AddTexture(File); + CTexture texture = CDraw.AddTexture(file); - if (!_CoverExists(Name)) - { - _Cover.Add(Name, texture); - } + if (!_CoverExists(name)) + _Cover.Add(name, texture); else { - CTexture tex = _Cover[Name]; + CTexture tex = _Cover[name]; CDraw.RemoveTexture(ref tex); - _Cover[Name] = texture; + _Cover[name] = texture; } } } diff --git a/Vocaluxe/Base/CDataBase.cs b/Vocaluxe/Base/CDataBase.cs index d9f56b01..ffe314cf 100644 --- a/Vocaluxe/Base/CDataBase.cs +++ b/Vocaluxe/Base/CDataBase.cs @@ -71,6 +71,76 @@ namespace Vocaluxe.Base } #region Highscores + + public static bool GetDataBaseSongInfos(string artist, string title, out int numPlayed, out string dateAdded, out int highscoreID) + { + string sArtist = string.Empty; + string sTitle = string.Empty; + int songID = -1; + using (SQLiteConnection connection = new SQLiteConnection()) + { + connection.ConnectionString = "Data Source=" + _HighscoreFilePath; + + try + { + connection.Open(); + } + catch (Exception) + { + } + + using (SQLiteCommand command = new SQLiteCommand(connection)) + { + songID = _GetDataBaseSongID(artist, title, 0, command); + highscoreID = songID; + } + } + return _GetDataBaseSongInfos(songID, out sArtist, out sTitle, out numPlayed, out dateAdded, _HighscoreFilePath); + } + + public static void IncreaseSongCounter(SPlayer player) + { + using (SQLiteConnection connection = new SQLiteConnection()) + { + connection.ConnectionString = "Data Source=" + _HighscoreFilePath; + + try + { + connection.Open(); + } + catch (Exception) + { + } + + using (SQLiteCommand command = new SQLiteCommand(connection)) + { + int dataBaseSongID = _GetDataBaseSongID(player, command); + _IncreaseSongCounter(dataBaseSongID, command); + } + } + } + + public static void IncreaseSongCounter(int dataBaseSongID) + { + using (SQLiteConnection connection = new SQLiteConnection()) + { + connection.ConnectionString = "Data Source=" + _HighscoreFilePath; + + try + { + connection.Open(); + } + catch (Exception) + { + } + + using (SQLiteCommand command = new SQLiteCommand(connection)) + { + _IncreaseSongCounter(dataBaseSongID, command); + } + } + } + public static int AddScore(string playerName, int score, int lineNr, long date, int medley, int duet, int shortSong, int difficulty, string artist, string title, int numPlayed, string filePath) { @@ -113,7 +183,7 @@ namespace Vocaluxe.Base using (SQLiteCommand command = new SQLiteCommand(connection)) { - int dataBaseSongID = _GetDataBaseSongID(player, command); + int dataBaseSongID = CSongs.GetSong(player.SongID).DataBaseSongID; return _AddScore(CProfiles.GetPlayerName(player.ProfileID), (int)Math.Round(player.Points), player.VoiceNr, player.DateTicks, player.Medley ? 1 : 0, player.Duet ? 1 : 0, player.ShortSong ? 1 : 0, (int)CProfiles.GetDifficulty(player.ProfileID), dataBaseSongID, command); } @@ -250,6 +320,13 @@ namespace Vocaluxe.Base } } + private static void _IncreaseSongCounter(int dataBaseSongID, SQLiteCommand command) + { + command.CommandText = "UPDATE Songs SET NumPlayed = NumPlayed + 1 WHERE [id] = @id"; + command.Parameters.Add("@id", DbType.Int32, 0).Value = dataBaseSongID; + command.ExecuteNonQuery(); + } + private static int _GetDataBaseSongID(SPlayer player, SQLiteCommand command) { CSong song = CSongs.GetSong(player.SongID); @@ -275,14 +352,16 @@ namespace Vocaluxe.Base reader.Dispose(); return id; } + if (reader != null) reader.Close(); - command.CommandText = "INSERT INTO Songs (Title, Artist, NumPlayed) " + - "VALUES (@title, @artist, @numplayed)"; + command.CommandText = "INSERT INTO Songs (Title, Artist, NumPlayed, DateAdded) " + + "VALUES (@title, @artist, @numplayed, @dateadded)"; command.Parameters.Add("@title", DbType.String, 0).Value = title; command.Parameters.Add("@artist", DbType.String, 0).Value = artist; command.Parameters.Add("@numplayed", DbType.Int32, 0).Value = defNumPlayed; + command.Parameters.Add("@dateadded", System.Data.DbType.Int64, 0).Value = DateTime.Now.Ticks; command.ExecuteNonQuery(); command.CommandText = "SELECT id FROM Songs WHERE [Title] = @title AND [Artist] = @artist"; @@ -302,11 +381,12 @@ namespace Vocaluxe.Base return -1; } - private static bool _GetDataBaseSongInfos(int songID, out string artist, out string title, out int numPlayed, string filePath) + private static bool _GetDataBaseSongInfos(int songID, out string artist, out string title, out int numPlayed, out string dateAdded, string filePath) { artist = String.Empty; title = String.Empty; numPlayed = 0; + dateAdded = String.Empty; using (SQLiteConnection connection = new SQLiteConnection()) { @@ -323,7 +403,7 @@ namespace Vocaluxe.Base using (SQLiteCommand command = new SQLiteCommand(connection)) { - command.CommandText = "SELECT Artist, Title, NumPlayed FROM Songs WHERE [id] = @id"; + command.CommandText = "SELECT Artist, Title, NumPlayed, DateAdded FROM Songs WHERE [id] = @id"; command.Parameters.Add("@id", DbType.String, 0).Value = songID; SQLiteDataReader reader; @@ -342,6 +422,7 @@ namespace Vocaluxe.Base artist = reader.GetString(0); title = reader.GetString(1); numPlayed = reader.GetInt32(2); + dateAdded = reader.GetInt64(3).ToString("dd/MM/yyyy"); reader.Dispose(); return true; } @@ -504,6 +585,7 @@ namespace Vocaluxe.Base _CreateHighscoreDBV1(filePath); _ConvertFrom110(filePath); _UpdateDatabase(1, connection); + _UpdateDatabase(2, connection); } else if (version == 0 && scoresTableExists) { @@ -511,6 +593,7 @@ namespace Vocaluxe.Base _CreateHighscoreDBV1(filePath); _ConvertFrom101(filePath); _UpdateDatabase(1, connection); + _UpdateDatabase(2, connection); } else _CreateHighscoreDB(filePath); @@ -524,6 +607,7 @@ namespace Vocaluxe.Base _CreateHighscoreDBV1(filePath); _ConvertFrom110(filePath); _UpdateDatabase(1, connection); + _UpdateDatabase(2, connection); } else if (version == 0 && scoresTableExists) { @@ -531,6 +615,7 @@ namespace Vocaluxe.Base _CreateHighscoreDBV1(filePath); _ConvertFrom101(filePath); _UpdateDatabase(1, connection); + _UpdateDatabase(2, connection); } else _CreateHighscoreDB(filePath); @@ -831,8 +916,16 @@ namespace Vocaluxe.Base private static bool _UpdateDatabase(int currentVersion, SQLiteConnection connection) { bool updated = true; + if (currentVersion < 2) + { updated &= _ConvertV1toV2(connection); + } + if (currentVersion < 3) + { + updated &= _ConvertV2toV3(connection); + } + return updated; } @@ -853,6 +946,25 @@ namespace Vocaluxe.Base return true; } + private static bool _ConvertV2toV3(SQLiteConnection connection) + { + SQLiteCommand command; + + command = new SQLiteCommand(connection); + + command.CommandText = "ALTER TABLE Songs ADD DateAdded BIGINT"; + command.ExecuteNonQuery(); + command.CommandText = "UPDATE Songs SET [DateAdded] = @DateAdded"; + command.Parameters.Add("@DateAdded",System.Data.DbType.Int64, 0).Value = DateTime.Now.Ticks; + command.ExecuteNonQuery(); + command.CommandText = "UPDATE Version SET [Value] = @version"; + command.Parameters.Add("@version", System.Data.DbType.Int32, 0).Value = 3; + command.ExecuteNonQuery(); + command.Dispose(); + + return true; + } + private static bool _ImportData(string sourceDBPath) { #region open db @@ -897,9 +1009,9 @@ namespace Vocaluxe.Base int shortsong = source.GetInt32(7); int diff = source.GetInt32(8); - string artist, title; + string artist, title, dateadded; int numplayed; - if (_GetDataBaseSongInfos(songid, out artist, out title, out numplayed, sourceDBPath)) + if (_GetDataBaseSongInfos(songid, out artist, out title, out numplayed, out dateadded, sourceDBPath)) AddScore(player, score, linenr, date, medley, duet, shortsong, diff, artist, title, numplayed, _HighscoreFilePath); } #endregion import table scores diff --git a/Vocaluxe/Base/CMain.cs b/Vocaluxe/Base/CMain.cs index 61a997ea..fdf8a305 100644 --- a/Vocaluxe/Base/CMain.cs +++ b/Vocaluxe/Base/CMain.cs @@ -782,6 +782,11 @@ namespace Vocaluxe.Base { return CDataBase.GetCover(fileName, ref texture, coverSize); } + + public bool GetDataBaseSongInfos(string artist, string title, out int numPlayed, out string dateAdded, out int highscoreID) + { + return CDataBase.GetDataBaseSongInfos(artist, title, out numPlayed, out dateAdded, out highscoreID); + } } class CBcontrollers : IControllers diff --git a/Vocaluxe/Base/CPlaylists.cs b/Vocaluxe/Base/CPlaylists.cs index 1dc0493b..533e2d23 100644 --- a/Vocaluxe/Base/CPlaylists.cs +++ b/Vocaluxe/Base/CPlaylists.cs @@ -52,14 +52,14 @@ namespace Vocaluxe.Base public static void Init() { LoadPlaylists(); - ConvertUSDXPlaylists(); + _ConvertUSDXPlaylists(); _SortPlaylistsByName(); } - public static void ConvertUSDXPlaylists() + private static void _ConvertUSDXPlaylists() { - List files = new List(); + var files = new List(); files.AddRange(CHelper.ListFiles(CSettings.FolderPlaylists, "*.upl", true, true)); foreach (string file in files) @@ -73,12 +73,12 @@ namespace Vocaluxe.Base public static void LoadPlaylists() { _Playlists = new List(); - List files = new List(); + var files = new List(); files.AddRange(CHelper.ListFiles(CSettings.FolderPlaylists, "*.xml", true, true)); foreach (string file in files) { - CPlaylistFile playlist = new CPlaylistFile(file); + var playlist = new CPlaylistFile(file); _Playlists.Add(playlist); } } @@ -126,7 +126,7 @@ namespace Vocaluxe.Base public static int NewPlaylist() { - CPlaylistFile pl = new CPlaylistFile {PlaylistName = "New Playlist"}; + var pl = new CPlaylistFile {PlaylistName = "New Playlist"}; _Playlists.Add(pl); return _Playlists.Count - 1; } @@ -219,7 +219,7 @@ namespace Vocaluxe.Base private static CPlaylistFile _ConvertUSDXPlaylist(string file) { - CPlaylistFile pl = new CPlaylistFile(); + var pl = new CPlaylistFile(); ReadOnlyCollection allSongs = CSongs.AllSongs; if (!File.Exists(file)) diff --git a/Vocaluxe/Base/CSettings.cs b/Vocaluxe/Base/CSettings.cs index eeaffaef..71cfd196 100644 --- a/Vocaluxe/Base/CSettings.cs +++ b/Vocaluxe/Base/CSettings.cs @@ -55,7 +55,7 @@ namespace Vocaluxe.Base public const ERevision VersionRevision = ERevision.Beta; - public const int DatabaseHighscoreVersion = 2; + public const int DatabaseHighscoreVersion = 3; public const int DatabaseCoverVersion = 1; public const int DatabaseCreditsRessourcesVersion = 1; @@ -224,4 +224,4 @@ namespace Vocaluxe.Base Directory.CreateDirectory(path); } } -} \ No newline at end of file +} diff --git a/Vocaluxe/Base/CUtility.cs b/Vocaluxe/Base/CUtility.cs index 27ad1db3..bb035d0c 100644 --- a/Vocaluxe/Base/CUtility.cs +++ b/Vocaluxe/Base/CUtility.cs @@ -31,12 +31,16 @@ namespace Vocaluxe.Base private static float _Fps; private static readonly double _NanosecPerTick = (1000.0 * 1000.0 * 1000.0) / Stopwatch.Frequency; + // ReSharper disable MemberCanBePrivate.Global public static void Reset() + // ReSharper restore MemberCanBePrivate.Global { _Stopwatch.Reset(); } + // ReSharper disable MemberCanBePrivate.Global public static void Start() + // ReSharper restore MemberCanBePrivate.Global { _Stopwatch.Start(); } @@ -117,7 +121,7 @@ namespace Vocaluxe.Base if (!_Timer.IsRunning || (_Timer.ElapsedMilliseconds > 75) || !keyRepeat) { - SKeyEvent pool = new SKeyEvent(ESender.Keyboard, alt, shift, ctrl, pressed && (unicode != Char.MinValue), unicode, key); + var pool = new SKeyEvent(ESender.Keyboard, alt, shift, ctrl, pressed && (unicode != Char.MinValue), unicode, key); lock (_CopyLock) { @@ -226,7 +230,7 @@ namespace Vocaluxe.Base x = (int)(x * (float)CSettings.RenderW / CDraw.GetScreenWidth()); y = (int)(y * (float)CSettings.RenderH / CDraw.GetScreenHeight()); - SMouseEvent pool = new SMouseEvent(ESender.Mouse, _Mod, x, y, lb, ld, rb, -wheel / 120, lbh, rbh, mb, mbh); + var pool = new SMouseEvent(ESender.Mouse, _Mod, x, y, lb, ld, rb, -wheel / 120, lbh, rbh, mb, mbh); lock (_CopyLock) { diff --git a/Vocaluxe/Base/Fonts/CGlyph.cs b/Vocaluxe/Base/Fonts/CGlyph.cs index ba136617..9c2331ee 100644 --- a/Vocaluxe/Base/Fonts/CGlyph.cs +++ b/Vocaluxe/Base/Fonts/CGlyph.cs @@ -80,7 +80,7 @@ namespace Vocaluxe.Base.Fonts bmpSize = new Size(1, 1); } } - using (Bitmap bmp = new Bitmap(bmpSize.Width, bmpSize.Height, PixelFormat.Format32bppArgb)) + using (var bmp = new Bitmap(bmpSize.Width, bmpSize.Height, PixelFormat.Format32bppArgb)) using (Graphics g = Graphics.FromImage(bmp)) { g.Clear(Color.Transparent); @@ -96,15 +96,15 @@ namespace Vocaluxe.Base.Fonts g.InterpolationMode = InterpolationMode.HighQualityBicubic; g.TextRenderingHint = TextRenderingHint.ClearTypeGridFit; - PointF point = new PointF(outlineSize / 2, outlineSize / 4); + var point = new PointF(outlineSize / 2, outlineSize / 4); - using (GraphicsPath path = new GraphicsPath()) + using (var path = new GraphicsPath()) { //Have to use size in em not pixels! float emSize = fo.Size * fo.FontFamily.GetCellAscent(fo.Style) / fo.FontFamily.GetEmHeight(fo.Style); path.AddString(chrString, fo.FontFamily, (int)fo.Style, emSize, point, new StringFormat()); - using (Pen pen = new Pen(CFonts.OutlineColor.AsColor(), outlineSize)) + using (var pen = new Pen(CFonts.OutlineColor.AsColor(), outlineSize)) { pen.LineJoin = LineJoin.Round; g.DrawPath(pen, path); @@ -123,6 +123,7 @@ namespace Vocaluxe.Base.Fonts _DrawBounding.Height = _Texture.Height;*/ #pragma warning disable 162 // ReSharper disable HeuristicUnreachableCode + // ReSharper disable ConditionIsAlwaysTrueOrFalse if (false && Char.IsLetterOrDigit(chr)) { if (outline > 0) @@ -130,6 +131,7 @@ namespace Vocaluxe.Base.Fonts else bmpCropped.Save("font_" + chr + CFonts.Style + "2.png", ImageFormat.Png); } + // ReSharper restore ConditionIsAlwaysTrueOrFalse // ReSharper restore HeuristicUnreachableCode #pragma warning restore 162 } @@ -164,7 +166,7 @@ namespace Vocaluxe.Base.Fonts int minX = 0, maxX = bmp.Width - 1, minY = 0; BitmapData bmpData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb); int values = bmpData.Width * bmp.Height; - Int32[] rgbValues = new Int32[values]; + var rgbValues = new Int32[values]; Marshal.Copy(bmpData.Scan0, rgbValues, 0, values); int index = 0; bool found = false; diff --git a/Vocaluxe/Base/Server/CClientHandler.cs b/Vocaluxe/Base/Server/CClientHandler.cs index a63939f9..b83b9544 100644 --- a/Vocaluxe/Base/Server/CClientHandler.cs +++ b/Vocaluxe/Base/Server/CClientHandler.cs @@ -21,12 +21,12 @@ namespace Vocaluxe.Base.Server { class CClientHandler { - public int ConnectionID; + private int _ConnectionID; public bool LoggedIn; public CClientHandler(int connectionID) { - ConnectionID = connectionID; + _ConnectionID = connectionID; LoggedIn = false; } } diff --git a/Vocaluxe/Base/Server/CCommands.cs b/Vocaluxe/Base/Server/CCommands.cs index aa99c89b..6c04e92c 100644 --- a/Vocaluxe/Base/Server/CCommands.cs +++ b/Vocaluxe/Base/Server/CCommands.cs @@ -8,12 +8,13 @@ namespace Vocaluxe.Base.Server { public static class CCommands { - private static UTF8Encoding encoder = new UTF8Encoding(); - public static SHA256Managed SHA256 = new SHA256Managed(); + private static readonly UTF8Encoding _Encoder = new UTF8Encoding(); + private static readonly SHA256Managed _Sha256 = new SHA256Managed(); public const string BroadcastKeyword = "I am a Vocaluxe Server"; #region Commands + // ReSharper disable UnusedMember.Global public const int ResponseOK = 1; public const int ResponseNOK = 2; @@ -41,27 +42,27 @@ namespace Vocaluxe.Base.Server public const int CommandSendAvatarPictureJpg = 501; public const int CommandSendProfile = 510; + // ReSharper restore UnusedMember.Global #endregion Commands #region General - public static byte[] CreateCommandWithoutParams(int Command) + public static byte[] CreateCommandWithoutParams(int command) { - return BitConverter.GetBytes(Command); + return BitConverter.GetBytes(command); } #endregion General #region Login - public static byte[] CreateCommandLogin(string Password) + public static byte[] CreateCommandLogin(string password) { - var data = new SLoginData(); - data.SHA256 = SHA256.ComputeHash(Encoding.UTF8.GetBytes(Password)); + var data = new SLoginData {Sha256 = _Sha256.ComputeHash(Encoding.UTF8.GetBytes(password))}; - return Serialize(CommandLogin, data); + return _Serialize(CommandLogin, data); } - public static bool DecodeCommandLogin(byte[] Message, out SLoginData LoginData) + public static bool DecodeCommandLogin(byte[] message, out SLoginData loginData) { - return TryDeserialize(Message, out LoginData); + return _TryDeserialize(message, out loginData); } #endregion Login @@ -69,74 +70,64 @@ namespace Vocaluxe.Base.Server #endregion Keyboard #region Profiles - public static byte[] CreateCommandSendAvatarPicture(int Width, int Height, byte[] data) + public static byte[] CreateCommandSendAvatarPicture(int width, int height, byte[] data) { - var ap = new SAvatarPicture(); - ap.Height = Height; - ap.Width = Width; - ap.data = new byte[data.Length]; - Array.Copy(data, ap.data, data.Length); + var ap = new SAvatarPicture {Height = height, Width = width, Data = new byte[data.Length]}; + Array.Copy(data, ap.Data, data.Length); - return Serialize(CommandSendAvatarPicture, ap); + return _Serialize(CommandSendAvatarPicture, ap); } - public static bool DecodeCommandSendAvatarPicture(byte[] Message, out SAvatarPicture AvatarPicture) + public static bool DecodeCommandSendAvatarPicture(byte[] message, out SAvatarPicture avatarPicture) { - return TryDeserialize(Message, out AvatarPicture); + return _TryDeserialize(message, out avatarPicture); } - public static byte[] CreateCommandSendAvatarPictureJpg(byte[] AvatarJpgData) + public static byte[] CreateCommandSendAvatarPictureJpg(byte[] avatarJpgData) { - var ap = new SAvatarPicture(); - ap.Height = 0; - ap.Width = 0; - ap.data = new byte[AvatarJpgData.Length]; - Array.Copy(AvatarJpgData, ap.data, AvatarJpgData.Length); + var ap = new SAvatarPicture {Height = 0, Width = 0, Data = new byte[avatarJpgData.Length]}; + Array.Copy(avatarJpgData, ap.Data, avatarJpgData.Length); - return Serialize(CommandSendAvatarPictureJpg, ap); + return _Serialize(CommandSendAvatarPictureJpg, ap); } - public static byte[] CreateCommandSendProfile(byte[] AvatarJpgData, string PlayerName, int Difficulty) + public static byte[] CreateCommandSendProfile(byte[] avatarJpgData, string playerName, int difficulty) { - var ap = new SAvatarPicture(); - ap.Height = 0; - ap.Width = 0; - ap.data = new byte[AvatarJpgData.Length]; - Array.Copy(AvatarJpgData, ap.data, AvatarJpgData.Length); + var ap = new SAvatarPicture {Height = 0, Width = 0, Data = new byte[avatarJpgData.Length]}; + Array.Copy(avatarJpgData, ap.Data, avatarJpgData.Length); - var profile = new SProfile(); - profile.Avatar = ap; - profile.PlayerName = PlayerName; - profile.Difficulty = Difficulty; + var profile = new SProfile {Avatar = ap, PlayerName = playerName, Difficulty = difficulty}; - return Serialize(CommandSendProfile, profile); + return _Serialize(CommandSendProfile, profile); } - public static bool DecodeCommandSendProfile(byte[] Message, out SProfile Profile) + public static bool DecodeCommandSendProfile(byte[] message, out SProfile profile) { - return TryDeserialize(Message, out Profile); + return _TryDeserialize(message, out profile); } #endregion Profiles #region Serializing - private static byte[] Serialize(int Command, T obj) + private static byte[] _Serialize(T obj) { - byte[] command = BitConverter.GetBytes(Command); + return _Serialize(0, obj); + } + + private static byte[] _Serialize(int command, T obj) + { + byte[] cmd = BitConverter.GetBytes(command); var stream = new MemoryStream(); - stream.Write(command, 0, command.Length); + stream.Write(cmd, 0, cmd.Length); byte[] data; - using (var ms = new MemoryStream()) - { - string json = JsonConvert.SerializeObject(obj); - data = encoder.GetBytes(json); - } + string json = JsonConvert.SerializeObject(obj); + data = _Encoder.GetBytes(json); stream.Write(data, 0, data.Length); return stream.ToArray(); } - private static bool TryDeserialize(byte[] message, out T obj) + private static bool _TryDeserialize(byte[] message, out T obj) { obj = default(T); @@ -148,17 +139,14 @@ namespace Vocaluxe.Base.Server var data = new byte[message.Length - 4]; Array.Copy(message, 4, data, 0, data.Length); - using (var ms = new MemoryStream(data)) + try { - try - { - obj = JsonConvert.DeserializeObject(encoder.GetString(data)); - return true; - } - catch - { - return false; - } + obj = JsonConvert.DeserializeObject(_Encoder.GetString(data)); + return true; + } + catch + { + return false; } } #endregion Serializing diff --git a/Vocaluxe/Base/Server/CVocaluxeServer.cs b/Vocaluxe/Base/Server/CVocaluxeServer.cs index 2b70775c..1c179fca 100644 --- a/Vocaluxe/Base/Server/CVocaluxeServer.cs +++ b/Vocaluxe/Base/Server/CVocaluxeServer.cs @@ -42,10 +42,8 @@ namespace Vocaluxe.Base.Server public static void Init() { _Clients = new Dictionary(); - if (CConfig.ServerEncryption == EOffOn.TR_CONFIG_ON) - _Server = new CServer(RequestHandler, CConfig.ServerPort, CConfig.ServerPassword); - else - _Server = new CServer(RequestHandler, CConfig.ServerPort, String.Empty); + String pw = (CConfig.ServerEncryption == EOffOn.TR_CONFIG_ON) ? CConfig.ServerPassword : String.Empty; + _Server = new CServer(RequestHandler, CConfig.ServerPort, pw); _Discover = new CDiscover(CConfig.ServerPort, CCommands.BroadcastKeyword); Controller.Init(); @@ -89,6 +87,9 @@ namespace Vocaluxe.Base.Server loggedIn = _Clients[connectionID].LoggedIn; } + if (!loggedIn) + return CCommands.CreateCommandWithoutParams(CCommands.ResponseNOK); + int command = BitConverter.ToInt32(message, 0); switch (command) @@ -121,7 +122,7 @@ namespace Vocaluxe.Base.Server using (var bmp = new Bitmap(avatarPicture.Width, avatarPicture.Height)) { BitmapData bmpData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb); - Marshal.Copy(avatarPicture.data, 0, bmpData.Scan0, avatarPicture.data.Length); + Marshal.Copy(avatarPicture.Data, 0, bmpData.Scan0, avatarPicture.Data.Length); bmp.UnlockBits(bmpData); const string filename = "snapshot"; @@ -140,7 +141,7 @@ namespace Vocaluxe.Base.Server SAvatarPicture avatarPictureJpg; if (CCommands.DecodeCommandSendAvatarPicture(message, out avatarPictureJpg)) { - if (_AddAvatar(avatarPictureJpg.data) != String.Empty) + if (_AddAvatar(avatarPictureJpg.Data) != String.Empty) answer = CCommands.CreateCommandWithoutParams(CCommands.ResponseOK); } break; @@ -151,7 +152,7 @@ namespace Vocaluxe.Base.Server { try { - string avatarFilename = _AddAvatar(profile.Avatar.data); + string avatarFilename = _AddAvatar(profile.Avatar.Data); if (avatarFilename != String.Empty) { var p = new CProfile diff --git a/Vocaluxe/Base/Server/VocaluxeStructs.cs b/Vocaluxe/Base/Server/VocaluxeStructs.cs index faf0dfa6..ae5fd5cf 100644 --- a/Vocaluxe/Base/Server/VocaluxeStructs.cs +++ b/Vocaluxe/Base/Server/VocaluxeStructs.cs @@ -2,14 +2,14 @@ { public struct SLoginData { - public byte[] SHA256; + public byte[] Sha256; } public struct SAvatarPicture { public int Width; public int Height; - public byte[] data; + public byte[] Data; } public struct SProfile diff --git a/Vocaluxe/Screens/CScreenProfiles.cs b/Vocaluxe/Screens/CScreenProfiles.cs index 08e37d59..a5991bd3 100644 --- a/Vocaluxe/Screens/CScreenProfiles.cs +++ b/Vocaluxe/Screens/CScreenProfiles.cs @@ -446,13 +446,16 @@ namespace Vocaluxe.Screens int selectedAvatarID = _SelectSlides[_SelectSlideAvatars].ValueIndex; _SelectSlides[_SelectSlideAvatars].Clear(); IEnumerable avatars = CProfiles.GetAvatars(); - foreach (CAvatar avatar in avatars) + if (avatars != null) { - _SelectSlides[_SelectSlideAvatars].AddValue( - Path.GetFileName(avatar.FileName), - null, - avatar.ID, - -1); + foreach (CAvatar avatar in avatars) + { + _SelectSlides[_SelectSlideAvatars].AddValue( + Path.GetFileName(avatar.FileName), + null, + avatar.ID, + -1); + } } if (keep) diff --git a/Vocaluxe/Screens/CScreenScore.cs b/Vocaluxe/Screens/CScreenScore.cs index 759a70f9..c697207d 100644 --- a/Vocaluxe/Screens/CScreenScore.cs +++ b/Vocaluxe/Screens/CScreenScore.cs @@ -123,6 +123,8 @@ namespace Vocaluxe.Screens _Round = CGame.NumRounds > 1 ? 0 : 1; _Points = CGame.GetPoints(); + _SavePlayedSongs(); + _SetVisibility(); _UpdateRatings(); } @@ -389,6 +391,25 @@ namespace Vocaluxe.Screens } } + private void _SavePlayedSongs() + { + for (int round = 0; round < _Points.NumRounds; round++) + { + SPlayer[] players = _Points.GetPlayer(round, CGame.NumPlayer); + + for (int p = 0; p < players.Length; p++) + { + if (players[p].Points > CSettings.MinScoreForDB && players[p].SongFinished) + { + CSong song = CSongs.GetSong(players[p].SongID); + CDataBase.IncreaseSongCounter(song.DataBaseSongID); + song.NumPlayed++; + break; + } + } + } + } + private void _LeaveScreen() { CParty.LeavingScore(); diff --git a/Vocaluxe/Screens/CScreenSing.cs b/Vocaluxe/Screens/CScreenSing.cs index 94dfa613..fcd8d78c 100644 --- a/Vocaluxe/Screens/CScreenSing.cs +++ b/Vocaluxe/Screens/CScreenSing.cs @@ -546,12 +546,14 @@ namespace Vocaluxe.Screens public override void ApplyVolume() { - CSound.SetStreamVolumeMax(_CurrentStream, CConfig.GameMusicVolume); + if(_CurrentStream > -1) + CSound.SetStreamVolumeMax(_CurrentStream, CConfig.GameMusicVolume); } private void _CloseSong() { - CSound.FadeAndStop(_CurrentStream, 0f, 0.5f); + if(_CurrentStream > -1) + CSound.FadeAndStop(_CurrentStream, 0f, 0.5f); CSound.RecordStop(); if (_CurrentVideo != -1) { diff --git a/VocaluxeLib/IBasic.cs b/VocaluxeLib/IBasic.cs index 2c85ba9d..afbbf8d4 100644 --- a/VocaluxeLib/IBasic.cs +++ b/VocaluxeLib/IBasic.cs @@ -262,6 +262,7 @@ namespace VocaluxeLib public interface IDataBase { bool GetCover(string fileName, ref CTexture texture, int coverSize); + bool GetDataBaseSongInfos(string artist, string title, out int numPlayed, out string dateAdded, out int highscoreID); } public interface IControllers diff --git a/VocaluxeLib/PartyModes/CPartyMode.cs b/VocaluxeLib/PartyModes/CPartyMode.cs index a8f31d23..79590760 100644 --- a/VocaluxeLib/PartyModes/CPartyMode.cs +++ b/VocaluxeLib/PartyModes/CPartyMode.cs @@ -25,7 +25,7 @@ namespace VocaluxeLib.PartyModes { public abstract class CPartyMode : IPartyMode { - protected string _Folder; + private string _Folder; protected SScreenSongOptions _ScreenSongOptions; protected readonly Dictionary _Screens; diff --git a/VocaluxeLib/Songs/CSong.cs b/VocaluxeLib/Songs/CSong.cs index 7b76f94b..9da713e4 100644 --- a/VocaluxeLib/Songs/CSong.cs +++ b/VocaluxeLib/Songs/CSong.cs @@ -147,6 +147,10 @@ namespace VocaluxeLib.Songs public readonly List Language = new List(); + public int DataBaseSongID = -1; + public string DateAdded = ""; + public int NumPlayed = 0; + // Notes public readonly CNotes Notes = new CNotes(); @@ -240,6 +244,10 @@ namespace VocaluxeLib.Songs foreach (string value in song.Language) Language.Add(value); + DataBaseSongID = song.DataBaseSongID; + DateAdded = song.DateAdded; + NumPlayed = song.NumPlayed; + Notes = new CNotes(song.Notes); } @@ -353,10 +361,12 @@ namespace VocaluxeLib.Songs if (value != "") ArtistSorting = value; break; + case "DUETSINGERP1": case "P1": if (value != "") DuetPart1 = value; break; + case "DUETSINGERP2": case "P2": if (value != "") DuetPart2 = value; @@ -528,6 +538,8 @@ namespace VocaluxeLib.Songs sr.Dispose(); _CheckFiles(); + CBase.DataBase.GetDataBaseSongInfos(Artist, Title, out NumPlayed, out DateAdded, out DataBaseSongID); + //Before saving this tags to .txt: Check, if ArtistSorting and Artist are equal, then don't save this tag. if (ArtistSorting == "") ArtistSorting = Artist; diff --git a/VocaluxeLib/Songs/CSongNote.cs b/VocaluxeLib/Songs/CSongNote.cs index 7619cbfd..d62dfeec 100644 --- a/VocaluxeLib/Songs/CSongNote.cs +++ b/VocaluxeLib/Songs/CSongNote.cs @@ -12,16 +12,11 @@ Text = note.Text; } - public CSongNote(int startBeat, int duration, int tone, string text) : base(startBeat, duration, tone) - { - NoteType = ENoteType.Normal; - Text = text; - } - public CSongNote(int startBeat, int duration, int tone, string text, ENoteType noteType) - : this(startBeat, duration, tone, text) + : base(startBeat, duration, tone) { NoteType = noteType; + Text = text; } #endregion Constructors diff --git a/VocaluxeLib/Songs/CSungNote.cs b/VocaluxeLib/Songs/CSungNote.cs index 976910bd..eb0aa49a 100644 --- a/VocaluxeLib/Songs/CSungNote.cs +++ b/VocaluxeLib/Songs/CSungNote.cs @@ -35,7 +35,7 @@ { get { return HitNote != null; } } - public CSongNote HitNote { get; set; } + public CSongNote HitNote { get; private set; } // for drawing perfect note effect public bool Perfect { get; set; } #endregion diff --git a/VocaluxeLib/Songs/CVoice.cs b/VocaluxeLib/Songs/CVoice.cs index c833d679..2b8ae897 100644 --- a/VocaluxeLib/Songs/CVoice.cs +++ b/VocaluxeLib/Songs/CVoice.cs @@ -34,10 +34,7 @@ namespace VocaluxeLib.Songs if (_Lines.Count == 0) return 0; - int startbeat = _Lines.Max(line => line.FirstNoteBeat); - int endbeat = _Lines.Min(line => line.LastNoteBeat); - - int result = endbeat - startbeat; + int result = _Lines[_Lines.Count - 1].EndBeat - _Lines[0].StartBeat; return result > 0 ? result : 0; } } @@ -111,7 +108,7 @@ namespace VocaluxeLib.Songs if (lineIndex < 0) { //Note is before ALL lines - CSongLine line = new CSongLine {StartBeat = note.StartBeat}; + var line = new CSongLine {StartBeat = note.StartBeat}; line.AddNote(note); _Lines.Insert(0, line); }