-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgodot_vfs.cpp
More file actions
139 lines (116 loc) · 3.77 KB
/
godot_vfs.cpp
File metadata and controls
139 lines (116 loc) · 3.77 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
#include "godot_vfs.h"
#include "os/os.h"
#include "math_funcs.h"
#define MAX_PATH_NAME 512
#include "godot_vfs_file.h"
int godot_vfs_open(sqlite3_vfs* vfs, const char* filename, sqlite3_file* file, int flags, int* outFlags)
{
godot_vfs_File* p = reinterpret_cast<godot_vfs_File*>(file);
ERR_FAIL_COND_V(filename == NULL, SQLITE_IOERR); // TODO: Support temp databases
// Get flags
// TODO: Support exclusive and create
int godot_flags = 0;
if (flags & SQLITE_OPEN_READONLY) godot_flags |= FileAccess::READ;
if (flags & SQLITE_OPEN_READWRITE) godot_flags |= FileAccess::READ_WRITE;
// Open file
p->file = FileAccess::open(String::utf8(filename), godot_flags, NULL);
ERR_FAIL_COND_V(!p->file, SQLITE_CANTOPEN);
// Return flags, we fail if they request the wrong access
if (outFlags) {
*outFlags = flags;
}
p->base.pMethods = &godot_vfs_File_io_methods; // This must be at end to prevent a call to xClose
return SQLITE_OK;
}
int godot_vfs_delete(sqlite3_vfs* vfs, const char* name, int syncDir)
{
return SQLITE_OK;
}
int godot_vfs_access(sqlite3_vfs* vfs, const char* name, int flags, int* resultOut)
{
return SQLITE_OK;
}
int godot_vfs_fullPathname(sqlite3_vfs* vfs, const char* name, int nOut, char* str_out)
{
for (int i = 0; i < nOut; ++i) {
str_out[i] = name[i];
if (name[i] == '\0') {
break;
}
}
return SQLITE_OK;
}
void* godot_vfs_dlOpen(sqlite3_vfs* vfs, const char* filename)
{
return 0;
}
void godot_vfs_dlError(sqlite3_vfs* vfs, int nBytes, char* errMsg)
{
sqlite3_snprintf(nBytes, errMsg, "Loadable extensions are not supported");
errMsg[nBytes-1] = '\0';
}
void (*godot_vfs_dlSym(sqlite3_vfs* vfs, void* data, const char* symbol))(void)
{
return 0;
}
void godot_vfs_dlClose(sqlite3_vfs* vfs, void* data)
{
return;
}
int godot_vfs_randomness(sqlite3_vfs* vfs, int nBytes, char* byte)
{
for (int i = 0; i < nBytes; ++i) {
byte[i] = Math::rand();
}
return SQLITE_OK;
}
int godot_vfs_sleep(sqlite3_vfs* vfs, int microseconds)
{
OS::get_singleton()->delay_usec(microseconds);
return microseconds;
}
int godot_vfs_currentTime(sqlite3_vfs* vfs, double* time)
{
uint64_t unix_time = OS::get_singleton()->get_unix_time();
*time = unix_time/86400.0 + 2440587.5; // Add the number of days since julian time
return SQLITE_OK;
}
int godot_vfs_getLastError(sqlite3_vfs* vfs, int nBuf, char* buf)
{
// TODO: Implement properly
return 0;
}
int godot_vfs_currentTimeInt64(sqlite3_vfs* vfs, sqlite3_int64* now)
{
uint64_t unix_time = OS::get_singleton()->get_unix_time();
*now = unix_time + 210866760000; // Add the number of ms since julian time
return SQLITE_OK;
}
sqlite3_vfs* sqlite3_godot_vfs()
{
static sqlite3_vfs godot_vfs = {
3, // Struct Version Number
sizeof(godot_vfs_File), // File struct size
MAX_PATH_NAME, // 512 characters in a filename is good enough for anyone
0, // pNext, set automatically
"godot", // Name of this vfs
NULL, // User data if needed
&godot_vfs_open, // Open function
&godot_vfs_delete, // Delete function
&godot_vfs_access, // Access function
&godot_vfs_fullPathname, // Converts relative pathnames to full pathnames
&godot_vfs_dlOpen, // Not used
&godot_vfs_dlError, // Not used
&godot_vfs_dlSym, // Not used
&godot_vfs_dlClose, // Not used
&godot_vfs_randomness, // Provides randomness
&godot_vfs_sleep, // Sleep function
&godot_vfs_currentTime, // Gets the current time in a double
&godot_vfs_getLastError, // Gets the last error from the vfs
&godot_vfs_currentTimeInt64, // Gets the curent time in an int64
NULL, // Not used
NULL, // Not used
NULL // Not used
};
return &godot_vfs;
}