Skip to content

Commit

Permalink
create_config fix: Avoid creating files with invalid path names (#425)
Browse files Browse the repository at this point in the history
* create_config: cleanup the basename to avoid using characters that are note valid paths

* version
  • Loading branch information
AngheloAlf authored Nov 27, 2024
1 parent ca45f21 commit 262a7e6
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 10 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# splat Release Notes

### 0.30.1

* Fix `create_config` not handling unsafe path characters.

### 0.30.0

* New `asmtu` segment type.
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ The brackets corresponds to the optional dependencies to install while installin
If you use a `requirements.txt` file in your repository, then you can add this library with the following line:

```txt
splat64[mips]>=0.30.0,<1.0.0
splat64[mips]>=0.30.1,<1.0.0
```

### Optional dependencies
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[project]
name = "splat64"
# Should be synced with src/splat/__init__.py
version = "0.30.0"
version = "0.30.1"
description = "A binary splitting tool to assist with decompilation and modding projects"
readme = "README.md"
license = {file = "LICENSE"}
Expand Down
2 changes: 1 addition & 1 deletion src/splat/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
__package_name__ = __name__

# Should be synced with pyproject.toml
__version__ = "0.30.0"
__version__ = "0.30.1"
__author__ = "ethteck"

from . import util as util
Expand Down
25 changes: 18 additions & 7 deletions src/splat/scripts/create_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,27 @@ def main(file_path: Path):
log.error(f"create_config does not support the file format of '{file_path}'")


def remove_invalid_path_characters(p: str) -> str:
invalid_characters = ["<", ">", ":", '"', "/", "\\", "|", "?", "*"]
for invalid in invalid_characters:
p = p.replace(invalid, "_")
return p


def create_n64_config(rom_path: Path):
rom_bytes = rominfo.read_rom(rom_path)

rom = rominfo.get_info(rom_path, rom_bytes)
basename = rom.name.replace(" ", "").replace("/", "_").lower()
basename = rom.name.replace(" ", "").lower()
cleaned_basename = remove_invalid_path_characters(basename)

header = f"""\
name: {rom.name.title()} ({rom.get_country_name()})
sha1: {rom.sha1}
options:
basename: {basename}
target_path: {rom_path.with_suffix(".z64")}
elf_path: build/{basename}.elf
elf_path: build/{cleaned_basename}.elf
base_path: .
platform: n64
compiler: {rom.compiler}
Expand All @@ -52,7 +60,7 @@ def create_n64_config(rom_path: Path):
# build_path: build
# create_asm_dependencies: True
ld_script_path: {basename}.ld
ld_script_path: {cleaned_basename}.ld
ld_dependencies: True
find_file_boundaries: True
Expand Down Expand Up @@ -166,7 +174,7 @@ def create_n64_config(rom_path: Path):
- [0x{rom.size:X}]
"""

out_file = f"{basename}.yaml"
out_file = f"{cleaned_basename}.yaml"
with open(out_file, "w", newline="\n") as f:
print(f"Writing config to {out_file}")
f.write(header)
Expand All @@ -175,14 +183,16 @@ def create_n64_config(rom_path: Path):

def create_psx_config(exe_path: Path, exe_bytes: bytes):
exe = psxexeinfo.PsxExe.get_info(exe_path, exe_bytes)
basename = exe_path.name.replace(" ", "").replace("/", "_").lower()
basename = exe_path.name.replace(" ", "").lower()
cleaned_basename = remove_invalid_path_characters(basename)

header = f"""\
name: {exe_path.name}
sha1: {exe.sha1}
options:
basename: {basename}
target_path: {exe_path}
elf_path: build/{cleaned_basename}.elf
base_path: .
platform: psx
compiler: PSYQ
Expand All @@ -192,7 +202,8 @@ def create_psx_config(exe_path: Path, exe_bytes: bytes):
# build_path: build
# create_asm_dependencies: True
ld_script_path: {basename}.ld
ld_script_path: {cleaned_basename}.ld
ld_dependencies: True
find_file_boundaries: False
gp_value: 0x{exe.initial_gp:08X}
Expand Down Expand Up @@ -257,7 +268,7 @@ def create_psx_config(exe_path: Path, exe_bytes: bytes):
- [0x{exe.size:X}]
"""

out_file = f"{basename}.yaml"
out_file = f"{cleaned_basename}.yaml"
with open(out_file, "w", newline="\n") as f:
print(f"Writing config to {out_file}")
f.write(header)
Expand Down

0 comments on commit 262a7e6

Please sign in to comment.