2012-04-19 17:29:52 -06:00
|
|
|
module DFHack
|
|
|
|
module MemHack
|
2012-05-05 16:49:23 -06:00
|
|
|
INSPECT_SIZE_LIMIT=16384
|
2012-04-06 11:30:58 -06:00
|
|
|
class MemStruct
|
|
|
|
attr_accessor :_memaddr
|
2012-04-20 10:20:24 -06:00
|
|
|
def _at(addr) ; d = dup ; d._memaddr = addr ; d ; end
|
2012-04-06 11:30:58 -06:00
|
|
|
def _get ; self ; end
|
2012-04-26 06:16:47 -06:00
|
|
|
def _cpp_init ; end
|
2012-04-06 11:30:58 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
class Compound < MemStruct
|
|
|
|
class << self
|
2012-04-21 07:04:52 -06:00
|
|
|
attr_accessor :_fields, :_rtti_classname, :_sizeof
|
2012-04-06 11:30:58 -06:00
|
|
|
def field(name, offset)
|
|
|
|
struct = yield
|
2012-05-05 16:49:23 -06:00
|
|
|
return if not struct
|
2012-04-06 11:30:58 -06:00
|
|
|
@_fields ||= []
|
|
|
|
@_fields << [name, offset, struct]
|
|
|
|
define_method(name) { struct._at(@_memaddr+offset)._get }
|
|
|
|
define_method("#{name}=") { |v| struct._at(@_memaddr+offset)._set(v) }
|
|
|
|
end
|
2012-04-25 11:22:04 -06:00
|
|
|
def _fields_ancestors
|
|
|
|
if superclass.respond_to?(:_fields_ancestors)
|
|
|
|
superclass._fields_ancestors + _fields.to_a
|
|
|
|
else
|
|
|
|
_fields.to_a
|
|
|
|
end
|
|
|
|
end
|
2012-04-06 11:30:58 -06:00
|
|
|
|
2012-04-26 12:55:52 -06:00
|
|
|
def number(bits, signed, initvalue=nil, enum=nil)
|
|
|
|
Number.new(bits, signed, initvalue, enum)
|
2012-04-06 11:30:58 -06:00
|
|
|
end
|
|
|
|
def float
|
|
|
|
Float.new
|
|
|
|
end
|
|
|
|
def bit(shift)
|
|
|
|
BitField.new(shift, 1)
|
|
|
|
end
|
2012-04-26 12:55:52 -06:00
|
|
|
def bits(shift, len, enum=nil)
|
|
|
|
BitField.new(shift, len, enum)
|
2012-04-06 11:30:58 -06:00
|
|
|
end
|
2012-04-19 17:29:52 -06:00
|
|
|
def pointer
|
|
|
|
Pointer.new((yield if block_given?))
|
|
|
|
end
|
|
|
|
def pointer_ary(tglen)
|
|
|
|
PointerAry.new(tglen, yield)
|
2012-04-06 11:30:58 -06:00
|
|
|
end
|
2012-04-25 09:18:24 -06:00
|
|
|
def static_array(len, tglen, indexenum=nil)
|
|
|
|
StaticArray.new(tglen, len, indexenum, yield)
|
2012-04-06 11:30:58 -06:00
|
|
|
end
|
|
|
|
def static_string(len)
|
|
|
|
StaticString.new(len)
|
|
|
|
end
|
|
|
|
|
|
|
|
def stl_vector(tglen=nil)
|
2012-04-13 08:17:56 -06:00
|
|
|
tg = yield if tglen
|
|
|
|
case tglen
|
|
|
|
when 1; StlVector8.new(tg)
|
|
|
|
when 2; StlVector16.new(tg)
|
|
|
|
else StlVector32.new(tg)
|
|
|
|
end
|
2012-04-06 11:30:58 -06:00
|
|
|
end
|
|
|
|
def stl_string
|
|
|
|
StlString.new
|
|
|
|
end
|
|
|
|
def stl_bit_vector
|
|
|
|
StlBitVector.new
|
|
|
|
end
|
2012-04-21 07:04:52 -06:00
|
|
|
def stl_deque(tglen)
|
|
|
|
StlDeque.new(tglen, yield)
|
2012-04-06 11:30:58 -06:00
|
|
|
end
|
|
|
|
|
2012-04-25 09:18:24 -06:00
|
|
|
def df_flagarray(indexenum=nil)
|
|
|
|
DfFlagarray.new(indexenum)
|
2012-04-06 11:30:58 -06:00
|
|
|
end
|
2012-04-21 07:04:52 -06:00
|
|
|
def df_array(tglen)
|
|
|
|
DfArray.new(tglen, yield)
|
2012-04-06 11:30:58 -06:00
|
|
|
end
|
|
|
|
def df_linked_list
|
2012-04-20 10:20:24 -06:00
|
|
|
DfLinkedList.new(yield)
|
2012-04-06 11:30:58 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
def global(glob)
|
|
|
|
Global.new(glob)
|
|
|
|
end
|
2012-04-27 09:59:54 -06:00
|
|
|
def compound(name=nil, &b)
|
2012-04-06 11:30:58 -06:00
|
|
|
m = Class.new(Compound)
|
2012-04-27 09:59:54 -06:00
|
|
|
DFHack.const_set(name, m) if name
|
2012-04-06 11:30:58 -06:00
|
|
|
m.instance_eval(&b)
|
2012-04-13 08:17:56 -06:00
|
|
|
m.new
|
2012-04-06 11:30:58 -06:00
|
|
|
end
|
2012-04-18 10:46:33 -06:00
|
|
|
def rtti_classname(n)
|
|
|
|
DFHack.rtti_register(n, self)
|
2012-04-21 07:04:52 -06:00
|
|
|
@_rtti_classname = n
|
|
|
|
end
|
|
|
|
def sizeof(n)
|
|
|
|
@_sizeof = n
|
2012-04-18 10:46:33 -06:00
|
|
|
end
|
2012-04-25 11:22:04 -06:00
|
|
|
|
|
|
|
# allocate a new c++ object, return its ruby wrapper
|
|
|
|
def cpp_new
|
|
|
|
ptr = DFHack.malloc(_sizeof)
|
|
|
|
if _rtti_classname and vt = DFHack.rtti_getvtable(_rtti_classname)
|
|
|
|
DFHack.memory_write_int32(ptr, vt)
|
|
|
|
# TODO call constructor
|
|
|
|
end
|
2012-04-26 06:16:47 -06:00
|
|
|
o = new._at(ptr)
|
|
|
|
o._cpp_init
|
|
|
|
o
|
2012-04-25 11:22:04 -06:00
|
|
|
end
|
|
|
|
end
|
|
|
|
def _cpp_init
|
2012-04-26 06:16:47 -06:00
|
|
|
_fields_ancestors.each { |n, o, s| s._at(@_memaddr+o)._cpp_init }
|
2012-04-06 11:30:58 -06:00
|
|
|
end
|
2012-04-27 10:50:27 -06:00
|
|
|
def _set(h)
|
|
|
|
case h
|
|
|
|
when Hash; h.each { |k, v| send("_#{k}=", v) }
|
2012-05-02 19:33:50 -06:00
|
|
|
when Array; names = _field_names ; raise 'bad size' if names.length != h.length ; names.zip(h).each { |n, a| send("#{n}=", a) }
|
2012-05-02 18:52:53 -06:00
|
|
|
when Compound; _field_names.each { |n| send("#{n}=", h.send(n)) }
|
2012-04-27 10:50:27 -06:00
|
|
|
else raise 'wut?'
|
|
|
|
end
|
|
|
|
end
|
2012-04-20 07:30:15 -06:00
|
|
|
def _fields ; self.class._fields.to_a ; end
|
2012-04-25 11:22:04 -06:00
|
|
|
def _fields_ancestors ; self.class._fields_ancestors.to_a ; end
|
2012-05-02 18:52:53 -06:00
|
|
|
def _field_names ; _fields_ancestors.map { |n, o, s| n } ; end
|
2012-04-21 07:04:52 -06:00
|
|
|
def _rtti_classname ; self.class._rtti_classname ; end
|
|
|
|
def _sizeof ; self.class._sizeof ; end
|
2012-05-05 14:43:12 -06:00
|
|
|
@@inspecting = {} # avoid infinite recursion on mutually-referenced objects
|
2012-04-20 07:30:15 -06:00
|
|
|
def inspect
|
|
|
|
cn = self.class.name.sub(/^DFHack::/, '')
|
|
|
|
cn << ' @' << ('0x%X' % _memaddr) if cn != ''
|
|
|
|
out = "#<#{cn}"
|
2012-05-05 14:43:12 -06:00
|
|
|
return out << ' ...>' if @@inspecting[_memaddr]
|
|
|
|
@@inspecting[_memaddr] = true
|
2012-04-25 11:22:04 -06:00
|
|
|
_fields_ancestors.each { |n, o, s|
|
2012-04-20 07:30:15 -06:00
|
|
|
out << ' ' if out.length != 0 and out[-1, 1] != ' '
|
2012-04-27 09:11:01 -06:00
|
|
|
if out.length > INSPECT_SIZE_LIMIT
|
2012-04-20 07:30:15 -06:00
|
|
|
out << '...'
|
|
|
|
break
|
|
|
|
end
|
|
|
|
out << inspect_field(n, o, s)
|
|
|
|
}
|
2012-04-27 09:38:31 -06:00
|
|
|
out.chomp!(' ')
|
2012-05-05 14:43:12 -06:00
|
|
|
@@inspecting.delete _memaddr
|
2012-04-20 07:30:15 -06:00
|
|
|
out << '>'
|
|
|
|
end
|
|
|
|
def inspect_field(n, o, s)
|
|
|
|
if s.kind_of?(BitField) and s._len == 1
|
|
|
|
send(n) ? n.to_s : ''
|
2012-04-27 09:11:01 -06:00
|
|
|
elsif s.kind_of?(Pointer)
|
2012-04-27 09:38:31 -06:00
|
|
|
"#{n}=#{s._at(@_memaddr+o).inspect}"
|
2012-04-20 07:30:15 -06:00
|
|
|
elsif n == :_whole
|
|
|
|
"_whole=0x#{_whole.to_s(16)}"
|
|
|
|
else
|
|
|
|
v = send(n).inspect
|
|
|
|
"#{n}=#{v}"
|
|
|
|
end
|
|
|
|
rescue
|
|
|
|
"#{n}=ERR(#{$!})"
|
|
|
|
end
|
2012-04-06 11:30:58 -06:00
|
|
|
end
|
2012-04-19 17:29:52 -06:00
|
|
|
|
2012-05-10 15:27:42 -06:00
|
|
|
class Enum
|
|
|
|
# number -> symbol
|
|
|
|
def self.enum
|
|
|
|
# ruby weirdness, needed to make the constants 'virtual'
|
|
|
|
@enum ||= const_get(:ENUM)
|
|
|
|
end
|
|
|
|
# symbol -> number
|
|
|
|
def self.nume
|
|
|
|
@nume ||= const_get(:NUME)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.to_i(i)
|
|
|
|
nume[i] || i
|
|
|
|
end
|
|
|
|
def self.to_sym(i)
|
|
|
|
enum[i] || i
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-04-06 11:30:58 -06:00
|
|
|
class Number < MemStruct
|
2012-04-26 12:55:52 -06:00
|
|
|
attr_accessor :_bits, :_signed, :_initvalue, :_enum
|
|
|
|
def initialize(bits, signed, initvalue, enum)
|
2012-04-06 11:30:58 -06:00
|
|
|
@_bits = bits
|
|
|
|
@_signed = signed
|
2012-04-26 06:16:47 -06:00
|
|
|
@_initvalue = initvalue
|
2012-04-26 12:55:52 -06:00
|
|
|
@_enum = enum
|
2012-04-06 11:30:58 -06:00
|
|
|
end
|
2012-04-12 11:12:46 -06:00
|
|
|
|
|
|
|
def _get
|
|
|
|
v = case @_bits
|
|
|
|
when 32; DFHack.memory_read_int32(@_memaddr)
|
|
|
|
when 16; DFHack.memory_read_int16(@_memaddr)
|
|
|
|
when 8; DFHack.memory_read_int8( @_memaddr)
|
|
|
|
when 64;(DFHack.memory_read_int32(@_memaddr) & 0xffffffff) + (DFHack.memory_read_int32(@_memaddr+4) << 32)
|
|
|
|
end
|
|
|
|
v &= (1 << @_bits) - 1 if not @_signed
|
2012-05-10 15:27:42 -06:00
|
|
|
v = @_enum.to_sym(v) if @_enum
|
2012-04-12 11:12:46 -06:00
|
|
|
v
|
|
|
|
end
|
|
|
|
|
|
|
|
def _set(v)
|
2012-05-10 15:27:42 -06:00
|
|
|
v = @_enum.to_i(v) if @_enum
|
2012-04-12 11:12:46 -06:00
|
|
|
case @_bits
|
|
|
|
when 32; DFHack.memory_write_int32(@_memaddr, v)
|
|
|
|
when 16; DFHack.memory_write_int16(@_memaddr, v)
|
|
|
|
when 8; DFHack.memory_write_int8( @_memaddr, v)
|
|
|
|
when 64; DFHack.memory_write_int32(@_memaddr, v & 0xffffffff) ; DFHack.memory_write_int32(@memaddr+4, v>>32)
|
|
|
|
end
|
|
|
|
end
|
2012-04-26 06:16:47 -06:00
|
|
|
|
|
|
|
def _cpp_init
|
|
|
|
_set(@_initvalue) if @_initvalue
|
|
|
|
end
|
2012-04-06 11:30:58 -06:00
|
|
|
end
|
|
|
|
class Float < MemStruct
|
2012-04-12 11:12:46 -06:00
|
|
|
def _get
|
|
|
|
DFHack.memory_read_float(@_memaddr)
|
|
|
|
end
|
|
|
|
|
|
|
|
def _set(v)
|
|
|
|
DFHack.memory_write_float(@_memaddr, v)
|
|
|
|
end
|
2012-04-26 06:16:47 -06:00
|
|
|
|
|
|
|
def _cpp_init
|
|
|
|
_set(0.0)
|
|
|
|
end
|
2012-04-06 11:30:58 -06:00
|
|
|
end
|
2012-04-12 11:12:46 -06:00
|
|
|
class BitField < MemStruct
|
2012-04-26 12:55:52 -06:00
|
|
|
attr_accessor :_shift, :_len, :_enum
|
|
|
|
def initialize(shift, len, enum=nil)
|
2012-04-06 11:30:58 -06:00
|
|
|
@_shift = shift
|
|
|
|
@_len = len
|
2012-04-26 12:55:52 -06:00
|
|
|
@_enum = enum
|
2012-04-12 11:12:46 -06:00
|
|
|
end
|
|
|
|
def _mask
|
|
|
|
(1 << @_len) - 1
|
2012-04-06 11:30:58 -06:00
|
|
|
end
|
|
|
|
|
2012-04-12 11:12:46 -06:00
|
|
|
def _get
|
|
|
|
v = DFHack.memory_read_int32(@_memaddr) >> @_shift
|
2012-04-06 11:30:58 -06:00
|
|
|
if @_len == 1
|
2012-04-12 11:12:46 -06:00
|
|
|
((v & 1) == 0) ? false : true
|
2012-04-06 11:30:58 -06:00
|
|
|
else
|
2012-04-26 12:55:52 -06:00
|
|
|
v &= _mask
|
2012-05-10 15:27:42 -06:00
|
|
|
v = @_enum.to_sym(v) if @_enum
|
2012-04-26 12:55:52 -06:00
|
|
|
v
|
2012-04-06 11:30:58 -06:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def _set(v)
|
|
|
|
if @_len == 1
|
2012-04-12 11:12:46 -06:00
|
|
|
# allow 'bit = 0'
|
2012-04-06 11:30:58 -06:00
|
|
|
v = (v && v != 0 ? 1 : 0)
|
|
|
|
end
|
2012-05-10 15:27:42 -06:00
|
|
|
v = @_enum.to_i(v) if @_enum
|
2012-04-12 11:12:46 -06:00
|
|
|
v = (v & _mask) << @_shift
|
2012-04-06 11:30:58 -06:00
|
|
|
|
2012-04-12 11:12:46 -06:00
|
|
|
ori = DFHack.memory_read_int32(@_memaddr) & 0xffffffff
|
|
|
|
DFHack.memory_write_int32(@_memaddr, ori - (ori & ((-1 & _mask) << @_shift)) + v)
|
2012-04-06 11:30:58 -06:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class Pointer < MemStruct
|
2012-04-19 17:29:52 -06:00
|
|
|
attr_accessor :_tg
|
|
|
|
def initialize(tg)
|
|
|
|
@_tg = tg
|
|
|
|
end
|
|
|
|
|
|
|
|
def _getp
|
|
|
|
DFHack.memory_read_int32(@_memaddr) & 0xffffffff
|
|
|
|
end
|
|
|
|
|
2012-04-27 09:11:01 -06:00
|
|
|
def _get
|
2012-04-19 17:29:52 -06:00
|
|
|
addr = _getp
|
2012-04-20 07:30:15 -06:00
|
|
|
return if addr == 0
|
|
|
|
@_tg._at(addr)._get
|
|
|
|
end
|
|
|
|
|
2012-04-25 11:22:04 -06:00
|
|
|
# XXX shaky...
|
|
|
|
def _set(v)
|
|
|
|
if v.kind_of?(Pointer)
|
2012-04-27 09:11:01 -06:00
|
|
|
DFHack.memory_write_int32(@_memaddr, v._getp)
|
2012-04-25 11:22:04 -06:00
|
|
|
elsif v.kind_of?(MemStruct)
|
|
|
|
DFHack.memory_write_int32(@_memaddr, v._memaddr)
|
|
|
|
else
|
2012-04-27 09:11:01 -06:00
|
|
|
_get._set(v)
|
2012-04-25 11:22:04 -06:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-04-20 07:30:15 -06:00
|
|
|
def inspect
|
2012-04-27 09:11:01 -06:00
|
|
|
ptr = _getp
|
|
|
|
if ptr == 0
|
|
|
|
'NULL'
|
|
|
|
else
|
2012-04-27 09:46:51 -06:00
|
|
|
cn = ''
|
|
|
|
cn = @_tg.class.name.sub(/^DFHack::/, '').sub(/^MemHack::/, '') if @_tg
|
|
|
|
cn = @_tg._glob if cn == 'Global'
|
2012-04-27 09:11:01 -06:00
|
|
|
"#<Pointer #{cn} #{'0x%X' % _getp}>"
|
|
|
|
end
|
2012-04-19 17:29:52 -06:00
|
|
|
end
|
|
|
|
end
|
|
|
|
class PointerAry < MemStruct
|
2012-04-06 11:30:58 -06:00
|
|
|
attr_accessor :_tglen, :_tg
|
|
|
|
def initialize(tglen, tg)
|
|
|
|
@_tglen = tglen
|
|
|
|
@_tg = tg
|
|
|
|
end
|
2012-04-12 11:12:46 -06:00
|
|
|
|
2012-04-13 11:31:12 -06:00
|
|
|
def _getp(i=0)
|
|
|
|
delta = (i != 0 ? i*@_tglen : 0)
|
|
|
|
(DFHack.memory_read_int32(@_memaddr) & 0xffffffff) + delta
|
2012-04-12 11:12:46 -06:00
|
|
|
end
|
|
|
|
|
2012-05-06 14:42:06 -06:00
|
|
|
def _get
|
|
|
|
addr = _getp
|
|
|
|
return if addr == 0
|
|
|
|
self
|
|
|
|
end
|
|
|
|
|
2012-04-13 11:31:12 -06:00
|
|
|
def [](i)
|
|
|
|
addr = _getp(i)
|
|
|
|
return if addr == 0
|
2012-04-12 11:12:46 -06:00
|
|
|
@_tg._at(addr)._get
|
|
|
|
end
|
2012-04-13 11:31:12 -06:00
|
|
|
def []=(i, v)
|
|
|
|
addr = _getp(i)
|
|
|
|
raise 'null pointer' if addr == 0
|
2012-04-12 11:12:46 -06:00
|
|
|
@_tg._at(addr)._set(v)
|
|
|
|
end
|
2012-04-20 07:30:15 -06:00
|
|
|
|
2012-05-06 14:42:06 -06:00
|
|
|
def inspect ; ptr = _getp ; (ptr == 0) ? 'NULL' : "#<PointerAry #{'0x%X' % ptr}>" ; end
|
2012-04-06 11:30:58 -06:00
|
|
|
end
|
2012-04-27 09:11:01 -06:00
|
|
|
module Enumerable
|
|
|
|
include ::Enumerable
|
2012-04-27 09:31:39 -06:00
|
|
|
attr_accessor :_indexenum
|
2012-04-27 09:11:01 -06:00
|
|
|
def each ; (0...length).each { |i| yield self[i] } ; end
|
|
|
|
def inspect
|
|
|
|
out = '['
|
2012-05-02 18:52:53 -06:00
|
|
|
each_with_index { |e, idx|
|
2012-04-27 09:11:01 -06:00
|
|
|
out << ', ' if out.length > 1
|
|
|
|
if out.length > INSPECT_SIZE_LIMIT
|
|
|
|
out << '...'
|
|
|
|
break
|
|
|
|
end
|
2012-05-10 15:27:42 -06:00
|
|
|
out << "#{_indexenum.to_sym(idx)}=" if _indexenum
|
2012-05-02 18:52:53 -06:00
|
|
|
out << e.inspect
|
2012-04-27 09:11:01 -06:00
|
|
|
}
|
|
|
|
out << ']'
|
|
|
|
end
|
2012-04-27 09:46:51 -06:00
|
|
|
def empty? ; length == 0 ; end
|
2012-04-27 09:11:01 -06:00
|
|
|
def flatten ; map { |e| e.respond_to?(:flatten) ? e.flatten : e }.flatten ; end
|
|
|
|
end
|
2012-04-06 11:30:58 -06:00
|
|
|
class StaticArray < MemStruct
|
2012-04-25 09:18:24 -06:00
|
|
|
attr_accessor :_tglen, :_length, :_indexenum, :_tg
|
|
|
|
def initialize(tglen, length, indexenum, tg)
|
2012-04-06 11:30:58 -06:00
|
|
|
@_tglen = tglen
|
|
|
|
@_length = length
|
2012-04-25 09:18:24 -06:00
|
|
|
@_indexenum = indexenum
|
2012-04-06 11:30:58 -06:00
|
|
|
@_tg = tg
|
|
|
|
end
|
2012-04-12 11:12:46 -06:00
|
|
|
def _set(a)
|
|
|
|
a.each_with_index { |v, i| self[i] = v }
|
|
|
|
end
|
2012-04-26 06:16:47 -06:00
|
|
|
def _cpp_init
|
|
|
|
_length.times { |i| _tgat(i)._cpp_init }
|
|
|
|
end
|
2012-04-06 11:30:58 -06:00
|
|
|
alias length _length
|
|
|
|
alias size _length
|
|
|
|
def _tgat(i)
|
2012-04-12 11:12:46 -06:00
|
|
|
@_tg._at(@_memaddr + i*@_tglen) if i >= 0 and i < @_length
|
2012-04-06 11:30:58 -06:00
|
|
|
end
|
|
|
|
def [](i)
|
2012-05-10 15:27:42 -06:00
|
|
|
i = _indexenum.to_i(i) if _indexenum
|
2012-04-12 11:12:46 -06:00
|
|
|
i += @_length if i < 0
|
2012-04-13 11:31:12 -06:00
|
|
|
_tgat(i)._get
|
2012-04-06 11:30:58 -06:00
|
|
|
end
|
|
|
|
def []=(i, v)
|
2012-05-10 15:27:42 -06:00
|
|
|
i = _indexenum.to_i(i) if _indexenum
|
2012-04-12 11:12:46 -06:00
|
|
|
i += @_length if i < 0
|
2012-04-13 11:31:12 -06:00
|
|
|
_tgat(i)._set(v)
|
2012-04-06 11:30:58 -06:00
|
|
|
end
|
2012-04-13 08:17:56 -06:00
|
|
|
|
|
|
|
include Enumerable
|
2012-04-06 11:30:58 -06:00
|
|
|
end
|
|
|
|
class StaticString < MemStruct
|
|
|
|
attr_accessor :_length
|
|
|
|
def initialize(length)
|
|
|
|
@_length = length
|
|
|
|
end
|
2012-04-12 11:12:46 -06:00
|
|
|
def _get
|
|
|
|
DFHack.memory_read(@_memaddr, @_length)
|
|
|
|
end
|
|
|
|
def _set(v)
|
|
|
|
DFHack.memory_write(@_memaddr, v[0, @_length])
|
|
|
|
end
|
2012-04-06 11:30:58 -06:00
|
|
|
end
|
|
|
|
|
2012-04-13 08:17:56 -06:00
|
|
|
class StlVector32 < MemStruct
|
|
|
|
attr_accessor :_tg
|
|
|
|
def initialize(tg)
|
2012-04-06 11:30:58 -06:00
|
|
|
@_tg = tg
|
|
|
|
end
|
2012-04-12 11:12:46 -06:00
|
|
|
|
|
|
|
def length
|
2012-04-13 08:17:56 -06:00
|
|
|
DFHack.memory_vector32_length(@_memaddr)
|
2012-04-12 11:12:46 -06:00
|
|
|
end
|
2012-04-19 11:35:55 -06:00
|
|
|
def size ; length ; end # alias wouldnt work for subclasses
|
|
|
|
def valueptr_at(idx)
|
|
|
|
DFHack.memory_vector32_ptrat(@_memaddr, idx)
|
2012-04-12 11:12:46 -06:00
|
|
|
end
|
|
|
|
def insert_at(idx, val)
|
2012-04-13 08:17:56 -06:00
|
|
|
DFHack.memory_vector32_insert(@_memaddr, idx, val)
|
2012-04-12 11:12:46 -06:00
|
|
|
end
|
|
|
|
def delete_at(idx)
|
2012-04-13 08:17:56 -06:00
|
|
|
DFHack.memory_vector32_delete(@_memaddr, idx)
|
2012-04-12 11:12:46 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
def _set(v)
|
|
|
|
delete_at(length-1) while length > v.length # match lengthes
|
|
|
|
v.each_with_index { |e, i| self[i] = e } # patch entries
|
|
|
|
end
|
|
|
|
|
2012-04-26 06:16:47 -06:00
|
|
|
def _cpp_init
|
|
|
|
DFHack.memory_vector_init(@_memaddr)
|
|
|
|
end
|
|
|
|
|
2012-04-12 11:12:46 -06:00
|
|
|
def clear
|
|
|
|
delete_at(length-1) while length > 0
|
|
|
|
end
|
|
|
|
def [](idx)
|
|
|
|
idx += length if idx < 0
|
2012-04-19 11:35:55 -06:00
|
|
|
@_tg._at(valueptr_at(idx))._get if idx >= 0 and idx < length
|
2012-04-12 11:12:46 -06:00
|
|
|
end
|
|
|
|
def []=(idx, v)
|
|
|
|
idx += length if idx < 0
|
|
|
|
if idx >= length
|
2012-04-19 11:35:55 -06:00
|
|
|
insert_at(idx, 0)
|
2012-04-12 11:12:46 -06:00
|
|
|
elsif idx < 0
|
|
|
|
raise 'invalid idx'
|
|
|
|
end
|
2012-04-19 11:35:55 -06:00
|
|
|
@_tg._at(valueptr_at(idx))._set(v)
|
2012-04-12 11:12:46 -06:00
|
|
|
end
|
2012-04-19 11:35:55 -06:00
|
|
|
def push(v)
|
|
|
|
self[length] = v
|
2012-04-12 11:12:46 -06:00
|
|
|
self
|
|
|
|
end
|
2012-04-19 11:35:55 -06:00
|
|
|
def <<(v) ; push(v) ; end
|
2012-04-12 11:12:46 -06:00
|
|
|
def pop
|
|
|
|
l = length
|
|
|
|
if l > 0
|
|
|
|
v = self[l-1]
|
|
|
|
delete_at(l-1)
|
|
|
|
end
|
|
|
|
v
|
|
|
|
end
|
2012-04-19 17:29:52 -06:00
|
|
|
|
2012-04-13 08:17:56 -06:00
|
|
|
include Enumerable
|
2012-04-26 11:37:58 -06:00
|
|
|
# do a binary search in an ordered vector for a specific target attribute
|
|
|
|
# ex: world.history.figures.binsearch(unit.hist_figure_id)
|
|
|
|
def binsearch(target, field=:id)
|
|
|
|
o_start = 0
|
|
|
|
o_end = length - 1
|
|
|
|
while o_end >= o_start
|
|
|
|
o_half = o_start + (o_end-o_start)/2
|
|
|
|
obj = self[o_half]
|
|
|
|
oval = obj.send(field)
|
|
|
|
if oval == target
|
|
|
|
return obj
|
|
|
|
elsif oval < target
|
|
|
|
o_start = o_half+1
|
|
|
|
else
|
|
|
|
o_end = o_half-1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2012-04-13 08:17:56 -06:00
|
|
|
end
|
|
|
|
class StlVector16 < StlVector32
|
|
|
|
def length
|
|
|
|
DFHack.memory_vector16_length(@_memaddr)
|
|
|
|
end
|
2012-04-19 11:35:55 -06:00
|
|
|
def valueptr_at(idx)
|
|
|
|
DFHack.memory_vector16_ptrat(@_memaddr, idx)
|
2012-04-13 08:17:56 -06:00
|
|
|
end
|
|
|
|
def insert_at(idx, val)
|
|
|
|
DFHack.memory_vector16_insert(@_memaddr, idx, val)
|
|
|
|
end
|
|
|
|
def delete_at(idx)
|
|
|
|
DFHack.memory_vector16_delete(@_memaddr, idx)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
class StlVector8 < StlVector32
|
|
|
|
def length
|
|
|
|
DFHack.memory_vector8_length(@_memaddr)
|
|
|
|
end
|
2012-04-19 11:35:55 -06:00
|
|
|
def valueptr_at(idx)
|
|
|
|
DFHack.memory_vector8_ptrat(@_memaddr, idx)
|
2012-04-13 08:17:56 -06:00
|
|
|
end
|
|
|
|
def insert_at(idx, val)
|
|
|
|
DFHack.memory_vector8_insert(@_memaddr, idx, val)
|
|
|
|
end
|
|
|
|
def delete_at(idx)
|
|
|
|
DFHack.memory_vector8_delete(@_memaddr, idx)
|
2012-04-12 11:12:46 -06:00
|
|
|
end
|
2012-04-06 11:30:58 -06:00
|
|
|
end
|
2012-04-19 11:35:55 -06:00
|
|
|
class StlBitVector < StlVector32
|
|
|
|
def initialize ; end
|
|
|
|
def length
|
|
|
|
DFHack.memory_vectorbool_length(@_memaddr)
|
|
|
|
end
|
|
|
|
def insert_at(idx, val)
|
|
|
|
DFHack.memory_vectorbool_insert(@_memaddr, idx, val)
|
|
|
|
end
|
|
|
|
def delete_at(idx)
|
|
|
|
DFHack.memory_vectorbool_delete(@_memaddr, idx)
|
|
|
|
end
|
|
|
|
def [](idx)
|
|
|
|
idx += length if idx < 0
|
|
|
|
DFHack.memory_vectorbool_at(@_memaddr, idx) if idx >= 0 and idx < length
|
|
|
|
end
|
|
|
|
def []=(idx, v)
|
|
|
|
idx += length if idx < 0
|
|
|
|
if idx >= length
|
|
|
|
insert_at(idx, v)
|
|
|
|
elsif idx < 0
|
|
|
|
raise 'invalid idx'
|
|
|
|
else
|
|
|
|
DFHack.memory_vectorbool_setat(@_memaddr, idx, v)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2012-04-06 11:30:58 -06:00
|
|
|
class StlString < MemStruct
|
2012-04-12 11:12:46 -06:00
|
|
|
def _get
|
|
|
|
DFHack.memory_read_stlstring(@_memaddr)
|
|
|
|
end
|
|
|
|
|
|
|
|
def _set(v)
|
|
|
|
DFHack.memory_write_stlstring(@_memaddr, v)
|
|
|
|
end
|
2012-04-26 06:16:47 -06:00
|
|
|
|
|
|
|
def _cpp_init
|
|
|
|
DFHack.memory_stlstring_init(@_memaddr)
|
|
|
|
end
|
2012-04-06 11:30:58 -06:00
|
|
|
end
|
|
|
|
class StlDeque < MemStruct
|
|
|
|
attr_accessor :_tglen, :_tg
|
|
|
|
def initialize(tglen, tg)
|
|
|
|
@_tglen = tglen
|
|
|
|
@_tg = tg
|
|
|
|
end
|
2012-04-21 07:04:52 -06:00
|
|
|
# XXX DF uses stl::deque<some_struct>, so to have a C binding we'd need to single-case every
|
|
|
|
# possible struct size, like for StlVector. Just ignore it for now, deque are rare enough.
|
2012-04-20 09:33:48 -06:00
|
|
|
def inspect ; "#<StlDeque>" ; end
|
2012-04-06 11:30:58 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
class DfFlagarray < MemStruct
|
2012-04-25 09:18:24 -06:00
|
|
|
attr_accessor :_indexenum
|
|
|
|
def initialize(indexenum)
|
|
|
|
@_indexenum = indexenum
|
|
|
|
end
|
2012-04-20 09:57:41 -06:00
|
|
|
def length
|
|
|
|
DFHack.memory_bitarray_length(@_memaddr)
|
|
|
|
end
|
2012-04-26 06:16:47 -06:00
|
|
|
# TODO _cpp_init
|
2012-04-20 09:57:41 -06:00
|
|
|
def size ; length ; end
|
|
|
|
def resize(len)
|
|
|
|
DFHack.memory_bitarray_resize(@_memaddr, len)
|
|
|
|
end
|
|
|
|
def [](idx)
|
2012-05-10 15:27:42 -06:00
|
|
|
idx = _indexenum.to_i(idx) if _indexenum
|
2012-04-20 09:57:41 -06:00
|
|
|
idx += length if idx < 0
|
|
|
|
DFHack.memory_bitarray_isset(@_memaddr, idx) if idx >= 0 and idx < length
|
|
|
|
end
|
|
|
|
def []=(idx, v)
|
2012-05-10 15:27:42 -06:00
|
|
|
idx = _indexenum.to_i(idx) if _indexenum
|
2012-04-20 09:57:41 -06:00
|
|
|
idx += length if idx < 0
|
|
|
|
if idx >= length or idx < 0
|
|
|
|
raise 'invalid idx'
|
|
|
|
else
|
|
|
|
DFHack.memory_bitarray_set(@_memaddr, idx, v)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
include Enumerable
|
2012-04-06 11:30:58 -06:00
|
|
|
end
|
2012-04-19 17:29:52 -06:00
|
|
|
class DfArray < Compound
|
2012-04-06 11:30:58 -06:00
|
|
|
attr_accessor :_tglen, :_tg
|
|
|
|
def initialize(tglen, tg)
|
|
|
|
@_tglen = tglen
|
|
|
|
@_tg = tg
|
|
|
|
end
|
2012-04-12 11:12:46 -06:00
|
|
|
|
2012-04-20 07:30:15 -06:00
|
|
|
field(:_ptr, 0) { number 32, false }
|
|
|
|
field(:_length, 4) { number 16, false }
|
2012-04-19 17:29:52 -06:00
|
|
|
|
|
|
|
def length ; _length ; end
|
|
|
|
def size ; _length ; end
|
2012-04-26 06:16:47 -06:00
|
|
|
# TODO _cpp_init
|
2012-04-19 17:29:52 -06:00
|
|
|
def _tgat(i)
|
|
|
|
@_tg._at(_ptr + i*@_tglen) if i >= 0 and i < _length
|
|
|
|
end
|
|
|
|
def [](i)
|
|
|
|
i += _length if i < 0
|
|
|
|
_tgat(i)._get
|
|
|
|
end
|
|
|
|
def []=(i, v)
|
|
|
|
i += _length if i < 0
|
|
|
|
_tgat(i)._set(v)
|
|
|
|
end
|
|
|
|
def _set(a)
|
|
|
|
a.each_with_index { |v, i| self[i] = v }
|
|
|
|
end
|
|
|
|
|
|
|
|
include Enumerable
|
2012-04-06 11:30:58 -06:00
|
|
|
end
|
2012-04-20 10:20:24 -06:00
|
|
|
class DfLinkedList < Compound
|
2012-04-06 11:30:58 -06:00
|
|
|
attr_accessor :_tg
|
|
|
|
def initialize(tg)
|
|
|
|
@_tg = tg
|
|
|
|
end
|
2012-04-20 10:20:24 -06:00
|
|
|
|
|
|
|
field(:_ptr, 0) { number 32, false }
|
|
|
|
field(:_prev, 4) { number 32, false }
|
|
|
|
field(:_next, 8) { number 32, false }
|
|
|
|
|
|
|
|
def item
|
2012-04-27 09:11:01 -06:00
|
|
|
# With the current xml structure, currently _tg designate
|
|
|
|
# the type of the 'next' and 'prev' fields, not 'item'.
|
|
|
|
# List head has item == NULL, so we can safely return nil.
|
|
|
|
|
|
|
|
#addr = _ptr
|
|
|
|
#return if addr == 0
|
|
|
|
#@_tg._at(addr)._get
|
2012-04-20 10:20:24 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
def item=(v)
|
2012-04-27 09:11:01 -06:00
|
|
|
#addr = _ptr
|
|
|
|
#raise 'null pointer' if addr == 0
|
|
|
|
#@_tg.at(addr)._set(v)
|
|
|
|
raise 'null pointer'
|
2012-04-20 10:20:24 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
def prev
|
|
|
|
addr = _prev
|
|
|
|
return if addr == 0
|
2012-04-27 09:11:01 -06:00
|
|
|
@_tg._at(addr)._get
|
2012-04-20 10:20:24 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
def next
|
|
|
|
addr = _next
|
|
|
|
return if addr == 0
|
2012-04-27 09:11:01 -06:00
|
|
|
@_tg._at(addr)._get
|
2012-04-20 10:20:24 -06:00
|
|
|
end
|
|
|
|
|
2012-04-20 12:43:18 -06:00
|
|
|
include Enumerable
|
|
|
|
def each
|
|
|
|
o = self
|
|
|
|
while o
|
|
|
|
yield o.item if o.item
|
|
|
|
o = o.next
|
|
|
|
end
|
|
|
|
end
|
2012-04-27 09:11:01 -06:00
|
|
|
def inspect ; "#<DfLinkedList #{item.inspect} prev=#{'0x%X' % _prev} next=#{'0x%X' % _next}>" ; end
|
2012-04-06 11:30:58 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
class Global < MemStruct
|
|
|
|
attr_accessor :_glob
|
|
|
|
def initialize(glob)
|
|
|
|
@_glob = glob
|
|
|
|
end
|
2012-04-18 10:46:33 -06:00
|
|
|
def _at(addr)
|
2012-04-19 17:29:52 -06:00
|
|
|
g = DFHack.const_get(@_glob)
|
|
|
|
g = DFHack.rtti_getclassat(g, addr)
|
2012-04-18 10:46:33 -06:00
|
|
|
g.new._at(addr)
|
|
|
|
end
|
2012-04-20 07:30:15 -06:00
|
|
|
def inspect ; "#<#{@_glob}>" ; end
|
2012-04-06 11:30:58 -06:00
|
|
|
end
|
2012-04-19 17:29:52 -06:00
|
|
|
end # module MemHack
|
2012-04-06 11:30:58 -06:00
|
|
|
|
|
|
|
|
2012-04-19 17:29:52 -06:00
|
|
|
# cpp rtti name -> rb class
|
|
|
|
@rtti_n2c = {}
|
|
|
|
@rtti_c2n = {}
|
|
|
|
|
2012-04-21 07:04:52 -06:00
|
|
|
# cpp rtti name -> vtable ptr
|
2012-04-19 17:29:52 -06:00
|
|
|
@rtti_n2v = {}
|
|
|
|
@rtti_v2n = {}
|
|
|
|
|
|
|
|
def self.rtti_n2c ; @rtti_n2c ; end
|
|
|
|
def self.rtti_c2n ; @rtti_c2n ; end
|
2012-04-21 07:04:52 -06:00
|
|
|
def self.rtti_n2v ; @rtti_n2v ; end
|
|
|
|
def self.rtti_v2n ; @rtti_v2n ; end
|
2012-04-19 17:29:52 -06:00
|
|
|
|
|
|
|
# register a ruby class with a cpp rtti class name
|
2012-04-21 07:04:52 -06:00
|
|
|
def self.rtti_register(cppname, cls)
|
|
|
|
@rtti_n2c[cppname] = cls
|
|
|
|
@rtti_c2n[cls] = cppname
|
2012-04-19 17:29:52 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
# return the ruby class to use for the cpp object at address if rtti info is available
|
|
|
|
def self.rtti_getclassat(cls, addr)
|
|
|
|
if addr != 0 and @rtti_c2n[cls]
|
|
|
|
# rtti info exist for class => cpp object has a vtable
|
|
|
|
@rtti_n2c[rtti_readclassname(get_vtable_ptr(addr))] || cls
|
|
|
|
else
|
|
|
|
cls
|
2012-04-18 10:46:33 -06:00
|
|
|
end
|
2012-04-19 17:29:52 -06:00
|
|
|
end
|
2012-04-06 11:59:11 -06:00
|
|
|
|
2012-04-19 17:29:52 -06:00
|
|
|
# try to read the rtti classname from an object vtable pointer
|
|
|
|
def self.rtti_readclassname(vptr)
|
|
|
|
unless n = @rtti_v2n[vptr]
|
2012-04-26 12:55:52 -06:00
|
|
|
n = @rtti_v2n[vptr] = get_rtti_classname(vptr).to_sym
|
2012-04-19 17:29:52 -06:00
|
|
|
@rtti_n2v[n] = vptr
|
2012-04-18 10:46:33 -06:00
|
|
|
end
|
2012-04-19 17:29:52 -06:00
|
|
|
n
|
2012-04-18 10:46:33 -06:00
|
|
|
end
|
2012-04-19 17:29:52 -06:00
|
|
|
|
|
|
|
# return the vtable pointer from the cpp rtti name
|
2012-04-21 07:04:52 -06:00
|
|
|
def self.rtti_getvtable(cppname)
|
|
|
|
unless v = @rtti_n2v[cppname]
|
2012-04-27 10:50:27 -06:00
|
|
|
v = get_vtable(cppname.to_s)
|
2012-04-21 07:04:52 -06:00
|
|
|
@rtti_n2v[cppname] = v
|
|
|
|
@rtti_v2n[v] = cppname if v != 0
|
2012-04-19 17:29:52 -06:00
|
|
|
end
|
|
|
|
v if v != 0
|
|
|
|
end
|
|
|
|
|
2012-05-03 10:49:12 -06:00
|
|
|
def self.vmethod_call(obj, voff, a0=0, a1=0, a2=0, a3=0, a4=0)
|
|
|
|
vmethod_do_call(obj._memaddr, voff, vmethod_arg(a0), vmethod_arg(a1), vmethod_arg(a2), vmethod_arg(a3))
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.vmethod_arg(arg)
|
|
|
|
case arg
|
|
|
|
when nil, false; 0
|
|
|
|
when true; 1
|
|
|
|
when Integer; arg
|
|
|
|
#when String; [arg].pack('p').unpack('L')[0] # raw pointer to buffer
|
|
|
|
when MemHack::Compound; arg._memaddr
|
|
|
|
else raise "bad vmethod arg #{arg.class}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-04-19 17:29:52 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
|