forked from YandalfFX/Redox
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRedox.cs
More file actions
233 lines (194 loc) · 8.41 KB
/
Redox.cs
File metadata and controls
233 lines (194 loc) · 8.41 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
using System;
using System.IO;
using System.Reflection;
using System.Diagnostics;
using System.Collections.Generic;
using System.Net;
using System.Security.Cryptography.X509Certificates;
using System.Net.Security;
using Newtonsoft.Json;
using Redox.API;
using Redox.API.Libraries;
using Redox.API.Permissions;
using Redox.API.Plugins;
using Redox.API.Plugins.CSharp;
using Redox.API.Plugins.Extension;
using Redox.Core.Plugins;
using Autofac;
using Redox.Core.Permissions;
using Redox.Core.Commands;
using Redox.API.Commands;
namespace Redox
{
public sealed class Redox
{
private readonly static Assembly assembly = Assembly.GetExecutingAssembly();
public static readonly Version Version = assembly.GetName().Version;
private readonly List<Assembly> dependencies = new List<Assembly>();
private Stopwatch LifeTimeWatch;
private Timer WebRequestTimer;
private string CustomPath;
#region Directories
public string RootPath { get; private set; }
public string PluginPath { get; private set; }
public string ExtensionPath { get; private set; }
public string DependencyPath { get; private set; }
public string DataPath { get; private set; }
public string LoggingPath { get; private set; }
public string AssemblePath { get; private set; }
#endregion Directories
public API.ILogger Logger { get; private set; }
public IRoleProvider RoleManager { get; private set; }
public IGroupProvider GroupManager { get; private set; }
public IPermissionProvider PermissionManager { get; private set; }
public RedoxConfig Config { get; private set; }
public IContainer Container { get; private set; }
public static Redox Mod
{
get
{
return Bootstrap.RedoxMod;
}
}
/// <summary>
/// Gets the number of seconds since Redox got initialized (Usefull for time measurements).
/// </summary>
public float LifeTime
{
get
{
return (float)LifeTimeWatch.Elapsed.TotalSeconds;
}
}
public Redox(string customPath = "")
{
CustomPath = customPath;
}
public async void Initialize()
{
try
{
if (!string.IsNullOrEmpty(CustomPath))
RootPath = CustomPath;
else
RootPath = Directory.GetCurrentDirectory() + "\\Redox\\";
PluginPath = Path.Combine(RootPath, "Plugins\\");
ExtensionPath = Path.Combine(RootPath, "Extensions\\");
DependencyPath = Path.Combine(RootPath, "Dependencies\\");
DataPath = Path.Combine(RootPath, "Data\\");
LoggingPath = Path.Combine(RootPath, "Logs\\");
AssemblePath = Path.GetDirectoryName(assembly.Location);
if (!Directory.Exists(RootPath)) Directory.CreateDirectory(RootPath);
if (!Directory.Exists(LoggingPath)) Directory.CreateDirectory(LoggingPath);
if (!Directory.Exists(ExtensionPath)) Directory.CreateDirectory(ExtensionPath);
if (!Directory.Exists(DependencyPath)) Directory.CreateDirectory(DependencyPath);
if (!Directory.Exists(PluginPath)) Directory.CreateDirectory(PluginPath);
if (!Directory.Exists(DataPath)) Directory.CreateDirectory(DataPath);
Config = new RedoxConfig();
string path = Path.Combine(RootPath, "Redox.json");
if (File.Exists(path))
Config = Utility.Json.FromFile<RedoxConfig>(path);
else
Utility.Json.ToFile(path, Config.Init());
this.EnableCertificates();
this.LoadDependencies();
ExtensionLoader.Load();
this.Container = ContainerConfig.Configure();
this.BuildContainer();
Logger = Container.Resolve<ILogger>();
Logger.LogInfo("[Redox] Initializing RedoxMod..");
PluginEngineManager.Register<CSPluginEngine>();
Logger.LogInfo("[Redox] Loading data...");
await PermissionManager.LoadAsync();
await GroupManager.LoadAsync();
await RoleManager.LoadAsync();
await RoleManager.CreateRoleAsync(new Role("default", "default role", 0, false));
await GroupManager.CreateGroupAsync(new Group("default", "default group for players.", "default", 0, true, false));
await RoleManager.AddGroupAsync("default", "default");
Logger.LogInfo("[Redox] Loading standard library..");
LocalStorage.Load();
PluginCollector.GetCollector();
Logger.LogInfo($"[Redox] RedoxMod V{Version} has been initialized.");
}
catch(Exception ex)
{
Console.Clear();
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(string.Format("[Error] Failed to load Redox, Error: {0}", ex));
}
LifeTimeWatch = Stopwatch.StartNew();
WebRequestTimer = Timers.Create(5, TimerType.Repeat, WebRequestUpdate);
}
private void EnableCertificates()
{
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(AcceptAllCertifications);
}
private bool AcceptAllCertifications(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{
return true;
}
public void WebRequestUpdate(Timer timer)
{
if(Web.Requests.Count <= 2)
{
if (Web.RequestsQueue.Count != 0 && Web.RequestsQueue.Peek() != null)
{
var request = Web.RequestsQueue.Dequeue();
request.Create();
Web.Requests.Add(request);
}
}
if (Web.Requests.Count == 0)
return;
foreach(var request in Web.Requests)
{
if (request.Complete)
Web.Requests.Remove(request);
}
}
private void LoadDependencies()
{
foreach (var file in Directory.GetFiles(DependencyPath, "*.dll"))
{
var assembly = Assembly.LoadFrom(file);
dependencies.Add(assembly);
}
}
public async void DeInitialize()
{
Logger.Log("[Redox] Preparing to shutdown..");
PluginEngineManager.UnloadAll();
LocalStorage.Save();
await PermissionManager.SaveAsync();
}
private void BuildContainer()
{
}
public class RedoxConfig
{
[JsonProperty("This message will be sent when an executed command doesn't exist.")]
public string UnknownCommand { get; private set; }
[JsonProperty("Plugin security prevents plugins from having resources.")]
public bool PluginSecurity { get; private set; }
[JsonProperty("Do you want outdated plugins to be loaded? (It's recommended to keep this disabled)")]
public bool LoadIncompitablePlugins { get; private set; }
[JsonProperty("Do you want Redox to log messages into the console?")]
public bool Logging { get; private set; }
[JsonProperty("Do you want to debug messages to show in the console? (This contains the load time of plugins)")]
public bool DebugLogging { get; private set; }
[JsonProperty("Enter here the plugin names you want to bypass the security check (Only works when PluginSecurity is enabled).")]
public string[] WhitelistedAssemblyNames { get; private set; }
public RedoxConfig Init()
{
UnknownCommand = "Unknown Command!";
PluginSecurity = true;
LoadIncompitablePlugins = false;
Logging = true;
DebugLogging = true;
WhitelistedAssemblyNames = new string[] { };
return this;
}
}
}
}