Skip to content

Commit

Permalink
Finalize config update event, add hot-reloading notifications to whit…
Browse files Browse the repository at this point in the history
…elist
  • Loading branch information
Mooshua committed Aug 27, 2023
1 parent 6c145c9 commit 3ce65ab
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 11 deletions.
15 changes: 14 additions & 1 deletion builtin/BitMod.Whitelist/Hooks/WhitelistHooks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
using BitMod.Attributes.Injects;
using BitMod.Attributes.Targets;
using BitMod.Configuration.Model;
using BitMod.Events.Config;
using BitMod.Events.Meta;
using BitMod.Events.Server;
using BitMod.Plugins.Events;
using BitMod.Whitelist.Adapter;
Expand All @@ -16,14 +18,25 @@ namespace BitMod.Whitelist.Hooks;
public class WhitelistHooks
{



[Config("whitelist")]
private IConfigObject _whitelist;

[Singleton]
private ILogger _logger;

[BitConfigUpdate("whitelist")]
public async Task ServerConfigUpdate(ConfigUpdatedEventArgs ev)
{
var config = new WhitelistConfigAdapter(ev.Config);

_logger.Information("Updated whitelist with new allowed IPs: {@List}",
config.GetAllowedAddresses().Select(x => x.ToString()));
}

[BitHook(Priority.LOW)]
private async Task<Directive> ServerConnectionRequest(GameServerConnectingEventArgs ev)
public async Task<Directive> ServerConnectionRequest(GameServerConnectingEventArgs ev)
{
var config = new WhitelistConfigAdapter(_whitelist);

Expand Down
7 changes: 5 additions & 2 deletions standalone/BitMod.Config/Cache/ConfigCache.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Security;

using BitMod.Configuration.Model;
using BitMod.Internal.Public;

using Serilog;

Expand All @@ -15,20 +16,22 @@ public class ConfigCache
private KVSerializer _serializer;
private KVSerializerOptions _serializerOptions;
private Dictionary<string, ConfigWatcher> _watchers = new();
private PluginInvoker _invoker;

public ConfigCache(KVSerializerOptions serializerOptions, KVSerializer serializer, ILogger logger)
public ConfigCache(KVSerializerOptions serializerOptions, KVSerializer serializer, ILogger logger, PluginInvoker invoker)
{
_serializerOptions = serializerOptions;
_serializer = serializer;
_logger = logger;
_invoker = invoker;
}

public ConfigWatcher GetWatcher(string file)
{
if (_watchers.TryGetValue(file, out var watcher))
return watcher;

_watchers[file] = new ConfigWatcher(file, _logger, _serializer, _serializerOptions);
_watchers[file] = new ConfigWatcher(file, _logger, _serializer, _serializerOptions, _invoker);
return _watchers[file];
}

Expand Down
9 changes: 7 additions & 2 deletions standalone/BitMod.Config/Cache/ConfigWatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

using BitMod.Config.Implementation;
using BitMod.Configuration.Model;
using BitMod.Internal.Public;

using Serilog;

Expand All @@ -24,19 +25,22 @@ public class ConfigWatcher

private ILogger _logger;

private PluginInvoker _invoker;

private KVSerializer _serializer;

private KVSerializerOptions _serializerOptions;

private FileSystemWatcher _watcher;

public ConfigWatcher(string configFileName, ILogger logger, KVSerializer serializer, KVSerializerOptions serializerOptions)
public ConfigWatcher(string configFileName, ILogger logger, KVSerializer serializer, KVSerializerOptions serializerOptions, PluginInvoker invoker)
{
_configFileName = configFileName;
_configFilePath = Path.Join(System.Environment.CurrentDirectory, CONFIG_PATH, $"{_configFileName}.{CONFIG_EXT}");
_logger = logger;
_serializer = serializer;
_serializerOptions = serializerOptions;
_invoker = invoker;
// Create an empty config object
_configObject = new ConfigObject(
new KVObject(_configFileName, Enumerable.Empty<KVObject>()));
Expand All @@ -59,6 +63,8 @@ private void OnChange(object sender, FileSystemEventArgs args)
{
_logger.Information("[BitMod Config] Reloading config file {@Name} due to update.", _configFileName);
TryRead();

_invoker.ConfigUpdate(_configFileName, _configObject);
}


Expand All @@ -69,7 +75,6 @@ private void TryRead()
using (var reader = File.OpenRead(_configFilePath))
{
var kvObject = _serializer.Deserialize(reader, _serializerOptions);

_configObject = new ConfigObject(kvObject);
}
}
Expand Down
2 changes: 1 addition & 1 deletion standalone/BitMod.Config/ConfigurationSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public void Start(BitMod env)
_logger = _parent.Logger;

// Set up the cache
_parent.Store(new ConfigCache(Options, Serializer, _logger));
_parent.Store(new ConfigCache(Options, Serializer, _logger, _parent.Invoker));
}

public IConfigObject Get(string name)
Expand Down
9 changes: 4 additions & 5 deletions standalone/BitMod.Launcher/configs/whitelist.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@
// default handler. Note that if you do not specify any here,
// other plugins will still be able to accept connections.
// Plugins can also override this list, and reject connections listed here.
"AllowedIPs"
{
// Format: "entryNumber" "ipAddress"
"0" "127.0.0.1"
}

// Format: "entryNumber" "ipAddress"
"0" "127.0.0.1"

}

0 comments on commit 3ce65ab

Please sign in to comment.