This repository was archived by the owner on Dec 21, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGlobalMembersHelper.cs
More file actions
91 lines (74 loc) · 3.43 KB
/
Copy pathGlobalMembersHelper.cs
File metadata and controls
91 lines (74 loc) · 3.43 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
using System;
using System.Drawing;
using Rocket.API;
using Rocket.API.Economy;
using Rocket.API.Eventing;
using Rocket.API.Logging;
using Rocket.API.Permissions;
using Rocket.API.Player;
using Rocket.API.Plugins;
using Rocket.API.Scheduler;
using Rocket.API.User;
namespace Rocket.Scripting
{
public static class GlobalMembersHelper
{
public static void SetGlobalVariables(this IScriptContext context)
{
context.SetGlobalVariable("container", context.Container);
context.SetGlobalVariable("plugin", context.Plugin);
TryRegisterService<ILogger>(context, "logger");
TryRegisterService<ITaskScheduler>(context, "scheduler");
TryRegisterService<IPlayerManager>(context, "playermanager");
TryRegisterService<IPluginManager>(context, "plugins");
TryRegisterService<IEconomyProvider>(context, "economy");
TryRegisterService<IUserManager>(context, "usermanager");
TryRegisterService<IEventManager>(context, "events");
TryRegisterService<IPermissionProvider>(context, "permissions");
foreach (var level in Enum.GetValues(typeof(LogLevel)))
context.SetGlobalVariable("level_" + level.ToString().ToLower(), (int)level);
foreach (var color in Enum.GetValues(typeof(ConsoleColor)))
context.SetGlobalVariable("color_" + color.ToString().ToLower(), (int)color);
context.SetGlobalFunction("getType", new Func<string, Type>(Type.GetType));
context.SetGlobalFunction("log", new Action<string>(message =>
{
context.Container.Resolve<ILogger>().Log(message);
}));
var host = context.Container.Resolve<IHost>();
context.SetGlobalFunction("print", new Action<string>(message =>
{
ConsoleColor bak = Console.ForegroundColor;
Console.ForegroundColor = ConsoleColor.White;
Console.Write("[");
Console.ForegroundColor = ConsoleColor.Yellow;
Console.Write(context.Plugin?.Name ?? "Script");
Console.ForegroundColor = ConsoleColor.White;
Console.Write("] ");
Console.WriteLine(message);
Console.ForegroundColor = bak;
}));
context.SetGlobalFunction("print_color", new Action<string, int>((message, c) =>
{
ConsoleColor bak = Console.ForegroundColor;
Console.ForegroundColor = ConsoleColor.White;
Console.Write("[");
Console.ForegroundColor = ConsoleColor.Yellow;
Console.Write(context.Plugin?.Name ?? "Script");
Console.ForegroundColor = ConsoleColor.White;
Console.Write("] ");
Console.ForegroundColor = (ConsoleColor) c;
Console.WriteLine(message);
Console.ForegroundColor = bak;
}));
context.SetGlobalFunction("log_level", new Action<string, int>((message, logLevel) =>
{
context.Container.Resolve<ILogger>().Log(message, (LogLevel)logLevel);
}));
}
public static void TryRegisterService<TService>(this IScriptContext context, string name)
{
if (context.Container.TryResolve(null, out TService service))
context.SetGlobalVariable(name, service);
}
}
}