Skip to content

Commit

Permalink
Update jint + fail gracefully on invalid enum from script
Browse files Browse the repository at this point in the history
  • Loading branch information
luttje committed Jul 8, 2024
1 parent 54d6de7 commit aa81860
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 97 deletions.
10 changes: 9 additions & 1 deletion Core/Key2Joy.Contracts/Util/TypeConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,15 @@ public static object ConvertToType(object value, Type desiredType)
}
else if (desiredType.IsEnum)
{
value = Enum.Parse(desiredType, value.ToString());
try
{
value = Enum.Parse(desiredType, value.ToString());
}
catch (ArgumentException)
{
Output.WriteLine($"Could not parse value '{value}' to enum '{desiredType}'. Using first value instead.");
value = Enum.GetValues(desiredType).GetValue(0);
}
}
else if (desiredType == typeof(DateTime))
{
Expand Down
2 changes: 1 addition & 1 deletion Core/Key2Joy.Core/Key2Joy.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@

<ItemGroup>
<PackageReference Include="CommonServiceLocator" Version="2.0.7" />
<PackageReference Include="Jint" Version="3.0.0-preview-488" />
<PackageReference Include="Jint" Version="3.1.4" />
<PackageReference Include="KeraLua">
<Version>1.3.4</Version>
</PackageReference>
Expand Down
174 changes: 87 additions & 87 deletions Core/Key2Joy.Core/Mapping/Actions/Logic/SetDelayedFunctionsAction.cs
Original file line number Diff line number Diff line change
@@ -1,87 +1,87 @@
using System;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
using Key2Joy.Contracts.Mapping;
using Key2Joy.Contracts.Mapping.Actions;
using Key2Joy.Contracts.Mapping.Triggers;

namespace Key2Joy.Mapping.Actions.Logic;

[Action(
Description = "Timeout for a specified duration before executing a function",
Visibility = MappingMenuVisibility.Never,
NameFormat = "Timeout for {0}ms",
GroupName = "Logic",
GroupImage = "application_xp_terminal"
)]
public class SetDelayedFunctionsAction : CoreAction
{
[JsonInclude]
public TimeSpan WaitTime;

public SetDelayedFunctionsAction(string name)
: base(name)
{
}

/// <markdown-doc>
/// <parent-name>Logic</parent-name>
/// <path>Api/Logic</path>
/// </markdown-doc>
/// <summary>
/// Execute functions whilst waiting the specified time between them.
///
/// The first function is executed immediately.
/// </summary>
/// <markdown-example>
/// Shows how to count down from 3 and execute a command using Lua.
/// <code language="lua">
/// <![CDATA[
/// SetDelayedFunctions(
/// 1000,
/// function ()
/// Print("Aborting in 3 second...")
/// end,
/// function ()
/// Print("Three")
/// end,
/// function ()
/// Print("Two")
/// end,
/// function ()
/// Print("One")
/// end,
/// function ()
/// App.Command("abort")
/// end
/// )
/// ]]>
/// </code>
/// </markdown-example>
/// <param name="waitTime">Time to wait (in milliseconds) between function calls</param>
/// <param name="callbacks">One or more functions to execute</param>
/// <name>SetDelayedFunctions</name>
[ExposesScriptingMethod("SetDelayedFunctions")]
public async void ExecuteForScript(long waitTime, params Action[] callbacks)
{
this.WaitTime = TimeSpan.FromMilliseconds(waitTime);

for (var i = 0; i < callbacks.Length; i++)
{
if (i > 0)
{
await Task.Delay(this.WaitTime);
}

callbacks[i].Invoke();
}
}

public override Task Execute(AbstractInputBag inputBag = null) =>
// Irrelevant because only scripts should use this function
Task.Delay(this.WaitTime);

public override string GetNameDisplay() =>
// Irrelevant because only scripts should use this function
this.Name.Replace("{0}", this.WaitTime.TotalMilliseconds.ToString());
}
using System;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
using Key2Joy.Contracts.Mapping;
using Key2Joy.Contracts.Mapping.Actions;
using Key2Joy.Contracts.Mapping.Triggers;

namespace Key2Joy.Mapping.Actions.Logic;

[Action(
Description = "Timeout for a specified duration before executing a function",
Visibility = MappingMenuVisibility.Never,
NameFormat = "Timeout for {0}ms",
GroupName = "Logic",
GroupImage = "application_xp_terminal"
)]
public class SetDelayedFunctionsAction : CoreAction
{
[JsonInclude]
public TimeSpan WaitTime;

public SetDelayedFunctionsAction(string name)
: base(name)
{
}

/// <markdown-doc>
/// <parent-name>Logic</parent-name>
/// <path>Api/Logic</path>
/// </markdown-doc>
/// <summary>
/// Execute functions whilst waiting the specified time between them.
///
/// The first function is executed immediately.
/// </summary>
/// <markdown-example>
/// Shows how to count down from 3 and execute a command using Lua.
/// <code language="lua">
/// <![CDATA[
/// SetDelayedFunctions(
/// 1000,
/// function ()
/// Print("Aborting in 3 second...")
/// end,
/// function ()
/// Print("Three")
/// end,
/// function ()
/// Print("Two")
/// end,
/// function ()
/// Print("One")
/// end,
/// function ()
/// App.Command("Abort")
/// end
/// )
/// ]]>
/// </code>
/// </markdown-example>
/// <param name="waitTime">Time to wait (in milliseconds) between function calls</param>
/// <param name="callbacks">One or more functions to execute</param>
/// <name>SetDelayedFunctions</name>
[ExposesScriptingMethod("SetDelayedFunctions")]
public async void ExecuteForScript(long waitTime, params Action[] callbacks)
{
this.WaitTime = TimeSpan.FromMilliseconds(waitTime);

for (var i = 0; i < callbacks.Length; i++)
{
if (i > 0)
{
await Task.Delay(this.WaitTime);
}

callbacks[i].Invoke();
}
}

public override Task Execute(AbstractInputBag inputBag = null) =>
// Irrelevant because only scripts should use this function
Task.Delay(this.WaitTime);

public override string GetNameDisplay() =>
// Irrelevant because only scripts should use this function
this.Name.Replace("{0}", this.WaitTime.TotalMilliseconds.ToString());
}
4 changes: 2 additions & 2 deletions Core/Key2Joy.Core/Mapping/Actions/Logic/SetTimeoutAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public SetTimeoutAction(string name)
/// Print("One")
///
/// setTimeout(function () {
/// App.Command("abort")
/// App.Command("Abort")
/// }, 1000)
/// }, 1000)
/// }, 1000)
Expand All @@ -76,7 +76,7 @@ public SetTimeoutAction(string name)
/// Print("One")
///
/// SetTimeout(function ()
/// App.Command("abort")
/// App.Command("Abort")
/// end, 1000)
/// end, 1000)
/// end, 1000)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ public override void RegisterScriptingMethod(ExposedMethod exposedMethod, Abstra
var functionName = exposedMethod.FunctionName;
var parents = functionName.Split('.');
var methodInfo = exposedMethod.GetExecutorMethodInfo();
var @delegate = new DelegateWrapper(this.Environment, methodInfo.CreateDelegate(exposedMethod));
var currentObject = this.Environment.Realm.GlobalObject;
var @delegate = JsValue.FromObject(this.Environment, methodInfo.CreateDelegate(exposedMethod));
var currentObject = this.Environment.Global;

// TODO: This may work for the setTimeout and setInterval methods, but will it work for other
// types of functions? I think this Func`3<JsValue, JsValue[], JsValue> may be the result of the
Expand Down
8 changes: 4 additions & 4 deletions Support/Key2Joy.Tests/BuildMarkdownDocs/sample.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit aa81860

Please sign in to comment.