Adding basic language support unfinished

This commit is contained in:
2022-12-15 21:11:37 -08:00
parent 2f48f61e9a
commit 5b48fb3901
22 changed files with 364 additions and 4 deletions

View File

@ -91,6 +91,12 @@ size_t AssetLoader::loadRaw(uint8_t **buffer) {
return read;
}
size_t AssetLoader::setPosition(size_t position) {
assertTrue(position >= 0);
this->rewind();
return this->skip(position);
}
AssetLoader::~AssetLoader() {
if(this->handle != nullptr) {
this->close();

View File

@ -71,6 +71,14 @@ namespace Dawn {
*/
size_t getPosition();
/**
* Sets the position of the file read head.
*
* @param position Position to set to.
* @return How many bytes were skipped / rewound.
*/
size_t setPosition(size_t position);
/**
* Loads the entire file into a raw buffer.
* @param buffer Pointer to where a pointer to the buffer will be stored.

View File

@ -7,6 +7,7 @@
#include "Asset.hpp"
#include "util/array.hpp"
#include "assets/LanguageAsset.hpp"
#include "assets/TextureAsset.hpp"
#include "assets/TilesetAsset.hpp"
#include "assets/TrueTypeAsset.hpp"

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);
};
}