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

@ -21,6 +21,7 @@ add_subdirectory(assert)
add_subdirectory(asset)
add_subdirectory(display)
add_subdirectory(input)
add_subdirectory(locale)
add_subdirectory(poker)
add_subdirectory(scene)
add_subdirectory(time)

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

View File

@ -11,6 +11,7 @@
#include "input/InputManager.hpp"
#include "time/TimeManager.hpp"
#include "input/InputBinds.hpp"
#include "locale/LanguageManager.hpp"
#define DAWN_GAME_INIT_RESULT_SUCCESS 0
#define DAWN_GAME_UPDATE_RESULT_SUCCESS 0

View File

@ -0,0 +1,11 @@
# Copyright (c) 2022 Dominic Masters
#
# This software is released under the MIT License.
# https://opensource.org/licenses/MIT
# Sources
target_sources(${DAWN_TARGET_NAME}
PRIVATE
LanguageManager.cpp
)

View File

@ -0,0 +1,19 @@
// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "LanguageManager.hpp"
#include "game/DawnGame.hpp"
using namespace Dawn;
LanguageManager::LanguageManager(DawnGame *game) {
this->game = game;
auto lang = this->game->assetManager.get<LanguageAsset>("language_en");
while(!lang->loaded) {
lang->updateAsync();
}
std::cout << lang->getValue("test2");
}

View File

@ -0,0 +1,29 @@
// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "event/Event.hpp"
#include "asset/AssetManager.hpp"
namespace Dawn {
class DawnGame;
struct LocalizedString {
std::string data;
};
class LanguageManager {
private:
DawnGame *game;
LanguageAsset *asset;
public:
Event<> eventLanguageChanged;
LanguageManager(DawnGame *game);
struct LocalizedString getString(std::string key);
};
}

View File

@ -23,6 +23,7 @@ add_subdirectory(visualnovel)
add_subdirectory(scenes)
# Assets
tool_language(language_en en.csv)
tool_texture(texture_test texture_test.png)
tool_texture(texture_city_day borrowed/city_day.png)
tool_texture(texture_city_night borrowed/city_night.png)
@ -40,6 +41,7 @@ tool_truetype(truetype_ark
)
add_dependencies(${DAWN_TARGET_NAME}
language_en
texture_test
tileset_penny
truetype_ark

View File

@ -14,7 +14,8 @@ TextureAsset *assetTexture;
DawnGame::DawnGame(DawnHost *host) :
host(host),
renderManager(this),
inputManager(this)
inputManager(this),
languageManager(this)
{
}

View File

@ -15,6 +15,7 @@ namespace Dawn {
AssetManager assetManager;
InputManager inputManager;
TimeManager timeManager;
LanguageManager languageManager;
DawnGame(DawnHost *host);
int32_t init() override;

View File

@ -13,10 +13,16 @@
namespace Dawn {
class PokerVNScene : public SimpleVNScene {
protected:
virtual std::vector<PokerPlayer*> getPokerPlayers() = 0;
void vnStage() override;
std::vector<Asset*> getRequiredAssets() override;
/**
* Returns the Poker Players that are in this poker scene.
*
* @return List of Poker Players.
*/
virtual std::vector<PokerPlayer*> getPokerPlayers() = 0;
public:
PokerGame *pokerGame;
std::vector<PokerPlayer*> pokerPlayers;

View File

@ -4,4 +4,5 @@
# https://opensource.org/licenses/MIT
add_subdirectory(display)
add_subdirectory(file)
add_subdirectory(file)
add_subdirectory(locale)

View File

@ -88,6 +88,7 @@ int main(int argc, char *args[]) {
size_t readSize = fread(ttfData, 1, fileSize, file);
fclose(file);
if(readSize < fileSize) {
free(ttfData);
printf("Failed to read all data form TTF\n");
return 1;
}

View File

@ -0,0 +1,15 @@
# Copyright (c) 2021 Dominic Msters
#
# This software is released under the MIT License.
# https://opensource.org/licenses/MIT
add_subdirectory(languagegen)
# Language Tool
function(tool_language target in)
add_custom_target(${target}
COMMAND languagegen "${DAWN_ASSETS_SOURCE_DIR}/${in}" "${DAWN_ASSETS_BUILD_DIR}/${target}"
COMMENT "Generating texture ${target} from ${in}"
DEPENDS languagegen
)
endfunction()

View File

@ -0,0 +1,25 @@
# Copyright (c) 2021 Dominic Msters
#
# This software is released under the MIT License.
# https://opensource.org/licenses/MIT
# Texture Build Tool
project(languagegen VERSION 1.0)
add_executable(languagegen)
target_sources(languagegen
PRIVATE
main.c
../../utils/file.c
../../utils/image.c
../../utils/csv.c
)
target_include_directories(languagegen
PUBLIC
${CMAKE_CURRENT_LIST_DIR}/../../
${CMAKE_CURRENT_LIST_DIR}
)
target_link_libraries(languagegen
PUBLIC
${DAWN_BUILD_HOST_LIBS}
stb
)

View File

@ -0,0 +1,110 @@
/**
* Copyright (c) 2021 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "../../utils/common.h"
#include "../../utils/file.h"
#include "../../utils/csv.h"
int main(int argc, char *argv[]) {
FILE *file;
char path[FILENAME_MAX + 1];
csv_t csv;
char *in;
char *out;
char *buffer;
char sep = '|';
if(argc != 3) {
printf("Invalid number of arguments\n");
return 1;
}
// Set up strings
in = argv[1];
out = argv[2];
// Normalize slashes
fileNormalizeSlashes(in);
fileNormalizeSlashes(out);
// Check the output doesn't already exist
sprintf(path, "%s.language", out);
// file = fopen(path, "rb");
// if(file != NULL) {
// fclose(file);
// return 0;
// }
// Read in original CSV string
file = fopen(in, "rb");
if(file == NULL) {
printf("Failed to open file!\n");
return 1;
}
// Seek to end, get length, seek back to start.
fseek(file, 0, SEEK_END);
size_t fileSize = ftell(file);
fseek(file, 0, SEEK_SET);
// Read in all data
buffer = malloc(sizeof(char) * (fileSize + 1));
size_t readSize = fread(buffer, 1, fileSize, file);
fclose(file);
if(readSize < fileSize) {
free(buffer);
printf("Failed to read all data from CSV\n");
return 1;
}
buffer[fileSize] = '\0';
csvParse(buffer, &csv);
free(buffer);
printf("Parsed\n");
// Prepare output file for writing.
sprintf(path, "%s.language", out);
fileMkdirp(path);
file = fopen(path, "wb");
if(file == NULL) {
csvDispose(&csv);
printf("Failed to create output language file\n");
return 1;
}
// Iterate over the CSV
for(int32_t y = 0; y < csv.rowCount; y++) {
// Ensure valid line
if(csv.cellCounts[y] != 2) {
printf("Failed to parse language. Line %i has %i cells instead of 2\n", y, csv.cellCounts);
fclose(file);
csvDispose(&csv);
return 1;
}
char *key = csvGetCell(&csv, y, 0);
char *value = csvGetCell(&csv, y, 1);
if(strlen(key) <= 0 || strlen(value) <= 0) {
printf("Failed to parse language. Line %i has an invalid string\n", y);
fclose(file);
csvDispose(&csv);
return 1;
}
fwrite(key, sizeof(char), strlen(key), file);
fwrite(&sep, sizeof(char), 1, file);
fwrite(value, sizeof(char), strlen(value), file);
fwrite(&sep, sizeof(char), 1, file);
}
// Finished writing
fclose(file);
csvDispose(&csv);
return 0;
}

View File

@ -7,12 +7,16 @@
#include "csv.h"
char * csvGetCell(csv_t *csv, int32_t row, int32_t cell) {
return csv->rows[(row * CSV_ROW_COUNT_MAX) + cell];
}
void csvParse(char *string, csv_t *csv) {
char c;
size_t i, j, length;
csvparsestate_t state;
int32_t rowCellCount;
length = strlen(string);
csv->buffer = malloc(sizeof(char) * length * 2);
csv->cellCounts = malloc(sizeof(int32_t) * CSV_ROW_COUNT_MAX);

View File

@ -24,6 +24,8 @@ typedef struct {
int32_t *cellCounts;
} csv_t;
char * csvGetCell(csv_t *csv, int32_t row, int32_t cell);
void csvParse(char *string, csv_t *csv);
void csvDispose(csv_t *csv);