def ipsPatch(self, ipsPatches): mergedIPS = IPS_Patch() for ips in ipsPatches: mergedIPS.append(ips) # generate records for ips from self data groupedData = {} startAddress = -1 prevAddress = -1 curData = [] for address in sorted(self.data): if address == prevAddress + 1: curData.append(self.data[address]) prevAddress = address else: if len(curData) > 0: groupedData[startAddress] = curData startAddress = address prevAddress = address curData = [self.data[startAddress]] if startAddress != -1: groupedData[startAddress] = curData patch = IPS_Patch(groupedData) mergedIPS.append(patch) patchData = mergedIPS.encode() self.data = {} self.data["ips"] = base64.b64encode(patchData).decode() if mergedIPS.truncate_length is not None: self.data["truncate_length"] = mergedIPS.truncate_length self.data["max_size"] = mergedIPS.max_size
def handleIps(self, plandoName, romDataJson): romDataJson = self.vars.romData romDataRaw = json.loads(romDataJson) # everything is string in json, cast to int romData = {} for addr in romDataRaw: romData[int(addr)] = int(romDataRaw[addr]) # dict: address -> value, transform it dict: address -> [values] ipsData = {} prevAddr = -0xff curRecord = [] curRecordAddr = -1 for addr in sorted(romData): if addr == prevAddr + 1: curRecord.append(romData[addr]) else: if len(curRecord) > 0: # save current record ipsData[curRecordAddr] = bytearray(curRecord) # start a new one curRecordAddr = addr curRecord = [romData[addr]] prevAddr = addr # save last record ipsData[curRecordAddr] = bytearray(curRecord) # generate ips using the records ipsPatch = IPS_Patch(ipsData) maxSize = ipsPatch.max_size # store ips in the repository ipsPatch.save(os.path.join(ipsBasePath, "{}.ips".format(plandoName))) return maxSize
def close(self): mergedIPS = IPS_Patch() for ips in self.ipsPatches: mergedIPS.append(ips) mergedIPS.append(self.ips()) patchData = mergedIPS.encode() self.data = {} self.data["ips"] = base64.b64encode(patchData).decode() if mergedIPS.truncate_length is not None: self.data["truncate_length"] = mergedIPS.truncate_length self.data["max_size"] = mergedIPS.max_size
def applyIPSPatch(self, patchName, patchDict=None, ipsDir="rando/patches"): if patchDict is None: patchDict = patches print("Apply patch {}".format(patchName)) if patchName in patchDict: patch = IPS_Patch(patchDict[patchName]) else: # look for ips file if os.path.exists(patchName): patch = IPS_Patch.load(patchName) else: patch = IPS_Patch.load(os.path.join(appDir, ipsDir, patchName)) self.ipsPatches.append(patch)
def applyIPSPatch(self, patchName, patchDict=None, ipsDir=None): if patchDict is None: patchDict = self.patchAccess.getDictPatches() print("Apply patch {}".format(patchName)) if patchName in patchDict: patch = IPS_Patch(patchDict[patchName]) else: # look for ips file if ipsDir is None: patch = IPS_Patch.load(self.patchAccess.getPatchPath(patchName)) else: patch = IPS_Patch.load(os.path.join(appDir, ipsDir, patchName)) self.ipsPatches.append(patch)
def ips(self): groupedData = {} startAddress = -1 prevAddress = -1 curData = [] for address in sorted(self.data): if address == prevAddress + 1: curData.append(self.data[address]) prevAddress = address else: if len(curData) > 0: groupedData[startAddress] = curData startAddress = address prevAddress = address curData = [self.data[startAddress]] if startAddress != -1: groupedData[startAddress] = curData return IPS_Patch(groupedData)
#!/usr/bin/python3 import sys, os, json # we're in directory 'tools/' we have to update sys.path sys.path.append(os.path.dirname(sys.path[0])) from rom.ips import IPS_Patch elev = IPS_Patch.load("patches/common/ips/elevators_speed.ips") doors = IPS_Patch.load("patches/common/ips/fast_doors.ips") merged = IPS_Patch() merged.append(elev) merged.append(doors) merged.save("patches/common/ips/elevators_doors_speed.ips")
def loadPatchPy(): from rando.patches import patches as patches_py for name, patch in patches_py.items(): addRanges(name, IPS_Patch(patch))
vanilla = sys.argv[1] ipsToReverse = sys.argv[2:] rom = RealROM(vanilla) for ips in ipsToReverse: if ips not in common_patches.patches: ipsPath = os.path.abspath(ips) destDir, destFile = os.path.split(ipsPath) patch = IPS_Patch.load(ips) patchDict = patch.toDict() else: patchDict = common_patches.patches[ips] destDir = sys.path[0] + "/../patches/common/ips" destFile = ips + ".ips" destFile = "remove_" + destFile destPath = os.path.join(destDir, destFile) reversePatchDict = {} for addr, bytez in patchDict.items(): sz = len(bytez) origBytes = [] rom.seek(addr) for i in range(sz): origBytes.append(rom.readByte()) assert len(origBytes) == sz reversePatchDict[addr] = origBytes reversePatch = IPS_Patch(reversePatchDict) reversePatch.save(destPath) rom.close()
def applyIPSPatchDict(self, patchDict): for patchName in patchDict.keys(): # print("Apply patch {}".format(patchName)) patch = IPS_Patch(patchDict[patchName]) self.ipsPatches.append(patch)
patch = IPS_Patch.load(ips) patch_dict = patch.toDict() # remove overlapping data filtered_dict = {} for keyLow, data in patch_dict.items(): dataLength = len(data) keyHigh = keyLow + dataLength if keyHigh < low or keyLow > high: print("[{}, {}] we have {} bytes of data".format(hex(pc_to_snes(keyLow)), hex(pc_to_snes(keyHigh)), dataLength)) filtered_dict[keyLow] = data else: print("[{}, {}] we have {} bytes of data overlaping with [{}, {}]".format(hex(pc_to_snes(keyLow)), hex(pc_to_snes(keyHigh)), dataLength, hex(pc_to_snes(low)), hex(pc_to_snes(high)))) if len(filtered_dict.keys()) < len(patch_dict.keys()): # write ips split = os.path.split(ips) out = os.path.join(split[0], 'out_'+split[1]) out_patch = IPS_Patch(filtered_dict) out_patch.save(out) print("filtered ips generated: {}".format(out)) else: print("no overlapping record found")