import Trigger from '../../../parsers/w3x/wtg/trigger' ; import ECA from '../../../parsers/w3x/wtg/eca' ; import Parameter from '../../../parsers/w3x/wtg/parameter' ; import SubParameters from '../../../parsers/w3x/wtg/subparameters' ; import WeuData from './data' ; import { createCustomScriptECA , convertSingleToMultiple , isConditionECA , ensureCustomScriptCodeSafety } from './utils' ; import { convertFunctionCall } from './conversions' ; import transformFunction from './transformations/functions' ; import transformPreset from './transformations/presets' ; interface ConvertionResult { convert : boolean ; reason : string ; } export function processTrigger ( data , trigger , callbacks ) { data . push ( trigger ) ; const eventsAndConditions = [ ] ; const actions = [ ] ; for ( const eca of trigger . ecas ) { const type = eca . type ; if ( type === 0 || type === 1 ) { eventsAndConditions . push ( eca ) ; } else if ( type === 2 ) { actions . push ( eca ) ; } } const outputEcas = [ ] ; for ( const eventOrCondition of eventsAndConditions ) { const result = processECA ( data , eventOrCondition , callbacks ) ; if ( result . convert ) { data . pop ( ) ; return result ; } outputEcas . push ( eventOrCondition ) ; } for ( const action of actions ) { const result = processECA ( data , action , callbacks ) ; if ( result . convert ) { const customScripts = convertFunctionCall ( data , action , callbacks ) ; data . change ( 'inlinecustomscript' , result . reason , customScripts . map ( ( eca ) => eca . parameters [ 0 ] . value ) . join ( '\n' ) ) ; outputEcas . push ( ... customScripts ) ; } else { outputEcas . push ( action ) ; } } trigger . ecas = ensureCustomScriptCodeSafety ( outputEcas ) ; data . pop ( ) ; return { convert : false , reason : '' } ; } export function processECA ( data , eca , callbacks ) { data . push ( eca ) ; const result = processFunctionCall ( data , eca , callbacks ) ; if ( result . convert ) { const reason = result . reason ; if ( convertSingleToMultiple ( eca ) ) { const result = processFunctionCall ( data , eca , callbacks ) ; if ( result . convert ) { data . pop ( ) ; return result ; } else { data . change ( 'singletomultiple' , reason , eca . name ) ; } } else { data . pop ( ) ; return result ; } } const outputEcas = [ ] ; for ( const child of eca . ecas ) { const result = processECA ( data , child , callbacks ) ; if ( result . convert ) { let customScripts ; if ( isConditionECA ( eca . name , child . group ) ) { let condition = convertFunctionCall ( data , child , callbacks ) [ 0 ] . parameters [ 0 ] . value ; if ( condition . startsWith ( 'call ' ) ) { condition = condition . slice ( 5 ) ; } let finalCondition ; let returnValue ; if ( eca . name === 'OrMultiple' ) { finalCondition = condition ; returnValue = 'true' ; } else { finalCondition = `not (${ condition })` ; returnValue = 'false' ; } customScripts = [ createCustomScriptECA ( `if ${ finalCondition } then` ) , createCustomScriptECA ( `return ${ returnValue }` ) , createCustomScriptECA ( 'endif' ) , ] ; } else { customScripts = convertFunctionCall ( data , child , callbacks ) ; } for ( const script of customScripts ) { script . group = child . group ; } data . change ( 'inlinecustomscript' , result . reason , customScripts . map ( ( eca ) => eca . parameters [ 0 ] . value ) . join ( '\n' ) ) ; outputEcas . push ( ... customScripts ) ; } else { outputEcas . push ( child ) ; } } eca . ecas = ensureCustomScriptCodeSafety ( outputEcas ) ; data . pop ( ) ; return { convert : false , reason : '' } ; } function processFunctionCall ( data , object , callbacks ) { const name = object . name ; if ( transformFunction ( data , object ) ) { data . change ( 'inlinegui' , name , object . name ) ; } if ( ! data . triggerData . isBaseFunction ( object . type , object . name ) ) { return { convert : true , reason : object . name } ; } for ( const parameter of object . parameters ) { if ( parameter . type === 0 && data . triggerData . isCustomPreset ( parameter . value ) ) { const value = parameter . value ; if ( transformPreset ( data , parameter ) ) { data . change ( 'inlinepreset' , value , parameter . value ) ; } else { return { convert : true , reason : value } ; } } const result = processParameter ( data , parameter , callbacks ) ; if ( result . convert ) { return result ; } } return { convert : false , reason : '' } ; } function processParameter ( data , parameter , callbacks ) { data . push ( parameter ) ; const type = parameter . type ; const value = parameter . value ; if ( type === 1 ) { if ( value . startsWith ( 'gg_' ) ) { data . updateGUIReference ( value , true ) ; } } else if ( type === 2 ) { const result = processSubParameters ( data , < SubParameters > parameter . subParameters , callbacks ) ; if ( result . convert ) { data . pop ( ) ; return result ; } } const index = parameter . arrayIndex ; if ( index ) { const result = processParameter ( data , index , callbacks ) ; if ( result . convert ) { data . pop ( ) ; return result ; } } data . pop ( ) ; return { convert : false , reason : '' } ; } function processSubParameters ( data , subParameters , callbacks ) { data . push ( subParameters ) ; const result = processFunctionCall ( data , subParameters , callbacks ) ; if ( result . convert ) { data . pop ( ) ; return result ; } data . pop ( ) ; return { convert : false , reason : '' } ; }