froggestspirit / sdattool Goto Github PK
View Code? Open in Web Editor NEWUnpack/Pack NDS SDAT Files
License: GNU General Public License v3.0
Unpack/Pack NDS SDAT Files
License: GNU General Public License v3.0
For the most part, this option works, removing unused items in infoblock, as well as removing files with duplicate MD5 checksums. There was an issue with 2 SBNK that are unique, but sharing 2 duplicate SWAR, not being renamed properly in the infoblock, causing a glitchy build.
Untested, but since MD5 is only calculated at time of unpack, a modified duplicate file will still be removed when rebuilding optimized.
Fix: Recalculate MD5 at build time if optimizing the build
In the interest of transpiling this code to C/C++, I recommend the following changes. Many of them are cosmetic, but all will improve code readability.
Define all global variables at the top of the file, before methods. This will make it easier to determine the type of a global variable that's used in a module-level method. It also provides security against exceptions like NameError and UnboundLocalError, and allows intellisense editors to detect if you may be shadowing a global variable.
Use list/dict comprehension where feasible. This is generally faster and more succinct than creating the container empty and appending to it.
Before:
foo = []
foo.append(1)
foo.append('apple')
foo.append(3.14159)
After:
foo = [1, 'apple', 3.14159]
Indent with 4 spaces instead of a tab character. This will cause your file to render consistently across editors. In particular, on GitHub where tabs are displayed as 8 characters wide, switching to 4 spaces will make the code's appearance easier on the eyes.
Conditional statements don't need to be wrapped in parentheses. Many IDEs will gripe about this.
Before:
if(my_var == 0):
pass
After:
if my_var == 0:
pass
with
statements where feasible. Some classes in Python, file types in particular, support the with
statement, which simplifies the following workflow:Before:
file = open(filename, 'rb')
try:
do stuff with file
finally:
file.close()
After:
with open(filename, 'rb') as file:
do stuff with file
# On unindent, file is automatically closed.
argparse
(Python standard library) to parse commandline arguments. Python implements argument parsing in a very easy-to-write and easy-to-read manner because, unlike with C/C++, it can.Before:
for i, argument in enumerate(sysargv):
if(i > 0): # The first iteration is wasted cycles.
if(argument.startswith("-")):
if(argument == "-u" or argument == "--unpack):
mode = 1
After:
parser = argparse.ArgumentParser()
parser.add_argument('infile', type=argparse.FileType('rb'))
parser.add_argument('outfile', type=argparse.FileType('wb'), nargs='?')
mode_grp = parser.add_mutually_exclusive_group(required=True)
mode_grp.add_argument('-u', '--unpack', dest='mode', action='store_false')
mode_grp.add_argument('-b', '--build', dest='mode', action='store_true')
parser.add_argument('-m', '--md5', dest='md5', action='store_true')
parser.add_argument('-o', '--optimize', dest='optimize', action='store_true')
parser.add_argument('-ru', '--removeUnused', dest='removeUnused', action='store_true')
parser.add_argument('-ns', '--noSymbBlock', dest='noSymbBlock', action='store_true')
args = parser.parse_args()
Note: -h
and --help
are added automatically by the argparse library.
in
instead. Example:Before:
if(x == 1 or x == 2 or x == 4 or x == 8):
pass
After:
if x in (1, 2, 4, 8):
pass
Rebuilding SDAT will fail on any option if any files are removed or edited.
Exampe of failiure regarding removed files using Pokemon B2W2's Sdat:
C:\Users\Goji Goodra\Downloads\SDATEDIT>py SDATTool -b b2w2.sdat
Building...
Traceback (most recent call last):
File "C:\Users\Goji Goodra\Downloads\SDATEDIT\SDATTool_main_.py", line 391, in
main()
File "C:\Users\Goji Goodra\Downloads\SDATEDIT\SDATTool_main_.py", line 384, in main
build(args)
File "C:\Users\Goji Goodra\Downloads\SDATEDIT\SDATTool_main_.py", line 325, in build
build_fileBlock(sdat, args)
File "C:\Users\Goji Goodra\Downloads\SDATEDIT\SDATTool\Sdat.py", line 574, in build_fileBlock
write_sseq(read_sseq_from_midi(args, fName), args, fName)
File "C:\Users\Goji Goodra\Downloads\SDATEDIT\SDATTool\Midi.py", line 301, in read_sseq_from_midi
raise Exception(f"Missing File:{testPath}")
Exception: Missing File:b2w2/Files/SEQ/SEQ_PV001.mid
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\Goji Goodra\AppData\Local\Programs\Python\Python310\lib\runpy.py", line 196, in _run_module_as_main
return _run_code(code, main_globals, None,
File "C:\Users\Goji Goodra\AppData\Local\Programs\Python\Python310\lib\runpy.py", line 86, in run_code
exec(code, run_globals)
File "C:\Users\Goji Goodra\Downloads\SDATEDIT\SDATTool_main.py", line 393, in
raise Exception(e)
Exception: Missing File:b2w2/Files/SEQ/SEQ_PV001.mid
In this case, I had manually removed all non music related files. (SFX and the like, leaving music, musical effects and STRM alone)
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.