-
Notifications
You must be signed in to change notification settings - Fork 2
/
export_and_patch_cartridge_release.sh
executable file
·183 lines (143 loc) · 7.57 KB
/
export_and_patch_cartridge_release.sh
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
#!/bin/bash
# Export and patch cartridge releases, then update existing archives with patched executables
# Also apply small tweaks to make release work completely:
# - rename HTML file to index.html to make it playable directly in browser (esp. on itch.io)
# - add '.png' to every occurrence of '.p8' in copy of game source before exporting to PNG
# (to allow reload() to work with png cartridges)
# Make sure to first build full game in release
# Configuration: paths
picoboots_scripts_path="$(dirname "$0")/pico-boots/scripts"
picoboots_plates_path="$(dirname "$0")/pico-boots/plates"
game_scripts_path="$(dirname "$0")"
data_path="$(dirname "$0")/data"
# Linux only
carts_dirpath="$HOME/.lexaloffle/pico-8/carts"
config_plates_dirpath="$HOME/.lexaloffle/pico-8/plates"
# Configuration: cartridge
pico8_version=`cat "$data_path/pico8_version.txt"`
version=`cat "$data_path/version.txt"`
cartridge_stem="picosonic"
export_folder="$carts_dirpath/${cartridge_stem}/v${version}_release"
cartridge_basename="${cartridge_stem}_v${version}_release"
# Verify that the export folder is present. This does not guarantee we built and installed all cartridges
# to carts correctly, but if not present don't even try to export.
if [[ ! -d "$export_folder" ]]; then
echo "No directory found at $export_folder. Make sure to build and install the cartridges in release first. STOP."
exit 1
fi
# Configuration: sub-directories
rel_p8_folder="${cartridge_basename}_cartridges"
rel_png_folder="${cartridge_basename}_png_cartridges"
rel_bin_folder="${cartridge_basename}.bin"
rel_web_folder="${cartridge_basename}_web"
p8_folder="${export_folder}/${rel_p8_folder}"
png_folder="${export_folder}/${rel_png_folder}"
bin_folder="${export_folder}/${rel_bin_folder}"
web_folder="${export_folder}/${rel_web_folder}"
# Cleanup p8 folder in case old extra cartridges remain that would not be overwritten
rm -rf "${p8_folder}/"*
# Cleanup png folder as PICO-8 will prompt before overwriting an existing cartridge with the same name,
# and we cannot reply "y" to prompt in headless script (and png tends to keep old label when overwritten)
# Note that we prefer deleting folder content than folder, to avoid file browser/terminal sometimes
# continuing to show old folder in system bin. Make sure to place blob * outside ""
rm -rf "${png_folder}/"*
# Cleanup bin folder as we want to remove any extraneous files too
# Note: PICO-8 used to accumulate files in .zip for each export (even homonymous files!),
# making this mandatory, but this was fixed in 0.2.2. We still decided to keep the line to be clean.
rm -rf "${bin_folder}/"*
# p8 cartridges can be distributed as such, so just copy them to the folder to zip later
mkdir -p "$p8_folder"
cp "${export_folder}/"*.p8 "$p8_folder"
# Create a variant of each non-data cartridge for PNG export, that reloads .p8.png instead of .p8
adapt_for_png_cmd="python3.6 \"$picoboots_scripts_path/adapt_for_png.py\" "${export_folder}/${cartridge_stem}_*.p8
echo "> $adapt_for_png_cmd"
bash -c "$adapt_for_png_cmd"
if [[ $? -ne 0 ]]; then
echo ""
echo "Adapt for PNG step failed, STOP."
exit 1
fi
# Copy custom template to PICO-8 config plates folder as "${cartridge_basename}_template.html"
# (just to avoid conflicts with other games)
cp "${picoboots_plates_path}/custom_template.html" "${config_plates_dirpath}/${cartridge_stem}_template.html"
# Export via PICO-8 editor: PNG cartridges, binaries, HTML
pico8 -x "$game_scripts_path/export_game_release.p8"
if [[ $? -ne 0 ]]; then
echo ""
echo "Export game release via PICO-8 step failed, STOP."
exit 1
fi
# ingame is the biggest cartridge so if PNG export fails, this one will fail first
if [[ ! -f "${png_folder}/picosonic_ingame.p8.png" ]]; then
echo ""
echo "Exporting PNG cartridge for ingame via PICO-8 failed, STOP. Check that this cartridge compressed size <= 100% even after adding '.png' for reload."
exit 1
fi
if [[ ! -d "$bin_folder" || ! $(ls -A "$bin_folder") ]]; then
echo ""
echo "Exporting game release binaries via PICO-8 failed, STOP. Check that each cartridge compressed size <= 100%."
exit 1
fi
# Patch the runtime binaries in-place with 4x_token, fast_reload, fast_load (experimental) if available
patch_bin_cmd="\"$picoboots_scripts_path/patch_pico8_runtime.sh\" --inplace \"$pico8_version\" \"$bin_folder\" \"$cartridge_basename\""
echo "> $patch_bin_cmd"
bash -c "$patch_bin_cmd"
if [[ $? -ne 0 ]]; then
echo ""
echo "Patch bin step failed, STOP."
exit 1
fi
# Rename HTML file to index.html for direct play-in-browser
html_filepath="${web_folder}/${cartridge_basename}.html"
mv "$html_filepath" "${web_folder}/index.html"
# Patch the HTML export in-place with 4x_token, fast_reload
js_filepath="${web_folder}/${cartridge_basename}.js"
patch_js_cmd="python3.6 \"$picoboots_scripts_path/patch_pico8_js.py\" \"$js_filepath\" \"$js_filepath\""
echo "> $patch_js_cmd"
bash -c "$patch_js_cmd"
if [[ $? -ne 0 ]]; then
echo ""
echo "Patch JS step failed, STOP."
exit 1
fi
# Archiving
# The archives we create here keep all the files under a folder with the full game name
# to avoid extracting files "in the wild". They are meant for manual distribution and preservation.
# itch.io uses a diff system with butler to only upload minimal patches, but surprisingly works well
# with folder structure changing slightly (renaming containing folder and executable), so don't worry
# about providing those customized zip archives to butler.
# Note that for OSX, the .app folder is at the same time the app and the top-level element.
pushd "${export_folder}"
# P8 cartridges archive (delete existing one to be safe)
rm -f "${cartridge_basename}_cartridges.zip"
zip -r "${cartridge_basename}_cartridges.zip" "$rel_p8_folder"
# PNG cartridges archive (delete existing one to be safe)
rm -f "${cartridge_basename}_png_cartridges.zip"
zip -r "${cartridge_basename}_png_cartridges.zip" "$rel_png_folder"
# PNG cartridges archive (delete existing one to be safe)
rm -f "${cartridge_basename}_png_cartridges.zip"
zip -r "${cartridge_basename}_png_cartridges.zip" "$rel_png_folder"
# HTML archive (delete existing one to be safe)
rm -f "${cartridge_basename}_web.zip"
zip -r "${cartridge_basename}_web.zip" "${cartridge_basename}_web"
# Bin archives
pushd "${rel_bin_folder}"
# Linux archive
# Rename linux folder with full game name so our archive contains a self-explanatory folder ()
mv "linux" "${cartridge_basename}_linux"
# To minimize operations, do not recreate the archive, just replace the executable in the archive generated by PICO-8 export
# with the patched executable. We still get some warnings about "Local Version Needed To Extract does not match CD"
# on other files, so make the operation quiet (-q)
zip -q "${cartridge_basename}_linux.zip" "${cartridge_basename}_linux/${cartridge_basename}"
# OSX archive
# Replace the executable in the archive generated by PICO-8 export with the patched executable
zip -q "${cartridge_basename}_osx.zip" "${cartridge_basename}.app/Contents/MacOS/${cartridge_basename}"
# Windows archive
# Rename linux folder with full game name so our archive contains a self-explanatory folder as the initial archive
mv "windows" "${cartridge_basename}_windows"
# To minimize operations, do not recreate the archive, just replace the executable in the archive generated by PICO-8 export
# with the patched executable. We still get some warnings about "Local Version Needed To Extract does not match CD"
# on other files, so make the operation quiet (-q)
zip -q "${cartridge_basename}_windows.zip" "${cartridge_basename}_windows/${cartridge_basename}.exe"
popd
popd