diff --git a/Directory.Build.props b/Directory.Build.props index a42f4fb..b0733cc 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -12,6 +12,6 @@ It should follow the format major.minor.patch (semantic versioning). If you publish your mod as a library to NuGet, this version will also be used as the package version. --> - 0.3.7 + 0.3.8 diff --git a/README.md b/README.md index 250a31a..f98e83d 100644 --- a/README.md +++ b/README.md @@ -6,13 +6,13 @@ This is a library for mods to make FSM edits easier. To add Silksong.FsmUtil to your mod, add the following line to your .csproj: ``` - + ``` The most up to date version number can be retrieved from [Nuget](https://www.nuget.org/packages/Silksong.FsmUtil). You will also need to add a dependency to your thunderstore.toml: ``` -silksong_modding-FsmUtil = "0.3.7" +silksong_modding-FsmUtil = "0.3.8" ``` The version number does not matter hugely, but the most up to date number can be retrieved from [Thunderstore](https://thunderstore.io/c/hollow-knight-silksong/p/silksong_modding/FsmUtil/). diff --git a/docs/index.md b/docs/index.md index 8f05dfa..f057abc 100644 --- a/docs/index.md +++ b/docs/index.md @@ -12,13 +12,13 @@ With this, it's easy to: To add Silksong.FsmUtil to your mod, add the following line to your .csproj: ``` - + ``` The most up to date version number can be retrieved from [Nuget](https://www.nuget.org/packages/Silksong.FsmUtil). You will also need to add a dependency to your thunderstore.toml: ``` -silksong_modding-FsmUtil = "0.3.7" +silksong_modding-FsmUtil = "0.3.8" ``` The version number does not matter hugely, but the most up to date number can be retrieved from [Thunderstore](https://thunderstore.io/c/hollow-knight-silksong/p/silksong_modding/FsmUtil/). diff --git a/src/Actions/LambdaAction.cs b/src/Actions/LambdaAction.cs index 8df4e3b..f5779ff 100644 --- a/src/Actions/LambdaAction.cs +++ b/src/Actions/LambdaAction.cs @@ -6,21 +6,56 @@ namespace Silksong.FsmUtil.Actions /// /// An action that executes a single zero-argument function, then exits. /// - /// The function to invoke. - /// If true, execute the function repeatedly on every update frame. - public class LambdaAction(Action action, bool everyFrame = false) : FsmStateAction + public class LambdaAction : FsmStateAction { - /// + /// + /// The method to invoke. + /// + public Action? Method; + + /// + /// If true, execute the function repeatedly on every update frame. + /// + public bool EveryFrame = false; + + /// + /// Resets the action. + /// + public override void Reset() + { + Method = null; + EveryFrame = false; + base.Reset(); + } + + /// + /// Called when the action is being processed. + /// public override void OnEnter() { - action?.Invoke(); - if (!everyFrame) Finish(); + if (Method != null) + { + Method.Invoke(); + } + + if (!EveryFrame) + { + Finish(); + } } - /// + /// + /// Called every update frame. + /// public override void OnUpdate() { - if (everyFrame) action?.Invoke(); + if (EveryFrame) + { + if (Method != null) + { + Method.Invoke(); + } + } } } } diff --git a/src/FsmUtil.cs b/src/FsmUtil.cs index 905bb6e..8fe41f7 100644 --- a/src/FsmUtil.cs +++ b/src/FsmUtil.cs @@ -453,16 +453,19 @@ public static void AddActions(this FsmState state, params FsmStateAction[] actio /// The name of the state in which the method is added /// The method that will be invoked with the action as the parameter [PublicAPI] + [Obsolete("In a future update, the method here will no longer take a parameter. Please use the `AddLambdaMethod` extension method instead! (Though that one's parameter is the `Finish` method.)")] public static void AddMethod(this PlayMakerFSM fsm, string stateName, Action method) => fsm.GetState(stateName)!.AddMethod(method); /// [PublicAPI] + [Obsolete("In a future update, the method here will no longer take a parameter. Please use the `AddLambdaMethod` extension method instead! (Though that one's parameter is the `Finish` method.)")] public static void AddMethod(this Fsm fsm, string stateName, Action method) => fsm.GetState(stateName)!.AddMethod(method); /// /// The fsm state /// The method that will be invoked with the action as the parameter [PublicAPI] + [Obsolete("In a future update, the method here will no longer take a parameter. Please use the `AddLambdaMethod` extension method instead! (Though that one's parameter is the `Finish` method.)")] public static void AddMethod(this FsmState state, Action method) { DelegateAction action = new DelegateAction { Method = method }; @@ -470,6 +473,34 @@ public static void AddMethod(this FsmState state, Action method) state.AddAction(action); } + /// + /// Adds a method in a PlayMakerFSM. + /// + /// The fsm + /// The name of the state in which the method is added + /// The method that will be invoked + /// If true, execute the function repeatedly on every update frame + [PublicAPI] + [Obsolete("In a future update, this method will be `AddMethod`.")] + public static void AddMethod2(this PlayMakerFSM fsm, string stateName, Action method, bool everyFrame = false) => fsm.GetState(stateName)!.AddMethod2(method, everyFrame); + + /// + [PublicAPI] + [Obsolete("In a future update, this method will be `AddMethod`.")] + public static void AddMethod2(this Fsm fsm, string stateName, Action method, bool everyFrame = false) => fsm.GetState(stateName)!.AddMethod2(method, everyFrame); + + /// + /// The fsm state + /// The method that will be invoked + /// If true, execute the function repeatedly on every update frame + [PublicAPI] + [Obsolete("In a future update, this method will be `AddMethod`.")] + public static void AddMethod2(this FsmState state, Action method, bool everyFrame = false) + { + LambdaAction action = new LambdaAction { Method = method }; + state.AddAction(action); + } + /// /// Adds a method with a parameter in a PlayMakerFSM. /// @@ -599,18 +630,22 @@ public static void InsertActions(this FsmState state, int index, params FsmState /// The index to place the action in /// bool that indicates whether the insertion was successful [PublicAPI] + [Obsolete("In a future update, the method here will no longer take a parameter. Please use the `InsertLambdaMethod` extension method instead! (Though that one's parameter is the `Finish` method.)")] public static void InsertMethod(this PlayMakerFSM fsm, string stateName, Action method, int index) => fsm.GetState(stateName)!.InsertMethod(index, method); /// [PublicAPI] + [Obsolete("In a future update, the method here will no longer take a parameter. Please use the `InsertLambdaMethod` extension method instead! (Though that one's parameter is the `Finish` method.)")] public static void InsertMethod(this PlayMakerFSM fsm, string stateName, int index, Action method) => fsm.GetState(stateName)!.InsertMethod(index, method); /// [PublicAPI] + [Obsolete("In a future update, the method here will no longer take a parameter. Please use the `InsertLambdaMethod` extension method instead! (Though that one's parameter is the `Finish` method.)")] public static void InsertMethod(this Fsm fsm, string stateName, Action method, int index) => fsm.GetState(stateName)!.InsertMethod(index, method); /// [PublicAPI] + [Obsolete("In a future update, the method here will no longer take a parameter. Please use the `InsertLambdaMethod` extension method instead! (Though that one's parameter is the `Finish` method.)")] public static void InsertMethod(this Fsm fsm, string stateName, int index, Action method) => fsm.GetState(stateName)!.InsertMethod(index, method); /// @@ -618,10 +653,12 @@ public static void InsertActions(this FsmState state, int index, params FsmState /// The method that will be invoked /// The index to place the action in [PublicAPI] + [Obsolete("In a future update, the method here will no longer take a parameter. Please use the `InsertLambdaMethod` extension method instead! (Though that one's parameter is the `Finish` method.)")] public static void InsertMethod(this FsmState state, Action method, int index) => state.InsertMethod(index, method); /// [PublicAPI] + [Obsolete("In a future update, the method here will no longer take a parameter. Please use the `InsertLambdaMethod` extension method instead! (Though that one's parameter is the `Finish` method.)")] public static void InsertMethod(this FsmState state, int index, Action method) { DelegateAction action = new DelegateAction { Method = method }; @@ -629,6 +666,51 @@ public static void InsertMethod(this FsmState state, int index, Action + /// Inserts a method in a PlayMakerFSM. + /// Trying to insert a method out of bounds will cause a `ArgumentOutOfRangeException`. + /// + /// The fsm + /// The name of the state in which the method is added + /// The method that will be invoked + /// The index to place the action in + /// bool that indicates whether the insertion was successful + [PublicAPI] + [Obsolete("In a future update, this method will be `InsertMethod`.")] + public static void InsertMethod2(this PlayMakerFSM fsm, string stateName, Action method, int index, bool everyFrame = false) => fsm.GetState(stateName)!.InsertMethod2(index, method, everyFrame); + + /// + [PublicAPI] + [Obsolete("In a future update, this method will be `InsertMethod`.")] + public static void InsertMethod2(this PlayMakerFSM fsm, string stateName, int index, Action method, bool everyFrame = false) => fsm.GetState(stateName)!.InsertMethod2(index, method, everyFrame); + + /// + [PublicAPI] + [Obsolete("In a future update, this method will be `InsertMethod`.")] + public static void InsertMethod2(this Fsm fsm, string stateName, Action method, int index, bool everyFrame = false) => fsm.GetState(stateName)!.InsertMethod2(index, method, everyFrame); + + /// + [PublicAPI] + [Obsolete("In a future update, this method will be `InsertMethod`.")] + public static void InsertMethod2(this Fsm fsm, string stateName, int index, Action method, bool everyFrame = false) => fsm.GetState(stateName)!.InsertMethod2(index, method, everyFrame); + + /// + /// The fsm state + /// The method that will be invoked + /// The index to place the action in + [PublicAPI] + [Obsolete("In a future update, this method will be `InsertMethod`.")] + public static void InsertMethod2(this FsmState state, Action method, int index, bool everyFrame = false) => state.InsertMethod2(index, method, everyFrame); + + /// + [PublicAPI] + [Obsolete("In a future update, this method will be `InsertMethod`.")] + public static void InsertMethod2(this FsmState state, int index, Action method, bool everyFrame = false) + { + LambdaAction action = new LambdaAction { Method = method }; + state.InsertAction(action, index); + } + /// /// Inserts a method with a parameter in a PlayMakerFSM. /// Trying to insert a method out of bounds will cause a `ArgumentOutOfRangeException`. @@ -673,9 +755,9 @@ public static void InsertLambdaMethod(this FsmState state, int index, Action /// The action to insert before. - /// The method to execute. - /// The argument will be the FsmStateAction which is being added. + /// The method to execute. The argument will be the FsmStateAction which is being added. [PublicAPI] + [Obsolete("In a future update, the method here will no longer take a parameter. Please use the `InsertLambdaMethodBefore` extension method instead! (Though that one's parameter is the `Finish` method.)")] public static void InsertMethodBefore(this FsmStateAction action, Action method) { FsmState state = action.State; @@ -687,9 +769,9 @@ public static void InsertMethodBefore(this FsmStateAction action, Action /// The action to insert after. - /// The method to execute. - /// The argument will be the FsmStateAction which is being added. + /// The method to execute. The argument will be the FsmStateAction which is being added. [PublicAPI] + [Obsolete("In a future update, the method here will no longer take a parameter. Please use the `InserLambdatMethodAfter` extension method instead! (Though that one's parameter is the `Finish` method.)")] public static void InsertMethodAfter(this FsmStateAction action, Action method) { FsmState state = action.State; @@ -697,6 +779,60 @@ public static void InsertMethodAfter(this FsmStateAction action, Action + /// Insert a method to run before the specified FsmStateAction. + /// + /// The action to insert before. + /// The method to execute. + [PublicAPI] + [Obsolete("In a future update, this method will be `InsertMethodBefore`.")] + public static void InsertMethod2Before(this FsmStateAction action, Action method) + { + FsmState state = action.State; + int idx = Array.IndexOf(state.Actions, action); + state.InsertMethod2(idx, method); + } + + /// + /// Insert a method to run after the specified FsmStateAction. + /// + /// The action to insert after. + /// The method to execute. + [PublicAPI] + [Obsolete("In a future update, this method will be `InsertMethodAfter`.")] + public static void InsertMethod2After(this FsmStateAction action, Action method) + { + FsmState state = action.State; + int idx = Array.IndexOf(state.Actions, action); + state.InsertMethod2(idx + 1, method); + } + + /// + /// Insert a method to run before the specified FsmStateAction. + /// + /// The action to insert before. + /// The method to execute. The argument will be the `Finish` method. + [PublicAPI] + public static void InsertLambdaMethodBefore(this FsmStateAction action, Action method) + { + FsmState state = action.State; + int idx = Array.IndexOf(state.Actions, action); + state.InsertLambdaMethod(idx, method); + } + + /// + /// Insert a method to run after the specified FsmStateAction. + /// + /// The action to insert after. + /// The method to execute. The argument will be the `Finish` method. + [PublicAPI] + public static void InsertLambdaMethodAfter(this FsmStateAction action, Action method) + { + FsmState state = action.State; + int idx = Array.IndexOf(state.Actions, action); + state.InsertLambdaMethod(idx + 1, method); + } + /// /// Insert an action to run before the specified FsmStateAction. /// diff --git a/thunderstore/thunderstore.toml b/thunderstore/thunderstore.toml index 7ace3de..5b46dfc 100644 --- a/thunderstore/thunderstore.toml +++ b/thunderstore/thunderstore.toml @@ -1,5 +1,5 @@ [config] -schemaVersion = "0.3.7" +schemaVersion = "0.3.8" [package] # The name of the team you are publishing as