Skip to content

Commit

Permalink
Dev (#18)
Browse files Browse the repository at this point in the history
* Create LICENSE.md (#5) (#6)

* refactor (#7)

* Add readmes (#8)

* readmes

* Update README.md

* Update README.md

* Update README.md

* Update README.md

* Console app (#10)

* remove debug output

* add help and basic parameter support

* Add md5 check (#12)

* Update MMRando.py

checksum check

* checksum check

* Update README.md (#14)

* readme img (#15)

* Update README.md

* Update README.md

* Update README.md

* d (#16)

* Update README.md

* Update README.md

* Update README.md

* Update README.md

* add header support (#17)
  • Loading branch information
avvie authored Jan 28, 2023
1 parent 5516433 commit 81e167f
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 48 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ Randomizes what each boss drops at the end of the level
## How to use
- Clone the repository to your local machine
- Navigate to the project directory/Randomizer
- You should add your megaman nes image into the directory/Randomizer, with
the file header removed (this will be improved in at a later time)
- You should add your megaman nes image into the directory/Randomizer. We support the (USA) or (U) release header.
If it wont load, open an issue :)
- Run the app using `python3 MMRando.py`

### Rando current options
Expand Down
59 changes: 15 additions & 44 deletions Randomizer/MMRando.py
Original file line number Diff line number Diff line change
@@ -1,39 +1,17 @@
from ast import arg
from genericpath import isfile
from platform import architecture
from typing import final
from pickle import TRUE
from Generators.PaletteGenerator import PaletteGenerator
from Generators.WeaponGenerator import WeaponGenerator
from Utilities import *
import hashlib
import shutil
import sys
import os.path
from Offsets import *

# Get console arguments passed
args = sys.argv[1:]

input_file = "./Mega Man (USA).nes"
output_file = "./MMRando.nes"
supported_md5_input_checksums = ["4de82cfceadbf1a5e693b669b1221107"]

# Gets the path to a valid file passed in arguments, positionally after param
def GetValidFileFromParameter(paramList, param, default = None):
if param in paramList:
i = paramList.index(param)
if i+1 < len(paramList):
if os.path.exists(paramList[i+1]):
local = paramList[i+1]
paramList.remove(local)
paramList.remove(param)
print("return value:"+ local)
return local
return default

def ParamExistsInArgs(paramList, param):
if param in paramList:
return True
return False

if ParamExistsInArgs(args, '-h'):
PrintHelp()
Expand All @@ -42,14 +20,11 @@ def ParamExistsInArgs(paramList, param):
# Get input file path passed into the app, or default
input_file = GetValidFileFromParameter(args, '-i', input_file)
# Get output file path passed into the app, or default
output_file = GetValidFileFromParameter(args, '-o', output_file)

#please name this more concretely
Megaman_Default = [0x2c, 0x11]
output_file = GetValidFileFromParameter(args, '-o', output_file, False)

try:
# Check for checksum
if os.path.getsize(input_file) >= 150*1024:
if os.path.getsize(input_file) >= max_image_file_size:
print("File too big")
sys.exit(0)

Expand All @@ -60,12 +35,15 @@ def ParamExistsInArgs(paramList, param):
if checksum not in supported_md5_input_checksums:
print("Input file md5 checksum not supported " + checksum)
sys.exit(0)

# Make a copy of the original input file
filecopy = shutil.copyfile(input_file, output_file)

file = open("MMRando.nes", "r+b")


if GetHeader(input_file) in headers:
headerless_buffer = StripHeader(input_file)
WriteBuffer(output_file, headerless_buffer)
else:
# Assume headerless and whatever happens
shutil.copyfile(input_file, output_file)

file = open(output_file, "r+b")
GeneratorList = []
if not ParamExistsInArgs(args, '-w'):
GeneratorList.append(WeaponGenerator(file))
Expand All @@ -80,12 +58,5 @@ def ParamExistsInArgs(paramList, param):
finally:
file.close()


#add header for proper booting
header = bytes(b'\x4E\x45\x53\x1A\x08\x00\x21\x08\x20\x00\x00\x07\x00\x00\x00\x01')
with open("MMRando.nes",'r+b') as contents:
save = contents.read()
with open("MMRando.nes",'w+b') as contents:
contents.write(header)
with open("MMRando.nes",'a+b') as contents:
contents.write(save)
# Assume headerless and add header
AddHeader(headers[0], output_file)
5 changes: 4 additions & 1 deletion Randomizer/Offsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,7 @@
Gutsman_ClearPatch = 0x1B69E # Fixes a cmp call for checking if we beat gutsman

Megaman_Bossroom_PalPatch = [0x1C29B, 0x1C2A0] # Patches hard coded palette values
# First address is 2c, the second is 11
# First address is 2c, the second is 11

#please name this more concretely
Megaman_Default = [0x2c, 0x11]
52 changes: 51 additions & 1 deletion Randomizer/Utilities.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,57 @@
import os.path

def AddHeader(header, __output_file):
with open(__output_file,'r+b') as contents:
save = contents.read()
with open(__output_file,'w+b') as contents:
contents.write(header)
with open(__output_file,'a+b') as contents:
contents.write(save)

# Gets the path to a valid file passed in arguments, positionally after param
def GetValidFileFromParameter(paramList, param, default = None, check_existence = True):
if param in paramList:
i = paramList.index(param)
if i+1 < len(paramList):
if os.path.exists(paramList[i+1]) or not check_existence:
local = paramList[i+1]
return local
return default

def ParamExistsInArgs(paramList, param):
if param in paramList:
return True
return False

def PrintHelp():
print ("Help menu for MegamanRandomizer:\n\n" +
"-i [filePath]:\t Set a specific input file path. Default: ./Mega Man (USA).nes\n" +
"-o [filePath]:\t Set a specific output file path. Default: ./MMRando.nes\n" +
"\nRandomizer Options:\n" +
"-w:\t Do NOT randomize weapon drops. Default: Weapons WILL get randomized\n" +
"-p:\t Do NOT randomize pallete drops. Default: Palletes WILL get randomized\n")
"-p:\t Do NOT randomize pallete drops. Default: Palletes WILL get randomized\n")

supported_md5_input_checksums = ["4de82cfceadbf1a5e693b669b1221107", "4d4ffdfe7979b5f06dec2cf3563440ad"]

max_image_file_size = 150*1024

headers = [
bytes(b'\x4E\x45\x53\x1A\x08\x00\x21\x08\x20\x00\x00\x07\x00\x00\x00\x01'),
bytes(b'\x4E\x45\x53\x1A\x08\x00\x21\x00\x00\x00\x00\x00\x00\x00\x00\x00')
]

def GetHeader(__filename):
with open(__filename,'rb') as contents:
# Header is in the first 16 bytes
return contents.read(16)

def StripHeader(path):
with open(path,'rb') as contents:
buffer = contents.read()[16:]
return buffer

def WriteBuffer(path, buffer):
if os.path.exists(path):
os.remove(path)
with open(path,'+bw') as contents:
contents.write(buffer)

0 comments on commit 81e167f

Please sign in to comment.