-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.ps1
235 lines (185 loc) · 5.75 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
param(
[Parameter()]
[String] $mod_folder,
[Switch] $verbos,
[Switch] $install,
[Switch] $uninstall
)
$packed_explorer_path = '.\bins\spe.exe';
$pyhton_path = 'C:\Program Files (x86)\Python\python.exe';
$scrapland_path = 'D:\Games\SteamLibrary\steamapps\common\Scrapland';
if (-not (Test-Path(".\config.ps1"))) {
Copy-Item .\config.example.ps1 .\config.ps1;
}
. "$PSScriptRoot\config.ps1"
$output_path = ".\out"
$pack_expl_name = 'ScrapPackedExplorerCli';
$pack_expl_ver = [version]'0.3.1';
$userless_err_msgs = @(
"ImportError: No module named Scrap",
"ImportError: No module named SAI",
"ImportError: No module named SAct",
"ImportError: No module named SFX",
"ImportError: No module named SInput",
"ImportError: No module named SLogic",
"ImportError: No module named SNet",
"ImportError: No module named SScorer",
"ImportError: No module named SSound",
"ImportError: No module named SVec",
"ImportError: No module named SWeap"
);
$folders_exclude = (
'bins',
'out'
);
function quite_rm($path) {
Remove-Item $path 2>&1 | out-null;
}
function is_string_in_array($string, $array) {
return $null -ne ($string | Where-Object { $array -match $_ });
}
function disable_colorded_output() {
$PSStyle.OutputRendering = [System.Management.Automation.OutputRendering]::PlainText;
}
function normalize_mod_name($mod) {
$mod = $mod.TrimEnd("\");
$mod = $mod.TrimStart(".\");
return $mod;
}
function check_pack_expl() {
if (-not (test-path $packed_explorer_path)) {
Write-Output "Error: $pack_expl_name was not found at '$packed_explorer_path'. Please download $pack_expl_name >= $pack_expl_ver";
exit 1;
}
# --version outputs to stderr;
$err = (& $packed_explorer_path --version) 2>&1;
$spe_version = $err[0].ToString();
$null = $spe_version -match '(?<name>[^\d]+)(?<version>\d.+)';
$name, $version = $Matches['name'].TrimEnd('.').Trim(), [version]$Matches['version'];
$is_pack_expl_good = $name -ne $pack_expl_name -or
$version.Major -lt $pack_expl_ver.Major -or
$version.Minor -lt $pack_expl_ver.Minor -or
$version.Build -lt $pack_expl_ver.Build;
if ($is_pack_expl_good) {
Write-Output "Error: wrong binary at '$packed_explorer_path'. $name >= $version needed.";
exit 1;
}
}
function compile_pyhton($mod) {
Push-Location $mod
Get-ChildItem "." -Recurse -Filter *.py | Foreach-Object {
Push-Location $_.Directory.FullName
$file_name = $_.Name
# NOTE: Pyhton will not compile with `pyhton $file_name` because of import errors
# but if we try to import required file it will compile
$output = (& $pyhton_path -c "import $file_name") 2>&1
# Surpressing error messages about Scrap/SInput/etc module not found
if (-not (is_string_in_array $output $userless_err_msgs) -or $v -or $verbos) {
# Additional empty lines for better errorformat parsing in vim. Check .vimrc for more info
Write-Output "$mod/../$file_name";
Write-Output ""
Write-Output $output
Write-Output ""
}
Pop-Location
}
Pop-Location
}
function pack_mod($mod) {
quite_rm "$output_path\$mod.packed";
if (Test-Path $mod\packed\) {
& $packed_explorer_path "$output_path\$mod.packed" add -s $mod\packed;
return;
} else {
& $packed_explorer_path "$output_path\$mod.packed" add -s $mod\;
& $packed_explorer_path "$output_path\$mod.packed" remove -d README.md;
}
}
function load_mod($mod) {
unload_mod($mod);
Copy-Item "$output_path\$mod.packed" $scrapland_path\Mods;
}
function unload_mod($mod) {
if (test-path "$scrapland_path\Mods\$mod.packed") {
quite_rm "$scrapland_path\Mods\$mod.packed";
}
}
function load_mod_languages($mod) {
if (-not (Test-Path $mod\lang\)) {
return;
}
$mod_lang_folder = "$scrapland_path\Language\$mod";
if (-not (Test-Path $mod_lang_folder)) {
mkdir $mod_lang_folder | Out-Null;
}
Copy-Item $mod\lang\* $mod_lang_folder;
}
function unload_mod_languages($mod) {
if (-not (Test-Path $mod\lang\)) {
return;
}
Remove-Item -Recurse "$scrapland_path\Language\$mod\";
}
function build_game_languages() {
$lang_path = "$scrapland_path\Language";
Get-ChildItem $lang_path -File -Filter *.txt.bak | ForEach-Object {
Copy-Item $_ ($_.FullName.Replace(".bak", "")) -Force
}
Get-ChildItem $lang_path -Directory | ForEach-Object {
Get-ChildItem $_ -Filter *.txt | ForEach-Object {
if (-not (Test-Path "$lang_path\$($_.Name).bak")) {
Write-Output "" >> $lang_path\$($_.Name);
Copy-Item "$lang_path\$($_.Name)" "$lang_path\$($_.Name).bak" -Force;
}
Get-Content $_ >> $lang_path\$($_.Name);
}
}
}
function build_mod($mod) {
compile_pyhton($mod);
pack_mod($mod);
}
function install_mod($mod) {
load_mod($mod);
load_mod_languages($mod);
}
function uninstall_mod($mod) {
unload_mod($mod);
unload_mod_languages($mod);
}
function make_all_mods() {
Get-ChildItem "." -Dir | Foreach-Object {
if (-not (is_string_in_array $_.Name $folders_exclude)) {
make_mod($_.Name);
}
}
}
function uninstall_all_mods() {
Get-ChildItem "." -Dir | Foreach-Object {
if (-not (is_string_in_array $_.Name $folders_exclude)) {
uninstall_mod($_.Name);
}
}
}
function make_mod($mod) {
build_mod($mod);
if ($install) {
install_mod($mod);
}
}
function main() {
disable_colorded_output;
check_pack_expl;
$mod_folder = normalize_mod_name($mod_folder);
if (-not $mod_folder) {
if ($uninstall) { uninstall_all_mods; } else { make_all_mods; }
build_game_languages;
return;
}
if (is_string_in_array $mod_folder $folders_exclude) {
return;
}
if ($uninstall) { uninstall_mod($mod_folder); } else { make_mod($mod_folder); }
build_game_languages;
}
main