Skip to content

Instantly share code, notes, and snippets.

@telamon
Created June 1, 2011 13:38
Show Gist options
  • Save telamon/1002302 to your computer and use it in GitHub Desktop.
Save telamon/1002302 to your computer and use it in GitHub Desktop.
BufferMapper provides lazy-loading bindings for a binary buffer
module Raumod::BufferMapper
# See http://ruby-doc.org/core/classes/String.html#M001112
# for map types.
def data_map;@data_map;end
def data;@data;end
def data_entry(method)
offset = data_map[method][:offset]
type = data_map[method][:type]
d = data[offset..-1].unpack(type)
if d.count > 1
d
else
d.first
end
end
def method_missing(method,*args)
if data_map[method]
data_entry(method)
else
raise "No such method: #{method}"
end
end
end
class Raumod::XM::Module
require 'buffer_mapper'
include Raumod::BufferMapper
HeaderMap = {
:id_text=>{ :offset=> 0, :type=>'A17'},
:name=>{:offset=>17,:type=>'A20'},
:'1a' => {:offset=> 37, :type=>'C'},
:tracker_name=>{:offset=>38,:type=>'A20'},
:version=> {:offset=> 58, :type=> 'v'},
:header_size=>{:offset=> 60,:type=>'V'},
:song_length=>{:offset=> 64,:type=>'v'},
:restart_position=>{:offset=> 66 ,:type=>'v'},
:channel_count=>{:offset=> 68 ,:type=>'v'},
:pattern_count=>{:offset=> 70 ,:type=>'v'},
:instrument_count =>{:offset=> 72 ,:type=>'v'},
:flags =>{:offset=> 74 ,:type=>'v'},
:default_tempo=>{:offset=> 76 ,:type=>'v'},
:default_bpm=>{:offset=> 78 ,:type=>'v'}
}
attr_accessor :patterns,:instruments
def initialize(data=nil)
@data= data #|| "Extended Module: "
@data_map = HeaderMap
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment