-
Notifications
You must be signed in to change notification settings - Fork 1
/
LC_LaunchProfile.ps1
executable file
·308 lines (240 loc) · 10.2 KB
/
LC_LaunchProfile.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
<#
.SYNOPSIS
PowerShell script to launch Lethal Company profiles easily.
.DESCRIPTION
This script facilitates the launching of Lethal Company with custom configurations.
It allows the user to select a game profile, define the number of concurrent game windows,
and set the size of these windows. It reads and saves configurations to a JSON file.
.NOTES
File Name : LC_LaunchProfile.ps1
Author : Ricky Davis
License : MIT License
Version : 1.0.0
.DISCLAIMER
This script is provided "as is", without warranty of any kind, express or implied.
In no event shall the author be liable for any claim, damages, or other liability
arising from, out of, or in connection with the software or the use or other dealings in the software.
.EXAMPLE
.\LC_LaunchProfile.ps1
Executes the script to start Lethal Company based on the user's configurations.
#>
Add-Type -AssemblyName System.Windows.Forms
#### Util Initializations ###
#### Util Initializations ###
#### Util Initializations ###
<#
.SYNOPSIS
Loads the application configuration from a file, or creates a default configuration if the file does not exist.
.DESCRIPTION
This function checks for the existence of a configuration file. If found, it loads and returns the configuration.
Otherwise, it creates a default configuration, saves it to a file, and then returns it.
.OUTPUTS
Hashtable. Returns a hashtable of configuration settings.
#>
function LoadConfig() {
# Check if the configuration file exists
if (Test-Path $configFilePath) {
# Read and return the configuration from the existing file as a JSON object
return Get-Content $configFilePath | ConvertFrom-Json
} else {
# Define the default configuration settings
$defaultConfig = @{
"exePath" = 'C:\Program Files (x86)\Steam\steamapps\common\Lethal Company\Lethal Company.exe'
"profileDirectory" = "$($env:APPDATA)\r2modmanPlus-local\LethalCompany\profiles"
"monitor" = 0 # Default monitor index
"selectedProfile" = "" # Default to no selected profile
"windowCount" = 1 # Default number of game windows to launch
"windowSize" = 1 # Default game window size (full size)
}
# Save the default configuration to a file and return it
$defaultConfig | ConvertTo-Json | Set-Content $configFilePath
return $defaultConfig
}
}
<#
.SYNOPSIS
Saves the application configuration to a file.
.DESCRIPTION
This function takes a configuration object as input and saves it to a file in JSON format.
The JSON is formatted for readability.
.PARAMETER config
The configuration object to be saved.
.INPUTS
Hashtable. The function expects a hashtable object containing configuration settings.
.OUTPUTS
None. The function saves the configuration to a file and does not return any output.
#>
function SaveConfig($config) {
# Convert the configuration object to a JSON string and save it to the config file
$config | ConvertTo-Json | Set-Content $configFilePath
}
<#
.SYNOPSIS
Gets a validated user selection from the console.
.DESCRIPTION
Prompts the user for input and validates the input against a specified range.
.PARAMETER Prompt
The prompt message to display to the user.
.PARAMETER DefaultIndex
The default index to use if the user does not provide an input.
.PARAMETER MaxValue
The maximum valid value for the user's selection.
.OUTPUTS
Int. Returns the validated user selection.
#>
function GetValidatedSelection($Prompt, $DefaultIndex, $MaxValue) {
do {
Write-Host -ForegroundColor Yellow -NoNewline $Prompt
$selection = Read-Host
if ([string]::IsNullOrWhiteSpace($selection)) {
return $DefaultIndex
}
if ($selection -match '^\d+$' -and $selection -ge 1 -and $selection -le $MaxValue) {
return $selection
}
Write-Host -ForegroundColor Red "Invalid selection. Please try again."
} while ($true)
}
<#
.SYNOPSIS
Prepares the arguments for launching the game.
.DESCRIPTION
This function creates a list of arguments for the game launch command based on the given window dimensions and configuration settings.
.PARAMETER Width
The width of the game window.
.PARAMETER Height
The height of the game window.
.PARAMETER Config
The configuration object containing various settings like profile directory, selected profile, etc.
.OUTPUTS
Array. Returns an array of strings representing the game launch arguments.
#>
function PrepareGameLaunchArguments {
param(
[int]$Width,
[int]$Height,
[PSCustomObject]$Config
)
# Prepare the screen dimensions arguments
$heightArg = '-screen-height ' + $Height
$widthArg = '-screen-width ' + $Width
# Prepare other necessary arguments
$doorstopEnable = '--doorstop-enable true'
$fullPath = [IO.Path]::Combine($Config.profileDirectory, $Config.selectedProfile, "BepInEx\core\BepInEx.Preloader.dll")
$doorstopTarget = "--doorstop-target `"$fullPath`""
$monitorArg = '-monitor ' + $Config.monitor
$fullscreenArg = '--screen-fullscreen 0'
# Return the array of arguments
return $doorstopEnable, $doorstopTarget, $monitorArg, $fullscreenArg, $heightArg, $widthArg
}
#### Main Function Initializations ###
#### Main Function Initializations ###
#### Main Function Initializations ###
<#
.SYNOPSIS
Prompts the user to select a game profile from the available directories.
.DESCRIPTION
This function lists all child directories in the profile directory and allows the user to select one.
If no selection is made, the default profile is used.
.OUTPUTS
String. Returns the name of the selected profile.
#>
function SelectProfile() {
# Retrieve all child directories in the profile directory
$childDirs = Get-ChildItem -Path $config.profileDirectory -Directory
# Handle case when no profiles are available
if ($childDirs.Count -eq 0) {
Write-Host -ForegroundColor Red "No profiles found."
return $null
}
# Default to the first profile if no profile is currently selected
if ([string]::IsNullOrWhiteSpace($config.selectedProfile)) {
$config.selectedProfile = $childDirs[0].Name
}
# Find the index of the default profile
$defaultProfileIndex = $childDirs.Name.IndexOf($config.selectedProfile) + 1
# Display the list of available profiles to the user
for ($i = 0; $i -lt $childDirs.Count; $i++) {
Write-Host "$($i+1): $($childDirs[$i].Name)"
}
# Get user selection with validation
$selection = GetValidatedSelection -Prompt "Select a profile ($($config.selectedProfile)): " -DefaultIndex $defaultProfileIndex -MaxValue $childDirs.Count
# Return the selected profile name
return $childDirs[$selection - 1].Name
}
<#
.SYNOPSIS
Prompts the user to select the number of concurrent game windows to launch.
.DESCRIPTION
This function asks the user to specify how many copies of the game they want to launch.
It ensures the input is within a valid range (1 to 4).
.OUTPUTS
Int. Returns the number of game windows to launch.
#>
function SelectConcurrentWindows() {
# Prompt for user input with validation
$promptMessage = "How many copies of the game do you want to launch? ($($config.windowCount)): "
$selection = GetValidatedSelection -Prompt $promptMessage -DefaultIndex $config.windowCount -MaxValue 8
# Return the validated selection
return $selection
}
<#
.SYNOPSIS
Prompts the user to select the size of the game window.
.DESCRIPTION
This function displays a list of possible window sizes as fractions of the full screen size.
The user is asked to choose one of these sizes.
.OUTPUTS
Double. Returns the fraction representing the selected window size.
#>
function SelectWindowSize() {
# Define an array of possible window sizes as fractions of full size
$sizeSelections = @(1.0, 0.75, 0.5, 0.25)
# Find the index of the default window size in the size selections
$defaultSizeIndex = $sizeSelections.IndexOf([double]$config.windowSize) + 1
# Display the window size options to the user
for ($i = 0; $i -lt $sizeSelections.Count; $i++) {
Write-Host "$($i+1): $([int]($sizeSelections[$i]*100))%"
}
# Prompt for user input with validation
$promptMessage = "How big do you want the game windows compared to your screen ($([int]($config.windowSize*100))%): "
$selection = GetValidatedSelection -Prompt $promptMessage -DefaultIndex $defaultSizeIndex -MaxValue $sizeSelections.Count
# Return the selected size from the size selections
return $sizeSelections[$selection - 1]
}
#### Main script execution block ###
#### Main script execution block ###
#### Main script execution block ###
# Load existing config or create a default one
$configFilePath = Join-Path $PSScriptRoot "LCLaunchConfig.json"
$config = LoadConfig
# User Configuration Section
# ---------------------------
# Select profile, number of windows, and window size based on user input or default values
$config.selectedProfile = SelectProfile
$config.windowCount = SelectConcurrentWindows
$config.windowSize = SelectWindowSize
# Display the final selections to the user
Write-Host -ForegroundColor Green "Selected Profile: $($config.selectedProfile)"
Write-Host -ForegroundColor Green "Number of Windows: $($config.windowCount)"
Write-Host -ForegroundColor Green "Window Size: $($config.windowSize*100)%"
# Save the current configuration
SaveConfig $config
# Game Launch Preparation Section
# -------------------------------
# Calculate screen resolution and window size
$ScreenWidth = [System.Windows.Forms.Screen]::AllScreens[0].Bounds.Width
$ScreenHeight = [System.Windows.Forms.Screen]::AllScreens[0].Bounds.Height
$WindowWidth = [Math]::Floor($ScreenWidth * $config.windowSize)
$WindowHeight = [Math]::Floor($ScreenHeight * $config.windowSize)
# Prepare arguments for game launch
$arguments = PrepareGameLaunchArguments -Width $WindowWidth -Height $WindowHeight -Config $config
# Launch the Game
# ----------------
foreach ($i in 1..$config.windowCount){
Write-Host "Starting game: $i"
Start-Process $config.exePath -ArgumentList $arguments
Start-Sleep -Milliseconds 500 # Brief pause between launches
}
# Wait for user input before exiting the script
Read-Host