Exemplo n.º 1
0
		} // End field
		#endregion

		#region Constructors
		public ScriptMetaData(string name, string type, ReferabilityMode referability, int menuOrder = 20000, bool builtin = false) {
			this.name = name;
			this.type = type;
			this.referability = referability.ToString();
			this.menuOrder = menuOrder;
			this.builtin = builtin;
		}
        }         // End CreateNewVariableType

        #region File Creation
        /// <summary>
        /// Creates a script from the given template.
        ///
        /// This function does very little checking...it assumes that all names
        /// are valid C# for identifiers.
        ///
        /// Be wary that this may throw exceptions. (See StreamReader.ReadToEnd(),
        /// StreamWriter.Write(), StreamReader's contructor, and StreamWriter's
        /// constructor.)
        ///
        /// This does not issue a refresh, nor does it add any labels.
        /// </summary>
        /// <returns>
        /// The relative path (starting with the project root) to the newly
        /// created file. If it wasn't created (i.e. referability doesn't match
        /// with the types supported by the template), and empty string is
        /// returned instead.
        /// </returns>
        /// <param name="readableName">Human readable name.</param>
        /// <param name="typeName">C# name of type to be supported.</param>
        /// <param name="referability">
        /// Referability mode associated with the C# type named by typeName.
        /// </param>
        /// <param name="template">Info for the template.</param>
        /// <param name="normalPath">Path for non-editor scripts.</param>
        /// <param name="editorPath">Path for editor scripts.</param>
        private static string CreateScriptFromTemplate(
            string readableName,
            string typeName,
            ReferabilityMode referability,
            TemplateInfo template,
            string normalPath,
            string editorPath
            )
        {
            string templatePath = "";               // Path of the template file
            string newFileName  = "";               // Name of the new file
            string newFilePath  = "";               // Full path of new file (including name)

            // Before attempting to copy the template, we'll check if it
            // even matches what we need.
            if (template.IsCompatibleWith(referability))
            {
                templatePath = template.path;

                newFileName = ReplaceTemplatePlaceholders(
                    Path.GetFileNameWithoutExtension(templatePath),
                    readableName, typeName, referability.ToString()
                    ) + ".cs";

                newFilePath = UnityPathUtils.Combine(
                    (template.IsEngineTemplate ? normalPath : editorPath),
                    newFileName
                    );

                if (File.Exists(newFilePath))
                {
                    throw new IOException(newFilePath + " already exists!");
                }

                StreamReader templateReader   = null;
                string       templateContents = "";
                try {
                    // After changing the conde in this block, revise the
                    // exception documentaion above.
                    templateReader   = new StreamReader(templatePath);
                    templateContents = templateReader.ReadToEnd();
                }
                finally {
                    if (templateReader != null)
                    {
                        templateReader.Close();
                    }
                }


                string newScriptContents = ReplaceTemplatePlaceholders(
                    templateContents,
                    readableName, typeName, referability.ToString()
                    );


                StreamWriter scriptWriter = null;
                try {
                    // After changing the conde in this block, revise the
                    // exception documentaion above.
                    scriptWriter = new StreamWriter(newFilePath);
                    scriptWriter.Write(newScriptContents);
                }
                finally {
                    if (scriptWriter != null)
                    {
                        scriptWriter.Close();
                    }
                }
            }             // End if( ... )

            return(newFilePath);
        }         // End CreateScriptFromTemplate