Esempio n. 1
0
 /// <summary>
 /// Tries to find script item that is used by the specified script type.
 /// </summary>
 /// <param name="scriptType">The type of the script.</param>
 /// <returns>Found script or null if cannot find it.</returns>
 public ScriptItem FindScriptWitScriptName(ScriptType scriptType)
 {
     if (scriptType != ScriptType.Null)
     {
         var className  = scriptType.Name;
         var scriptName = ScriptItem.CreateScriptName(className);
         return(FindScriptWitScriptName(scriptName));
     }
     return(null);
 }
Esempio n. 2
0
        /// <summary>
        /// Tries to find script item that is used by the specified script object.
        /// </summary>
        /// <param name="script">The instance of the script.</param>
        /// <returns>Found script or null if cannot find it.</returns>
        public ScriptItem FindScriptWitScriptName(Script script)
        {
            if (script)
            {
                var className  = script.GetType().Name;
                var scriptName = ScriptItem.CreateScriptName(className);
                return(FindScriptWitScriptName(scriptName));
            }

            return(null);
        }
        /// <inheritdoc />
        public override void Create(string outputPath, object arg)
        {
            // Load template

            if (_pathGetter != null)
            {
                _path = _pathGetter?.Invoke();
            }

            // TODO: better naming :)
            var templatePath   = _isPath ? (Path.IsPathRooted(_path) ? _path : Path.GetFullPath(_path)) : _path;
            var scriptTemplate = _isPath ? File.ReadAllText(templatePath) : templatePath;

            var scriptNamespace = Editor.Instance.ProjectInfo.Name.Replace(" ", "");

            // Get directories
            var sourceDirectory = Globals.ProjectFolder.Replace('\\', '/') + "/Source/";
            var outputDirectory = new FileInfo(outputPath).DirectoryName.Replace('\\', '/');

            // Generate "sub" namespace from relative path between source root and output path
            // NOTE: Could probably use Replace instead substring, but this is faster :)
            var subNamespaceStr = outputDirectory.Substring(sourceDirectory.Length - 1).Replace(" ", "").Replace(".", "").Replace('/', '.');

            // Replace all namespace invalid characters
            // NOTE: Need to handle number sequence at the beginning since namespace which begin with numeric sequence are invalid
            string subNamespace = string.Empty;
            bool   isStart      = true;

            for (int pos = 0; pos < subNamespaceStr.Length; pos++)
            {
                var c = subNamespaceStr[pos];

                if (isStart)
                {
                    // Skip characters that cannot start the sub namespace
                    if (char.IsLetter(c))
                    {
                        isStart       = false;
                        subNamespace += '.';
                        subNamespace += c;
                    }
                }
                else
                {
                    // Add only valid characters
                    if (char.IsLetterOrDigit(c) || c == '_')
                    {
                        subNamespace += c;
                    }
                    // Check for sub namespace start
                    else if (c == '.')
                    {
                        isStart = true;
                    }
                }
            }

            // Append if valid
            if (subNamespace.Length > 1)
            {
                scriptNamespace += subNamespace;
            }

            // Format
            var scriptName = ScriptItem.CreateScriptName(outputPath);

            scriptTemplate = scriptTemplate.Replace("%class%", scriptName);
            scriptTemplate = scriptTemplate.Replace("%namespace%", scriptNamespace);

            // Save
            File.WriteAllText(outputPath, scriptTemplate, Encoding.UTF8);
        }