Nothing is working but it is on the right track! ASYNCIFY removed, manual waiting instead and some proxying logic

This commit is contained in:
msqr1
2024-01-28 00:09:16 -08:00
parent efb56e19f9
commit 8796f35445
22 changed files with 239 additions and 7375 deletions

View File

@@ -1,5 +1,5 @@
#include "genericModel.h"
fetchData::fetchData(const std::string& storepath, bool* successful, std::atomic_flag* blocker, genericModel* self) : storepath(storepath), successful(successful), blocker(blocker), self(self) {};
genericModel::genericModel(const std::string &url, const std::string& storepath, const std::string &id) : url(url), id(id) {
fs::current_path("/opfs");
fs::create_directories(storepath);
@@ -7,9 +7,7 @@ genericModel::genericModel(const std::string &url, const std::string& storepath,
}
bool genericModel::checkId(const std::string& id) {
std::ifstream file {"id", std::ifstream::binary};
if(!file.is_open()) {
return false;
};
if(!file.is_open()) return false;
long long size {file.seekg(0, std::ios::end).tellg()};
std::string oldid(size, ' ');
file.seekg(0);
@@ -17,41 +15,47 @@ bool genericModel::checkId(const std::string& id) {
return id.compare(oldid) == 0 ? true : false;
}
bool genericModel::loadModel(const std::string& storepath) {
if(!checkModel() || !checkId(id)) {
char filename[] {"/opfs/XXXXXX.tzst"};
close(mkostemps(filename, 5, O_PATH));
if(emscripten_wget(url.c_str(),filename) == 1) {
throwJS("Unable to fetch model");
return false;
}
if(!extractModel(filename)) {
if(checkModel() && checkId(id)) return true;
std::atomic_flag blocker{};
bool successful{};
fetchData data{storepath, &successful, &blocker, this};
emscripten_async_wget2(url.c_str(), "A_fIlEnAmE_tHaT_dOeS_nOt_CoNfLiCt.tzst", "GET", nullptr, (void*)&data, [](unsigned handle, void* arg, const char* fname){
fetchData* data = (fetchData*)arg;
if(!extractModel()) {
throwJS("Unable to extract model");
return false;
return;
}
fs::remove(filename);
if(!checkModel()) {
fs::remove(fname);
if(!data->self->checkModel()) {
throwJS("Model URL contains invalid model files");
fs::current_path("/opfs");
fs::remove_all(storepath);
return false;
fs::remove_all(data->storepath);
return;
}
std::ofstream idFile("id");
if(!idFile.is_open()) {
throwJS("Unable to write new id");
fs::remove_all(storepath);
return false;
fs::current_path("/opfs");
fs::remove_all(data->storepath);
return;
}
idFile << id;
}
return true;
idFile << data->self->id;
*data->successful = true;
data->blocker->notify_one();
}, [](unsigned handle, void* arg, int status) {
throwJS("Unable to fetch model");
((fetchData*)arg)->blocker->notify_one();
}, nullptr);
blocker.wait(false, std::memory_order_relaxed);
return successful;
}
bool genericModel::extractModel(char* name) {
bool genericModel::extractModel() {
std::string path{};
archive* src {archive_read_new()};
archive_entry* entry {};
archive_read_support_filter_all(src);
archive_read_support_format_all(src);
archive_read_open_filename(src, name,10240);
archive_read_open_filename(src, "A_fIlEnAmE_tHaT_dOeS_nOt_CoNfLiCt.tzst",10240);
if(archive_errno(src) != 0) return false;
while (archive_read_next_header(src, &entry) == ARCHIVE_OK) {
path = archive_entry_pathname(entry);