File size: 3,394 Bytes
fc9bc1a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
using System;
using System.IO;
using System.Linq;
using System.Text;

namespace UnityMeshSimplifier
{
    internal static class IOUtils
    {
        internal static string MakeSafeRelativePath(string path)
        {
            if (string.IsNullOrEmpty(path))
                return null;

            path = path.Replace('\\', '/').Trim('/');

            if (Path.IsPathRooted(path))
                throw new ArgumentException("The path cannot be rooted.", "path");

            // Make the path safe
            var pathParts = path.Split(new [] { '/' }, StringSplitOptions.RemoveEmptyEntries);
            for (int i = 0; i < pathParts.Length; i++)
            {
                pathParts[i] = MakeSafeFileName(pathParts[i]);
            }
            return string.Join("/", pathParts);
        }

        internal static string MakeSafeFileName(string name)
        {
            char[] invalidFileNameChars = Path.GetInvalidFileNameChars();

            var sb = new StringBuilder(name.Length);
            bool lastWasInvalid = false;
            for (int i = 0; i < name.Length; i++)
            {
                char c = name[i];
                if (!invalidFileNameChars.Contains(c))
                {
                    sb.Append(c);
                }
                else if (!lastWasInvalid)
                {
                    lastWasInvalid = true;
                    sb.Append('_');
                }
            }
            return sb.ToString();
        }

#if UNITY_EDITOR
        internal static void CreateParentDirectory(string path)
        {
            int lastSlashIndex = path.LastIndexOf('/');
            if (lastSlashIndex != -1)
            {
                string parentPath = path.Substring(0, lastSlashIndex);
                if (!UnityEditor.AssetDatabase.IsValidFolder(parentPath))
                {
                    lastSlashIndex = parentPath.LastIndexOf('/');
                    if (lastSlashIndex != -1)
                    {
                        string folderName = parentPath.Substring(lastSlashIndex + 1);
                        string folderParentPath = parentPath.Substring(0, lastSlashIndex);
                        CreateParentDirectory(parentPath);
                        UnityEditor.AssetDatabase.CreateFolder(folderParentPath, folderName);
                    }
                    else
                    {
                        UnityEditor.AssetDatabase.CreateFolder(string.Empty, parentPath);
                    }
                }
            }
        }

        internal static bool DeleteEmptyDirectory(string path)
        {
            bool deletedAllSubFolders = true;
            var subFolders = UnityEditor.AssetDatabase.GetSubFolders(path);
            for (int i = 0; i < subFolders.Length; i++)
            {
                if (!DeleteEmptyDirectory(subFolders[i]))
                {
                    deletedAllSubFolders = false;
                }
            }

            if (!deletedAllSubFolders)
                return false;
            else if (!UnityEditor.AssetDatabase.IsValidFolder(path))
                return true;

            string[] assetGuids = UnityEditor.AssetDatabase.FindAssets(string.Empty, new string[] { path });
            if (assetGuids.Length > 0)
                return false;

            return UnityEditor.AssetDatabase.DeleteAsset(path);
        }
#endif
    }
}