Skip to content

Instantly share code, notes, and snippets.

@mod-san
Created February 8, 2019 00:13
Show Gist options
  • Save mod-san/1d5784e8adf5c95633df5aa148d132ca to your computer and use it in GitHub Desktop.
Save mod-san/1d5784e8adf5c95633df5aa148d132ca to your computer and use it in GitHub Desktop.
Search for a specific text reference inside files of a whole tree of directories. [It has been used specifically to find strings in the files of the Shenmue games.]
def search(query, path = "**/*", output = "log.txt")
entry = query; entrySource = entry.source; #sndCase = entrySource.include?(".snd")
logfile = File.open(output, "w")
logfile << "== REREFERNCES OF \"#{entrySource}\" ==\n\n"
findsSize = 0; matchesOffsets = {}; filenames = []
Dir.glob(path) { |f|
( File.directory?(f) or f == File.basename(__FILE__) ) and next
string = IO.binread(f); stringSize = string.size
refsSize = 0; lastOffset = 0; matchesOffsets.clear
while lastOffset < stringSize
match = string.match(entry, lastOffset) or break
( (matchSize = match.size) > 1 ? 1 : 0 ).upto( (matchSize > 1 ? (matchSize-1) : 0) ) { |i|
lastOffset = match.end(i); matchString = match[i]
#sndCase and matchString.include?("%") and next
(matchesOffsets[matchString] ||= []) << match.begin(i).to_s(16)
refsSize += 1
}
end
refsSize > 0 or next
logfile << "#{f}\n => References: #{refsSize}\n => Matches (Offsets):\n"
matchesOffsets.each {|m, o| logfile << "\t\t\"#{m}\"\t(#{o.join(", ")})\n"}; logfile << "\n\n"
filenames << f
findsSize += refsSize
}
logfile << "====\nTOTAL FINDS: " << findsSize.to_s << "\n====\nALL FILE NAMES:\n"
filenames.each { |fn| logfile << "\t #{fn}" << "\n"}
end
search(/over already/)
#[^0-9A-Za-z_%]([0-9A-Za-z_%]+\.snd)
####
exit
exceptions = [:f, :string, :offsets]
# ...
if offsets[refsSize] == "dc598"
local_variables.each { |v|
exceptions.include?(v) or puts "#{v} = #{eval v.to_s}"
}
end
#refsSize = IO.binread(f).scan(entry).size; next if refsSize == 0
#puts refsSize
#(0...refsSize).each { |n| offsets << $~.offset(n)[0].to_s(16) }
#logfile << " => Offsets: #{offsets[0...refsSize].join(", ")}\n\n"
#; offsets << match.begin(0).to_s(16)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment