This repository has been archived by the owner on Sep 3, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathScriptService.cs
87 lines (71 loc) · 2.53 KB
/
ScriptService.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
using System;
using System.IO;
namespace MSIRGB
{
class ScriptService
{
private const string SCRIPT_SVC_NAME = "MSIRGB.ScriptService";
private const string SCRIPT_SVC_DISPLAY_NAME = "MSIRGB Script Service";
private const string SCRIPT_SVC_EXE = SCRIPT_SVC_NAME + ".exe";
private const string SCRIPT_LOG_FILE = "ScriptLog.txt";
private const string SCRIPTS_FOLDER = @"Scripts\";
public static string GetScriptLogFilePath()
{
return Path.GetFullPath(SCRIPT_LOG_FILE);
}
public static string[] GetScripts()
{
if (!Directory.Exists(SCRIPTS_FOLDER))
{
return new string[0];
}
var scripts = Directory.GetFiles(SCRIPTS_FOLDER, "*.lua");
for (int i = 0; i < scripts.Length; i++)
{
scripts[i] = Path.GetFileNameWithoutExtension(scripts[i]);
}
return scripts;
}
public static string GetRunningScript()
{
string[] args = Utils.ServiceInstaller.GetPermanentArguments(SCRIPT_SVC_NAME);
if (args.Length > 0)
{
return Path.GetFileNameWithoutExtension(args[1]);
}
else
{
return null;
}
}
public static void RunScript(string scriptName, bool ignoreMbCheck)
{
if (GetRunningScript() == scriptName)
return;
var scriptPath = Path.Combine(Path.GetFullPath(SCRIPTS_FOLDER), scriptName + ".lua");
if (!File.Exists(scriptPath))
throw new Exception("Invalid script path");
string svcFullPath = Path.GetFullPath(SCRIPT_SVC_EXE);
if (Utils.ServiceInstaller.IsServiceInstalled(SCRIPT_SVC_NAME))
{
Utils.ServiceInstaller.WaitStop(SCRIPT_SVC_NAME);
}
else
{
Utils.ServiceInstaller.Install(SCRIPT_SVC_NAME, SCRIPT_SVC_DISPLAY_NAME, svcFullPath);
}
string[] args =
{
GetScriptLogFilePath(),
scriptPath,
Convert.ToString(ignoreMbCheck)
};
Utils.ServiceInstaller.SetPermanentArguments(SCRIPT_SVC_NAME, svcFullPath, args);
Utils.ServiceInstaller.Start(SCRIPT_SVC_NAME);
}
public static void StopAnyRunningScript()
{
Utils.ServiceInstaller.Uninstall(SCRIPT_SVC_NAME);
}
}
}