From de8d65cb5deab596ca8570f8755b1cb9677ac548 Mon Sep 17 00:00:00 2001 From: ousttrue Date: Thu, 2 Jun 2022 18:31:17 +0900 Subject: [PATCH] =?UTF-8?q?public=20string=20FullPath=20{=20get;=20}.=20?= =?UTF-8?q?=E6=9C=AB=E5=B0=BE=E3=81=AB=20'/'=20=E3=82=92=E4=BB=98=E3=81=91?= =?UTF-8?q?=E3=81=AA=E3=81=84=E4=BB=95=E6=A7=98=E3=82=92=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../VRMShaders/GLTF/IO/Editor/PathObject.cs | 38 ++++++++++++------- 1 file changed, 25 insertions(+), 13 deletions(-) diff --git a/Assets/VRMShaders/GLTF/IO/Editor/PathObject.cs b/Assets/VRMShaders/GLTF/IO/Editor/PathObject.cs index aef3765e0..6401362d1 100644 --- a/Assets/VRMShaders/GLTF/IO/Editor/PathObject.cs +++ b/Assets/VRMShaders/GLTF/IO/Editor/PathObject.cs @@ -15,8 +15,9 @@ namespace VRMShaders /// * Delemeter は / を保証 /// * .. を解決済み /// * フルパス + /// * 末尾に / を付けない /// - public readonly string FullPath { get; } + public string FullPath { get; } public string Extension => Path.GetExtension(FullPath); @@ -24,14 +25,7 @@ namespace VRMShaders public PathObject Parent => FromFullPath(Path.GetDirectoryName(FullPath)); - public bool IsUnderAsset - { - get - { - var assets = UnityAssets; - return FullPath.StartsWith(assets.FullPath); - } - } + public bool IsUnderAsset => IsDescendantOf(UnityAssets); /// /// relative from UnityEngine.Application.dataPath @@ -42,11 +36,11 @@ namespace VRMShaders get { var root = UnityRoot; - if (!FullPath.StartsWith(root.FullPath)) + if (!IsDescendantOf(UnityRoot)) { throw new ArgumentException($"{FullPath} is not under UnityPath"); } - return FullPath.Substring(root.FullPath.Length); + return FullPath.Substring(root.FullPath.Length + 1); } } @@ -57,7 +51,7 @@ namespace VRMShaders { if (!_root.HasValue) { - _root = FromFullPath(Path.GetDirectoryName(Application.dataPath) + "/"); + _root = FromFullPath(Path.GetDirectoryName(Application.dataPath)); } return _root.Value; } @@ -71,7 +65,12 @@ namespace VRMShaders { throw new ArgumentNullException(); } - src = src.Replace('\\', '/'); + src = Path.GetFullPath(src).Replace('\\', '/'); + if (src.Length > 1 && src[src.Length - 1] == '/') + { + // drop last / + src = src.Substring(0, src.Length - 1); + } if (src[0] == '/') { FullPath = src; @@ -135,6 +134,19 @@ namespace VRMShaders return FromFullPath(Path.Combine(FullPath, child)); } + public bool IsDescendantOf(PathObject ascendant) + { + if (!FullPath.StartsWith(ascendant.FullPath)) + { + return false; + } + if (FullPath[ascendant.FullPath.Length] != '/') + { + return false; + } + return true; + } + public byte[] ReadAllBytes() { return File.ReadAllBytes(FullPath);