diff --git a/plugins/ruby/codegen.pl b/plugins/ruby/codegen.pl index 9d76c6872..03017a0f5 100755 --- a/plugins/ruby/codegen.pl +++ b/plugins/ruby/codegen.pl @@ -698,6 +698,8 @@ sub sizeof { return 12; } elsif ($subtype eq 'df-flagarray') { return 8; + } elsif ($subtype eq 'df-static-flagarray') { + return $field->getAttribute('count'); } elsif ($subtype eq 'df-array') { return 8; # XXX 6 ? } else { @@ -913,6 +915,7 @@ sub render_item_container { my $rbmethod = join('_', split('-', $subtype)); my $tg = $item->findnodes('child::ld:item')->[0]; my $indexenum = $item->getAttribute('index-enum'); + my $count = $item->getAttribute('count'); if ($tg) { if ($rbmethod eq 'df_linked_list') { @@ -929,11 +932,19 @@ sub render_item_container { elsif ($indexenum) { $indexenum = rb_ucase($indexenum); - push @lines_rb, "$rbmethod($indexenum)"; + if ($count) { + push @lines_rb, "$rbmethod($count, $indexenum)"; + } else { + push @lines_rb, "$rbmethod($indexenum)"; + } } else { - push @lines_rb, "$rbmethod"; + if ($count) { + push @lines_rb, "$rbmethod($count)"; + } else { + push @lines_rb, "$rbmethod"; + } } } diff --git a/plugins/ruby/ruby-autogen-defs.rb b/plugins/ruby/ruby-autogen-defs.rb index e657962d5..ffd68bf1e 100644 --- a/plugins/ruby/ruby-autogen-defs.rb +++ b/plugins/ruby/ruby-autogen-defs.rb @@ -78,6 +78,9 @@ module DFHack def df_flagarray(indexenum=nil) DfFlagarray.new(indexenum) end + def df_static_flagarray(len, indexenum=nil) + DfStaticFlagarray.new(len, indexenum) + end def df_array(tglen) DfArray.new(tglen, yield) end @@ -680,6 +683,48 @@ module DFHack include Enumerable end + class DfStaticFlagarray < MemStruct + attr_accessor :_indexenum + def initialize(len, indexenum) + @len = len*8 + @_indexenum = indexenum + end + def length + @len + end + def size ; length ; end + def [](idx) + idx = _indexenum.int(idx) if _indexenum + idx += length if idx < 0 + return if idx < 0 or idx >= length + byte = DFHack.memory_read_int8(@_memaddr + idx/8) + (byte & (1 << (idx%8))) > 0 + end + def []=(idx, v) + idx = _indexenum.int(idx) if _indexenum + idx += length if idx < 0 + if idx >= length or idx < 0 + raise 'index out of bounds' + else + byte = DFHack.memory_read_int8(@_memaddr + idx/8) + if (v == nil or v == false or v == 0) + byte &= 0xff ^ (1 << (idx%8)) + else + byte |= (1 << (idx%8)) + end + DFHack.memory_write_int8(@_memaddr + idx/8, byte) + end + end + def inspect + out = "#' + end + + include Enumerable + end class DfArray < Compound attr_accessor :_tglen, :_tg def initialize(tglen, tg)