import War3Map from '../../../parsers/w3x/map' ; import { TriggerData } from '../../../parsers/w3x/wtg/triggerdata' ; import CustomTextTrigger from '../../../parsers/w3x/wct/customtexttrigger' ; import parseWtg from './parsewtg' ; import WeuData , { WEUChange } from './data' ; import { processTrigger } from './processing' ; import { convertTrigger } from './conversions' ; export interface WeuConversionResult { ok : boolean ; error ? : string ; changes ? : WEUChange [ ] ; } export default function convertWeu ( map , customTriggerData , weTriggerData ) { let wts ; let wtg ; let wct ; try { wts = map . readStringTable ( ) ; } catch ( e ) { return { ok : false , error : `Failed to read the string table file: ${ e }` } ; } if ( ! wts ) { return { ok : false , error : `The string table file doesn't exist` } ; } const scriptFile = map . getScriptFile ( ) ; if ( scriptFile && scriptFile . name . endsWith ( '.j' ) ) { try { customTriggerData . addJassFunctions ( scriptFile . text ( ) ) ; } catch ( e ) { } } const data = new WeuData ( customTriggerData , wts ) ; try { wtg = parseWtg ( map , customTriggerData , data ) ; } catch ( e ) { return { ok : false , error : `Failed to read the triggers file: ${ e }` } ; } if ( ! wtg ) { return { ok : false , error : `The triggers file doesn't exist` } ; } try { wct = map . readCustomTextTriggers ( ) ; } catch ( e ) { return { ok : false , error : `Failed to read the custom text triggers file: ${ e }` } ; } if ( ! wct ) { return { ok : false , error : `The custom text triggers file doesn't exist` } ; } const triggers = wtg . triggers ; const customTextTriggers = wct . triggers ; const mapHeader = wct . trigger ; if ( customTextTriggers . length < triggers . length ) { for ( let i = 0 , l = triggers . length - customTextTriggers . length ; i < l ; i ++ ) { customTextTriggers . push ( new CustomTextTrigger ( ) ) ; } } for ( let i = 0 , l = triggers . length ; i < l ; i ++ ) { const trigger = triggers [ i ] ; const callbacks = [ ] ; try { const result = processTrigger ( data , trigger , callbacks ) ; if ( result . convert ) { data . push ( trigger ) ; let body = convertTrigger ( data , trigger , callbacks ) ; if ( callbacks . length ) { body = `${ callbacks . join ( '\r\n' ) }\r\n${ body }` ; } customTextTriggers [ i ] . text = body ; trigger . ecas . length = 0 ; trigger . isCustom = 1 ; data . change ( 'convertedtrigger' , result . reason , customTextTriggers [ i ] . text ) ; data . pop ( ) ; } else if ( callbacks . length ) { const callbacksText = callbacks . join ( '\r\n' ) ; mapHeader . text += `// Callbacks generated for trigger "${ trigger . name }" due to conversions\r\n${ callbacksText }\r\n` ; data . change ( 'generatedcallbacks' , trigger . name , callbacksText ) ; } } catch ( e ) { return { ok : false , error : `Error at ${ data . stackToString ( ) }: ${ e }` } ; } } data . saveGUIReferences ( triggers , customTextTriggers ) ; map . set ( 'war3map.wtg' , wtg . save ( ) ) ; map . set ( 'war3map.wct' , wct . save ( ) ) ; try { wtg = map . readTriggers ( weTriggerData ) ; } catch ( e ) { return { ok : false , error : `Failed to validate the triggers file: ${ e }` } ; } if ( ! wtg ) { return { ok : false , error : `Failed to re-read the triggers file` } ; } return { ok : true , changes : data . changes } ; }