示例#1
0
文件: Xml.php 项目: samilesma/Dice
 private function loadV2(\SimpleXmlElement $xml, \Dice\Dice $dice)
 {
     foreach ($xml as $key => $value) {
         $rule = $dice->getRule((string) $value->name);
         if ($value->call) {
             foreach ($value->call as $name => $call) {
                 $callArgs = [];
                 foreach ($call->children() as $key => $param) {
                     $callArgs[] = $this->getComponent($param);
                 }
                 $rule['call'][] = [(string) $call['method'], $callArgs];
             }
         }
         if (isset($value['inherit'])) {
             $rule['inherit'] = $value['inherit'] == 'false' ? false : true;
         }
         if ($value['instanceOf']) {
             $rule['instanceOf'] = (string) $value['instanceOf'];
         }
         if (isset($value['shared'])) {
             $rule['shared'] = (string) $value['shared'] === 'true';
         }
         if ($value->constructParams) {
             foreach ($value->constructParams->children() as $child) {
                 $rule['constructParams'][] = $this->getComponent($child);
             }
         }
         if ($value->substitute) {
             foreach ($value->substitute as $use) {
                 $rule['substitutions'][(string) $use['as']] = $this->getComponent($use['use'], true);
             }
         }
         if ($value->shareInstances) {
             foreach ($value->shareInstances->children() as $share) {
                 $rule['shareInstances'][] = $this->getComponent($share);
             }
         }
         $dice->addRule((string) $value['name'], $rule);
     }
 }
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     set_time_limit(10800);
     // 3 hours is the maximum for this command. Need more? You really screwed something, full suite for all Oblivion vanilla data takes 20 minutes. :)
     $mode = $input->getArgument('mode');
     switch ($mode) {
         case "sloppy":
             $threshold = 0.5;
             break;
         case "normal":
             $threshold = 0.85;
             break;
         case "strict":
         default:
             $threshold = 0.95;
             break;
         case "perfect":
             $threshold = 1;
             break;
     }
     $skipParsing = $input->getOption('skip-parsing');
     if (!$skipParsing) {
         $parser = new \Ormin\OBSLexicalParser\TES4\Parser\SyntaxErrorCleanParser(new \Ormin\OBSLexicalParser\TES4\Parser\TES4ObscriptCodeGrammar());
         #        $parser = new Parser(new TES4OBScriptGrammar());
         $dice = new Dice();
         $rule = new \Dice\Rule();
         $rule->shared = true;
         $rule->shareInstances = ['Ormin\\OBSLexicalParser\\TES4\\Context\\ESMAnalyzer'];
         $dice->addRule('Ormin\\OBSLexicalParser\\TES5\\Converter\\TES4ToTES5ASTTIFFragmentConverter', $rule);
         $dice->addRule('Ormin\\OBSLexicalParser\\TES5\\Factory\\TES5TypeFactory', $rule);
         /**
          * @var \Ormin\OBSLexicalParser\TES5\Converter\TES4ToTES5ASTConverter $converter
          */
         $converter = $dice->create("Ormin\\OBSLexicalParser\\TES5\\Converter\\TES4ToTES5ASTTIFFragmentConverter");
         $inputFolder = './Fragments/TIF/fragments/';
         $outputFolder = './Fragments/TIF/PapyrusFragments/';
         $scandir = scandir($inputFolder);
         $success = 0;
         $total = 0;
         $f = fopen("php://stderr", 'r+');
         $ASTTable = [];
         $output->writeln("Lexing and parsing..");
         $totalNumber = count($scandir) - 2;
         foreach ($scandir as $scriptPath) {
             if ($scriptPath == '.' || $scriptPath == '..') {
                 continue;
             }
             if (substr($scriptPath, -4) != ".txt") {
                 continue;
             }
             if ($total % 10 == 0) {
                 $output->writeln($total . " / " . $totalNumber . " ...");
             }
             $outputScriptPath = substr($scriptPath, 0, -4) . '.psc';
             $path = $inputFolder . $scriptPath;
             ++$total;
             try {
                 $scriptName = substr($scriptPath, 0, -4);
                 $output->writeln($scriptName . ' ...');
                 $lexer = new \Ormin\OBSLexicalParser\TES4\Lexer\FragmentLexer();
                 $tokens = $lexer->lex(file_get_contents($path));
                 $variableList = $this->fragmentsReferencesBuilder->buildVariableDeclarationList($inputFolder . $scriptName . '.references');
                 $AST = $parser->parse($tokens);
                 $ASTTable[$scriptPath] = $AST;
                 $TES5AST = $converter->convert($scriptName, $variableList, $AST);
                 $outputScript = $TES5AST->output();
                 file_put_contents($outputFolder . $outputScriptPath, $outputScript);
                 system('lua "Utilities/beautifier.lua" "' . $outputFolder . $outputScriptPath . '"');
                 ++$success;
             } catch (\Exception $e) {
                 fwrite($f, $scriptPath . PHP_EOL . get_class($e) . PHP_EOL . $e->getMessage() . PHP_EOL . PHP_EOL);
                 continue;
             }
         }
         fclose($f);
         $successRate = $success / $total;
         if ($successRate < $threshold) {
             $percent = round($successRate * 100);
             $output->writeln("ERROR: Build failed on parsing step in " . $mode . " mode. The rate is " . $success . "/" . $total . " ( " . $percent . " %)");
             return;
         }
         $output->writeln("Parsing in " . $mode . " mode succedeed ( rate " . $success . "/" . $total . " ), copying Skyrim scripts and parsed papyrus fragments to build folder...");
     }
     $output->writeln("Build in " . $mode . " mode succeeded!");
 }