Пример #1
0
        /// <summary>
        /// Returns the path of the file
        /// </summary>
        /// <param name="pathOfInclude"></param>
        /// <param name="pathOfScript"></param>
        /// <returns></returns>
        private string GetFilePath(string pathOfInclude, string pathOfScript)
        {
            // Step 1 (optional). Search from include directories
            // Step 2. Search from relative path from script

            string pathOfIncludeOriginal = pathOfInclude;

            pathOfInclude = SearchFile(pathOfInclude);

            if (pathOfInclude == "")
            {
                // If path is relative and no includedirectories
                if (!Path.IsPathRooted(pathOfIncludeOriginal))
                {
                    pathOfInclude = LSLIPathHelper.GetRelativePath(pathOfScript, Environment.CurrentDirectory) + pathOfIncludeOriginal;
                }
                else if (this.implementedIncludes.Count > 0) // If there are already includes, the relative path is already correct
                {
                    pathOfInclude = Path.GetDirectoryName(this.implementedIncludes.LastOrDefault()) + '\\' + pathOfIncludeOriginal;
                }
                else
                {
                    pathOfInclude = pathOfIncludeOriginal;
                }

                // If path is absolute it will stay the pathOfInclude
                pathOfInclude = SearchFile(pathOfInclude);
            }

            return(pathOfInclude);
        }
Пример #2
0
        /// <summary>
        /// Imports scripts from //#include statements
        /// </summary>
        /// <param name="strC">Sourcecode</param>
        /// <param name="pathOfScript">Path of the source code of the script</param>
        /// <returns>Sourcecode with imported scripts</returns>
        private string ImportScripts(string strC, string pathOfScript, bool ShowBeginEnd = true)
        {
            if (!LSLIPathHelper.IsLSLI(pathOfScript))
            {
                // If it's not an LSLI script, it can't import a script
                return(strC);
            }

            StringBuilder   sb        = new StringBuilder(strC);
            MatchCollection mIncludes = Regex.Matches(strC, INCLUDE_REGEX); // Find includes

            foreach (Match m in mIncludes)
            {
                if (this.includeDepth == 0)
                {
                    this.implementedIncludes = new List <string>();
                }
                includeDepth++;

                string line       = GetLineOfMatch(strC, m);
                int    lineNumber = strC.Take(m.Index + line.Length - 1).Count(c => c == '\n') + 1;

                string pathOfIncludeOriginal = Regex.Match(line, PATH_OF_INCLUDE_REGEX).Value.Trim('"');
                string pathOfInclude         = pathOfIncludeOriginal;
                string ext = Path.GetExtension(pathOfInclude).ToLower();

                if ((validExtensions.Contains(ext) || ext == "") && IsDifferentScript(pathOfInclude, pathOfScript))
                {
                    pathOfInclude = GetFilePath(pathOfInclude, pathOfScript);

                    if (pathOfInclude != "" && !this.implementedIncludes.Contains(Path.GetFullPath(pathOfInclude)))
                    {
                        if (!ShowBeginEnd)
                        {
                            sb = WriteExportScript(pathOfInclude, sb, line);
                        }
                        else
                        {
                            sb = WriteDebugScript(pathOfInclude, sb, line);
                        }
                    }
                    else if (pathOfInclude != "" && this.implementedIncludes.Contains(Path.GetFullPath(pathOfInclude)))
                    {
                        string message = "Error: Recursive include loop detected: \"" + Path.GetFullPath(pathOfInclude) +
                                         "\". In script \""
                                         + Path.GetFileName(pathOfScript) + "\". Line " + lineNumber + ".";

                        ShowError(message);
                    }
                    else
                    {
                        string relativeToPathOfScript = LSLIPathHelper.GetRelativePath(pathOfScript, Environment.CurrentDirectory);
                        string correctPath            = Path.GetFullPath(relativeToPathOfScript) + pathOfIncludeOriginal;
                        string message = "Error: Unable to find file \"" + correctPath +
                                         "\". In script \"" + Path.GetFileName(pathOfScript) + "\". Line " + lineNumber + ".";

                        ShowError(message);
                    }
                }
                includeDepth--;
                if (this.implementedIncludes.Count > 0)
                {
                    this.implementedIncludes.Remove(this.implementedIncludes.Last());
                }
            }

            return(sb.ToString());
        }