public static Dictionary <int, List <int> > BuildStageMapDictionary(BuildTarget target, string resultingFragmentName)
        {
            string sourcePath = target.GetSourceFromPath(resultingFragmentName);
            //ToLower() is needed for Linux's case-sensitive file system since these files seem to all be lowercase.
            string scriptName   = Path.GetFileNameWithoutExtension(sourcePath).ToLower();
            string stageMapFile = Path.Combine(Path.GetDirectoryName(sourcePath), scriptName + ".map");

            string[] stageMapFileLines             = File.ReadAllLines(stageMapFile);
            Dictionary <int, List <int> > stageMap = new Dictionary <int, List <int> >();

            foreach (var stageMapLine in stageMapFileLines)
            {
                string[] numberAndItemsSplit = stageMapLine.Split('-');
                int      stageId             = int.Parse(numberAndItemsSplit[0].Trim());

                /*
                 * Clear the rows
                 */
                string[]   items     = numberAndItemsSplit[1].Split(' ');
                List <int> stageRows = new List <int>();
                foreach (string item in items)
                {
                    string itemTrimmed = item.Trim();
                    if (itemTrimmed != "")
                    {
                        stageRows.Add(int.Parse(itemTrimmed));
                    }
                }

                stageMap.Add(stageId, stageRows);
            }

            return(stageMap);
        }
Esempio n. 2
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));
        }
        public void Execute(string targets)
        {
            if (!PreExecutionChecks(true, true, false, false))
            {
                return;
            }
            Directory.CreateDirectory(DataDirectory.GetGraphDirectoryPath());
            Build build = new Build(Build.DEFAULT_BUILD_PATH); //This argument might well not be important in this case

            using (BuildLogServices buildLogServices = new BuildLogServices(build))
            {
                BuildTargetCollection buildTargets = BuildTargetFactory.GetCollection(targets, build, buildLogServices);
                //if (!buildTargets.CanBuildAndWarnIfNot()) { return; }//WTM:  Change:  This doesn't matter for building graphs.
                Dictionary <string, List <string> > dependencyGraph = new Dictionary <string, List <string> >();
                Dictionary <string, List <string> > usageGraph      = new Dictionary <string, List <string> >();
                BuildSourceFilesCollection          sourceFiles     = buildTargets.GetSourceFiles();
                ProgressWriter     progressWriter = new ProgressWriter("Building Interoperable Compilation Graph", buildTargets.GetTotalSourceFiles());
                TES5TypeInferencer inferencer     = new TES5TypeInferencer(new ESMAnalyzer(), BuildTarget.StandaloneSourcePath);
                using (StreamWriter errorLog = new StreamWriter(TES5ScriptDependencyGraph.ErrorLogPath, false))
                {
                    using (StreamWriter debugLog = new StreamWriter(TES5ScriptDependencyGraph.DebugLogPath, false))
                    {
                        foreach (var kvp in sourceFiles)
                        {
                            var         buildTargetName  = kvp.Key;
                            var         sourceBuildFiles = kvp.Value;
                            BuildTarget buildTarget      = buildTargets.GetByName(buildTargetName);
                            foreach (var sourceFile in sourceBuildFiles)
                            {
                                string scriptName = sourceFile.Substring(0, sourceFile.Length - 4);
                                ITES4CodeFilterable AST;
                                try
                                {
                                    AST = buildTarget.GetAST(buildTarget.GetSourceFromPath(scriptName));
                                }
                                catch (EOFOnlyException) { continue; }//Ignore files that are only whitespace or comments.

                                /*catch (UnexpectedTokenException ex)
                                 * {//Exceptions no longer occur, so this code should not be invoked.
                                 *  errorLog.WriteLine(sourceFile + ":  " + ex.Message + Environment.NewLine);
                                 *  continue;
                                 * }*/
                                List <TES4ObjectProperty> propertiesAccesses = new List <TES4ObjectProperty>();
                                AST.Filter((data) =>
                                {
                                    TES4ObjectProperty property = data as TES4ObjectProperty;
                                    if (property == null)
                                    {
                                        return(false);
                                    }
                                    propertiesAccesses.Add(property);
                                    return(true);
                                });
                                Dictionary <string, TES5Property> preparedProperties      = new Dictionary <string, TES5Property>();
                                Dictionary <string, ITES5Type>    preparedPropertiesTypes = new Dictionary <string, ITES5Type>();
                                foreach (var property in propertiesAccesses)
                                {
                                    Match        match           = TES5ReferenceFactory.PropertyNameRegex.Match(property.StringValue);
                                    string       propertyName    = match.Groups[1].Value;
                                    string       propertyKeyName = propertyName.ToLower();
                                    bool         containedKey;
                                    TES5Property preparedProperty = preparedProperties.GetOrAdd(propertyKeyName, () => new TES5Property(propertyName, TES5BasicType.T_FORM, propertyName), out containedKey);
                                    ITES5Type    inferencingType  = inferencer.ResolveInferenceTypeByReferenceEdid(preparedProperty);
                                    if (!containedKey)
                                    {
                                        preparedPropertiesTypes.Add(propertyKeyName, inferencingType);
                                    }
                                    else
                                    {
                                        if (!inferencingType.Equals(preparedPropertiesTypes[propertyKeyName]))
                                        {
                                            throw new ConversionException("Cannot settle up the properties types - conflict.");
                                        }
                                    }
                                }

                                debugLog.WriteLine(scriptName + " - " + preparedProperties.Count + " prepared");
                                string lowerScriptType = scriptName.ToLower();
                                foreach (var kvp2 in preparedProperties)
                                {
                                    var    preparedPropertyKey = kvp2.Key;
                                    var    preparedProperty    = kvp2.Value;
                                    string propertyTypeName    = preparedPropertiesTypes[preparedPropertyKey].OriginalName;
                                    //Only keys are lowercased.
                                    string lowerPropertyType = propertyTypeName.ToLower();
                                    dependencyGraph.AddNewListIfNotContainsKeyAndAddValueToList(lowerPropertyType, lowerScriptType);
                                    usageGraph.AddNewListIfNotContainsKeyAndAddValueToList(lowerScriptType, lowerPropertyType);
                                    debugLog.WriteLine("Registering a dependency from " + scriptName + " to " + propertyTypeName);
                                }

                                progressWriter.IncrementAndWrite();
                            }
                        }
                    }
                }
                progressWriter.Write("Saving");
                TES5ScriptDependencyGraph graph = new TES5ScriptDependencyGraph(dependencyGraph, usageGraph);
                buildTargets.WriteGraph(graph);
                progressWriter.WriteLast();
            }
        }