This repository has been archived by the owner on Apr 15, 2022. It is now read-only.
/
Program.cs
372 lines (325 loc) · 13.5 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
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
using System;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Windows.Forms;
using LECommonLibrary;
namespace LEProc
{
internal static class Program
{
internal static string[] Args;
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
private static void Main(string[] args)
{
SystemHelper.DisableDPIScale();
try
{
Process.Start(Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location),
"LEUpdater.exe"),
"schedule");
}
catch
{
}
if (!GlobalHelper.CheckCoreDLLs())
{
MessageBox.Show(
"Some of the core Dlls are missing.\r\n" +
"Please whitelist these Dlls in your antivirus software, then download and re-install LE.\r\n"
+
"\r\n" +
"These Dlls are:\r\n" +
"LoaderDll.dll\r\n" +
"LocaleEmulator.dll",
"Locale Emulator Version " + GlobalHelper.GetLEVersion(),
MessageBoxButtons.OK,
MessageBoxIcon.Error);
return;
}
//If global config does not exist, create a new one.
LEConfig.CheckGlobalConfigFile(true);
if (args.Length == 0)
{
MessageBox.Show(
"Welcome to Locale Emulator command line tool.\r\n" +
"\r\n" +
"Usage: LEProc.exe\r\n" +
"\tpath\r\n" +
"\t-run path [args]\r\n" +
"\t-runas guid path [args]\r\n" +
"\t-manage path\r\n" +
"\t-global\r\n" +
"\r\n" +
"path\tFull path of the target application.\r\n" +
"guid\tGuid of the target profile (in LEConfig.xml).\r\n" +
"args\tAdditional arguments will be passed to the application.\r\n" +
"\r\n" +
"path\tRun an application with \r\n" +
"\t\t(i) its own profile (if any) or \r\n" +
"\t\t(ii) first global profile (if any) or \r\n" +
"\t\t(iii) default ja-JP profile.\r\n" +
"-run\tRun an application with it's own profile.\r\n" +
"-runas\tRun an application with a global profile of specific Guid.\r\n" +
"-manage\tModify the profile of one application.\r\n" +
"-global\tOpen Global Profile Manager.\r\n" +
"\r\n" +
"\r\n" +
"You can press CTRL+C to copy this message to your clipboard.\r\n",
"Locale Emulator Version " + GlobalHelper.GetLEVersion()
);
GlobalHelper.ShowErrorDebugMessageBox("SYSTEM_REPORT", 0);
return;
}
try
{
Args = args;
switch (Args[0])
{
case "-run": //-run %APP%
RunWithIndependentProfile(Args[1]);
break;
case "-manage": //-manage %APP%
Process.Start(Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location),
"LEGUI.exe"),
$"\"{Args[1]}.le.config\"");
break;
case "-global": //-global
Process.Start(Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location),
"LEGUI.exe"));
break;
case "-runas": //-runas %GUID% %APP%
RunWithGlobalProfile(Args[1], Args[2]);
break;
default:
if (File.Exists(Args[0]))
{
RunWithDefaultProfile(Args[0]);
}
break;
}
}
catch
{
}
}
private static void RunWithDefaultProfile(string path)
{
path = SystemHelper.EnsureAbsolutePath(path);
var conf = path + ".le.config";
var appProfile = LEConfig.GetProfiles(conf);
var globalProfiles = LEConfig.GetProfiles();
// app profile > first global profile > new profile(ja-JP)
var profile = appProfile.Any()
? appProfile.First()
: globalProfiles.Any() ? globalProfiles.First() : new LEProfile(true);
DoRunWithLEProfile(path, 1, profile);
}
private static void RunWithGlobalProfile(string guid, string path)
{
path = SystemHelper.EnsureAbsolutePath(path);
// We do not check whether the config exists because only when it exists can this method be called.
var profile = LEConfig.GetProfiles().First(p => p.Guid == guid);
DoRunWithLEProfile(path, 3, profile);
}
private static void RunWithIndependentProfile(string path)
{
path = SystemHelper.EnsureAbsolutePath(path);
var conf = path + ".le.config";
if (!File.Exists(conf))
{
Process.Start(
Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "LEGUI.exe"),
$"\"{path}.le.config\"");
}
else
{
var profile = LEConfig.GetProfiles(conf)[0];
DoRunWithLEProfile(path, 2, profile);
}
}
private static void DoRunWithLEProfile(string absPath, int argumentsStart, LEProfile profile)
{
try
{
if (profile.RunAsAdmin && !SystemHelper.IsAdministrator())
{
ElevateProcess();
return;
}
if (profile.RunWithSuspend)
{
if (DialogResult.No ==
MessageBox.Show(
"You are running a exectuable with CREATE_SUSPENDED flag.\r\n" +
"\r\n" +
"The exectuable will be executed after you click the \"Yes\" button, " +
"but as a background process which has no notifications at all." +
"You can attach it by using OllyDbg, or stop it with Task Manager.\r\n",
"Locale Emulator Debug Mode Warning",
MessageBoxButtons.YesNo,
MessageBoxIcon.Information
))
return;
}
var applicationName = string.Empty;
var commandLine = string.Empty;
if (Path.GetExtension(absPath).ToLower() == ".exe")
{
applicationName = absPath;
commandLine = absPath.StartsWith("\"")
? $"{absPath} "
: $"\"{absPath}\" ";
// use arguments in le.config, prior to command line arguments
commandLine += string.IsNullOrEmpty(profile.Parameter) && Args.Skip(argumentsStart).Any()
? Args.Skip(argumentsStart).Aggregate((a, b) => $"{a} {b}")
: profile.Parameter;
}
else
{
var jb = AssociationReader.GetAssociatedProgram(Path.GetExtension(absPath));
if (jb == null)
return;
applicationName = jb[0];
commandLine = jb[0].StartsWith("\"")
? $"{jb[0]} "
: $"\"{jb[0]}\" ";
commandLine += jb[1].Replace("%1", absPath).Replace("%*", profile.Parameter);
}
var currentDirectory = Path.GetDirectoryName(absPath);
var ansiCodePage = (uint) CultureInfo.GetCultureInfo(profile.Location).TextInfo.ANSICodePage;
var oemCodePage = (uint) CultureInfo.GetCultureInfo(profile.Location).TextInfo.OEMCodePage;
var localeID = (uint) CultureInfo.GetCultureInfo(profile.Location).TextInfo.LCID;
var defaultCharset = (uint)
GetCharsetFromANSICodepage(CultureInfo.GetCultureInfo(profile.Location)
.TextInfo.ANSICodePage);
var registries = profile.RedirectRegistry
? RegistryEntriesLoader.GetRegistryEntries(profile.IsAdvancedRedirection)
: null;
var l = new LoaderWrapper
{
ApplicationName = applicationName,
CommandLine = commandLine,
CurrentDirectory = currentDirectory,
AnsiCodePage = ansiCodePage,
OemCodePage = oemCodePage,
LocaleID = localeID,
DefaultCharset = defaultCharset,
HookUILanguageAPI = profile.IsAdvancedRedirection ? (uint) 1 : 0,
Timezone = profile.Timezone,
NumberOfRegistryRedirectionEntries = registries?.Length ?? 0,
DebugMode = profile.RunWithSuspend
};
registries?.ToList()
.ForEach(
item =>
l.AddRegistryRedirectEntry(item.Root,
item.Key,
item.Name,
item.Type,
item.GetValue(CultureInfo.GetCultureInfo(profile.Location))));
uint ret;
if ((ret = l.Start()) != 0)
{
if (SystemHelper.IsAdministrator())
{
GlobalHelper.ShowErrorDebugMessageBox(commandLine, ret);
}
else
{
ElevateProcess();
}
}
}
catch (Exception e)
{
MessageBox.Show(e.ToString());
}
}
private static void ElevateProcess()
{
try
{
SystemHelper.RunWithElevatedProcess(
Path.Combine(
Path.GetDirectoryName(
Assembly.GetExecutingAssembly()
.Location),
"LEProc.exe"),
Args);
}
catch (Exception)
{
}
}
private static int GetCharsetFromANSICodepage(int ansicp)
{
const int ANSI_CHARSET = 0;
const int DEFAULT_CHARSET = 1;
const int SYMBOL_CHARSET = 2;
const int SHIFTJIS_CHARSET = 128;
const int HANGEUL_CHARSET = 129;
const int HANGUL_CHARSET = 129;
const int GB2312_CHARSET = 134;
const int CHINESEBIG5_CHARSET = 136;
const int OEM_CHARSET = 255;
const int JOHAB_CHARSET = 130;
const int HEBREW_CHARSET = 177;
const int ARABIC_CHARSET = 178;
const int GREEK_CHARSET = 161;
const int TURKISH_CHARSET = 162;
const int VIETNAMESE_CHARSET = 163;
const int THAI_CHARSET = 222;
const int EASTEUROPE_CHARSET = 238;
const int RUSSIAN_CHARSET = 204;
const int MAC_CHARSET = 77;
const int BALTIC_CHARSET = 186;
var charset = ANSI_CHARSET;
switch (ansicp)
{
case 932: // Japanese
charset = SHIFTJIS_CHARSET;
break;
case 936: // Simplified Chinese
charset = GB2312_CHARSET;
break;
case 949: // Korean
charset = HANGEUL_CHARSET;
break;
case 950: // Traditional Chinese
charset = CHINESEBIG5_CHARSET;
break;
case 1250: // Eastern Europe
charset = EASTEUROPE_CHARSET;
break;
case 1251: // Russian
charset = RUSSIAN_CHARSET;
break;
case 1252: // Western European Languages
charset = ANSI_CHARSET;
break;
case 1253: // Greek
charset = GREEK_CHARSET;
break;
case 1254: // Turkish
charset = TURKISH_CHARSET;
break;
case 1255: // Hebrew
charset = HEBREW_CHARSET;
break;
case 1256: // Arabic
charset = ARABIC_CHARSET;
break;
case 1257: // Baltic
charset = BALTIC_CHARSET;
break;
}
return charset;
}
}
}