ruby: add df-static-flagarray

develop
jj 2012-12-04 17:46:13 +01:00
parent 0b80dff09d
commit 74ebe7d207
2 changed files with 58 additions and 2 deletions

@ -698,6 +698,8 @@ sub sizeof {
return 12; return 12;
} elsif ($subtype eq 'df-flagarray') { } elsif ($subtype eq 'df-flagarray') {
return 8; return 8;
} elsif ($subtype eq 'df-static-flagarray') {
return $field->getAttribute('count');
} elsif ($subtype eq 'df-array') { } elsif ($subtype eq 'df-array') {
return 8; # XXX 6 ? return 8; # XXX 6 ?
} else { } else {
@ -913,6 +915,7 @@ sub render_item_container {
my $rbmethod = join('_', split('-', $subtype)); my $rbmethod = join('_', split('-', $subtype));
my $tg = $item->findnodes('child::ld:item')->[0]; my $tg = $item->findnodes('child::ld:item')->[0];
my $indexenum = $item->getAttribute('index-enum'); my $indexenum = $item->getAttribute('index-enum');
my $count = $item->getAttribute('count');
if ($tg) if ($tg)
{ {
if ($rbmethod eq 'df_linked_list') { if ($rbmethod eq 'df_linked_list') {
@ -929,12 +932,20 @@ sub render_item_container {
elsif ($indexenum) elsif ($indexenum)
{ {
$indexenum = rb_ucase($indexenum); $indexenum = rb_ucase($indexenum);
if ($count) {
push @lines_rb, "$rbmethod($count, $indexenum)";
} else {
push @lines_rb, "$rbmethod($indexenum)"; push @lines_rb, "$rbmethod($indexenum)";
} }
}
else else
{ {
if ($count) {
push @lines_rb, "$rbmethod($count)";
} else {
push @lines_rb, "$rbmethod"; push @lines_rb, "$rbmethod";
} }
}
} }
sub render_item_pointer { sub render_item_pointer {

@ -78,6 +78,9 @@ module DFHack
def df_flagarray(indexenum=nil) def df_flagarray(indexenum=nil)
DfFlagarray.new(indexenum) DfFlagarray.new(indexenum)
end end
def df_static_flagarray(len, indexenum=nil)
DfStaticFlagarray.new(len, indexenum)
end
def df_array(tglen) def df_array(tglen)
DfArray.new(tglen, yield) DfArray.new(tglen, yield)
end end
@ -680,6 +683,48 @@ module DFHack
include Enumerable include Enumerable
end 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 = "#<DfStaticFlagarray"
each_with_index { |e, idx|
out << " #{_indexenum.sym(idx)}" if e
}
out << '>'
end
include Enumerable
end
class DfArray < Compound class DfArray < Compound
attr_accessor :_tglen, :_tg attr_accessor :_tglen, :_tg
def initialize(tglen, tg) def initialize(tglen, tg)