-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathProgram.cs
132 lines (115 loc) · 4.9 KB
/
Program.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
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
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
namespace UniHacker
{
public partial class Program
{
// Unity Arg
public const string UNITY_PATH = nameof(UNITY_PATH);
// Unity Hub Arg
public const string HUB_PATH = nameof(HUB_PATH);
public const string NEED_LOGIN = nameof(NEED_LOGIN);
public const string DISABLE_UPDATE = nameof(DISABLE_UPDATE);
// Common Arg
public const string EXEC_METHOD = nameof(EXEC_METHOD);
// Execute Method Arg
public const string PATCH = nameof(PATCH);
public const string RESTORE = nameof(RESTORE);
public const string CHECK = nameof(CHECK);
public static HashSet<string> MethodNames = new HashSet<string>() { PATCH, RESTORE, CHECK };
public static async Task Main(string[] args)
{
var filePath = string.Empty;
TryGetEnvironmentVariable(UNITY_PATH, out var unityPath);
TryGetEnvironmentVariable(HUB_PATH, out var hubPath);
TryGetEnvironmentVariable(EXEC_METHOD, out var execMethod);
if (unityPath != null)
{
await MessageBox.Show($"Unity Argument.\n" +
$"\t\t{UNITY_PATH}: {unityPath}\n" +
$"\t\t{EXEC_METHOD}: {execMethod}\n");
filePath = unityPath;
}
else if (hubPath != null)
{
var hasNeedLogin = TryGetEnvironmentVariable(NEED_LOGIN, out var needLogin, bool.FalseString);
var hasDisableUpdate = TryGetEnvironmentVariable(DISABLE_UPDATE, out var disableUpdate, bool.FalseString);
await MessageBox.Show($"UnityHub Argument.\n" +
$"\t\t{HUB_PATH}: {hubPath}\n" +
$"\t\t{EXEC_METHOD}: {execMethod}\n" +
$"\t\t{NEED_LOGIN}: {needLogin}{(hasNeedLogin ? "" : " (Default)")}\n" +
$"\t\t{DISABLE_UPDATE}: {disableUpdate}{(hasDisableUpdate ? "" : " (Default)")}\n");
filePath = hubPath;
}
else
{
await MessageBox.Show($"Please provide {nameof(UNITY_PATH)} parameter or {nameof(HUB_PATH)} parameter");
return;
}
if (!PlatformUtils.IsAdministrator)
{
await MessageBox.Show("Please run as an administrator.");
return;
}
var patcher = PatchManager.GetPatcher(filePath, PlatformUtils.GetPlatformType());
var status = patcher?.PatchStatus ?? PatchStatus.Unknown;
var architectureName = MachineArchitecture.GetArchitectureName(patcher?.ArchitectureType ?? ArchitectureType.UnKnown);
await MessageBox.Show($"Information.\n" +
$"\t\tVersion: {patcher?.FileVersion}({architectureName})\n" +
$"\t\tStatus: {status}\n" +
$"\t\tPlatform: {PlatformUtils.GetPlatformType()}\n");
if (patcher == null || patcher.PatchStatus == PatchStatus.Unknown)
{
await MessageBox.Show($"Unknown binary file. '{filePath}'");
return;
}
if (!MethodNames.Contains(execMethod!))
{
await MessageBox.Show($"Unknown '{EXEC_METHOD}' parameter: {execMethod}");
return;
}
try
{
switch (execMethod)
{
case PATCH:
{
(bool success, string errorMsg) = await patcher.ApplyPatch(progress => { });
if (!success)
throw new Exception(errorMsg);
}
break;
case RESTORE:
{
(bool success, string errorMsg) = await patcher.RemovePatch(progress => { });
if (!success)
throw new Exception(errorMsg);
}
break;
}
await MessageBox.Show($"Successfully {execMethod}.");
}
catch (Exception ex)
{
await MessageBox.Show(ex.ToString());
}
}
public static bool TryGetEnvironmentVariable(string variable, out string? value, string? defaultValue = null)
{
var target = EnvironmentVariableTarget.Process;
value = Environment.GetEnvironmentVariable(variable, target);
if (value == null)
{
if (defaultValue != null)
{
value = defaultValue;
Environment.SetEnvironmentVariable(variable, defaultValue, target);
}
return false;
}
return true;
}
}
}