-
Notifications
You must be signed in to change notification settings - Fork 12
/
JistPlugin.cs
executable file
·99 lines (87 loc) · 3.43 KB
/
JistPlugin.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TerrariaApi.Server;
using System.Reflection;
using Wolfje.Plugins.Jist.Framework;
namespace Wolfje.Plugins.Jist {
[ApiVersion(1, 20)]
public class JistPlugin : TerrariaPlugin {
public static JistEngine Instance { get; protected set; }
public override string Author { get { return "Wolfje"; } }
public override string Description { get { return "Javascript interpreted scripting for TShock"; } }
public override string Name { get { return "Jist"; } }
public override Version Version { get { return Assembly.GetExecutingAssembly().GetName().Version; } }
protected JistRestInterface _restInterface;
/// <summary>
/// Subscribe to this event to have the opportunity to load
/// functions into the JavaScript engine when it reloads.
/// </summary>
public static event EventHandler<JavascriptFunctionsNeededEventArgs> JavascriptFunctionsNeeded;
public JistPlugin(Terraria.Main game)
: base(game)
{
Order = 1;
Instance = new JistEngine(this);
TShockAPI.Commands.ChatCommands.Add(new TShockAPI.Command("jist.cmd", TShockAPI_JistChatCommand, "jist"));
ServerApi.Hooks.GameInitialize.Register(this, game_initialize);
}
private async void TShockAPI_JistChatCommand(TShockAPI.CommandArgs args)
{
if (args.Parameters.Count == 0) {
//TODO: Print help
return;
}
if (args.Parameters[0].Equals("dumpenv", StringComparison.CurrentCultureIgnoreCase)) {
if (Instance == null) {
return;
}
foreach (var property in Instance.DumpGlobalEnvironment().OrderBy(i => i.Key)) {
args.Player.SendInfoMessage("{0}: {1}", property.Key,
property.Value.Get.HasValue == false ? "undefined" : property.Value.Get.Value.ToString());
}
} else if (args.Parameters[0].Equals("dumptasks", StringComparison.CurrentCultureIgnoreCase)) {
foreach (Wolfje.Plugins.Jist.stdlib.RecurringFunction recur in Instance.stdTask.DumpTasks().OrderBy(i => i.NextRunTime)) {
args.Player.SendInfoMessage(recur.ToString());
}
} else if (args.Parameters[0].Equals("eval", StringComparison.CurrentCultureIgnoreCase)
|| args.Parameters[0].Equals("ev", StringComparison.CurrentCultureIgnoreCase)
&& (args.Parameters.Count > 1)) {
args.Player.SendInfoMessage(Instance.Eval(args.Parameters[1]));
} else if (args.Parameters[0].Equals("reload", StringComparison.CurrentCultureIgnoreCase)
|| args.Parameters[0].Equals("rl", StringComparison.CurrentCultureIgnoreCase)) {
Instance.Dispose();
Instance = null;
Instance = new JistEngine(this);
await Instance.LoadEngineAsync();
args.Player.SendInfoMessage("Jist reloaded");
}
}
internal static void RequestExternalFunctions()
{
JavascriptFunctionsNeededEventArgs args = new JavascriptFunctionsNeededEventArgs(Instance);
if (JavascriptFunctionsNeeded != null) {
JavascriptFunctionsNeeded(Instance, args);
}
}
/// <summary>
/// Entry point of TerrariaServerAPI
/// </summary>
public override void Initialize()
{
}
void game_initialize(EventArgs args)
{
_restInterface = new JistRestInterface(this);
}
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
if (disposing) {
Instance.Dispose();
}
}
}
}