[WIP - Languages] Started external loader

This commit is contained in:
KiritoDev 2021-05-08 19:57:11 -05:00
parent f13b32f363
commit 6c39fc0fbc
3 changed files with 139 additions and 105 deletions

View File

@ -1,5 +1,5 @@
// Tencent is pleased to support the open source community by making RapidJSON available.
//
//
// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip.
//
// Licensed under the MIT License (the "License"); you may not use this file except
@ -7,9 +7,9 @@
//
// http://opensource.org/licenses/MIT
//
// Unless required by applicable law or agreed to in writing, software distributed
// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
// CONDITIONS OF ANY KIND, either express or implied. See the License for the
// Unless required by applicable law or agreed to in writing, software distributed
// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
// CONDITIONS OF ANY KIND, either express or implied. See the License for the
// specific language governing permissions and limitations under the License.
#ifndef RAPIDJSON_DOCUMENT_H_
@ -107,7 +107,7 @@ class GenericDocument;
But a compiler (IBM XL C/C++ for AIX) have reported to have problem with that so it moved as a namespace scope struct.
https://code.google.com/p/rapidjson/issues/detail?id=64
*/
template <typename Encoding, typename Allocator>
template <typename Encoding, typename Allocator>
class GenericMember {
public:
GenericValue<Encoding, Allocator> name; //!< name of member (must be a string)
@ -509,7 +509,7 @@ namespace internal {
template <typename ValueType, typename T>
struct TypeHelper {};
template<typename ValueType>
template<typename ValueType>
struct TypeHelper<ValueType, bool> {
static bool Is(const ValueType& v) { return v.IsBool(); }
static bool Get(const ValueType& v) { return v.GetBool(); }
@ -517,7 +517,7 @@ struct TypeHelper<ValueType, bool> {
static ValueType& Set(ValueType& v, bool data, typename ValueType::AllocatorType&) { return v.SetBool(data); }
};
template<typename ValueType>
template<typename ValueType>
struct TypeHelper<ValueType, int> {
static bool Is(const ValueType& v) { return v.IsInt(); }
static int Get(const ValueType& v) { return v.GetInt(); }
@ -525,7 +525,7 @@ struct TypeHelper<ValueType, int> {
static ValueType& Set(ValueType& v, int data, typename ValueType::AllocatorType&) { return v.SetInt(data); }
};
template<typename ValueType>
template<typename ValueType>
struct TypeHelper<ValueType, unsigned> {
static bool Is(const ValueType& v) { return v.IsUint(); }
static unsigned Get(const ValueType& v) { return v.GetUint(); }
@ -553,7 +553,7 @@ struct TypeHelper<ValueType, unsigned long> {
};
#endif
template<typename ValueType>
template<typename ValueType>
struct TypeHelper<ValueType, int64_t> {
static bool Is(const ValueType& v) { return v.IsInt64(); }
static int64_t Get(const ValueType& v) { return v.GetInt64(); }
@ -561,7 +561,7 @@ struct TypeHelper<ValueType, int64_t> {
static ValueType& Set(ValueType& v, int64_t data, typename ValueType::AllocatorType&) { return v.SetInt64(data); }
};
template<typename ValueType>
template<typename ValueType>
struct TypeHelper<ValueType, uint64_t> {
static bool Is(const ValueType& v) { return v.IsUint64(); }
static uint64_t Get(const ValueType& v) { return v.GetUint64(); }
@ -569,7 +569,7 @@ struct TypeHelper<ValueType, uint64_t> {
static ValueType& Set(ValueType& v, uint64_t data, typename ValueType::AllocatorType&) { return v.SetUint64(data); }
};
template<typename ValueType>
template<typename ValueType>
struct TypeHelper<ValueType, double> {
static bool Is(const ValueType& v) { return v.IsDouble(); }
static double Get(const ValueType& v) { return v.GetDouble(); }
@ -577,7 +577,7 @@ struct TypeHelper<ValueType, double> {
static ValueType& Set(ValueType& v, double data, typename ValueType::AllocatorType&) { return v.SetDouble(data); }
};
template<typename ValueType>
template<typename ValueType>
struct TypeHelper<ValueType, float> {
static bool Is(const ValueType& v) { return v.IsFloat(); }
static float Get(const ValueType& v) { return v.GetFloat(); }
@ -585,7 +585,7 @@ struct TypeHelper<ValueType, float> {
static ValueType& Set(ValueType& v, float data, typename ValueType::AllocatorType&) { return v.SetFloat(data); }
};
template<typename ValueType>
template<typename ValueType>
struct TypeHelper<ValueType, const typename ValueType::Ch*> {
typedef const typename ValueType::Ch* StringType;
static bool Is(const ValueType& v) { return v.IsString(); }
@ -595,7 +595,7 @@ struct TypeHelper<ValueType, const typename ValueType::Ch*> {
};
#if RAPIDJSON_HAS_STDSTRING
template<typename ValueType>
template<typename ValueType>
struct TypeHelper<ValueType, std::basic_string<typename ValueType::Ch> > {
typedef std::basic_string<typename ValueType::Ch> StringType;
static bool Is(const ValueType& v) { return v.IsString(); }
@ -604,7 +604,7 @@ struct TypeHelper<ValueType, std::basic_string<typename ValueType::Ch> > {
};
#endif
template<typename ValueType>
template<typename ValueType>
struct TypeHelper<ValueType, typename ValueType::Array> {
typedef typename ValueType::Array ArrayType;
static bool Is(const ValueType& v) { return v.IsArray(); }
@ -613,14 +613,14 @@ struct TypeHelper<ValueType, typename ValueType::Array> {
static ValueType& Set(ValueType& v, ArrayType data, typename ValueType::AllocatorType&) { return v = data; }
};
template<typename ValueType>
template<typename ValueType>
struct TypeHelper<ValueType, typename ValueType::ConstArray> {
typedef typename ValueType::ConstArray ArrayType;
static bool Is(const ValueType& v) { return v.IsArray(); }
static ArrayType Get(const ValueType& v) { return v.GetArray(); }
};
template<typename ValueType>
template<typename ValueType>
struct TypeHelper<ValueType, typename ValueType::Object> {
typedef typename ValueType::Object ObjectType;
static bool Is(const ValueType& v) { return v.IsObject(); }
@ -629,7 +629,7 @@ struct TypeHelper<ValueType, typename ValueType::Object> {
static ValueType& Set(ValueType& v, ObjectType data, typename ValueType::AllocatorType&) { return v = data; }
};
template<typename ValueType>
template<typename ValueType>
struct TypeHelper<ValueType, typename ValueType::ConstObject> {
typedef typename ValueType::ConstObject ObjectType;
static bool Is(const ValueType& v) { return v.IsObject(); }
@ -787,7 +787,7 @@ public:
//! Constructor for unsigned value.
explicit GenericValue(unsigned u) RAPIDJSON_NOEXCEPT : data_() {
data_.n.u64 = u;
data_.n.u64 = u;
data_.f.flags = (u & 0x80000000) ? kNumberUintFlag : (kNumberUintFlag | kIntFlag | kInt64Flag);
}
@ -1021,14 +1021,14 @@ public:
switch (GetType()) {
case kObjectType: // Warning: O(n^2) inner-loop
if (data_.o.size != rhs.data_.o.size)
return false;
return false;
for (ConstMemberIterator lhsMemberItr = MemberBegin(); lhsMemberItr != MemberEnd(); ++lhsMemberItr) {
typename RhsType::ConstMemberIterator rhsMemberItr = rhs.FindMember(lhsMemberItr->name);
if (rhsMemberItr == rhs.MemberEnd() || lhsMemberItr->value != rhsMemberItr->value)
return false;
}
return true;
case kArrayType:
if (data_.a.size != rhs.data_.a.size)
return false;
@ -1496,7 +1496,7 @@ public:
\note Linear time complexity.
*/
void RemoveAllMembers() {
RAPIDJSON_ASSERT(IsObject());
RAPIDJSON_ASSERT(IsObject());
DoClearMembers();
}
@ -1627,7 +1627,7 @@ public:
\note Linear time complexity.
*/
void Clear() {
RAPIDJSON_ASSERT(IsArray());
RAPIDJSON_ASSERT(IsArray());
GenericValue* e = GetElementsPointer();
for (GenericValue* v = e; v != e + data_.a.size; ++v)
v->~GenericValue();
@ -1833,7 +1833,7 @@ public:
//! Set this value as a string without copying source string.
/*! This version has better performance with supplied length, and also support string containing null character.
\param s source string pointer.
\param s source string pointer.
\param length The length of source string, excluding the trailing null terminator.
\return The value itself for fluent API.
\post IsString() == true && GetString() == s && GetStringLength() == length
@ -1850,7 +1850,7 @@ public:
//! Set this value as a string by copying from source string.
/*! This version has better performance with supplied length, and also support string containing null character.
\param s source string.
\param s source string.
\param length The length of source string, excluding the trailing null terminator.
\param allocator Allocator for allocating copied buffer. Commonly use GenericDocument::GetAllocator().
\return The value itself for fluent API.
@ -1859,7 +1859,7 @@ public:
GenericValue& SetString(const Ch* s, SizeType length, Allocator& allocator) { return SetString(StringRef(s, length), allocator); }
//! Set this value as a string by copying from source string.
/*! \param s source string.
/*! \param s source string.
\param allocator Allocator for allocating copied buffer. Commonly use GenericDocument::GetAllocator().
\return The value itself for fluent API.
\post IsString() == true && GetString() != s && strcmp(GetString(),s) == 0 && GetStringLength() == length
@ -1944,10 +1944,10 @@ public:
if (RAPIDJSON_UNLIKELY(!v->Accept(handler)))
return false;
return handler.EndArray(data_.a.size);
case kStringType:
return handler.String(GetString(), GetStringLength(), (data_.f.flags & kCopyFlag) != 0);
default:
RAPIDJSON_ASSERT(GetType() == kNumberType);
if (IsDouble()) return handler.Double(data_.n.d);
@ -2459,7 +2459,7 @@ private:
typedef GenericValue<UTF8<> > Value;
///////////////////////////////////////////////////////////////////////////////
// GenericDocument
// GenericDocument
//! A document for parsing JSON text as DOM.
/*!
@ -2491,12 +2491,12 @@ public:
}
//! Constructor
/*! Creates an empty document which type is Null.
/*! Creates an empty document which type is Null.
\param allocator Optional allocator for allocating memory.
\param stackCapacity Optional initial capacity of stack in bytes.
\param stackAllocator Optional allocator for allocating memory for stack.
*/
GenericDocument(Allocator* allocator = 0, size_t stackCapacity = kDefaultStackCapacity, StackAllocator* stackAllocator = 0) :
GenericDocument(Allocator* allocator = 0, size_t stackCapacity = kDefaultStackCapacity, StackAllocator* stackAllocator = 0) :
allocator_(allocator), ownAllocator_(0), stack_(stackAllocator, stackCapacity), parseResult_()
{
if (!allocator_)
@ -2713,7 +2713,7 @@ public:
GenericDocument& Parse(const Ch* str, size_t length) {
return Parse<parseFlags, Encoding>(str, length);
}
GenericDocument& Parse(const Ch* str, size_t length) {
return Parse<kParseDefaultFlags>(str, length);
}
@ -2733,7 +2733,7 @@ public:
GenericDocument& Parse(const std::basic_string<Ch>& str) {
return Parse<kParseDefaultFlags>(str);
}
#endif // RAPIDJSON_HAS_STDSTRING
#endif // RAPIDJSON_HAS_STDSTRING
//!@}
@ -2798,16 +2798,16 @@ public:
bool Uint64(uint64_t i) { new (stack_.template Push<ValueType>()) ValueType(i); return true; }
bool Double(double d) { new (stack_.template Push<ValueType>()) ValueType(d); return true; }
bool RawNumber(const Ch* str, SizeType length, bool copy) {
if (copy)
bool RawNumber(const Ch* str, SizeType length, bool copy) {
if (copy)
new (stack_.template Push<ValueType>()) ValueType(str, length, GetAllocator());
else
new (stack_.template Push<ValueType>()) ValueType(str, length);
return true;
}
bool String(const Ch* str, SizeType length, bool copy) {
if (copy)
bool String(const Ch* str, SizeType length, bool copy) {
if (copy)
new (stack_.template Push<ValueType>()) ValueType(str, length, GetAllocator());
else
new (stack_.template Push<ValueType>()) ValueType(str, length);
@ -2815,7 +2815,7 @@ public:
}
bool StartObject() { new (stack_.template Push<ValueType>()) ValueType(kObjectType); return true; }
bool Key(const Ch* str, SizeType length, bool copy) { return String(str, length, copy); }
bool EndObject(SizeType memberCount) {
@ -2825,7 +2825,7 @@ public:
}
bool StartArray() { new (stack_.template Push<ValueType>()) ValueType(kArrayType); return true; }
bool EndArray(SizeType elementCount) {
ValueType* elements = stack_.template Pop<ValueType>(elementCount);
stack_.template Top<ValueType>()->SetArrayRaw(elements, elementCount, GetAllocator());

View File

@ -6,6 +6,7 @@
#include <limits.h>
#include <stdlib.h>
#include <dirent.h>
#include <algorithm>
extern "C" {
#include "text/text-loader.h"
@ -28,7 +29,6 @@ LanguageEntry *current;
void Moon_LoadLanguage( string path ) {
LanguageEntry * language = new LanguageEntry();
FILE* fp = fopen(path.c_str(), "r");
char readBuffer[65536];
FileReadStream is(fp, readBuffer, sizeof(readBuffer));
@ -39,84 +39,118 @@ void Moon_LoadLanguage( string path ) {
WValue manifest, dialogs, courses, secrets, options, strings;
manifest = raw[L"manifest"];
dialogs = raw[L"dialogs"];
courses = raw[L"courses"];
secrets = raw[L"secrets"];
options = raw[L"options"];
strings = raw[L"strings"];
language->name = narrow(manifest[L"langName"].GetString());
language->logo = narrow(manifest[L"langLogo"].GetString());
for (WValue& dialog : dialogs.GetArray()) {
int linesPerBox = dialog[L"linesPerBox"].GetInt();
int leftOffset = dialog[L"leftOffset"].GetInt();
int width = dialog[L"width"].GetInt();
int id = dialog[L"ID"].GetInt();
DialogEntry *entry = new DialogEntry();
entry->linesPerBox = linesPerBox;
entry->leftOffset = leftOffset;
entry->width = width;
entry->unused = 1;
wstring base;
for (WValue& line : dialog[L"lines"].GetArray()){
base += line.GetString();
base += L"\n";
}
entry->str = getTranslatedText(narrow(base).c_str());
language->dialogs.push_back(entry);
if(!raw.HasMember(L"manifest")) {
cout << "Failed to load language" << endl;
cout << "Missing manifest" << endl;
return;
}
int course_name_table_size = courses.Size() + secrets.Size();
int courseId = 0;
int padding = 0;
manifest = raw[L"manifest"];
u8* tmpCourses[course_name_table_size];
bool isChild = manifest.HasMember(L"parent");
for (WValue& course : courses.GetArray()){
LanguageEntry * language;
if(courseId + 1 <= courses.Size() - 1) {
tmpCourses[courseId] = getTranslatedText(narrow(course[L"course"].GetString()).c_str());
if(!isChild){
language = new LanguageEntry();
} else {
isChild = false;
string parentName = narrow(manifest[L"langLogo"].GetString());
for(auto &lng : languages){
if(lng->name == parentName){
language = lng;
isChild = true;
break;
}
}
}
if(!isChild){
language->name = narrow(manifest[L"langName"].GetString());
language->logo = narrow(manifest[L"langLogo"].GetString());
}
if(raw.HasMember(L"dialogs")){
dialogs = raw[L"dialogs"];
for (WValue& dialog : dialogs.GetArray()) {
int linesPerBox = dialog[L"linesPerBox"].GetInt();
int leftOffset = dialog[L"leftOffset"].GetInt();
int width = dialog[L"width"].GetInt();
int id = dialog[L"ID"].GetInt();
DialogEntry *entry = new DialogEntry();
entry->linesPerBox = linesPerBox;
entry->leftOffset = leftOffset;
entry->width = width;
entry->unused = 1;
wstring base;
for (WValue& line : dialog[L"lines"].GetArray()){
base += line.GetString();
base += L"\n";
}
entry->str = getTranslatedText(narrow(base).c_str());
language->dialogs.push_back(entry);
}
}
if(raw.HasMember(L"courses") && raw.HasMember(L"secrets")){
courses = raw[L"courses"];
secrets = raw[L"secrets"];
int course_name_table_size = courses.Size() + secrets.Size();
int courseId = 0;
int padding = 0;
u8* tmpCourses[course_name_table_size];
for (WValue& course : courses.GetArray()){
if(courseId + 1 <= courses.Size() - 1) {
tmpCourses[courseId] = getTranslatedText(narrow(course[L"course"].GetString()).c_str());
courseId++;
}
for (WValue& act : course[L"acts"].GetArray()){
language->acts.push_back(getTranslatedText(narrow(act.GetString()).c_str()));
}
}
for (WValue& secret : secrets.GetArray()){
if(courseId == SECRET_NULL) {
tmpCourses[courseId] = getTranslatedText(0);
padding++;
}
tmpCourses[courseId + padding] = getTranslatedText(narrow(secret.GetString()).c_str());
courseId++;
}
for (WValue& act : course[L"acts"].GetArray()){
language->acts.push_back(getTranslatedText(narrow(act.GetString()).c_str()));
language->courses.insert(language->courses.end(), &tmpCourses[0], &tmpCourses[course_name_table_size]);
}
if(raw.HasMember(L"options")){
options = raw[L"options"];
for (WValue::ConstMemberIterator option = options.MemberBegin(); option != options.MemberEnd(); ++option) {
language->strings.insert(pair<string, string>(
narrow(option->name.GetString()),
narrow(option->value.GetString())
));
}
}
for (WValue& secret : secrets.GetArray()){
if(courseId == SECRET_NULL) {
tmpCourses[courseId] = getTranslatedText(0);
padding++;
if(raw.HasMember(L"secrets")){
strings = raw[L"strings"];
for (WValue::ConstMemberIterator item = strings.MemberBegin(); item != strings.MemberEnd(); ++item) {
language->strings.insert(pair<string, string>(
narrow(item->name.GetString()),
narrow(item->value.GetString())
));
}
tmpCourses[courseId + padding] = getTranslatedText(narrow(secret.GetString()).c_str());
courseId++;
}
language->courses.insert(language->courses.end(), &tmpCourses[0], &tmpCourses[course_name_table_size]);
for (WValue::ConstMemberIterator option = options.MemberBegin(); option != options.MemberEnd(); ++option) {
language->strings.insert(pair<string, string>(
narrow(option->name.GetString()),
narrow(option->value.GetString())
));
}
for (WValue::ConstMemberIterator item = strings.MemberBegin(); item != strings.MemberEnd(); ++item) {
language->strings.insert(pair<string, string>(
narrow(item->name.GetString()),
narrow(item->value.GetString())
));
}
languages.push_back(language);
if(!isChild) languages.push_back(language);
languagesAmount = languages.size();
}

View File

@ -27,7 +27,7 @@ inline std::string narrow (const std::wstring& str) {
struct LanguageEntry {
std::string name;
std::string logo;
std::string logo;
std::map<std::string, std::string> strings;
std::vector<u8*> acts;
std::vector<struct DialogEntry*> dialogs;