Created
October 23, 2022 16:42
-
-
Save jsmolina/d2d397d210e23dd4c4c76377a8b5b620 to your computer and use it in GitHub Desktop.
generates menu for dos launching games
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os, glob | |
import shutil | |
import string | |
translation_table = dict.fromkeys(map(ord, ' [](),.~!@#$%^&*{}: '), None) | |
# todo use argparse to parametrize | |
OUT = "./games" | |
ROOT = ["./DIST/games1", | |
"./DIST/games2", | |
"./DIST/games3"] | |
DOSnames = [] | |
def dir_to_dos(longname): | |
# code token from Total Dos launcher by MobyGamer | |
dname = longname.encode('ascii', 'ignore').decode() | |
if len(longname) > 12: | |
dname = dname.translate(translation_table) | |
dname = str.upper(dname) | |
dname = dname[0:8] | |
dname = dname.replace(" ", "") | |
collided = dname | |
# Do we have a collision? | |
if dname in DOSnames: | |
for i in string.ascii_uppercase + string.digits: | |
dname = longname.translate(translation_table) + i | |
dname = str.upper(dname) | |
dname = dname[0:8] | |
if dname not in DOSnames: | |
break | |
else: | |
# If we still have a collision, we need to mangle the name some more | |
for i in string.ascii_uppercase + string.digits: | |
for j in string.ascii_uppercase + string.digits: | |
oldname = dname | |
dname = longname.translate(translation_table)[0:6] + i + j + longname[-4:] | |
dname = str.upper(dname) | |
if dname not in DOSnames: | |
break | |
if dname not in DOSnames: | |
break | |
# If we got here, too many collisions (need more code!) | |
if dname in DOSnames: | |
print("Namespace collision converting", longname, "to", dname) | |
print("Ask the progammer to enhance the collision algorithm.") | |
exit(8) | |
dname = dname.replace(" ", "") | |
DOSnames.append(dname) | |
return dname | |
def find_executable(directory): | |
for w in os.listdir(directory): | |
filename = w.lower() | |
if filename.endswith(".exe") or filename.endswith(".bat") or filename.endswith(".com"): | |
return w | |
def main(): | |
for origin in ROOT: | |
for dirname in os.listdir(origin): | |
if dirname.startswith("."): | |
continue | |
fulldirpath = os.path.join(origin, dirname) | |
executable = find_executable(fulldirpath) | |
shortname = dir_to_dos(dirname) | |
print(f"{shortname}:") | |
print(f" title: {dirname}") | |
print(f" author: x") | |
print(f" genre: x") | |
print(f" year: X") | |
print(f" information: |") | |
print(" TBD") | |
print(f" configure: |") | |
print(f" cd {shortname}") | |
print(f" setup.exe") | |
print(f" play: |") | |
print(f" cd {shortname}") | |
print(f" {executable}") | |
shutil.copytree(fulldirpath, os.path.join(OUT, shortname)) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment