User File #45899570601133311

Upload All User Files

#45899570601133311 - Search Diff or Same Memory Values in JPC-rr

searchdiffmemory.lua
Game: Unknown Game ( NES, see all files )
757 downloads
Uploaded 3/21/2018 1:59 AM by c-square (see all 10)
A lua script for finding values that have changed or stayed the same
--inputFilename="outputfile.out" -- Uncomment for searches after the first one
outputFilename="outputfile.out" -- Rename output file for subseqent searches
searchLevel="word" -- word for > 256 or byte for <= 256
searchType="diff" -- diff or same

function string:split( inSplitPattern, outResults )
  numSplit=0
  if not outResults then
    outResults = { }
  end
  local theStart = 1
  local theSplitStart, theSplitEnd = string.find( self, inSplitPattern, theStart )
  while theSplitStart do
    table.insert( outResults, string.sub( self, theStart, theSplitStart-1 ) )
    theStart = theSplitEnd + 1
    theSplitStart, theSplitEnd = string.find( self, inSplitPattern, theStart )
    numSplit=numSplit+1
    if numSplit % 10000 == 0 then
  	print(numSplit)
    end
  end
  table.insert( outResults, string.sub( self, theStart ) )
  return outResults
end

function searchFor(byteList, value, filename)
	print("Searching " .. #byteList .. " values for " .. searchType);
	local input=1
	local output=0
	print("Opening Output File")
	outputBinary=io.open_write(filename)
	print("Output File Open")
	while byteList[input] do
		searchStr = byteList[input]..""
		oldValue=nil
		pipeLoc = string.find(searchStr, "|")
		if pipeLoc then
			searchAddr = tonumber(string.sub(searchStr,1,pipeLoc-1))
			oldValue = tonumber(string.sub(searchStr,pipeLoc+1))
		else
			searchAddr = byteList[input]
		end
		if searchLevel=="word" then
			memValue = jpcrr.read_word(searchAddr)
		else
			memValue = jpcrr.read_byte(searchAddr)
		end
		if memValue and (oldValue == nil or (searchType == "diff" and memValue ~= oldValue) or (searchType == "same" and memValue == oldValue)) then
			outputBinary.write("?",searchAddr.."|"..memValue.."\n")
			output = output + 1
		end
		if input % 10000 == 0 then
			print(input)
		end
		input=input+1
	end
	print("Search Complete!")
	print("Found " .. output .. " " .. searchType)
	outputBinary:close()
end

-- Main
if inputFilename then
	print("Opening input file")
	inputBinary, error=io.open_read(inputFilename)
	numRead=0
	searchStr = inputBinary.read()
	inputBinary:close()
	print("Splitting Input")
	searchList={}
	for line in searchStr:gmatch("([^\n]*)\n?") do
		if line and string.len(line) > 0 then
			searchList[#searchList+1] = line
		end
		numRead=numRead+1
   	        if numRead % 10000 == 0 then
			print(numRead)
		end
	end
	print("Input split into " .. #searchList .. " items")
end
if not searchList then
	searchList={}
	if searchLevel=="word" then
		step = 2
	else
		step = 1
	end
	x=1
	for i=0,2090000,step do
		searchList[x]=i
		x=x+1
	end
end
searchFor(searchList, value, outputFilename)