| |
| |
|
|
| |
| |
| |
| |
|
|
| |
| |
| |
|
|
| |
| |
|
|
| import sys, os, datetime |
| from io import BytesIO |
|
|
| from hddimgreader import read_hddimage |
| from hddimgwriter import write_hddimage |
| import zipfile |
| import urllib.request |
|
|
| |
| DRIVES = [ "C:\\", "D:\\", "E:\\", "F:\\" ] |
|
|
| def usage(msg=None): |
| if msg: print("Error:", msg) |
| print("Usage mkhdmenu.py [options] <imagename|size|cfgfile> [commands...] [outname]") |
| print("Options:") |
| print(" -export-bootloader=<name> if present export bootloaders from MBR and") |
| print(" bootsectors to <name>_mbr.bin and <name>_bootsector.bin") |
| print(" -quiet print less output") |
| print("<imagename|size> name of existing hdd image to start with or size description") |
| print(" of the individual partitions like e.g. 16M+16384K for two") |
| print(" partitions of 16 megabytes each") |
| print("Commands:") |
| print(" dest=src copy src into the dest path in the image.") |
| print(" Src can be a zip file, a single regular file or") |
| print(" a entire directory like e.g.") |
| print(" C:\\GAMES\\BUBLGOST=zips/Bubble_Ghost.zip") |
| print("") |
| print("Bootloaders for AHDI and ICD will be installed in the MBR and the bootsector of") |
| print("partition C if a file named SHDRIVER.SYS (AHDI) or ICDBOOT.SYS (ICD) is being") |
| print("included in partition C. This also works with hddriver and CBHD if they are being") |
| print("(re-)named SHDRIVER.SYS") |
| |
| sys.exit(0) |
|
|
| def timestr(time): |
| return "{:02d}:{:02d}:{:02d}".format((time>>11)&0x1f,(time>>5)&0x3f,2*(time&0x1f)) |
|
|
| def datestr(date): |
| return "{:02d}.{:02d}.{:04d}".format(date&0x1f,(date>>5)&0x0f,1980+((date>>9)&0x7f)) |
|
|
| |
| PPERA_PRG = [ "RUNME.TOS", "RUNFALC.TOS", "START.TOS", "START2M.TOS", "START.PRG" ] |
|
|
| |
| |
| |
|
|
| def statistics(partitions): |
| def fs_statistics(fs): |
| s = { "files":0, "directories":1, "datasize":0 } |
| |
| for i in fs: |
| if "subdir" in i: |
| d = fs_statistics(i["subdir"]) |
| s["files"] += d["files"] |
| s["directories"] += d["directories"] |
| s["datasize"] += d["datasize"] |
| elif "data" in i: |
| s["files"] += 1 |
| s["datasize"] += len(i["data"]) |
|
|
| return s |
|
|
| print("== Statistics ==") |
| |
| files = 0 |
| directories = 0 |
| datasize = 0 |
| |
| for i in partitions: |
| s = fs_statistics(i["files"]) |
| files += s["files"] |
| directories += s["directories"] |
| datasize += s["datasize"] |
|
|
| print("Total number of directories: ", directories) |
| print("Total number of files: ", files) |
| print("Total data bytes: ", datasize) |
|
|
| def dump_trees(partitions): |
| |
| |
| def dump_tree(prefix, fs): |
| for f in fs: |
| if "subdir" in f: |
| print(prefix+f["name"]+"\\") |
| dump_tree(prefix+" ", f["subdir"]) |
| else: |
| print(prefix+f["name"], (" "*24)[:-len(f["name"]+prefix)-len(str(len(f["data"])))], len(f["data"]), " ", timestr(f["time"]), " ", datestr(f["date"])) |
|
|
| for i in range(len(partitions)): |
| print("== Files on partition",DRIVES[i],"==") |
| dump_tree("", partitions[i]["files"]) |
|
|
| def find_file(partitions, name): |
| def find_file_int(prefix, fs, name): |
| for f in fs: |
| if "subdir" in f: |
| if find_file_int(prefix+f["name"]+"\\", f["subdir"], name): |
| return True |
| else: |
| if prefix+f["name"] == name: |
| return True |
|
|
| return False |
|
|
| for i in range(len(partitions)): |
| if find_file_int("", partitions[i]["files"], name): |
| return i |
|
|
| return None |
| |
| |
| |
| |
| |
|
|
| def get_file(flist, name): |
| for f in flist: |
| if f["name"] == name: |
| return f |
|
|
| if "subdir" in f: |
| h = get_file(f["subdir"], name) |
| if h: return h |
| |
| return None |
| |
| def add_file(files, file): |
| |
| for p in file["name"].split("\\")[:-1]: |
| d = get_file(files, p) |
| if not d: |
| |
| d = { "name": p, "subdir": [], "time": file["time"], "date": file["date"] } |
| files.append(d) |
|
|
| if not "subdir" in d: |
| print("Error, is not a directory", p) |
| return False |
| |
| files = d["subdir"] |
|
|
| |
| file["name"] = file["name"].split("\\")[-1] |
|
|
| index = None |
| |
| for i in range(len(files)): |
| if files[i]["name"] == file["name"]: |
| index = i |
|
|
| if index != None: |
| files[index] = file |
| else: |
| files.append(file) |
| |
| return True |
|
|
| def import_zip(drive, partition, src, dst, prg): |
| |
| name = None |
| if isinstance(src, tuple): |
| name = src[0] |
| src = BytesIO(src[1]) |
| try: |
| archive = zipfile.ZipFile(src, 'r') |
| except Exception as e: |
| print(str(e)) |
| return None |
|
|
| |
| |
| if dst: |
| if not dst[-1] == "\\": dst = dst + "\\" |
| else: |
| |
| if prg: |
| |
| dst = "GAMES\\"+prg+"\\" |
| else: |
| dst = None |
| |
| |
| for filename in archive.namelist(): |
| for prg_name in PPERA_PRG: |
| if not dst and filename.lower().endswith(prg_name.lower()): |
| dst = "GAMES\\" |
| |
| if not "/" in filename: |
| dst += name + "\\" |
|
|
| if not dst: |
| for filename in archive.namelist(): |
| if not dst and filename.lower().endswith(".prg"): |
| dst = "GAMES\\"+filename.split(".")[0]+"\\" |
| |
| |
| if not dst: |
| print("No program path found in ZIP!") |
| return None |
|
|
| for filename in archive.namelist(): |
| print("Creating", drive+dst+filename.replace("/","\\")) |
| with archive.open(filename) as f: |
| dt = archive.getinfo(filename).date_time |
| ftime = (dt[3] << 11) + (dt[4] << 5) + dt[5]//2 |
| fdate = dt[2] + (dt[1] << 5) + ((dt[0]-1980)<<9) |
|
|
| |
| filename = filename.replace("/","\\") |
|
|
| |
| if filename[-1] != "\\": |
| file = { "name": dst.upper()+filename.upper(), "data": f.read(), "time": ftime, "date":fdate } |
| if not add_file(partition, file): |
| return None |
|
|
| |
| |
| if dst == "GAMES\\": |
| |
| paths = [] |
| for filename in archive.namelist(): |
| if "/" in filename: |
| if not filename.split("/")[0] in paths: |
| paths.append(filename.split("/")[0]) |
|
|
| |
| dst += paths[0] |
| |
| |
| if dst[-1] == "\\": dst = dst[:-1] |
| |
| return drive + dst |
|
|
| def import_directory(drive, partition, src, dst): |
| def import_dir(drive, partition, src, dst): |
| with os.scandir(src) as entries: |
| dst_path = dst.upper() |
| if len(dst_path): dst_path += "\\" |
|
|
| for entry in entries: |
| if entry.is_file(): |
| import_file(drive, partition, src + "/" + entry.name, dst_path + entry.name.upper()) |
|
|
| elif entry.is_dir(): |
| import_dir(drive, partition, src+"/"+entry.name, dst_path + entry.name.upper()) |
|
|
| if len(dst) and dst[-1] == "\\": dst = dst[:-1] |
| src = os.path.normpath(src) |
| |
| import_dir(drive, partition, src, dst) |
| |
| return True |
| |
| def import_file(drive, partition, src, dst): |
| try: |
| f = open(src, 'rb') |
| except Exception as e: |
| print(str(e)) |
| return False |
|
|
| |
| |
| |
| if not dst: |
| dst = src.split("/")[-1].upper() |
| elif dst[-1] == "\\": |
| dst = dst + src.split("/")[-1].upper() |
| else: |
| |
| i = get_file(partition, dst) |
| if i and "subdir" in i: |
| dst = dst + "\\" + src.split("/")[-1].upper() |
|
|
| print("Creating", drive + dst) |
|
|
| dt = datetime.datetime.fromtimestamp(os.stat(src).st_mtime, tz=datetime.timezone.utc) |
| ftime = (dt.hour << 11) + (dt.minute << 5) + dt.second//2 |
| fdate = dt.day + (dt.month << 5) + ((dt.year-1980)<<9) |
|
|
| file = { "name": dst.upper(), "date": fdate, "time": ftime, "data": f.read() } |
| f.close() |
| |
| return add_file(partition, file) |
| |
| def import_item(partitions, src, dst=None): |
| print("Import", src, "to", dst if dst else "<game dir>") |
|
|
| |
| if isinstance(dst, str): |
| if not dst[:3] in DRIVES: |
| print("Error, not a valid partition") |
| return False |
|
|
| if DRIVES.index(dst[:3]) > len(partitions): |
| print("Error, partition not present") |
| return False |
|
|
| |
| partition = partitions[DRIVES.index(dst[:3])] |
|
|
| |
| dst = dst[3:] |
| elif isinstance(dst, int): |
| |
| partition = partitions[dst] |
| dst = None |
| else: |
| |
| partition = partitions[0] |
| |
| |
| if src.lower().startswith("http://") or src.lower().startswith("https://"): |
| |
| |
|
|
| prg = None |
| if not src.lower().endswith(".zip") and src.rsplit(":",1)[0].lower().endswith(".zip"): |
| src, prg = src.rsplit(":",1) |
| |
| |
| if not src.lower().endswith(".zip"): |
| print("Only ZIP files can be downloaded") |
| return False |
| |
| print("Downloading", src) |
| with urllib.request.urlopen(src) as response: |
| if response.getcode() != 200: |
| print("Download failed with code", response.getcode()) |
| return False |
|
|
| |
| bname = src.split("/")[-1].split(".")[0] |
| return import_zip(partition["drive"], partition["files"], (bname, response.read()), dst, prg) |
| |
| |
| if src.lower().startswith("file://"): |
| src = src[7:] |
| |
| |
| if os.path.isfile(src) and src.lower().endswith(".zip"): |
| return import_zip(partition["drive"], partition["files"], src, dst, None) |
| elif os.path.isdir(src): |
| return import_directory(partition["drive"], partition["files"], src, dst) |
| elif os.path.isfile(src): |
| return import_file(partition["drive"], partition["files"], src, dst) |
| else: |
| print("Error, don't know how to import", src) |
| |
| return False |
|
|
| def import_screenshots(partitions, games, data): |
| |
| try: |
| neopics = zipfile.ZipFile("NEOPICS.zip", 'r') |
| except Exception as e: |
| print("Unable to open NEOPICS.zip", str(e)) |
| return |
|
|
| for game in games: |
| name = game |
|
|
| |
| |
| if data: |
| for src in data: |
| if "neopic" in src: |
| if game == src["path"].split("\\")[-1]: |
| name = src["neopic"] |
| |
| |
| if "/" in name: |
| neo = name + "/" + name.split("/")[-1] + ".NEO" |
| else: |
| neo = name[0] + "/" + name + "/" + name + ".NEO" |
|
|
| |
| try: |
| f = neopics.open(neo) |
| except: |
| print("No matching screenshot found for", name) |
| f = None |
|
|
| if f: |
| dt = neopics.getinfo(neo).date_time |
| ftime = (dt[3] << 11) + (dt[4] << 5) + dt[5]//2 |
| fdate = dt[2] + (dt[1] << 5) + ((dt[0]-1980)<<9) |
|
|
| |
| |
| |
| result = find_file(partitions, "GAMES\\"+game+"\\"+game+".PRG") |
| |
| for prg_name in PPERA_PRG: |
| if result == None: |
| result = find_file(partitions, "GAMES\\"+game+"\\"+prg_name) |
| |
| if result != None: |
| |
| file = { "name": "GAMES\\"+game+"\\"+game+".NEO", "data": f.read(), "time": ftime, "date":fdate } |
| print("Adding screenshot", partitions[result]["drive"]+file["name"]) |
| if not add_file(partitions[result]["files"], file): |
| print("Failed to add screenshot!!") |
| else: |
| print("Unable to identify", game) |
|
|
| f.close() |
| |
| neopics.close() |
| |
| def mk_csv(partitions, cfg=None): |
| def csv_scan(files, parent): |
| gamelist = [ ] |
| for f in files: |
| if "subdir" in f: |
| sublist = csv_scan(f["subdir"], f["name"]) |
| for entry in sublist: |
| gamelist.append(f["name"]+"\\"+entry) |
| |
| else: |
| |
| if parent and parent == f["name"].split(".")[0] and f["name"].split(".")[-1].lower() == "prg": |
| gamelist.append(f["name"]) |
| |
| if parent: |
| for prg_name in PPERA_PRG: |
| if f["name"].split("/")[-1].lower() == prg_name.lower(): |
| gamelist.append(f["name"].replace("/", "\\")) |
| return gamelist |
|
|
| return gamelist |
| |
| |
| |
| print("Creating game list in C:\\HDMENU.CSV...") |
| |
| csv = bytearray() |
| games = [ ] |
| for p in range(len(partitions)): |
| plist = csv_scan(partitions[p]["files"], None) |
| for i in plist: |
| print("Found game", DRIVES[p] + i) |
|
|
| |
| speaking_name = None |
| if cfg: |
| for src in cfg["data"]: |
| if "name" in src: |
| if (DRIVES[p]+i).startswith(src["path"]): |
| speaking_name = src["name"] |
|
|
| |
| if not speaking_name: |
| if i.split("\\")[-2] in cfg["links"]: |
| speaking_name = cfg["links"][i.split("\\")[-2]] |
| |
| if speaking_name: |
| csv = csv + speaking_name.encode("latin-1") |
| else: |
| csv = csv + i.split("\\")[-2].encode("latin-1") |
| |
| csv = csv + (";"+DRIVES[p]).encode("latin-1") |
| csv = csv + i.encode("latin-1") |
| csv = csv + "\r\n".encode("latin-1") |
|
|
| games.append(i.split("\\")[-2]) |
| if csv: |
| dt = datetime.datetime.now() |
| ftime = (dt.hour << 11) + (dt.minute << 5) + dt.second//2 |
| fdate = dt.day + (dt.month << 5) + ((dt.year-1980)<<9) |
|
|
| import_screenshots(partitions, games, cfg["data"]) |
| |
| partitions[0]["files"].append( { "name": "HDMENU.CSV", "time":ftime, "date":fdate, "data":csv } ) |
| else: |
| print("Warning, no games found, creating no HDMENU.CSV") |
| |
|
|
| |
|
|
| if len(sys.argv) < 2: usage("No arguments given") |
|
|
| |
| options = { "export-bootloader": None, "quiet": False } |
| arg_idx = 1 |
| while len(sys.argv) > arg_idx and sys.argv[arg_idx][0] == '-': |
| |
| if "=" in sys.argv[arg_idx][1:]: |
| name, parm = sys.argv[arg_idx][1:].split("=",1) |
| else: |
| name = sys.argv[arg_idx][1:] |
| parm = None |
| |
| if not name in options: usage("Unknown option "+sys.argv[arg_idx][1:]) |
|
|
| |
| if not isinstance(name, bool): |
| if not parm and arg_idx+1 == len(sys.argv): |
| usage("Missing option parameter") |
| elif parm: |
| options[name] = parm |
| else: |
| options[name] = sys.argv[arg_idx+1] |
| arg_idx += 1 |
| else: |
| options[name] = True |
|
|
| arg_idx += 1 |
|
|
| def get_size(p): |
| |
| if not ((p[-1] == 'M' or p[-1] == 'K') and len(p) > 1 and p[:-1].isnumeric()) and not p.isnumeric(): |
| return None |
|
|
| if p[-1] == 'M': size = int(p[:-1]) * 1048576 |
| elif p[-1] == 'K': size = int(p[:-1]) * 1024 |
| else: size = int(p) |
|
|
| if size > 16*1024*1024: |
| print("Error, partition size must be 16 Megabytes at most") |
| return None |
| |
| if size & 511: |
| print("Error, partition size must be a multiple of 512") |
| return None |
| |
| return size |
|
|
| def add_hdmenu_cfg(part): |
| print("Creating C:\\HDMENU.CFG") |
|
|
| data = bytearray(168) |
|
|
| data[0:4] = bytearray([0,0,0,3]) |
| data[4] = 0 |
| data[5] = 0 |
| data[6] = 1 |
| data[7] = 0 |
| data[8] = 0 |
| data[9] = 0 |
| data[10] = 2 |
| data[11] = 0 |
| data[12:16] = bytearray([0,0,0,0]) |
| data[16:30] = bytearray([0x20]*14) |
| data[109] = 2 |
| data[110] = 1 |
| data[111] = 0 |
| data[112] = 0 |
| data[113] = 0 |
| data[114] = 0 |
| data[115] = 0 |
| data[116] = 1 |
| data[117] = 1 |
| |
| dt = datetime.datetime.now() |
| ftime = (dt.hour << 11) + (dt.minute << 5) + dt.second//2 |
| fdate = dt.day + (dt.month << 5) + ((dt.year-1980)<<9) |
| part["files"].append( { "name": "HDMENU.CFG", "time":ftime, "date":fdate, "data":data } ) |
| |
| def parse_cfg_file(filename): |
| cfg = { "data": [], "links": { } } |
| with open(filename) as cfgfile: |
| partition_index = 0 |
| |
| |
| for line in cfgfile: |
| line = line.strip() |
| |
| |
| if line[0] != '#': |
| |
| cmd = line.split(" ",1)[0] |
| if cmd.lower() == "img": |
| |
| img = line.split(" ",1)[1].strip().split(";") |
| cfg["img"] = { "name": img[0].strip(), "size": get_size(img[1].strip()) } |
| elif cmd.lower() == "file": |
| |
| src = line.split(" ",1)[1].strip().split(";") |
| data = { "path": src[0].strip(), "url": src[1].strip() } |
| cfg["data"].append(data) |
| elif cmd.lower() == "game": |
| |
| src = line.split(" ",1)[1].strip().split(";") |
| data = { "url": src[0].strip() } |
| data["partition_index"] = partition_index |
| if len(src) >= 2: data["name"]= src[1].strip() |
| if len(src) >= 3: data["neopic"]= src[2].strip() |
| cfg["data"].append(data) |
| elif cmd.lower() == "link": |
| link = line.split(" ",1)[1].strip().split(";") |
| cfg["links"][link[0].strip()] = link[1].strip() |
| elif cmd.lower() == "partition": |
| partition_index += 1 |
| elif cmd.lower() == "cfg": |
| cfg["hdmenu_cfg"] = True |
| elif cmd.lower() == "end": |
| break |
| else: |
| print("Unknown command", cmd) |
| return None |
| |
| |
| partitions = [] |
| for i in range(partition_index+1): |
| partitions.append({"size":cfg["img"]["size"]//512, "files": [], "drive": DRIVES[i] }) |
|
|
| |
| if "hdmenu_cfg" in cfg and cfg["hdmenu_cfg"]: |
| add_hdmenu_cfg(partitions[0]) |
| |
| |
| for item in cfg["data"]: |
| p = import_item(partitions, item["url"], item["path"] if "path" in item else item["partition_index"]) |
| if not "path" in item: item["path"] = p |
|
|
| mk_csv(partitions, cfg) |
| |
| if not options["quiet"]: |
| |
| dump_trees(partitions) |
|
|
| |
| statistics(partitions) |
|
|
| write_hddimage(cfg["img"]["name"], partitions, options) |
|
|
| |
| if len(sys.argv) == arg_idx: usage("Missing <imagename|size|cfgfile> argument") |
|
|
| if not options["quiet"]: print("== mkhdmenu.py ==") |
|
|
| |
| if len(sys.argv) == arg_idx+1 and sys.argv[arg_idx].lower().endswith(".cfg"): |
| print("Building from config file", sys.argv[arg_idx]) |
|
|
| parse_cfg_file(sys.argv[arg_idx]) |
| sys.exit(0) |
|
|
| image = sys.argv[arg_idx] |
| arg_idx += 1 |
|
|
| |
| parts = image.split("+") |
|
|
| |
| is_size = True |
| for p in parts: |
| if not ((p[-1] == 'M' or p[-1] == 'K') and len(p) > 1 and p[:-1].isnumeric()) and not p.isnumeric(): |
| is_size = False |
|
|
| |
| if is_size: |
| partitions = [] |
| for p in parts: |
| if p[-1] == 'M': size = int(p[:-1]) * 1048576 |
| elif p[-1] == 'K': size = int(p[:-1]) * 1024 |
| else: size = int(p) |
|
|
| if size > 16*1024*1024: |
| print("Error, partition size must be 16 Megabytes at most") |
| sys.exit(-1) |
| |
| if size & 511: |
| print("Error, partition size must be a multiple of 512") |
| sys.exit(-1) |
| |
| partitions.append( {"size":size//512, "files": [] } ) |
| else: |
| |
| partitions = read_hddimage(image, options) |
|
|
| if not partitions: sys.exit(-1) |
|
|
| |
| for p in range(len(partitions)): |
| partitions[p]["drive"] = DRIVES[p] |
|
|
| |
| while arg_idx < len(sys.argv)-1: |
| cmd = sys.argv[arg_idx] |
|
|
| |
| if cmd[:3] in DRIVES: |
| |
| dst, src = cmd.split("=",1) |
| dst = dst.replace("/", "\\") |
|
|
| if not import_item(partitions, src, dst): |
| sys.exit(-1) |
| else: |
| print("Error, unknown command", cmd) |
| sys.exit(-1) |
| |
| arg_idx += 1 |
|
|
| mk_csv(partitions) |
| |
| if not options["quiet"]: |
| |
| dump_trees(partitions) |
|
|
| |
| statistics(partitions) |
| |
| |
| if arg_idx == len(sys.argv)-1: |
| write_hddimage(sys.argv[-1], partitions, options) |
|
|