-
Notifications
You must be signed in to change notification settings - Fork 13
/
Scripting.cs
128 lines (110 loc) · 4.04 KB
/
Scripting.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IronPython.Hosting;
using Microsoft.Scripting.Hosting;
using System.Collections;
namespace OpenSMO
{
public class HookInfo
{
public User User;
public int SubCommand;
}
public class Scripting
{
public ScriptRuntime Host;
public ScriptScope Scope;
public ScriptEngine Engine;
public delegate void UpdateHookCall();
public delegate void PacketHookCall(HookInfo info);
public delegate bool ChatHookCall(User user, string message);
public delegate bool ChatCommandHookCall(User user, string args);
public delegate string NameFormatHookCall(User user, string current);
public delegate string WebFormatHookCall(Hashtable user, string current);
public delegate int PlayerXPHookCall(User user, int current);
public Dictionary<NSCommand, List<PacketHookCall>> PacketHooks;
public List<UpdateHookCall> UpdateHooks;
public List<ChatHookCall> ChatHooks;
public Dictionary<string, List<ChatCommandHookCall>> ChatCommandHooks;
public List<NameFormatHookCall> NameFormatHooks;
public List<WebFormatHookCall> WebFormatHooks;
public List<PlayerXPHookCall> PlayerXPHooks;
public Scripting()
{
Host = Python.CreateRuntime();
Scope = Host.CreateScope();
Engine = Host.GetEngine("Python");
PacketHooks = new Dictionary<NSCommand, List<PacketHookCall>>();
UpdateHooks = new List<UpdateHookCall>();
ChatHooks = new List<ChatHookCall>();
ChatCommandHooks = new Dictionary<string, List<ChatCommandHookCall>>();
NameFormatHooks = new List<NameFormatHookCall>();
WebFormatHooks = new List<WebFormatHookCall>();
PlayerXPHooks = new List<PlayerXPHookCall>();
}
public void HookPacket(NSCommand command, PacketHookCall call, bool priority = false)
{
if (!PacketHooks.ContainsKey(command))
PacketHooks.Add(command, new List<PacketHookCall>());
if (priority) PacketHooks[command].Insert(0, call);
else PacketHooks[command].Add(call);
}
public void HookUpdate(UpdateHookCall call, bool priority = false)
{
if (priority) UpdateHooks.Insert(0, call);
else UpdateHooks.Add(call);
}
public void HookChat(ChatHookCall call, bool priority = false)
{
if (priority) ChatHooks.Insert(0, call);
else ChatHooks.Add(call);
}
public void HookChatCommand(string command, ChatCommandHookCall call, bool priority = false)
{
if (!ChatCommandHooks.ContainsKey(command))
ChatCommandHooks.Add(command, new List<ChatCommandHookCall>());
if (priority) ChatCommandHooks[command].Insert(0, call);
else ChatCommandHooks[command].Add(call);
}
public void HookNameFormat(NameFormatHookCall call, bool priority = false)
{
if (priority) NameFormatHooks.Insert(0, call);
else NameFormatHooks.Add(call);
}
public void HookWebFormat(WebFormatHookCall call, bool priority = false)
{
if (priority) WebFormatHooks.Insert(0, call);
else WebFormatHooks.Add(call);
}
public void HookPlayerXP(PlayerXPHookCall call, bool priority = false)
{
if (priority) PlayerXPHooks.Insert(0, call);
else PlayerXPHooks.Add(call);
}
public void Start()
{
try {
Engine.ExecuteFile("Scripts/_Main.py", Scope);
} catch (Exception ex) { HandleError(ex); }
}
public void HandleError(Exception ex)
{
MainClass.AddLog("Scripting error: " + Engine.GetService<ExceptionOperations>().FormatException(ex), true);
}
public void Shell()
{
while (true) {
Console.Write(">>> ");
try {
string input = Console.ReadLine();
if (input.Trim().ToLower() == "exit") return;
Console.WriteLine(Engine.Execute("str(" + input + ")", Scope));
} catch (Exception ex) {
Console.WriteLine(ex.Message);
}
}
}
}
}