Skip to content

Commit f892336

Browse files
committed
Add asset embedding script
1 parent 8675d6c commit f892336

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

embed_assets.py

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import re
2+
from sys import argv
3+
from pathlib import Path
4+
5+
6+
PATTERN = re.compile(r'\b(Load\w+)\s*\(\s*"([^"]+)"')
7+
8+
9+
def include_bytes(file: str) -> tuple[str, int]:
10+
resource = Path(file)
11+
assert resource.is_file()
12+
13+
content = resource.read_bytes()
14+
length = len(content)
15+
16+
content = "".join(f"\\x{x:02x}" for x in content)
17+
return (f'"{content}"', length)
18+
19+
20+
def file_type(file: str) -> str:
21+
resource = Path(file)
22+
suffix = resource.suffix
23+
assert suffix
24+
return suffix
25+
26+
27+
def replacer(m: re.Match) -> str:
28+
func, file = m.groups()
29+
bytes_content, length = include_bytes(file)
30+
return f'{func}FromMemory("{file_type(file)}", (const unsigned char *) {bytes_content}, {length}'
31+
32+
33+
def process_file(file: str):
34+
file = Path(file)
35+
assert file.is_file()
36+
37+
content = file.read_text()
38+
new_content = PATTERN.sub(replacer, content)
39+
file.write_text(new_content)
40+
41+
42+
def main():
43+
for file in argv[1:]:
44+
assert file.endswith(".c")
45+
for file in argv[1:]:
46+
process_file(file)
47+
48+
49+
if __name__ == "__main__":
50+
raise SystemExit(main())

0 commit comments

Comments
 (0)