少年修仙传客户端基础资源
hch
2024-04-01 d01413b00ef631ac20347716b23818b0b811f65f
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
#include "il2cpp-config.h"
 
#if IL2CPP_TARGET_POSIX && !IL2CPP_TINY_WITHOUT_DEBUGGER
 
#include "os/Directory.h"
#include "os/ErrorCodes.h"
#include "os/File.h"
#include "os/Posix/Error.h"
#include "utils/DirectoryUtils.h"
#include "utils/Memory.h"
#include "utils/PathUtils.h"
#include "utils/StringUtils.h"
#include <assert.h>
#include <errno.h>
#include <dirent.h>
#include <stdint.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/errno.h>
#include <sys/stat.h>
#include <sys/types.h>
 
namespace il2cpp
{
namespace os
{
    std::string Directory::GetCurrent(int *error)
    {
        char buf[PATH_MAX + 1];
        // Note: not all implementations would allocate a buffer when passing 0 to getcwd, as we used to do.
        // this does *not* seem to be part of the POSIX spec:
        // http://pubs.opengroup.org/onlinepubs/000095399/functions/getcwd.html
        char* cwd = getcwd(buf, PATH_MAX + 1);
 
        if (cwd == NULL)
        {
            *error = FileErrnoToErrorCode(errno);
            return std::string();
        }
 
        std::string directory(cwd);
 
        *error = kErrorCodeSuccess;
        return directory;
    }
 
    bool Directory::SetCurrent(const std::string& path, int *error)
    {
        const int ret = chdir(path.c_str());
 
        if (ret == -1)
        {
            *error = FileErrnoToErrorCode(errno);
            return false;
        }
 
        *error = kErrorCodeSuccess;
        return true;
    }
 
    bool Directory::Create(const std::string& path, int *error)
    {
        const int ret = mkdir(path.c_str(), 0777);
 
        if (ret == -1)
        {
            *error = PathErrnoToErrorCode(path, errno);
            return false;
        }
 
        *error = kErrorCodeSuccess;
        return true;
    }
 
    bool Directory::Remove(const std::string& path, int *error)
    {
        const int ret = rmdir(path.c_str());
 
        if (ret == -1)
        {
            *error = PathErrnoToErrorCode(path, errno);
            return false;
        }
 
        *error = kErrorCodeSuccess;
        return true;
    }
 
    static void DirectoryGlob(DIR *dir, const std::string& pattern, std::set<std::string>& result)
    {
        if (pattern.empty())
            return;
 
        std::string matchPattern = il2cpp::utils::CollapseAdjacentStars(pattern);
 
        struct dirent *entry;
 
        while ((entry = readdir(dir)) != NULL)
        {
            const std::string filename(entry->d_name);
 
            if (!il2cpp::utils::Match(filename, matchPattern))
                continue;
 
            result.insert(filename);
        }
    }
 
    static bool DirectoryGlob(const std::string& directoryPath, const std::string& pattern, std::set<std::string>& result, int* error)
    {
        DIR* dir = opendir(directoryPath.c_str());
 
        if (dir == NULL)
        {
            *error = PathErrnoToErrorCode(directoryPath, errno);
            return false;
        }
 
        DirectoryGlob(dir, pattern, result);
 
        closedir(dir);
 
        return true;
    }
 
    std::set<std::string> Directory::GetFileSystemEntries(const std::string& path, const std::string& pathWithPattern, int32_t attributes, int32_t mask, int* error)
    {
        const std::string directoryPath(il2cpp::utils::PathUtils::DirectoryName(pathWithPattern));
        const std::string pattern(il2cpp::utils::PathUtils::Basename(pathWithPattern));
 
        std::set<std::string> globResult;
 
        if (DirectoryGlob(directoryPath, pattern, globResult, error) == false)
            return std::set<std::string>();
 
        std::set<std::string> result;
 
        for (std::set<std::string>::const_iterator it = globResult.begin(), end = globResult.end(); it != end; ++it)
        {
            const std::string& filename = *it;
 
            if (filename == "." || filename == "..")
                continue;
 
            const std::string path(directoryPath + IL2CPP_DIR_SEPARATOR + filename);
 
            int attributeError;
            const int32_t pathAttributes = static_cast<int32_t>(File::GetFileAttributes(path, &attributeError));
 
            if (attributeError != kErrorCodeSuccess)
                continue;
 
            if ((pathAttributes & mask) == attributes)
                result.insert(path);
        }
 
 
        *error = kErrorCodeSuccess;
        return result;
    }
 
    Directory::FindHandle::FindHandle(const utils::StringView<Il2CppNativeChar>& searchPathWithPattern) :
        osHandle(NULL),
        handleFlags(os::kNoFindHandleFlags)
    {
        directoryPath = il2cpp::utils::PathUtils::DirectoryName(searchPathWithPattern);
        pattern = il2cpp::utils::PathUtils::Basename(searchPathWithPattern);
        pattern = il2cpp::utils::CollapseAdjacentStars(pattern);
    }
 
    Directory::FindHandle::~FindHandle()
    {
        IL2CPP_ASSERT(osHandle == NULL);
    }
 
    int32_t Directory::FindHandle::CloseOSHandle()
    {
        int32_t result = os::kErrorCodeSuccess;
 
        if (osHandle != NULL)
        {
            int32_t ret = closedir(static_cast<DIR*>(osHandle));
            if (ret != 0)
                result = FileErrnoToErrorCode(errno);
 
            osHandle = NULL;
        }
 
        return result;
    }
 
    os::ErrorCode Directory::FindFirstFile(FindHandle* findHandle, const utils::StringView<Il2CppNativeChar>& searchPathWithPattern, Il2CppNativeString* resultFileName, int32_t* resultAttributes)
    {
        DIR* dir = opendir(findHandle->directoryPath.c_str());
        if (dir == NULL)
            return PathErrnoToErrorCode(findHandle->directoryPath, errno);
 
        findHandle->SetOSHandle(dir);
        return FindNextFile(findHandle, resultFileName, resultAttributes);
    }
 
    os::ErrorCode Directory::FindNextFile(FindHandle* findHandle, Il2CppNativeString* resultFileName, int32_t* resultAttributes)
    {
        errno = 0;
 
        dirent* entry;
        while ((entry = readdir(static_cast<DIR*>(findHandle->osHandle))) != NULL)
        {
            const Il2CppNativeString filename(entry->d_name);
 
            if (il2cpp::utils::Match(filename, findHandle->pattern))
            {
                const Il2CppNativeString path = utils::PathUtils::Combine(findHandle->directoryPath, filename);
 
                int attributeError;
                const int32_t pathAttributes = static_cast<int32_t>(File::GetFileAttributes(path, &attributeError));
 
                if (attributeError == kErrorCodeSuccess)
                {
                    *resultFileName = filename;
                    *resultAttributes = pathAttributes;
                    return os::kErrorCodeSuccess;
                }
            }
        }
 
        if (errno != 0)
            return FileErrnoToErrorCode(errno);
 
        return os::kErrorCodeNoMoreFiles;
    }
}
}
 
#endif