-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.ps1
455 lines (408 loc) · 15.9 KB
/
build.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
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
<#
.SYNOPSIS
Pulls in assets from the skyrim Data Folder to create the 7z Skyrim SE mod package.
.DESCRIPTION
Pulls in assets from the skyrim Data Folder to create the 7z Skyrim SE mod package.
.PARAMETER ConfigFile
The path to the buildConfig.json used to build this mod
.PARAMETER SkipReadme
Skips generating the txt and bbcode readme from MD.
.EXAMPLE
PS C:\> <example usage>
Explanation of what the example does
.INPUTS
None
.OUTPUTS
None
.NOTES
Copyright 2021 Mark E. Kraus
#>
[CmdletBinding()]
param (
[Parameter(
Position=0,
ValueFromPipelineByPropertyName=$true)]
[ValidateNotNullOrEmpty()]
[string]
$ConfigFile =
$(
if (![string]::IsNullOrWhitespace($PSScriptRoot)) {
Join-Path $PSScriptRoot "buildConfig.json"
}
else {
Join-Path $pwd.Path "buildConfig.json"
}
),
[Parameter()]
[switch]
$SkipReadme
)
$StartTime = [datetime]::UtcNow
Write-Host ("Build started {0} UTC" -f $StartTime)
Write-Host " "
$7zFiles = [System.Collections.Generic.List[string]]::New()
$BsaFiles = [System.Collections.Generic.List[string]]::New()
$BasePath = Split-Path $ConfigFile
Push-Location $BasePath
$FomodBasePath = Join-Path $BasePath "fomod"
$FomodInfoFile = Join-Path $FomodBasePath "info.xml"
$FomodModuleConfigFile = Join-Path $FomodBasePath "ModuleConfig.xml"
$Config = Get-Content -Path $ConfigFile | ConvertFrom-Json -Depth 10
$ModInfo = $Config.ModInfo
if([string]::IsNullOrWhiteSpace($Config.'$schema')) {
Write-Error "config file is missing '`$schema'"
exit 1
}
$Schema = (Invoke-WebRequest $Config.'$schema').Content
$SchemaIsValid = Get-Content -Path $ConfigFile -Raw | Test-Json -Schema $Schema
if(!$SchemaIsValid) {
Write-Error "Invalid JSON Schema."
exit 1
}
$SkyrimInstallPath = $Config.SkyrimSEInstallPath
$SkyrimDataPath = Join-Path $SkyrimInstallPath 'Data'
$SkyrimScriptPath = Join-Path $SkyrimDataPath 'scripts'
$SkyrimScriptSourcePath = Join-Path $SkyrimDataPath -ChildPath 'source' -AdditionalChildPath 'scripts'
$OutputScriptPath = Join-Path $BasePath 'scripts'
$OutputScriptSourcePath = Join-Path $BasePath -ChildPath 'source' -AdditionalChildPath 'scripts'
$ArchivePath = Join-Path $SkyrimInstallPath 'tools' 'archive','archive.exe'
$FuzExtractorPath = Join-Path $Config.UnfuzerPath "Fuz_extractor.exe"
$XWmaEncodePath = Join-Path $Config.UnfuzerPath "xWMAEncode.exe"
$XWmaEncodeCmd = Get-Command $XWmaEncodePath
$Plugin = $Config.Plugin
$PluginPath = Join-Path $SkyrimDataPath $Plugin
$BsaName = [System.IO.Path]::GetFileNameWithoutExtension($Plugin) + '.bsa'
$BsaPath = Join-Path $SkyrimDataPath $BsaName
$VoiceBasePath = Join-Path $SkyrimDataPath 'Sound' 'Voice',$Plugin
$MeshesBasePath = Join-Path $SkyrimDataPath 'Meshes'
$FaceGenMeshPath = Join-Path $SkyrimDataPath 'Meshes' 'Actors','character','FaceGenData','FaceGeom',$Plugin
$FaceGenTexturePath = Join-Path $SkyrimDataPath 'Textures' 'Actors','character','FaceGenData','FaceTint',$Plugin
$SkyrimSeqPath = Join-Path $SkyrimDataPath 'Seq'
Write-Host @"
BasePath: $BasePath
OutputScriptPath: $OutputScriptPath
OutputScriptSourcePath: $OutputScriptSourcePath
FomodBasePath: $FomodBasePath
FomodInfoFile: $FomodInfoFile
FomodModuleConfigFile: $FomodModuleConfigFile
SkyrimInstallPath: $SkyrimInstallPath
SkyrimDataPath: $SkyrimDataPath
SkyrimScriptPath: $SkyrimScriptPath
SkyrimScriptSourcePath: $SkyrimScriptSourcePath
SkyrimSeqPath: $SkyrimSeqPath
ArchivePath: $ArchivePath
MeshesBasePath: $MeshesBasePath
VoiceBasePath: $VoiceBasePath
FaceGenMeshPath: $FaceGenMeshPath
FaceGenTexturePath: $FaceGenTexturePath
FuzExtractorPath: $FuzExtractorPath
XWmaEncodePath: $XWmaEncodePath
PluginPath: $PluginPath
BsaPath: $BsaPath
BsaName: $BsaName
"@
$null = New-Item -ItemType Directory -Path $OutputScriptPath -Force
$null = New-Item -ItemType Directory -Path $OutputScriptSourcePath -Force
$PluginXmlTemplate = @'
<plugin name="{0}">
<description>{1}</description>
<image path="{2}" />
<files>
<file source="{3}" destination="{3}" priority="0" />
<file source="{4}" destination="{4}" priority="0" />
</files>
<typeDescriptor>
<type name="Optional"/>
</typeDescriptor>
</plugin>
'@
$PluginPartXml = ""
if(Test-Path -Path $PluginPath){
$7zFiles.Add($Plugin)
Write-Host "Copying $PluginPath"
Copy-Item -Path $PluginPath -Destination $BasePath -Force
$PluginPartXml = $PluginPartXml + ($PluginXmlTemplate -f @(
$Plugin
$ModInfo.Description
$ModInfo.Logo
$Plugin
$BsaName
))
}
$ScriptSourceXmlTemplate = @'
<file source="source\scripts\{0}" destination="source\scripts\{0}" priority="0" />
'@
$ScriptsXmlPart=""
foreach ($papyrusScript in $Config.Scripts) {
$pexFile = $papyrusScript + ".pex"
$pscFile = $papyrusScript + ".psc"
$PapyrusScriptPath = Join-Path $SkyrimScriptPath $pexFile
$PapyrusScriptSourcePath = Join-Path $SkyrimScriptSourcePath $pscFile
if(Test-Path -Path $PapyrusScriptPath){
$BsaFiles.Add("scripts\" + $pexFile)
Write-Host "Copying $PapyrusScriptPath"
Copy-Item -Path $PapyrusScriptPath -Destination $OutputScriptPath -Force
$ScriptsXmlPart += $ScriptXmlTemplate -f $pexFile
}
else {
Write-Error "Unable to find script '$PapyrusScriptPath'"
}
if(Test-Path -Path $PapyrusScriptSourcePath){
$7zFiles.Add("source\scripts\" + $pscFile)
Write-Host "Copying $PapyrusScriptSourcePath"
Copy-Item -Path $PapyrusScriptSourcePath -Destination $OutputScriptSourcePath -Force
$ScriptsXmlPart += $ScriptSourceXmlTemplate -f $pscFile
}
else {
Write-Warning "Unable to find script source '$PapyrusScriptSourcePath'"
}
}
$InfoXml = @'
<fomod>
<Name>{0}</Name>
<Author>{1}</Author>
<Version>{2}</Version>
<Website>{3}</Website>
<Description>{4}</Description>
<Groups>
<element>{5}</element>
</Groups>
</fomod>
'@ -f @(
$ModInfo.Name
$ModInfo.Author
$ModInfo.Version
$ModInfo.Website
$ModInfo.Description
$ModInfo.Category
)
$Null = New-Item -ItemType Directory -Path $FomodBasePath -Force
$InfoXml | Set-Content -Encoding utf8NoBOM -Path $FomodInfoFile
$ModuleConfigXML = @'
<!-- Created with build.ps1 by Mark E. Kraus -->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://qconsulting.ca/fo3/ModConfig5.0.xsd">
<moduleName>{0}</moduleName>
<installSteps order="Explicit">
<installStep name="Install">
<optionalFileGroups order="Explicit">
<group name="Main" type="SelectExactlyOne">
<plugins order="Explicit">{1}
</plugins>
</group>
<group name="Install Script Sources?" type="SelectExactlyOne">
<plugins order="Explicit">
<plugin name="No (Default)">
<description>Skips installing script sources.</description>
<image path="{2}" />
<files></files>
<typeDescriptor>
<type name="Optional"/>
</typeDescriptor>
</plugin>
<plugin name="Yes (For Mod Developers)">
<description>Installs script sources.</description>
<image path="{2}" />
<files>{3}
</files>
<typeDescriptor>
<type name="Optional"/>
</typeDescriptor>
</plugin>
</plugins>
</group>
</optionalFileGroups>
</installStep>
</installSteps>
</config>
'@ -f @(
$ModInfo.Name
$PluginPartXml
$ModInfo.Logo
$ScriptsXmlPart
)
Write-Host @'
Converting Fuzzing Voice and Lip Files...
'@
if(Test-Path $VoiceBasePath) {
Push-Location $VoiceBasePath
foreach ($WavFile in (Get-ChildItem -Recurse -Filter '*.wav')) {
$DataRelativeVoiceFolder = [Io.Path]::GetRelativePath($SkyrimDataPath, $WavFile.Directory)
$LocalVoiceFolder = [Io.Path]::Combine($BasePath, $DataRelativeVoiceFolder)
Push-Location $WavFile.Directory
Write-Host ([Io.Path]::GetRelativePath($VoiceBasePath, $WavFile))
$XWmaFile = $WavFile.BaseName + '.xwm'
$LipFile = $WavFile.BaseName + '.lip'
$FuzFile = $WavFile.BaseName + '.fuz'
$HashFile = $WavFile.BaseName + '.hash'
$currentHash = (Get-FileHash $WavFile -Algorithm SHA256).Hash
if ((Test-Path $HashFile) -and (Test-Path $FuzFile)) {
$oldHash = Get-Content $HashFile -TotalCount 1
if ($currentHash -eq $oldHash) {
Write-Host "No change. Skipping..."
Pop-Location
continue
}
}
else {
$currentHash | Set-Content -Path $HashFile -Encoding utf8BOM -NoNewline
}
$null = New-Item -ItemType Directory -Path $LocalVoiceFolder -Force
Copy-Item -Path $WavFile -Destination $LocalVoiceFolder -Force
& $XWmaEncodeCmd $WavFile $XWmaFile
if (Test-Path $LipFile) {
Copy-Item -Path $LipFile -Destination $LocalVoiceFolder -Force
& $FuzExtractorPath -i $FuzFile $XWmaFile /l
# Remove-Item $LipFile -Force
} else {
& $FuzExtractorPath -i $FuzFile $XWmaFile
}
# Remove-Item $WavFile -Force
Remove-Item $XWmaFile -Force
Pop-Location
}
foreach ($FuzFile in ((Get-ChildItem -Recurse -Filter '*.fuz'))) {
$RelPath = [Io.Path]::GetRelativePath($SkyrimDataPath, $FuzFile)
$BsaFiles.Add($RelPath)
}
Pop-Location
}
$SeqFileName = [System.IO.Path]::GetFileNameWithoutExtension($Plugin) + '.seq'
$SeqFilePath = Join-Path $SkyrimSeqPath $SeqFileName
$SeqFilePathRel = "Seq\" + $SeqFileName
if (Test-Path $SeqFilePath) {
$null = New-Item -ItemType Directory -Name "Seq" -Force
Copy-Item $SeqFilePath -Destination "Seq" -Force
$BsaFiles.Add($SeqFilePathRel)
}
if(Test-Path $FaceGenMeshPath) {
$DataRelativeFaceGenMeshPath = [Io.Path]::GetRelativePath($SkyrimDataPath, $FaceGenMeshPath)
$LocalFaceGenMeshPath = [Io.Path]::Combine($BasePath, $DataRelativeFaceGenMeshPath)
$null = New-Item -ItemType Directory -Path $LocalFaceGenMeshPath -Force
foreach ($FaceGenFile in (Get-ChildItem $FaceGenMeshPath)) {
if ($FaceGenFile.Extension -imatch '\.tga' ) { continue }
Copy-Item -Path $FaceGenFile.FullName -Destination $LocalFaceGenMeshPath -Force
$RelPath = [Io.Path]::GetRelativePath($SkyrimDataPath, $FaceGenFile)
$BsaFiles.Add($RelPath)
}
}
if(Test-Path $FaceGenTexturePath) {
$DataRelativeFaceGenTexturePath = [Io.Path]::GetRelativePath($SkyrimDataPath, $FaceGenTexturePath)
$LocalFaceGenTexturePath = [Io.Path]::Combine($BasePath, $DataRelativeFaceGenTexturePath)
$null = New-Item -ItemType Directory -Path $LocalFaceGenTexturePath -Force
foreach ($FaceGenFile in (Get-ChildItem $FaceGenTexturePath)) {
if ($FaceGenFile.Extension -imatch '\.tga' ) { continue }
Copy-Item -Path $FaceGenFile.FullName -Destination $LocalFaceGenTexturePath -Force
$RelPath = [Io.Path]::GetRelativePath($SkyrimDataPath, $FaceGenFile)
$BsaFiles.Add($RelPath)
}
}
foreach ($MeshFilePart in $Config.Meshes) {
$MeshFileFull = Join-Path $MeshesBasePath $MeshFilePart
if(Test-Path $MeshFileFull){
$MeshFile = Get-Item $MeshFileFull
$DataRelativeMeshPath = [Io.Path]::GetRelativePath($SkyrimDataPath, $MeshFile.Directory.FullName)
$LocalMeshPath = [Io.Path]::Combine($BasePath, $DataRelativeMeshPath)
$null = New-Item -ItemType Directory -Path $LocalMeshPath -Force
$MeshFile | Copy-Item -Destination $LocalMeshPath -Force
$RelPath = [Io.Path]::GetRelativePath($SkyrimDataPath, $MeshFile)
$BsaFiles.Add($RelPath)
}
}
Write-Host @"
Creating archive '$BsaName'...
BSA Files:
"@
foreach ($file in $BsaFiles) {
Write-Host $file
}
Push-Location $SkyrimInstallPath
$BsaFilesFile = $Plugin + ".bsafiles.txt"
$BsaScriptFile = $Plugin + ".bsascript.txt"
$BsaFiles | Set-Content $BsaFilesFile -Encoding utf8NoBOM
@"
Log: Logs\Archives\MyModArchiveLog.txt
New Archive
Check: Misc
Check: Meshes
Check: Textures
Check: Menus
Check: Sounds
Check: Voices
Check: Compress Archive
Check: Retain File Names
Set File Group Root: Data\
Add File Group: $BsaFilesFile
Save Archive: Data\$BsaName
"@ | Set-Content $BsaScriptFile -Encoding utf8NoBOM
if (Test-Path $BsaPath) {
"Backing up existing BSA..."
Remove-Item -Path "$BsaPath.bak" -Force
Rename-Item -Path $BsaPath -NewName "$BsaName.bak" -Force
}
Start-Process -Wait -FilePath $ArchivePath -ArgumentList $BsaScriptFile -WorkingDirectory $SkyrimInstallPath
Pop-Location
Copy-Item -Path $BsaPath -Destination $BasePath -Force
$7zFiles.Add($BsaName)
$ModuleConfigXML | Set-Content -Encoding utf8NoBOM -Path $FomodModuleConfigFile
# Get-ChildItem -Recurse $VoiceBasePath
$bbcode = [System.Text.StringBuilder]::New()
$inList = $false
if(!$SkipReadme){
Copy-Item ".\README.md" "README.txt"
$7zFiles.Add("README.txt")
foreach ($Line in (Get-Content "README.md")) {
if ($Line -match '^#[^#]') {
$Line = $Line -replace '^# ','[size=6]'
$Line = $Line + '[/size]'
}
elseif ($Line -match '^##[^#]') {
$Line = $Line -replace '^## ','[size=5]'
$Line = $Line + '[/size]'
}
elseif ($Line -match '^#') {
$Line = $Line -replace '^#* ','[size=4]'
$Line = $Line + '[/size]'
}
if ($inList -and $Line -notmatch '^\* ') {
$inList = $false
$null = $bbcode.AppendLine('[/list]')
}
if (!$inList -and $Line -match '^\* ') {
$inList = $true
$null = $bbcode.AppendLine('[list]')
}
if($inList -and $Line -match '^\* ') {
$Line = $Line -replace '^\* ', '[*]'
}
$Line = $Line -replace '!\[[^)]*\)'
$Line = $Line -replace '\[([^\]]*)\]\(([^)]*)\)', '[url=$2]$1[/url]'
$Line = $Line -replace '`([^`]*)`', '[font=Courier New]$1[/font]'
$null = $bbcode.AppendLine($Line)
}
$bbcode.ToString() | Set-Content -Encoding utf8NoBOM README.bbcode -NoNewline
}
if(Test-Path '3rd_Party_Notice.md'){
$7zFiles.Add('3rd_Party_Notice.md')
}
if(Test-Path 'LICENSE'){
$7zFiles.Add('LICENSE')
}
if(Test-Path $ModInfo.Logo){
$7zFiles.Add($ModInfo.logo)
}
$7zFiles.Add("fomod\info.xml")
$7zFiles.Add("fomod\ModuleConfig.xml")
Write-Host ""
Write-Host "Archive files:"
foreach ($file in $7zFiles) {
Write-Host $file
}
Remove-Item -Path $Config.PackageName -Force -ErrorAction SilentlyContinue
7za.exe a -t7z $Config.PackageName $7zFiles
Pop-Location
$EndTime = [datetime]::UtcNow
$Elapsed = ($EndTime - $StartTime).TotalSeconds
Write-Host @"
Build ended $EndTime UTC
$Elapsed (s)
"@