Added check to not use files under 128KiB

This commit is contained in:
Uttarayan Mondal
2021-01-30 14:33:06 +05:30
parent b7f1173824
commit 59f0f213f4
2 changed files with 27 additions and 1 deletions

View File

@@ -1,6 +1,7 @@
## Drastic to MelonDS
This script will convert the dsv save from drastic emulator to a melonDS save file
This also works for desmume to MelonDS save files since desmume also seems to add padding to the end of the file.
### Usage
@@ -17,6 +18,16 @@ optional arguments:
-f, --force Overwrite the output file if it exists
```
### Install
Add it to a folder in your $PATH
```bash
install -Dm755 dr2mds.py ~/.local/bin/dr2mds
```
_For windows users just run the script from the folder_
### How to use
You need python 3 installed.
@@ -33,7 +44,7 @@ You can also specify the output like
./dr2mds.py "Pokemon Black.dsv" -o "PKMN.sav"
```
### How does this works ?
### How does this work ?
MelonDS determines the type of save using exact byte count in the save file

View File

@@ -9,6 +9,16 @@ import os
sys.tracebacklimit = 0
class SaveFileTooSmallError(Exception):
def __init__(self, size, message="Save file is smaller than 128KiB"):
self.size = size
self.message = message
super().__init__(self.message)
def __str__(self):
return f"{self.size} is smaller than 131072 or 128KiB"
def closest2pow(n):
# int always returns the floor of the float
# log(n)/log(2) gives log base 2 of n
@@ -18,6 +28,11 @@ def closest2pow(n):
def drastic2melonds(infile, outfile):
with open(infile, "rb") as inf, open(outfile, "wb") as ouf:
in_bytes = os.stat(infile).st_size
if in_bytes < 131072:
print(f"The file {infile} seems to be smaller than 128KiB"
" and not a valid save file", file=sys.stderr)
raise SaveFileTooSmallError(in_bytes)
out_bytes = closest2pow(in_bytes)
print(f"Reading {in_bytes} bytes from {infile}", file=sys.stderr)
print(f"Writing {out_bytes} bytes to {outfile}", file=sys.stderr)