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;
} 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,12 +932,20 @@ sub render_item_container {
elsif ($indexenum)
{
$indexenum = rb_ucase($indexenum);
if ($count) {
push @lines_rb, "$rbmethod($count, $indexenum)";
} else {
push @lines_rb, "$rbmethod($indexenum)";
}
}
else
{
if ($count) {
push @lines_rb, "$rbmethod($count)";
} else {
push @lines_rb, "$rbmethod";
}
}
}
sub render_item_pointer {

@ -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 = "#<DfStaticFlagarray"
each_with_index { |e, idx|
out << " #{_indexenum.sym(idx)}" if e
}
out << '>'
end
include Enumerable
end
class DfArray < Compound
attr_accessor :_tglen, :_tg
def initialize(tglen, tg)