-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFactorioServerManager.ps1
More file actions
184 lines (169 loc) · 7.2 KB
/
Copy pathFactorioServerManager.ps1
File metadata and controls
184 lines (169 loc) · 7.2 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
<#!
.SYNOPSIS
Factorio Dedicated Server Manager (PowerShell Edition)
.DESCRIPTION
Launches and manages a local Factorio server with credential-verified downloads.
#>
Set-StrictMode -Version Latest
$ErrorActionPreference = "Stop"
$VerbosePreference = "Continue"
$scriptRoot = $PSScriptRoot
$InstallDir = Join-Path $scriptRoot "Factorio"
$SavesDir = Join-Path $env:APPDATA "Factorio\saves"
$LogDir = Join-Path $scriptRoot "logs"
$startupLog = Join-Path $LogDir "startup.log"
if (!(Test-Path $LogDir)) { [System.IO.Directory]::CreateDirectory($LogDir) | Out-Null }
Add-Content -Path $startupLog -Value "[INIT] Script launched at $(Get-Date)"
$windowsIdentity = [Security.Principal.WindowsIdentity]::GetCurrent()
$windowsPrincipal = New-Object Security.Principal.WindowsPrincipal($windowsIdentity)
$adminRole = [Security.Principal.WindowsBuiltInRole]::Administrator
if (-not $windowsPrincipal.IsInRole($adminRole)) {
Add-Content -Path $startupLog -Value "[INFO] Relaunching with elevated privileges."
Start-Process -FilePath "powershell.exe" -ArgumentList "-ExecutionPolicy Bypass -File `"$PSCommandPath`"" -Verb RunAs
exit
}
$logFile = Join-Path $LogDir ("session_" + (Get-Date -Format "yyyyMMdd_HHmmss") + ".log")
Start-Transcript -Path $logFile -Append -Force
function Request-FactorioToken {
do {
$username = Read-Host "Enter your Factorio.com username"
$password = Read-Host -AsSecureString "Enter your Factorio.com password"
$plainPass = [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($password))
$postBody = @{ username = $username; password = $plainPass; api_version = 6 } | ConvertTo-Json
try {
$response = Invoke-RestMethod -Method Post -Uri "https://auth.factorio.com/api-login" -Body $postBody -ContentType 'application/json'
if ($response.token) {
return @{ username = $username; token = $response.token }
} else {
Write-Warning "[ERROR] Login failed: No token returned"
}
} catch {
Write-Warning "[ERROR] Login failed: $($_.Exception.Message)"
}
} while ($true)
}
function Ensure-FactorioInstalled {
Write-Verbose "Checking if Factorio is installed."
$exePath = Join-Path $InstallDir 'bin\x64\factorio.exe'
if (!(Test-Path $exePath)) {
Write-Host "[INFO] Validating credentials for Factorio download..."
$creds = Request-FactorioToken
$zipUrl = "https://factorio.com/get-download/stable/headless/windows64?username=$($creds.username)&token=$($creds.token)"
$zipFile = Join-Path $env:TEMP "factorio_server.zip"
Invoke-WebRequest -Uri $zipUrl -OutFile $zipFile -UseBasicParsing
Expand-Archive -Path $zipFile -DestinationPath $InstallDir -Force
Remove-Item $zipFile
Write-Host "[INFO] Factorio installed to $InstallDir"
}
return $exePath
}
function Ensure-SettingsFile {
$template = Join-Path $InstallDir 'data\server-settings.example.json'
$settings = Join-Path $scriptRoot 'server-settings.json'
if (!(Test-Path $settings) -and (Test-Path $template)) {
Copy-Item $template $settings
Write-Host "[INFO] server-settings.json created."
}
}
function Show-MainMenu {
Clear-Host
Write-Host "=========================================" -ForegroundColor Cyan
Write-Host " Factorio Server Manager GUI " -ForegroundColor Green
Write-Host "=========================================" -ForegroundColor Cyan
Write-Host " 1. Load latest save"
Write-Host " 2. Choose save manually"
Write-Host " 3. Edit server settings"
Write-Host " 4. Create new save & launch server"
Write-Host " 5. Force update Factorio Server"
Write-Host " 6. Exit"
Write-Host "=========================================" -ForegroundColor Cyan
return Read-Host "Select option (1-6)"
}
function Launch-Server {
param (
[string]$Exe,
[string]$SaveFile = $null,
[switch]$CreateSave
)
$settingsPath = Join-Path $scriptRoot 'server-settings.json'
$adminlistPath = Join-Path $scriptRoot 'server-adminlist.json'
if ($CreateSave) {
$saveName = Read-Host "Enter name for new save (e.g., 'my_factory')"
if ([string]::IsNullOrWhiteSpace($saveName)) {
Write-Warning "Save name cannot be empty."
return
}
$SaveFile = Join-Path $SavesDir "$saveName.zip"
Write-Host "[INFO] Creating new save file at $SaveFile..."
Start-Process -FilePath $Exe -ArgumentList @('--create', "`"$SaveFile`"") -NoNewWindow -Wait
if (-not (Test-Path $SaveFile)) {
Write-Warning "[ERROR] Failed to create save file."
return
}
}
$args = @('--start-server', "`"$SaveFile`"")
if (Test-Path $settingsPath) {
$args += '--server-settings'
$args += "`"$settingsPath`""
}
if (Test-Path $adminlistPath) {
$args += '--server-adminlist'
$args += "`"$adminlistPath`""
}
Write-Host "[INFO] Starting Factorio server with save $SaveFile..."
Start-Process -FilePath $Exe -ArgumentList $args -NoNewWindow -Wait
}
try {
$factorioExe = Ensure-FactorioInstalled
Ensure-SettingsFile
if (!(Test-Path $SavesDir)) { New-Item -Path $SavesDir -ItemType Directory -Force | Out-Null }
while ($true) {
$choice = Show-MainMenu
switch ($choice) {
'1' {
$latest = Get-ChildItem -Path $SavesDir -Filter "*.zip" | Sort-Object LastWriteTime -Descending | Select-Object -First 1
if ($latest) {
Launch-Server -Exe $factorioExe -SaveFile $latest.FullName
} else {
Write-Host "[WARN] No save files found."
Pause
}
}
'2' {
$selection = Get-ChildItem -Path $SavesDir -Filter "*.zip" | Out-GridView -Title "Select Save File" -PassThru
if ($selection) {
Launch-Server -Exe $factorioExe -SaveFile $selection.FullName
}
}
'3' {
notepad (Join-Path $scriptRoot 'server-settings.json')
}
'4' {
Launch-Server -Exe $factorioExe -CreateSave
}
'5' {
$confirm = Read-Host "Are you sure you want to re-download the server? (Y/N)"
if ($confirm -eq 'Y' -or $confirm -eq 'y') {
if (Test-Path (Join-Path $InstallDir 'bin\x64\factorio.exe')) {
Remove-Item (Join-Path $InstallDir 'bin\x64\factorio.exe') -Force
}
$factorioExe = Ensure-FactorioInstalled
Pause
}
}
'6' {
break
}
default {
Write-Warning "Invalid input. Please try again."
Start-Sleep -Seconds 2
}
}
}
} catch {
Write-Error "[FATAL] $($_.Exception.Message)"
Add-Content -Path $startupLog -Value "[FATAL] Exception: $($_.Exception.Message)"
} finally {
Stop-Transcript
Add-Content -Path $startupLog -Value "[END] Script ended at $(Get-Date)"
}