#!/usr/bin/env python
# -*- coding: windows-1252 -*-
'''
Created on 12/11/2013

@author: Hansen
'''
import struct

import pyml.lib
import pyml.handler
from pytable import normal_table

from controller import *  # @UnusedWildImport

LNG_ENGLISH = 0
LNG_FRENCH = 1
LNG_GERMAN = 2
LNG_ITALIAN = 3
LNG_SPANISH = 4

CURRENT_LNG = LNG_ENGLISH

dialog_counter = 0

def ExtractOther( pos ):
    fd.seek( pos, 0 )

    text = ""
    line = 1
    textValue = TextEntry( fd )
    while True:
        c = fd.read( 1 )
        # always break if NULL
        if c == "\x00":
            if text:
                textValue.AddValue( text )
                text = ""
            break
        else:
            text += c
    return textValue

def ExtractDialog( pos ):
    global dialog_counter
    fd.seek( pos, 0 )

    dialog_counter += 1
    dialogEntry = DialogEntryTag( fd, id = dialog_counter )

    text = ""
    line = 1
    while True:
        c = fd.read( 1 )
        if IsTag( c ):
            descriptor = fd.read( 1 )
            tag = GetTag( descriptor )( fd, line = line )
            if isinstance( tag, EndDialogTag ):
                if text:
                    textValue = TextEntry( fd, line = line )
                    textValue.AddValue( text )
                    dialogEntry.AddChild( textValue )
                    text = ""
                break
            elif isinstance( tag, NewLineTag ):
                if text:
                    textValue = TextEntry( fd, line = line )
                    textValue.AddValue( text )
                    dialogEntry.AddChild( textValue )
                    text = ""
                line += 1
            elif isinstance( tag, NewDialogTag ):
                if text:
                    textValue = TextEntry( fd, line = line )
                    textValue.AddValue( text )
                    dialogEntry.AddChild( textValue )
                    text = ""
                dialogEntry.AddChild( tag )
                line = 1
            else:
                if text:
                    textValue = TextEntry( fd, line = line )
                    textValue.AddValue( text )
                    dialogEntry.AddChild( textValue )
                    text = ""
                dialogEntry.AddChild( tag )
        else:
            text += c

    return dialogEntry


ptr_table = [ ( 0x4e8898, 0x4f46c0, ExtractDialog, "MainTexts" ) ,
             ( 0x3bb7f4, 0x3bbbdc, ExtractOther, "Items1" ) ,
             ( 0x3bbf60, 0x3bc6b8, ExtractOther, "Items2" ) ,
             ( 0x3bcdf4, 0x3bd4d4, ExtractOther, "Items3" ) ,
             ( 0x3bdbb4, 0x3be2e4, ExtractOther, "Items4" ) ,
             ( 0x3bea14, 0x3beb2c, ExtractOther, "Items5" ),
             ( 0x3bebf0, 0x3bf0f0, ExtractOther, "Attacks" ),
             ( 0x3bf470, 0x3bfc2c, ExtractOther, "Locals" ),
             ( 0x3c07a4, 0x3c160c, ExtractOther, "Null" )]
def Extract( fd ):

    # buffer pointer table
    for begin, end, func, name in ptr_table:
        fd.seek( begin, 0 )
        total = end - begin
        pointers = filter( lambda i: i != 0, map( lambda i: i & 0xffffff, struct.unpack( "<%dL" % ( total >> 2 ), fd.read( total ) ) ) )

        # ok, extract text now
        if func == ExtractDialog:
            entries = map( func, [ptr for i, ptr in enumerate( pointers ) if ( i % 5 == CURRENT_LNG )] )
        else:
            ff = range( len( pointers ) / 5 * CURRENT_LNG, len( pointers ) / 5 * ( CURRENT_LNG + 1 ) )
            entries = map( func, [ptr for i, ptr in enumerate( pointers ) if ( i in ff )] )

        xml = pyml.handler.Handler( name = None, root = "SuperstarSagaScript" )
        for i, e in enumerate( entries ):
            xml.hXML[e.Name] = e

        xml.SaveTo( "../../Textos Originais/{0}.xml".format( name ) )




if __name__ == "__main__":

    with open( "../../1277 - Mario & Luigi - Superstar Saga (UA).gba", "rb" ) as fd:
        Extract( fd )



