A game about forced loneliness, made by TACStudios
1/*---------------------------------------------------------------------------------------------
2 * Copyright (c) Unity Technologies.
3 * Copyright (c) Microsoft Corporation. All rights reserved.
4 * Licensed under the MIT License. See License.txt in the project root for license information.
5 *--------------------------------------------------------------------------------------------*/
6using System;
7using System.IO;
8using UnityEngine;
9
10namespace Microsoft.Unity.VisualStudio.Editor
11{
12 internal static class FileUtility
13 {
14 public const char WinSeparator = '\\';
15 public const char UnixSeparator = '/';
16
17 public static string GetPackageAssetFullPath(params string[] components)
18 {
19 // Unity has special IO handling of Packages and will resolve those path to the right package location
20 return Path.GetFullPath(Path.Combine("Packages", "com.unity.ide.visualstudio", Path.Combine(components)));
21 }
22
23 public static string GetAssetFullPath(string asset)
24 {
25 var basePath = Path.GetFullPath(Path.Combine(Application.dataPath, ".."));
26 return Path.GetFullPath(Path.Combine(basePath, NormalizePathSeparators(asset)));
27 }
28
29 public static string NormalizePathSeparators(this string path)
30 {
31 if (string.IsNullOrEmpty(path))
32 return path;
33
34 if (Path.DirectorySeparatorChar == WinSeparator)
35 path = path.Replace(UnixSeparator, WinSeparator);
36 if (Path.DirectorySeparatorChar == UnixSeparator)
37 path = path.Replace(WinSeparator, UnixSeparator);
38
39 return path.Replace(string.Concat(WinSeparator, WinSeparator), WinSeparator.ToString());
40 }
41
42 public static string NormalizeWindowsToUnix(this string path)
43 {
44 if (string.IsNullOrEmpty(path))
45 return path;
46
47 return path.Replace(WinSeparator, UnixSeparator);
48 }
49
50 internal static bool IsFileInProjectRootDirectory(string fileName)
51 {
52 var relative = MakeRelativeToProjectPath(fileName);
53 if (string.IsNullOrEmpty(relative))
54 return false;
55
56 return relative == Path.GetFileName(relative);
57 }
58
59 public static string MakeAbsolutePath(this string path)
60 {
61 if (string.IsNullOrEmpty(path)) { return string.Empty; }
62 return Path.IsPathRooted(path) ? path : Path.GetFullPath(path);
63 }
64
65 // returns null if outside of the project scope
66 internal static string MakeRelativeToProjectPath(string fileName)
67 {
68 var basePath = Path.GetFullPath(Path.Combine(Application.dataPath, ".."));
69 fileName = NormalizePathSeparators(fileName);
70
71 if (!Path.IsPathRooted(fileName))
72 fileName = Path.Combine(basePath, fileName);
73
74 if (!fileName.StartsWith(basePath, StringComparison.OrdinalIgnoreCase))
75 return null;
76
77 return fileName
78 .Substring(basePath.Length)
79 .Trim(Path.DirectorySeparatorChar);
80 }
81 }
82}