Fix Merge Conflicts

This commit is contained in:
Skeli
2019-08-25 03:45:53 -04:00
parent 993b3ce2bd
commit 745de0d9fa
3 changed files with 1714 additions and 1527 deletions

View File

@@ -1,94 +1,119 @@
#!/usr/bin/env python3
import shutil
import sys
import os
import shutil
############
#Options go here.
############
ROM_NAME = "Pokemon Unbound.gba" #The name of your rom
OFFSET_TO_PUT = 0x133bed0
SEARCH_FREE_SPACE = False #Set to True if you want the script to search for free space;Set to False if you don't want to search for free space as you for example update the engine
ROM_NAME = "BPRE0.gba" #The name of your rom
OFFSET_TO_PUT = 0x1800000
SEARCH_FREE_SPACE = False # Set to True if you want the script to search for free space
# Set to False if you don't want to search for free space as you for example update the engine
#############
#Options end here.
# Options end here.
#############
###############
#Functions start here.
# Functions start here.
###############
def align_x100(offset):
mod_x100 = offset % 0x100
if mod_x100 != 0x0: #not aligned properly
offset += (0x100 - mod_x100)
return offset
def find_offset_to_put(rom, needed_bytes, start_loc):
offset = start_loc
rom.seek(0, 2)
max_pos = rom.tell()
found_bytes = 0
while found_bytes < needed_bytes:
if offset + found_bytes >= max_pos:
print("End of file reached. Not enough free space.")
return 0
found_bytes += 1
rom.seek(offset + found_bytes)
if rom.read(1) != b'\xFF':
offset = align_x100(offset + found_bytes)
found_bytes = 0
return offset
def file_change_line(file_path, line_to_change, replacement):
file = open(file_path, 'r')
copy = file.read()
file.seek(0x0)
line_no = 1
for line in file:
if (line_no == line_to_change):
copy = copy.replace(line, replacement)
break
line_no += 1
file.close()
file = open(file_path, 'w')
file.write(copy)
file.close()
def MakeOffset0x100Aligned(offset: int) -> int:
while offset % 16 != 0:
offset += 1
def edit_linker(offset):
file_change_line("linker.ld", 4, "\t\trom : ORIGIN = (0x08000000 + " + hex(offset) + "), LENGTH = 32M\n")
def edit_insert(offset):
file_change_line("./scripts/insert.py", 11, "OFFSET_TO_PUT = " + hex(offset) + '\n')
file_change_line("./scripts/insert.py", 12, 'SOURCE_ROM = "' + ROM_NAME + '"\n')
def build_code():
os.system("python scripts/build.py")
def insert_code():
os.system("python scripts/insert.py")
def clear_from_to(rom, from_, to_):
rom.seek(from_)
for i in range(0, to_ - from_):
rom.write(b'\xFF')
return offset
def FindOffsetToPut(rom, neededBytes: int, startOffset: int) -> int:
offset = startOffset
rom.seek(0, 2)
maxPosition = rom.tell()
numFoundBytes = 0
while numFoundBytes < neededBytes:
if offset + numFoundBytes >= maxPosition:
print("End of file reached. Not enough free space.")
return 0
numFoundBytes += 1
rom.seek(offset + numFoundBytes)
if rom.read(1) != b'\xFF':
offset = MakeOffset0x100Aligned(offset + numFoundBytes)
numFoundBytes = 0
return offset
def ChangeFileLine(filePath: str, lineToChange: int, replacement: str):
with open(filePath, 'r') as file:
copy = file.read()
file.seek(0x0)
lineNum = 1
for line in file:
if lineNum == lineToChange:
copy = copy.replace(line, replacement)
break
lineNum += 1
with open(filePath, 'w') as file:
file.write(copy)
def EditLinker(offset: int):
ChangeFileLine("linker.ld", 4, "\t\trom : ORIGIN = (0x08000000 + " + hex(offset) + "), LENGTH = 32M\n")
def EditInsert(offset: int):
ChangeFileLine("./scripts/insert.py", 10, "OFFSET_TO_PUT = " + hex(offset) + '\n')
ChangeFileLine("./scripts/insert.py", 11, 'SOURCE_ROM = "' + ROM_NAME + '"\n')
def BuildCode():
if shutil.which('python3') is not None:
os.system("python3 scripts/build.py")
else:
os.system("python scripts/build.py")
def InsertCode():
if shutil.which('python3') is not None:
os.system("python3 scripts/insert.py")
else:
os.system("python scripts/insert.py")
def ClearFromTo(rom, from_: int, to_: int):
rom.seek(from_)
for i in range(0, to_ - from_):
rom.write(b'\xFF')
##############
#Functions end here.
# Functions end here.
##############
try:
with open(ROM_NAME, 'rb+') as rom:
offset = OFFSET_TO_PUT
if SEARCH_FREE_SPACE == True:
offset = find_offset_to_put(rom, 0x50000, align_x100(offset))
edit_linker(offset)
edit_insert(offset)
build_code()
insert_code()
rom.close()
except:
print('Error: Could not find source rom: "' + ROM_NAME + '".\nPlease make sure a rom with this name exists in the root.')
def main():
try:
with open(ROM_NAME, 'rb+') as rom:
offset = OFFSET_TO_PUT
if SEARCH_FREE_SPACE is True:
offset = FindOffsetToPut(rom, 0x50000, MakeOffset0x100Aligned(offset))
EditLinker(offset)
EditInsert(offset)
BuildCode()
InsertCode()
rom.close()
except:
print('Error: Could not find source rom: "' + ROM_NAME + '".\n'
+ 'Please make sure a rom with this name exists in the root.')
if __name__ == '__main__':
main()