Skip to content

Commit dfe22d4

Browse files
feat: 1.2.4-dev.1 | fix up some localization shit and fix tests not creating folders
1 parent d0f146e commit dfe22d4

File tree

8 files changed

+88
-69
lines changed

8 files changed

+88
-69
lines changed

CMakeLists.txt

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
cmake_minimum_required(VERSION 3.26)
22
project(LCE VERSION 1.2.4)
33

4-
set(VERSION_SUFFIX "-dev.0") # used for non-stable versions
4+
set(VERSION_SUFFIX "-dev.1") # used for non-stable versions
55
set(CMAKE_CXX_STANDARD 17)
66

77
add_definitions(-DCOMPILER_NAME="${CMAKE_CXX_COMPILER_ID}")
@@ -38,34 +38,47 @@ set(ZLIB_FILES
3838
)
3939

4040
set(HEADERS
41+
# Windows
4142
include/Resources/resource.h
4243
include/Resources/resource.rc
4344

45+
# libLCE
4446
include/libLCE.h
4547
include/libLCEExports.h
46-
include/World/Region.h
47-
include/World/Chunk.h
48+
# Save
4849
include/Save/SaveFile.h
4950
include/Save/SaveFileOld.h
5051
include/Save/SaveFileCommons.h
52+
include/Save/Thumb.h
53+
# IO
54+
include/IO/Serializable.h
5155
include/IO/BinaryIO.h
52-
include/Filesystem/Filesystem.h
53-
include/Filesystem/File.h
56+
# ARC
5457
include/Archive/Archive.h
58+
# COL
5559
include/Color/Color.h
5660
include/Color/ColorFile.h
61+
# LOC
5762
include/Localization/LocalizationFile.h
5863
include/Localization/Language.h
64+
# MSSCMP
5965
include/Soundbank/Soundbank.h
6066
include/Soundbank/BinkaFile.h
61-
include/Compression/Compression.h
62-
include/Save/Thumb.h
67+
# World
6368
include/World/SplitSave.h
6469
include/World/Block.h
70+
include/World/Region.h
71+
include/World/Chunk.h
72+
# Other
73+
include/Compression/Compression.h
74+
# Filesystem
6575
include/Filesystem/FSObject.h
6676
include/Filesystem/Directory.h
77+
include/Filesystem/Filesystem.h
78+
include/Filesystem/File.h
79+
# Bindings
6780
include/Bindings/EmscriptenBindings.h
68-
include/IO/Serializable.h
81+
# Unsorted
6982
)
7083

7184
set(FILES
@@ -111,15 +124,15 @@ else ()
111124
add_compile_options(
112125
"$<$<COMPILE_LANGUAGE:CXX>:-O3;-ffast-math;-march=native;-fpic;-ftree-vectorize>"
113126
)
114-
115-
if (CMAKE_BUILD_TYPE STREQUAL "Debug")
116-
add_compile_options(
117-
"-D_GLIBCXX_DEBUG"
118-
)
119-
endif ()
120127
endif ()
121128
unset(ENABLE_OPTIMIZATIONS CACHE)
122129

130+
if (CMAKE_BUILD_TYPE STREQUAL "Debug")
131+
add_compile_options(
132+
"-D_GLIBCXX_DEBUG"
133+
)
134+
endif ()
135+
123136
set(ENVIRONMENT_TYPE "Native")
124137
set(LIB_TYPE SHARED)
125138
add_library(LCE ${LIB_TYPE} ${HEADERS} ${FILES} ${LODEPNG_FILES} ${ZLIB_FILES})

include/Localization/Language.h

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
#define LANGUAGE_H
77

88
#include "IO/Serializable.h"
9-
#include "LocStringId.h"
109

1110
#include <IO/BinaryIO.h>
1211
#include <libLCE.h>
@@ -64,13 +63,22 @@ namespace lce::loc {
6463
};
6564

6665
explicit Language(io::BinaryIO &io, std::vector<uint32_t> &keys);
66+
67+
Language(const uint8_t _byte, const uint32_t _shouldReadByte,
68+
std::string _code, std::vector<uint32_t> &keys)
69+
: mUnk(_byte), mShouldReadByte(_shouldReadByte), mKeys(&keys),
70+
mName(std::move(_code)) {
71+
for (auto &key : keys) {
72+
mStrings.emplace(key, "");
73+
}
74+
}
75+
6776
Language(const uint8_t _byte, const uint32_t _shouldReadByte,
6877
std::string _code)
6978
: mUnk(_byte), mShouldReadByte(_shouldReadByte),
7079
mKeys(new std::vector<uint32_t>()), mName(std::move(_code)) {}
7180

72-
std::unordered_map<LocStringId, std::string, LocIdHash, LocIdKeyEqual> &
73-
getStrings();
81+
std::unordered_map<uint32_t, std::string> &getStrings();
7482

7583
const std::string &getName() const;
7684

@@ -126,8 +134,7 @@ namespace lce::loc {
126134
// this is changed...
127135

128136
std::string mName;
129-
std::unordered_map<LocStringId, std::string, LocIdHash, LocIdKeyEqual>
130-
mStrings;
137+
std::unordered_map<uint32_t, std::string> mStrings;
131138
};
132139
} // namespace lce::loc
133140

include/Localization/LocStringId.h

Lines changed: 0 additions & 26 deletions
This file was deleted.

include/Localization/LocalizationFile.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,15 @@ namespace lce::loc {
3333
size_t getSize() const override;
3434
uint8_t *serialize() const override;
3535

36+
Language *createLanguage(const std::string &name,
37+
const uint8_t _byte = 2,
38+
const uint32_t _shouldReadByte = 1);
39+
3640
Language *getLanguage(const std::string &name);
3741

38-
uint32_t &createString(uint32_t id);
42+
bool languageExists(const std::string &name);
43+
44+
uint32_t createString(uint32_t id);
3945

4046
/** Nicer looking way to add data-driven strings
4147
*

src/Localization/Language.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ namespace lce::loc {
2929
}
3030

3131
const uint32_t ss = io.readBE<uint16_t>();
32-
mStrings.emplace(std::cref(keys[s]), io.readString(ss));
32+
mStrings.emplace(keys[s], io.readString(ss));
3333
}
3434
}
3535

@@ -76,14 +76,13 @@ namespace lce::loc {
7676
return addString(str, std::hash<std::string>{}(str));
7777
}
7878

79-
std::unordered_map<LocStringId, std::string, LocIdHash, LocIdKeyEqual> &
80-
Language::getStrings() {
79+
std::unordered_map<uint32_t, std::string> &Language::getStrings() {
8180
return this->mStrings;
8281
}
8382

8483
const std::string &Language::getName() const { return this->mName; }
8584

86-
void Language::setString(uint32_t id, const std::string &str) {
85+
void Language::setString(const uint32_t id, const std::string &str) {
8786
if (!mStrings.count(id))
8887
throw std::runtime_error("String has does not exist in map");
8988

@@ -98,7 +97,7 @@ namespace lce::loc {
9897
return h;
9998
}
10099

101-
std::string &Language::getString(uint32_t id) { return mStrings[id]; }
100+
std::string &Language::getString(const uint32_t id) { return mStrings[id]; }
102101

103102
std::string &Language::getString(const std::string &id) {
104103
return getString(std::hash<std::string>{}(id));

src/Localization/LocalizationFile.cpp

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,19 @@ namespace lce::loc {
9999

100100
return io.getData();
101101
}
102+
Language *LocalizationFile::createLanguage(const std::string &name,
103+
const uint8_t _byte,
104+
const uint32_t _shouldReadByte) {
105+
if (languageExists(name))
106+
throw std::runtime_error("Language already exists with name: " +
107+
name);
108+
109+
Language::Id id = Language::Id(name);
110+
Language language = Language(_byte, _shouldReadByte, name, this->mKeys);
111+
this->mLanguages.emplace(id, language);
112+
113+
return getLanguage(name);
114+
}
102115

103116
Language *LocalizationFile::getLanguage(const std::string &name) {
104117
for (auto &[id, lang] : mLanguages) {
@@ -108,19 +121,23 @@ namespace lce::loc {
108121

109122
return nullptr;
110123
}
124+
bool LocalizationFile::languageExists(const std::string &name) {
125+
for (auto &[id, lang] : mLanguages) {
126+
if (id.getName() == name)
127+
return true;
128+
}
129+
130+
return false;
131+
}
111132

112-
uint32_t &LocalizationFile::createString(const uint32_t id) {
133+
uint32_t LocalizationFile::createString(const uint32_t id) {
113134
mKeys.reserve(mKeys.size() + 1);
114135
mKeys.push_back(id);
115136

116-
// TODO: fix, we have access to mKeys in the Language anyways...
117-
// we keep taking references to the ID everywhere lol
118-
uint32_t &key = this->mKeys.back();
119-
120137
for (auto &[i, lang] : mLanguages)
121-
lang.addString("", key);
138+
lang.addString("", id);
122139

123-
return key;
140+
return id;
124141
}
125142

126143
uint32_t LocalizationFile::setString(const std::string &language,
@@ -139,15 +156,10 @@ namespace lce::loc {
139156
if (!lang)
140157
throw std::runtime_error("Language not found: " + language);
141158

142-
uint32_t *h;
143-
144-
auto n = std::find(mKeys.begin(), mKeys.end(), id);
145-
if (n == mKeys.end())
146-
h = &createString(id);
147-
else
148-
h = &(*n);
159+
const auto n = std::find(mKeys.begin(), mKeys.end(), id);
160+
const uint32_t h = (n == mKeys.end()) ? createString(id) : *n;
149161

150-
lang->setString(*h, str);
162+
lang->setString(h, str);
151163
}
152164

153165
std::pair<const Language::Id, Language> *

tests/tests/formats.cpp

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,27 +28,33 @@ namespace lce::tests::formats {
2828

2929
void locTest() {
3030
std::string n = "example.loc";
31-
std::filesystem::create_directories(util::output / (n + ".dir"));
3231

3332
OPEN_FILE(n, f);
3433

3534
loc::LocalizationFile file = loc::LocalizationFile(f.data());
3635

3736
#if WRITE_LOC
37+
std::filesystem::create_directories(util::output / (n + ".dir"));
3838
for (auto &[i, l] : file.getLanguages()) {
3939
std::ofstream o(util::output / (n + ".dir") /
4040
(i.getName() + ".txt"));
4141

4242
for (const auto &[id, str] : l.getStrings()) {
43-
o << "[0x" << std::hex << id.get() << "] " << str << std::endl;
43+
o << "[0x" << std::hex << id << "] " << str << std::endl;
4444
}
4545

4646
o.close();
4747
}
48-
4948
#endif
5049

5150
file.setString("en-EN", "test.hello", "Hello!");
51+
file.setString("fi-FI", "test.hello", "Hei!");
52+
53+
if (!file.languageExists("TestLang")) {
54+
file.createLanguage("TestLang");
55+
}
56+
57+
file.setString("TestLang", "test.hello", "Hello, world!");
5258

5359
std::cout << file.getString("en-EN", "test.hello") << std::endl;
5460

@@ -99,6 +105,7 @@ namespace lce::tests::formats {
99105
}
100106

101107
void saveFromFolderTest() {
108+
std::filesystem::create_directories(util::examples / "FSSaveFolder");
102109
fs::Directory *d = new fs::Directory(util::examples / "FSSaveFolder");
103110
fs::Filesystem *fs = new fs::Filesystem(d);
104111

tests/tests/vfs.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ namespace lce::tests::vfs {
4848
});
4949
}
5050
void runForEach() {
51+
std::filesystem::create_directories(util::examples / "VFSTestFolder");
5152
const fs::Directory *d =
5253
new fs::Directory(util::examples / "VFSTestFolder");
5354

0 commit comments

Comments
 (0)