-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathcleanluas.ps1
More file actions
359 lines (301 loc) · 12.7 KB
/
Copy pathcleanluas.ps1
File metadata and controls
359 lines (301 loc) · 12.7 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
357
358
#Requires -Version 5.1
# Clean Lua Script - Removes bad Lua files from Steam stplug-in folder
# Clear screen
Clear-Host
# Header
Write-Host ""
Write-Host "===============================================================" -ForegroundColor Cyan
Write-Host "Lua Cleaner - by discord.gg/luatools" -ForegroundColor Cyan
Write-Host "===============================================================" -ForegroundColor Cyan
Write-Host ""
# Step 0: Get Steam path from registry
Write-Host "Step 0: Locating Steam installation..." -ForegroundColor Yellow
function Get-SteamPath {
$steamPath = $null
Write-Host " Searching for Steam installation..." -ForegroundColor Gray
# Try HKCU first (User registry)
$regPath = "HKCU:\Software\Valve\Steam"
if (Test-Path $regPath) {
$steamPath = (Get-ItemProperty -Path $regPath -Name "SteamPath" -ErrorAction SilentlyContinue).SteamPath
if ($steamPath -and (Test-Path $steamPath)) {
return $steamPath
}
}
# Try HKLM (System registry)
$regPath = "HKLM:\Software\Valve\Steam"
if (Test-Path $regPath) {
$steamPath = (Get-ItemProperty -Path $regPath -Name "InstallPath" -ErrorAction SilentlyContinue).InstallPath
if ($steamPath -and (Test-Path $steamPath)) {
return $steamPath
}
}
# Try 32-bit registry on 64-bit systems
$regPath = "HKLM:\Software\WOW6432Node\Valve\Steam"
if (Test-Path $regPath) {
$steamPath = (Get-ItemProperty -Path $regPath -Name "InstallPath" -ErrorAction SilentlyContinue).InstallPath
if ($steamPath -and (Test-Path $steamPath)) {
return $steamPath
}
}
return $null
}
$steamPath = Get-SteamPath
if (-not $steamPath) {
Write-Host " [ERROR] Steam installation not found in registry." -ForegroundColor Red
Write-Host " Please ensure Steam is installed on your system." -ForegroundColor Yellow
Write-Host ""
Write-Host "Press Enter to exit..."
Read-Host
exit
}
Write-Host " [SUCCESS] Steam found!" -ForegroundColor Green
Write-Host " Location: $steamPath" -ForegroundColor White
Write-Host ""
# Step 1: Check for non-.lua files in stplug-in folder
Write-Host "Step 1: Checking stplug-in folder..." -ForegroundColor Yellow
$stpluginPath = Join-Path $steamPath "config\stplug-in"
if (-not (Test-Path $stpluginPath)) {
Write-Host " [ERROR] Luas folder does not exist! Add some games bozo" -ForegroundColor Red
Write-Host " Expected path: $stpluginPath" -ForegroundColor Yellow
Write-Host ""
Write-Host "Press Enter to exit..."
Read-Host
exit
}
Write-Host " [SUCCESS] stplug-in folder found" -ForegroundColor Green
Write-Host " Location: $stpluginPath" -ForegroundColor White
Write-Host ""
# Collect all bad files
$badFiles = @()
# Step 1: Find non-.lua files
Write-Host "Step 1: Checking for non-.lua files..." -ForegroundColor Yellow
$allFiles = Get-ChildItem -Path $stpluginPath -File -ErrorAction SilentlyContinue
foreach ($file in $allFiles) {
if ($file.Extension -ne ".lua") {
Write-Host " [BAD] Non-Lua file found: $($file.Name)" -ForegroundColor Red
$badFiles += @{
File = $file
BadLines = @() # Non-Lua files don't have bad lines
}
}
}
if ($badFiles.Count -eq 0) {
Write-Host " [INFO] No non-.lua files found" -ForegroundColor Cyan
}
Write-Host ""
# Function to validate a line
function Test-ValidLuaLine {
param([string]$Line)
$trimmedLine = $Line.Trim()
# Check if line is fully blank
if ([string]::IsNullOrWhiteSpace($trimmedLine)) {
return $true
}
# Check if line starts with - (comment or valid line)
if ($trimmedLine.StartsWith('-')) {
return $true
}
# Check if line starts with addappid (case-insensitive) - needs special validation
if ($trimmedLine -match '^(?i)addappid') {
# Must have opening parenthesis after addappid (not directly followed by number/letter)
if (-not $trimmedLine -match '^(?i)addappid\s*\(') {
# Missing opening paren or has space/characters between addappid and paren
return $false
}
# Extract content before any comment
$beforeComment = $trimmedLine
if ($trimmedLine.Contains('--')) {
$beforeComment = $trimmedLine.Substring(0, $trimmedLine.IndexOf('--'))
}
# Find opening parenthesis
$openParen = $beforeComment.IndexOf('(')
if ($openParen -lt 0) {
return $false
}
# Find the matching closing parenthesis by counting nested parens
$parenCount = 0
$closeParenInComment = -1
for ($i = $openParen; $i -lt $beforeComment.Length; $i++) {
if ($beforeComment[$i] -eq '(') {
$parenCount++
} elseif ($beforeComment[$i] -eq ')') {
$parenCount--
if ($parenCount -eq 0) {
$closeParenInComment = $i
break
}
}
}
if ($closeParenInComment -lt 0) {
# No matching closing paren found
return $false
}
# Check if there's content after the matching closing parenthesis (before comment)
# After closing paren, there should only be whitespace or nothing
if ($closeParenInComment + 1 -lt $beforeComment.Length) {
$afterCloseParen = $beforeComment.Substring($closeParenInComment + 1).Trim()
if ($afterCloseParen -ne '' -and -not $afterCloseParen.StartsWith('--')) {
# There's content after closing paren that's not a comment - invalid
return $false
}
}
# Extract paren content safely
$parenLength = $closeParenInComment - $openParen - 1
if ($parenLength -gt 0) {
$parenContent = $beforeComment.Substring($openParen + 1, $parenLength)
# Check for unquoted hash-like strings (40+ hex characters)
# Remove all quoted strings first
$withoutQuotes = $parenContent -replace '"[^"]*"', ''
# Check if there are unquoted hash-like strings remaining
if ($withoutQuotes -match '[a-f0-9]{40,}') {
# Found unquoted hash-like string - invalid
return $false
}
}
return $true
}
# Check if line starts with setManifestid (case-insensitive) - needs special validation
if ($trimmedLine -match '^(?i)setManifestid') {
# Must have opening parenthesis after setManifestid (not directly followed by number/letter)
if (-not $trimmedLine -match '^(?i)setManifestid\s*\(') {
# Missing opening paren or has space/characters between setManifestid and paren
return $false
}
# Extract content before any comment
$beforeComment = $trimmedLine
if ($trimmedLine.Contains('--')) {
$beforeComment = $trimmedLine.Substring(0, $trimmedLine.IndexOf('--'))
}
# Find opening parenthesis
$openParen = $beforeComment.IndexOf('(')
if ($openParen -lt 0) {
return $false
}
# Find the matching closing parenthesis by counting nested parens
$parenCount = 0
$closeParenInComment = -1
for ($i = $openParen; $i -lt $beforeComment.Length; $i++) {
if ($beforeComment[$i] -eq '(') {
$parenCount++
} elseif ($beforeComment[$i] -eq ')') {
$parenCount--
if ($parenCount -eq 0) {
$closeParenInComment = $i
break
}
}
}
if ($closeParenInComment -lt 0) {
# No matching closing paren found
return $false
}
# Check if there's content after the matching closing parenthesis (before comment)
# After closing paren, there should only be whitespace or nothing
if ($closeParenInComment + 1 -lt $beforeComment.Length) {
$afterCloseParen = $beforeComment.Substring($closeParenInComment + 1).Trim()
if ($afterCloseParen -ne '' -and -not $afterCloseParen.StartsWith('--')) {
# There's content after closing paren that's not a comment - invalid
return $false
}
}
# Extract paren content safely
$parenLength = $closeParenInComment - $openParen - 1
if ($parenLength -gt 0) {
$parenContent = $beforeComment.Substring($openParen + 1, $parenLength)
# Check for unquoted hash-like strings (40+ hex characters)
# Remove all quoted strings first
$withoutQuotes = $parenContent -replace '"[^"]*"', ''
# Check if there are unquoted hash-like strings remaining
if ($withoutQuotes -match '[a-f0-9]{40,}') {
# Found unquoted hash-like string - invalid
return $false
}
}
return $true
}
# Check if line starts with addtoken (case-insensitive)
if ($trimmedLine -match '^(?i)addtoken') {
return $true
}
# If we get here, the line is invalid
return $false
}
# Step 2: Check every line of every .lua file
Write-Host "Step 2: Checking Lua files for invalid content..." -ForegroundColor Yellow
$luaFiles = Get-ChildItem -Path $stpluginPath -Filter "*.lua" -ErrorAction SilentlyContinue
$luaFileCount = ($luaFiles | Measure-Object).Count
if ($luaFileCount -eq 0) {
Write-Host " [INFO] No .lua files found to check" -ForegroundColor Cyan
} else {
Write-Host " Found $luaFileCount .lua file(s) to check..." -ForegroundColor Gray
$fileIndex = 0
foreach ($luaFile in $luaFiles) {
$fileIndex++
$hasBadLines = $false
$badLines = @() # Array of hashtables with LineNumber and Content
try {
$lines = Get-Content -Path $luaFile.FullName -ErrorAction Stop
$lineNumber = 0
foreach ($line in $lines) {
$lineNumber++
# Check if line is valid using the validation function
if (-not (Test-ValidLuaLine -Line $line)) {
$hasBadLines = $true
$badLines += @{
LineNumber = $lineNumber
Content = $line.Trim()
}
}
}
if ($hasBadLines) {
Write-Host " [BAD] Invalid content in: $($luaFile.Name)" -ForegroundColor Red
Write-Host " Bad lines: $(($badLines | ForEach-Object { $_.LineNumber }) -join ', ')" -ForegroundColor Yellow
# Store file info with bad lines
$badFiles += @{
File = $luaFile
BadLines = $badLines
}
} else {
Write-Host " [OK] $($luaFile.Name)" -ForegroundColor Green
}
} catch {
Write-Host " [ERROR] Failed to read file: $($luaFile.Name) - $_" -ForegroundColor Red
$badFiles += @{
File = $luaFile
BadLines = @()
}
}
# Show progress
if ($fileIndex % 10 -eq 0 -or $fileIndex -eq $luaFileCount) {
Write-Host " Progress: $fileIndex / $luaFileCount files checked..." -ForegroundColor Gray
}
}
}
Write-Host ""
# Summary
Write-Host "===============================================================" -ForegroundColor Cyan
Write-Host "Summary" -ForegroundColor Cyan
Write-Host "===============================================================" -ForegroundColor Cyan
Write-Host ""
if ($badFiles.Count -eq 0) {
Write-Host " [SUCCESS] No bad files found! All files are clean." -ForegroundColor Green
} else {
Write-Host " [WARNING] Found $($badFiles.Count) bad file(s):" -ForegroundColor Yellow
Write-Host ""
foreach ($badFileInfo in $badFiles) {
$badFile = $badFileInfo.File
$badLines = $badFileInfo.BadLines
Write-Host " - $($badFile.Name)" -ForegroundColor Red
if ($badLines.Count -gt 0) {
foreach ($badLine in $badLines) {
Write-Host " Line $($badLine.LineNumber): $($badLine.Content)" -ForegroundColor Yellow
}
}
Write-Host ""
}
}
Write-Host ""
Write-Host "===============================================================" -ForegroundColor Cyan
Write-Host "Press Enter to exit..." -ForegroundColor Cyan
Write-Host "===============================================================" -ForegroundColor Cyan
Read-Host