コード例 #1
0
ファイル: MutationCodeScope.cs プロジェクト: num0005/Mutation
        /// <summary>
        /// Creates a new code scope for a type is one does not already exist, else it turns the existing scope for the type.
        /// </summary>
        /// <param name="typeName">Name of the type</param>
        /// <param name="definitionAddress">Guerilla definition address for the type</param>
        /// <param name="scopeType">Type of code scope to be created</param>
        /// <returns>The code scope for the type.</returns>
        public MutationCodeScope CreateCodeScopeForType(string typeName, int definitionAddress, MutationCodeScopeType scopeType)
        {
            // Check if there is an entry in the Types list with the same definition address.
            MutationCodeScope codeScope = FindExistingCodeScope(definitionAddress);

            if (codeScope != null)
            {
                // There is an existing code scope for this type so just return that.
                return(codeScope);
            }

            // Create a code safe type name for the new type.
            string            newTypeName, displayName, units, tooltip;
            EditorMarkUpFlags markupFlags;

            MutationCodeFormatter.ProcessFieldName(typeName, out newTypeName, out displayName, out units, out tooltip, out markupFlags);
            if (newTypeName == "" || MutationCodeFormatter.IsValidFieldName(newTypeName) == false)
            {
                // For now we will create a no name type for it, and I will create a preprocessing function later on.
                newTypeName = this.CreateNoNameType();
            }

            // Check if the type is an enum or flags and append the corresponding character.
            if (scopeType == MutationCodeScopeType.Bitmask)
            {
                // Append 'b' for bitmask.
                newTypeName = newTypeName.Insert(0, char.IsUpper(newTypeName[0]) == true ? "b" : "b_");
            }
            else if (scopeType == MutationCodeScopeType.Enum)
            {
                // Append 'e' for enum.
                newTypeName = newTypeName.Insert(0, char.IsUpper(newTypeName[0]) == true ? "e" : "e_");
            }

            // Check if the type name is unique or if it already exists.
            if (this.Types.Keys.Contains(newTypeName) == true)
            {
                string tempTypeName = "";

                // This shouldn't really happen, but if it does loop until we have a valid type name.
                int uniqueInt = 1;
                do
                {
                    // Append an integer to the type name to try and make it unique.
                    tempTypeName = string.Format("{0}{1}", newTypeName, uniqueInt++);
                }while (this.Types.Keys.Contains(tempTypeName) == true);

                // Save the temp type name.
                newTypeName = tempTypeName;
            }

            // Create a new code scope for this type.
            codeScope = new MutationCodeScope(newTypeName, this.Namespace, definitionAddress, scopeType);

            // Add the new type to the types dictionary.
            this.Types.Add(newTypeName, codeScope);

            // Return the new code scope for the type.
            return(codeScope);
        }
コード例 #2
0
        private void ProcessTagBlockDefinition(GuerillaReader reader, TagBlockDefinition blockDefinition, MutationCodeCreator blockCodeCreator,
                                               MutationCodeScope blockCodeScope, MutationCodeScope parentScope)
        {
            // Get the field set index that most closely resembles halo 2 xbox layout.
            int fieldSetIndex = blockDefinition.GetFieldSetIndexClosestToH2Xbox();

            // Process all of the fields in the field_set.
            ProcessFields(blockDefinition.TagFields[fieldSetIndex], reader, blockCodeCreator, blockCodeScope, parentScope);
        }
コード例 #3
0
        public TagLayoutGenerator()
        {
            // Initialize the global code scope.
            this.globalCodeScope = new MutationCodeScope(MutationCodeCreator.MutationTagsNamespace, "", -1, MutationCodeScopeType.GlobalNamespace);

            // Cache all of the pre/post processing functions.
            CachePreProcessingFunctions();
            CachePostProcessingFunctions();
        }
コード例 #4
0
        public void CreateCodeScope(MutationCodeScope parentScope)
        {
            // Create a new code scope for this type.
            this.CodeScope = parentScope.CreateCodeScopeForType(this.TagBlockDefinition.s_tag_block_definition.Name,
                                                                this.TagBlockDefinition.s_tag_block_definition.address,
                                                                (this.TagBlockDefinition.IsTagGroup == true ? MutationCodeScopeType.TagGroup : MutationCodeScopeType.TagBlock));

            // Set the type name and field name based on the code scope that was just created.
            this.FieldName = this.TagBlockDefinition.s_tag_block_definition.Name;
            this.TypeName  = this.CodeScope.Namespace;
        }
コード例 #5
0
        /// <summary>
        /// Creates a new enum or bitmask type definition in the current code object.
        /// </summary>
        /// <param name="codeScope">Code scope of the enum for ensuring each flag option is unique.</param>
        /// <param name="field">Enum definition from Guerilla.</param>
        public void AddEnumOrBitmask(MutationCodeScope codeScope, enum_definition field)
        {
            // Determine the underlying type for the enum based on the field type.
            Type fieldType = ValueTypeDictionary[field.type];

            // Create the enum field by creating a new CodeTypeDeclaration.
            CodeTypeDeclaration @enum = new CodeTypeDeclaration(codeScope.Namespace);

            @enum.IsEnum = true;
            @enum.BaseTypes.Add(fieldType);

            // Check if this field is a bitmask and if so add the Flags attribute.
            if (codeScope.Type == MutationCodeScopeType.Bitmask)
            {
                // Setup a code attribute declaration object for the Flags attribute.
                CodeAttributeDeclaration attribute = new CodeAttributeDeclaration(new CodeTypeReference(typeof(FlagsAttribute).Name));
                @enum.CustomAttributes.Add(attribute);
            }

            // Loop through all of the enum options and add each one to the enum field.
            for (int i = 0; i < field.option_count; i++)
            {
                // Check if the option name is valid and if not skip it.
                if (field.options[i] == string.Empty)
                {
                    continue;
                }

                // Create a code safe field name for the enum option.
                string            displayName, units, tooltip;
                EditorMarkUpFlags markupFlags;
                string            optionName = codeScope.CreateCodeSafeFieldName(field_type._field_enum_option, field.options[i], out displayName, out units, out tooltip, out markupFlags);

                // Create a new CodeMemberField for the enum option.
                CodeMemberField option = new CodeMemberField
                {
                    Name = optionName,

                    // Set the flag value accordingly.
                    InitExpression = new CodeSnippetExpression(string.Format("0x{0}",
                                                                             (codeScope.Type == MutationCodeScopeType.Bitmask ? (1 << i) : i).ToString("x"))),
                };

                // Create a new UI markup attribute and add it to the enum option.
                option.CustomAttributes.Add(EditorMarkUpAttribute.CreateAttributeDeclaration(flags: markupFlags, displayName: displayName, unitsSpecifier: units, tooltipText: tooltip));

                // Add the option to the enum.
                @enum.Members.Add(option);
            }

            // Add the enum to the class definition.
            this.CodeClass.Members.Add(@enum);
        }
コード例 #6
0
        public void CreateTagLayout(GuerillaReader reader, MutationCodeScope parentScope)
        {
            // Our child code creator for the tag group/block.
            MutationCodeCreator childCodeCreator = null;

            // Check if this tag block is a tag group.
            if (this.TagBlockDefinition.IsTagGroup == true)
            {
                // Get the tag group that points to this tag block definition.
                tag_group tagGroup = reader.TagGroups.First(tag => tag.definition_address == this.TagBlockDefinition.s_tag_block_definition.address);

                // Compute the size of the definition.
                int definitionSize = TagLayoutValidator.ComputeMutationDefinitionSize(reader, tagGroup.Definition.TagFields[tagGroup.Definition.GetFieldSetIndexClosestToH2Xbox()]);

                // Check if the tag group has a parent tag group it inherits from.
                if (tagGroup.ParentGroupTag != string.Empty)
                {
                    // Get the parent tag group object.
                    tag_group parentTagGroup = reader.TagGroups.First(tag => tag.GroupTag.Equals(tagGroup.ParentGroupTag) == true);

                    // Now find the code scope for the parent tag layout.
                    MutationCodeScope parentTagScope = parentScope.Types.Values.First(scope => scope.DefinitionAddress == parentTagGroup.definition_address);

                    // Create a new tag group class.
                    childCodeCreator = this.CodeCreator.CreateTagGroupClass(this.TypeName,
                                                                            TagGroupDefinitionAttribute.CreateAttributeDeclaration(tagGroup, definitionSize), parentTagScope.Namespace);
                }
                else
                {
                    // Create a new tag group class.
                    childCodeCreator = this.CodeCreator.CreateTagGroupClass(this.TypeName, TagGroupDefinitionAttribute.CreateAttributeDeclaration(tagGroup, definitionSize));
                }
            }
            else
            {
                // Compute the size of the definition.
                int definitionSize = TagLayoutValidator.ComputeMutationDefinitionSize(reader, this.TagBlockDefinition.TagFields[this.TagBlockDefinition.GetFieldSetIndexClosestToH2Xbox()]);

                // Create a new tag block class.
                childCodeCreator = this.CodeCreator.CreateTagBlockClass(this.TypeName, TagBlockDefinitionAttribute.CreateAttributeDeclaration(this.TagBlockDefinition, definitionSize));
            }

            // Process the tag block definition.
            ProcessTagBlockDefinition(reader, this.TagBlockDefinition, childCodeCreator, this.CodeScope, parentScope);
        }
コード例 #7
0
        private void ProcessFields(List <tag_field> fields, GuerillaReader reader, MutationCodeCreator blockCodeCreator,
                                   MutationCodeScope blockCodeScope, MutationCodeScope parentScope)
        {
            // Loop through all of the fields in the collection.
            for (int i = 0; i < fields.Count; i++)
            {
                // Create a new field and add it to the scope for this block.
                string            displayName, units, tooltip;
                EditorMarkUpFlags markupFlags;
                string            fieldName = blockCodeScope.CreateCodeSafeFieldName(fields[i].type, fields[i].Name, out displayName, out units, out tooltip, out markupFlags);

                // Create an attribute collection for any attributes we might add to the field.
                CodeAttributeDeclarationCollection attributeCollection = new CodeAttributeDeclarationCollection();

                // Make sure at least one of the required fields for a UI markup attribute is valid.
                if (markupFlags != EditorMarkUpFlags.None || displayName != string.Empty || units != string.Empty || tooltip != string.Empty)
                {
                    // Create the UI markup attribute using the information provided.
                    attributeCollection.Add(EditorMarkUpAttribute.CreateAttributeDeclaration(flags: markupFlags, displayName: displayName, unitsSpecifier: units, tooltipText: tooltip));
                }

                // Handle each field accordingly.
                switch (fields[i].type)
                {
                case field_type._field_char_enum:
                case field_type._field_enum:
                case field_type._field_long_enum:
                {
                    // Cast the field to a enum_definition struct.
                    enum_definition enumDefinition = (enum_definition)fields[i];

                    // Check if there is an existing code scope for this enum type.
                    MutationCodeScope enumScope = blockCodeScope.FindExistingCodeScope(enumDefinition.definition_address);
                    if (enumScope == null)
                    {
                        // Create a new code scope for the enum type.
                        enumScope = blockCodeScope.CreateCodeScopeForType(enumDefinition.Name, enumDefinition.definition_address, MutationCodeScopeType.Enum);

                        // Create a new enum in the block definition for this field.
                        blockCodeCreator.AddEnumOrBitmask(enumScope, enumDefinition);
                    }

                    // Add a field to the block definition for this enum type.
                    blockCodeCreator.AddCustomTypedField(fields[i].type, fieldName, enumScope.Namespace, attributeCollection);
                    break;
                }

                case field_type._field_byte_flags:
                case field_type._field_word_flags:
                case field_type._field_long_flags:
                {
                    // Cast the field to a enum_definition struct.
                    enum_definition enumDefinition = (enum_definition)fields[i];

                    // Check if there is an existing code scope for this bitmask type.
                    MutationCodeScope bitmaskScope = blockCodeScope.FindExistingCodeScope(enumDefinition.definition_address);
                    if (bitmaskScope == null)
                    {
                        // Create a new code scope for the bitmask type.
                        bitmaskScope = blockCodeScope.CreateCodeScopeForType(enumDefinition.Name, enumDefinition.definition_address, MutationCodeScopeType.Bitmask);

                        // Create a new bitmask in the block definition for this field.
                        blockCodeCreator.AddEnumOrBitmask(bitmaskScope, enumDefinition);
                    }

                    // Add a field to the block definition for this bitmask type.
                    blockCodeCreator.AddCustomTypedField(fields[i].type, fieldName, bitmaskScope.Namespace, attributeCollection);
                    break;
                }

                case field_type._field_block:
                {
                    // Get the definition struct from the field address.
                    TagBlockDefinition tagBlockDefinition = reader.TagBlockDefinitions[fields[i].definition_address];

                    // Check if the tag block definition already exists in the parent code scope.
                    MutationCodeScope tagBlockScope = parentScope.FindExistingCodeScope(tagBlockDefinition.s_tag_block_definition.address);
                    if (tagBlockScope == null)
                    {
                        // Create a new code scope for the tag block definition.
                        tagBlockScope = parentScope.CreateCodeScopeForType(tagBlockDefinition.s_tag_block_definition.Name,
                                                                           tagBlockDefinition.s_tag_block_definition.address, MutationCodeScopeType.TagBlock);

                        // Compute the size of the definition.
                        int definitionSize = TagLayoutValidator.ComputeMutationDefinitionSize(reader, tagBlockDefinition.TagFields[tagBlockDefinition.GetFieldSetIndexClosestToH2Xbox()]);

                        // Create a new class for the tag block definition.
                        MutationCodeCreator childBlockCodeCreator = this.CodeCreator.CreateTagBlockClass(tagBlockScope.Namespace,
                                                                                                         TagBlockDefinitionAttribute.CreateAttributeDeclaration(tagBlockDefinition, definitionSize));

                        // Process the tag block definition.
                        ProcessTagBlockDefinition(reader, tagBlockDefinition, childBlockCodeCreator, tagBlockScope, parentScope);
                    }

                    // Create a field for the tag block.
                    blockCodeCreator.AddTagBlockField(fieldName, tagBlockScope.Namespace, attributeCollection);
                    break;
                }

                case field_type._field_struct:
                {
                    // Cast the field to a tag_struct_definition.
                    tag_struct_definition tagStruct = (tag_struct_definition)fields[i];

                    // Get the definition struct from the field address.
                    TagBlockDefinition tagBlockDefinition = reader.TagBlockDefinitions[tagStruct.block_definition_address];

                    // Check if the tag block definition already exists in the parent code scope.
                    MutationCodeScope tagBlockScope = parentScope.FindExistingCodeScope(tagBlockDefinition.s_tag_block_definition.address);
                    if (tagBlockScope == null)
                    {
                        // Create a new code scope for the tag block definition.
                        tagBlockScope = parentScope.CreateCodeScopeForType(tagBlockDefinition.s_tag_block_definition.Name,
                                                                           tagBlockDefinition.s_tag_block_definition.address, MutationCodeScopeType.Struct);

                        // Compute the size of the definition.
                        int definitionSize = TagLayoutValidator.ComputeMutationDefinitionSize(reader, tagBlockDefinition.TagFields[tagBlockDefinition.GetFieldSetIndexClosestToH2Xbox()]);

                        // Create a new class for the tag block definition.
                        MutationCodeCreator childBlockCodeCreator = this.CodeCreator.CreateTagBlockClass(tagBlockScope.Namespace,
                                                                                                         TagBlockDefinitionAttribute.CreateAttributeDeclaration(tagBlockDefinition, definitionSize));

                        // Process the tag block definition.
                        ProcessTagBlockDefinition(reader, tagBlockDefinition, childBlockCodeCreator, tagBlockScope, parentScope);
                    }

                    // Build the tag block definition attribute for the field.
                    attributeCollection.Add(TagStructAttribute.CreateAttributeDeclaration());

                    // Create a field for the tag block.
                    blockCodeCreator.AddCustomTypedField(field_type._field_struct, fieldName, tagBlockScope.Namespace, attributeCollection);
                    break;
                }

                case field_type._field_tag_reference:
                {
                    // Cast the field to a tag_reference_definition definition.
                    tag_reference_definition tagRegDefinition = (tag_reference_definition)fields[i];

                    // Build the tag reference attribute for the field.
                    attributeCollection.Add(TagReferenceAttribute.CreateAttributeDeclaration(tagRegDefinition));

                    // Add the field with the group tag filter attribute.
                    blockCodeCreator.AddField(fields[i].type, fieldName, attributeCollection);
                    break;
                }

                case field_type._field_pad:
                case field_type._field_skip:
                case field_type._field_useless_pad:
                {
                    // Build the padding attribute for the field.
                    attributeCollection.Add(PaddingAttribute.CreateAttributeDeclaration(fields[i].type, fields[i].definition_address));

                    // Add the field with the padding attribute.
                    blockCodeCreator.AddPaddingField(fieldName, fields[i].definition_address, attributeCollection);
                    break;
                }

                case field_type._field_explanation:
                {
                    // Cast the field to a explanation_definition.
                    explaination_definition explanation = (explaination_definition)fields[i];

                    // Create a field for the explanation block.
                    blockCodeCreator.AddExplanationField(fieldName, explanation.Name, explanation.Explaination);
                    break;
                }

                case field_type._field_array_start:
                {
                    // Build a list of fields inside of the array.
                    List <tag_field> arrayFields = CreateArrayFieldList(fields, i);

                    // Loop for the length of the array and process the fields.
                    for (int x = 0; x < fields[i].definition_address; x++)
                    {
                        // Process the array fields.
                        ProcessFields(arrayFields, reader, blockCodeCreator, blockCodeScope, parentScope);
                    }

                    // Skip the fields we just processed and the array terminator.
                    i += arrayFields.Count + 1;
                    break;
                }

                case field_type._field_data:
                {
                    // Cast the field to a tag_dat_definition.
                    tag_data_definition tagData = (tag_data_definition)fields[i];

                    // Create a tag data attribute for this field.
                    attributeCollection.Add(TagDataAttribute.CreateAttributeDeclaration(tagData));

                    // Add the field to the collection.
                    blockCodeCreator.AddField(fields[i].type, fieldName, attributeCollection);
                    break;
                }

                case field_type._field_custom: break;

                case field_type._field_terminator: break;

                default:
                {
                    // Check if the value type dictionary contains this field type.
                    if (MutationCodeCreator.ValueTypeDictionary.Keys.Contains(fields[i].type) == false)
                    {
                        break;
                    }

                    // Add the field to the collection.
                    blockCodeCreator.AddField(fields[i].type, fieldName, attributeCollection);
                    break;
                }
                }
            }
        }