Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #==============================================================================
- # MBS - Event Export
- #------------------------------------------------------------------------------
- # por Masked
- #==============================================================================
- ($imported ||= {})[:mbs_event_export] = true
- #==============================================================================
- # * Verificação
- #==============================================================================
- unless $imported[:mbs_event_export_adapter]
- raise "MBS - Event Export: É necessário usar um módulo de adaptação para a engine que estiver usando."
- end
- #==============================================================================
- # ** MBS::EventExport
- #------------------------------------------------------------------------------
- # Este módulo controla a exportação
- #==============================================================================
- module MBS::EventExport
- #---------------------------------------------------------------------------
- # Versão [Maior, Menor]
- #
- # Alterações na versão maior implicam que o script não funciona em versões
- # anteriores
- #---------------------------------------------------------------------------
- VERSION = [1, 0]
- #---------------------------------------------------------------------------
- # Marcações do arquivo
- #
- # Ajudam a organizar o arquivo
- #---------------------------------------------------------------------------
- # Identifica o arquivo
- MAGIC = 'PEvF'
- # Identifica um começo de página
- PAGE = '^'
- # Identifica um começo de comando
- CMD = '@'
- # Identifica um chunk de dados
- DATA = '!'
- # Fim do arquivo
- EOF = ';'
- #---------------------------------------------------------------------------
- module_function
- #---------------------------------------------------------------------------
- # * Escrita do evento para um arquivo
- # arg : Argumento que identifica o evento
- # filename : Nome do arquivo de destino
- #---------------------------------------------------------------------------
- def export_event(arg, filename)
- begin
- file = File.open(filename, 'wb')
- # Aquisição do RPG::Event equivalente ao argumento passado
- if arg.is_a?(Game_Event)
- event = arg.instance_variable_get(:@event)
- elsif arg.is_a?(Integer)
- event = $game_map.events[arg].instance_variable_get(:@event)
- elsif arg.is_a?(RPG::Event)
- event = a
- else
- raise ArgumentError.new('Invalid argument')
- end
- # Escrita do cabeçalho do arquivo
- Writer.write_header(event, file)
- # Escrita de cada comando de cada página no arquivo
- Writer.write_pages(event, file)
- file.write(EOF)
- ensure
- file.close
- end
- end
- #--------------------------------------------------------------------------
- # * Importação de um evento
- # filename : Nome do arquivo onde o evento está
- #--------------------------------------------------------------------------
- def import_event(filename)
- file = File.open(filename, 'rb')
- event = RPG::Event.new(0,0)
- event.pages = []
- begin
- Reader.read_header(event, file)
- Reader.read_pages(event, file)
- ensure
- file.close
- end
- return event
- end
- end
- #==============================================================================
- # ** MBS::EventExport::Writer
- #------------------------------------------------------------------------------
- # Este é o módulo responsável pela escrita no arquivo
- #==============================================================================
- module MBS::EventExport::Writer
- include MBS::EventExport
- module_function
- #---------------------------------------------------------------------------
- # * Escrita de dados para um arquivo
- # data : Os dados a serem escritos
- # file : O arquivo onde os dados serão escritos
- #---------------------------------------------------------------------------
- def write_data(data, file)
- file.write(DATA)
- if data.nil?
- file.write([0].pack('L'))
- else
- data = data.to_s
- file.write([data.size].pack('L'))
- file.write(data)
- end
- end
- #---------------------------------------------------------------------------
- # * Escrita do cabeçalho do arquivo
- # event : Evento sendo exportado
- # file : Arquivo de destino
- #---------------------------------------------------------------------------
- def write_header(event, file)
- # Mágica
- file.write(MAGIC)
- # Versão
- file.write(VERSION.pack('CC'))
- # ID do evento
- write_data([event.id].pack('L'), file)
- # Nome do evento
- write_data(event.name,file)
- # X e Y do evento
- write_data([event.x,event.y].pack('LL'), file)
- # Fim de lote
- write_data(nil,file)
- end
- #---------------------------------------------------------------------------
- # * Escrita das páginas do evento para o arquivo
- # event : Evento sendo exportado
- # file : Arquivo de destino
- #---------------------------------------------------------------------------
- def write_pages(event, file)
- event.pages.each do |page|
- write_page_header(page, file)
- page.list.each do |command|
- write_command(command, file)
- end
- write_data(nil, file)
- end
- end
- #---------------------------------------------------------------------------
- # * Aquisição de um integer a partir de uma array de bools
- # bools : Lista de até 8 booleanos
- #---------------------------------------------------------------------------
- def pack_bool(*bools)
- x = 8
- return bools.inject(0) do |r, v|
- x -= 1
- r + (v ? 1 : 0) * (2**x)
- end
- end
- #---------------------------------------------------------------------------
- # * Escrita do cabeçalho da página num arquivo
- # page : Página do evento sendo exportado
- # file : Arquivo de destino
- #---------------------------------------------------------------------------
- def write_page_header(page, file)
- # Mágica
- file.write(PAGE)
- # Condições da página
- c = page.condition
- i = pack_bool(c.switch1_valid,c.switch2_valid,c.variable_valid,
- c.self_switch_valid, RGSS > 1 ? c.item_valid : false, RGSS > 1 ? c.actor_valid : false)
- write_data(i.chr, file)
- write_data([c.switch1_id, c.switch2_id, c.variable_id, c.variable_value].pack('S4'),file)
- write_data(c.self_switch_ch, file)
- write_data([RGSS > 1 ? c.item_id : 1, RGSS > 1 ? c.actor_id : 1].pack('SS'), file)
- # Gráfico da página
- g = page.graphic
- write_data([g.tile_id].pack('S'), file)
- write_data(g.character_name, file)
- write_data([RGSS > 1 ? g.character_index : 0,g.direction,g.pattern].pack('C3'), file)
- # Movimento
- write_data([page.move_type,page.move_speed,page.move_frequency].pack('C3'), file)
- write_data(move_route(page.move_route), file)
- # Propriedades da página
- i = pack_bool(page.walk_anime,page.step_anime,page.direction_fix,page.through)
- if RGSS > 1
- i += page.priority_type & 0b11
- else
- i += page.always_on_top ? 3 : 0
- end
- write_data([i,page.trigger].pack('CC'), file)
- # Fim de lote
- write_data(nil,file)
- end
- #---------------------------------------------------------------------------
- # * Conversão de uma rota de movimento em texto
- # route : Rota de movimento
- #---------------------------------------------------------------------------
- def move_route(route)
- result = ""
- i = pack_bool(route.repeat, route.skippable, RGSS > 1 ? route.wait : false)
- result << i.chr
- route.list.each do |command|
- result << DATA
- params = command.parameters.collect do |param|
- Marshal.dump(param)
- end
- params << Marshal.dump(nil)
- par_str = params.inject('') do |r, v|
- r += [v.size].pack('S')
- r += v
- end
- result << [par_str.size + 2].pack('S')
- result << [MBS::EventExport.move_command(command.code)].pack('S')
- result << par_str
- end
- result
- end
- #---------------------------------------------------------------------------
- # * Escrita do comando num arquivo
- # command : Comando do evento sendo exportado
- # file : Arquivo de destino
- #---------------------------------------------------------------------------
- def write_command(command, file)
- # Mágica
- file.write(CMD)
- # Código do comando
- write_data([MBS::EventExport.command(command.code), command.indent].pack('SC'), file)
- # Parâmetros do comando
- command.parameters.each do |param|
- write_data(Marshal.dump(param), file)
- end
- # Fim de lote
- write_data(nil,file)
- end
- end
- #==============================================================================
- # ** MBS::EventExport::Reader
- #------------------------------------------------------------------------------
- # Este é o módulo responsável pela leitura do arquivo
- #==============================================================================
- module MBS::EventExport::Reader
- include MBS::EventExport
- module_function
- #--------------------------------------------------------------------------
- # * Leitura de dados do arquivo
- # file : Arquivo de onde os dados serão lidos
- #--------------------------------------------------------------------------
- def read_data(file)
- raise IOError.new('Invalid data chunk') unless file.read(1) == DATA
- size = file.read(4).unpack('L')[0]
- return nil if size == 0
- result = file.read(size)
- return result
- end
- #--------------------------------------------------------------------------
- # * Leitura do cabeçalho do arquivo
- # event : Evento de destino
- # file : Arquivo-fonte do evento
- #--------------------------------------------------------------------------
- def read_header(event, file)
- # Mágica
- raise IOError.new('Invalid event file') unless file.read(4) == MAGIC
- # Versão
- version = file.read(2).unpack('CC')
- raise 'Incompatible event file version' unless version[0] == VERSION[0]
- raise 'Incompatible event file version' if version[1] > VERSION[1]
- # ID, nome e XY
- event.id = read_data(file).unpack('L')[0]
- event.name = read_data(file)
- event.x, event.y = *read_data(file).unpack('LL')
- read_data(file)
- end
- #--------------------------------------------------------------------------
- # * Leitura das páginas do evento
- # event : Evento de destino
- # file : Arquivo-fonte do evento
- #--------------------------------------------------------------------------
- def read_pages(event, file)
- until (c = file.read(1)) == EOF || file.eof?
- if c == PAGE
- file.pos -= 1
- page = RPG::Event::Page.new
- read_page_header(page, file)
- until file.eof?
- c = file.read(1)
- if c == CMD
- file.pos -= 1
- page.list << read_command(file)
- else
- file.pos -= 1
- break if read_data(file) == nil
- end
- end
- event.pages << page
- end
- end
- end
- #--------------------------------------------------------------------------
- # * Aquisição de uma array de booleans por um int
- # n : Número a ser dividido em 8 booleanos
- #--------------------------------------------------------------------------
- def unpack_int(n)
- return [] if n == 0
- bits = 8
- x = 8
- ary = Array.new(bits, false)
- for i in 0..bits
- x -= 1
- ary[i] = n & (1 * (2**x)) != 0
- end
- ary
- end
- #---------------------------------------------------------------------------
- # * Conversão de um texto em rota de movimento
- # str : Texto a ser convertido
- #---------------------------------------------------------------------------
- def move_route(str)
- route = RPG::MoveRoute.new
- route.list = []
- if RGSS == 3
- route.repeat, route.skippable, route.wait = *unpack_int(str[0].unpack('C')[0])
- elsif RGSS == 2
- route.repeat, route.skippable, route.wait = *unpack_int(str[0].chr.unpack('C')[0])
- else
- route.repeat, route.skippable, w = *unpack_int(str[0].chr.unpack('C')[0])
- end
- str = str[1..(str.size-1)]
- while (RGSS == 3 ? str[0] : str[0].to_i.chr) == DATA
- size = str[1..2].unpack('S')[0]
- data = str[3,size]
- command = RPG::MoveCommand.new
- command.code = MBS::EventExport.move_command(data[0..1].unpack('S')[0])
- data = data[2..(data.size-1)]
- params = []
- par = 0
- i = 0
- until par.nil?
- par_sz = data[i,2].unpack('S')[0]
- par = Marshal.load(data[i+2,par_sz])
- params << par
- i += par_sz + 2
- end
- command.parameters = params[0...(data.size-1)]
- route.list << command
- str = str[(size+3)..(str.size-1)]
- end
- route
- end
- #--------------------------------------------------------------------------
- # * Leitura do cabeçalho da página
- # page : Página do evento de destino
- # file : Arquivo-fonte do evento
- #--------------------------------------------------------------------------
- def read_page_header(page, file)
- # Mágica
- raise IOError.new('Invalid page header') unless file.read(1) == PAGE
- # Condição
- page.condition = RPG::Event::Page::Condition.new
- bools = unpack_int(read_data(file).unpack('C')[0])
- bools += [false] * (6 - bools.size)
- c = page.condition
- if RGSS > 1
- c.switch1_valid,c.switch2_valid,c.variable_valid,c.self_switch_valid,c.item_valid,c.actor_valid = *bools
- else
- c.switch1_valid,c.switch2_valid,c.variable_valid,c.self_switch_valid,iv,av = *bools
- end
- ids = read_data(file).unpack('S4')
- c.switch1_id,c.switch2_id,c.variable_id,c.variable_value = *ids
- ch = read_data(file)
- c.self_switch_ch = ch
- ids = read_data(file).unpack('SS')
- if RGSS > 1
- c.item_id,c.actor_id = *ids
- else
- iid,aid = *ids
- end
- # Graphics
- page.graphic = RPG::Event::Page::Graphic.new
- g = page.graphic
- g.tile_id = read_data(file).unpack('S')[0]
- g.character_name = read_data(file)
- if RGSS > 1
- g.character_index,g.direction,g.pattern = *read_data(file).unpack('C3')
- else
- ci,g.direction,g.pattern = *read_data(file).unpack('C3')
- end
- # Movimento
- page.move_type,page.move_speed,page.move_frequency = *read_data(file).unpack('C3')
- page.move_route = move_route(read_data(file))
- # Propriedades da página
- i,page.trigger = read_data(file).unpack('CC')
- page.walk_anime,page.step_anime,page.direction_fix,page.through = *unpack_int(i & 0b11111100)
- if RGSS > 1
- page.priority_type = i & 0b11
- elsif i & 0b11 == 3
- page.always_on_top = true
- end
- # Fim de lote
- read_data(file)
- end
- #--------------------------------------------------------------------------
- # * Leitura do chunk de comando
- # file : Arquivo-fonte do evento
- #--------------------------------------------------------------------------
- def read_command(file)
- # Mágica
- raise IOError.new('Invalid command chunk') unless file.read(1) == CMD
- # Código e identação do comando
- command = RPG::EventCommand.new
- command.code, command.indent = *read_data(file).unpack('SC')
- command.code = MBS::EventExport.to_command(command.code)
- # Parâmetros
- until (obj = read_data(file)) == nil
- command.parameters << Marshal.load(obj)
- end
- command
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement