-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfigure.ps1
More file actions
356 lines (306 loc) · 14.8 KB
/
configure.ps1
File metadata and controls
356 lines (306 loc) · 14.8 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
<#
.SYNOPSIS
A script to configure the project by generating cfg-defines.h and preparing necessary folders.
.DESCRIPTION
This script generates cfg-defines.h based on the build type (Debug or Release) and user input.
In Debug mode, it prompts the user to select the LBA2 Common directory, ensuring it contains LBA2.HQR.
.PARAMETER BuildType
Specifies the build type. Acceptable values are "Debug" or "Release".
.EXAMPLE
./configure.ps1 -BuildType Debug
#>
param (
[string]$BuildType = "Release"
)
# Ensure the BuildType parameter is valid
if ($BuildType -notin @("Debug", "Release")) {
Write-Error "Invalid BuildType. Use 'Debug' or 'Release'."
exit 1
}
# Define hardcoded revisions for git submodules (used when downloading from ZIP)
# IMPORTANT: Update these revisions when git modules are updated in the repository
$submoduleRevisions = @{
"SDL" = "fa24d868ac2f8fd558e4e914c9863411245db8fd"
"soloud" = "e82fd32c1f62183922f08c14c814a02b58db1873"
}
# Function to show folder selection dialog
function Select-Folder {
Add-Type -AssemblyName System.Windows.Forms
$dialog = New-Object System.Windows.Forms.FolderBrowserDialog
$dialog.Description = "Select LBA2 Common directory (where HQR files are):"
$dialog.ShowNewFolderButton = $true
if ($dialog.ShowDialog() -eq [System.Windows.Forms.DialogResult]::OK) {
return $dialog.SelectedPath
}
else {
Write-Host "No folder selected. Exiting." -ForegroundColor Yellow
exit 1
}
}
# Function to validate directory contains LBA2.HQR
function Validate-LBA2Directory($directory) {
$requiredFile = Join-Path $directory "LBA2.HQR"
return Test-Path $requiredFile
}
# Function to normalize paths and ensure they end with a single backslash and wrapped in quotes
function Normalize-Path($path) {
$normalizedPath = $path.TrimEnd('\') + '\'
$normalizedPath = $normalizedPath -replace '\\+', '\\'
return "`"$normalizedPath`""
}
# Runs "git submodule update --init --recursive" and handles the git-worktree/network-drive failure
# gracefully by warning the user and prompting whether to continue.
# Returns $true if the caller should proceed, $false if the caller should abort.
function Invoke-GitSubmoduleUpdate {
$output = git submodule update --init --recursive 2>&1
if ($LASTEXITCODE -eq 0) {
Write-Host "[v] Git submodules initialized successfully." -ForegroundColor Green
return $true
}
$outputText = $output -join "`n"
# Detect the specific worktree-not-accessible error
$isWorktreeError = $outputText -match 'not a git repository.*[/\\]worktrees[/\\]'
if ($isWorktreeError) {
Write-Host ""
Write-Host "[!] Git submodule initialization failed because the main repository folder" -ForegroundColor Yellow
Write-Host " is not accessible from this machine." -ForegroundColor Yellow
Write-Host ""
Write-Host " You appear to be working in a git worktree mounted from a network drive or similar scenario." -ForegroundColor Yellow
Write-Host " Git requires access to the main '.git' folder to manage submodules, but" -ForegroundColor Yellow
Write-Host " that folder cannot be reached from this machine." -ForegroundColor Yellow
Write-Host ""
Write-Host " To fix this, run the following command on the machine where the main" -ForegroundColor Cyan
Write-Host " repository folder IS accessible:" -ForegroundColor Cyan
Write-Host ""
Write-Host " git submodule update --init --recursive" -ForegroundColor White
Write-Host ""
Write-Host " After the submodules are checked out there, continue the script on this machine by choosing [Y]." -ForegroundColor Yellow
Write-Host ""
$answer = Read-Host "If you have already initialized the submodules elsewhere, do you want to continue the script? (Y/N)"
if ($answer -match '^[Yy]$') {
Write-Host "[~] Continuing with manual submodule initialization." -ForegroundColor Yellow
# Reset $LASTEXITCODE so the git failure code (128) does not propagate to callers
$global:LASTEXITCODE = 0
return $true
}
else {
Write-Host "[x] Aborted by user." -ForegroundColor Red
return $false
}
}
# Generic failure
Write-Host "[x] Failed to initialize git submodules:" -ForegroundColor Red
Write-Host $outputText -ForegroundColor Red
Write-Host "This is required for the project to build properly." -ForegroundColor Red
return $false
}
$isDebug = $BuildType -eq "Debug"
# if packages.config doesn't exist in Ida, warning the user
if (-not (Test-Path (Join-Path "Ida" "packages.config"))) {
Write-Host "Warning: packages.config not found in Ida directory. The project might not build if it depends on any NuGet packages." -ForegroundColor Yellow
}
else {
Copy-Item -Path (Join-Path "Ida" "packages.config") -Destination (Join-Path "SOURCES" "packages.config") -Force
}
# Prompt user to select LBA2 Common directory (Debug mode only)
$pathResource = ""
# if ($isDebug) {
do {
Write-Host "Please select the LBA2 Common directory from your Steam or GoG game installation. This configuration script or IdaJS will not modify any files in your game directory. It will only read the game assets from it." -ForegroundColor Yellow
Write-Host "Opening folder selection dialog for LBA2 Common directory..."
$pathResource = Select-Folder
if (-not (Validate-LBA2Directory $pathResource)) {
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.MessageBox]::Show("LBA2.HQR not found in the selected directory. Please select a valid LBA2 Common directory.", "Error", [System.Windows.Forms.MessageBoxButtons]::OK, [System.Windows.Forms.MessageBoxIcon]::Error) | Out-Null
}
} while (-not (Validate-LBA2Directory $pathResource))
Write-Host "Selected valid LBA2 Common directory: $pathResource" -ForegroundColor Green
#}
$scriptDirectory = Split-Path -Parent $MyInvocation.MyCommand.Definition
# if ($isDebug) {
# Create necessary folders if they don't exist
$gameRunFolders = @(
"GameRun",
"GameRun\\save",
"GameRun\\save\\shoot",
"GameRun\\bugs",
"GameRun\\mods",
"Debug",
"Release"
)
foreach ($folder in $gameRunFolders) {
$fullPath = Join-Path $scriptDirectory $folder
if (-not (Test-Path $fullPath)) {
New-Item -ItemType Directory -Path $fullPath -Force | Out-Null
}
}
# Copy LBA2.cfg from SOURCES to the appropriate build folder only if it doesn't already exist
$buildFolder = if ($isDebug) { "Debug" } else { "Release" }
$buildConfigPath = Join-Path $buildFolder "LBA2.cfg"
if (-not (Test-Path $buildConfigPath)) {
Copy-Item -Path (Join-Path "SOURCES" "LBA2.cfg") -Destination $buildConfigPath
}
# In Release mode, modify the config file to enable fullscreen by default
if (-not $isDebug -and (Test-Path $buildConfigPath)) {
$configContent = Get-Content $buildConfigPath -Raw
$configContent = $configContent -replace "FullScreen: 0", "FullScreen: 1"
Set-Content -Path $buildConfigPath -Value $configContent -NoNewline
}
$pathResource = Normalize-Path $pathResource
$pathSave = Normalize-Path (Join-Path $scriptDirectory "GameRun\save\")
$pathPcxSave = Normalize-Path (Join-Path $scriptDirectory "GameRun\save\shoot\")
$pathSaveBugs = Normalize-Path (Join-Path $scriptDirectory "GameRun\bugs\")
$pathMods = Normalize-Path (Join-Path $scriptDirectory "GameRun\mods\")
$displayFps = if ($isDebug) { 1 } else { 0 }
$logLevel = if ($isDebug) { "LogLevel::DEBUG" } else { "LogLevel::INFO" }
# }
# This would prepare a distributable version, but redistribution of the derived binary might be not allowed if user uses non-GPLv2 compliant libraries
# else {
# $pathResource = Normalize-Path "Common"
# $buildFolder = Join-Path $scriptDirectory "dist"
# if (Test-Path $buildFolder) {
# Remove-Item -Recurse -Force -Path $buildFolder
# }
# New-Item -ItemType Directory -Path $buildFolder | Out-Null
# # Create the required folder structure inside the build folder
# $buildSubfolders = @(
# "save",
# "save\\shoot",
# "bugs",
# "mods"
# )
# foreach ($folder in $buildSubfolders) {
# $fullPath = Join-Path $buildFolder $folder
# New-Item -ItemType Directory -Path $fullPath | Out-Null
# }
# # Copy LBA2.cfg from SOURCES to the build folder
# Copy-Item -Path (Join-Path "SOURCES" "LBA2.cfg") -Destination (Join-Path $buildFolder "LBA2.cfg") -Force
# }
# Path to template and output files
$templateFile = "configure.defines.h"
$outputFile = "cfg-defines.h"
# Ensure the template file exists
if (-not (Test-Path $templateFile)) {
Write-Error "Template file '$templateFile' not found. Ensure it exists in the project directory."
exit 1
}
# Generate the cfg-defines.h file
Write-Host "Generating $outputFile with the following values:"
Write-Host " PATH_RESOURCE: $pathResource"
Write-Host " PATH_SAVE: $pathSave"
Write-Host " PATH_PCX_SAVE: $pathPcxSave"
Write-Host " PATH_MODS: $pathMods"
Write-Host " PATH_SAVE_BUGS: $pathSaveBugs"
Write-Host " DISPLAY_FPS: $displayFps"
Write-Host " LOGLEVEL: $logLevel"
$templateContent = Get-Content $templateFile -Raw
$templateContent = $templateContent -replace "\$\{PATH_RESSOURCE\}", $pathResource
$templateContent = $templateContent -replace "\$\{PATH_SAVE\}", $pathSave
$templateContent = $templateContent -replace "\$\{PATH_PCX_SAVE\}", $pathPcxSave
$templateContent = $templateContent -replace "\$\{PATH_MODS\}", $pathMods
$templateContent = $templateContent -replace "\$\{PATH_SAVE_BUGS\}", $pathSaveBugs
$templateContent = $templateContent -replace "\$\{DISPLAY_FPS\}", $displayFps
$templateContent = $templateContent -replace "\$\{LOGLEVEL\}", $logLevel
$outputFileSources = Join-Path "SOURCES" $outputFile
Set-Content -Path $outputFileSources -Value $templateContent
Write-Host "$outputFile has been successfully generated." -ForegroundColor Green
# Initialize git submodules
Write-Host "Initializing git submodules..." -ForegroundColor Yellow
# Check if we're in a git repository
$isGitRepo = Test-Path ".git"
if ($isGitRepo) {
# We're in a git repository, use standard git submodule command
if (-not (Invoke-GitSubmoduleUpdate)) {
exit 1
}
}
else {
# Not in a git repository (downloaded as ZIP), manually clone submodules
Write-Host "Not in a git repository. Manually downloading submodules..." -ForegroundColor Yellow
# Read .gitmodules file to get submodule paths and URLs
$gitmodulesFile = ".gitmodules"
if (-not (Test-Path $gitmodulesFile)) {
throw ".gitmodules file not found. Cannot determine submodule configuration."
}
# Parse .gitmodules file
$gitmodulesContent = Get-Content $gitmodulesFile
$submodules = @{}
$currentSubmodule = $null
foreach ($line in $gitmodulesContent) {
$line = $line.Trim()
if ($line -match '^\[submodule "(.+)"\]$') {
$currentSubmodule = $matches[1]
$submodules[$currentSubmodule] = @{}
}
elseif ($line -match '^\s*path\s*=\s*(.+)$' -and $currentSubmodule) {
$submodules[$currentSubmodule].path = $matches[1].Trim()
}
elseif ($line -match '^\s*url\s*=\s*(.+)$' -and $currentSubmodule) {
$submodules[$currentSubmodule].url = $matches[1].Trim()
}
}
try {
foreach ($submoduleName in $submodules.Keys) {
$submoduleInfo = $submodules[$submoduleName]
$submodulePath = $submoduleInfo.path
$submoduleUrl = $submoduleInfo.url
$submoduleRevision = $submoduleRevisions[$submoduleName]
if (-not $submodulePath -or -not $submoduleUrl) {
Write-Warning " ⚠ Incomplete configuration for submodule '$submoduleName', skipping..."
continue
}
if (-not $submoduleRevision) {
Write-Warning " ⚠ No hardcoded revision for submodule '$submoduleName', skipping..."
continue
}
# Check if directory exists and is not empty (excluding hidden/system entries)
if (Test-Path $submodulePath) {
$nonHiddenItems = Get-ChildItem $submodulePath | Where-Object { ($_.Attributes -band [IO.FileAttributes]::Hidden) -eq 0 -and ($_.Attributes -band [IO.FileAttributes]::System) -eq 0 }
if ($nonHiddenItems.Count -gt 0) {
Write-Host " [v] $submoduleName directory already exists and is not empty, skipping clone." -ForegroundColor Green
continue
}
}
Write-Host " → Cloning $submoduleName to $submodulePath..." -ForegroundColor Gray
# Remove existing directory if it exists (it would be empty at this point)
if (Test-Path $submodulePath) {
Remove-Item -Recurse -Force $submodulePath
}
# Clone the repository
git clone $submoduleUrl $submodulePath
if ($LASTEXITCODE -ne 0) {
throw "Failed to clone $submoduleName from $submoduleUrl"
}
# Checkout specific revision
Push-Location $submodulePath
try {
git checkout $submoduleRevision
if ($LASTEXITCODE -ne 0) {
throw "Failed to checkout revision $submoduleRevision for $submoduleName"
}
Write-Host " [v] $submoduleName cloned and checked out to revision $submoduleRevision." -ForegroundColor Green
}
finally {
Pop-Location
}
}
Write-Host "[v] All submodules downloaded successfully." -ForegroundColor Green
}
catch {
Write-Host "[x] Failed to download submodules: $($_.Exception.Message)" -ForegroundColor Red
Write-Host "This is required for the project to build properly." -ForegroundColor Red
exit 1
}
}
# Persist build configuration for subsequent build.ps1 runs
try {
$configState = @{ buildType = $BuildType }
$configJsonPath = Join-Path $scriptDirectory "configure.build.json"
$configState | ConvertTo-Json -Depth 3 | Set-Content -Encoding UTF8 $configJsonPath
Write-Host "Saved build configuration to $configJsonPath" -ForegroundColor DarkGreen
}
catch {
Write-Warning "Failed to write configure.build.json: $($_.Exception.Message)"
}
Write-Host "You can now build the game by running .\build.ps1" -ForegroundColor Green
exit 0