Пример #1
0
        /// <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
        /// <summary>
        /// Creates a new code safe flag name that is unique in the current code scope and adds it to the fields list.
        /// </summary>
        /// <param name="fieldName">Name of the flag.</param>
        /// <returns>The new code safe representation of the flag name.</returns>
        public string CreateCodeSafeFlagName(string fieldName)
        {
            // Convert the flag name to a code safe representation.
            string newFlagName = MutationCodeFormatter.CreateCodeSafeFieldName(fieldName);

            // Make sure the new flag name is unique in the code scope.
            if (this.Fields.Contains(newFlagName) == true)
            {
                string tempFlagName = "";

                // Loop and append an integer until the flag name becomes unique.
                int uniqueInt = 1;
                do
                {
                    // Append an integer to the flag name to try and make it unique.
                    tempFlagName = String.Format("{0}{1}", tempFlagName, uniqueInt++);
                }while (this.Fields.Contains(tempFlagName) == true);

                // Save the new field name.
                newFlagName = tempFlagName;
            }

            // Add the new flag name to the fields list.
            this.Fields.Add(newFlagName);

            // Return the new flag name.
            return(newFlagName);
        }
Пример #3
0
        /// <summary>
        /// Creates a new code safe field name that is unique in the current scope and adds it to the fields list for this scope.
        /// </summary>
        /// <param name="fieldType">Type of field.</param>
        /// <param name="fieldName">Name of the field</param>
        /// <param name="displayName">The UI mark up display name for the field.</param>
        /// <param name="units">String to receive the units specifier is one is present</param>
        /// <param name="tooltip">String to receive the tooltip text if it is present</param>
        /// <param name="markupFlags">The UI markup flags for this field.</param>
        /// <returns>The new code safe field name for the field</returns>
        public string CreateCodeSafeFieldName(field_type fieldType, string fieldName, out string displayName, out string units, out string tooltip, out EditorMarkUpFlags markupFlags)
        {
            string newFieldName = "";

            // Satisfy the compiler.
            displayName = string.Empty;
            units       = string.Empty;
            tooltip     = string.Empty;
            markupFlags = EditorMarkUpFlags.None;

            // Check if the field is a padding field.
            if (fieldType == field_type._field_pad || fieldType == field_type._field_skip || fieldType == field_type._field_useless_pad)
            {
                // Create a new padding field name.
                return(AddPaddingField());
            }
            else if (fieldType == field_type._field_explanation)
            {
                // Create a new explanation field name.
                return(AddExplanationField());
            }

            // Check if the name is invalid.
            if (MutationCodeFormatter.IsValidFieldName(fieldName) == false)
            {
                // Create a new no-name field name.
                return(AddNoNameField());
            }

            // Convert the field name to a code safe representation.
            MutationCodeFormatter.ProcessFieldName(fieldName, out newFieldName, out displayName, out units, out tooltip, out markupFlags);
            if (newFieldName == "")
            {
                // Create a new no-name field name.
                return(AddNoNameField());
            }

            // Make sure the new field name is unique in the code scope.
            if (this.Fields.Contains(newFieldName) == true)
            {
                string tempFieldName = "";

                // Loop and append an integer until the field name becomes unique.
                int uniqueInt = 1;
                do
                {
                    // Append an integer to the field name to try and make it unique.
                    tempFieldName = String.Format("{0}{1}", newFieldName, uniqueInt++);
                }while (this.Fields.Contains(tempFieldName) == true);

                // Save the new field name.
                newFieldName = tempFieldName;
            }

            // Add the new field name to the fields list.
            this.Fields.Add(newFieldName);

            // Return the new field name.
            return(newFieldName);
        }
Пример #4
0
 public void WriteToFile(string folder)
 {
     // Check if the definition is a tag group or tag block and write it to the correct location.
     if (this.TagBlockDefinition.IsTagGroup == true)
     {
         // Write the tag group to file.
         this.CodeCreator.WriteToFile(string.Format("{0}\\{1}.cs", folder,
                                                    MutationCodeFormatter.CreateCodeSafeFieldName(this.TagBlockDefinition.s_tag_block_definition.Name)));
     }
     else
     {
         // Write the block definition to file.
         this.CodeCreator.WriteToFile(string.Format("{0}\\BlockDefinitions\\{1}.cs", folder,
                                                    MutationCodeFormatter.CreateCodeSafeFieldName(this.TagBlockDefinition.s_tag_block_definition.Name)));
     }
 }
Пример #5
0
        /// <summary>
        /// Creates a new field for the specified type and adds it to the current code object.
        /// </summary>
        /// <param name="fieldType">Guerilla field type of the field.</param>
        /// <param name="fieldName">Name of the field.</param>
        /// <param name="attributeCollection">Collection of attributes to be put on the field.</param>
        public void AddField(field_type fieldType, string fieldName, CodeAttributeDeclarationCollection attributeCollection = null)
        {
            // Get the underlying type for this field.
            Type standardFieldType = ValueTypeDictionary[fieldType];

            // Create a new code member field for the tag field.
            CodeMemberField field = new CodeMemberField(MutationCodeFormatter.CreateShortCodeTypeReference(standardFieldType, MutationNamespaces), fieldName);

            field.Attributes = MemberAttributes.Public;

            // Add any attributes for this field.
            field.CustomAttributes = attributeCollection;

            // Add the field to the class definition.
            this.CodeClass.Members.Add(field);
        }
Пример #6
0
        /// <summary>
        /// Creates a new tag block field and adds it to the current code object.
        /// </summary>
        /// <param name="fieldName">Name of the field.</param>
        /// <param name="blockTypeName">Name of the underlying tag block definition type.</param>
        /// <param name="attributeCollection">Collection of attributes to be put on the field.</param>
        public void AddTagBlockField(string fieldName, string blockTypeName, CodeAttributeDeclarationCollection attributeCollection = null)
        {
            // Create a new code type reference to reference the tag_block data type.
            CodeTypeReference tagBlockType = MutationCodeFormatter.CreateShortCodeTypeReference(ValueTypeDictionary[field_type._field_block], MutationNamespaces);

            tagBlockType.TypeArguments.Add(blockTypeName);

            // Create a new code member field for the tag field.
            CodeMemberField field = new CodeMemberField(tagBlockType, fieldName);

            field.Attributes = MemberAttributes.Public;

            // Add any attributes for this field.
            field.CustomAttributes = attributeCollection;

            // Add the field to the class definition.
            this.CodeClass.Members.Add(field);
        }
Пример #7
0
        /// <summary>
        /// Creates a new Explanation field using the information provided.
        /// </summary>
        /// <param name="fieldName">Name of the explanation field.</param>
        /// <param name="blockName">Name of the explanation block.</param>
        /// <param name="explanation">Explanation for the block.</param>
        /// <param name="attributeCollection">Collection of attributes to be put on the field.</param>
        public void AddExplanationField(string fieldName, string blockName = "", string explanation = "", CodeAttributeDeclarationCollection attributeCollection = null)
        {
            // Get the underlying type for this field.
            Type standardFieldType = ValueTypeDictionary[field_type._field_explanation];

            // Create a new code member field for the explanation block.
            CodeMemberField field = new CodeMemberField(MutationCodeFormatter.CreateShortCodeTypeReference(standardFieldType, MutationNamespaces), fieldName);

            field.Attributes = MemberAttributes.Public;

            // Create a list of parameters to give to the explanation constructor.
            List <CodeExpression> initializers = new List <CodeExpression>();

            // Check if the block name is present.
            if (blockName != string.Empty)
            {
                // Create a code expression for the block name initializer.
                initializers.Add(new CodeSnippetExpression(string.Format("name: \"{0}\"", blockName)));
            }

            // Check if the explanation is present.
            if (MutationCodeFormatter.IsValidFieldName(explanation) == true)
            {
                // Create a code expression for the explanation initializer.
                initializers.Add(new CodeSnippetExpression(string.Format("explanation: {0}", MutationCodeFormatter.CreateCodeSafeStringLiteral(explanation))));
            }

            // Create the init expression which will call the constructor of the Explanation object.
            field.InitExpression = new CodeObjectCreateExpression(MutationCodeFormatter.CreateShortCodeTypeReference(standardFieldType, MutationNamespaces), initializers.ToArray());

            // Add any attributes for this field.
            field.CustomAttributes = attributeCollection;

            // Add the field to the class definition.
            this.CodeClass.Members.Add(field);
        }