Skip to content

Commit

Permalink
Final small integration fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
MSWS committed Sep 8, 2024
1 parent 5efc6cb commit 10d2749
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 8 deletions.
5 changes: 4 additions & 1 deletion src/CS2/Commands/CommandManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
12 changes: 10 additions & 2 deletions src/CS2/Commands/GangCommand.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Commands.Gang;
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Modules.Utils;
using GangsAPI;
using GangsAPI.Data;
Expand Down Expand Up @@ -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"];
Expand All @@ -41,7 +49,7 @@ public async Task<CommandResult> 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;
}

Expand All @@ -50,7 +58,7 @@ public async Task<CommandResult> 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;
}

Expand Down
6 changes: 5 additions & 1 deletion src/CS2/Gangs/CS2Gangs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using CounterStrikeSharp.API.Core;
using GangsAPI;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Localization;
using Microsoft.Extensions.Logging;

namespace GangsImpl;
Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/CS2/Gangs/GangServiceCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using GangsAPI.Services.Gang;
using GangsAPI.Services.Player;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Localization;
using Mock;

namespace GangsImpl;
Expand All @@ -20,6 +21,5 @@ public void ConfigureServices(IServiceCollection serviceCollection) {
serviceCollection
.AddPluginBehavior<IPlayerStatManager, MockInstanceStatManager>();
serviceCollection.RegisterCommands();
serviceCollection.AddPluginBehavior<ICommandManager, CommandManager>();
}
}
38 changes: 38 additions & 0 deletions src/CS2/Gangs/PluginStringLocalizer.cs
Original file line number Diff line number Diff line change
@@ -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<LocalizedString>
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();
}
10 changes: 7 additions & 3 deletions src/GangsAPI/Data/Command/CommandInfoWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down

0 comments on commit 10d2749

Please sign in to comment.