Skip to content

Commit

Permalink
Update scripts and such
Browse files Browse the repository at this point in the history
  • Loading branch information
Limiana committed Aug 3, 2024
1 parent 8fb42d3 commit 443ae37
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 48 deletions.
2 changes: 2 additions & 0 deletions Splatoon/Splatoon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ public unsafe class Splatoon : IDalamudPlugin
internal ClipZoneSelector ClipZoneSelector;
public NotificationMasterApi NotificationMasterApi;
public Archive Archive;
private ActorControlProcessor ActorControlProcessor;

internal void Load(IDalamudPluginInterface pluginInterface)
{
Expand Down Expand Up @@ -185,6 +186,7 @@ internal void Load(IDalamudPluginInterface pluginInterface)
NotificationMasterApi = new(pluginInterface);
SingletonServiceManager.Initialize(typeof(S));
Archive = EzConfig.LoadConfiguration<Archive>("Archive.json");
ActorControlProcessor = new ActorControlProcessor();
Init = true;
SplatoonIPC.Init();
}
Expand Down
51 changes: 51 additions & 0 deletions Splatoon/SplatoonScripting/Controller.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ public unsafe class Controller
internal SplatoonScript Script;
internal Dictionary<string, Layout> Layouts = new();
internal Dictionary<string, Element> Elements = new();
internal List<TickScheduler> TickSchedulers = [];
internal IEzConfig? Configuration;
internal long AutoResetAt = long.MaxValue;

internal int autoIncrement = 0;
internal int AutoIncrement => ++autoIncrement;
Expand Down Expand Up @@ -296,4 +298,53 @@ public void SaveOverrides()
}
}
}

/// <summary>
/// Resets the state of the script, calling your OnReset method AND performing additional cleanup tasks.
/// </summary>
public void Reset()
{
ScriptingProcessor.OnReset(this.Script);
this.CancelSchedulers();
}

/// <summary>
/// Schedules a task to be executed in specified amount of time. All tasks are cleaned up when script is reset.
/// </summary>
/// <param name="action"></param>
/// <param name="delayMs"></param>
public void Schedule(Action action, int delayMs)
{
TickSchedulers.Add(new(() =>
{
try
{
action();
}
catch(Exception ex)
{
ScriptingProcessor.LogError(this.Script, ex, nameof(Schedule));
}
TickSchedulers.RemoveAll(x => x.Disposed);
}, delayMs));
}

/// <summary>
/// Cancels all scheduled tasks. Auto-called upon calling Reset.
/// </summary>
public void CancelSchedulers()
{
PluginLog.Debug($"CancelSchedulers called for script {this.Script.InternalData.Name}");
TickSchedulers.Each(x => x.Dispose());
TickSchedulers.Clear();
}

/// <summary>
/// Schedules reset of script after specified amount of miliseconds have passed.
/// </summary>
/// <param name="delayMs"></param>
public void ScheduleReset(uint delayMs = uint.MaxValue)
{
AutoResetAt = Environment.TickCount64 + delayMs;
}
}
20 changes: 16 additions & 4 deletions Splatoon/SplatoonScripting/ScriptingProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -359,15 +359,22 @@ internal static void CompileAndLoad(string sourceCode, string fpath)

internal static void OnUpdate()
{
var tickCount = Environment.TickCount64;
for (var i = 0; i < Scripts.Count; i++)
{
if (Scripts[i].IsEnabled)
{
var script = Scripts[i];
try
{
Scripts[i].OnUpdate();
script.OnUpdate();
}
catch (Exception e) { Scripts[i].LogError(e, nameof(SplatoonScript.OnUpdate)); }
if(tickCount > script.Controller.AutoResetAt)
{
PluginLog.Debug($"Resetting script {script.InternalData.Name} because of timer");
OnReset(script);
}
}
}
}
Expand Down Expand Up @@ -404,13 +411,18 @@ internal static void OnCombatEnd()
}
}

internal static void OnReset(int i)
internal static void OnReset(int i) => OnReset(Scripts[i]);

internal static void OnReset(SplatoonScript script)
{
try
{
Scripts[i].OnReset();
PluginLog.Debug($"OnReset called for script {script.InternalData.Name}");
script.Controller.CancelSchedulers();
script.OnReset();
script.Controller.AutoResetAt = long.MaxValue;
}
catch(Exception e) { Scripts[i].LogError(e, nameof(SplatoonScript.OnReset)); }
catch(Exception e) { script.LogError(e, nameof(SplatoonScript.OnReset)); }
}

internal static void OnMapEffect(uint Position, ushort Param1, ushort Param2)
Expand Down
12 changes: 3 additions & 9 deletions Splatoon/SplatoonScripting/SplatoonScript.cs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ public virtual void OnActionEffectEvent(ActionEffectSet set) { }
public virtual void OnUpdate() { }

/// <summary>
/// Called BEFORE director update indicates commence, recommence or wipe; combat start; combat end.<br></br>
/// Called BEFORE director update indicates commence, recommence or wipe; combat start; combat end; disabling.<br></br>
/// Also called AFTER enabling.<br></br>
/// You can put cleanup here.
/// </summary>
Expand Down Expand Up @@ -290,14 +290,7 @@ internal bool Enable()
ScriptingProcessor.LogError(this, ex, nameof(Enable));
}
this.IsEnabled = true;
try
{
this.OnReset();
}
catch(Exception ex)
{
ScriptingProcessor.LogError(this, ex, nameof(OnReset));
}
ScriptingProcessor.OnReset(this);
return true;
}

Expand All @@ -308,6 +301,7 @@ internal bool Disable()
{
return false;
}
ScriptingProcessor.OnReset(this);
try
{
PluginLog.Information($"Disabling script {this}");
Expand Down
43 changes: 8 additions & 35 deletions SplatoonScripts/Duties/Dawntrail/R4S Midnight Sabbath.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,10 @@ namespace SplatoonScriptsOfficial.Duties.Dawntrail;
internal class R4S_Midnight_Sabbath: SplatoonScript
{
public override HashSet<uint> ValidTerritories => new() { 1232 };
public override Metadata? Metadata => new(2, "Fragile");

public override Metadata? Metadata => new(1, "Fragile");

private delegate void ProcessPacketActorControlDelegate(uint actorID, uint category, uint p1, uint p2, uint p3, uint p4, uint p5, uint p6, ulong targetID, byte replaying);
private static Hook<ProcessPacketActorControlDelegate>? ProcessPacketActorControlHook;

public override void OnEnable()
{
ProcessPacketActorControlHook = Svc.Hook.HookFromSignature<ProcessPacketActorControlDelegate>("E8 ?? ?? ?? ?? 0F B7 0B 83 E9 64", ProcessPacketActorControlDetour);
ProcessPacketActorControlHook?.Enable();
}

private void ProcessPacketActorControlDetour(uint actorID, uint category, uint p1, uint p2, uint p3, uint p4, uint p5, uint p6, ulong targetID, byte replaying)
public override void OnActorControl(uint actorID, uint category, uint p1, uint p2, uint p3, uint p4, uint p5, uint p6, ulong targetId, byte replaying)
{
ProcessPacketActorControlHook!.Original(actorID, category, p1, p2, p3, p4, p5, p6, targetID, replaying);
if (category == 407)
{
var obj = Svc.Objects.SearchById(actorID);
Expand All @@ -53,19 +42,13 @@ private void ProcessPacketActorControlDetour(uint actorID, uint category, uint p
private void FirstLine(IGameObject obj)
{
Controller.RegisterElementFromCode(obj.EntityId.ToString(), $"{{\"Name\":\"\",\"type\":3,\"refY\":30.0,\"radius\":5.0,\"fillIntensity\":0.345,\"originFillColor\":1157628159,\"endFillColor\":1157628159,\"refActorObjectID\":{obj.EntityId},\"refActorUseCastTime\":true,\"refActorCastTimeMax\":5.0,\"refActorUseOvercast\":true,\"refActorComparisonType\":2,\"includeRotation\":true,\"onlyVisible\":true,\"refActorTetherTimeMin\":0.0,\"refActorTetherTimeMax\":0.0,\"refActorUseTransformation\":true,\"refActorTetherConnectedWithPlayer\":[],\"refActorTransformationID\":7}}");
_ = new TickScheduler(delegate
{
Controller.ClearRegisteredElements();
}, 20000);
Controller.ScheduleReset(20000);
}

private void FirstWing(IGameObject obj)
{
Controller.RegisterElementFromCode(obj.EntityId.ToString(), $"{{\"Name\":\"\",\"type\":1,\"radius\":5.0,\"Donut\":10.0,\"fillIntensity\":0.345,\"originFillColor\":1157628159,\"endFillColor\":1157628159,\"refActorObjectID\":{obj.EntityId},\"refActorUseCastTime\":true,\"refActorCastTimeMax\":5.0,\"refActorUseOvercast\":true,\"refActorComparisonType\":2,\"includeRotation\":true,\"onlyVisible\":true,\"refActorTetherTimeMin\":0.0,\"refActorTetherTimeMax\":0.0,\"refActorUseTransformation\":true,\"refActorTetherConnectedWithPlayer\":[],\"refActorTransformationID\":31}}");
_ = new TickScheduler(delegate
{
Controller.ClearRegisteredElements();
}, 20000);
Controller.ScheduleReset(20000);
}

private void AfterLine(IGameObject obj)
Expand All @@ -74,10 +57,7 @@ private void AfterLine(IGameObject obj)
{
Controller.RegisterElementFromCode(obj.EntityId.ToString(), $"{{\"Name\":\"\",\"type\":3,\"refY\":30.0,\"radius\":5.0,\"fillIntensity\":0.345,\"originFillColor\":1157628159,\"endFillColor\":1157628159,\"refActorObjectID\":{obj.EntityId},\"refActorUseCastTime\":true,\"refActorCastTimeMax\":5.0,\"refActorUseOvercast\":true,\"refActorComparisonType\":2,\"includeRotation\":true,\"onlyVisible\":true,\"refActorTetherTimeMin\":0.0,\"refActorTetherTimeMax\":0.0,\"refActorUseTransformation\":true,\"refActorTetherConnectedWithPlayer\":[],\"refActorTransformationID\":7}}");
}, 8000);
_ = new TickScheduler(delegate
{
Controller.ClearRegisteredElements();
}, 20000);
Controller.ScheduleReset(20000);
}

private void AfterWing(IGameObject obj)
Expand All @@ -86,18 +66,11 @@ private void AfterWing(IGameObject obj)
{
Controller.RegisterElementFromCode(obj.EntityId.ToString(), $"{{\"Name\":\"\",\"type\":1,\"radius\":5.0,\"Donut\":10.0,\"fillIntensity\":0.345,\"originFillColor\":1157628159,\"endFillColor\":1157628159,\"refActorObjectID\":{obj.EntityId},\"refActorUseCastTime\":true,\"refActorCastTimeMax\":5.0,\"refActorUseOvercast\":true,\"refActorComparisonType\":2,\"includeRotation\":true,\"onlyVisible\":true,\"refActorTetherTimeMin\":0.0,\"refActorTetherTimeMax\":0.0,\"refActorUseTransformation\":true,\"refActorTetherConnectedWithPlayer\":[],\"refActorTransformationID\":31}}");
}, 8000);
_ = new TickScheduler(delegate
{
Controller.ClearRegisteredElements();
}, 20000);

Controller.ScheduleReset(20000);
}


public override void OnDisable()
public override void OnReset()
{
ProcessPacketActorControlHook?.Dispose();
Controller.ClearRegisteredElements();
}


}

0 comments on commit 443ae37

Please sign in to comment.