forked from LiveSplit/LiveSplit.Server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJavaScriptScript.cs
More file actions
62 lines (54 loc) · 1.73 KB
/
JavaScriptScript.cs
File metadata and controls
62 lines (54 loc) · 1.73 KB
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
61
62
using LiveSplit.Model;
using Noesis.Javascript;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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);
}
}
}