-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFunctions.cs
More file actions
469 lines (436 loc) · 19 KB
/
Functions.cs
File metadata and controls
469 lines (436 loc) · 19 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
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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
using Microsoft.WindowsAPICodePack.Dialogs;
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Drawing;
using System.Text;
namespace ReShade_Centralized
{
public static class Functions
{
public static void readRCIni()
{
using (StreamReader r = new StreamReader(@".\ReShadeCentralized.ini"))
{
int lineNum = 0;
Program.iniEntry line = new Program.iniEntry();
while (!r.EndOfStream)
{
line.value = r.ReadLine();
lineNum++;
if (line.value.StartsWith("shaders="))
{
try
{
Program.shaders = Path.GetFullPath(line.value.Substring(8));
}
catch
{
MessageBox.Show(@"Fatal Error: Invalid shaders path in ReShadeCentralized.ini. Manually fix the path and try running again.");
Application.Exit();
}
}
else if (line.value.StartsWith("textures="))
{
try
{
Program.textures = Path.GetFullPath(line.value.Substring(9));
}
catch
{
MessageBox.Show(@"Fatal Error: Invalid textures path in ReShadeCentralized.ini. Manually fix the path and try running again.");
Application.Exit();
}
}
else if (line.value.StartsWith("presets="))
{
try
{
Program.presets = Path.GetFullPath(line.value.Substring(8));
}
catch
{
MessageBox.Show(@"Fatal Error: Invalid presets path in ReShadeCentralized.ini. Manually fix the path and try running again.");
Application.Exit();
}
}
else if (line.value.StartsWith("screenshots="))
{
try
{
Program.screenshots = Path.GetFullPath(line.value.Substring(12));
}
catch
{
MessageBox.Show(@"Fatal Error: Invalid screenshots path in ReShadeCentralized.ini. Manually fix the path and try running again.");
Application.Exit();
}
}
else if (line.value.StartsWith("dlls="))
{
try
{
Program.dlls = Path.GetFullPath(line.value.Substring(5));
}
catch
{
MessageBox.Show(@"Fatal Error: Invalid dlls path in ReShadeCentralized.ini. Manually fix the path and try running again.");
Application.Exit();
}
}
else if (line.value.StartsWith("mdlls="))
{
try
{
Program.mdlls = Path.GetFullPath(line.value.Substring(6));
}
catch
{
MessageBox.Show(@"Fatal Error: Invalid mdlls path in ReShadeCentralized.ini. Manually fix the path and try running again.");
Application.Exit();
}
}
}
r.Close();
}
}
public static void overwriteIni(string path, List<string> var, List<string> value)
{
//Read file into memory
List<string> file = new List<string>();
using (StreamReader r = new StreamReader(path))
{
while (!r.EndOfStream)
{
file.Add(r.ReadLine());
}
}
//Write new text to file
using (StreamWriter w = new StreamWriter(path))
{
int filecount = file.Count;
int varcount = var.Count; //var and value will always be the same size
for (int i = 0; i < filecount; i++)
{
for (int j = 0; j < varcount; j++)
{
if (file[i].StartsWith(var[j]))
{
file[i] = var[j] + value[j];
}
}
w.WriteLine(file[i]);
}
}
}
public static void writetoIni(string path, string header, string var) //Adds a new line after header is encountered
{
StringBuilder sb = new StringBuilder();
using (StreamReader r = new StreamReader(path))
{
string line;
do
{
line = r.ReadLine();
sb.AppendLine(line);
} while (!r.EndOfStream && !line.Contains(header));
sb.Append(var);
sb.Append(System.Environment.NewLine);
sb.Append(r.ReadToEnd());
}
using (StreamWriter w = new StreamWriter(path))
{
w.Write(sb.ToString());
}
}
public static CommonOpenFileDialog getUserDirectoryCommon(CommonOpenFileDialog dir)
{
bool exitLoop = false;
while (exitLoop == false)
{
CommonFileDialogResult result = dir.ShowDialog();
if (!(result == CommonFileDialogResult.Ok && !string.IsNullOrWhiteSpace(dir.FileName)))
{
MessageBox.Show("You must select a valid directory, try again");
}
else
{
exitLoop = true;
}
}
return dir;
}
public static string getGameName()
{
GetInput g = new GetInput(@"Please enter Game Name for folder creation.");
g.Text = @"Enter Game Name";
g.ShowDialog();
while (string.IsNullOrEmpty(g.gameName))
{
MessageBox.Show("Game Name can't be blank, try again.");
g.ShowDialog();
}
return g.gameName;
}
public static string getGameExeName()
{
GetInput g = new GetInput(@"Launch the UWP game and open the task manager. Locate the .exe from the Details tab. Type the name here (example: Game.exe)");
g.Text = @"Enter Game.exe";
g.ShowDialog();
while (string.IsNullOrEmpty(g.gameName))
{
MessageBox.Show("Entry can't be blank, try again.");
g.ShowDialog();
}
return g.gameName;
}
//public static OpenFileDialog getUserDirectory(OpenFileDialog dir)
//{
// bool exitLoop = false;
// while (exitLoop == false)
// {
// DialogResult result = dir.ShowDialog();
// if (!(result == DialogResult.OK && !string.IsNullOrWhiteSpace(dir.FileName)))
// {
// MessageBox.Show("You must select a valid file, try again");
// }
// else
// {
// exitLoop = true;
// }
// }
// return dir;
//}
public static void deployReshadeConfigs(string gameName, string workingDLLPath, string installPath)
{
DialogResult overwrite;
if (File.Exists(Program.presets + @"\" + gameName + @"\reshade.ini") || File.Exists(Program.presets + @"\" + gameName + @"\ReShade.ini"))
{
overwrite = MessageBox.Show("ReShade.ini Detected. Would you like to overwrite?", "Warning", MessageBoxButtons.YesNo);
}
else
{
overwrite = DialogResult.Yes;
}
if (overwrite == DialogResult.Yes)
{
if (File.Exists(installPath + @"\reshade.ini"))
{
File.Delete(installPath + @"\reshade.ini");
}
Functions.writeReshadeini(Program.presets + @"\" + gameName, workingDLLPath, gameName, installPath);
}
if (!File.Exists(Program.presets + @"\" + gameName + @"\ReshadePreset.ini"))
{
string nl = "\n"; //makes typing up that multiline write a bit easier
File.WriteAllText(Program.presets + @"\" + gameName + @"\ReshadePreset.ini",
@"PreprocessorDefinitions=" + nl +
@"Techniques=" + nl +
@"TechniqueSorting=DisplayDepth"
);
}
else
{
MessageBox.Show("ReshadePreset.ini detected. File has not been overwritten.");
}
}
public static void deployReshadeFilesInjector(string source, string dest, bool bit64) //
{
string inject = @"\inject32.exe";
string reshade = @"\ReShade32.dll";
if (bit64)
{
inject = @"\inject64.exe";
reshade = @"\ReShade64.dll";
}
SymbolicLink.CreateSymbolicLink(dest + @"\inject.exe", source + inject, 0);
SymbolicLink.CreateSymbolicLink(dest + reshade, source + reshade, 0);
}
public static void writeReshadeini(string dest, string workingDLLPath, string gameName, string gameDir)
{
if (!File.Exists(@".\ReShade_Template.ini"))
{
MessageBox.Show(@"ReShade_Template.ini not found, installing with old method. Rename ReShade_Template_Example.ini to ReShade_Template.ini to use the new method.");
writeReshadeiniFallback(dest, workingDLLPath, gameName, gameDir);
return;
}
List<string> reshadeconfig = new List<string>();
using (StreamReader r = new StreamReader(@".\ReShade_Template.ini"))
{
while (!r.EndOfStream)
{
string line = r.ReadLine();
if (line.StartsWith(@"EffectSearchPaths="))
{
line = @"EffectSearchPaths=" + Program.shaders + @"," + Program.shaders + @"\development";
}
else if (line.StartsWith(@"IntermediateCachePath="))
{
line = @"IntermediateCachePath=" + workingDLLPath + @"\Cache";
}
else if (line.StartsWith(@"PresetPath="))
{
line = @"PresetPath=" + Program.presets + @"\" + gameName + @"\ReshadePreset.ini";
}
else if (line.StartsWith(@"TextureSearchPaths="))
{
line = @"TextureSearchPaths=" + Program.textures + @"," + Program.textures + @"\development";
}
else if (line.StartsWith(@"SavePath="))
{
line = @"SavePath=" + Program.screenshots + @"\" + gameName;
}
reshadeconfig.Add(line);
}
}
using (StreamWriter w = new StreamWriter(dest + @"\reshade.ini"))
{
foreach(string line in reshadeconfig)
{
w.WriteLine(line);
}
}
SymbolicLink.CreateSymbolicLink(gameDir + @"\reshade.ini", dest + @"\reshade.ini", 0);
}
public static void writeReshadeiniFallback(string dest, string workingDLLPath, string gameName, string gameDir) //Used if reshade_template.ini is missing
{
string nl = "\n"; //makes typing up that multiline write a bit easier
File.WriteAllText(dest + @"\reshade.ini",
@"[GENERAL]" + nl +
@"EffectSearchPaths=" + Program.shaders + @"," + Program.shaders + @"\development" + nl +
@"IntermediateCachePath=" + workingDLLPath + @"\Cache" + nl +
@"PerformanceMode=0" + nl +
@"PreprocessorDefinitions=RESHADE_DEPTH_INPUT_IS_REVERSED=0,RESHADE_DEPTH_INPUT_IS_LOGARITHMIC=0,RESHADE_DEPTH_INPUT_IS_UPSIDE_DOWN=0,RESHADE_DEPTH_LINEARIZATION_FAR_PLANE=1000" + nl +
@"PresetPath=" + Program.presets + @"\" + gameName + @"\ReshadePreset.ini" + nl +
@"PresetTransitionDelay=1000" + nl +
@"SkipLoadingDisabledEffects=0" + nl +
@"TextureSearchPaths=" + Program.textures + @"," + Program.textures + @"\development" + nl + nl +
@"[INPUT]" + nl +
@"ForceShortcutModifiers=1" + nl +
@"InputProcessing=2" + nl +
@"KeyEffects=145,0,0,0" + nl +
@"KeyNextPreset=0,0,0,0" + nl +
@"KeyOverlay=36,0,0,0" + nl +
@"KeyPerformanceMode=0,0,0,0" + nl +
@"KeyPreviousPreset=0,0,0,0" + nl +
@"KeyReload=0,0,0,0" + nl +
@"KeyScreenshot=44,0,0,0" + nl + nl +
@"[OVERLAY]" + nl +
@"ClockFormat=0" + nl +
@"FPSPosition=1" + nl +
@"NoFontScaling=1" + nl +
@"SaveWindowState=0" + nl +
@"ShowClock=0" + nl +
@"ShowForceLoadEffectsButton=1" + nl +
@"ShowFPS=0" + nl +
@"ShowFrameTime=0" + nl +
@"ShowScreenshotMessage=1" + nl +
@"TutorialProgress=4" + nl +
@"VariableListHeight=300.000000" + nl +
@"VariableListUseTabs=0" + nl + nl +
@"[SCREENSHOT]" + nl +
@"ClearAlpha=1" + nl +
@"FileFormat=1" + nl +
@"FileNamingFormat=0" + nl +
@"JPEGQuality=90" + nl +
@"SaveBeforeShot=1" + nl +
@"SaveOverlayShot=0" + nl +
@"SavePath=" + Program.screenshots + @"\" + gameName + nl +
@"SavePresetFile=0" + nl + nl +
@"[STYLE]" + nl +
@"Alpha=1.000000" + nl +
@"ChildRounding=0.000000" + nl +
@"ColFPSText=1.000000,1.000000,0.784314,1.000000" + nl +
@"EditorFont=ProggyClean.ttf" + nl +
@"EditorFontSize=13" + nl +
@"EditorStyleIndex=0" + nl +
@"Font=ProggyClean.ttf" + nl +
@"FontSize=13" + nl +
@"FPSScale=1.000000" + nl +
@"FrameRounding=0.000000" + nl +
@"GrabRounding=0.000000" + nl +
@"PopupRounding=0.000000" + nl +
@"ScrollbarRounding=0.000000" + nl +
@"StyleIndex=2" + nl +
@"TabRounding=4.000000" + nl +
@"WindowRounding=0.000000"
);
SymbolicLink.CreateSymbolicLink(gameDir + @"\reshade.ini", dest + @"\reshade.ini", 0);
}
public static void moveWithReplace(string source, string dest)
{
if (File.Exists(dest))
{
File.Delete(dest);
}
string workingDir = Path.GetDirectoryName(dest);
if (!Directory.Exists(workingDir))
{
Directory.CreateDirectory(workingDir);
}
File.Move(source, dest);
}
public static void moveFiles(string source, string dest, string[] extensions) //enumerates all files of given extensions in source directory and subdirectories and moves them to dest directory, effectively collapsing folder structure.
{
DirectoryInfo d = new DirectoryInfo(source);
var files =
d.EnumerateFiles("*", SearchOption.AllDirectories)
.Where(f => extensions.Contains(f.Extension.ToLower()))
.ToArray();
foreach (var file in files)
{
moveWithReplace(file.FullName, dest + @"\" + file.Name);
}
}
public static void copyFiles(string source, string dest, string[] extensions) //enumerates all files of given extensions in source directory and subdirectories and moves them to dest directory, effectively collapsing folder structure.
{
DirectoryInfo d = new DirectoryInfo(source);
if (!Directory.Exists(dest))
{
Directory.CreateDirectory(dest);
}
var files =
d.EnumerateFiles("*", SearchOption.AllDirectories)
.Where(f => extensions.Contains(f.Extension.ToLower()))
.ToArray();
foreach (var file in files)
{
File.Copy(file.FullName, dest + @"\" + file.Name, true);
}
}
public static void extractAllZip(string source, string dest, string[] extensions)
{
DirectoryInfo d = new DirectoryInfo(source);
var files =
d.EnumerateFiles("*", SearchOption.AllDirectories)
.Where(f => extensions.Contains(f.Extension.ToLower()))
.ToArray();
foreach (var file in files)
{
ZipArchiveExtensions.ExtractToDirectory(ZipFile.OpenRead(file.FullName), dest, true);
}
}
public enum MachineType
{
Native = 0, I386 = 0x014c, Itanium = 0x0200, x64 = 0x8664
}
public static MachineType GetMachineType(string fileName)
{
const int PE_POINTER_OFFSET = 60;
const int MACHINE_OFFSET = 4;
byte[] data = new byte[4096];
using (Stream s = new FileStream(fileName, FileMode.Open, FileAccess.Read))
{
s.Read(data, 0, 4096);
}
// dos header is 64 bytes, last element, long (4 bytes) is the address of the PE header
int PE_HEADER_ADDR = BitConverter.ToInt32(data, PE_POINTER_OFFSET);
int machineUint = BitConverter.ToUInt16(data, PE_HEADER_ADDR + MACHINE_OFFSET);
return (MachineType)machineUint;
}
}
}