Adding basic language support unfinished

This commit is contained in:
2022-12-15 21:11:37 -08:00
parent 02b4e3f9aa
commit 36ad2fecca
23 changed files with 367 additions and 4 deletions

View File

@ -6,6 +6,7 @@
# Sources
target_sources(${DAWN_TARGET_NAME}
PRIVATE
LanguageAsset.cpp
TextureAsset.cpp
TilesetAsset.cpp
TrueTypeAsset.cpp

View File

@ -0,0 +1,84 @@
// 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);
// 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);
// 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);
}

View File

@ -0,0 +1,31 @@
// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "../Asset.hpp"
#include "../AssetLoader.hpp"
namespace Dawn {
struct AssetLanguageValue {
size_t begin;
size_t length;
};
class LanguageAsset : public Asset {
protected:
AssetLoader loader;
uint8_t state = 0x00;
std::map<std::string, struct AssetLanguageValue> values;
uint8_t buffer[1024];
public:
LanguageAsset(AssetManager *man, std::string name);
void updateSync() override;
void updateAsync() override;
std::string getValue(std::string key);
};
}