-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGCIE-PS.ps1
199 lines (175 loc) · 8 KB
/
GCIE-PS.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
<#
.DESCRIPTION
Extracts or lists files from an uncompressed GameCube image (uncompressed gcm/iso). As this was originally written for "Harbor Masters 64" ports, it tries to extract a PAL OoT, PAL MQ or NTSC-U MM ROM by default. But it can also be used to extract any raw file from the gcm/iso by name or just list its file system.
Requires you to install .NET and Powershell on MacOS and Linux (untested, but should work).
> [!TIP]
> Instead of starting the script the usual way, you can also Drag & Drop your rom on the included batch file to kickstart the automatic extraction.
There is also a C# port of this ported by xoascf (aka Amaro): https://github.com/xoascf/GCIE
.NOTES
GameCube Image Extractor - PowerShell Script v25.01.19
MIT License
Copyright (C) 2024 Spodi
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
#>
Using Module '.\GC.psm1'
Using Module '.\N64.psm1'
[CmdletBinding(PositionalBinding = $false, DefaultParameterSetName = 'Default')]
param (
# GameCube image file to extract or list files from (uncompressed gcm/iso).
[Parameter(ParameterSetName = 'Extract', Position = 0, Mandatory)]
[Parameter(ParameterSetName = 'List', Position = 0, Mandatory)]
[Parameter(ParameterSetName = 'Default', Position = 0, Mandatory)]
[string]$fileIn,
# Extracts all files where their full name (path + name) matches this Regular Expression.
[Parameter(ParameterSetName = 'Extract', Position = 1, Mandatory)] [string]$Extract,
# Lists all files in the image. "Object" sends the file infos as objects to the pipeline. "Text" and "Json" saves the infos as "FileList.txt" or "FileList.json".
[Parameter(ParameterSetName = 'List', Position = 1, Mandatory)][ValidateSet('Object', 'Text', 'Json')] [string]$ListFiles,
# Does not pause to keep the console open after extraction (useful for automation).
[Parameter(ParameterSetName = 'Default')] [Parameter(ParameterSetName = 'Extract')] [switch] $nopause
)
Begin {
$erroroccured = $null
}
Process {
$RSHashes = [powershell]::Create()
$RSHashes.Runspace.SessionStateProxy.SetVariable('Root', $PSScriptRoot)
[void]$RSHashes.AddScript({
if ((Test-Path 'validateHMHash.ps1' -PathType Leaf) -and -not (Test-Path 'HMHashes.json' -PathType Leaf)) {
. 'validateHMHash.ps1' -UpdateHashes
}
})
if (!(Test-Path -LiteralPath $fileIn -PathType Leaf)) {
Write-Error "File `"$fileIn`" not found!"
return
}
$RSHashesHandle = $RSHashes.BeginInvoke()
$fileInfo = [System.IO.FileInfo] $fileIn
$Stream = [System.IO.File]::OpenRead($fileIn)
try { $Disc = [GC.Disc]$Stream }
catch {
Write-Error 'Error while reading GC Rom file.'
Write-Verbose $_
$erroroccured = $true
return
}
Write-Host '------ Input: ------' -ForegroundColor Cyan
([PSCustomObject]@{
Name = $fileInfo.Name
Path = $fileInfo.Directory
GC_Header = $Disc.ToString()
#SHA1 = (Get-FileHash $fileIn -Algorithm SHA1).Hash
} | Format-List | Out-String).Trim() | Out-Host
$list = $Disc.GetAllEntries() | & { Process { if ($_ -is [FileEntry]) { $_ } } }
Write-Host '--------------------' -ForegroundColor Cyan
if ($ListFiles) {
switch ($ListFiles) {
'Object' {
$list | Sort-Object FileOffset | Write-Output
}
'json' {
$list | Select-Object FileOffset, Size, Path, Name | Sort-Object FileOffset | ConvertTo-Json | Out-File FileList.json
}
'Text' {
($list | Select-Object FileOffset, Size, Path, Name | Sort-Object FileOffset | Format-Table | Out-String).Trim() | Out-File FileList.txt
}
}
}
elseif ($Extract) {
$List = $List | Where-Object 'FullName' -Match $Extract
if ($List) {
$extractedFiles = $List | & { Process {
if ([System.IO.Path]::GetExtension($_.Name) -EQ '.n64') {
$_.WriteFile((Join-Path $PSScriptRoot ([System.IO.Path]::ChangeExtension($_.Name, '.z64'))), $true)
}
else {
$_.WriteFile((Join-Path $PSScriptRoot $_.Name), $true)
}
} }
}
else {
Write-Host "Couldn't find any file or path matching the regular expression `"$Extract`"."
}
}
else {
if ($list) {
$extractedFiles = $list | ForEach-Object {
# Ocarina of Time
if ($_.name -eq 'zlp_f.n64') {
try { $_.WriteFile((Join-Path $PSScriptRoot 'TLoZ-OoT-GC-PAL.z64'), $true) }
catch [FileAlreadyExistsException] {
Write-Error -ErrorRecord $_ -ErrorAction 'Continue'
}
}
# Ocarina of Time - Master Quest
elseif ($_.name -eq 'urazlp_f.n64') {
try { $_.WriteFile((Join-Path $PSScriptRoot 'TLoZ-OoT-MQ-GC-PAL.z64'), $true) }
catch [FileAlreadyExistsException] {
Write-Error -ErrorRecord $_ -ErrorAction 'Continue'
}
}
# Majoras Mask
elseif ($_.name -eq 'zelda2e.n64') {
try { $_.WriteFile((Join-Path $PSScriptRoot 'TLoZ-MM-GC-NTSCU.z64'), $true) }
catch [FileAlreadyExistsException] {
Write-Error -ErrorRecord $_ -ErrorAction 'Continue'
}
}
}
}
else {
Write-Host "Couldn't find any PAL OoT, PAL MQ or NTSC-U MM ROM."
}
}
$RSHashes.EndInvoke($RSHashesHandle)
$RSHashes.Runspace.Close()
$RSHashes.Dispose()
if ($extractedFiles) {
if (Test-Path '.\validateHMHash.ps1' -PathType Leaf) {
$extractedFiles | & '.\validateHMHash.ps1' -nopause | Write-Output
}
else {
$extractedFiles | ForEach-Object {
if (Test-Path $_ -PathType Leaf) {
$header = $null
$File = [System.IO.FileInfo] $_
if ($_.Extension -EQ '.z64') {
try { $header = [N64.RomHeader]::Read($File) }
catch {
Write-Error 'Error while reading N64 Rom file.'
Write-Verbose $_
}
}
([PSCustomObject]@{
Name = $File.Name
Path = $File.Directory
SHA1 = (Get-FileHash $File -Algorithm SHA1).Hash
N64_Header = $header
HM_Game = $null
HM_Type = $null
}) | Write-Output
}
}
}
if (!$nopause) {
Pause
}
}
}
End {
if ($erroroccured) { EXIT 1 }
EXIT
}