Repository navigation
Expand file tree
/
Copy pathProgram.cs
More file actions
153 lines (126 loc) · 4.84 KB
/
Copy pathProgram.cs
File metadata and controls
153 lines (126 loc) · 4.84 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
using discordamx.Discord;
using discordamx.Plugin;
using discordamx.Scripting;
using DSharpPlus;
using DSharpPlus.Entities;
using DSharpPlus.VoiceNext;
using System.Runtime.InteropServices;
using System.Text;
namespace discordamx
{
internal static partial class Program
{
public static string VERSION = "0.0.3 (Voice beta)";
public static bool m_Run = true;
public static Properties m_Properties = null!;
private static ConsoleColor m_DefForegrColor;
private static bool m_Setup = false;
public static DiscordConfiguration dConfig = null!;
public static bool m_ScriptingInited = false;
public static List<DiscordChannel> m_DmUsers = null!;
public static List<Scripting.DiscordEmbedBuilder> m_Embeds = null!;
public static List<Scripting.Guild> m_ScriptGuilds = null!;
public static Logger m_Logger = null!;
public static List<ScriptTimer> m_ScriptTimers = null!;
[DllImport("Kernel32")]
private static extern bool SetConsoleCtrlHandler(EventHandler handler, bool add);
private delegate bool EventHandler(CtrlType sig);
static EventHandler _handler = null!;
private static int m_ShutdownStarted;
enum CtrlType
{
CTRL_C_EVENT = 0,
CTRL_BREAK_EVENT = 1,
CTRL_CLOSE_EVENT = 2,
CTRL_LOGOFF_EVENT = 5,
CTRL_SHUTDOWN_EVENT = 6
}
private static bool Handler(CtrlType sig)
{
switch (sig)
{
case CtrlType.CTRL_BREAK_EVENT:
case CtrlType.CTRL_LOGOFF_EVENT:
case CtrlType.CTRL_SHUTDOWN_EVENT:
case CtrlType.CTRL_CLOSE_EVENT:
StopEverything();
return true;
default:
return false;
}
}
static void Main(string[] args)
{
//=============================================================================================
/// INTERNAL
//=============================================================================================
if (!Directory.Exists(AppDomain.CurrentDomain.BaseDirectory + "Logs/"))
Directory.CreateDirectory(AppDomain.CurrentDomain.BaseDirectory + "Logs/");
m_Logger = new Logger();
if (!Directory.Exists(AppDomain.CurrentDomain.BaseDirectory + "Scripts/"))
Directory.CreateDirectory(AppDomain.CurrentDomain.BaseDirectory + "Scripts/");
if (!Directory.Exists(AppDomain.CurrentDomain.BaseDirectory + "Plugins/"))
Directory.CreateDirectory(AppDomain.CurrentDomain.BaseDirectory + "Plugins/");
Console.OutputEncoding = System.Text.Encoding.Unicode;
m_DefForegrColor = Console.ForegroundColor;
Console.ForegroundColor = ConsoleColor.White;
Console.CancelKeyPress += OnCancelKeyPress;
if (OperatingSystem.IsWindows())
{
_handler = Handler;
SetConsoleCtrlHandler(_handler, true);
}
Setup(args);
while (m_Run)
{
//Listen for input - handle it locally - trigger script event too.
ProcessLocalInput();
Thread.Sleep(100);
}
}
private static void OnCancelKeyPress(object? sender, ConsoleCancelEventArgs e)
{
e.Cancel = true;
StopEverything();
}
public static void StopEverything(int errcode = 0)
{
if (System.Threading.Interlocked.Exchange(ref m_ShutdownStarted, 1) != 0)
return;
m_Run = false;
var scripts = Manager.m_Scripts.ToArray();
foreach (Script script in scripts)
{
if (script.m_Amx == null)
continue;
try
{
var callback = script.m_Amx.FindPublic("OnSystemExit");
if (callback != null)
{
script.m_Amx.Push(errcode);
callback.Execute();
}
}
catch (Exception ex)
{
m_Logger.Exception(ex, script);
}
}
foreach (Script script in scripts)
{
try
{
Manager.UnloadScript(script);
}
catch (Exception ex)
{
m_Logger.Exception(ex, script);
}
}
PluginLoader.UnloadPlugins();
Console.ForegroundColor = m_DefForegrColor;
Environment.Exit(errcode);
}
}
}