Refactor the handling of date time to no longer depend on a static
Build / Build (push) Successful in 10m13s

This commit is contained in:
Deukhoofd committed 2026-11-14 21:46:12 +01:00
1 parent 4c51e67985
commit 7649be8294
11 files changed
+53 -39

No files matched your search

+2 -2
View File
@@ -68,9 +68,9 @@ public class DynamicLibraryImpl : IDynamicLibrary
/// Initializes a new instance of the <see cref="DynamicLibraryImpl"/> class, with the given
/// plugins and static library.
/// </summary>
public static IDynamicLibrary Create(IEnumerable<IPlugin> plugins)
public static IDynamicLibrary Create(IEnumerable<IPlugin> plugins, PluginEnvironmentHelpers helpers)
{
var load = LibraryLoader.LoadPlugins(plugins);
var load = LibraryLoader.LoadPlugins(plugins, helpers);
return new DynamicLibraryImpl(load.StaticLibrary, load.Registry.BattleStatCalculator!,
load.Registry.DamageCalculator!, load.Registry.MiscLibrary!, load.Registry.CaptureLibrary!, load.Resolver,
+2 -2
View File
@@ -19,14 +19,14 @@ public static class LibraryLoader
/// <summary>
/// Loads plugins and creates a static library from them.
/// </summary>
public static LoadResult LoadPlugins(IEnumerable<IPlugin> plugins)
public static LoadResult LoadPlugins(IEnumerable<IPlugin> plugins, PluginEnvironmentHelpers helpers)
{
var registry = new ScriptRegistry();
var orderedPlugins = plugins.OrderBy(x => x.LoadOrder).ToList();
var staticLibrary = CreateStaticLibrary(orderedPlugins);
foreach (var plugin in orderedPlugins)
{
plugin.Register(registry);
plugin.Register(registry, helpers);
}
if (registry.DamageCalculator is null)
throw new InvalidOperationException("Damage calculator not found in plugins.");
+2 -2
View File
@@ -26,7 +26,7 @@ public interface IPlugin
/// <summary>
/// Run the registration of the plugin when we're building the library.
/// </summary>
void Register(ScriptRegistry registry);
void Register(ScriptRegistry registry, PluginEnvironmentHelpers helpers);
}
/// <inheritdoc cref="IPlugin"/>
@@ -59,7 +59,7 @@ public abstract class Plugin<TConfiguration> : IPlugin where TConfiguration : IP
/// <summary>
/// Run the registration of the plugin when we're building the library.
/// </summary>
public abstract void Register(ScriptRegistry registry);
public abstract void Register(ScriptRegistry registry, PluginEnvironmentHelpers helpers);
}
/// <summary>
@@ -0,0 +1,18 @@
namespace PkmnLib.Dynamic.Plugins;
/// <summary>
/// Helper functions to get information about the environment we're running in, such as the current date time.
/// </summary>
public class PluginEnvironmentHelpers
{
/// <summary>
/// A delegate that provides the current date and time.
/// </summary>
public delegate DateTimeOffset DateTimeProviderDelegate();
/// <summary>
/// A function to get the current date and time. This can be replaced in cases where the date and time
/// may not be the same as the system time.
/// </summary>
public required DateTimeProviderDelegate DateTimeProvider { get; init; }
}
-24
View File
@@ -1,24 +0,0 @@
namespace PkmnLib.Dynamic;
/// <summary>
/// A static helper class that contains functions that are used in multiple places.
/// </summary>
public static class StaticHelpers
{
/// <summary>
/// A delegate that provides the current date and time.
/// </summary>
public delegate DateTimeOffset DateTimeProviderDelegate();
/// <summary>
/// A function to get the current date and time. This can be replaced in cases where the date and time
/// may not be the same as the system time.
/// </summary>
[PublicAPI]
public static DateTimeProviderDelegate DateTimeProvider { get; set; } = () => DateTimeOffset.Now;
/// <summary>
/// Get the current date and time.
/// </summary>
public static DateTimeOffset GetCurrentDateTime() => DateTimeProvider();
}