diff options
author | Ignacio Etcheverry <ignalfonsore@gmail.com> | 2019-07-03 09:44:53 +0200 |
---|---|---|
committer | Ignacio Etcheverry <ignalfonsore@gmail.com> | 2019-07-05 09:38:23 +0200 |
commit | 270af6fa089ccfb93ace68ada8d476bd902b10fa (patch) | |
tree | fee771a4f58a8d799f70d37547391831f52b5cd2 /modules/mono/editor/GodotTools/GodotTools.Core/StringExtensions.cs | |
parent | 7b569e91c0c6b84965cad416b8e86dcfdacbcfc4 (diff) | |
download | redot-engine-270af6fa089ccfb93ace68ada8d476bd902b10fa.tar.gz |
Re-write mono module editor code in C#
Make the build system automatically build the C# Api assemblies to be shipped with the editor.
Make the editor, editor player and debug export templates use Api assemblies built with debug symbols.
Always run MSBuild to build the editor tools and Api assemblies when building Godot.
Several bugs fixed related to assembly hot reloading and restoring state.
Fix StringExtensions internal calls not being registered correctly, resulting in MissingMethodException.
Diffstat (limited to 'modules/mono/editor/GodotTools/GodotTools.Core/StringExtensions.cs')
-rw-r--r-- | modules/mono/editor/GodotTools/GodotTools.Core/StringExtensions.cs | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/modules/mono/editor/GodotTools/GodotTools.Core/StringExtensions.cs b/modules/mono/editor/GodotTools/GodotTools.Core/StringExtensions.cs new file mode 100644 index 0000000000..8cd7e76303 --- /dev/null +++ b/modules/mono/editor/GodotTools/GodotTools.Core/StringExtensions.cs @@ -0,0 +1,77 @@ +using System; +using System.Collections.Generic; +using System.IO; + +namespace GodotTools.Core +{ + public static class StringExtensions + { + public static string RelativeToPath(this string path, string dir) + { + // Make sure the directory ends with a path separator + dir = Path.Combine(dir, " ").TrimEnd(); + + if (Path.DirectorySeparatorChar == '\\') + dir = dir.Replace("/", "\\") + "\\"; + + Uri fullPath = new Uri(Path.GetFullPath(path), UriKind.Absolute); + Uri relRoot = new Uri(Path.GetFullPath(dir), UriKind.Absolute); + + return relRoot.MakeRelativeUri(fullPath).ToString(); + } + + public static string NormalizePath(this string path) + { + bool rooted = path.IsAbsolutePath(); + + path = path.Replace('\\', '/'); + + string[] parts = path.Split(new[] {'/'}, StringSplitOptions.RemoveEmptyEntries); + + path = string.Join(Path.DirectorySeparatorChar.ToString(), parts).Trim(); + + return rooted ? Path.DirectorySeparatorChar.ToString() + path : path; + } + + private static readonly string driveRoot = Path.GetPathRoot(Environment.CurrentDirectory); + + public static bool IsAbsolutePath(this string path) + { + return path.StartsWith("/", StringComparison.Ordinal) || + path.StartsWith("\\", StringComparison.Ordinal) || + path.StartsWith(driveRoot, StringComparison.Ordinal); + } + + public static string CsvEscape(this string value, char delimiter = ',') + { + bool hasSpecialChar = value.IndexOfAny(new char[] {'\"', '\n', '\r', delimiter}) != -1; + + if (hasSpecialChar) + return "\"" + value.Replace("\"", "\"\"") + "\""; + + return value; + } + + public static string ToSafeDirName(this string dirName, bool allowDirSeparator) + { + var invalidChars = new List<string> {":", "*", "?", "\"", "<", ">", "|"}; + + if (allowDirSeparator) + { + // Directory separators are allowed, but disallow ".." to avoid going up the filesystem + invalidChars.Add(".."); + } + else + { + invalidChars.Add("/"); + } + + string safeDirName = dirName.Replace("\\", "/").Trim(); + + foreach (string invalidChar in invalidChars) + safeDirName = safeDirName.Replace(invalidChar, "-"); + + return safeDirName; + } + } +} |