#!/usr/bin/ruby # http://metasm.cr0.org/ require 'metasm' include Metasm if ARGV.length != 2 abort 'usage: m_findcursoroffset , with the current value of the cursor coords (hint: use 1 1 and 2 2) - topleft = 0 0' end dfp = OS.current.find_process('Dwarf_Fortress') || OS.current.find_process('dwarfort') abort 'no running df found' if not dfp dbg = dfp.debugger file = 'caret_offset.tmp' x, y = ARGV.shift.to_i, ARGV.shift.to_i bin = [x, y].pack('l*') if not File.exist? file puts "full memory scan, could take a while..." offs = dbg.pattern_scan(bin) else offs = File.read(file).split.map { |o| o.to_i(16) } offs.delete_if { |o| dbg[o, bin.length] != bin } end File.open(file, 'w') { |fd| fd.puts offs.map { |o| o.to_s(16) } } case offs.length when 0 File.unlink(file) puts "no offset found :(", "I removed the cache file, try again" when 1 base = ((offs.first > 0x8000000) ? 0x8048000 : 0x400000) puts "found cursor offset: #{'%x' % offs.first} (=#{'%x' % base}+#{'%x' % (offs.first - base)})" puts "cursor value: #{dbg[offs.first, 4*3].unpack('l*').inspect}" else puts "found #{offs.length} candidates, try again after moving the cursor" end