86 lines
2.2 KiB
C++
86 lines
2.2 KiB
C++
// Copyright (c) 2022 Dominic Masters
|
|
//
|
|
// This software is released under the MIT License.
|
|
// https://opensource.org/licenses/MIT
|
|
|
|
#include "LanguageAsset.hpp"
|
|
|
|
using namespace Dawn;
|
|
|
|
LanguageAsset::LanguageAsset(AssetManager *man, std::string name) :
|
|
Asset(man, name),
|
|
loader(name + ".language")
|
|
{
|
|
|
|
}
|
|
|
|
void LanguageAsset::updateSync() {
|
|
|
|
}
|
|
|
|
void LanguageAsset::updateAsync() {
|
|
assertTrue(this->state == 0x00, "LanguageAsset::updateAsync: State must be 0x00");
|
|
|
|
// Open Asset
|
|
this->state = 0x01;
|
|
this->loader.open();
|
|
|
|
// Get Length
|
|
this->state = 0x02;
|
|
this->loader.end();
|
|
this->state = 0x03;
|
|
size_t len = this->loader.getPosition();
|
|
this->state = 0x04;
|
|
this->loader.rewind();
|
|
|
|
// Create buffer
|
|
this->state = 0x05;
|
|
size_t position = 0;
|
|
|
|
// Loop over CSV
|
|
while(position < len) {
|
|
this->loader.read(buffer, 1024);
|
|
|
|
// Get strings
|
|
uint8_t *keyStart = buffer;
|
|
uint8_t *keyEnd = (uint8_t*)strchr((char*)keyStart, '|');
|
|
*keyEnd = '\0';
|
|
uint8_t *valueStart = keyEnd + 1;
|
|
uint8_t *valueEnd = (uint8_t*)strchr((char*)valueStart, '|');
|
|
|
|
// Load value positions
|
|
struct AssetLanguageValue value;
|
|
value.begin = position + (size_t)(valueStart - buffer);
|
|
value.length = (size_t)(valueEnd - valueStart);
|
|
|
|
// Prepare for next string.
|
|
position = position + (size_t)(valueEnd - buffer + 1);
|
|
this->loader.rewind();
|
|
this->loader.skip(position);
|
|
|
|
// Store strings.
|
|
std::string key((char *)keyStart);
|
|
this->values[key] = value;
|
|
}
|
|
|
|
this->state = 0x06;
|
|
this->loader.rewind();
|
|
|
|
this->state = 0x07;
|
|
this->loaded = true;
|
|
}
|
|
|
|
std::string LanguageAsset::getValue(std::string key) {
|
|
assertTrue(this->state == 0x07, "LanguageAsset::getValue: State must be 0x07");
|
|
assertMapHasKey(this->values, key, "LanguageAsset::getValue: Key does not exist");
|
|
assertTrue(this->values[key].begin >= 0 && this->values[key].length > 0, "LanguageAsset::getValue: Value is invalid");
|
|
|
|
// Get the positions
|
|
struct AssetLanguageValue value = this->values[key];
|
|
|
|
this->loader.setPosition(value.begin);
|
|
this->loader.read(buffer, value.length);
|
|
buffer[value.length] = '\0';
|
|
|
|
return std::string((char *)buffer, value.length + 1);
|
|
} |