From 81b6872d6e2572e5f4343a8dbd2c35ddc3a3030b Mon Sep 17 00:00:00 2001 From: Luke Date: Sun, 11 Apr 2021 21:00:28 +1000 Subject: [PATCH 1/4] Fix OTAPI reference This now allows MonoDevelop to compile without receiving error while starting a build: System.IO.FileLoadException: The assembly name is invalid --- TShockAPI/TShockAPI.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/TShockAPI/TShockAPI.csproj b/TShockAPI/TShockAPI.csproj index cde1f86e..add34f1e 100644 --- a/TShockAPI/TShockAPI.csproj +++ b/TShockAPI/TShockAPI.csproj @@ -70,7 +70,7 @@ ..\packages\Newtonsoft.Json.10.0.3\lib\net45\Newtonsoft.Json.dll - + False ..\TerrariaServerAPI\TerrariaServerAPI\bin\$(ConfigurationName)\OTAPI.dll From d0ce5746ea8d2c4d8ef7d5d289aa2ca4acbb12bd Mon Sep 17 00:00:00 2001 From: Luke Date: Sun, 11 Apr 2021 21:18:37 +1000 Subject: [PATCH 2/4] Fix mysql & sqlite ban manager migration during init Tested with existing bans in both sqlite & mysql environments. This is in addition to the syntax fix from earlier. --- TShockAPI/DB/BanManager.cs | 75 ++++++++++++++++++++++++++++---------- 1 file changed, 55 insertions(+), 20 deletions(-) diff --git a/TShockAPI/DB/BanManager.cs b/TShockAPI/DB/BanManager.cs index 7bbe013d..5777e84c 100644 --- a/TShockAPI/DB/BanManager.cs +++ b/TShockAPI/DB/BanManager.cs @@ -37,18 +37,7 @@ namespace TShockAPI.DB /// /// Readonly dictionary of Bans, keyed on ban ticket number. /// - public ReadOnlyDictionary Bans - { - get - { - if (_bans == null) - { - _bans = RetrieveAllBans().ToDictionary(b => b.TicketNumber); - } - - return new ReadOnlyDictionary(_bans); - } - } + public ReadOnlyDictionary Bans => new ReadOnlyDictionary(_bans); /// /// Event invoked when a ban is checked for validity @@ -93,12 +82,24 @@ namespace TShockAPI.DB throw new Exception("Could not find a database library (probably Sqlite3.dll)"); } + EnsureBansCollection(); TryConvertBans(); OnBanValidate += BanValidateCheck; OnBanPreAdd += BanAddedCheck; } + /// + /// Ensures the collection is ready to use. + /// + private void EnsureBansCollection() + { + if (_bans == null) + { + _bans = RetrieveAllBans().ToDictionary(b => b.TicketNumber); + } + } + /// /// Converts bans from the old ban system to the new. /// @@ -116,6 +117,7 @@ namespace TShockAPI.DB if (res != 0) { + var bans = new List(); using (var reader = database.QueryReader("SELECT * FROM Bans")) { while (reader.Read()) @@ -140,22 +142,46 @@ namespace TShockAPI.DB if (!string.IsNullOrWhiteSpace(ip)) { - InsertBan($"{Identifier.IP}{ip}", reason, banningUser, start, end); + bans.Add(new BanPreAddEventArgs + { + Identifier = $"{Identifier.IP}{ip}", + Reason = reason, + BanningUser = banningUser, + BanDateTime = start, + ExpirationDateTime = end + }); } if (!string.IsNullOrWhiteSpace(account)) { - InsertBan($"{Identifier.Account}{account}", reason, banningUser, start, end); + bans.Add(new BanPreAddEventArgs + { + Identifier = $"{Identifier.Account}{account}", + Reason = reason, + BanningUser = banningUser, + BanDateTime = start, + ExpirationDateTime = end + }); } if (!string.IsNullOrWhiteSpace(uuid)) { - InsertBan($"{Identifier.UUID}{uuid}", reason, banningUser, start, end); + bans.Add(new BanPreAddEventArgs + { + Identifier = $"{Identifier.UUID}{uuid}", + Reason = reason, + BanningUser = banningUser, + BanDateTime = start, + ExpirationDateTime = end + }); } } } - database.Query("DROP TABLE 'Bans'"); + foreach (var ban in bans) + InsertBan(ban); + + database.Query("DROP TABLE Bans"); } } @@ -232,7 +258,7 @@ namespace TShockAPI.DB args.Message = args.Valid ? null : "a current ban for this identifier already exists."; } } - + /// /// Adds a new ban for the given identifier. Returns a Ban object if the ban was added, else null /// @@ -252,7 +278,16 @@ namespace TShockAPI.DB BanDateTime = fromDate, ExpirationDateTime = toDate }; + return InsertBan(args); + } + /// + /// Adds a new ban for the given data. Returns a Ban object if the ban was added, else null + /// + /// A predefined instance of + /// + public AddBanResult InsertBan(BanPreAddEventArgs args) + { OnBanPreAdd?.Invoke(this, args); if (!args.Valid) @@ -265,21 +300,21 @@ namespace TShockAPI.DB if (database.GetSqlType() == SqlType.Mysql) { - query += "SELECT CAST(LAST_INSERT_ID() as INT);"; + query += "SELECT LAST_INSERT_ID();"; } else { query += "SELECT CAST(last_insert_rowid() as INT);"; } - int ticketId = database.QueryScalar(query, identifier, reason, banningUser, fromDate.Ticks, toDate.Ticks); + int ticketId = database.QueryScalar(query, args.Identifier, args.Reason, args.BanningUser, args.BanDateTime.Ticks, args.ExpirationDateTime.Ticks); if (ticketId == 0) { return new AddBanResult { Message = "Database insert failed." }; } - Ban b = new Ban(ticketId, identifier, reason, banningUser, fromDate, toDate); + Ban b = new Ban(ticketId, args.Identifier, args.Reason, args.BanningUser, args.BanDateTime, args.ExpirationDateTime); _bans.Add(ticketId, b); OnBanPostAdd?.Invoke(this, new BanEventArgs { Ban = b }); From 29e55a866d178cf4903bd14f42d5a03f17bd4662 Mon Sep 17 00:00:00 2001 From: Luke Date: Sun, 11 Apr 2021 21:18:52 +1000 Subject: [PATCH 3/4] Update CHANGELOG.md --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index bf48f1aa..9a70062b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,7 +16,7 @@ This is the rolling changelog for TShock for Terraria. Use past tense when addin * This could be you! ## TShock 4.5.0.1 -* Fixed typo in conversion from old to new ban system for MySQL hosted ban databases. (@DeathCradle) +* Fixed conversion from old to new ban system for MySQL hosted ban databases. (@DeathCradle) ## TShock 4.5.0 * Updated OTAPI and TSAPI to Terraria 1.4.2.1. (@Stealownz, @DeathCradle) From 71e2ae1b316300da144a7cfd6a90bdbfe1bdce7c Mon Sep 17 00:00:00 2001 From: Luke Date: Mon, 12 Apr 2021 17:35:46 +1000 Subject: [PATCH 4/4] Corrected UUID InsertBan in TSPlayer Pointed out thanks to this discussion: https://github.com/Pryaxis/TShock/discussions/2268#discussioncomment-598331 --- TShockAPI/TSPlayer.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/TShockAPI/TSPlayer.cs b/TShockAPI/TSPlayer.cs index dc76e94c..4c24468b 100644 --- a/TShockAPI/TSPlayer.cs +++ b/TShockAPI/TSPlayer.cs @@ -1637,7 +1637,7 @@ namespace TShockAPI if (force) { TShock.Bans.InsertBan($"{Identifier.IP}{IP}", reason, adminUserName, DateTime.UtcNow, DateTime.MaxValue); - TShock.Bans.InsertBan($"{Identifier.IP}{UUID}", reason, adminUserName, DateTime.UtcNow, DateTime.MaxValue); + TShock.Bans.InsertBan($"{Identifier.UUID}{UUID}", reason, adminUserName, DateTime.UtcNow, DateTime.MaxValue); if (Account != null) { TShock.Bans.InsertBan($"{Identifier.Account}{Account.Name}", reason, adminUserName, DateTime.UtcNow, DateTime.MaxValue);