public List <ITES5CodeChunk> GenerateObjectiveHandling(ITES5CodeBlock codeBlock, TES5GlobalScope globalScope, List <int> stageMap) { TES5LocalVariable castedToQuest = new TES5LocalVariable("__temp", TES5BasicType.T_QUEST); TES5Reference referenceToTemp = TES5ReferenceFactory.CreateReferenceToVariable(castedToQuest); List <ITES5CodeChunk> result = new List <ITES5CodeChunk>() { TES5VariableAssignationFactory.CreateAssignation(referenceToTemp, TES5ReferenceFactory.CreateReferenceToSelf(globalScope)) }; TES5LocalScope localScope = codeBlock.CodeScope.LocalScope; localScope.AddVariable(castedToQuest); int i = 0; foreach (var stageTargetState in stageMap) { TES5Integer targetIndex = new TES5Integer(i); if (stageTargetState != 0) { //Should be visible TES5ObjectCallArguments displayedArguments = new TES5ObjectCallArguments() { targetIndex }; TES5ObjectCall isObjectiveDisplayed = new TES5ObjectCall(referenceToTemp, "IsObjectiveDisplayed", displayedArguments); TES5ComparisonExpression expression = TES5ExpressionFactory.CreateComparisonExpression(isObjectiveDisplayed, TES5ComparisonExpressionOperator.OPERATOR_EQUAL, new TES5Integer(0)); TES5ObjectCallArguments arguments = new TES5ObjectCallArguments() { targetIndex, new TES5Integer(1) }; TES5ObjectCall showTheObjective = new TES5ObjectCall(referenceToTemp, "SetObjectiveDisplayed", arguments); TES5Branch branch = TES5BranchFactory.CreateSimpleBranch(expression, localScope); branch.MainBranch.CodeScope.AddChunk(showTheObjective); result.Add(branch); } else { TES5ObjectCallArguments displayedArguments = new TES5ObjectCallArguments() { targetIndex }; TES5ObjectCall isObjectiveDisplayed = new TES5ObjectCall(referenceToTemp, "IsObjectiveDisplayed", displayedArguments); TES5ComparisonExpression expression = TES5ExpressionFactory.CreateComparisonExpression(isObjectiveDisplayed, TES5ComparisonExpressionOperator.OPERATOR_EQUAL, new TES5Integer(1)); TES5ObjectCallArguments arguments = new TES5ObjectCallArguments() { targetIndex, new TES5Integer(1) }; TES5ObjectCall completeTheObjective = new TES5ObjectCall(referenceToTemp, "SetObjectiveCompleted", arguments); TES5Branch branch = TES5BranchFactory.CreateSimpleBranch(expression, localScope); branch.MainBranch.CodeScope.AddChunk(completeTheObjective); result.Add(branch); } ++i; } return(result); }
public List <ITES5CodeChunk> GenerateObjectiveHandling(ITES5CodeBlock codeBlock, TES5GlobalScope globalScope, List <int> stageMap) { List <ITES5CodeChunk> result = new List <ITES5CodeChunk>(); //WTM: Change: if (!stageMap.Any()) { return(result); } TES5LocalVariable castedToQuest = new TES5LocalVariable("__temp", TES5BasicType.T_QUEST);//WTM: Note: Why is this variable even necessary? TES5Reference referenceToTemp = TES5ReferenceFactory.CreateReferenceToVariableOrProperty(castedToQuest); TES5SelfReference questSelfReference = TES5ReferenceFactory.CreateReferenceToSelf(globalScope); TES5VariableAssignation tempQuestAssignation = TES5VariableAssignationFactory.CreateAssignation(referenceToTemp, questSelfReference); result.Add(tempQuestAssignation); TES5LocalScope localScope = codeBlock.CodeScope.LocalScope; localScope.AddVariable(castedToQuest); int i = 0; foreach (var stageTargetState in stageMap) { TES5Integer targetIndex = new TES5Integer(i); TES5ObjectCallArguments displayedArguments = new TES5ObjectCallArguments() { targetIndex }; TES5ObjectCall isObjectiveDisplayed = objectCallFactory.CreateObjectCall(referenceToTemp, "IsObjectiveDisplayed", displayedArguments, inference: false); int isObjectiveDisplayedArgument = stageTargetState != 0 ? 0 : 1; TES5ComparisonExpression expression = TES5ExpressionFactory.CreateComparisonExpression(isObjectiveDisplayed, TES5ComparisonExpressionOperator.OPERATOR_EQUAL, new TES5Integer(isObjectiveDisplayedArgument)); TES5ObjectCallArguments arguments = new TES5ObjectCallArguments() { targetIndex, new TES5Integer(1) }; string setObjectiveFunction = stageTargetState != 0 ? "SetObjectiveDisplayed" : "SetObjectiveCompleted"; TES5ObjectCall setObjectiveObjectCall = objectCallFactory.CreateObjectCall(referenceToTemp, setObjectiveFunction, arguments, inference: false); TES5Branch branch = TES5BranchFactory.CreateSimpleBranch(expression, localScope); branch.MainBranch.CodeScope.AddChunk(setObjectiveObjectCall); result.Add(branch); ++i; } return(result); }
/* * The script to be converted * The script"s global scope * The scope under which we"re converting * * @throws ConversionException */ public TES5Target Convert(TES4Target target, bool isStandalone, TES5GlobalScope globalScope, TES5MultipleScriptsScope multipleScriptsScope) { TES4Script script = target.Script; TES4BlockList?parsedBlockList = script.BlockList; Dictionary <string, List <ITES5CodeBlock> > createdBlocks = new Dictionary <string, List <ITES5CodeBlock> >(); if (parsedBlockList != null) { TES5EventCodeBlock?onUpdateBlockOfNonQuestOrAME = null; foreach (TES4CodeBlock block in parsedBlockList.Blocks) { TES5BlockList newBlockList = this.blockFactory.CreateBlock(block, globalScope, multipleScriptsScope, ref onUpdateBlockOfNonQuestOrAME); foreach (ITES5CodeBlock newBlock in newBlockList.Blocks) { createdBlocks.AddNewListIfNotContainsKeyAndAddValueToList(newBlock.BlockName, newBlock); } } } TES5BlockList blockList = new TES5BlockList(); foreach (var createdBlock in createdBlocks) { var blockType = createdBlock.Key; var blocks = createdBlock.Value; if (blocks.Count > 1) { foreach (TES5CodeBlock block in CombineRepeatedEventCodeBlockNames(blocks, blockType, isStandalone, globalScope)) { blockList.Add(block); } } else { ITES5CodeBlock block = blocks[0]; blockList.Add(block); } } TES5Target result = new TES5Target(new TES5Script(globalScope, blockList, false), target.OutputPath); return(result); }
/* * The script to be converted * The script"s global scope * The scope under which we"re converting * * @throws ConversionException */ public TES5Target Convert(TES4Target target, TES5GlobalScope globalScope, TES5MultipleScriptsScope multipleScriptsScope) { TES4Script script = target.Script; TES5BlockList blockList = new TES5BlockList(); TES4BlockList parsedBlockList = script.BlockList; Dictionary <string, List <ITES5CodeBlock> > createdBlocks = new Dictionary <string, List <ITES5CodeBlock> >(); if (parsedBlockList != null) { foreach (TES4CodeBlock block in parsedBlockList.Blocks) { TES5BlockList newBlockList = this.blockFactory.CreateBlock(block, globalScope, multipleScriptsScope); foreach (ITES5CodeBlock newBlock in newBlockList.Blocks) { createdBlocks.AddNewListIfNotContainsKeyAndAddValueToList(newBlock.BlockName, newBlock); } } } //todo encapsulate it to a different class. bool isStandalone = target.OutputPath.Contains(Path.DirectorySeparatorChar + "Standalone" + Path.DirectorySeparatorChar); foreach (var createdBlock in createdBlocks) { var blockType = createdBlock.Key; var blocks = createdBlock.Value; if (blocks.Count > 1) { List <TES5FunctionCodeBlock> functions = new List <TES5FunctionCodeBlock>(); int i = 1; TES5ObjectCallArguments localScopeArguments = null; foreach (TES5CodeBlock block in blocks) { TES5FunctionScope newFunctionScope = new TES5FunctionScope(blockType + "_" + i.ToString()); foreach (var variable in block.FunctionScope.Variables.Values) { newFunctionScope.AddVariable(variable); } TES5FunctionCodeBlock function = new TES5FunctionCodeBlock(newFunctionScope, block.CodeScope, new TES5VoidType(), isStandalone); functions.Add(function); if (localScopeArguments == null) { localScopeArguments = new TES5ObjectCallArguments(); foreach (var variable in block.FunctionScope.Variables.Values) { localScopeArguments.Add(TES5ReferenceFactory.CreateReferenceToVariable(variable)); } } ++i; } //Create the proxy block. ITES5CodeBlock lastBlock = blocks.Last(); TES5EventCodeBlock proxyBlock = TES5BlockFactory.CreateEventCodeBlock(blockType, /* * //WTM: Change: block was used below, but block is out of scope. The PHP must have been using the last defined block from above. * //WTM: Change: PHP called "clone" below, but I'm not sure if this is necessary or would even operate the same in C#. */ lastBlock.FunctionScope); foreach (var function in functions) { blockList.Add(function); TES5ObjectCall functionCall = this.objectCallFactory.CreateObjectCall(TES5ReferenceFactory.CreateReferenceToSelf(globalScope), function.BlockName, multipleScriptsScope, localScopeArguments, false // hacky. ); proxyBlock.AddChunk(functionCall); } blockList.Add(proxyBlock); } else { ITES5CodeBlock block = blocks[0]; blockList.Add(block); } } TES5Target result = new TES5Target(new TES5Script(globalScope, blockList), target.OutputPath); return(result); }
/* * Joins N QF subfragments into one QF fragment that can be properly binded into Skyrim VM * @throws ConversionException */ public TES5Target JoinQFFragments(IBuildTarget target, string resultingFragmentName, List <QuestStageScript> subfragmentsTrees) { int tes4FormID = GetTES4FormID(resultingFragmentName); StageMap stageMap = StageMapBuilder.Build(target, resultingFragmentName, esmAnalyzer, tes4FormID); /* * 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 = TES5ScriptHeaderFactory.GetFromCacheOrConstructByBasicType(resultingFragmentName, TES5BasicType.T_QUEST, TES5TypeFactory.TES4_Prefix, true); TES5GlobalScope resultingGlobalScope = new TES5GlobalScope(resultingScriptHeader); string[] referenceAliases = ReferenceAliasBuilder.Build(target, resultingFragmentName, esmAnalyzer, tes4FormID).ToArray(); foreach (string propertyName in referenceAliases) { resultingGlobalScope.AddProperty(TES5PropertyFactory.ConstructWithoutFormID(propertyName, TES5BasicType.T_REFERENCEALIAS, propertyName)); } List <QuestStageBlock> questStageBlocks = new List <QuestStageBlock>(); HashSet <int> implementedStages = new HashSet <int>(); HashSet <string> propertiesNamesDeclared = new HashSet <string>(); 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 */ if (propertiesNamesDeclared.Add(subfragmentProperty.Name)) { resultingGlobalScope.AddProperty(subfragmentProperty); } else { if (subfragmentProperty.IsPlayerRef) { continue; } //WTM: Change: I don't think renaming these properties actually helps anything. /* * string propertyName = GeneratePropertyName(subfragmentScript.ScriptHeader, subfragmentProperty); * subfragmentProperty.Rename(propertyName); * if (!propertiesNamesDeclared.Add(subfragmentProperty.Name)) * { * throw new ConversionException(nameof(propertiesNamesDeclared) + " already contained property " + subfragmentProperty.Name + "."); * } */ //WTM: Change: I'm trying to unify properties and include extended type declarations. TES5Property existingProperty = resultingGlobalScope.GetPropertyByName(subfragmentProperty.Name); if (TES5InheritanceGraphAnalyzer.IsTypeOrExtendsType(existingProperty.TES5Type, subfragmentProperty.TES5Type)) { continue; } if (TES5InheritanceGraphAnalyzer.IsExtending(subfragmentProperty.TES5Type, existingProperty.TES5Type)) { existingProperty.TES5Type = subfragmentProperty.TES5Type; continue; } if (TES5InheritanceGraphAnalyzer.IsExtending(existingProperty.TES5Type, subfragmentProperty.TES5Type.NativeType)) { subfragmentProperty.TES5Type.NativeType = existingProperty.TES5Type.NativeType; existingProperty.TES5Type = subfragmentProperty.TES5Type; continue; } throw new ConversionException("Types were not compatible for property " + subfragmentProperty.Name + ": " + subfragmentProperty.TES5Type.Value + " should extend " + existingProperty.TES5Type.Value + " (" + existingProperty.TES5Type.NativeType.Value + ")."); } } 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 != TES5FragmentFactory.GetFragmentName(0)) { throw new ConversionException("Wrong QF fragment funcname, actual function name: " + subfragmentBlock.FunctionScope.BlockName + "."); } string newFragmentFunctionName = TES5FragmentFactory.GetFragmentName(subfragment.Stage, subfragment.LogIndex); subfragmentBlock.FunctionScope.Rename(newFragmentFunctionName); List <int>?stageMapOfStage = stageMap.TryGetStageTargetsMap(subfragment.Stage); if (stageMapOfStage != null) { var objectiveCodeChunks = objectiveHandlingFactory.GenerateObjectiveHandling(subfragmentBlock, resultingGlobalScope, stageMapOfStage); foreach (var newCodeChunk in objectiveCodeChunks) { subfragmentBlock.AddChunk(newCodeChunk); } } questStageBlocks.Add(new QuestStageBlock(subfragment.Stage, subfragment.LogIndex, subfragmentBlock)); implementedStages.Add(subfragment.Stage); } /* * Diff to find stages which we still need to mark */ int[] nonDoneStages = stageMap.StageIDs.Where(stageID => !implementedStages.Contains(stageID)).ToArray(); foreach (int nonDoneStage in nonDoneStages) { TES5FunctionCodeBlock fragment = objectiveHandlingFactory.CreateEnclosedFragment(resultingGlobalScope, nonDoneStage, stageMap.GetStageTargetsMap(nonDoneStage)); questStageBlocks.Add(new QuestStageBlock(nonDoneStage, 0, fragment)); } this.mappedTargetsLogService.WriteScriptName(resultingFragmentName); foreach (var kvp in stageMap.MappedTargetsIndex) { var originalTargetIndex = kvp.Key; var mappedTargetIndexes = kvp.Value; this.mappedTargetsLogService.WriteLine(originalTargetIndex, mappedTargetIndexes); } TES5BlockList resultingBlockList = new TES5BlockList(questStageBlocks.OrderBy(b => b.StageID).ThenBy(b => b.LogIndex).Select(b => b.CodeBlock)); TES5Script resultingTree = new TES5Script(resultingGlobalScope, resultingBlockList, true); string outputPath = target.GetTranspileToPath(resultingFragmentName); return(new TES5Target(resultingTree, outputPath)); }
public void AddBlock(ITES5CodeBlock block) { codeBlocks.Add(block); }
/* * 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 QuestStageBlock(int stageID, int logIndex, ITES5CodeBlock codeBlock) { StageID = stageID; LogIndex = logIndex; CodeBlock = codeBlock; }
private IEnumerable <TES5CodeBlock> CombineRepeatedEventCodeBlockNames(List <ITES5CodeBlock> blocks, string blockType, bool isStandalone, TES5GlobalScope globalScope) { ITES5CodeBlock[] nonEvents = blocks.Where(b => !(b is TES5EventCodeBlock)).ToArray(); if (nonEvents.Any()) { throw new ConversionException("Some non-event code blocks were present in " + nameof(CombineRepeatedEventCodeBlockNames) + ": " + string.Join("; ", nonEvents.Select(b => b.GetType().FullName))); } TES5EventCodeBlock[] eventBlocks = blocks.Cast <TES5EventCodeBlock>().ToArray(); List <TES5FunctionCodeBlock> functions = new List <TES5FunctionCodeBlock>(); TES5ObjectCallArguments? localScopeArguments = null; for (int i = 0; i < eventBlocks.Length; i++) { ITES5CodeBlock block = eventBlocks[i]; TES5FunctionScope newFunctionScope = new TES5FunctionScope(blockType + "_" + (i + 1).ToString()); foreach (var parameter in block.FunctionScope.GetParameters()) { newFunctionScope.AddParameter(parameter); } TES5FunctionCodeBlock function = new TES5FunctionCodeBlock(newFunctionScope, block.CodeScope, TES5VoidType.Instance, isStandalone, false); functions.Add(function); if (localScopeArguments == null) { localScopeArguments = new TES5ObjectCallArguments(); foreach (var parameter in block.FunctionScope.GetParameters()) { localScopeArguments.Add(TES5ReferenceFactory.CreateReferenceToVariableOrProperty(parameter)); } } } if (functions.Any()) { //Create the proxy block. ITES5CodeBlock lastBlock = blocks.Last(); TES5EventCodeBlock proxyBlock = TES5BlockFactory.CreateEventCodeBlock( //WTM: Change: block was used below, but block is out of scope. The PHP must have been using the last defined block from above. //WTM: Change: PHP called "clone" below, but I'm not sure if this is necessary or would even operate the same in C#. lastBlock.FunctionScope, globalScope); foreach (var function in functions) { yield return(function); TES5ObjectCall functionCall = this.objectCallFactory.CreateObjectCall(TES5ReferenceFactory.CreateReferenceToSelf(globalScope), function.BlockName, localScopeArguments, false// hacky. ); proxyBlock.AddChunk(functionCall); } yield return(proxyBlock); } /* * //This was not ultimately necessary. * IGrouping<string, TES5StateCodeBlock>[] states = blocks.OfType<TES5StateCodeBlock>().ToLookup(b => b.BlockName).ToArray(); * List<TES5CodeBlock> consolidatedStateCodeBlocks = states.Where(b => b.Count() == 1).SelectMany(b => b.Select(b2 => (TES5CodeBlock)b2).ToArray()).ToList(); * IGrouping<string, TES5StateCodeBlock>[] repeatedStateCodeBlocks = states.Where(b => b.Count() > 1).ToArray(); * for (int i = 0; i < repeatedStateCodeBlocks.Length; i++) * { * IGrouping<string, TES5StateCodeBlock> groupsWithSameName = repeatedStateCodeBlocks[i]; * TES5StateCodeBlock firstGroup = groupsWithSameName.First(); * foreach (var otherGroup in groupsWithSameName.Skip(1)) * { * foreach (ITES5CodeChunk chunk in otherGroup.CodeScope.CodeChunks) * { * firstGroup.AddChunk(chunk); * } * } * if (firstGroup.CodeBlocks.Blocks.Any()) * { * TES5CodeBlock[] reducedBlocks = CombineRepeatedCodeBlockNames(firstGroup.CodeBlocks.Blocks, blockType, isStandalone, globalScope, multipleScriptsScope).ToArray(); * firstGroup.CodeBlocks.Blocks.Clear(); * firstGroup.CodeBlocks.Blocks.AddRange(reducedBlocks); * } * consolidatedStateCodeBlocks.Add(firstGroup); * } * foreach(TES5CodeBlock codeBlock in consolidatedStateCodeBlocks) * { * yield return codeBlock; * } */ }