Plugins - Moved test data model to a seperate plugin

Core - Moved built-in types to a separate folder and namespace
This commit is contained in:
Robert committed 2020-09-16 19:22:37 +02:00
1 parent d788183f25
commit d9017c67b4
59 files changed
+350 -131

No files matched your search

+10 -10
View File
@@ -165,16 +165,6 @@ namespace Artemis.Core.Modules
/// </param>
public abstract void ModuleDeactivated(bool isOverride);
internal virtual void InternalUpdate(double deltaTime)
{
Update(deltaTime);
}
internal virtual void InternalRender(double deltaTime, ArtemisSurface surface, SKCanvas canvas, SKImageInfo canvasInfo)
{
Render(deltaTime, surface, canvas, canvasInfo);
}
/// <summary>
/// Evaluates the activation requirements following the <see cref="ActivationRequirementMode" /> and returns the result
/// </summary>
@@ -190,6 +180,16 @@ namespace Artemis.Core.Modules
return false;
}
internal virtual void InternalUpdate(double deltaTime)
{
Update(deltaTime);
}
internal virtual void InternalRender(double deltaTime, ArtemisSurface surface, SKCanvas canvas, SKImageInfo canvasInfo)
{
Render(deltaTime, surface, canvas, canvasInfo);
}
internal virtual void Activate(bool isOverride)
{
+14
View File
@@ -39,6 +39,20 @@ namespace Artemis.Core
/// </summary>
public abstract void DisablePlugin();
/// <summary>
/// Registers a timed update that whenever the plugin is enabled calls the provided <paramref name="action" /> at the provided
/// <paramref name="interval" />
/// </summary>
/// <param name="interval">The interval at which the update should occur</param>
/// <param name="action">The action to call every time the interval has passed. The delta time parameter represents the time passed since the last update in seconds</param>
/// <returns>The resulting plugin update registration</returns>
public PluginUpdateRegistration AddTimedUpdate(TimeSpan interval, Action<double> action)
{
if (action == null)
throw new ArgumentNullException(nameof(action));
return new PluginUpdateRegistration(PluginInfo, interval, action);
}
internal void SetEnabled(bool enable, bool isAutoEnable = false)
{
if (enable && !Enabled)
@@ -0,0 +1,102 @@
using System;
using System.Timers;
namespace Artemis.Core
{
/// <summary>
/// Represents a registration for a timed plugin update
/// </summary>
public class PluginUpdateRegistration
{
private DateTime _lastEvent;
private Timer _timer;
internal PluginUpdateRegistration(PluginInfo pluginInfo, TimeSpan interval, Action<double> action)
{
PluginInfo = pluginInfo;
Interval = interval;
Action = action;
PluginInfo.Instance.PluginEnabled += InstanceOnPluginEnabled;
PluginInfo.Instance.PluginDisabled += InstanceOnPluginDisabled;
if (PluginInfo.Instance.Enabled)
Start();
}
/// <summary>
/// Gets the plugin info of the plugin this registration is associated with
/// </summary>
public PluginInfo PluginInfo { get; }
/// <summary>
/// Gets the interval at which the update should occur
/// </summary>
public TimeSpan Interval { get; }
/// <summary>
/// Gets the action that gets called each time the update event fires
/// </summary>
public Action<double> Action { get; }
/// <summary>
/// Starts calling the <see cref="Action" /> at the configured <see cref="Interval" />
/// <para>Note: Called automatically when the plugin enables</para>
/// </summary>
public void Start()
{
lock (this)
{
if (!PluginInfo.Instance.Enabled)
throw new ArtemisPluginException("Cannot start a timed update for a disabled plugin");
if (_timer != null)
return;
_lastEvent = DateTime.Now;
_timer = new Timer(Interval.TotalMilliseconds);
_timer.Elapsed += TimerOnElapsed;
_timer.Start();
}
}
/// <summary>
/// Stops calling the <see cref="Action" /> at the configured <see cref="Interval" />
/// <para>Note: Called automatically when the plugin disables</para>
/// </summary>
public void Stop()
{
lock (this)
{
if (_timer == null)
return;
_timer.Elapsed -= TimerOnElapsed;
_timer.Stop();
_timer.Dispose();
_timer = null;
}
}
private void TimerOnElapsed(object sender, ElapsedEventArgs e)
{
if (!PluginInfo.Instance.Enabled)
return;
var interval = DateTime.Now - _lastEvent;
_lastEvent = DateTime.Now;
Action(interval.TotalSeconds);
}
private void InstanceOnPluginEnabled(object? sender, EventArgs e)
{
Start();
}
private void InstanceOnPluginDisabled(object? sender, EventArgs e)
{
Stop();
}
}
}