From 10d2749d226adc20cafbc315dd3a079246b37b3d Mon Sep 17 00:00:00 2001 From: MSWS Date: Sun, 8 Sep 2024 04:28:26 -0700 Subject: [PATCH] Final small integration fixes --- src/CS2/Commands/CommandManager.cs | 5 ++- src/CS2/Commands/GangCommand.cs | 12 +++++- src/CS2/Gangs/CS2Gangs.cs | 6 ++- src/CS2/Gangs/GangServiceCollection.cs | 2 +- src/CS2/Gangs/PluginStringLocalizer.cs | 38 +++++++++++++++++++ .../Data/Command/CommandInfoWrapper.cs | 10 +++-- 6 files changed, 65 insertions(+), 8 deletions(-) create mode 100644 src/CS2/Gangs/PluginStringLocalizer.cs diff --git a/src/CS2/Commands/CommandManager.cs b/src/CS2/Commands/CommandManager.cs index 9f5894b..be516b2 100644 --- a/src/CS2/Commands/CommandManager.cs +++ b/src/CS2/Commands/CommandManager.cs @@ -14,14 +14,17 @@ namespace Commands; public class CommandManager(IGangManager gangMgr, IStringLocalizer locale) : MockCommandManager(locale), IPluginBehavior { private BasePlugin? plugin; + private bool hotReload; public void Start(BasePlugin? basePlugin, bool hotReload) { - plugin = basePlugin; + plugin = basePlugin; + this.hotReload = hotReload; RegisterCommand(new GangCommand(gangMgr, Locale)); } public override bool RegisterCommand(ICommand command) { + command.Start(plugin, hotReload); var registration = base.RegisterCommand(command); if (registration == false) return false; foreach (var alias in command.Aliases) diff --git a/src/CS2/Commands/GangCommand.cs b/src/CS2/Commands/GangCommand.cs index 0765fd7..4642b3b 100644 --- a/src/CS2/Commands/GangCommand.cs +++ b/src/CS2/Commands/GangCommand.cs @@ -1,4 +1,5 @@ using Commands.Gang; +using CounterStrikeSharp.API.Core; using CounterStrikeSharp.API.Modules.Utils; using GangsAPI; using GangsAPI.Data; @@ -27,6 +28,13 @@ public class GangCommand(IGangManager gangMgr, IStringLocalizer locale) ["create"] = new CreateCommand(gangMgr), ["help"] = new HelpCommand() }; + private IStringLocalizer myLocale = locale; + + public void Start(BasePlugin? plugin, bool hotReload) { + Console.WriteLine("GangCommand loaded with plugin: " + plugin); + if (plugin != null) myLocale = plugin?.Localizer ?? myLocale; + } + public string Name => "css_gang"; public string Description => "Master command for gangs"; public string[] Aliases => ["css_gang", "css_gangs"]; @@ -41,7 +49,7 @@ public async Task Execute(PlayerWrapper? executor, $"Attempted to execute GangCommand with invalid name: {info[0]}"); if (executor?.Player != null) { - info.ReplySync(locale.Get(SOONTM)); + info.ReplySync(myLocale.Get(SOONTM)); return CommandResult.SUCCESS; } @@ -50,7 +58,7 @@ public async Task Execute(PlayerWrapper? executor, var gang = await gangMgr.GetGang(executor.Steam); if (gang == null) { - info.ReplySync(locale[NOT_IN_GANG.Key()]); + info.ReplySync(myLocale[NOT_IN_GANG.Key()]); return CommandResult.SUCCESS; } diff --git a/src/CS2/Gangs/CS2Gangs.cs b/src/CS2/Gangs/CS2Gangs.cs index c07ac45..36ad07a 100644 --- a/src/CS2/Gangs/CS2Gangs.cs +++ b/src/CS2/Gangs/CS2Gangs.cs @@ -2,6 +2,7 @@ using CounterStrikeSharp.API.Core; using GangsAPI; using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Localization; using Microsoft.Extensions.Logging; namespace GangsImpl; @@ -21,10 +22,13 @@ public override void Load(bool hotReload) { Logger.LogInformation("[Gangs] Loading {Count} extensions", extensions.Count); + // Override the default localizer with our own + Localizer = new PluginStringLocalizer(Localizer); + foreach (var ext in extensions) { RegisterAllAttributes(ext); try { - Logger.LogDebug("Loading {@Name}", ext.GetType().FullName); + Logger.LogInformation("Loading {@Name}", ext.GetType().FullName); ext.Start(this, hotReload); } catch (Exception e) { Logger.LogError(e, "Failed to load {@Name}", ext.GetType().FullName); diff --git a/src/CS2/Gangs/GangServiceCollection.cs b/src/CS2/Gangs/GangServiceCollection.cs index 9f1d458..9c488a5 100644 --- a/src/CS2/Gangs/GangServiceCollection.cs +++ b/src/CS2/Gangs/GangServiceCollection.cs @@ -6,6 +6,7 @@ using GangsAPI.Services.Gang; using GangsAPI.Services.Player; using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Localization; using Mock; namespace GangsImpl; @@ -20,6 +21,5 @@ public void ConfigureServices(IServiceCollection serviceCollection) { serviceCollection .AddPluginBehavior(); serviceCollection.RegisterCommands(); - serviceCollection.AddPluginBehavior(); } } \ No newline at end of file diff --git a/src/CS2/Gangs/PluginStringLocalizer.cs b/src/CS2/Gangs/PluginStringLocalizer.cs new file mode 100644 index 0000000..38f2e13 --- /dev/null +++ b/src/CS2/Gangs/PluginStringLocalizer.cs @@ -0,0 +1,38 @@ +using System.Reflection; +using System.Text.RegularExpressions; +using Microsoft.Extensions.Localization; + +namespace GangsImpl; + +public partial class PluginStringLocalizer(IStringLocalizer localizer) + : IStringLocalizer { + public LocalizedString this[string name] => GetString(name); + + public LocalizedString this[string name, params object[] arguments] + => new(name, string.Format(GetString(name).Value, arguments)); + + public IEnumerable + GetAllStrings(bool includeParentCultures) { + return localizer.GetAllStrings(includeParentCultures) + .Select(str => GetString(str.Name)); + } + + private LocalizedString GetString(string name) { + // Replace %[key]% with that key's value + // Eg: if we have a locale key of "prefix", then + // other locale values can use %prefix% to reference it. + var value = localizer[name].Value; + var matches = Percents().Matches(value); + foreach (Match match in matches) + // Check if the key exists + try { + var key = match.Groups[0].Value; + value = value.Replace(key, localizer[key[1..^1]].Value); + } catch (NullReferenceException) { } + + return new LocalizedString(name, value); + } + + [GeneratedRegex(@"%.*?%")] + private static partial Regex Percents(); +} \ No newline at end of file diff --git a/src/GangsAPI/Data/Command/CommandInfoWrapper.cs b/src/GangsAPI/Data/Command/CommandInfoWrapper.cs index e6464a9..76b67d7 100644 --- a/src/GangsAPI/Data/Command/CommandInfoWrapper.cs +++ b/src/GangsAPI/Data/Command/CommandInfoWrapper.cs @@ -17,13 +17,17 @@ public CommandInfoWrapper(PlayerWrapper? executor, int offset = 0, this.offset = offset; this.args = args; + Console.WriteLine($"CommandInfoWrapper: {CallingPlayer} {offset} {args}"); if (offset == 0 && args.Length > 0) this.args[0] = args[0].ToLower(); } - public CommandInfoWrapper(CommandInfo info, int offset = 0) : this( - info.CallingPlayer == null ? null : new PlayerWrapper(info.CallingPlayer), - offset, new string[info.ArgCount]) { + public CommandInfoWrapper(CommandInfo info, int offset = 0) { + CallingPlayer = info.CallingPlayer == null ? + null : + new PlayerWrapper(info.CallingPlayer); + this.offset = offset; CallingContext = info.CallingContext; + args = new string[info.ArgCount]; for (var i = 0; i < info.ArgCount; i++) args[i] = info.GetArg(i); if (offset == 0 && info.ArgCount > 0) args[0] = info.GetArg(0).ToLower(); }