1
0
mirror of https://github.com/OpenRCT2/OpenRCT2 synced 2026-01-23 23:04:36 +01:00

clang-format object

This commit is contained in:
clang-format
2018-06-22 23:03:20 +02:00
committed by Hielke Morsink
parent e32189fd98
commit a6a12af1ec
42 changed files with 1452 additions and 1283 deletions

View File

@@ -9,10 +9,10 @@
#pragma warning(disable : 4706) // assignment within conditional expression
#include <cstdlib>
#include <cstring>
#include <unordered_map>
#include "ObjectJsonHelpers.h"
#include "../Context.h"
#include "../PlatformEnvironment.h"
#include "../core/File.h"
#include "../core/FileScanner.h"
#include "../core/Math.hpp"
@@ -22,48 +22,43 @@
#include "../drawing/ImageImporter.h"
#include "../interface/Cursors.h"
#include "../localisation/Language.h"
#include "../PlatformEnvironment.h"
#include "../sprites.h"
#include "Object.h"
#include "ObjectFactory.h"
#include "ObjectJsonHelpers.h"
#include <cstdlib>
#include <cstring>
#include <unordered_map>
using namespace OpenRCT2;
using namespace OpenRCT2::Drawing;
namespace ObjectJsonHelpers
{
bool GetBoolean(const json_t * obj, const std::string &name, bool defaultValue)
bool GetBoolean(const json_t* obj, const std::string& name, bool defaultValue)
{
auto value = json_object_get(obj, name.c_str());
return json_is_boolean(value) ?
json_boolean_value(value) :
defaultValue;
return json_is_boolean(value) ? json_boolean_value(value) : defaultValue;
}
std::string GetString(const json_t * value)
std::string GetString(const json_t* value)
{
return json_is_string(value) ?
std::string(json_string_value(value)) :
std::string();
return json_is_string(value) ? std::string(json_string_value(value)) : std::string();
}
std::string GetString(const json_t * obj, const std::string &name, const std::string &defaultValue)
std::string GetString(const json_t* obj, const std::string& name, const std::string& defaultValue)
{
auto value = json_object_get(obj, name.c_str());
return json_is_string(value) ?
json_string_value(value) :
defaultValue;
return json_is_string(value) ? json_string_value(value) : defaultValue;
}
int32_t GetInteger(const json_t * obj, const std::string &name, const int32_t &defaultValue)
int32_t GetInteger(const json_t* obj, const std::string& name, const int32_t& defaultValue)
{
auto value = json_object_get(obj, name.c_str());
if (json_is_integer(value))
{
int64_t val = json_integer_value(value);
if (val >= std::numeric_limits<int32_t>::min() &&
val <= std::numeric_limits<int32_t>::max())
if (val >= std::numeric_limits<int32_t>::min() && val <= std::numeric_limits<int32_t>::max())
{
return static_cast<int32_t>(val);
}
@@ -71,15 +66,13 @@ namespace ObjectJsonHelpers
return defaultValue;
}
float GetFloat(const json_t * obj, const std::string &name, const float &defaultValue)
float GetFloat(const json_t* obj, const std::string& name, const float& defaultValue)
{
auto value = json_object_get(obj, name.c_str());
return json_is_number(value) ?
json_number_value(value) :
defaultValue;
return json_is_number(value) ? json_number_value(value) : defaultValue;
}
std::vector<std::string> GetJsonStringArray(const json_t * arr)
std::vector<std::string> GetJsonStringArray(const json_t* arr)
{
std::vector<std::string> result;
if (json_is_array(arr))
@@ -99,7 +92,7 @@ namespace ObjectJsonHelpers
return result;
}
std::vector<int32_t> GetJsonIntegerArray(const json_t * arr)
std::vector<int32_t> GetJsonIntegerArray(const json_t* arr)
{
std::vector<int32_t> result;
if (json_is_array(arr))
@@ -119,46 +112,43 @@ namespace ObjectJsonHelpers
return result;
}
uint8_t ParseCursor(const std::string &s, uint8_t defaultValue)
uint8_t ParseCursor(const std::string& s, uint8_t defaultValue)
{
static const std::unordered_map<std::string, uint8_t> LookupTable
{
{ "CURSOR_BLANK", CURSOR_BLANK },
{ "CURSOR_UP_ARROW", CURSOR_UP_ARROW },
{ "CURSOR_UP_DOWN_ARROW", CURSOR_UP_DOWN_ARROW },
{ "CURSOR_HAND_POINT", CURSOR_HAND_POINT },
{ "CURSOR_ZZZ", CURSOR_ZZZ },
static const std::unordered_map<std::string, uint8_t> LookupTable{
{ "CURSOR_BLANK", CURSOR_BLANK },
{ "CURSOR_UP_ARROW", CURSOR_UP_ARROW },
{ "CURSOR_UP_DOWN_ARROW", CURSOR_UP_DOWN_ARROW },
{ "CURSOR_HAND_POINT", CURSOR_HAND_POINT },
{ "CURSOR_ZZZ", CURSOR_ZZZ },
{ "CURSOR_DIAGONAL_ARROWS", CURSOR_DIAGONAL_ARROWS },
{ "CURSOR_PICKER", CURSOR_PICKER },
{ "CURSOR_TREE_DOWN", CURSOR_TREE_DOWN },
{ "CURSOR_FOUNTAIN_DOWN", CURSOR_FOUNTAIN_DOWN },
{ "CURSOR_STATUE_DOWN", CURSOR_STATUE_DOWN },
{ "CURSOR_BENCH_DOWN", CURSOR_BENCH_DOWN },
{ "CURSOR_CROSS_HAIR", CURSOR_CROSS_HAIR },
{ "CURSOR_BIN_DOWN", CURSOR_BIN_DOWN },
{ "CURSOR_LAMPPOST_DOWN", CURSOR_LAMPPOST_DOWN },
{ "CURSOR_FENCE_DOWN", CURSOR_FENCE_DOWN },
{ "CURSOR_FLOWER_DOWN", CURSOR_FLOWER_DOWN },
{ "CURSOR_PATH_DOWN", CURSOR_PATH_DOWN },
{ "CURSOR_DIG_DOWN", CURSOR_DIG_DOWN },
{ "CURSOR_WATER_DOWN", CURSOR_WATER_DOWN },
{ "CURSOR_HOUSE_DOWN", CURSOR_HOUSE_DOWN },
{ "CURSOR_VOLCANO_DOWN", CURSOR_VOLCANO_DOWN },
{ "CURSOR_WALK_DOWN", CURSOR_WALK_DOWN },
{ "CURSOR_PAINT_DOWN", CURSOR_PAINT_DOWN },
{ "CURSOR_ENTRANCE_DOWN", CURSOR_ENTRANCE_DOWN },
{ "CURSOR_HAND_OPEN", CURSOR_HAND_OPEN },
{ "CURSOR_HAND_CLOSED", CURSOR_HAND_CLOSED },
{ "CURSOR_ARROW", CURSOR_ARROW },
{ "CURSOR_PICKER", CURSOR_PICKER },
{ "CURSOR_TREE_DOWN", CURSOR_TREE_DOWN },
{ "CURSOR_FOUNTAIN_DOWN", CURSOR_FOUNTAIN_DOWN },
{ "CURSOR_STATUE_DOWN", CURSOR_STATUE_DOWN },
{ "CURSOR_BENCH_DOWN", CURSOR_BENCH_DOWN },
{ "CURSOR_CROSS_HAIR", CURSOR_CROSS_HAIR },
{ "CURSOR_BIN_DOWN", CURSOR_BIN_DOWN },
{ "CURSOR_LAMPPOST_DOWN", CURSOR_LAMPPOST_DOWN },
{ "CURSOR_FENCE_DOWN", CURSOR_FENCE_DOWN },
{ "CURSOR_FLOWER_DOWN", CURSOR_FLOWER_DOWN },
{ "CURSOR_PATH_DOWN", CURSOR_PATH_DOWN },
{ "CURSOR_DIG_DOWN", CURSOR_DIG_DOWN },
{ "CURSOR_WATER_DOWN", CURSOR_WATER_DOWN },
{ "CURSOR_HOUSE_DOWN", CURSOR_HOUSE_DOWN },
{ "CURSOR_VOLCANO_DOWN", CURSOR_VOLCANO_DOWN },
{ "CURSOR_WALK_DOWN", CURSOR_WALK_DOWN },
{ "CURSOR_PAINT_DOWN", CURSOR_PAINT_DOWN },
{ "CURSOR_ENTRANCE_DOWN", CURSOR_ENTRANCE_DOWN },
{ "CURSOR_HAND_OPEN", CURSOR_HAND_OPEN },
{ "CURSOR_HAND_CLOSED", CURSOR_HAND_CLOSED },
{ "CURSOR_ARROW", CURSOR_ARROW },
};
auto result = LookupTable.find(s);
return (result != LookupTable.end()) ?
result->second :
defaultValue;
return (result != LookupTable.end()) ? result->second : defaultValue;
}
rct_object_entry ParseObjectEntry(const std::string & s)
rct_object_entry ParseObjectEntry(const std::string& s)
{
rct_object_entry entry = {};
std::fill_n(entry.name, sizeof(entry.name), ' ');
@@ -170,7 +160,7 @@ namespace ObjectJsonHelpers
static std::vector<int32_t> ParseRange(std::string s)
{
// Currently only supports [###] or [###..###]
std::vector<int32_t> result = { };
std::vector<int32_t> result = {};
if (s.length() >= 3 && s[0] == '[' && s[s.length() - 1] == ']')
{
s = s.substr(1, s.length() - 2);
@@ -202,7 +192,7 @@ namespace ObjectJsonHelpers
return result;
}
static std::string FindLegacyObject(const std::string &name)
static std::string FindLegacyObject(const std::string& name)
{
const auto env = GetContext()->GetPlatformEnvironment();
auto objectsPath = env->GetDirectoryPath(DIRBASE::RCT2, DIRID::OBJECT);
@@ -225,14 +215,15 @@ namespace ObjectJsonHelpers
return objectPath;
}
static std::vector<rct_g1_element> LoadObjectImages(IReadObjectContext * context, const std::string &name, const std::vector<int32_t> &range)
static std::vector<rct_g1_element>
LoadObjectImages(IReadObjectContext* context, const std::string& name, const std::vector<int32_t>& range)
{
std::vector<rct_g1_element> result;
auto objectPath = FindLegacyObject(name);
auto obj = ObjectFactory::CreateObjectFromLegacyFile(context->GetObjectRepository(), objectPath.c_str());
if (obj != nullptr)
{
auto &imgTable = static_cast<const Object *>(obj)->GetImageTable();
auto& imgTable = static_cast<const Object*>(obj)->GetImageTable();
auto numImages = (int32_t)imgTable.GetCount();
auto images = imgTable.GetImages();
size_t placeHoldersAdded = 0;
@@ -240,10 +231,10 @@ namespace ObjectJsonHelpers
{
if (i >= 0 && i < numImages)
{
auto &objg1 = images[i];
auto& objg1 = images[i];
auto length = g1_calculate_data_size(&objg1);
auto g1 = objg1;
g1.offset = (uint8_t *)std::malloc(length);
g1.offset = (uint8_t*)std::malloc(length);
std::memcpy(g1.offset, objg1.offset, length);
result.push_back(g1);
}
@@ -272,7 +263,7 @@ namespace ObjectJsonHelpers
return result;
}
static std::vector<rct_g1_element> ParseImages(IReadObjectContext * context, std::string s)
static std::vector<rct_g1_element> ParseImages(IReadObjectContext* context, std::string s)
{
std::vector<rct_g1_element> result;
if (s.empty())
@@ -289,10 +280,10 @@ namespace ObjectJsonHelpers
{
for (auto i : range)
{
auto &csg1 = *gfx_get_g1_element(SPR_CSG_BEGIN + i);
auto& csg1 = *gfx_get_g1_element(SPR_CSG_BEGIN + i);
auto length = g1_calculate_data_size(&csg1);
auto g1 = csg1;
g1.offset = (uint8_t *)std::malloc(length);
g1.offset = (uint8_t*)std::malloc(length);
std::memcpy(g1.offset, csg1.offset, length);
result.push_back(g1);
}
@@ -316,7 +307,7 @@ namespace ObjectJsonHelpers
{
auto length = g1_calculate_data_size(og1);
auto g1 = *og1;
g1.offset = (uint8_t *)std::malloc(length);
g1.offset = (uint8_t*)std::malloc(length);
std::memcpy(g1.offset, og1->offset, length);
result.push_back(g1);
}
@@ -359,7 +350,7 @@ namespace ObjectJsonHelpers
return result;
}
static std::vector<rct_g1_element> ParseImages(IReadObjectContext * context, json_t * el)
static std::vector<rct_g1_element> ParseImages(IReadObjectContext* context, json_t* el)
{
auto path = GetString(el, "path");
auto x = GetInteger(el, "x");
@@ -395,27 +386,31 @@ namespace ObjectJsonHelpers
return result;
}
static uint8_t ParseStringId(const std::string &s)
static uint8_t 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;
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)
void LoadStrings(const json_t* root, StringTable& stringTable)
{
auto jsonStrings = json_object_get(root, "strings");
const char * key;
json_t * jlanguages;
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;
const char* locale;
json_t* jstring;
json_object_foreach(jlanguages, locale, jstring)
{
auto langId = language_get_id_from_locale(locale);
@@ -430,13 +425,13 @@ namespace ObjectJsonHelpers
stringTable.Sort();
}
void LoadImages(IReadObjectContext * context, const json_t * root, ImageTable &imageTable)
void LoadImages(IReadObjectContext* context, const json_t* root, ImageTable& imageTable)
{
if (context->ShouldLoadImages())
{
auto jsonImages = json_object_get(root, "images");
size_t i;
json_t * el;
json_t* el;
json_array_foreach(jsonImages, i, el)
{
std::vector<rct_g1_element> images;
@@ -449,7 +444,7 @@ namespace ObjectJsonHelpers
{
images = ParseImages(context, el);
}
for (const auto &g1 : images)
for (const auto& g1 : images)
{
imageTable.AddImage(&g1);
std::free(g1.offset);