(Hopefully) fixed wrong merging - Don't know what happend...

This commit is contained in:
Florian Kuepper committed 2012-03-03 23:28:08 +01:00
1 parent c94d296462
commit e4020188e9
1 file changed
+337 -110
+337 -110
View File
@@ -38,22 +38,48 @@ namespace Vocaluxe.Base
InitHighscoreDB();
InitCoverDB();
InitCreditsRessourcesDB();
/**
AddImageToCreditsDB("C:\\Users\\Florian\\Desktop\\Credit-Ressources\\brunzel.png");
AddImageToCreditsDB("C:\\Users\\Florian\\Desktop\\Credit-Ressources\\bohning.png");
AddImageToCreditsDB("C:\\Users\\Florian\\Desktop\\Credit-Ressources\\babene03.png");
AddImageToCreditsDB("C:\\Users\\Florian\\Desktop\\Credit-Ressources\\Darkice.png");
AddImageToCreditsDB("C:\\Users\\Florian\\Desktop\\Credit-Ressources\\flokuep.png");
AddImageToCreditsDB("C:\\Users\\Florian\\Desktop\\Credit-Ressources\\mesand.png");
*/
}
#region Highscores
public static int AddScore(string PlayerName, int Score, int LineNr, long Date, int Medley, int Duet, int Diff,
string Artist, string Title, int NumPlayed, string FilePath)
{
SPlayer player = new SPlayer();
player.Name = PlayerName;
player.Points = Score;
player.LineNr = LineNr;
player.DateTicks = Date;
player.Medley = (Medley == 1);
player.Duet = (Duet == 1);
player.Difficulty = (EGameDifficulty)Diff;
SQLiteConnection connection = new SQLiteConnection();
connection.ConnectionString = "Data Source=" + FilePath;
SQLiteCommand command;
try
{
connection.Open();
}
catch (Exception)
{
return -1;
}
command = new SQLiteCommand(connection);
int DataBaseSongID = GetDataBaseSongID(Artist, Title, NumPlayed, command);
int result = AddScore(player, command, DataBaseSongID);
command.Dispose();
connection.Close();
connection.Dispose();
return result;
}
public static int AddScore(SPlayer player)
{
int lastInsertID = -1;
SQLiteConnection connection = new SQLiteConnection();
connection.ConnectionString = "Data Source=" + _HighscoreFilePath;
SQLiteCommand command;
@@ -70,6 +96,18 @@ namespace Vocaluxe.Base
command = new SQLiteCommand(connection);
int DataBaseSongID = GetDataBaseSongID(player, command);
int result = AddScore(player, command, DataBaseSongID);
connection.Close();
connection.Dispose();
return result;
}
private static int AddScore(SPlayer player, SQLiteCommand command, int DataBaseSongID)
{
int lastInsertID = -1;
if (DataBaseSongID >= 0)
{
@@ -80,7 +118,38 @@ namespace Vocaluxe.Base
int Duet = 0;
if (player.Duet)
Duet = 1;
command.CommandText = "SELECT id FROM Scores WHERE SongID = @SongID AND PlayerName = @PlayerName AND Score = @Score AND " +
"LineNr = @LineNr AND Date = @Date AND Medley = @Medley AND Duet = @Duet AND Difficulty = @Difficulty";
command.Parameters.Add("@SongID", System.Data.DbType.Int32, 0).Value = DataBaseSongID;
command.Parameters.Add("@PlayerName", System.Data.DbType.String, 0).Value = player.Name;
command.Parameters.Add("@Score", System.Data.DbType.Int32, 0).Value = (int)Math.Round(player.Points);
command.Parameters.Add("@LineNr", System.Data.DbType.Int32, 0).Value = (int)player.LineNr;
command.Parameters.Add("@Date", System.Data.DbType.Int64, 0).Value = player.DateTicks;
command.Parameters.Add("@Medley", System.Data.DbType.Int32, 0).Value = Medley;
command.Parameters.Add("@Duet", System.Data.DbType.Int32, 0).Value = Duet;
command.Parameters.Add("@Difficulty", System.Data.DbType.Int32, 0).Value = (int)player.Difficulty;
SQLiteDataReader reader = null;
try
{
reader = command.ExecuteReader();
}
catch (Exception)
{
;
}
if (reader != null && reader.HasRows)
{
if (reader.Read())
return reader.GetInt32(0);
}
if (reader != null)
reader.Close();
command.CommandText = "INSERT INTO Scores (SongID, PlayerName, Score, LineNr, Date, Medley, Duet, Difficulty) " +
"VALUES (@SongID, @PlayerName, @Score, @LineNr, @Date, @Medley, @Duet, @Difficulty)";
command.Parameters.Add("@SongID", System.Data.DbType.Int32, 0).Value = DataBaseSongID;
@@ -96,7 +165,7 @@ namespace Vocaluxe.Base
//Read last insert line
command.CommandText = "SELECT id FROM Scores ORDER BY Date DESC LIMIT 0, 1";
SQLiteDataReader reader = null;
reader = null;
try
{
reader = command.ExecuteReader();
@@ -118,10 +187,6 @@ namespace Vocaluxe.Base
}
}
command.Dispose();
connection.Close();
connection.Dispose();
return lastInsertID;
}
@@ -132,7 +197,7 @@ namespace Vocaluxe.Base
SQLiteCommand command;
Score = new List<SScores>();
try
{
connection.Open();
@@ -161,7 +226,7 @@ namespace Vocaluxe.Base
command.Parameters.Add("@SongID", System.Data.DbType.Int32, 0).Value = DataBaseSongID;
command.Parameters.Add("@Medley", System.Data.DbType.Int32, 0).Value = Medley;
command.Parameters.Add("@Duet", System.Data.DbType.Int32, 0).Value = Duet;
SQLiteDataReader reader = null;
try
{
@@ -186,13 +251,13 @@ namespace Vocaluxe.Base
Score.Add(score);
}
reader.Close();
reader.Dispose();
reader.Dispose();
}
}
command.Dispose();
connection.Close();
@@ -206,9 +271,38 @@ namespace Vocaluxe.Base
if (song == null)
return -1;
return GetDataBaseSongID(song.Artist, song.Title, 0, command);
}
private static int GetDataBaseSongID(string Artist, string Title, string FilePath, int DefNumPlayed)
{
SQLiteConnection connection = new SQLiteConnection();
connection.ConnectionString = "Data Source=" + FilePath;
SQLiteCommand command;
try
{
connection.Open();
}
catch (Exception)
{
return -1;
}
command = new SQLiteCommand(connection);
int id = GetDataBaseSongID(Artist, Title, DefNumPlayed, command);
command.Dispose();
connection.Close();
connection.Dispose();
return id;
}
private static int GetDataBaseSongID(string Artist, string Title, int DefNumPlayed, SQLiteCommand command)
{
command.CommandText = "SELECT id FROM Songs WHERE [Title] = @title AND [Artist] = @artist";
command.Parameters.Add("@title", System.Data.DbType.String, 0).Value = song.Title;
command.Parameters.Add("@artist", System.Data.DbType.String, 0).Value = song.Artist;
command.Parameters.Add("@title", System.Data.DbType.String, 0).Value = Title;
command.Parameters.Add("@artist", System.Data.DbType.String, 0).Value = Artist;
SQLiteDataReader reader = null;
try
@@ -234,14 +328,15 @@ namespace Vocaluxe.Base
reader.Close();
command.CommandText = "INSERT INTO Songs (Title, Artist, NumPlayed) " +
"VALUES (@title, @artist, 0)";
command.Parameters.Add("@title", System.Data.DbType.String, 0).Value = song.Title;
command.Parameters.Add("@artist", System.Data.DbType.String, 0).Value = song.Artist;
"VALUES (@title, @artist, @numplayed)";
command.Parameters.Add("@title", System.Data.DbType.String, 0).Value = Title;
command.Parameters.Add("@artist", System.Data.DbType.String, 0).Value = Artist;
command.Parameters.Add("@numplayed", System.Data.DbType.Int32, 0).Value = DefNumPlayed;
command.ExecuteNonQuery();
command.CommandText = "SELECT id FROM Songs WHERE [Title] = @title AND [Artist] = @artist";
command.Parameters.Add("@title", System.Data.DbType.String, 0).Value = song.Title;
command.Parameters.Add("@artist", System.Data.DbType.String, 0).Value = song.Artist;
command.Parameters.Add("@title", System.Data.DbType.String, 0).Value = Title;
command.Parameters.Add("@artist", System.Data.DbType.String, 0).Value = Artist;
reader = null;
try
@@ -268,28 +363,140 @@ namespace Vocaluxe.Base
reader.Close();
reader.Dispose();
}
return -1;
}
private static bool InitHighscoreDB()
private static bool GetDataBaseSongInfos(int SongID, out string Artist, out string Title, out int NumPlayed, string FilePath)
{
Artist = String.Empty;
Title = String.Empty;
NumPlayed = 0;
SQLiteConnection connection = new SQLiteConnection();
connection.ConnectionString = "Data Source=" + FilePath;
SQLiteCommand command;
try
{
connection.Open();
}
catch (Exception)
{
return false;
}
command = new SQLiteCommand(connection);
command.CommandText = "SELECT Artist, Title, NumPlayed FROM Songs WHERE [id] = @id";
command.Parameters.Add("@id", System.Data.DbType.String, 0).Value = SongID;
SQLiteDataReader reader = null;
try
{
reader = command.ExecuteReader();
}
catch (Exception)
{
return false;
}
if (reader != null && reader.HasRows)
{
reader.Read();
Artist = reader.GetString(0);
Title = reader.GetString(1);
NumPlayed = reader.GetInt32(2);
reader.Close();
reader.Dispose();
command.Dispose();
connection.Close();
connection.Dispose();
return true;
}
else
{
if (reader != null)
reader.Close();
}
command.Dispose();
connection.Close();
connection.Dispose();
return false;
}
private static void InitHighscoreDB()
{
string OldDBFilePath = Path.Combine(Environment.CurrentDirectory, CSettings.sFileOldHighscoreDB);
if (File.Exists(OldDBFilePath))
{
if (File.Exists(_HighscoreFilePath))
{
CLog.LogError("Found old Highscore DB (" + OldDBFilePath + "). If you want to import it just delete the new one (" + _HighscoreFilePath + ")");
CreateOrConvert(OldDBFilePath);
CreateOrConvert(_HighscoreFilePath);
ImportData(OldDBFilePath, _HighscoreFilePath);
File.Delete(OldDBFilePath);
}
else
{
File.Copy(OldDBFilePath, _HighscoreFilePath);
File.Move(OldDBFilePath, OldDBFilePath + ".old");
CreateOrConvert(_HighscoreFilePath);
File.Delete(OldDBFilePath);
}
}
else
CreateOrConvert(_HighscoreFilePath);
}
private static void CreateHighscoreDB(string FilePath)
{
SQLiteConnection connection = new SQLiteConnection();
connection.ConnectionString = "Data Source=" + _HighscoreFilePath;
connection.ConnectionString = "Data Source=" + FilePath;
SQLiteCommand command;
try
{
connection.Open();
}
catch (Exception)
{
return;
}
command = new SQLiteCommand(connection);
command.CommandText = "CREATE TABLE IF NOT EXISTS Version ( id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, Value INTEGER NOT NULL);";
command.ExecuteNonQuery();
command.CommandText = "INSERT INTO Version (id, Value) VALUES(NULL, " + CSettings.iDatabaseHighscoreVersion.ToString() + ")";
command.ExecuteNonQuery();
command.CommandText = "CREATE TABLE IF NOT EXISTS Songs ( id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, " +
"Artist TEXT NOT NULL, Title TEXT NOT NULL, NumPlayed INTEGER);";
command.ExecuteNonQuery();
command.CommandText = "CREATE TABLE IF NOT EXISTS Scores ( id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, " +
"SongID INTEGER NOT NULL, PlayerName TEXT NOT NULL, Score INTEGER NOT NULL, LineNr INTEGER NOT NULL, Date BIGINT NOT NULL, " +
"Medley INTEGER NOT NULL, Duet INTEGER NOT NULL, Difficulty INTEGER NOT NULL);";
command.ExecuteNonQuery();
command.Dispose();
connection.Close();
connection.Dispose();
}
/// <summary>
/// Creates a new Vocaluxe Database if no file exists. Converts an existing old Ultrastar Deluxe highscore database into vocaluxe format.
/// </summary>
/// <param name="FilePath">Database file path</param>
/// <returns></returns>
private static bool CreateOrConvert(string FilePath)
{
SQLiteConnection connection = new SQLiteConnection();
connection.ConnectionString = "Data Source=" + FilePath;
SQLiteCommand command;
try
@@ -318,12 +525,12 @@ namespace Vocaluxe.Base
if (reader == null)
{
// create new database/tables
CreateHighscoreDB();
CreateHighscoreDB(FilePath);
}
else if (reader.FieldCount == 0)
{
// create new database/tables
CreateHighscoreDB();
CreateHighscoreDB(FilePath);
}
else
{
@@ -361,10 +568,10 @@ namespace Vocaluxe.Base
//Check for USDX 1.1 DB
if (version == 1)
ConvertFrom110();
ConvertFrom110(FilePath);
//Check for USDX 1.01 or CMD Mod DB
else if (version == 0 && scoresTableExists)
ConvertFrom101();
ConvertFrom101(FilePath);
command.Dispose();
@@ -374,61 +581,15 @@ namespace Vocaluxe.Base
return true;
}
private static void CreateHighscoreDB()
{
SQLiteConnection connection = new SQLiteConnection();
connection.ConnectionString = "Data Source=" + _HighscoreFilePath;
SQLiteCommand command;
try
{
connection.Open();
}
catch (Exception)
{
return;
}
command = new SQLiteCommand(connection);
command.CommandText = "CREATE TABLE IF NOT EXISTS Version ( id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, Value INTEGER NOT NULL);";
command.ExecuteNonQuery();
command.CommandText = "INSERT INTO Version (id, Value) VALUES(NULL, " + CSettings.iDatabaseHighscoreVersion.ToString() + ")";
command.ExecuteNonQuery();
command.CommandText = "CREATE TABLE IF NOT EXISTS Songs ( id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, " +
"Artist TEXT NOT NULL, Title TEXT NOT NULL, NumPlayed INTEGER);";
command.ExecuteNonQuery();
command.CommandText = "CREATE TABLE IF NOT EXISTS Scores ( id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, " +
"SongID INTEGER NOT NULL, PlayerName TEXT NOT NULL, Score INTEGER NOT NULL, LineNr INTEGER NOT NULL, Date BIGINT NOT NULL, " +
"Medley INTEGER NOT NULL, Duet INTEGER NOT NULL, Difficulty INTEGER NOT NULL);";
command.ExecuteNonQuery();
command.Dispose();
connection.Close();
connection.Dispose();
/*
// Auslesen des zuletzt eingefügten Datensatzes.
command.CommandText = "SELECT id, name FROM beispiel ORDER BY id DESC LIMIT 0, 1";
while (reader.Read())
{
Console.WriteLine("Dies ist der {0}. eingefügte Datensatz mit dem Wert: \"{1}\"", reader[0].ToString(), reader[1].ToString());
}
*/
}
/// <summary>
/// Converts a USDX 1.1 database into the Vocaluxe format
/// </summary>
/// <param name="FilePath">Database file path</param>
/// <returns>True if succeeded</returns>
private static bool ConvertFrom110()
private static bool ConvertFrom110(string FilePath)
{
SQLiteConnection connection = new SQLiteConnection();
connection.ConnectionString = "Data Source=" + _HighscoreFilePath;
connection.ConnectionString = "Data Source=" + FilePath;
SQLiteCommand command;
try
@@ -521,7 +682,7 @@ namespace Vocaluxe.Base
command.Parameters.Add("@id", System.Data.DbType.Int32, 0).Value = data.id;
command.ExecuteNonQuery();
}
//Delete old tables after conversion
command.CommandText = "DROP TABLE US_Scores;";
command.ExecuteNonQuery();
@@ -546,11 +707,12 @@ namespace Vocaluxe.Base
/// <summary>
/// Converts a USDX 1.01 or CMD 1.01 database to Vocaluxe format
/// </summary>
/// <param name="FilePath">Database file path</param>
/// <returns>True if succeeded</returns>
private static bool ConvertFrom101()
private static bool ConvertFrom101(string FilePath)
{
SQLiteConnection connection = new SQLiteConnection();
connection.ConnectionString = "Data Source=" + _HighscoreFilePath;
connection.ConnectionString = "Data Source=" + FilePath;
SQLiteCommand command;
SQLiteDataReader reader = null;
@@ -567,7 +729,7 @@ namespace Vocaluxe.Base
command.CommandText = "PRAGMA table_info(US_Scores);";
reader = command.ExecuteReader();
bool dateExists = false;
@@ -584,12 +746,12 @@ namespace Vocaluxe.Base
}
}
}
reader.Close();
//This is a USDX 1.01 DB
if(!dateExists)
if (!dateExists)
command.CommandText = "INSERT INTO Scores (SongID, PlayerName, Score, LineNr, Date, Medley, Duet, Difficulty) SELECT SongID, Player, Score, '0', '0', '0', '0', Difficulty from US_Scores";
else // This is a CMD 1.01 DB
command.CommandText = "INSERT INTO Scores (SongID, PlayerName, Score, LineNr, Date, Medley, Duet, Difficulty) SELECT SongID, Player, Score, '0', Date, '0', '0', Difficulty from US_Scores";
@@ -603,11 +765,11 @@ namespace Vocaluxe.Base
List<SData> songs = new List<SData>();
Sqlite3.sqlite3 OldDB;
int res = Sqlite3.sqlite3_open(_HighscoreFilePath, out OldDB);
int res = Sqlite3.sqlite3_open(FilePath, out OldDB);
if (res != Sqlite3.SQLITE_OK)
{
CLog.LogError("Error opening Database: " + _HighscoreFilePath + " (" + Sqlite3.sqlite3_errmsg(OldDB) + ")");
CLog.LogError("Error opening Database: " + FilePath + " (" + Sqlite3.sqlite3_errmsg(OldDB) + ")");
}
else
{
@@ -616,11 +778,11 @@ namespace Vocaluxe.Base
if (res != Sqlite3.SQLITE_OK)
{
CLog.LogError("Error query Database: " + _HighscoreFilePath + " (" + Sqlite3.sqlite3_errmsg(OldDB) + ")");
CLog.LogError("Error query Database: " + FilePath + " (" + Sqlite3.sqlite3_errmsg(OldDB) + ")");
}
else
{
Sqlite3.sqlite3_step(Stmt);
//Sqlite3.sqlite3_step(Stmt);
Encoding UTF8 = Encoding.UTF8;
Encoding CP1252 = Encoding.GetEncoding(1252);
@@ -631,9 +793,9 @@ namespace Vocaluxe.Base
data.id = Sqlite3.sqlite3_column_int(Stmt, 0);
byte[] bytes = Sqlite3.sqlite3_column_rawbytes(Stmt, 1);
byte[] bytes = Sqlite3.sqlite3_column_rawbytes(Stmt, 1);
data.str1 = UTF8.GetString(Encoding.Convert(CP1252, UTF8, bytes));
bytes = Sqlite3.sqlite3_column_rawbytes(Stmt, 2);
data.str2 = UTF8.GetString(Encoding.Convert(CP1252, UTF8, bytes));
@@ -643,7 +805,7 @@ namespace Vocaluxe.Base
}
Stmt = new Sqlite3.Vdbe();
if (!dateExists)
res = Sqlite3.sqlite3_prepare_v2(OldDB, "SELECT rowid, Player FROM US_Scores", -1, ref Stmt, 0);
else
@@ -651,11 +813,11 @@ namespace Vocaluxe.Base
if (res != Sqlite3.SQLITE_OK)
{
CLog.LogError("Error query Database: " + _HighscoreFilePath + " (" + Sqlite3.sqlite3_errmsg(OldDB) + ")");
CLog.LogError("Error query Database: " + FilePath + " (" + Sqlite3.sqlite3_errmsg(OldDB) + ")");
}
else
{
Sqlite3.sqlite3_step(Stmt);
//Sqlite3.sqlite3_step(Stmt);
Encoding UTF8 = Encoding.UTF8;
Encoding CP1252 = Encoding.GetEncoding(1252);
@@ -671,7 +833,7 @@ namespace Vocaluxe.Base
if (dateExists)
data.ticks = UnixTimeToTicks(Sqlite3.sqlite3_column_int(Stmt, 2));
scores.Add(data);
}
Sqlite3.sqlite3_finalize(Stmt);
@@ -704,14 +866,14 @@ namespace Vocaluxe.Base
command.Parameters.Add("@id", System.Data.DbType.Int32, 0).Value = data.id;
command.ExecuteNonQuery();
}
//Delete old tables after conversion
command.CommandText = "DROP TABLE US_Scores;";
command.ExecuteNonQuery();
command.CommandText = "DROP TABLE US_Songs;";
command.ExecuteNonQuery();
reader.Dispose();
command.Dispose();
connection.Close();
@@ -719,6 +881,71 @@ namespace Vocaluxe.Base
return true;
}
private static bool ImportData(string SourceDBPath, string DestinationDBPath)
{
#region open db
SQLiteConnection connSource = new SQLiteConnection();
connSource.ConnectionString = "Data Source=" + SourceDBPath;
try
{
connSource.Open();
}
catch (Exception e)
{
CLog.LogError("Error on import high score data. Can't open source database \"" + SourceDBPath + "\" (" + e.Message + ")");
return false;
}
#endregion open db
SQLiteCommand cmdSource = new SQLiteCommand(connSource);
SQLiteDataReader rSource;
#region import table scores
cmdSource.CommandText = "SELECT SongID, PlayerName, Score, LineNr, Date, Medley, Duet, Difficulty FROM Scores";
rSource = cmdSource.ExecuteReader();
if (rSource == null)
{
cmdSource.Dispose();
connSource.Close();
return false;
}
if (rSource.FieldCount == 0)
{
rSource.Close();
cmdSource.Dispose();
connSource.Close();
return true;
}
while (rSource.Read())
{
int songid = rSource.GetInt32(0);
string player = rSource.GetString(1);
int score = rSource.GetInt32(2);
int linenr = rSource.GetInt32(3);
long date = rSource.GetInt64(4);
int medley = rSource.GetInt32(5);
int duet = rSource.GetInt32(6);
int diff = rSource.GetInt32(7);
string artist, title;
int numplayed;
if (GetDataBaseSongInfos(songid, out artist, out title, out numplayed, SourceDBPath))
{
AddScore(player, score, linenr, date, medley, duet, diff, artist, title, numplayed, _HighscoreFilePath);
}
}
#endregion import table scores
rSource.Close();
cmdSource.Dispose();
connSource.Close();
return true;
}
#endregion Highscores
#region Cover
@@ -810,7 +1037,7 @@ namespace Vocaluxe.Base
return false;
}
int w = MaxSize;
int h = MaxSize;
@@ -827,7 +1054,7 @@ namespace Vocaluxe.Base
byte[] data = new byte[w * h * 4];
BitmapData bmp_data = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
Marshal.Copy(bmp_data.Scan0, data, 0, w*h*4);
Marshal.Copy(bmp_data.Scan0, data, 0, w * h * 4);
bmp.UnlockBits(bmp_data);
bmp.Dispose();
@@ -964,7 +1191,7 @@ namespace Vocaluxe.Base
command.ExecuteNonQuery();
command.CommandText = "CREATE TABLE IF NOT EXISTS CoverData ( id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, " +
"ImageID INTEGER NOT NULL, Data BLOB NOT NULL);";
"CoverID INTEGER NOT NULL, Data BLOB NOT NULL);";
command.ExecuteNonQuery();
command.Dispose();
@@ -1266,4 +1493,4 @@ namespace Vocaluxe.Base
return t70.Ticks;
}
}
}
}