1
0
mirror of https://github.com/OpenRCT2/OpenRCT2 synced 2025-12-24 00:03:11 +01:00

Write common JSON string loader

This commit is contained in:
Ted John
2017-12-11 12:30:08 +00:00
committed by Gymnasiast
parent d56f1445f8
commit 900bc48efa
10 changed files with 68 additions and 43 deletions

View File

@@ -83,6 +83,20 @@ void utf8_remove_format_codes(utf8 * text, bool allowcolours)
*dstCh = 0;
}
uint8 language_get_id_from_locale(const char * locale)
{
uint8 i = 0;
for (const auto &langDesc : LanguagesDescriptors)
{
if (String::Equals(locale, langDesc.locale))
{
return i;
}
i++;
}
return LANGUAGE_UNDEFINED;
}
const char * language_get_string(rct_string_id id)
{
const char * result = nullptr;

View File

@@ -94,6 +94,7 @@ extern const utf8 BlackLeftArrowString[];
extern const utf8 BlackRightArrowString[];
extern const utf8 CheckBoxMarkString[];
uint8 language_get_id_from_locale(const char * locale);
const char *language_get_string(rct_string_id id);
bool language_open(sint32 id);
void language_close_all();

View File

@@ -68,16 +68,10 @@ void EntranceObject::DrawPreview(rct_drawpixelinfo * dpi, sint32 width, sint32 h
void EntranceObject::ReadJson(IReadObjectContext * context, const json_t * root)
{
// Strings
auto stringTable = GetStringTable();
auto jsonStrings = json_object_get(root, "strings");
auto jsonName = json_object_get(jsonStrings, "name");
stringTable->SetString(0, 0, json_string_value(json_object_get(jsonName, "en-GB")));
auto properties = json_object_get(root, "properties");
_legacyType.scrolling_mode = json_integer_value(json_object_get(properties, "scrollingMode"));
_legacyType.text_height = json_integer_value(json_object_get(properties, "textHeight"));
auto imageTable = GetImageTable();
ObjectJsonHelpers::LoadImages(root, *imageTable);
ObjectJsonHelpers::LoadStrings(root, *GetStringTable());
ObjectJsonHelpers::LoadImages(root, *GetImageTable());
}

View File

@@ -137,12 +137,6 @@ static uint8 ParseCursor(const std::string &s)
void FootpathItemObject::ReadJson(IReadObjectContext * context, const json_t * root)
{
// Strings
auto stringTable = GetStringTable();
auto jsonStrings = json_object_get(root, "strings");
auto jsonName = json_object_get(jsonStrings, "name");
stringTable->SetString(0, 0, json_string_value(json_object_get(jsonName, "en-GB")));
auto properties = json_object_get(root, "properties");
_legacyType.path_bit.draw_type = ParseDrawType(ObjectJsonHelpers::GetString(json_object_get(properties, "renderAs")));
_legacyType.path_bit.tool_id = ParseCursor(ObjectJsonHelpers::GetString(json_object_get(properties, "cursor")));
@@ -185,6 +179,6 @@ void FootpathItemObject::ReadJson(IReadObjectContext * context, const json_t * r
}
_legacyType.path_bit.flags = flags;
auto imageTable = GetImageTable();
ObjectJsonHelpers::LoadImages(root, *imageTable);
ObjectJsonHelpers::LoadStrings(root, *GetStringTable());
ObjectJsonHelpers::LoadImages(root, *GetImageTable());
}

View File

@@ -72,12 +72,6 @@ static uint8 ParseSupportType(const std::string &s)
void FootpathObject::ReadJson(IReadObjectContext * context, const json_t * root)
{
// Strings
auto stringTable = GetStringTable();
auto jsonStrings = json_object_get(root, "strings");
auto jsonName = json_object_get(jsonStrings, "name");
stringTable->SetString(0, 0, json_string_value(json_object_get(jsonName, "en-GB")));
auto properties = json_object_get(root, "properties");
_legacyType.support_type = ParseSupportType(ObjectJsonHelpers::GetString(json_object_get(properties, "supportType")));
_legacyType.scrolling_mode = json_integer_value(json_object_get(properties, "scrollingMode"));
@@ -97,6 +91,6 @@ void FootpathObject::ReadJson(IReadObjectContext * context, const json_t * root)
_legacyType.flags |= FOOTPATH_ENTRY_FLAG_SHOW_ONLY_IN_SCENARIO_EDITOR;
}
auto imageTable = GetImageTable();
ObjectJsonHelpers::LoadImages(root, *imageTable);
ObjectJsonHelpers::LoadStrings(root, *GetStringTable());
ObjectJsonHelpers::LoadImages(root, *GetImageTable());
}

View File

@@ -111,9 +111,10 @@ struct rct_object_filters {
assert_struct_size(rct_object_filters, 3);
#pragma pack(pop)
enum OBJ_STRING_ID
enum OBJ_STRING_ID : uint8
{
OBJ_STRING_ID_NAME,
OBJ_STRING_ID_UNKNOWN = 255,
OBJ_STRING_ID_NAME = 0,
OBJ_STRING_ID_DESCRIPTION,
OBJ_STRING_ID_SCENARIO_NAME = 0,
OBJ_STRING_ID_PARK_NAME = 1,

View File

@@ -14,16 +14,19 @@
*****************************************************************************/
#pragma endregion
#pragma warning(disable : 4706) // assignment within conditional expression
#include "../Context.h"
#include "../core/Math.hpp"
#include "../core/Memory.hpp"
#include "../core/Path.hpp"
#include "../core/String.hpp"
#include "../localisation/language.h"
#include "../PlatformEnvironment.h"
#include "../sprites.h"
#include "Object.h"
#include "ObjectFactory.h"
#include "ObjectJsonHelpers.h"
#include "../core/Math.hpp"
using namespace OpenRCT2;
@@ -168,6 +171,40 @@ namespace ObjectJsonHelpers
return result;
}
static uint8 ParseStringId(const std::string &s)
{
if (s == "name") return OBJ_STRING_ID_NAME;
if (s == "description") return OBJ_STRING_ID_DESCRIPTION;
if (s == "capacity") return OBJ_STRING_ID_CAPACITY;
if (s == "vehicleName") return OBJ_STRING_ID_VEHICLE_NAME;
return OBJ_STRING_ID_UNKNOWN;
}
void LoadStrings(const json_t * root, StringTable &stringTable)
{
auto jsonStrings = json_object_get(root, "strings");
const char * key;
json_t * jlanguages;
json_object_foreach(jsonStrings, key, jlanguages)
{
auto stringId = ParseStringId(key);
if (stringId != OBJ_STRING_ID_UNKNOWN)
{
const char * locale;
json_t * jstring;
json_object_foreach(jlanguages, locale, jstring)
{
auto langId = language_get_id_from_locale(locale);
if (langId != LANGUAGE_UNDEFINED)
{
auto string = json_string_value(jstring);
stringTable.SetString(stringId, langId, string);
}
}
}
}
}
void LoadImages(const json_t * root, ImageTable &imageTable)
{
auto jsonImages = json_object_get(root, "images");

View File

@@ -22,11 +22,13 @@
#include "../core/Json.hpp"
#include "../drawing/drawing.h"
#include "ImageTable.h"
#include "StringTable.h"
namespace ObjectJsonHelpers
{
bool GetBoolean(const json_t * obj, const std::string &name, bool defaultValue = false);
std::string GetString(const json_t * value);
std::vector<std::string> GetJsonStringArray(const json_t * arr);
void LoadStrings(const json_t * root, StringTable &stringTable);
void LoadImages(const json_t * root, ImageTable &imageTable);
};

View File

@@ -726,22 +726,10 @@ void RideObject::ReadJson(IReadObjectContext * context, const json_t * root)
_legacyType.shop_item_secondary = ParseShopItem(rideSells[1]);
}
_legacyType.flags |= RIDE_ENTRY_FLAG_SEPARATE_RIDE;
auto stringTable = GetStringTable();
auto jsonStrings = json_object_get(root, "strings");
auto jsonName = json_object_get(jsonStrings, "name");
auto jsonDescription = json_object_get(jsonStrings, "description");
stringTable->SetString(0, 0, json_string_value(json_object_get(jsonName, "en-GB")));
stringTable->SetString(1, 0, json_string_value(json_object_get(jsonDescription, "en-GB")));
stringTable->SetString(2, 0, "Capacity");
stringTable->SetString(3, 0, "Vehicle");
auto imageTable = GetImageTable();
ObjectJsonHelpers::LoadImages(root, *imageTable);
rct_ride_entry_vehicle * vehicle0 = &_legacyType.vehicles[0];
vehicle0->sprite_flags |= VEHICLE_SPRITE_FLAG_FLAT;
vehicle0->base_image_id = 0;
ObjectJsonHelpers::LoadStrings(root, *GetStringTable());
ObjectJsonHelpers::LoadImages(root, *GetImageTable());
}

View File

@@ -97,7 +97,7 @@ std::string StringTable::GetString(uint8 id) const
return string.Text;
}
}
return nullptr;
return std::string();
}
void StringTable::SetString(uint8 id, uint8 language, const std::string &text)