forked from LiveSplit/LiveSplit.Server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
JavaScriptScript.cs
60 lines (52 loc) · 1.69 KB
/
JavaScriptScript.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
using Noesis.Javascript;
using System;
using System.Collections.Generic;
using System.Linq;
namespace LiveSplit
{
public class JavaScriptScript : IScript
{
private JavascriptContext context;
public string Code { get; set; }
public dynamic this[string name]
{
get
{
return context.GetParameter(name);
}
set
{
context.SetParameter(name, value);
}
}
public JavaScriptScript(string code)
{
context = new JavascriptContext();
this["createObject"] = new Func<string, Dictionary<string, Object>, Object>(createObject);
this["createArray"] = new Func<string, int, Object>(createArray);
this["getStaticProperty"] = new Func<string, string, Object>(getStaticProperty);
Code = code;
}
Object createObject(string name, Dictionary<string, Object> parameters)
{
var pars = parameters.Values.ToArray();
var type = Type.GetType(name, true);
var constructor = type.GetConstructor(pars.Select(x => x.GetType()).ToArray());
return constructor.Invoke(pars);
}
Object createArray(string name, int count)
{
var type = Type.GetType(name, true);
return Array.CreateInstance(type, count);
}
Object getStaticProperty(string name, string property)
{
var type = Type.GetType(name, true);
return type.GetProperty(property).GetValue(null, null);
}
public dynamic Run()
{
return context.Run(Code);
}
}
}