forked from darekasan/inf_launch_ext
-
Notifications
You must be signed in to change notification settings - Fork 1
/
inf_launch_ext.ps1
363 lines (298 loc) · 10.6 KB
/
inf_launch_ext.ps1
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
Add-Type -AssemblyName System.Windows.Forms
$ScriptName = [Regex]::Match(
$MyInvocation.InvocationName,
'[^\\]+\Z',
[System.Text.RegularExpressions.RegexOptions]::IgnoreCase -bor [System.Text.RegularExpressions.RegexOptions]::SingleLine
).Value
Write-Host "Executing: $ScriptName"
$ScriptIsUsta = $ScriptName -match '.*kr.ps1$'
if ($ScriptIsUsta) {
Write-Host "KR USTA mode"
$ScriptIsUsta = $true
} else {
Write-Host "JP eamusement mode"
$ScriptIsUsta = $false
}
# Use to get INFINITAS registry installation path
$InfRegistry = "HKLM:\SOFTWARE\KONAMI\beatmania IIDX INFINITAS"
# Path of the game itself Usually obtained from the registry
#$InfPath = "C:\Games\beatmania IIDX INFINITAS\"
$InfPath = Get-ItemPropertyValue -LiteralPath $InfRegistry -Name "InstallDir"
$InfExe = Join-Path $InfPath "\game\app\bm2dx.exe"
$InfLauncher = Join-Path $InfPath "\launcher\modules\bm2dx_launcher.exe"
cd $InfPath | Out-Null
# bm2dxinf:// registry
$InfOpen = "HKCR:bm2dxinf\shell\open\command\"
if ($ScriptIsUsta) {
$InfOpen = "HKCR:bm2dx-kr\shell\open\command\"
}
# full path to this script
$ScriptPath = $MyInvocation.MyCommand.Path
# setting file
$ConfigJson = Join-Path $PSScriptRoot "config.json"
$Config = [ordered]@{
"Option"="0"
"WindowWidth"="1280"
"WindowHeight"="720"
"WindowPositionX"="0"
"WindowPositionY"="0"
"Borderless"=$false
}
# window style
# see https://learn.microsoft.com/en-us/windows/win32/winmsg/window-styles
$WSDefault = 0x14CC0000
$WSBorderless = 0x14080000
# Define Win32 API functions
$Source = @"
using System;
using System.Runtime.InteropServices;
public class Win32Api {
[DllImport("user32.dll")]
public static extern int MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);
[DllImport("user32.dll")]
public static extern int SetWindowLong(IntPtr hWnd, int nIndex, long dwLong);
[DllImport("user32.dll")]
public static extern long GetWindowLong(IntPtr hWnd, int nIndex);
[DllImport("user32.dll")]
internal static extern bool GetWindowRect(IntPtr hwnd, out RECT lpRect);
[DllImport("user32.dll")]
internal static extern bool GetClientRect(IntPtr hwnd, out RECT lpRect);
[DllImport("User32.dll")]
public static extern int GetWindowThreadProcessId(IntPtr hWnd, out int lpdwProcessId);
[StructLayout(LayoutKind.Sequential)]
internal struct RECT
{
public int left, top, right, bottom;
}
public static void MoveWindow2(IntPtr hndl, int x, int y, int w, int h, bool isBl){
if(isBl){
MoveWindow(hndl, x, y, w, h, true);
}else{
RECT cRect = new RECT();
RECT wRect = new RECT();
GetClientRect(hndl, out cRect);
GetWindowRect(hndl, out wRect);
int cWidth = cRect.right - cRect.left;
int cHeight = cRect.bottom - cRect.top;
int wWidth = wRect.right - wRect.left;
int wHeight = wRect.bottom - wRect.top;
int newW = w + (wWidth - cWidth);
int newH = h + (wHeight - cHeight);
MoveWindow(hndl, x, y, newW, newH, true);
}
}
}
"@
Add-Type -TypeDefinition $Source -Language CSharp
function Save-Config() {
$Config | ConvertTo-Json | Out-File -FilePath $ConfigJson -Encoding utf8
}
function Start-Exe($exe, $workDir, $arg){
Write-Host "Start-Exe launching:`n EXE: $exe`n ARG: $arg`n DIR: $workDir`n"
$info = New-Object System.Diagnostics.ProcessStartInfo
$info.FileName = $exe
$info.WorkingDirectory = $workDir
$info.Arguments = $arg
$info.UseShellExecute = $false
$p = New-Object System.Diagnostics.Process
$p.StartInfo = $info
$p.Start() | Out-Null
return $p
}
function Switch-Borderless($isBl){
if ($isBl) {
[Win32Api]::SetWindowLong($handle, -16, $WSBorderless) | Out-Null
}else{
[Win32Api]::SetWindowLong($handle, -16, $WSDefault) | Out-Null
}
}
function Get-Monitor($monitor_number){
$monitor_number = [int]$monitor_number
# https://stackoverflow.com/questions/7967699/get-screen-resolution-using-wmi-powershell-in-windows-7
# get a lit of monitors...
$screens = [system.windows.forms.screen]::AllScreens
# find primary monitor first
$primary = $screens[0]
$col_screens | ForEach-Object {
if ("$($_.Primary)" -eq "True") {
$primary = $_
break
}
}
# 0 = primary, 1 and up = monitor number
if ($monitor_number -eq 0) {
return $primary
}
if ($monitor_number -gt $screens.Count) {
return $primary
}
return $screens[$monitor_number - 1]
}
# change registry when no argument is specified
if ([string]::IsNullOrEmpty($Args[0])) {
echo("Script install / uninstall")
if (!(New-Object Security.Principal.WindowsPrincipal(
[Security.Principal.WindowsIdentity]::GetCurrent()
)).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
Write-Host "ERROR: please run the script as administrator!" -ForegroundColor Red
Pause
exit;
}
New-PSDrive -Name HKCR -PSProvider Registry -Root HKEY_CLASSES_ROOT | Out-Null
$val = Get-ItemPropertyValue -LiteralPath $InfOpen -Name "(default)"
echo("current command: " + $val)
echo ""
echo("script path: " + $ScriptPath)
echo("game path: " + $InfPath)
echo ""
echo "0 : revert to default (uninstall)"
echo "1 : set to this script path"
echo "3 : copy script file to game directory and set to new script path (install)"
$num = Read-Host "number->"
switch ($num) {
0 {
$val = """${InfLauncher}"" ""%1"""
}
1 {
$val = """powershell"" ""-file"" ""${ScriptPath}"" ""%1"""
}
3 {
$From = Join-Path $PSScriptRoot $ScriptName
Copy-Item -Path $From -Destination $InfPath
$From = Join-Path $PSScriptRoot "infzoom.exe"
Copy-Item -Path $From -Destination $InfPath
$From = Join-Path $PSScriptRoot "infzoom.ini"
Copy-Item -Path $From -Destination $InfPath
$NewScriptPath = Join-Path $InfPath $ScriptName
$val = """powershell"" ""-file"" ""${NewScriptPath}"" ""%1"""
}
Default {
exit
}
}
Set-ItemProperty $InfOpen -name "(default)" -value $val
echo "done. Press enter key to exit."
Read-Host
exit
}
# Something to start the game from here
# read configuration file
if(Test-Path $ConfigJson){
$Config = [ordered]@{}
(ConvertFrom-Json (Get-Content $ConfigJson -Encoding utf8 -Raw )).psobject.properties | Foreach { $Config[$_.Name] = $_.Value }
}
# Arguments to pass to the game itself
$InfArgs = ""
# pick up the token from the arguments
$Args[0] -match "tk=(.{64})" | Out-Null
$InfArgs += " -t "+$Matches[1]
# add --trial for trial mode
if ($Args[0].Contains("trial")) {
$InfArgs += " --trial"
}
echo "Please select option."
echo "0 : Launcher (required for game updates)"
echo "1 : WASAPI"
echo "2 : WASAPI + window mode (60Hz only)"
echo "3 : ASIO"
echo "4 : ASIO + window mode (60Hz only)"
echo "5 : WASAPI + fullscreen borderless (60Hz only) with zoom"
echo "6 : ASIO + fullscreen borderless (60Hz only) with zoom"
$num = Read-Host "number (press enter for option $($Config["Option"]))"
if([string]::IsNullOrEmpty($num)){
$num=$Config["Option"]
}
$FullScreenBorderlessWithZoom = $false
switch ($num) {
0 {
Start-Process -FilePath $InfLauncher -ArgumentList $Args[0]
exit
}
1 {
}
2 {
$InfArgs += " -w"
}
3 {
$InfArgs += " --asio"
}
4 {
$InfArgs += " -w"
$InfArgs += " --asio"
}
5 {
$InfArgs += " -w"
$FullScreenBorderlessWithZoom = $true
}
6 {
$InfArgs += " -w"
$InfArgs += " --asio"
$FullScreenBorderlessWithZoom = $true
}
Default {
exit
}
}
if ($ScriptIsUsta) {
$InfArgs += " --kr"
}
# save settings
$Config["Option"] = [string]$num
Save-Config
# start INFINITAS
$p = Start-Exe $InfExe "" $InfArgs
# in window mode...
if ($InfArgs.Contains("-w")){
# wait for window creation
$p.WaitForInputIdle() | Out-Null
$handle = $p.MainWindowHandle
if ($FullScreenBorderlessWithZoom) {
# we let the separate EXE handle everything for this mode
$InfZoomExe = Join-Path $PSScriptRoot "infzoom.exe"
$infzoom_p = Start-Exe $InfZoomExe $PSScriptRoot $handle
$infzoom_p.WaitForExit() | Out-Null
Pause
} else {
# set to previous position and size
Switch-Borderless($Config["Borderless"])
[Win32Api]::MoveWindow2(
$handle,
$Config["WindowPositionX"],
$Config["WindowPositionY"],
$Config["WindowWidth"],
$Config["WindowHeight"],
$Config["Borderless"])
echo ""
echo "window mode setting"
echo "example:"
echo " window size -> type 1280x720"
echo " window position -> type 100,100"
echo "Press enter key to switch to Borderless window, or use mouse cursor to resize window"
while($true){
$inputStr=Read-Host " "
if([string]::IsNullOrEmpty($inputStr)){
$Config["Borderless"] = (-Not $Config["Borderless"])
}elseif($inputStr.Contains("x")){
$val = $inputStr.Split('x')
$Config["WindowWidth"]=$val[0]
$Config["WindowHeight"]=$val[1]
}elseif($inputStr.Contains(",")){
$val = $inputStr.Split(',')
$Config["WindowPositionX"]=$val[0]
$Config["WindowPositionY"]=$val[1]
}
# make borderless
Switch-Borderless($Config["Borderless"])
# Reflect position and size
[Win32Api]::MoveWindow2(
$handle,
$Config["WindowPositionX"],
$Config["WindowPositionY"],
$Config["WindowWidth"],
$Config["WindowHeight"],
$Config["Borderless"])
# write to config file
Save-Config
}
}
}