Dawn/src/dawn/asset/loaders/JSONLoader.cpp
2024-09-27 21:55:53 -05:00

46 lines
1.1 KiB
C++

// Copyright (c) 2024 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "JSONLoader.hpp"
#include "assert/assert.hpp"
using namespace Dawn;
JSONLoader::JSONLoader(const std::string name) :
AssetLoader(name),
loader(name + ".json"),
state(JSONLoaderLoadState::INITIAL)
{
data = std::make_shared<json>();
}
void JSONLoader::updateAsync() {
if(this->state != JSONLoaderLoadState::INITIAL) return;
this->state = JSONLoaderLoadState::LOADING_FILE;
this->loader.open();
auto size = this->loader.getSize();
auto buffer = new uint8_t[size + 1];
assertNotNull(buffer, "Failed to allocate buffer!");
this->state = JSONLoaderLoadState::PARSING_DATA;
auto read = this->loader.read(buffer, size);
assertTrue(read == size, "Failed to read entire file!");
buffer[size] = '\0';
*data = json::parse(buffer);
delete[] buffer;
this->state = JSONLoaderLoadState::DONE;
this->loaded = true;
}
void JSONLoader::updateSync() {
}
JSONLoader::~JSONLoader() {
this->data = nullptr;
}