diff --git a/plugins/ruby/codegen.pl b/plugins/ruby/codegen.pl index 408121cf5..effcb9327 100755 --- a/plugins/ruby/codegen.pl +++ b/plugins/ruby/codegen.pl @@ -51,7 +51,8 @@ sub render_global_enum { my ($name, $type) = @_; my $rbname = rb_ucase($name); - push @lines_rb, "class $rbname"; + # store constants in DFHack::EnumName and not in DFHack::MemHack::EnumName + push @lines_rb, "class ::DFHack::$rbname"; %seen_enum_name = (); indent_rb { render_enum_fields($type); diff --git a/plugins/ruby/ruby-memstruct.rb b/plugins/ruby/ruby-memstruct.rb index 5a759add8..e87731bf8 100644 --- a/plugins/ruby/ruby-memstruct.rb +++ b/plugins/ruby/ruby-memstruct.rb @@ -152,28 +152,29 @@ class Pointer < MemStruct @_tg = tg end - def _getp - DFHack.memory_read_int32(@_memaddr) & 0xffffffff + def _getp(i=0) + delta = (i != 0 ? i*@_tglen : 0) + (DFHack.memory_read_int32(@_memaddr) & 0xffffffff) + delta end def _setp(v) DFHack.memory_write_int32(@_memaddr, v) end + def _get ; self ; end - # _getp/_setp defined in ruby.cpp, access the pointer value - def _get - addr = _getp - return if addr == 0 # XXX are there pointers with -1 as 'empty' value ? + def [](i) + addr = _getp(i) + return if addr == 0 @_tg._at(addr)._get end - def _set(v) - addr = _getp - raise 'null pointer' if addr == 0 # TODO malloc ? + def []=(i, v) + addr = _getp(i) + raise 'null pointer' if addr == 0 @_tg._at(addr)._set(v) end # the pointer is invisible, forward all methods to the pointed object def method_missing(*a) - _get.send(*a) + self[0].send(*a) end end class StaticArray < MemStruct @@ -193,11 +194,11 @@ class StaticArray < MemStruct end def [](i) i += @_length if i < 0 - tgat(i)._get + _tgat(i)._get end def []=(i, v) i += @_length if i < 0 - tgat(i)._set(v) + _tgat(i)._set(v) end include Enumerable diff --git a/plugins/ruby/ruby.rb b/plugins/ruby/ruby.rb index 3decef958..13567c9ec 100644 --- a/plugins/ruby/ruby.rb +++ b/plugins/ruby/ruby.rb @@ -19,12 +19,14 @@ module DFHack a.flatten.each { |l| print_str(l.to_s.chomp + "\n") } + nil end def puts_err(*a) a.flatten.each { |l| print_err(l.to_s.chomp + "\n") } + nil end def test @@ -34,6 +36,11 @@ module DFHack puts "cursor pos: #{cursor.x} #{cursor.y} #{cursor.z}" puts "unit[0] id: #{world.units.all[0].id}" + + if cursor.x >= 0 + world.map.block_index[cursor.x/16][cursor.y/16][cursor.z].designation[cursor.x%16][cursor.y%16].dig = TileDigDesignation::Default + puts "dug cursor tile" + end } puts "done"