/**
  * @param string $scriptName
  * @param TES4VariableDeclarationList $variableList
  * @param TES4CodeChunks $script
  * @return TES5Script
  * @throws ConversionException
  */
 public function convert($scriptName, TES4VariableDeclarationList $variableList, TES4CodeChunks $script)
 {
     //Create the header.
     $scriptHeader = new TES5ScriptHeader($this->nameTransformer->transform($scriptName, ''), $scriptName, TES5BasicType::T_TOPICINFO(), '', true);
     $globalScope = new TES5GlobalScope($scriptHeader);
     foreach ($this->esmAnalyzer->getGlobalVariables() as $globalVariable) {
         $globalScope->addGlobalVariable($globalVariable);
     }
     if ($variableList !== null) {
         //Converting the variables to the properties.
         $this->propertiesFactory->createProperties($variableList, $globalScope);
     }
     $fragment = $this->fragmentFactory->createFragment(TES5FragmentType::T_TIF(), "Fragment_0", $globalScope, new TES5MultipleScriptsScope([$globalScope]), $script);
     $blockList = new TES5BlockList();
     $blockList->add($fragment);
     return new TES5Script($scriptHeader, $globalScope, $blockList);
 }
 /**
  * @param TES4ScriptCollection $scripts
  * @return TES5ScriptCollection
  * @throws ConversionException
  */
 public function convert(TES4ScriptCollection $scripts)
 {
     $transpiledCollection = new TES5ScriptCollection();
     $scriptHeaders = [];
     $globalScopes = [];
     /**
      * @var TES4Target $scriptTarget
      */
     foreach ($scripts->getIterator() as $k => $scriptTarget) {
         $script = $scriptTarget->getScript();
         //Create the header.
         $scriptHeader = $this->createHeader($script);
         $variableList = $script->getVariableDeclarationList();
         $globalScope = new TES5GlobalScope($scriptHeader);
         foreach ($this->esmAnalyzer->getGlobalVariables() as $globalVariable) {
             $globalScope->addGlobalVariable($globalVariable);
         }
         if ($variableList !== null) {
             $this->propertiesFactory->createProperties($variableList, $globalScope);
         }
         $scriptHeaders[$k] = $scriptHeader;
         $globalScopes[$k] = $globalScope;
     }
     //Add the static global scopes which are added by complimenting scripts..
     $staticGlobalScopes = $this->staticGlobalScopesFactory->createGlobalScopes();
     foreach ($staticGlobalScopes as $staticGlobalScope) {
         $globalScopes[] = $staticGlobalScope;
     }
     $multipleScriptsScope = new TES5MultipleScriptsScope($globalScopes);
     /**
      * @var TES4Target $scriptTarget
      */
     foreach ($scripts->getIterator() as $k => $scriptTarget) {
         $scriptHeader = $scriptHeaders[$k];
         $globalScope = $globalScopes[$k];
         $script = $scriptTarget->getScript();
         $blockList = new TES5BlockList();
         $parsedBlockList = $script->getBlockList();
         $createdBlocks = [];
         if ($parsedBlockList !== null) {
             foreach ($parsedBlockList->getBlocks() as $block) {
                 $newBlockList = $this->blockFactory->createBlock($multipleScriptsScope, $globalScope, $block);
                 foreach ($newBlockList->getBlocks() as $newBlock) {
                     if (!isset($createdBlocks[$newBlock->getBlockType()])) {
                         $createdBlocks[$newBlock->getBlockType()] = [];
                     }
                     $createdBlocks[$newBlock->getBlockType()][] = $newBlock;
                 }
             }
         }
         //todo encapsulate it to a different class.
         foreach ($createdBlocks as $blockType => $blocks) {
             if (count($blocks) > 1) {
                 /**
                  * @var TES5FunctionCodeBlock[] $functions
                  */
                 $functions = [];
                 $i = 1;
                 $localScopeArguments = null;
                 /**
                  * @var TES5EventCodeBlock $block
                  */
                 foreach ($blocks as $block) {
                     $function = new TES5FunctionCodeBlock($blockType . "_" . $i, null, $block->getLocalScope(), $block->getCodeScope());
                     $functions[] = $function;
                     if ($localScopeArguments === null) {
                         $localScopeArguments = new TES5ObjectCallArguments();
                         foreach ($block->getLocalScope()->getLocalVariables() as $localVariable) {
                             $localScopeArguments->add($this->referenceFactory->createReferenceToVariable($localVariable));
                         }
                     }
                     ++$i;
                 }
                 //Create the proxy block.
                 $proxyBlock = $this->blockFactory->createNewBlock($blockType, clone $block->getLocalScope());
                 foreach ($functions as $function) {
                     $blockList->add($function);
                     $functionCall = $this->valueFactory->createObjectCall($this->referenceFactory->createReferenceToSelf($globalScope), $function->getFunctionName(), $multipleScriptsScope, $localScopeArguments, false);
                     $proxyBlock->addChunk($functionCall);
                 }
                 $blockList->add($proxyBlock);
             } else {
                 $block = current($blocks);
                 $blockList->add($block);
             }
         }
         $tesScript = new TES5Script($scriptHeader, $globalScope, $blockList);
         $transpiledCollection->add($tesScript, $scriptTarget->getOutputPath());
     }
     return $transpiledCollection;
 }