From 227553360147741e44b39708ac319972a3017485 Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Fri, 15 Jun 2018 10:14:51 +1000 Subject: [PATCH] Fix last commit to work --- MCGalaxy/Blocks/Extended/MessageBlock.cs | 2 +- MCGalaxy/Commands/Chat/CmdInbox.cs | 6 ++-- MCGalaxy/Commands/Information/CmdTop.cs | 6 ++-- MCGalaxy/Database/Backends/MySQL.cs | 4 --- MCGalaxy/Database/Backends/SQLite.cs | 10 ++----- MCGalaxy/Database/Database.cs | 17 ++++++----- MCGalaxy/Database/ParameterisedQuery.cs | 3 +- MCGalaxy/Database/PlayerData.cs | 34 +++++++++++----------- MCGalaxy/Economy/Economy.DB.cs | 14 ++++----- MCGalaxy/Games/ZombieSurvival/ZSConfig.cs | 8 ++--- MCGalaxy/Games/ZombieSurvival/ZSGame.DB.cs | 8 ++--- MCGalaxy/Levels/LevelDB.cs | 14 ++++----- MCGalaxy/Player/Player.Login.cs | 4 +-- MCGalaxy/Server/Tasks/UpgradeTasks.cs | 4 +-- MCGalaxy/util/Extensions/DateExts.cs | 2 +- 15 files changed, 65 insertions(+), 71 deletions(-) diff --git a/MCGalaxy/Blocks/Extended/MessageBlock.cs b/MCGalaxy/Blocks/Extended/MessageBlock.cs index ecfbceb18..0a5996e94 100644 --- a/MCGalaxy/Blocks/Extended/MessageBlock.cs +++ b/MCGalaxy/Blocks/Extended/MessageBlock.cs @@ -22,7 +22,7 @@ using MCGalaxy.Maths; using MCGalaxy.SQL; namespace MCGalaxy.Blocks.Extended { - public static class MessageBlock { + public static class MessageBlock { public static bool Handle(Player p, ushort x, ushort y, ushort z, bool alwaysRepeat) { if (!p.level.hasMessageBlocks) return false; diff --git a/MCGalaxy/Commands/Chat/CmdInbox.cs b/MCGalaxy/Commands/Chat/CmdInbox.cs index 39283f9f5..85a2dbde3 100644 --- a/MCGalaxy/Commands/Chat/CmdInbox.cs +++ b/MCGalaxy/Commands/Chat/CmdInbox.cs @@ -29,9 +29,9 @@ namespace MCGalaxy.Commands.Chatting { class MailEntry { public string Contents, Timestamp, From; } static object ReadInbox(IDataRecord record, object arg) { MailEntry e = new MailEntry(); - e.Contents = record.GetString("Contents"); - e.Timestamp = record.GetString("TimeSent"); - e.From = record.GetString("PlayerFrom"); + e.Contents = record.GetText("Contents"); + e.Timestamp = record.GetText("TimeSent"); + e.From = record.GetText("PlayerFrom"); ((List)arg).Add(e); return arg; } diff --git a/MCGalaxy/Commands/Information/CmdTop.cs b/MCGalaxy/Commands/Information/CmdTop.cs index c7934cece..2afa7bc8c 100644 --- a/MCGalaxy/Commands/Information/CmdTop.cs +++ b/MCGalaxy/Commands/Information/CmdTop.cs @@ -53,9 +53,9 @@ namespace MCGalaxy.Commands.Info { Player.Message(p, "&a{0}:", stat.Title()); for (int i = 0; i < stats.Count; i++) { - string name = PlayerInfo.GetColoredName(p, stats[i][0]); - string value = stat.Formatter(stats[i][1]); - Player.Message(p, "{0}) {1} %S- {2}", offset + (i + 1), name, value); + string name = PlayerInfo.GetColoredName(p, stats[i][0]); + string value = stat.Formatter(stats[i][1]); + Player.Message(p, "{0}) {1} %S- {2}", offset + (i + 1), name, value); } } diff --git a/MCGalaxy/Database/Backends/MySQL.cs b/MCGalaxy/Database/Backends/MySQL.cs index d5fd83f7c..e99657477 100644 --- a/MCGalaxy/Database/Backends/MySQL.cs +++ b/MCGalaxy/Database/Backends/MySQL.cs @@ -204,10 +204,6 @@ namespace MCGalaxy.SQL { return new MySqlCommand(query, (MySqlConnection)conn); } - protected override DbDataAdapter CreateDataAdapter(string query, IDbConnection conn) { - return new MySqlDataAdapter(query, (MySqlConnection)conn); - } - protected override IDbDataParameter CreateParameter() { return new MySqlParameter(); } diff --git a/MCGalaxy/Database/Backends/SQLite.cs b/MCGalaxy/Database/Backends/SQLite.cs index 573b781eb..9d21bdca0 100644 --- a/MCGalaxy/Database/Backends/SQLite.cs +++ b/MCGalaxy/Database/Backends/SQLite.cs @@ -68,8 +68,8 @@ namespace MCGalaxy.SQL { public override List AllTables() { const string syntax = "SELECT name from sqlite_master WHERE type='table'"; - List tables = Database.GetStrings(syntax); - + List tables = Database.GetStrings(syntax); + // exclude sqlite built-in database tables for (int i = tables.Count - 1; i >= 0; i--) { if (tables[i].StartsWith("sqlite_")) tables.RemoveAt(i); @@ -79,7 +79,7 @@ namespace MCGalaxy.SQL { static object IterateColumnNames(IDataRecord record, object arg) { List columns = (List)arg; - columns.Add(record.GetString("name")); + columns.Add(record.GetText("name")); return arg; } @@ -186,10 +186,6 @@ namespace MCGalaxy.SQL { protected override IDbCommand CreateCommand(string query, IDbConnection conn) { return new SQLiteCommand(query, (SQLiteConnection)conn); - } - - protected override DbDataAdapter CreateDataAdapter(string query, IDbConnection conn) { - return new SQLiteDataAdapter(query, (SQLiteConnection)conn); } protected override IDbDataParameter CreateParameter() { diff --git a/MCGalaxy/Database/Database.cs b/MCGalaxy/Database/Database.cs index 23eb00e44..58ba8b826 100644 --- a/MCGalaxy/Database/Database.cs +++ b/MCGalaxy/Database/Database.cs @@ -50,7 +50,7 @@ namespace MCGalaxy.SQL { internal static object ReadFields(IDataRecord record, object arg) { string[] field = new string[record.FieldCount]; - for (int i = 0; i < field.Length; i++) { field[i] = record.GetString(i); } + for (int i = 0; i < field.Length; i++) { field[i] = record.GetString(i); } ((List)arg).Add(field); return arg; } @@ -108,16 +108,19 @@ namespace MCGalaxy.SQL { } - internal static string GetString(this IDataRecord record, string name) { - return record.GetString(record.GetOrdinal(name)); + internal static string GetText(this IDataRecord record, string name) { + int i = record.GetOrdinal(name); + return record.IsDBNull(i) ? "" : record.GetString(i); } - internal static int GetInt32(this IDataRecord record, string name) { - return record.GetInt32(record.GetOrdinal(name)); + internal static int GetInt(this IDataRecord record, string name) { + int i = record.GetOrdinal(name); + return record.IsDBNull(i) ? 0 : record.GetInt32(i); } - internal static long GetInt64(this IDataRecord record, string name) { - return record.GetInt64(record.GetOrdinal(name)); + internal static long GetLong(this IDataRecord record, string name) { + int i = record.GetOrdinal(name); + return record.IsDBNull(i) ? 0 : record.GetInt64(i); } internal static DateTime GetDateTime(this IDataRecord record, string name) { diff --git a/MCGalaxy/Database/ParameterisedQuery.cs b/MCGalaxy/Database/ParameterisedQuery.cs index 9af739a14..d87db736f 100644 --- a/MCGalaxy/Database/ParameterisedQuery.cs +++ b/MCGalaxy/Database/ParameterisedQuery.cs @@ -29,8 +29,7 @@ namespace MCGalaxy.SQL { protected abstract bool MultipleSchema { get; } protected abstract IDbConnection CreateConnection(string connString); - protected abstract IDbCommand CreateCommand(string query, IDbConnection conn); - protected abstract DbDataAdapter CreateDataAdapter(string query, IDbConnection conn); + protected abstract IDbCommand CreateCommand(string query, IDbConnection conn); protected abstract IDbDataParameter CreateParameter(); diff --git a/MCGalaxy/Database/PlayerData.cs b/MCGalaxy/Database/PlayerData.cs index 79cff450c..f15c964d3 100644 --- a/MCGalaxy/Database/PlayerData.cs +++ b/MCGalaxy/Database/PlayerData.cs @@ -90,42 +90,42 @@ namespace MCGalaxy.DB { } internal static PlayerData Parse(IDataRecord record) { - PlayerData data = new PlayerData(); - data.Name = record.GetString("Name"); - data.IP = record.GetString("IP"); - data.DatabaseID = record.GetInt32("ID"); + PlayerData data = new PlayerData(); + data.Name = record.GetText("Name"); + data.IP = record.GetText("IP"); + data.DatabaseID = record.GetInt("ID"); // Backwards compatibility with old format - string rawTime = record.GetString(ColumnTimeSpent); + string rawTime = record.GetText(ColumnTimeSpent); try { long secs = long.Parse(rawTime); data.TotalTime = TimeSpan.FromSeconds(secs); } catch { - data.TotalTime = rawTime.ParseDBTime(); + data.TotalTime = rawTime.ParseOldDBTimeSpent(); } data.FirstLogin = record.GetDateTime(ColumnFirstLogin); data.LastLogin = record.GetDateTime(ColumnLastLogin); - data.Title = record.GetString(ColumnTitle); + data.Title = record.GetText(ColumnTitle); data.Title = data.Title.Cp437ToUnicode(); - data.TitleColor = ParseCol(record.GetString(ColumnTColor)); - data.Color = ParseCol(record.GetString(ColumnColor)); + data.TitleColor = ParseCol(record.GetText(ColumnTColor)); + data.Color = ParseCol(record.GetText(ColumnColor)); - data.Money = record.GetInt32(ColumnMoney); - data.Deaths = record.GetInt32(ColumnDeaths); - data.Logins = record.GetInt32(ColumnLogins); - data.Kicks = record.GetInt32(ColumnKicked); - data.Messages = record.GetInt32(ColumnMessages); + data.Money = record.GetInt(ColumnMoney); + data.Deaths = record.GetInt(ColumnDeaths); + data.Logins = record.GetInt(ColumnLogins); + data.Kicks = record.GetInt(ColumnKicked); + data.Messages = record.GetInt(ColumnMessages); - long blocks = record.GetInt64(ColumnTotalBlocks); - long cuboided = record.GetInt64(ColumnTotalCuboided); + long blocks = record.GetLong(ColumnTotalBlocks); + long cuboided = record.GetLong(ColumnTotalCuboided); data.TotalModified = blocks & LowerBitsMask; data.TotalPlaced = blocks >> LowerBits; data.TotalDrawn = cuboided & LowerBitsMask; data.TotalDeleted = cuboided >> LowerBits; return data; - } + } internal static object Read(IDataRecord record, object arg) { return Parse(record); } internal static long ParseLong(string value) { diff --git a/MCGalaxy/Economy/Economy.DB.cs b/MCGalaxy/Economy/Economy.DB.cs index 8df2566d4..c84f0622c 100644 --- a/MCGalaxy/Economy/Economy.DB.cs +++ b/MCGalaxy/Economy/Economy.DB.cs @@ -36,7 +36,7 @@ namespace MCGalaxy.Eco { static object ListOld(IDataRecord record, object arg) { EcoStats stats = ParseStats(record); - stats.__unused = record.GetInt32("money"); + stats.__unused = record.GetInt("money"); ((List)arg).Add(stats); return arg; } @@ -80,13 +80,13 @@ namespace MCGalaxy.Eco { static EcoStats ParseStats(IDataRecord record) { EcoStats stats; - stats.Player = record.GetString("player"); - stats.Payment = Parse(record.GetString("payment")); - stats.Purchase = Parse(record.GetString("purchase")); - stats.Salary = Parse(record.GetString("salary")); - stats.Fine = Parse(record.GetString("fine")); + stats.Player = record.GetText("player"); + stats.Payment = Parse(record.GetText("payment")); + stats.Purchase = Parse(record.GetText("purchase")); + stats.Salary = Parse(record.GetText("salary")); + stats.Fine = Parse(record.GetText("fine")); - stats.TotalSpent = record.GetInt32("total"); + stats.TotalSpent = record.GetInt("total"); stats.__unused = 0; return stats; } diff --git a/MCGalaxy/Games/ZombieSurvival/ZSConfig.cs b/MCGalaxy/Games/ZombieSurvival/ZSConfig.cs index ea8ddfba9..35877fd14 100644 --- a/MCGalaxy/Games/ZombieSurvival/ZSConfig.cs +++ b/MCGalaxy/Games/ZombieSurvival/ZSConfig.cs @@ -117,16 +117,16 @@ namespace MCGalaxy.Games { "{0} transplanted {1}'s living brain" }; public static List LoadInfectMessages() { - List msgs = new List(); + List msgs = new List(); try { - if (!File.Exists("text/infectmessages.txt")) { + if (!File.Exists("text/infectmessages.txt")) { File.WriteAllLines("text/infectmessages.txt", defMessages); - } + } msgs = Utils.ReadAllLinesList("text/infectmessages.txt"); } catch (Exception ex) { Logger.LogError(ex); } - + if (msgs.Count == 0) msgs = new List(defMessages); return msgs; } diff --git a/MCGalaxy/Games/ZombieSurvival/ZSGame.DB.cs b/MCGalaxy/Games/ZombieSurvival/ZSGame.DB.cs index c82a3d012..bdf1e42ef 100644 --- a/MCGalaxy/Games/ZombieSurvival/ZSGame.DB.cs +++ b/MCGalaxy/Games/ZombieSurvival/ZSGame.DB.cs @@ -103,10 +103,10 @@ namespace MCGalaxy.Games { static object ReadStats(IDataRecord record, object arg) { ZombieStats stats; - stats.TotalRounds = record.GetInt32("TotalRounds"); - stats.MaxRounds = record.GetInt32("MaxRounds"); - stats.TotalInfected = record.GetInt32("TotalInfected"); - stats.MaxInfected = record.GetInt32("MaxInfected"); + stats.TotalRounds = record.GetInt("TotalRounds"); + stats.MaxRounds = record.GetInt("MaxRounds"); + stats.TotalInfected = record.GetInt("TotalInfected"); + stats.MaxInfected = record.GetInt("MaxInfected"); return stats; } diff --git a/MCGalaxy/Levels/LevelDB.cs b/MCGalaxy/Levels/LevelDB.cs index a402fa9b1..66aca0dfe 100644 --- a/MCGalaxy/Levels/LevelDB.cs +++ b/MCGalaxy/Levels/LevelDB.cs @@ -43,14 +43,14 @@ namespace MCGalaxy { static object ListZones(IDataRecord record, object arg) { Zone z = new Zone(); - z.MinX = (ushort)record.GetInt32("SmallX"); - z.MinY = (ushort)record.GetInt32("SmallY"); - z.MinX = (ushort)record.GetInt32("SmallZ"); + z.MinX = (ushort)record.GetInt("SmallX"); + z.MinY = (ushort)record.GetInt("SmallY"); + z.MinX = (ushort)record.GetInt("SmallZ"); - z.MaxX = (ushort)record.GetInt32("BigX"); - z.MaxY = (ushort)record.GetInt32("BigY"); - z.MaxX = (ushort)record.GetInt32("BigZ"); - z.Config.Name = record.GetString("Owner"); + z.MaxX = (ushort)record.GetInt("BigX"); + z.MaxY = (ushort)record.GetInt("BigY"); + z.MaxX = (ushort)record.GetInt("BigZ"); + z.Config.Name = record.GetText("Owner"); ((List)arg).Add(z); return arg; diff --git a/MCGalaxy/Player/Player.Login.cs b/MCGalaxy/Player/Player.Login.cs index fe7282981..1166dcb4e 100644 --- a/MCGalaxy/Player/Player.Login.cs +++ b/MCGalaxy/Player/Player.Login.cs @@ -214,8 +214,8 @@ namespace MCGalaxy { void GetPlayerStats() { object raw = Database.Backend.ReadRows("Players", "*", - null, PlayerData.Read, - "WHERE Name=@0", name); + null, PlayerData.Read, + "WHERE Name=@0", name); if (raw == null) { PlayerData.Create(this); Chat.MessageFrom(this, "λNICK %Shas connected for the first time!"); diff --git a/MCGalaxy/Server/Tasks/UpgradeTasks.cs b/MCGalaxy/Server/Tasks/UpgradeTasks.cs index c540f323d..50ca61b2e 100644 --- a/MCGalaxy/Server/Tasks/UpgradeTasks.cs +++ b/MCGalaxy/Server/Tasks/UpgradeTasks.cs @@ -237,7 +237,7 @@ namespace MCGalaxy.Tasks { playerCount++; try { int id = record.GetInt32(0); - TimeSpan span = record.GetString(1).ParseDBTime(); + TimeSpan span = record.GetString(1).ParseOldDBTimeSpent(); playerIds.Add(id); playerSeconds.Add((long)span.TotalSeconds); @@ -256,7 +256,7 @@ namespace MCGalaxy.Tasks { idParam.Value = playerIds[i]; secsParam.Value = playerSeconds[i]; - using (IDbCommand cmd = bulk.CreateCommand("UPDATE Players SET TimeSpent = @1 WHERE ID = @0")) { + using (IDbCommand cmd = bulk.CreateCommand("UPDATE Players SET TimeSpent=@1 WHERE ID=@0")) { cmd.Parameters.Add(idParam); cmd.Parameters.Add(secsParam); cmd.ExecuteNonQuery(); diff --git a/MCGalaxy/util/Extensions/DateExts.cs b/MCGalaxy/util/Extensions/DateExts.cs index f442082ee..fac1283b2 100644 --- a/MCGalaxy/util/Extensions/DateExts.cs +++ b/MCGalaxy/util/Extensions/DateExts.cs @@ -21,7 +21,7 @@ using System.Collections.Generic; namespace MCGalaxy { public static class DateExts { - public static TimeSpan ParseDBTime(this string value) { + public static TimeSpan ParseOldDBTimeSpent(this string value) { string[] parts = value.SplitSpaces(); return new TimeSpan(int.Parse(parts[0]), int.Parse(parts[1]), int.Parse(parts[2]), int.Parse(parts[3]));