From 9df15e5736df8428f7173cc9026bcc8095a08f89 Mon Sep 17 00:00:00 2001 From: MSWS Date: Wed, 7 Aug 2024 20:13:21 -0700 Subject: [PATCH 1/2] fix/synchronousAsync --- mod/Jailbreak.Debug/Subcommands/DebugZone.cs | 30 ++++++++++++------- .../AbstractArmoryRestrictedDay.cs | 2 +- .../SpecialDays/AbstractCellRestrictedDay.cs | 2 +- mod/Jailbreak.Zones/RandomZoneGenerator.cs | 18 +++++++---- mod/Jailbreak.Zones/SqlZoneManager.cs | 6 ++-- .../Mod/SpecialDay/AbstractSpecialDay.cs | 7 +++-- .../Mod/Zones/IZoneManager.cs | 22 ++++++++++---- public/Jailbreak.Public/Utils/MapUtil.cs | 2 +- 8 files changed, 59 insertions(+), 30 deletions(-) diff --git a/mod/Jailbreak.Debug/Subcommands/DebugZone.cs b/mod/Jailbreak.Debug/Subcommands/DebugZone.cs index 0718065d..77f3bb5f 100644 --- a/mod/Jailbreak.Debug/Subcommands/DebugZone.cs +++ b/mod/Jailbreak.Debug/Subcommands/DebugZone.cs @@ -48,6 +48,7 @@ public override void OnCommand(CCSPlayerController? executor, specifiedType = success; } + string? map = Server.MapName; switch (info.GetArg(1).ToLower()) { case "finish": case "done": @@ -76,8 +77,9 @@ public override void OnCommand(CCSPlayerController? executor, var zoneCount = 0; foreach (var type in Enum.GetValues()) { if (specifiedType != null && type != specifiedType) continue; - var displayZones = - zoneManager.GetZones(type).GetAwaiter().GetResult(); + var displayZones = zoneManager.GetZones(Server.MapName, type) + .GetAwaiter() + .GetResult(); foreach (var z in displayZones) z.Draw(plugin, type.GetColor(), 120); zoneCount += displayZones.Count; @@ -100,8 +102,9 @@ public override void OnCommand(CCSPlayerController? executor, case "delete": var toDelete = getUniqueZone(executor, specifiedType); if (toDelete == null) return; + map = Server.MapName; Server.NextFrameAsync(async () => { - await zoneManager.DeleteZone(toDelete.Value.Item1.Id); + await zoneManager.DeleteZone(toDelete.Value.Item1.Id, map); Server.NextFrame(() => { executor.PrintToChat("Deleted zone #" + toDelete.Value.Item1.Id); }); @@ -113,7 +116,7 @@ public override void OnCommand(CCSPlayerController? executor, if (innerPair == null) return; var innerZone = innerPair.Value.Item1; innerZone.AddPoint(position); - var map = Server.MapName; + map = Server.MapName; Server.NextFrameAsync(async () => { await zoneManager.DeleteZone(innerZone.Id); await zoneManager.PushZoneWithID(innerZone, innerPair.Value.Item2, @@ -145,7 +148,9 @@ await zoneManager.PushZoneWithID(innerZone, innerPair.Value.Item2, if (specifiedType == null) { foreach (var type in Enum.GetValues()) { - var zones = zoneManager.GetZones(type).GetAwaiter().GetResult(); + var zones = zoneManager.GetZones(Server.MapName, type) + .GetAwaiter() + .GetResult(); if (!allZones.ContainsKey(type)) continue; info.ReplyToCommand($"{type} zones: {zones.Count}"); @@ -154,7 +159,7 @@ await zoneManager.PushZoneWithID(innerZone, innerPair.Value.Item2, return; } - var toList = zoneManager.GetZones(specifiedType.Value) + var toList = zoneManager.GetZones(Server.MapName, specifiedType.Value) .GetAwaiter() .GetResult(); foreach (var listZone in toList) @@ -164,7 +169,7 @@ await zoneManager.PushZoneWithID(innerZone, innerPair.Value.Item2, case "cleanup": // Cleanup auto-generated zones // Remove spawns that are inside of any DO NOT TELEPORT zones - var spawns = zoneManager.GetZones(ZoneType.SPAWN_AUTO) + var spawns = zoneManager.GetZones(Server.MapName, ZoneType.SPAWN_AUTO) .GetAwaiter() .GetResult(); if (spawns.Count == 0) { @@ -173,7 +178,8 @@ await zoneManager.PushZoneWithID(innerZone, innerPair.Value.Item2, } var doNotTeleport = zoneManager - .GetZones(ZoneTypeExtensions.DoNotTeleports().ToArray()) + .GetZones(Server.MapName, + ZoneTypeExtensions.DoNotTeleports().ToArray()) .GetAwaiter() .GetResult(); @@ -186,7 +192,8 @@ await zoneManager.PushZoneWithID(innerZone, innerPair.Value.Item2, info.ReplyToCommand("Removing " + toRemove.Count + " auto-generated zones"); Server.NextFrameAsync(async () => { - foreach (var z in toRemove) await zoneManager.DeleteZone(z.Id); + foreach (var z in toRemove) + await zoneManager.DeleteZone(z.Id, Server.MapName); }); return; } @@ -203,14 +210,15 @@ await zoneManager.PushZoneWithID(innerZone, innerPair.Value.Item2, attemptBeginCreation(executor, specifiedType.Value); return; case "set": - var zones = zoneManager.GetZones(specifiedType.Value) + var zones = zoneManager.GetZones(Server.MapName, specifiedType.Value) .GetAwaiter() .GetResult(); Server.NextFrameAsync(async () => { var copy = zones.ToList(); - foreach (var zone in copy) await zoneManager.DeleteZone(zone.Id); + foreach (var zone in copy) + await zoneManager.DeleteZone(zone.Id, Server.MapName); Server.NextFrame(() => attemptBeginCreation(executor, specifiedType.Value)); diff --git a/mod/Jailbreak.SpecialDay/SpecialDays/AbstractArmoryRestrictedDay.cs b/mod/Jailbreak.SpecialDay/SpecialDays/AbstractArmoryRestrictedDay.cs index 932ee98f..1f64b69f 100644 --- a/mod/Jailbreak.SpecialDay/SpecialDays/AbstractArmoryRestrictedDay.cs +++ b/mod/Jailbreak.SpecialDay/SpecialDays/AbstractArmoryRestrictedDay.cs @@ -31,7 +31,7 @@ public virtual IView ArmoryReminder override protected IZone GetZone() { var manager = provider.GetRequiredService(); - var zones = manager.GetZones(ZoneType.ARMORY).GetAwaiter().GetResult(); + var zones = manager.GetZones(Server.MapName, ZoneType.ARMORY).GetAwaiter().GetResult(); if (zones.Count > 0) return new MultiZoneWrapper(zones); var bounds = new DistanceZone( diff --git a/mod/Jailbreak.SpecialDay/SpecialDays/AbstractCellRestrictedDay.cs b/mod/Jailbreak.SpecialDay/SpecialDays/AbstractCellRestrictedDay.cs index 71e2d0a4..4ad1eaa1 100644 --- a/mod/Jailbreak.SpecialDay/SpecialDays/AbstractCellRestrictedDay.cs +++ b/mod/Jailbreak.SpecialDay/SpecialDays/AbstractCellRestrictedDay.cs @@ -30,7 +30,7 @@ public virtual IView CellReminder override protected IZone GetZone() { var manager = provider.GetRequiredService(); - var zones = manager.GetZones(ZoneType.CELL).GetAwaiter().GetResult(); + var zones = manager.GetZones(Server.MapName, ZoneType.CELL).GetAwaiter().GetResult(); if (zones.Count > 0) return new MultiZoneWrapper(zones); var bounds = new DistanceZone( diff --git a/mod/Jailbreak.Zones/RandomZoneGenerator.cs b/mod/Jailbreak.Zones/RandomZoneGenerator.cs index a5798f26..03bdb1d9 100644 --- a/mod/Jailbreak.Zones/RandomZoneGenerator.cs +++ b/mod/Jailbreak.Zones/RandomZoneGenerator.cs @@ -127,19 +127,23 @@ private float zoneScore(IList? zones) { } private IList getManualSpawnPoints() { - var zones = zoneManager.GetZones(ZoneType.SPAWN).GetAwaiter().GetResult(); + var zones = zoneManager.GetZones(Server.MapName, ZoneType.SPAWN) + .GetAwaiter() + .GetResult(); return zones; } private IList getAutoSpawnPoints() { - var zones = zoneManager.GetZones(ZoneType.SPAWN_AUTO) + var zones = zoneManager.GetZones(Server.MapName, ZoneType.SPAWN_AUTO) .GetAwaiter() .GetResult(); return zones; } private IZone getCells() { - var result = zoneManager.GetZones(ZoneType.CELL).GetAwaiter().GetResult(); + var result = zoneManager.GetZones(Server.MapName, ZoneType.CELL) + .GetAwaiter() + .GetResult(); if (result.Count > 0) return new MultiZoneWrapper(result); var bounds = new DistanceZone( @@ -154,11 +158,15 @@ private IZone getCells() { private IList getRestrictedAreas() { List result = []; foreach (var zone in ZoneTypeExtensions.DoNotTeleports()) { - var zones = zoneManager.GetZones(zone).GetAwaiter().GetResult(); + var zones = zoneManager.GetZones(Server.MapName, zone) + .GetAwaiter() + .GetResult(); result.AddRange(zones); } - var armory = zoneManager.GetZones(ZoneType.ARMORY).GetAwaiter().GetResult(); + var armory = zoneManager.GetZones(Server.MapName, ZoneType.ARMORY) + .GetAwaiter() + .GetResult(); if (armory.Count == 0) { var bounds = new DistanceZone( Utilities diff --git a/mod/Jailbreak.Zones/SqlZoneManager.cs b/mod/Jailbreak.Zones/SqlZoneManager.cs index 944dcd1e..0815cfea 100644 --- a/mod/Jailbreak.Zones/SqlZoneManager.cs +++ b/mod/Jailbreak.Zones/SqlZoneManager.cs @@ -132,10 +132,10 @@ public async Task PushZone(IZone zone, ZoneType type, string map) { await PushZoneWithID(zone, type, map); } - public async Task UpdateZone(IZone zone, ZoneType type, int id) { - await DeleteZone(id, Server.MapName); + public async Task UpdateZone(IZone zone, ZoneType type, int id, string map) { + await DeleteZone(id, map); zone.Id = id; - await PushZoneWithID(zone, type, Server.MapName); + await PushZoneWithID(zone, type, map); } public Task>> GetAllZones() { diff --git a/public/Jailbreak.Public/Mod/SpecialDay/AbstractSpecialDay.cs b/public/Jailbreak.Public/Mod/SpecialDay/AbstractSpecialDay.cs index a4fa7547..2dd5db54 100644 --- a/public/Jailbreak.Public/Mod/SpecialDay/AbstractSpecialDay.cs +++ b/public/Jailbreak.Public/Mod/SpecialDay/AbstractSpecialDay.cs @@ -210,12 +210,15 @@ protected List getRandomSpawns(bool includeSpawns = true, var zoneManager = Provider.GetService(); if (zoneManager != null) { if (includeAuto) - result.AddRange(zoneManager.GetZones(ZoneType.SPAWN_AUTO) + result.AddRange(zoneManager + .GetZones(Server.MapName, ZoneType.SPAWN_AUTO) .GetAwaiter() .GetResult() .Select(z => z.GetCenterPoint())); - var zones = zoneManager.GetZones(ZoneType.SPAWN).GetAwaiter().GetResult(); + var zones = zoneManager.GetZones(Server.MapName, ZoneType.SPAWN) + .GetAwaiter() + .GetResult(); result.AddRange(zones.Select(z => z.GetCenterPoint())); } diff --git a/public/Jailbreak.Public/Mod/Zones/IZoneManager.cs b/public/Jailbreak.Public/Mod/Zones/IZoneManager.cs index 6ee837b5..24ef1da4 100644 --- a/public/Jailbreak.Public/Mod/Zones/IZoneManager.cs +++ b/public/Jailbreak.Public/Mod/Zones/IZoneManager.cs @@ -5,19 +5,29 @@ namespace Jailbreak.Public.Mod.Zones; public interface IZoneManager : IPluginBehavior { Task LoadZones(string map); - Task DeleteZone(int zoneId) { return DeleteZone(zoneId, Server.MapName); } + + [Obsolete( + "This method hides asynchroneous behavior, use the async version instead")] + Task DeleteZone(int zoneId) { + Server.NextFrame(() => { + var map = Server.MapName; + Server.NextFrameAsync(async () => { await DeleteZone(zoneId, map); }); + }); + return Task.CompletedTask; + } + Task DeleteZone(int zoneId, string map); Task> GetZones(string map, ZoneType type); - async Task> GetZones(params ZoneType[] type) { + async Task> GetZones(string map, params ZoneType[] type) { List>> tasks = []; - tasks.AddRange(type.Select(GetZones)); + tasks.AddRange(type.Select(t => GetZones(t, map))); return (await Task.WhenAll(tasks)).SelectMany(x => x).ToList(); } - Task> GetZones(ZoneType type) { - return GetZones(Server.MapName, type); + Task> GetZones(ZoneType type, string map) { + return GetZones(map, type); } Task PushZoneWithID(IZone zone, ZoneType type, string map); @@ -27,7 +37,7 @@ Task PushZone(IZone zone, ZoneType type) { return PushZone(zone, type, Server.MapName); } - Task UpdateZone(IZone zone, ZoneType type, int id); + Task UpdateZone(IZone zone, ZoneType type, int id, string map); Task>> GetAllZones(); } \ No newline at end of file diff --git a/public/Jailbreak.Public/Utils/MapUtil.cs b/public/Jailbreak.Public/Utils/MapUtil.cs index 9f2bea35..295d6801 100644 --- a/public/Jailbreak.Public/Utils/MapUtil.cs +++ b/public/Jailbreak.Public/Utils/MapUtil.cs @@ -19,7 +19,7 @@ private static Vector getCtSpawn() { } public static bool OpenCells(IZoneManager zoneManager) { - var zones = zoneManager.GetZones(ZoneType.CELL_BUTTON) + var zones = zoneManager.GetZones(Server.MapName, ZoneType.CELL_BUTTON) .GetAwaiter() .GetResult(); From 59ebba784718a90aad68f1a67097302e3dfc8789 Mon Sep 17 00:00:00 2001 From: MSWS Date: Wed, 7 Aug 2024 20:24:36 -0700 Subject: [PATCH 2/2] Remove obsolete usage --- lang/Jailbreak.English/SpecialDay/GunDayLocale.cs | 1 - lang/Jailbreak.English/SpecialDay/SDLocale.cs | 1 - lang/Jailbreak.English/SpecialDay/SoloDayLocale.cs | 1 - .../SpecialDay/SpeedrunDayLocale.cs | 1 - lang/Jailbreak.English/SpecialDay/TeamDayLocale.cs | 1 - mod/Jailbreak.Debug/Subcommands/DebugZone.cs | 14 ++++++-------- 6 files changed, 6 insertions(+), 13 deletions(-) diff --git a/lang/Jailbreak.English/SpecialDay/GunDayLocale.cs b/lang/Jailbreak.English/SpecialDay/GunDayLocale.cs index 3b3ed110..73bb4737 100644 --- a/lang/Jailbreak.English/SpecialDay/GunDayLocale.cs +++ b/lang/Jailbreak.English/SpecialDay/GunDayLocale.cs @@ -1,7 +1,6 @@ using CounterStrikeSharp.API.Core; using CounterStrikeSharp.API.Modules.Utils; using Jailbreak.Formatting.Base; -using Jailbreak.Formatting.Views; using Jailbreak.Formatting.Views.SpecialDay; namespace Jailbreak.English.SpecialDay; diff --git a/lang/Jailbreak.English/SpecialDay/SDLocale.cs b/lang/Jailbreak.English/SpecialDay/SDLocale.cs index fc471098..b6c88209 100644 --- a/lang/Jailbreak.English/SpecialDay/SDLocale.cs +++ b/lang/Jailbreak.English/SpecialDay/SDLocale.cs @@ -3,7 +3,6 @@ using Jailbreak.Formatting.Core; using Jailbreak.Formatting.Logistics; using Jailbreak.Formatting.Objects; -using Jailbreak.Formatting.Views; using Jailbreak.Formatting.Views.SpecialDay; namespace Jailbreak.English.SpecialDay; diff --git a/lang/Jailbreak.English/SpecialDay/SoloDayLocale.cs b/lang/Jailbreak.English/SpecialDay/SoloDayLocale.cs index 23b785ff..8d5ef5d9 100644 --- a/lang/Jailbreak.English/SpecialDay/SoloDayLocale.cs +++ b/lang/Jailbreak.English/SpecialDay/SoloDayLocale.cs @@ -1,5 +1,4 @@ using Jailbreak.Formatting.Base; -using Jailbreak.Formatting.Views; using Jailbreak.Formatting.Views.SpecialDay; using Jailbreak.Public.Utils; diff --git a/lang/Jailbreak.English/SpecialDay/SpeedrunDayLocale.cs b/lang/Jailbreak.English/SpecialDay/SpeedrunDayLocale.cs index 3e43777c..b3ec791a 100644 --- a/lang/Jailbreak.English/SpecialDay/SpeedrunDayLocale.cs +++ b/lang/Jailbreak.English/SpecialDay/SpeedrunDayLocale.cs @@ -1,7 +1,6 @@ using CounterStrikeSharp.API.Core; using CounterStrikeSharp.API.Modules.Utils; using Jailbreak.Formatting.Base; -using Jailbreak.Formatting.Views; using Jailbreak.Formatting.Views.SpecialDay; namespace Jailbreak.English.SpecialDay; diff --git a/lang/Jailbreak.English/SpecialDay/TeamDayLocale.cs b/lang/Jailbreak.English/SpecialDay/TeamDayLocale.cs index d4c31aeb..54f09a74 100644 --- a/lang/Jailbreak.English/SpecialDay/TeamDayLocale.cs +++ b/lang/Jailbreak.English/SpecialDay/TeamDayLocale.cs @@ -1,6 +1,5 @@ using CounterStrikeSharp.API.Modules.Utils; using Jailbreak.Formatting.Base; -using Jailbreak.Formatting.Views; using Jailbreak.Formatting.Views.SpecialDay; using Jailbreak.Public.Utils; diff --git a/mod/Jailbreak.Debug/Subcommands/DebugZone.cs b/mod/Jailbreak.Debug/Subcommands/DebugZone.cs index 77f3bb5f..68956aa6 100644 --- a/mod/Jailbreak.Debug/Subcommands/DebugZone.cs +++ b/mod/Jailbreak.Debug/Subcommands/DebugZone.cs @@ -48,7 +48,7 @@ public override void OnCommand(CCSPlayerController? executor, specifiedType = success; } - string? map = Server.MapName; + string map = Server.MapName; switch (info.GetArg(1).ToLower()) { case "finish": case "done": @@ -77,7 +77,7 @@ public override void OnCommand(CCSPlayerController? executor, var zoneCount = 0; foreach (var type in Enum.GetValues()) { if (specifiedType != null && type != specifiedType) continue; - var displayZones = zoneManager.GetZones(Server.MapName, type) + var displayZones = zoneManager.GetZones(map, type) .GetAwaiter() .GetResult(); foreach (var z in displayZones) z.Draw(plugin, type.GetColor(), 120); @@ -102,7 +102,6 @@ public override void OnCommand(CCSPlayerController? executor, case "delete": var toDelete = getUniqueZone(executor, specifiedType); if (toDelete == null) return; - map = Server.MapName; Server.NextFrameAsync(async () => { await zoneManager.DeleteZone(toDelete.Value.Item1.Id, map); Server.NextFrame(() => { @@ -116,9 +115,8 @@ public override void OnCommand(CCSPlayerController? executor, if (innerPair == null) return; var innerZone = innerPair.Value.Item1; innerZone.AddPoint(position); - map = Server.MapName; Server.NextFrameAsync(async () => { - await zoneManager.DeleteZone(innerZone.Id); + await zoneManager.DeleteZone(innerZone.Id, map); await zoneManager.PushZoneWithID(innerZone, innerPair.Value.Item2, map); Server.NextFrame(() => { @@ -159,7 +157,7 @@ await zoneManager.PushZoneWithID(innerZone, innerPair.Value.Item2, return; } - var toList = zoneManager.GetZones(Server.MapName, specifiedType.Value) + var toList = zoneManager.GetZones(map, specifiedType.Value) .GetAwaiter() .GetResult(); foreach (var listZone in toList) @@ -169,7 +167,7 @@ await zoneManager.PushZoneWithID(innerZone, innerPair.Value.Item2, case "cleanup": // Cleanup auto-generated zones // Remove spawns that are inside of any DO NOT TELEPORT zones - var spawns = zoneManager.GetZones(Server.MapName, ZoneType.SPAWN_AUTO) + var spawns = zoneManager.GetZones(map, ZoneType.SPAWN_AUTO) .GetAwaiter() .GetResult(); if (spawns.Count == 0) { @@ -193,7 +191,7 @@ await zoneManager.PushZoneWithID(innerZone, innerPair.Value.Item2, + " auto-generated zones"); Server.NextFrameAsync(async () => { foreach (var z in toRemove) - await zoneManager.DeleteZone(z.Id, Server.MapName); + await zoneManager.DeleteZone(z.Id, map); }); return; }