-
Notifications
You must be signed in to change notification settings - Fork 16
/
PathUtil.cs
184 lines (166 loc) · 7.58 KB
/
PathUtil.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
using System;
using System.IO;
using UnityEngine;
namespace PLATEAU.Util
{
/// <summary>
/// PLATEAU のファイルパスに関するユーティリティです。
/// </summary>
public static class PathUtil
{
// この値はテストのときのみ変更されるのでreadonlyにしないでください。
// ReSharper disable once FieldCanBeMadeReadOnly.Local
private static string unityProjectDataPath = Application.dataPath;
public static readonly string PLATEAUSrcFetchDir = Path.GetFullPath(Application.streamingAssetsPath + "/.PLATEAU");
public const string UdxFolderName = "udx";
/// <summary>
/// 入力ファイル用のパスとして正しければtrue,不適切であればfalseを返します。
/// </summary>
/// <param name="filePath">入力ファイルのパスです。</param>
/// <param name="expectedExtension">入力ファイルとして想定される拡張子です。</param>
/// <param name="shouldFileInAssetsFolder">ファイルがUnityプロジェクトのAssetsフォルダ内にあるべきならtrue、他の場所でも良いならfalseを指定します。</param>
public static bool IsValidInputFilePath(string filePath, string expectedExtension,
bool shouldFileInAssetsFolder)
{
try
{
CheckFileExist(filePath);
CheckExtension(filePath, expectedExtension);
if (shouldFileInAssetsFolder) CheckSubDirectoryOfAssets(filePath);
}
catch (Exception)
{
Debug.LogError($"Input file path is invalid. filePath = {filePath}");
return false;
}
return true;
}
/// <summary>
/// 出力用のファイルパスとして正しければtrue,不適切であればfalseを返します。
/// </summary>
public static bool IsValidOutputFilePath(string filePath, string expectedExtension)
{
try
{
CheckDirectoryExist(filePath);
CheckExtension(filePath, expectedExtension);
}
catch (Exception e)
{
Debug.LogError($"Output file path is invalid.\n{e.Message}");
return false;
}
return true;
}
/// <summary>
/// 指定パスにファイルが存在するかチェックします。
/// 存在しなければ例外を投げます。
/// </summary>
private static void CheckFileExist(string filePath)
{
if (File.Exists(filePath)) return;
throw new IOException($"File is not found on the path :\n{filePath}");
}
/// <summary>
/// 指定パスのうちディレクトリ部分が存在するかチェックします。
/// 存在しなければ例外を投げます。
/// </summary>
private static void CheckDirectoryExist(string filePath)
{
var directory = Path.GetDirectoryName(filePath);
if (Directory.Exists(directory)) return;
throw new IOException($"Directory is not found on the path :\n{directory}.");
}
/// <summary>
/// ファイルパスの拡張子が expectedExtension と同一かチェックします。
/// 異なる場合は例外を投げます。
/// </summary>
private static void CheckExtension(string filePath, string expectedExtension)
{
var actualExtension = Path.GetExtension(filePath).ToLower().Replace(".", "");
if (actualExtension == expectedExtension) return;
throw new IOException(
$"The file extension should be '{expectedExtension}', but actual is '{actualExtension}'"
);
}
/// <summary>
/// ファイルパスがUnityプロジェクトのAssetsフォルダ内であるかチェックします。
/// そうでない場合は例外を投げます。
/// </summary>
private static void CheckSubDirectoryOfAssets(string filePath)
{
if (IsSubDirectoryOfAssets(filePath)) return;
throw new IOException($"File must exist in Assets folder, but the path is outside Assets folder.\filePath = {filePath}");
}
public static bool IsSubDirectoryOfAssets(string filePath)
{
if (!filePath.EndsWith(Path.DirectorySeparatorChar.ToString()))
{
filePath += Path.DirectorySeparatorChar;
}
var fullPath = Path.GetFullPath(filePath);
var assetsPath = Path.GetFullPath(unityProjectDataPath) + Path.DirectorySeparatorChar;
return fullPath.StartsWith(assetsPath);
}
public static bool IsSubDirectory(string subPath, string dir)
{
if (string.IsNullOrEmpty(subPath) || string.IsNullOrEmpty(dir)) return false;
string subFull = Path.GetFullPath(subPath);
string dirFull = Path.GetFullPath(dir);
return subFull.StartsWith(dirFull);
}
/// <summary>
/// フルパスをAssetsフォルダからのパスに変換します。
/// パスがAssetsフォルダ内を指すことが前提であり、そうでない場合は <see cref="IOException"/> を投げます。
/// </summary>
public static string FullPathToAssetsPath(string filePath)
{
CheckSubDirectoryOfAssets(filePath);
var fullPath = Path.GetFullPath(filePath);
var dataPath = Path.GetFullPath(unityProjectDataPath);
var assetsPath = "Assets" + fullPath.Replace(dataPath, "");
#if UNITY_STANDALONE_WIN
assetsPath = assetsPath.Replace('\\', '/');
#endif
return assetsPath;
}
#if UNITY_EDITOR
/// <summary>
/// PLATEAU SDK のパスは、 GitHub からインポートした場合は Packages 以下になり、
/// Unity Asset Store からインポートした場合は Assets 以下になります。
/// SDKの基本パスを返します。
/// </summary>
public static string SdkBasePath
{
get
{
return IsInPackageDir ? "Packages/com.synesthesias.plateau-unity-sdk" : "Assets/PLATEAU-SDK-for-Unity";
}
}
/// <summary>
/// SdkBasePath からの相対パスを受け取り、アセットパスに変換して返します。
/// </summary>
public static string SdkPathToAssetPath(string sdkPath)
{
return Path.Combine(SdkBasePath, sdkPath);
}
private static bool isInPackageDirCalculated;
private static bool isInPackageDir;
/// <summary>
/// PLATEAU SDK が Package フォルダにある場合は true, Asset フォルダにある場合は false を返します。
/// 参考 : https://forum.unity.com/threads/how-to-detect-if-we-are-in-a-packager-context-or-in-the-assets-folder.1116505/#post-7182082
/// </summary>
private static bool IsInPackageDir {
get
{
if (isInPackageDirCalculated) return isInPackageDir;
var assembly = System.Reflection.Assembly.GetExecutingAssembly();
var packageInfo = UnityEditor.PackageManager.PackageInfo.FindForAssembly(assembly);
isInPackageDir = packageInfo != null;
isInPackageDirCalculated = true;
return isInPackageDir;
}
}
#endif
}
}