|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.IO; |
| 4 | +using System.Reflection; |
| 5 | +using System.Text.Json; |
| 6 | +using System.Threading; |
| 7 | + |
| 8 | +using CounterStrikeSharp.API.Core; |
| 9 | + |
| 10 | +namespace CSSUniversalMenuAPI; |
| 11 | + |
| 12 | +/// <summary> |
| 13 | +/// Because menus may want access to other drivers, and PluginCapability<> is not expressive enough, |
| 14 | +/// this helper class is included with the interface to aid in constructing and |
| 15 | +/// </summary> |
| 16 | +public static class UniversalMenu |
| 17 | +{ |
| 18 | + private static string? DefaultDriverDesired { get; } |
| 19 | + private static string? DefaultDriverName { get; set; } |
| 20 | + public static IMenuAPI? DefaultDriver { get; private set; } |
| 21 | + |
| 22 | + private static Dictionary<string, IMenuAPI> RegisteredDrivers { get; } = new(); |
| 23 | + public static IReadOnlyDictionary<string, IMenuAPI> Drivers => RegisteredDrivers; |
| 24 | + public static event EventHandler? DriversChanged; |
| 25 | + |
| 26 | + static UniversalMenu() |
| 27 | + { |
| 28 | + var configDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) ?? Environment.CurrentDirectory; |
| 29 | + var configPath = Path.Join(configDir, "config.json"); |
| 30 | + |
| 31 | + if (!File.Exists(configPath)) |
| 32 | + { |
| 33 | + Console.WriteLine("UniversalMenu: No default driver configured, will use first registered driver"); |
| 34 | + return; |
| 35 | + } |
| 36 | + |
| 37 | + var contents = File.ReadAllText(configPath); |
| 38 | + var doc = JsonDocument.Parse(contents); |
| 39 | + DefaultDriverDesired = doc.RootElement.GetProperty("driver").GetString(); |
| 40 | + |
| 41 | + Console.WriteLine($"UniversalMenu: Desired default driver: {DefaultDriverDesired}"); |
| 42 | + } |
| 43 | + |
| 44 | + public static void RegisterDriver(string name, IMenuAPI driver) |
| 45 | + { |
| 46 | + if (!RegisteredDrivers.TryAdd(name, driver)) |
| 47 | + throw new InvalidOperationException($"RegisterDriver(): Conflicting named drivers configured: {name}"); |
| 48 | + |
| 49 | + if (DefaultDriverDesired is null || name == DefaultDriverDesired) |
| 50 | + { |
| 51 | + if (DefaultDriver is not null) |
| 52 | + throw new InvalidOperationException($"RegisterDriver(): Conflicting default drivers configured: {name} and {DefaultDriverName}"); |
| 53 | + DefaultDriver = driver; |
| 54 | + DefaultDriverName = name; |
| 55 | + } |
| 56 | + DriversChanged?.Invoke(null, EventArgs.Empty); |
| 57 | + } |
| 58 | + |
| 59 | + public static void UnregisterDriver(string name) |
| 60 | + { |
| 61 | + if (RegisteredDrivers.Remove(name, out var driver) && driver == DefaultDriver) |
| 62 | + { |
| 63 | + DefaultDriver = null; |
| 64 | + DefaultDriverName = null; |
| 65 | + } |
| 66 | + DriversChanged?.Invoke(null, EventArgs.Empty); |
| 67 | + } |
| 68 | + |
| 69 | + public static IMenu CreateMenu(CCSPlayerController player, CancellationToken ct = default) |
| 70 | + { |
| 71 | + if (DefaultDriver is null) |
| 72 | + throw new InvalidOperationException("No default driver has been registered. Do you have a driver installed and/or configured?"); |
| 73 | + return DefaultDriver.CreateMenu(player, ct); |
| 74 | + } |
| 75 | + |
| 76 | + public static IMenu CreateMenu(IMenu parent, CancellationToken ct = default) |
| 77 | + { |
| 78 | + if (DefaultDriver is null) |
| 79 | + throw new InvalidOperationException("No default driver has been registered. Do you have a driver installed and/or configured?"); |
| 80 | + return DefaultDriver.CreateMenu(parent, ct); |
| 81 | + } |
| 82 | +} |
0 commit comments