UnityPath.cs 10 KB
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 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424
using System;
using System.IO;
using UnityEngine;
using System.Collections.Generic;
#if UNITY_EDITOR
using UnityEditor;
#endif


namespace UniGLTF
{
    /// <summary>
    /// relative path from Unity project root.
    /// For AssetDatabase.
    /// </summary>
    public struct UnityPath
    {
        #region UnityPath
        public string Value
        {
            get;
            private set;
        }

        public override string ToString()
        {
            return string.Format("unity://{0}", Value);
        }

        public bool IsNull
        {
            get { return Value == null; }
        }

        public bool IsUnderAssetsFolder
        {
            get
            {
                if (IsNull)
                {
                    return false;
                }
                return Value == "Assets" || Value.StartsWith("Assets/");
            }
        }

        public bool IsStreamingAsset
        {
            get
            {
                if (IsNull)
                {
                    return false;
                }

                return FullPath.StartsWith(Application.streamingAssetsPath + "/");
            }
        }

        public string FileNameWithoutExtension
        {
            get { return Path.GetFileNameWithoutExtension(Value); }
        }

        public string Extension
        {
            get { return Path.GetExtension(Value); }
        }

        public UnityPath Parent
        {
            get
            {
                if (IsNull)
                {
                    return default(UnityPath);
                }

                return new UnityPath(Path.GetDirectoryName(Value));
            }
        }

        public bool HasParent
        {
            get
            {
                return !string.IsNullOrEmpty(Value);
            }
        }

        static readonly char[] EscapeChars = new char[]
        {
            '\\',
            '/',
            ':',
            '*',
            '?',
            '"',
            '<',
            '>',
            '|',
        };

        static string EscapeFilePath(string path)
        {
            foreach (var x in EscapeChars)
            {
                path = path.Replace(x, '+');
            }
            return path;
        }

        public UnityPath Child(string name)
        {
            if (IsNull)
            {
                throw new NotImplementedException();
            }
            else if (Value == "")
            {
                return new UnityPath(name);
            }
            else
            {
                return new UnityPath(Value + "/" + name);
            }
        }

        public override int GetHashCode()
        {
            if (IsNull)
            {
                return 0;
            }
            return Value.GetHashCode();
        }

        public override bool Equals(object obj)
        {
            if(obj is UnityPath)
            {
                var rhs = (UnityPath)obj;
                if(Value==null && rhs.Value == null)
                {
                    return true;
                }
                else if (Value == null)
                {
                    return false;
                }
                else if (rhs.Value == null)
                {
                    return false;
                }
                else
                {
                    return Value == rhs.Value;
                }
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// Remove extension and add suffix
        /// </summary>
        /// <param name="prefabPath"></param>
        /// <param name="suffix"></param>
        /// <returns></returns>
        public UnityPath GetAssetFolder(string suffix)
        {
            if (!IsUnderAssetsFolder)
            {
                throw new NotImplementedException();
            }

            return new UnityPath(
                string.Format("{0}/{1}{2}",
                Parent.Value,
                FileNameWithoutExtension,
                suffix
                ));
        }

        UnityPath(string value) : this()
        {
            Value = value.Replace("\\", "/");
        }

        /// <summary>
        ///
        /// </summary>
        /// <param name="unityPath">Relative from unity current path. GetParent(Application.dataPath)</param>
        /// <returns></returns>
        public static UnityPath FromUnityPath(string unityPath)
        {
            if (String.IsNullOrEmpty(unityPath))
            {
                return new UnityPath
                {
                    Value=""
                };
            }
            return FromFullpath(Path.GetFullPath(unityPath));
        }
        #endregion

        #region FullPath
        static string s_basePath;
        static string BaseFullPath
        {
            get
            {
                if (string.IsNullOrEmpty(s_basePath))
                {
                    s_basePath = Path.GetFullPath(Application.dataPath + "/..").Replace("\\", "/");
                }
                return s_basePath;
            }
        }

        static string AssetFullPath
        {
            get
            {
                return BaseFullPath + "/Assets";
            }
        }

        public string FullPath
        {
            get
            {
                if (IsNull)
                {
                    throw new NotImplementedException();
                }
                return Path.Combine(BaseFullPath, Value).Replace("\\", "/");
            }
        }

        public bool IsFileExists
        {
            get { return File.Exists(FullPath); }
        }

        public bool IsDirectoryExists
        {
            get { return Directory.Exists(FullPath); }
        }

        /// <summary>
        ///
        /// </summary>
        /// <param name="fullPath">C:/path/to/file</param>
        /// <returns></returns>
        public static UnityPath FromFullpath(string fullPath)
        {
            if(fullPath == null)
            {
                fullPath = "";
            }
            fullPath = fullPath.Replace("\\", "/");

            if (fullPath == BaseFullPath) {
                return new UnityPath
                {
                    Value=""
                };
            }
            else if(fullPath.StartsWith(BaseFullPath + "/"))
            {
                return new UnityPath(fullPath.Substring(BaseFullPath.Length + 1));
            }
            else
            {
                return default(UnityPath);
            }
        }

        public static bool IsUnderAssetFolder(string fullPath)
        {
            return fullPath.Replace("\\", "/").StartsWith(AssetFullPath);
        }
        #endregion

        public IEnumerable<UnityPath> TravserseDir()
        {
            if (IsDirectoryExists)
            {
                yield return this;

                foreach(var child in ChildDirs)
                {
                    foreach(var x in child.TravserseDir())
                    {
                        yield return x;
                    }
                }
            }
        }

        public IEnumerable<UnityPath> ChildDirs
        {
            get
            {
                foreach(var x in Directory.GetDirectories(FullPath))
                {
                    yield return UnityPath.FromFullpath(x);
                }
            }
        }

        public IEnumerable<UnityPath> ChildFiles
        {
            get
            {
                foreach (var x in Directory.GetFiles(FullPath))
                {
                    yield return UnityPath.FromFullpath(x);
                }
            }
        }

#if UNITY_EDITOR
        public T GetImporter<T>() where T : AssetImporter
        {
            return AssetImporter.GetAtPath(Value) as T;
        }

        public static UnityPath FromAsset(UnityEngine.Object asset)
        {
            return new UnityPath(AssetDatabase.GetAssetPath(asset));
        }

        public void ImportAsset()
        {
            if (!IsUnderAssetsFolder)
            {
                throw new NotImplementedException();
            }
            AssetDatabase.ImportAsset(Value);
        }

        public void EnsureFolder()
        {
            if (IsNull)
            {
                throw new NotImplementedException();
            }

            if (HasParent)
            {
                Parent.EnsureFolder();
            }

            if (!IsDirectoryExists)
            {
                var parent = Parent;
                // ensure parent
                parent.ImportAsset();
                // create
                AssetDatabase.CreateFolder(
                    parent.Value,
                    Path.GetFileName(Value)
                    );
                ImportAsset();
            }
        }

        public UnityEngine.Object[] GetSubAssets()
        {
            if (!IsUnderAssetsFolder)
            {
                throw new NotImplementedException();
            }

            return AssetDatabase.LoadAllAssetsAtPath(Value);
        }

        public void CreateAsset(UnityEngine.Object o)
        {
            if (!IsUnderAssetsFolder)
            {
                throw new NotImplementedException();
            }

            AssetDatabase.CreateAsset(o, Value);
        }

        public void AddObjectToAsset(UnityEngine.Object o)
        {
            if (!IsUnderAssetsFolder)
            {
                throw new NotImplementedException();
            }

            AssetDatabase.AddObjectToAsset(o, Value);
        }

        public T LoadAsset<T>() where T : UnityEngine.Object
        {
            if (!IsUnderAssetsFolder)
            {
                throw new NotImplementedException();
            }

            return AssetDatabase.LoadAssetAtPath<T>(Value);
        }

        public UnityPath GenerateUniqueAssetPath()
        {
            if (!IsUnderAssetsFolder)
            {
                throw new NotImplementedException();
            }

            return new UnityPath(AssetDatabase.GenerateUniqueAssetPath(Value));
        }
        #endif
    }
}