Esempio n. 1
0
        public void GenerateLayouts(GuerillaReader reader, string outputFolder)
        {
            // Check if the subfolder for block definitions exists.
            if (Directory.Exists(string.Format("{0}\\BlockDefinitions", outputFolder)) == false)
            {
                // Create the subfolder for block definitions.
                Directory.CreateDirectory(string.Format("{0}\\BlockDefinitions", outputFolder));
            }

            // Loop through all of the definitions in Guerilla and verify the size of each one.
            for (int i = 0; i < reader.TagBlockDefinitions.Keys.Count; i++)
            {
                // Get the tag block definition.
                TagBlockDefinition tagBlock = reader.TagBlockDefinitions[reader.TagBlockDefinitions.Keys.ElementAt(i)];

                // Verify the definition address is correct.
                int definitionSize = TagLayoutValidator.ComputeGuerillaDefinitionSize(reader, tagBlock.TagFields[tagBlock.GetFieldSetIndexClosestToH2Xbox()]);
                if (definitionSize != tagBlock.TagFieldSets[tagBlock.GetFieldSetIndexClosestToH2Xbox()].size)
                {
                    // Print the details to the console.
                    System.Diagnostics.Debug.WriteLine(string.Format("Size mismatch on block \"{0}\" computed: {1} actual: {2}", tagBlock.s_tag_block_definition.Name,
                                                                     definitionSize, tagBlock.TagFieldSets[tagBlock.GetFieldSetIndexClosestToH2Xbox()].size));
                }
            }

            // Build a list of references for each tag block definition we have.
            Dictionary <string, List <TagBlockDefinition> > tagBlockReferences   = PreProcessTagBlockDefinitions(reader);
            Dictionary <string, List <TagBlockDefinition> > nonUniqueDefinitions = tagBlockReferences.Where(b => b.Value.Count > 1).ToDictionary(p => p.Key, p => p.Value);

            // Initialize our list of tag definitions to process with the tag groups from the guerilla reader.
            //tag_group hlmt = reader.TagGroups.First(tag => tag.GroupTag.Equals("hlmt"));
            List <TagBlockDefinition> tagBlockDefinitions = new List <TagBlockDefinition>(reader.TagBlockDefinitions.Values.Where(block => block.IsTagGroup == true));

            //tagBlockDefinitions.Add(reader.TagBlockDefinitions[hlmt.definition_address]);

            // Loop through all of the non-unique tag blocks and add them to the list of definitions to be processed.
            foreach (TagBlockDefinition definition in reader.TagBlockDefinitions.Values)
            {
                // Check if the definition name is in the non-unique block list.
                if (nonUniqueDefinitions.Keys.Contains(definition.s_tag_block_definition.Name) == true)
                {
                    // Add the definition to the list to be extracted.
                    tagBlockDefinitions.Add(definition);
                }
            }

            // Pre-process any tag layouts that need fixups.
            for (int i = 0; i < this.preProcessingFunctions.Count; i++)
            {
                // Find the tag block definition this preprocessing function is associated with.
                TagBlockDefinition tagBlock = reader.TagBlockDefinitions.Single(
                    block => block.Value.s_tag_block_definition.Name.Equals(this.preProcessingFunctions.ElementAt(i).Key) == true).Value;

                // Invoke the preprocessing method.
                this.preProcessingFunctions.ElementAt(i).Value.Invoke(null, new object[] { tagBlock });
            }

            // Create a list of layout creators for all of the definitions we will be processing.
            List <MutationTagLayoutCreator> layoutCreators = new List <MutationTagLayoutCreator>();

            // Loop through the list of tag block definitions and create a code scope for each one.
            // This will build a list of types in the global namespace which we need before we start processing layouts.
            for (int i = 0; i < tagBlockDefinitions.Count; i++)
            {
                // Create a new tag layout creator and have it create its code scope using the tag block definition.
                MutationTagLayoutCreator layoutCreator = new MutationTagLayoutCreator(tagBlockDefinitions[i]);
                layoutCreator.CreateCodeScope(this.globalCodeScope);

                // Add the layout creator to the list.
                layoutCreators.Add(layoutCreator);
            }

            // Now that we have a type list built loop through all of the layout creators and create the actual tag layouts.
            for (int i = 0; i < layoutCreators.Count; i++)
            {
                // Generate the layout.
                layoutCreators[i].CreateTagLayout(reader, this.globalCodeScope);

                // Check if there is a post process function for this block.
                if (this.postProcessingFunctions.Keys.Contains(layoutCreators[i].TagBlockDefinition.s_tag_block_definition.Name) == true)
                {
                    // Invoke the post processing function.
                    this.postProcessingFunctions[layoutCreators[i].TagBlockDefinition.s_tag_block_definition.Name].Invoke(null,
                                                                                                                          new object[] { layoutCreators[i] });
                }

                // Write it to file.
                layoutCreators[i].WriteToFile(outputFolder);
            }
        }