-
Notifications
You must be signed in to change notification settings - Fork 0
/
LaunchManager.cs
161 lines (134 loc) · 3.61 KB
/
LaunchManager.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
using System.Diagnostics;
using System.Text.RegularExpressions;
namespace TohoLauncher;
public class LaunchManager
{
const string EOSD_KANJI = "東方紅魔郷";
private readonly GlobalSettings? GlobalSettings;
private readonly GameSettings GameSettings;
private readonly string Game;
public LaunchManager(GlobalSettings? globalSettings,
GameSettings gameSettings)
{
GlobalSettings = globalSettings;
GameSettings = gameSettings;
Game = DetermineGame();
}
private string DetermineGame()
{
Console.Write("Checking game... ");
var gamePath = GameSettings.GamePath ??
throw new ArgumentNullException(
$"No game path for {GameSettings.GameName}");
var files = Directory.GetFiles(gamePath);
var fileNames = files.Select(x => Path.GetFileName(x));
var game = fileNames.FirstOrDefault(
x => Regex.IsMatch(x, @"^th\d{2,3}(tr)?\.dat$"));
if (game is null)
{
if (!fileNames.Any(x => x.StartsWith(EOSD_KANJI)))
throw new FileNotFoundException("Not a valid Toho game directory");
game = EOSD_KANJI;
}
else
game = game[0..^4];
Console.WriteLine("OK");
return game;
}
public void Launch()
{
if (GameSettings.SaveInGameFolder ?? false) SetAppData();
StartGame();
if (GameSettings.UseThPrac ?? false) ApplyPrac();
}
private void StartGame()
{
if (GameSettings.UseThCrap ?? false)
{
LaunchThCrap();
return;
}
if (GameSettings.UseVPatch ?? false)
{
LaunchExe("vpatch");
return;
}
LaunchExe(Game);
}
private void LaunchThCrap()
{
Console.WriteLine("Launching thcrap...");
CheckForPath(GlobalSettings?.ThCrapPath, "thcrap");
if (string.IsNullOrEmpty(GameSettings.ThCrapArg))
{
throw new ArgumentNullException("Missing parameter for thcrap");
}
var arg = GameSettings.ThCrapArg + " ";
arg += Game == EOSD_KANJI ? "th06" : Game;
Process.Start(GlobalSettings!.ThCrapPath!, arg);
}
private void LaunchExe(string name)
{
var gamePath = GameSettings.GamePath ??
throw new ArgumentNullException(
$"No game path for {GameSettings.GameName}");
var exeFile = $"{gamePath}\\{name}.exe";
Console.WriteLine("Launching " + exeFile);
if (!File.Exists(exeFile))
throw new FileNotFoundException("Can't find " + exeFile);
if (Game == EOSD_KANJI)
TryLaunchTroughLocale(exeFile);
else
Process.Start(exeFile);
}
private void TryLaunchTroughLocale(string exe)
{
var localePath = GlobalSettings?.LocaleEmulatorPath;
if (localePath is null)
Process.Start(exe);
else
Process.Start(localePath, $"\"{exe}\"");
}
private static void CheckForPath(string? path, string name)
{
if (string.IsNullOrEmpty(path))
{
throw new ArgumentNullException("Missing path for " + name);
}
}
private void ApplyPrac()
{
var pracPath = GlobalSettings?.ThPracPath;
CheckForPath(pracPath, "thprac");
Console.WriteLine("Applying thprac, waiting for the game to launch...");
if (!WaitForGameLaunch(10))
{
Console.WriteLine("Looks like the game is not launching properly.");
Console.WriteLine("Press any key to exit.");
Console.ReadKey();
return;
}
if (Game == EOSD_KANJI)
TryLaunchTroughLocale(pracPath!);
else
Process.Start(pracPath!);
}
private bool WaitForGameLaunch(int seconds)
{
var procName = Game.Replace("tr", string.Empty);
for (int i = 0; i < seconds; i++)
{
Thread.Sleep(1000);
if (Process.GetProcessesByName(procName).Any())
return true;
Console.WriteLine(".");
}
return false;
}
private static void SetAppData()
{
Console.WriteLine("Setting save location to game folder...");
Environment.SetEnvironmentVariable(
"APPDATA", Directory.GetCurrentDirectory() + "/save");
}
}