Skip to content

Commit

Permalink
Add UniqueID collision checking
Browse files Browse the repository at this point in the history
  • Loading branch information
lifehackerhansol committed Feb 20, 2022
1 parent c868222 commit 460e778
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 8 deletions.
51 changes: 45 additions & 6 deletions generator/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,46 @@
def fail(error):
return error

def collisioncheck(path) -> list:
tidlow = []
root = None
id0 = None
id1 = None
if os.name == 'nt':
root = os.path.abspath(path)[:2]
else:
temp = path
orig_dev = os.stat(temp).st_dev
while temp != '/':
direc = os.path.dirname(temp)
if os.stat(direc).st_dev != orig_dev:
break
temp = direc
root = temp
if not os.path.isdir(f"{root}/Nintendo 3DS"):
return "Failed to find Nintendo 3DS folder. Is the ROM on the SD card?"
if (len([folder for folder in os.listdir(f"{root}/Nintendo 3DS") if os.path.isdir(f"{root}/Nintendo 3DS/{folder}")])) != 2:
return "More than one ID0 folder detected. Please remove unnecessary ID0 folders before continuing."
for name in os.listdir(f"{root}/Nintendo 3DS"):
if len(name) == 32:
id0 = name
break
if id0 is None:
return "ID0 not found. Is this ROM on the SD card?"
if (len([folder for folder in os.listdir(f"{root}/Nintendo 3DS/{id0}") if os.path.isdir(f"{root}/Nintendo 3DS/{id0}/{folder}")])) != 1:
return "More than one ID1 folder detected. Please remove unnecessary ID1 folders before continuing."
for name in os.listdir(f"{root}/Nintendo 3DS/{id0}"):
if len(name) == 32:
id1 = name
break
if id1 is None:
return "ID1 not found. Is this ROM on the SD card?"
for name in os.listdir(f"{root}/Nintendo 3DS/{id0}/{id1}/title/00040000"):
tidlow.append(name)
for index, value in enumerate(tidlow):
tidlow[index] = value[1:-2]
return tidlow

def get_title(path) -> dict:
# get banner title
rom = open(path, "rb")
Expand Down Expand Up @@ -217,24 +257,23 @@ def makeromfs(path):
romfs.close()
return 0

def makecia(cmdarg, path, title, output=None, randomize=False):
def makecia(cmdarg, path, title, output=None, randomize=False, tidlow=[]):
gamecode = getgamecode(path)
uniqueid = None
if randomize:
uniqueid = hex(random.randint(0x300, 0xF7FFF))
else:
gamecodeint = int(hexlify(gamecode.encode()).decode(), 16)
uniqueid = f"0x{hex(gamecodeint ^ ((gamecodeint) >> 27))[3:8]}"
uniqueid = hex(gamecodeint ^ ((gamecodeint) >> 27))[3:8]
while uniqueid in tidlow:
uniqueid = str(int(uniqueid) + 1)
makeromarg = f"{cmdarg}makerom -f cia -target t -exefslogo -rsf data/build-cia.rsf -elf data/forwarder.elf -banner data/banner.bin -icon data/output.smdh -DAPP_ROMFS=romfs -major 1 -minor 3 -micro 0 -DAPP_VERSION_MAJOR=1 "
if output:
makeromarg += f'-o "{output}" '
else:
makeromarg += '-o "output.cia" '
makeromarg += f'-DAPP_PRODUCT_CODE=CTR-H-{gamecode} -DAPP_TITLE="{title["eng"][0]}" -DAPP_UNIQUE_ID={uniqueid}'
makeromarg += f'-DAPP_PRODUCT_CODE=CTR-H-{gamecode} -DAPP_TITLE="{title["eng"][0]}" -DAPP_UNIQUE_ID=0x{uniqueid}'
makeromrun = subprocess.run(makeromarg, shell=True, capture_output=True, universal_newlines=True)
if makeromrun.returncode != 0:
return f"{makeromrun.stdout}\n{makeromrun.stderr}"
return "CIA generated."



7 changes: 5 additions & 2 deletions generator/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
import core

def execute(error):
if isinstance(error, int):
if isinstance(error, int) or isinstance(error, list):
return
else:
print(error)
Expand All @@ -56,6 +56,7 @@ def execute(error):
boxart = None
output = None
randomize = False
tidlow = None
if args.boxart:
boxart = args.boxart[0]
if args.input:
Expand All @@ -69,6 +70,8 @@ def execute(error):
cmdarg = ""
if os.name != 'nt':
cmdarg = "./"
tidlow = core.collisioncheck(path)
execute(tidlow)
print("Extracting and resizing icon...")
core.makeicon(path)
print("Getting ROM titles...")
Expand All @@ -82,4 +85,4 @@ def execute(error):
print("Getting filepath...")
execute(core.makeromfs(path))
print("Running makerom...")
execute(core.makecia(cmdarg, path, title, output, randomize))
execute(core.makecia(cmdarg, path, title, output, randomize, tidlow))

0 comments on commit 460e778

Please sign in to comment.