Skip to content

Commit

Permalink
Added Code
Browse files Browse the repository at this point in the history
  • Loading branch information
DodoBirby committed Jun 20, 2022
1 parent 79bd833 commit a81fc25
Show file tree
Hide file tree
Showing 8 changed files with 549 additions and 0 deletions.
119 changes: 119 additions & 0 deletions GUI.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
from tkinter import *
from tkinter import filedialog as fd
import Randomiser
import Patcher
root = Tk()
root.title("MTTNE Randomiser")
root.geometry("720x360")
root.config(background="#1e1e1e")

class Error(Exception):
pass

class itemError(Error):
pass

class fileError(Error):
pass

def selectFile(arg):
filetypes = (('Data files', '*.dat'), ('All files', '*.*'))

if arg == "in":
filename = fd.askopenfilename(title='Select a Decoded map file', filetypes=filetypes)
e.delete(0, END)
e.insert(0, filename)
elif arg == "out":
filename = fd.asksaveasfilename(title='Choose Output Directory', defaultextension=".dat", filetypes=filetypes)
o.delete(0, END)
o.insert(0, filename)

def generateGame():
try:
infile = e.get()
outfile = o.get()
if infile == "" or outfile == "":
raise fileError
if seedEntry.get() != "":
seed = int(seedEntry.get())
else:
seed = ""
missiles = int(MissEntry.get())
supers = int(SupEntry.get())
powerBombs = int(PBEntry.get())
magShields = int(ShieldEntry.get())
eTanks = int(ETankEntry.get())
if missiles + supers + powerBombs + magShields + eTanks > 110:
raise itemError
itemDict = Randomiser.randomiser(seed, missiles, powerBombs, supers, magShields, eTanks)
Patcher.patch(infile, outfile, itemDict)
except ValueError:
pass
except itemError:
pass
except fileError:
pass

def updateItems():
try:
missiles = int(MissEntry.get())
supers = int(SupEntry.get())
powerBombs = int(PBEntry.get())
magShields = int(ShieldEntry.get())
eTanks = int(ETankEntry.get())
count = missiles + supers + powerBombs + magShields + eTanks
except ValueError:
count = "N/A"
Itemcount.config(text=str(count) + "/110")
root.after(100, updateItems)


seedEntry = Entry(root)
seedLabel = Label(root, text="Input Seed (Optional): ", fg="white",bg="#1e1e1e")
e = Entry(root)
o = Entry(root)
inbutton = Button(root, text="Choose Decoded Map File", width="20",command=lambda: selectFile("in"))
outButton = Button(root, text="Choose Output File", width="20",command=lambda: selectFile("out"))
patchButton = Button(root, text="Patch", width="20",command=generateGame)

MissLabel = Label(root, text="Missiles:", fg="white", bg="#1e1e1e")
SupLabel = Label(root, text="Super Missiles:", fg="white", bg="#1e1e1e")
PBLabel = Label(root, text="Power Bombs:",fg="white",bg="#1e1e1e")
ShieldLabel = Label(root, text="Shields:",fg="white",bg="#1e1e1e")
ETankLabel = Label(root, text="E-Tanks:",fg="white",bg="#1e1e1e")
Itemcount = Label(root, text="0/110", fg="white", bg="#1e1e1e")

MissEntry = Entry(root)
MissEntry.insert(0, "46")
SupEntry = Entry(root)
SupEntry.insert(0, "20")
PBEntry = Entry(root)
PBEntry.insert(0, "16")
ShieldEntry = Entry(root)
ShieldEntry.insert(0, "14")
ETankEntry = Entry(root)
ETankEntry.insert(0, "14")


seedEntry.grid(column=1, row=2, pady="5")
seedLabel.grid(column=0, row=2, padx="5")
e.grid(column=1, row=0, padx="10")
o.grid(column=1,row=1, pady="5")
inbutton.grid(column=0, row=0)
outButton.grid(column=0,row=1)
patchButton.grid(column=0,row=3)
MissLabel.grid(column=2, row=0, padx="10")
SupLabel.grid(column=2, row=1, padx="10")
PBLabel.grid(column=2, row=2, padx="10")
ShieldLabel.grid(column=2, row=3, padx="10")
ETankLabel.grid(column=2, row=4, padx="10")
Itemcount.grid(column=2, row=5, padx="10")
MissEntry.grid(column=3, row=0, pady="5")
SupEntry.grid(column=3, row=1, pady="5")
PBEntry.grid(column=3, row=2, pady="5")
ShieldEntry.grid(column=3, row=3, pady="5")
ETankEntry.grid(column=3, row=4, pady="5")


updateItems()
root.mainloop()
69 changes: 69 additions & 0 deletions Patcher.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import shutil
import os

half_n = 2**31
n = half_n * 2
def normalise(value):
return (value + half_n) % n - half_n

def patch(infile, outfile, itemDict):
workingDir = os.getcwd()
tempfile = shutil.copy(infile, workingDir + "/temp.dat")
dictList = list(itemDict)
with open(tempfile, "rb+") as f:
for i in dictList:
f.seek(i)
value = int.to_bytes(itemDict[i], 1, 'little')
f.write(value)


#Encoding code, basically assembler
counter = 0
mult = 84
exp = 225

with open(tempfile, "rb") as f:

size = os.path.getsize(tempfile)
if os.path.exists(outfile):
os.remove(outfile)
output = open(outfile, "ab")
while counter < size:
char = f.read(1)
char = int.from_bytes(char, 'little')
counter += 1
ecx = mult
eax = 0x66666667
edx = eax * ecx >> 32
edx = edx // 2
eax = ecx
eax = eax // 2**31
ebx = edx
ebx -= eax
ecx = exp
eax = 0x4bda12f7
edx = eax * ecx >> 32
edx = edx // 2**3
eax = ecx
eax = eax // 2**31
edx -= eax
eax = edx
dl = ebx % 256
dl -= eax % 256
signextended = char
if char >= 0x80:
signextended = 0xffffff << 8
signextended += char
signextended = int.to_bytes(signextended, 4, 'little')
signextended = int.from_bytes(signextended, 'little', signed=True)
eax = char - dl
storage = eax % 256
mult += signextended
eax = (signextended + 7) * counter
exp += eax
exp = normalise(exp)
output.write(int.to_bytes(storage, 1, 'little'))
output.close()
os.remove(tempfile)


Loading

0 comments on commit a81fc25

Please sign in to comment.