Пример #1
0
        public void RunTask(StreamWriter errorLog, ProgressWriter progressWriter)
        {
            foreach (var buildChunk in this.buildPlan)
            {
                Dictionary <string, TES5GlobalScope> scriptsScopes = new Dictionary <string, TES5GlobalScope>();
                TES5GlobalVariables globalVariables = this.esmAnalyzer.GlobalVariables;

                /*
                 * First, build the scripts global scopes
                 */
                foreach (var kvp in buildChunk)
                {
                    var         buildTargetName = kvp.Key;
                    var         buildScripts    = kvp.Value;
                    BuildTarget buildTarget     = this.GetBuildTarget(buildTargetName);
                    foreach (var buildScript in buildScripts)
                    {
                        string scriptName = Path.GetFileNameWithoutExtension(buildScript);
                        string sourcePath = buildTarget.GetSourceFromPath(scriptName);
                        scriptsScopes.Add(scriptName, buildTarget.BuildScope(sourcePath, globalVariables));
                    }
                }

                //Add the static global scopes which are added by complimenting scripts..
                List <TES5GlobalScope> staticGlobalScopes = TES5StaticGlobalScopesFactory.CreateGlobalScopes();
                //WTM:  Change:  In the PHP, scriptsScopes is used as a dictionary above but as a list below.  I have added the "GlobalScope"+n key to ameliorate this.
                int globalScopeIndex = 0;
                foreach (var staticGlobalScope in staticGlobalScopes)
                {
                    scriptsScopes.Add("GlobalScope" + globalScopeIndex.ToString(), staticGlobalScope);
                    globalScopeIndex++;
                }

                TES5MultipleScriptsScope multipleScriptsScope = new TES5MultipleScriptsScope(scriptsScopes.Values, globalVariables);
                //Dictionary<string, TES5Target> convertedScripts = new Dictionary<string, TES5Target>();
                foreach (var kvp in buildChunk)
                {
                    var buildTargetName = kvp.Key;
                    var buildScripts    = kvp.Value;
                    foreach (var buildScript in buildScripts)
                    {
                        BuildTarget     buildTarget = this.GetBuildTarget(buildTargetName);
                        string          scriptName  = Path.GetFileNameWithoutExtension(buildScript);
                        TES5GlobalScope globalScope = scriptsScopes[scriptName];
                        string          sourcePath  = buildTarget.GetSourceFromPath(scriptName);
                        string          outputPath  = buildTarget.GetTranspileToPath(scriptName);
                        TES5Target      convertedScript;
                        try
                        {
                            convertedScript = buildTarget.Transpile(sourcePath, outputPath, globalScope, multipleScriptsScope);
                        }
                        catch (EOFOnlyException) { continue; }//Ignore files that are only whitespace or comments.
#if !DEBUG || LOG_EXCEPTIONS
                        catch (ConversionException ex) when(ex.Expected)
                        {
                            errorLog.Write(scriptName + " (" + sourcePath + ")" + Environment.NewLine + ex.GetType().FullName + Environment.NewLine + ex.Message + Environment.NewLine + Environment.NewLine);
                            continue;
                        }
#endif
                        this.buildTracker.RegisterBuiltScript(buildTarget, convertedScript);
                        //convertedScripts.Add(buildScript, convertedScript);
                        progressWriter.IncrementAndWrite();
                    }
                }

                /*foreach (var kvp2 in convertedScripts)
                 * {
                 *  var originalScriptName = kvp2.Key;
                 *  Console.WriteLine("Script Complete:  " + originalScriptName);
                 * }*/
            }
        }
        /*
         * Joins N QF subfragments into one QF fragment that can be properly binded into Skyrim VM
         * @throws ConversionException
         */
        public TES5Target JoinQFFragments(BuildTarget target, string resultingFragmentName, List <QuestStageScript> subfragmentsTrees)
        {
            StageMap stageMap = BuildStageMap(target, resultingFragmentName);

            /*
             * We need script fragment for objective handling for each stage, so when parsing the script fragments,
             * we"ll be marking them there, and intersecting this with stage.
             * This will give us an array of stages which don"t have script fragment, but will need it anyways
             * for objective handling.
             */
            TES5ScriptHeader resultingScriptHeader = new TES5ScriptHeader(resultingFragmentName, TES5BasicType.T_QUEST, "", true);
            TES5BlockList    resultingBlockList    = new TES5BlockList();
            TES5GlobalScope  resultingGlobalScope  = new TES5GlobalScope(resultingScriptHeader);

            /*
             * Add ReferenceAlias"es
             * At some point, we might port the conversion so it doesn"t use the directly injected property,
             * but instead has a map to aliases and we"ll map accordingly and have references point to aliases instead
             */
            string sourcePath  = target.GetSourceFromPath(resultingFragmentName);
            string scriptName  = Path.GetFileNameWithoutExtension(sourcePath);
            string aliasesFile = Path.Combine(Path.GetDirectoryName(sourcePath), scriptName + ".aliases");

            string[] aliasesLines = File.ReadAllLines(aliasesFile);
            Dictionary <string, bool> aliasesDeclared = new Dictionary <string, bool>();

            foreach (var alias in aliasesLines)
            {
                string trimmedAlias = alias.Trim();
                if (trimmedAlias == "")
                {
                    continue;
                }
                try
                {
                    aliasesDeclared.Add(trimmedAlias, true);
                }
                catch (ArgumentException) { continue; }
                resultingGlobalScope.AddProperty(new TES5Property(trimmedAlias, TES5BasicType.T_REFERENCEALIAS, trimmedAlias));
            }

            Dictionary <int, bool>    implementedStages       = new Dictionary <int, bool>();
            Dictionary <string, bool> propertiesNamesDeclared = new Dictionary <string, bool>();

            foreach (var subfragment in subfragmentsTrees)
            {
                TES5Target      subfragmentsTree       = subfragment.Script;
                TES5Script      subfragmentScript      = subfragmentsTree.Script;
                TES5GlobalScope subfragmentGlobalScope = subfragmentScript.GlobalScope;
                foreach (TES5Property subfragmentProperty in subfragmentGlobalScope.Properties)
                {
                    /*
                     * Move over the properties to the new global scope
                     */
                    string propertyName;
                    if (propertiesNamesDeclared.ContainsKey(subfragmentProperty.Name))
                    {
                        propertyName = GeneratePropertyName(subfragmentScript.ScriptHeader, subfragmentProperty);
                        subfragmentProperty.Rename(propertyName);
                    }
                    else
                    {
                        propertyName = subfragmentProperty.Name;
                    }

                    propertiesNamesDeclared.Add(propertyName, true);
                    resultingGlobalScope.AddProperty(subfragmentProperty);
                    //WTM:  Note:  See QF_FGD03Viranus_0102d154.  Since ViranusDontonREF is present in multiple of the original fragments,
                    //ViranusDontonREF gets renamed by the above.  So multiple ViranusDontonREF variables are output.
                    //Below I tried not renaming, assuming instead that variables with matching names and types within a set of fragments were intended to be the same variable.
                    //It had OK results, but I'm leaving it commented for now.

                    /*string propertyNameWithSuffix = subfragmentProperty.PropertyNameWithSuffix;
                     * TES5Property existingProperty = resultingGlobalScope.Properties.Where(p => p.PropertyNameWithSuffix == propertyNameWithSuffix).FirstOrDefault();
                     * if (existingProperty != null && TES5InheritanceGraphAnalyzer.isExtending(subfragmentProperty.PropertyType, existingProperty.PropertyType))
                     * {
                     *  existingProperty.PropertyType = subfragmentProperty.PropertyType;
                     * }
                     * else
                     * {
                     *  bool add = true;
                     *  if (existingProperty != null)
                     *  {
                     *      if (TES5InheritanceGraphAnalyzer.isExtending(existingProperty.PropertyType, subfragmentProperty.PropertyType))
                     *      {
                     *          add = false;
                     *      }
                     *      else
                     *      {
                     *          string generatedPropertyName = generatePropertyName(subfragmentScript.ScriptHeader, subfragmentProperty, i);
                     *          subfragmentProperty.Rename(generatedPropertyName);
                     *      }
                     *  }
                     *  if (add)
                     *  {
                     *      resultingGlobalScope.Add(subfragmentProperty);
                     *  }
                     * }*/
                }

                List <ITES5CodeBlock> subfragmentBlocks = subfragmentScript.BlockList.Blocks;
                if (subfragmentBlocks.Count != 1)
                {
                    throw new ConversionException("Wrong QF fragment, actual function count: " + subfragmentBlocks.Count + "..");
                }

                ITES5CodeBlock subfragmentBlock = subfragmentBlocks[0];
                if (subfragmentBlock.FunctionScope.BlockName != "Fragment_0")
                {
                    throw new ConversionException("Wrong QF fragment funcname, actual function name: " + subfragmentBlock.FunctionScope.BlockName + "..");
                }

                string newFragmentFunctionName = "Fragment_" + subfragment.Stage.ToString();
                if (subfragment.LogIndex != 0)
                {
                    newFragmentFunctionName += "_" + subfragment.LogIndex;
                }

                subfragmentBlock.FunctionScope.Rename(newFragmentFunctionName);
                var objectiveCodeChunks = this.objectiveHandlingFactory.GenerateObjectiveHandling(subfragmentBlock, resultingGlobalScope, stageMap.GetStageTargetsMap(subfragment.Stage));
                foreach (var newCodeChunk in objectiveCodeChunks)
                {
                    subfragmentBlock.AddChunk(newCodeChunk);
                }

                resultingBlockList.Add(subfragmentBlock);
                implementedStages[subfragment.Stage] = true;
            }

            /*
             * Diff to find stages which we still need to mark
             */
            int[] nonDoneStages = stageMap.StageIDs.Where(stageID => !implementedStages.ContainsKey(stageID)).ToArray();
            foreach (int nonDoneStage in nonDoneStages)
            {
                TES5FunctionCodeBlock fragment = this.objectiveHandlingFactory.CreateEnclosedFragment(resultingGlobalScope, nonDoneStage, stageMap.GetStageTargetsMap(nonDoneStage));
                resultingBlockList.Add(fragment);
            }

            this.mappedTargetsLogService.WriteScriptName(resultingFragmentName);
            foreach (var kvp in stageMap.MappedTargetsIndex)
            {
                var originalTargetIndex = kvp.Key;
                var mappedTargetIndexes = kvp.Value;
                this.mappedTargetsLogService.WriteLine(originalTargetIndex, mappedTargetIndexes);
            }

            TES5Script resultingTree = new TES5Script(resultingGlobalScope, resultingBlockList);
            string     outputPath    = target.GetTranspileToPath(resultingFragmentName);

            return(new TES5Target(resultingTree, outputPath));
        }