This repository was archived by the owner on Dec 21, 2019. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathScriptingPlugin.cs
More file actions
127 lines (105 loc) · 3.86 KB
/
Copy pathScriptingPlugin.cs
File metadata and controls
127 lines (105 loc) · 3.86 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
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
using System;
using System.Collections.Generic;
using System.Linq;
using Rocket.API.Commands;
using Rocket.API.DependencyInjection;
using Rocket.API.Eventing;
using Rocket.API.User;
using Rocket.Core.Commands.Events;
using Rocket.Core.Player.Events;
using Rocket.Core.Plugins;
using Rocket.Core.User;
using Rocket.Core.User.Events;
namespace Rocket.Scripting
{
class ScriptingPlugin : Plugin, IEventListener<PreCommandExecutionEvent>, IEventListener<UserChatEvent>
{
public ScriptingPlugin(IDependencyContainer container) : base("ScriptingPlugin", container)
{
}
protected override void OnLoad(bool isFromReload)
{
EventManager.AddEventListener(this, this);
}
public IScriptContext StartSession(IDependencyContainer container, IUser user, string providerName)
{
var context = GetScriptContext(user);
if (context != null)
return context;
var provider = GetScriptingProvider(container, providerName);
if (provider != null)
{
context = provider.CreateScriptContext(container);
context.SetGlobalVariables();
context.SetGlobalVariable("me", user);
ScriptContexts.Add(user, context);
return context;
}
return null;
}
public IScriptingProvider GetScriptingProvider(IDependencyContainer container, string providerName)
{
var providers = container.ResolveAll<IScriptingProvider>();
foreach (var provider in providers)
{
if (provider.ScriptName.Equals(providerName, StringComparison.OrdinalIgnoreCase)
|| provider.FileTypes.Any(c => c.Equals(providerName, StringComparison.OrdinalIgnoreCase)))
{
return provider;
}
}
return null;
}
public IScriptContext GetScriptContext(IUser user)
{
foreach (var scriptContext in ScriptContexts)
{
if (scriptContext.Key.Id.Equals(user.Id, StringComparison.OrdinalIgnoreCase))
return scriptContext.Value;
}
return null;
}
public bool StopSession(IUser user)
{
return ScriptContexts.Keys
.ToList()
.Where(c => c.Id.Equals(user.Id, StringComparison.OrdinalIgnoreCase))
.All(c => ScriptContexts.Remove(c));
}
public Dictionary<IUser, IScriptContext> ScriptContexts { get; } = new Dictionary<IUser, IScriptContext>();
public void HandleEvent(IEventEmitter emitter, PreCommandExecutionEvent @event)
{
if (!(@event.User is IConsole console))
return;
var session = GetScriptContext(console);
if (session == null)
return;
@event.IsCancelled = true;
var cmd = @event.CommandLine;
if (cmd == "exit")
{
StopSession(@event.User);
return;
}
var result = session.Eval(cmd);
if (result.HasReturn)
console.SendMessage("> " + result.Return, ConsoleColor.Gray);
}
public void HandleEvent(IEventEmitter emitter, UserChatEvent @event)
{
var session = GetScriptContext(@event.User);
if (session == null)
return;
@event.IsCancelled = true;
if (@event.Message == "exit")
{
StopSession(@event.User);
return;
}
var cmd = @event.Message;
var result = session.Eval(cmd);
if (result.HasReturn)
@event.User.SendMessage("> " + result.Return, ConsoleColor.Gray);
}
}
}