2022-06-25 01:55:54 +02:00
|
|
|
#include "file_cache.h"
|
|
|
|
|
2022-06-27 01:31:05 +02:00
|
|
|
#include "core/os/dir_access.h"
|
|
|
|
#include "core/os/file_access.h"
|
2022-07-01 22:13:45 +02:00
|
|
|
#include "core/os/os.h"
|
2022-08-17 14:19:55 +02:00
|
|
|
#include "core/string/print_string.h"
|
2022-08-17 15:35:28 +02:00
|
|
|
#include "core/config/project_settings.h"
|
2022-06-25 01:55:54 +02:00
|
|
|
|
2022-06-27 12:26:57 +02:00
|
|
|
String FileCache::get_wwwroot() {
|
2022-07-01 22:13:45 +02:00
|
|
|
return _wwwroot_orig;
|
2022-06-27 12:26:57 +02:00
|
|
|
}
|
|
|
|
void FileCache::set_wwwroot(const String &val) {
|
2022-07-01 22:13:45 +02:00
|
|
|
_wwwroot_orig = val;
|
|
|
|
}
|
|
|
|
|
|
|
|
String FileCache::get_wwwroot_abs() {
|
2022-07-02 19:58:06 +02:00
|
|
|
return _wwwroot;
|
2022-06-27 12:26:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int FileCache::get_cache_invalidation_time() {
|
2022-06-27 15:47:10 +02:00
|
|
|
return static_cast<int>(cache_invalidation_time);
|
2022-06-27 12:26:57 +02:00
|
|
|
}
|
|
|
|
void FileCache::set_cache_invalidation_time(const int &val) {
|
2022-06-27 15:47:10 +02:00
|
|
|
cache_invalidation_time = static_cast<uint64_t>(val);
|
2022-06-27 12:26:57 +02:00
|
|
|
}
|
|
|
|
|
2022-06-25 01:55:54 +02:00
|
|
|
void FileCache::wwwroot_register_file(const String &file_path) {
|
2022-07-01 22:13:45 +02:00
|
|
|
RegisteredFileEntry e;
|
|
|
|
e.orig_path = file_path;
|
|
|
|
e.lowercase_path = file_path.to_lower();
|
|
|
|
|
|
|
|
_registered_files.push_back(e);
|
2022-06-25 01:55:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void FileCache::wwwroot_deregister_file(const String &file_path) {
|
2022-07-01 22:13:45 +02:00
|
|
|
for (int i = 0; i < _registered_files.size(); ++i) {
|
|
|
|
const RegisteredFileEntry &e = _registered_files[i];
|
|
|
|
|
|
|
|
if (file_path == e.orig_path) {
|
|
|
|
_registered_files.remove(i);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2022-06-25 01:55:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool FileCache::wwwroot_has_file(const String &file_path) {
|
2022-07-01 22:13:45 +02:00
|
|
|
//return registered_files.has(file_path);
|
|
|
|
|
|
|
|
for (int i = 0; i < _registered_files.size(); ++i) {
|
|
|
|
const RegisteredFileEntry &e = _registered_files[i];
|
|
|
|
|
|
|
|
if (file_path == e.lowercase_path) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
int FileCache::wwwroot_get_file_index(const String &file_path) {
|
|
|
|
for (int i = 0; i < _registered_files.size(); ++i) {
|
|
|
|
const RegisteredFileEntry &e = _registered_files[i];
|
|
|
|
|
|
|
|
if (file_path == e.lowercase_path) {
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
String FileCache::wwwroot_get_file_orig_path(const int index) {
|
|
|
|
ERR_FAIL_INDEX_V(index, _registered_files.size(), "");
|
|
|
|
|
|
|
|
return _registered_files[index].orig_path;
|
2022-06-25 01:55:54 +02:00
|
|
|
}
|
|
|
|
|
2022-07-02 19:58:06 +02:00
|
|
|
String FileCache::wwwroot_get_file_orig_path_abs(const int index) {
|
|
|
|
return get_wwwroot_abs() + wwwroot_get_file_orig_path(index);
|
|
|
|
}
|
|
|
|
|
2022-06-25 01:55:54 +02:00
|
|
|
void FileCache::wwwroot_refresh_cache() {
|
|
|
|
_lock.write_lock();
|
|
|
|
|
2022-07-01 22:13:45 +02:00
|
|
|
_registered_files.clear();
|
2022-06-25 01:55:54 +02:00
|
|
|
|
2022-07-01 22:13:45 +02:00
|
|
|
if (_wwwroot_orig != "") {
|
2023-03-22 14:30:07 +01:00
|
|
|
_wwwroot = _wwwroot_orig;//DirAccess::get_filesystem_abspath_for(_wwwroot_orig);
|
2022-07-01 22:13:45 +02:00
|
|
|
_wwwroot = _wwwroot.path_clean_end_slash();
|
2022-07-02 19:58:06 +02:00
|
|
|
|
2022-07-01 22:13:45 +02:00
|
|
|
wwwroot_evaluate_dir(_wwwroot);
|
|
|
|
}
|
2022-06-25 01:55:54 +02:00
|
|
|
|
|
|
|
_lock.write_unlock();
|
|
|
|
}
|
|
|
|
|
2022-06-27 01:31:05 +02:00
|
|
|
void FileCache::wwwroot_evaluate_dir(const String &path, const bool should_exist) {
|
|
|
|
DirAccess *da = DirAccess::open(path);
|
2022-06-25 01:55:54 +02:00
|
|
|
|
2022-06-27 01:31:05 +02:00
|
|
|
ERR_FAIL_COND_MSG(!da, "Error opening wwwroot! folder: " + path);
|
2022-06-25 01:55:54 +02:00
|
|
|
|
2022-06-27 01:31:05 +02:00
|
|
|
da->list_dir_begin();
|
|
|
|
String f = da->get_next();
|
2022-06-25 01:55:54 +02:00
|
|
|
|
2022-06-27 01:31:05 +02:00
|
|
|
while (f != String()) {
|
2022-06-27 15:04:44 +02:00
|
|
|
if (f == "." || f == "..") {
|
|
|
|
f = da->get_next();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-06-27 01:31:05 +02:00
|
|
|
if (!da->current_is_dir()) {
|
|
|
|
String np = path + "/" + f;
|
2022-07-01 22:13:45 +02:00
|
|
|
np = np.substr(_wwwroot.size() - 1, np.size() - _wwwroot.size());
|
|
|
|
wwwroot_register_file(np);
|
2022-07-02 19:58:06 +02:00
|
|
|
|
2022-06-25 01:55:54 +02:00
|
|
|
} else {
|
2022-06-27 01:31:05 +02:00
|
|
|
wwwroot_evaluate_dir(path + "/" + f);
|
2022-06-25 01:55:54 +02:00
|
|
|
}
|
2022-06-27 01:31:05 +02:00
|
|
|
|
|
|
|
f = da->get_next();
|
2022-06-25 01:55:54 +02:00
|
|
|
}
|
2022-06-27 01:31:05 +02:00
|
|
|
da->list_dir_end();
|
2022-06-25 01:55:54 +02:00
|
|
|
|
2022-06-27 01:31:05 +02:00
|
|
|
memdelete(da);
|
2022-06-25 01:55:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool FileCache::get_cached_body(const String &path, String *body) {
|
|
|
|
//TODO ERROR MACRO body == null
|
|
|
|
|
|
|
|
_lock.read_lock();
|
|
|
|
CacheEntry *e = cache_map[path];
|
|
|
|
_lock.read_unlock();
|
|
|
|
|
|
|
|
if (!e) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-06-27 12:26:57 +02:00
|
|
|
if (cache_invalidation_time > 0) {
|
|
|
|
uint64_t current_timestamp = OS::get_singleton()->get_unix_time();
|
|
|
|
uint64_t diff = current_timestamp - e->timestamp;
|
2022-06-25 01:55:54 +02:00
|
|
|
|
2022-06-27 12:26:57 +02:00
|
|
|
if (diff > cache_invalidation_time) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
body->operator+=(e->body);
|
2022-06-25 01:55:54 +02:00
|
|
|
|
2022-06-27 12:26:57 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FileCache::has_cached_body(const String &path) {
|
|
|
|
//TODO ERROR MACRO body == null
|
|
|
|
|
|
|
|
_lock.read_lock();
|
|
|
|
CacheEntry *e = cache_map[path];
|
|
|
|
_lock.read_unlock();
|
|
|
|
|
|
|
|
if (!e) {
|
2022-06-25 01:55:54 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-06-27 12:26:57 +02:00
|
|
|
if (cache_invalidation_time > 0) {
|
|
|
|
uint64_t current_timestamp = OS::get_singleton()->get_unix_time();
|
|
|
|
uint64_t diff = current_timestamp - e->timestamp;
|
|
|
|
|
|
|
|
if (diff > cache_invalidation_time) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2022-06-25 01:55:54 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-06-27 12:26:57 +02:00
|
|
|
String FileCache::get_cached_body_bind(const String &path) {
|
|
|
|
//TODO ERROR MACRO body == null
|
|
|
|
|
|
|
|
_lock.read_lock();
|
|
|
|
CacheEntry *e = cache_map[path];
|
|
|
|
_lock.read_unlock();
|
|
|
|
|
|
|
|
if (!e) {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cache_invalidation_time > 0) {
|
|
|
|
uint64_t current_timestamp = OS::get_singleton()->get_unix_time();
|
|
|
|
uint64_t diff = current_timestamp - e->timestamp;
|
|
|
|
|
|
|
|
if (diff > cache_invalidation_time) {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return e->body;
|
|
|
|
}
|
|
|
|
|
2022-06-25 01:55:54 +02:00
|
|
|
void FileCache::set_cached_body(const String &path, const String &body) {
|
|
|
|
_lock.write_lock();
|
|
|
|
|
|
|
|
CacheEntry *e = cache_map[path];
|
|
|
|
|
|
|
|
if (!e) {
|
2022-06-27 12:26:57 +02:00
|
|
|
e = memnew(CacheEntry());
|
2022-06-25 01:55:54 +02:00
|
|
|
cache_map[path] = e;
|
|
|
|
}
|
|
|
|
|
2022-06-27 12:26:57 +02:00
|
|
|
uint64_t current_timestamp = OS::get_singleton()->get_unix_time();
|
2022-06-25 01:55:54 +02:00
|
|
|
|
|
|
|
e->timestamp = current_timestamp;
|
|
|
|
e->body = body;
|
|
|
|
|
|
|
|
_lock.write_unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
void FileCache::clear() {
|
|
|
|
_lock.write_lock();
|
|
|
|
|
2022-07-01 22:13:45 +02:00
|
|
|
_registered_files.clear();
|
2022-06-25 01:55:54 +02:00
|
|
|
|
2023-01-15 19:12:50 +01:00
|
|
|
for (RBMap<String, CacheEntry *>::Element *E = cache_map.front(); E; E++) {
|
2022-06-27 01:31:05 +02:00
|
|
|
CacheEntry *ce = E->get();
|
2022-06-25 01:55:54 +02:00
|
|
|
|
|
|
|
if (ce) {
|
2022-06-27 12:26:57 +02:00
|
|
|
memdelete(ce);
|
2022-06-25 01:55:54 +02:00
|
|
|
}
|
|
|
|
}
|
2022-06-27 01:31:05 +02:00
|
|
|
|
2022-06-25 01:55:54 +02:00
|
|
|
cache_map.clear();
|
|
|
|
|
|
|
|
_lock.write_unlock();
|
|
|
|
}
|
|
|
|
|
2022-06-27 01:31:05 +02:00
|
|
|
FileCache::FileCache() {
|
2022-06-27 12:26:57 +02:00
|
|
|
cache_invalidation_time = 0;
|
2022-06-25 01:55:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
FileCache::~FileCache() {
|
2022-07-01 22:13:45 +02:00
|
|
|
_registered_files.clear();
|
2022-06-25 01:55:54 +02:00
|
|
|
}
|
2022-06-27 12:26:57 +02:00
|
|
|
|
|
|
|
void FileCache::_bind_methods() {
|
|
|
|
ClassDB::bind_method(D_METHOD("get_wwwroot"), &FileCache::get_wwwroot);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_wwwroot", "val"), &FileCache::set_wwwroot);
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::STRING, "wwwroot"), "set_wwwroot", "get_wwwroot");
|
|
|
|
|
2023-01-04 00:40:28 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("get_wwwroot_abs"), &FileCache::get_wwwroot_abs);
|
|
|
|
|
2022-06-27 12:26:57 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("get_cache_invalidation_time"), &FileCache::get_cache_invalidation_time);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_cache_invalidation_time", "val"), &FileCache::set_cache_invalidation_time);
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::INT, "cache_invalidation_time"), "set_cache_invalidation_time", "get_cache_invalidation_time");
|
|
|
|
|
|
|
|
ClassDB::bind_method(D_METHOD("wwwroot_register_file", "file_path"), &FileCache::wwwroot_register_file);
|
|
|
|
ClassDB::bind_method(D_METHOD("wwwroot_deregister_file", "file_path"), &FileCache::wwwroot_deregister_file);
|
|
|
|
ClassDB::bind_method(D_METHOD("wwwroot_has_file", "file_path"), &FileCache::wwwroot_has_file);
|
2023-01-04 00:40:28 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("wwwroot_get_file_index", "file_path"), &FileCache::wwwroot_get_file_index);
|
|
|
|
ClassDB::bind_method(D_METHOD("wwwroot_get_file_orig_path", "index"), &FileCache::wwwroot_get_file_orig_path);
|
|
|
|
ClassDB::bind_method(D_METHOD("wwwroot_get_file_orig_path_abs", "index"), &FileCache::wwwroot_get_file_orig_path_abs);
|
2022-06-27 12:26:57 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("wwwroot_refresh_cache"), &FileCache::wwwroot_refresh_cache);
|
|
|
|
ClassDB::bind_method(D_METHOD("wwwroot_evaluate_dir", "file_path", "should_exist "), &FileCache::wwwroot_evaluate_dir, true);
|
|
|
|
|
|
|
|
ClassDB::bind_method(D_METHOD("get_cached_body", "path"), &FileCache::get_cached_body_bind);
|
|
|
|
ClassDB::bind_method(D_METHOD("has_cached_body", "path"), &FileCache::has_cached_body);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_cached_body", "path", "body"), &FileCache::set_cached_body);
|
|
|
|
|
|
|
|
ClassDB::bind_method(D_METHOD("clear"), &FileCache::clear);
|
|
|
|
}
|
2023-01-04 00:40:28 +01:00
|
|
|
|