Files
OpenRCT2-Web/src/openrct2/object/ObjectRepository.cpp
T

817 lines
26 KiB
C++
Raw Normal View History

/*****************************************************************************
* Copyright (c) 2014-2020 OpenRCT2 developers
*
2018-06-15 14:07:34 +02:00
* For a complete list of all authors, please refer to contributors.md
* Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2
*
2018-06-15 14:07:34 +02:00
* OpenRCT2 is licensed under the GNU General Public License version 3.
*****************************************************************************/
2018-06-22 23:03:20 +02:00
#include "ObjectRepository.h"
#include "../Context.h"
2018-06-22 23:03:20 +02:00
#include "../PlatformEnvironment.h"
#include "../common.h"
#include "../config/Config.h"
2016-06-25 16:34:35 +01:00
#include "../core/Console.hpp"
2017-08-30 21:58:43 +01:00
#include "../core/FileIndex.hpp"
#include "../core/FileStream.hpp"
#include "../core/Guard.hpp"
#include "../core/IStream.hpp"
#include "../core/Memory.hpp"
2016-07-07 19:17:09 +01:00
#include "../core/MemoryStream.h"
2016-06-25 01:41:25 +01:00
#include "../core/Path.hpp"
#include "../core/String.hpp"
2018-01-06 18:32:25 +01:00
#include "../localisation/Localisation.h"
2018-04-26 23:48:25 +01:00
#include "../localisation/LocalisationService.h"
2018-01-02 20:36:42 +01:00
#include "../object/Object.h"
#include "../platform/platform.h"
2018-06-22 23:03:20 +02:00
#include "../rct12/SawyerChunkReader.h"
#include "../rct12/SawyerChunkWriter.h"
#include "../scenario/ScenarioRepository.h"
2017-12-13 13:02:24 +01:00
#include "../util/SawyerCoding.h"
#include "../util/Util.h"
2018-06-22 23:03:20 +02:00
#include "Object.h"
#include "ObjectFactory.h"
#include "ObjectList.h"
#include "ObjectManager.h"
#include "RideObject.h"
#include <algorithm>
#include <memory>
#include <unordered_map>
#include <vector>
// windows.h defines CP_UTF8
#undef CP_UTF8
2017-06-11 12:53:37 +01:00
using namespace OpenRCT2;
2016-06-26 16:06:03 +01:00
struct ObjectEntryHash
{
2018-06-22 23:03:20 +02:00
size_t operator()(const rct_object_entry& entry) const
2016-06-26 16:06:03 +01:00
{
uint32_t hash = 5381;
2017-12-20 02:50:18 +01:00
for (auto i : entry.name)
2016-06-26 16:06:03 +01:00
{
2017-12-20 02:50:18 +01:00
hash = ((hash << 5) + hash) + i;
2016-06-26 16:06:03 +01:00
}
return hash;
}
};
2016-11-13 20:17:49 +01:00
2016-06-26 16:06:03 +01:00
struct ObjectEntryEqual
{
2018-06-22 23:03:20 +02:00
bool operator()(const rct_object_entry& lhs, const rct_object_entry& rhs) const
2016-06-26 16:06:03 +01:00
{
return memcmp(&lhs.name, &rhs.name, 8) == 0;
}
};
using ObjectEntryMap = std::unordered_map<rct_object_entry, size_t, ObjectEntryHash, ObjectEntryEqual>;
2016-07-04 00:05:39 +01:00
2017-08-30 21:58:43 +01:00
class ObjectFileIndex final : public FileIndex<ObjectRepositoryItem>
{
private:
static constexpr uint32_t MAGIC_NUMBER = 0x5844494F; // OIDX
2018-12-11 19:35:27 +01:00
static constexpr uint16_t VERSION = 19;
static constexpr auto PATTERN = "*.dat;*.pob;*.json;*.parkobj";
2017-08-30 21:58:43 +01:00
IObjectRepository& _objectRepository;
2017-08-30 21:58:43 +01:00
public:
2018-06-22 23:03:20 +02:00
explicit ObjectFileIndex(IObjectRepository& objectRepository, const IPlatformEnvironment& env)
: FileIndex(
2019-11-04 14:02:30 +01:00
"object index", MAGIC_NUMBER, VERSION, env.GetFilePath(PATHID::CACHE_OBJECTS), std::string(PATTERN),
std::vector<std::string>{
env.GetDirectoryPath(DIRBASE::OPENRCT2, DIRID::OBJECT),
env.GetDirectoryPath(DIRBASE::USER, DIRID::OBJECT),
})
2018-06-22 23:03:20 +02:00
, _objectRepository(objectRepository)
2017-08-30 21:58:43 +01:00
{
}
public:
std::tuple<bool, ObjectRepositoryItem> Create([[maybe_unused]] int32_t language, const std::string& path) const override
2017-08-30 21:58:43 +01:00
{
2018-06-22 23:03:20 +02:00
Object* object = nullptr;
2017-12-04 21:56:06 +00:00
auto extension = Path::GetExtension(path);
if (String::Equals(extension, ".json", true))
2017-08-30 21:58:43 +01:00
{
2018-05-12 16:11:51 +01:00
object = ObjectFactory::CreateObjectFromJsonFile(_objectRepository, path);
}
else if (String::Equals(extension, ".parkobj", true))
{
object = ObjectFactory::CreateObjectFromZipFile(_objectRepository, path);
2017-08-30 21:58:43 +01:00
}
else
{
2018-05-12 16:11:51 +01:00
object = ObjectFactory::CreateObjectFromLegacyFile(_objectRepository, path.c_str());
}
if (object != nullptr)
{
ObjectRepositoryItem item = {};
2018-05-12 16:11:51 +01:00
item.ObjectEntry = *object->GetObjectEntry();
2018-05-14 13:05:59 +01:00
item.Path = path;
item.Name = object->GetName();
2018-07-06 15:15:56 +02:00
item.Sources = object->GetSourceGames();
2018-05-12 16:11:51 +01:00
object->SetRepositoryItem(&item);
delete object;
return std::make_tuple(true, item);
2017-08-30 21:58:43 +01:00
}
2017-12-04 21:56:06 +00:00
return std::make_tuple(false, ObjectRepositoryItem());
2017-08-30 21:58:43 +01:00
}
protected:
2018-06-22 23:03:20 +02:00
void Serialise(IStream* stream, const ObjectRepositoryItem& item) const override
2017-08-30 21:58:43 +01:00
{
stream->WriteValue(item.ObjectEntry);
stream->WriteString(item.Path);
stream->WriteString(item.Name);
2020-04-22 17:09:29 +02:00
uint8_t sourceLength = static_cast<uint8_t>(item.Sources.size());
2018-07-06 15:15:56 +02:00
stream->WriteValue(sourceLength);
for (auto source : item.Sources)
{
stream->WriteValue(source);
}
2017-08-30 21:58:43 +01:00
switch (item.ObjectEntry.GetType())
2018-06-22 23:03:20 +02:00
{
case OBJECT_TYPE_RIDE:
stream->WriteValue<uint8_t>(item.RideInfo.RideFlags);
for (int32_t i = 0; i < MAX_CATEGORIES_PER_RIDE; i++)
{
stream->WriteValue<uint8_t>(item.RideInfo.RideCategory[i]);
}
for (int32_t i = 0; i < MAX_RIDE_TYPES_PER_RIDE_ENTRY; i++)
{
stream->WriteValue<uint8_t>(item.RideInfo.RideType[i]);
}
stream->WriteValue<uint8_t>(item.RideInfo.RideGroupIndex);
break;
case OBJECT_TYPE_SCENERY_GROUP:
2020-04-22 17:09:29 +02:00
stream->WriteValue<uint16_t>(static_cast<uint16_t>(item.SceneryGroupInfo.Entries.size()));
2018-06-22 23:03:20 +02:00
for (const auto& entry : item.SceneryGroupInfo.Entries)
{
stream->WriteValue<rct_object_entry>(entry);
}
break;
2017-08-30 21:58:43 +01:00
}
}
2018-06-22 23:03:20 +02:00
ObjectRepositoryItem Deserialise(IStream* stream) const override
2017-08-30 21:58:43 +01:00
{
2018-05-14 13:05:59 +01:00
ObjectRepositoryItem item;
2017-08-30 21:58:43 +01:00
item.ObjectEntry = stream->ReadValue<rct_object_entry>();
2018-07-17 23:27:16 +02:00
item.Path = stream->ReadStdString();
item.Name = stream->ReadStdString();
2018-07-06 15:15:56 +02:00
auto sourceLength = stream->ReadValue<uint8_t>();
for (size_t i = 0; i < sourceLength; i++)
{
auto value = stream->ReadValue<uint8_t>();
item.Sources.push_back(value);
}
2017-08-30 21:58:43 +01:00
switch (item.ObjectEntry.GetType())
2018-06-22 23:03:20 +02:00
{
case OBJECT_TYPE_RIDE:
item.RideInfo.RideFlags = stream->ReadValue<uint8_t>();
for (int32_t i = 0; i < MAX_CATEGORIES_PER_RIDE; i++)
{
item.RideInfo.RideCategory[i] = stream->ReadValue<uint8_t>();
}
for (int32_t i = 0; i < MAX_RIDE_TYPES_PER_RIDE_ENTRY; i++)
{
item.RideInfo.RideType[i] = stream->ReadValue<uint8_t>();
}
item.RideInfo.RideGroupIndex = stream->ReadValue<uint8_t>();
break;
case OBJECT_TYPE_SCENERY_GROUP:
2017-08-30 21:58:43 +01:00
{
auto numEntries = stream->ReadValue<uint16_t>();
2018-05-14 13:05:59 +01:00
item.SceneryGroupInfo.Entries = std::vector<rct_object_entry>(numEntries);
for (size_t i = 0; i < numEntries; i++)
{
item.SceneryGroupInfo.Entries[i] = stream->ReadValue<rct_object_entry>();
}
break;
2017-08-30 21:58:43 +01:00
}
}
return item;
}
private:
2018-06-22 23:03:20 +02:00
bool IsTrackReadOnly(const std::string& path) const
2017-08-30 21:58:43 +01:00
{
2018-06-22 23:03:20 +02:00
return String::StartsWith(path, SearchPaths[0]) || String::StartsWith(path, SearchPaths[1]);
2017-08-30 21:58:43 +01:00
}
};
class ObjectRepository final : public IObjectRepository
{
2018-04-20 19:58:59 +01:00
std::shared_ptr<IPlatformEnvironment> const _env;
ObjectFileIndex const _fileIndex;
std::vector<ObjectRepositoryItem> _items;
ObjectEntryMap _itemMap;
public:
2018-04-27 18:47:57 +01:00
explicit ObjectRepository(const std::shared_ptr<IPlatformEnvironment>& env)
2018-06-22 23:03:20 +02:00
: _env(env)
, _fileIndex(*this, *env)
{
}
~ObjectRepository() final
2016-06-25 14:44:04 +01:00
{
ClearItems();
}
void LoadOrConstruct(int32_t language) override
{
2016-06-25 01:41:25 +01:00
ClearItems();
2018-04-26 23:48:25 +01:00
auto items = _fileIndex.LoadOrBuild(language);
2017-08-30 21:58:43 +01:00
AddItems(items);
SortItems();
}
void Construct(int32_t language) override
{
2018-04-26 23:48:25 +01:00
auto items = _fileIndex.Rebuild(language);
2017-08-30 21:58:43 +01:00
AddItems(items);
SortItems();
}
2016-11-04 09:44:13 +01:00
size_t GetNumObjects() const override
2016-06-28 23:24:49 +01:00
{
return _items.size();
}
2018-06-22 23:03:20 +02:00
const ObjectRepositoryItem* GetObjects() const override
2016-06-28 23:24:49 +01:00
{
return _items.data();
}
const ObjectRepositoryItem* FindObject(const std::string_view& legacyIdentifier) const override
2016-06-26 16:55:46 +01:00
{
rct_object_entry entry = {};
entry.SetName(legacyIdentifier);
2016-06-26 16:55:46 +01:00
auto kvp = _itemMap.find(entry);
if (kvp != _itemMap.end())
{
return &_items[kvp->second];
}
return nullptr;
}
2018-06-22 23:03:20 +02:00
const ObjectRepositoryItem* FindObject(const rct_object_entry* objectEntry) const override final
{
2016-06-26 16:06:03 +01:00
auto kvp = _itemMap.find(*objectEntry);
if (kvp != _itemMap.end())
{
2016-06-26 16:06:03 +01:00
return &_items[kvp->second];
}
return nullptr;
}
2018-06-22 23:03:20 +02:00
Object* LoadObject(const ObjectRepositoryItem* ori) override
2016-06-25 16:34:35 +01:00
{
2016-07-16 14:17:36 +01:00
Guard::ArgumentNotNull(ori, GUARD_LINE);
2016-07-03 21:05:18 +01:00
2017-12-04 21:56:06 +00:00
auto extension = Path::GetExtension(ori->Path);
if (String::Equals(extension, ".json", true))
{
return ObjectFactory::CreateObjectFromJsonFile(*this, ori->Path);
2017-12-04 21:56:06 +00:00
}
2018-05-12 16:11:51 +01:00
else if (String::Equals(extension, ".parkobj", true))
{
return ObjectFactory::CreateObjectFromZipFile(*this, ori->Path);
}
2017-12-04 21:56:06 +00:00
else
{
2018-05-14 13:05:59 +01:00
return ObjectFactory::CreateObjectFromLegacyFile(*this, ori->Path.c_str());
2017-12-04 21:56:06 +00:00
}
2016-06-25 16:34:35 +01:00
}
2018-06-22 23:03:20 +02:00
void RegisterLoadedObject(const ObjectRepositoryItem* ori, Object* object) override
2016-07-04 00:05:39 +01:00
{
2018-06-22 23:03:20 +02:00
ObjectRepositoryItem* item = &_items[ori->Id];
2016-07-04 00:05:39 +01:00
2016-07-16 14:17:36 +01:00
Guard::Assert(item->LoadedObject == nullptr, GUARD_LINE);
2016-07-04 00:05:39 +01:00
item->LoadedObject = object;
}
2018-06-22 23:03:20 +02:00
void UnregisterLoadedObject(const ObjectRepositoryItem* ori, Object* object) override
2016-07-04 00:05:39 +01:00
{
2018-06-22 23:03:20 +02:00
ObjectRepositoryItem* item = &_items[ori->Id];
2016-07-04 00:05:39 +01:00
if (item->LoadedObject == object)
{
item->LoadedObject = nullptr;
}
}
2018-06-22 23:03:20 +02:00
void AddObject(const rct_object_entry* objectEntry, const void* data, size_t dataSize) override
2016-06-26 23:30:44 +01:00
{
2016-07-09 15:24:00 +01:00
utf8 objectName[9];
object_entry_get_name_fixed(objectName, sizeof(objectName), objectEntry);
2016-06-26 23:30:44 +01:00
2016-07-09 14:52:56 +01:00
// Check that the object is loadable before writing it
2018-06-22 23:03:20 +02:00
Object* object = ObjectFactory::CreateObjectFromLegacyData(*this, objectEntry, data, dataSize);
2016-07-09 14:52:56 +01:00
if (object == nullptr)
2016-06-26 23:30:44 +01:00
{
2016-07-09 15:02:05 +01:00
Console::Error::WriteLine("[%s] Unable to export object.", objectName);
2016-06-26 23:30:44 +01:00
}
2016-07-09 14:52:56 +01:00
else
2016-06-26 23:30:44 +01:00
{
2016-07-10 16:34:54 +01:00
log_verbose("Adding object: [%s]", objectName);
auto path = GetPathForNewObject(objectName);
2016-07-09 14:52:56 +01:00
try
{
SaveObject(path, objectEntry, data, dataSize);
ScanObject(path);
}
2018-06-22 23:03:20 +02:00
catch (const std::exception&)
2016-07-09 14:52:56 +01:00
{
Console::Error::WriteLine("Failed saving object: [%s] to '%s'.", objectName, path.c_str());
2016-07-09 14:52:56 +01:00
}
2016-06-26 23:30:44 +01:00
}
}
2019-02-02 17:49:34 +00:00
void AddObjectFromFile(const std::string_view& objectName, const void* data, size_t dataSize) override
{
log_verbose("Adding object: [%s]", std::string(objectName).c_str());
auto path = GetPathForNewObject(objectName);
2019-02-02 17:49:34 +00:00
try
{
File::WriteAllBytes(path, data, dataSize);
ScanObject(path);
}
catch (const std::exception&)
{
Console::Error::WriteLine("Failed saving object: [%s] to '%s'.", std::string(objectName).c_str(), path.c_str());
2019-02-02 17:49:34 +00:00
}
}
2018-06-22 23:03:20 +02:00
void ExportPackedObject(IStream* stream) override
{
auto chunkReader = SawyerChunkReader(stream);
// Check if we already have this object
rct_object_entry entry = stream->ReadValue<rct_object_entry>();
if (FindObject(&entry) != nullptr)
{
chunkReader.SkipChunk();
}
else
{
// Read object and save to new file
std::shared_ptr<SawyerChunk> chunk = chunkReader.ReadChunk();
AddObject(&entry, chunk->GetData(), chunk->GetLength());
}
2017-02-05 15:45:23 +00:00
}
2018-06-22 23:03:20 +02:00
void WritePackedObjects(IStream* stream, std::vector<const ObjectRepositoryItem*>& objects) override
2017-02-05 15:45:23 +00:00
{
log_verbose("packing %u objects", objects.size());
2018-06-22 23:03:20 +02:00
for (const auto& object : objects)
2017-02-05 15:45:23 +00:00
{
Guard::ArgumentNotNull(object);
log_verbose("exporting object %.8s", object->ObjectEntry.name);
if (IsObjectCustom(object))
{
WritePackedObject(stream, &object->ObjectEntry);
}
else
{
log_warning("Refusing to pack vanilla/expansion object \"%s\"", object->ObjectEntry.name);
}
}
}
private:
2016-06-25 01:41:25 +01:00
void ClearItems()
{
_items.clear();
2016-06-26 16:06:03 +01:00
_itemMap.clear();
2016-06-25 01:41:25 +01:00
}
2016-06-29 21:34:59 +01:00
void SortItems()
{
2018-06-22 23:03:20 +02:00
std::sort(_items.begin(), _items.end(), [](const ObjectRepositoryItem& a, const ObjectRepositoryItem& b) -> bool {
return String::Compare(a.Name, b.Name) < 0;
2016-06-29 21:34:59 +01:00
});
2017-08-30 21:58:43 +01:00
// Fix the IDs
for (size_t i = 0; i < _items.size(); i++)
{
_items[i].Id = i;
}
2016-06-29 21:34:59 +01:00
// Rebuild item map
_itemMap.clear();
for (size_t i = 0; i < _items.size(); i++)
{
rct_object_entry entry = _items[i].ObjectEntry;
_itemMap[entry] = i;
}
}
2018-06-22 23:03:20 +02:00
void AddItems(const std::vector<ObjectRepositoryItem>& items)
2016-06-26 16:06:03 +01:00
{
size_t numConflicts = 0;
2017-08-30 21:58:43 +01:00
for (auto item : items)
{
if (!AddItem(item))
{
numConflicts++;
}
2017-08-30 21:58:43 +01:00
}
2017-08-31 18:00:33 +01:00
if (numConflicts > 0)
{
Console::Error::WriteLine("%zu object conflicts found.", numConflicts);
}
2017-08-30 21:58:43 +01:00
}
2018-06-22 23:03:20 +02:00
bool AddItem(const ObjectRepositoryItem& item)
2017-08-30 21:58:43 +01:00
{
auto conflict = FindObject(&item.ObjectEntry);
if (conflict == nullptr)
{
2016-07-04 00:05:39 +01:00
size_t index = _items.size();
2017-08-30 21:58:43 +01:00
auto copy = item;
copy.Id = index;
_items.push_back(copy);
_itemMap[item.ObjectEntry] = index;
return true;
}
else
{
2018-05-14 13:05:59 +01:00
Console::Error::WriteLine("Object conflict: '%s'", conflict->Path.c_str());
Console::Error::WriteLine(" : '%s'", item.Path.c_str());
return false;
}
2016-06-26 16:06:03 +01:00
}
2018-06-22 23:03:20 +02:00
void ScanObject(const std::string& path)
{
2018-04-27 18:23:39 +01:00
auto language = LocalisationService_GetCurrentLanguage();
auto result = _fileIndex.Create(language, path);
2017-08-30 21:58:43 +01:00
if (std::get<0>(result))
{
auto ori = std::get<1>(result);
AddItem(ori);
}
}
static void SaveObject(
const std::string_view& path, const rct_object_entry* entry, const void* data, size_t dataSize, bool fixChecksum = true)
2016-06-26 23:30:44 +01:00
{
2016-07-11 19:09:16 +01:00
if (fixChecksum)
{
uint32_t realChecksum = object_calculate_checksum(entry, data, dataSize);
2016-07-11 19:09:16 +01:00
if (realChecksum != entry->checksum)
{
2016-07-11 19:09:16 +01:00
char objectName[9];
object_entry_get_name_fixed(objectName, sizeof(objectName), entry);
log_verbose("[%s] Incorrect checksum, adding salt bytes...", objectName);
// Calculate the value of extra bytes that can be appended to the data so that the
// data is then valid for the object's checksum
size_t extraBytesCount = 0;
2018-06-22 23:03:20 +02:00
void* extraBytes = CalculateExtraBytesToFixChecksum(realChecksum, entry->checksum, &extraBytesCount);
2016-07-11 19:09:16 +01:00
// Create new data blob with appended bytes
size_t newDataSize = dataSize + extraBytesCount;
2018-06-22 23:03:20 +02:00
uint8_t* newData = Memory::Allocate<uint8_t>(newDataSize);
uint8_t* newDataSaltOffset = newData + dataSize;
2020-04-22 17:09:29 +02:00
std::copy_n(static_cast<const uint8_t*>(data), dataSize, newData);
std::copy_n(static_cast<const uint8_t*>(extraBytes), extraBytesCount, newDataSaltOffset);
2016-07-11 19:09:16 +01:00
try
{
uint32_t newRealChecksum = object_calculate_checksum(entry, newData, newDataSize);
2016-07-11 19:09:16 +01:00
if (newRealChecksum != entry->checksum)
{
Console::Error::WriteLine("CalculateExtraBytesToFixChecksum failed to fix checksum.");
2016-07-11 19:09:16 +01:00
// Save old data form
SaveObject(path, entry, data, dataSize, false);
}
else
{
// Save new data form
SaveObject(path, entry, newData, newDataSize, false);
}
Memory::Free(newData);
Memory::Free(extraBytes);
}
2018-06-22 23:03:20 +02:00
catch (const std::exception&)
2016-07-11 19:09:16 +01:00
{
Memory::Free(newData);
Memory::Free(extraBytes);
throw;
}
return;
}
}
2016-07-11 19:09:16 +01:00
// Encode data
uint8_t objectType = entry->GetType();
2016-07-11 19:09:16 +01:00
sawyercoding_chunk_header chunkHeader;
chunkHeader.encoding = object_entry_group_encoding[objectType];
2020-04-22 17:09:29 +02:00
chunkHeader.length = static_cast<uint32_t>(dataSize);
2018-06-22 23:03:20 +02:00
uint8_t* encodedDataBuffer = Memory::Allocate<uint8_t>(0x600000);
size_t encodedDataSize = sawyercoding_write_chunk_buffer(encodedDataBuffer, (uint8_t*)data, chunkHeader);
2016-07-11 19:09:16 +01:00
// Save to file
try
{
auto fs = FileStream(std::string(path), FILE_MODE_WRITE);
fs.Write(entry, sizeof(rct_object_entry));
fs.Write(encodedDataBuffer, encodedDataSize);
Memory::Free(encodedDataBuffer);
}
2018-06-22 23:03:20 +02:00
catch (const std::exception&)
{
Memory::Free(encodedDataBuffer);
throw;
}
2016-06-26 23:30:44 +01:00
}
2018-06-22 23:03:20 +02:00
static void* CalculateExtraBytesToFixChecksum(int32_t currentChecksum, int32_t targetChecksum, size_t* outSize)
{
// Allocate 11 extra bytes to manipulate the checksum
2018-06-22 23:03:20 +02:00
uint8_t* salt = Memory::Allocate<uint8_t>(11);
if (outSize != nullptr)
*outSize = 11;
// Next work out which bits need to be flipped to make the current checksum match the one in the file
// The bitwise rotation compensates for the rotation performed during the checksum calculation*/
int32_t bitsToFlip = targetChecksum ^ ((currentChecksum << 25) | (currentChecksum >> 7));
// Each set bit encountered during encoding flips one bit of the resulting checksum (so each bit of the checksum is an
// XOR of bits from the file). Here, we take each bit that should be flipped in the checksum and set one of the bits in
// the data that maps to it. 11 bytes is the minimum needed to touch every bit of the checksum - with less than that,
// you wouldn't always be able to make the checksum come out to the desired target
salt[0] = (bitsToFlip & 0x00000001) << 7;
salt[1] = ((bitsToFlip & 0x00200000) >> 14);
salt[2] = ((bitsToFlip & 0x000007F8) >> 3);
salt[3] = ((bitsToFlip & 0xFF000000) >> 24);
salt[4] = ((bitsToFlip & 0x00100000) >> 13);
salt[5] = (bitsToFlip & 0x00000004) >> 2;
salt[6] = 0;
salt[7] = ((bitsToFlip & 0x000FF000) >> 12);
salt[8] = (bitsToFlip & 0x00000002) >> 1;
salt[9] = (bitsToFlip & 0x00C00000) >> 22;
salt[10] = (bitsToFlip & 0x00000800) >> 11;
return salt;
}
std::string GetPathForNewObject(const std::string_view& name)
2016-06-26 23:30:44 +01:00
{
// Get object directory and create it if it doesn't exist
auto userObjPath = _env->GetDirectoryPath(DIRBASE::USER, DIRID::OBJECT);
Path::CreateDirectory(userObjPath);
// Find a unique file name
auto fileName = GetFileNameForNewObject(name);
auto fullPath = Path::Combine(userObjPath, fileName + ".DAT");
auto counter = 1U;
while (File::Exists(fullPath))
{
counter++;
fullPath = Path::Combine(userObjPath, String::StdFormat("%s-%02X.DAT", fileName.c_str(), counter));
}
return fullPath;
}
std::string GetFileNameForNewObject(const std::string_view& name)
{
// Trim name
2016-06-26 23:30:44 +01:00
char normalisedName[9] = { 0 };
auto maxLength = std::min<size_t>(name.size(), 8);
for (size_t i = 0; i < maxLength; i++)
2016-06-26 23:30:44 +01:00
{
if (name[i] != ' ')
{
normalisedName[i] = toupper(name[i]);
}
else
{
normalisedName[i] = '\0';
break;
2016-06-26 23:30:44 +01:00
}
}
// Convert to UTF-8 filename
return String::Convert(normalisedName, CODE_PAGE::CP_1252, CODE_PAGE::CP_UTF8);
2016-06-26 23:30:44 +01:00
}
2017-02-05 15:45:23 +00:00
2018-06-22 23:03:20 +02:00
void WritePackedObject(IStream* stream, const rct_object_entry* entry)
2017-02-05 15:45:23 +00:00
{
2018-06-22 23:03:20 +02:00
const ObjectRepositoryItem* item = FindObject(entry);
2017-02-05 15:45:23 +00:00
if (item == nullptr)
{
2018-01-02 19:23:22 +00:00
throw std::runtime_error(String::StdFormat("Unable to find object '%.8s'", entry->name));
2017-02-05 15:45:23 +00:00
}
// Read object data from file
auto fs = FileStream(item->Path, FILE_MODE_OPEN);
auto fileEntry = fs.ReadValue<rct_object_entry>();
if (!object_entry_compare(entry, &fileEntry))
{
2018-01-02 19:23:22 +00:00
throw std::runtime_error("Header found in object file does not match object to pack.");
2017-02-05 15:45:23 +00:00
}
auto chunkReader = SawyerChunkReader(&fs);
auto chunk = chunkReader.ReadChunk();
// Write object data to stream
auto chunkWriter = SawyerChunkWriter(stream);
stream->WriteValue(*entry);
chunkWriter.WriteChunk(chunk.get());
}
};
std::unique_ptr<IObjectRepository> CreateObjectRepository(const std::shared_ptr<IPlatformEnvironment>& env)
{
return std::make_unique<ObjectRepository>(env);
}
2016-06-25 01:41:25 +01:00
2018-06-22 23:03:20 +02:00
bool IsObjectCustom(const ObjectRepositoryItem* object)
2017-02-05 15:45:23 +00:00
{
Guard::ArgumentNotNull(object);
2018-12-10 15:14:50 +00:00
// Do not count our new object types as custom yet, otherwise the game
// will try to pack them into saved games.
auto type = object->ObjectEntry.GetType();
2018-12-10 15:14:50 +00:00
if (type > OBJECT_TYPE_SCENARIO_TEXT)
{
return false;
}
switch (object->GetFirstSourceGame())
{
case OBJECT_SOURCE_RCT2:
case OBJECT_SOURCE_WACKY_WORLDS:
case OBJECT_SOURCE_TIME_TWISTER:
case OBJECT_SOURCE_OPENRCT2_OFFICIAL:
return false;
default:
return true;
}
2017-02-05 15:45:23 +00:00
}
2018-06-22 23:03:20 +02:00
const rct_object_entry* object_list_find(rct_object_entry* entry)
2016-06-25 01:41:25 +01:00
{
2018-06-22 23:03:20 +02:00
const rct_object_entry* result = nullptr;
2018-08-29 00:52:10 +02:00
auto& objRepo = GetContext()->GetObjectRepository();
auto item = objRepo.FindObject(entry);
2018-02-01 18:49:14 +01:00
if (item != nullptr)
2016-06-26 16:55:46 +01:00
{
2018-02-01 18:49:14 +01:00
result = &item->ObjectEntry;
2016-06-26 16:55:46 +01:00
}
2018-02-01 18:49:14 +01:00
return result;
}
2016-06-26 16:55:46 +01:00
2018-06-22 23:03:20 +02:00
void* object_repository_load_object(const rct_object_entry* objectEntry)
2018-02-01 18:49:14 +01:00
{
2018-06-22 23:03:20 +02:00
Object* object = nullptr;
2018-08-29 00:52:10 +02:00
auto& objRepository = GetContext()->GetObjectRepository();
const ObjectRepositoryItem* ori = objRepository.FindObject(objectEntry);
2018-02-01 18:49:14 +01:00
if (ori != nullptr)
2016-06-25 01:41:25 +01:00
{
2018-08-29 00:52:10 +02:00
object = objRepository.LoadObject(ori);
2018-02-01 18:49:14 +01:00
if (object != nullptr)
2016-07-03 21:05:18 +01:00
{
2018-02-01 18:49:14 +01:00
object->Load();
2016-07-03 21:05:18 +01:00
}
2016-07-02 21:52:17 +01:00
}
2018-06-22 23:03:20 +02:00
return (void*)object;
2018-02-01 18:49:14 +01:00
}
2016-07-02 21:52:17 +01:00
2018-08-29 23:29:45 +02:00
void scenario_translate(scenario_index_entry* scenarioEntry)
2018-02-01 18:49:14 +01:00
{
rct_string_id localisedStringIds[3];
if (language_get_localised_scenario_strings(scenarioEntry->name, localisedStringIds))
2016-06-25 16:34:35 +01:00
{
2018-02-01 18:49:14 +01:00
if (localisedStringIds[0] != STR_NONE)
2016-06-25 16:34:35 +01:00
{
2018-02-01 18:49:14 +01:00
String::Set(scenarioEntry->name, sizeof(scenarioEntry->name), language_get_string(localisedStringIds[0]));
2016-06-25 16:34:35 +01:00
}
2018-02-01 18:49:14 +01:00
if (localisedStringIds[2] != STR_NONE)
2016-06-25 16:34:35 +01:00
{
2018-02-01 18:49:14 +01:00
String::Set(scenarioEntry->details, sizeof(scenarioEntry->details), language_get_string(localisedStringIds[2]));
}
}
}
2016-06-26 23:30:44 +01:00
2018-02-01 18:49:14 +01:00
size_t object_repository_get_items_count()
{
2018-08-29 00:52:10 +02:00
auto& objectRepository = GetContext()->GetObjectRepository();
return objectRepository.GetNumObjects();
2018-02-01 18:49:14 +01:00
}
2018-06-22 23:03:20 +02:00
const ObjectRepositoryItem* object_repository_get_items()
2018-02-01 18:49:14 +01:00
{
2018-08-29 00:52:10 +02:00
auto& objectRepository = GetContext()->GetObjectRepository();
return objectRepository.GetObjects();
2018-02-01 18:49:14 +01:00
}
2018-06-22 23:03:20 +02:00
const ObjectRepositoryItem* object_repository_find_object_by_entry(const rct_object_entry* entry)
2018-02-01 18:49:14 +01:00
{
2018-08-29 00:52:10 +02:00
auto& objectRepository = GetContext()->GetObjectRepository();
return objectRepository.FindObject(entry);
2018-02-01 18:49:14 +01:00
}
2018-06-22 23:03:20 +02:00
const ObjectRepositoryItem* object_repository_find_object_by_name(const char* name)
2018-02-01 18:49:14 +01:00
{
2018-08-29 00:52:10 +02:00
auto& objectRepository = GetContext()->GetObjectRepository();
return objectRepository.FindObject(name);
2018-02-01 18:49:14 +01:00
}
2018-06-22 23:03:20 +02:00
void object_delete(void* object)
2018-02-01 18:49:14 +01:00
{
if (object != nullptr)
2016-06-28 23:24:49 +01:00
{
2018-06-22 23:03:20 +02:00
Object* baseObject = static_cast<Object*>(object);
2018-02-01 18:49:14 +01:00
baseObject->Unload();
delete baseObject;
2016-07-02 22:13:37 +01:00
}
2016-06-25 01:41:25 +01:00
}
2018-02-01 18:49:14 +01:00
2018-06-22 23:03:20 +02:00
void object_draw_preview(const void* object, rct_drawpixelinfo* dpi, int32_t width, int32_t height)
2018-02-01 18:49:14 +01:00
{
2018-06-22 23:03:20 +02:00
const Object* baseObject = static_cast<const Object*>(object);
2018-02-01 18:49:14 +01:00
baseObject->DrawPreview(dpi, width, height);
}
2018-06-22 23:03:20 +02:00
bool object_entry_compare(const rct_object_entry* a, const rct_object_entry* b)
2018-02-01 18:49:14 +01:00
{
// If an official object don't bother checking checksum
if ((a->flags & 0xF0) || (b->flags & 0xF0))
{
if (a->GetType() != b->GetType())
2018-02-01 18:49:14 +01:00
{
return false;
}
int32_t match = memcmp(a->name, b->name, 8);
2018-02-01 18:49:14 +01:00
if (match)
{
return false;
}
}
else
{
if (a->flags != b->flags)
{
return false;
}
int32_t match = memcmp(a->name, b->name, 8);
2018-02-01 18:49:14 +01:00
if (match)
{
return false;
}
if (a->checksum != b->checksum)
{
return false;
}
}
return true;
}
2018-06-22 23:03:20 +02:00
int32_t object_calculate_checksum(const rct_object_entry* entry, const void* data, size_t dataLength)
2018-02-01 18:49:14 +01:00
{
2018-06-22 23:03:20 +02:00
const uint8_t* entryBytePtr = (uint8_t*)entry;
2018-02-01 18:49:14 +01:00
uint32_t checksum = 0xF369A75B;
2018-02-01 18:49:14 +01:00
checksum ^= entryBytePtr[0];
checksum = rol32(checksum, 11);
for (int32_t i = 4; i < 12; i++)
2018-02-01 18:49:14 +01:00
{
checksum ^= entryBytePtr[i];
checksum = rol32(checksum, 11);
}
2018-06-22 23:03:20 +02:00
uint8_t* dataBytes = (uint8_t*)data;
2018-02-01 18:49:14 +01:00
const size_t dataLength32 = dataLength - (dataLength & 31);
for (size_t i = 0; i < 32; i++)
{
for (size_t j = i; j < dataLength32; j += 32)
{
checksum ^= dataBytes[j];
}
checksum = rol32(checksum, 11);
}
for (size_t i = dataLength32; i < dataLength; i++)
{
checksum ^= dataBytes[i];
checksum = rol32(checksum, 11);
}
2020-04-22 17:09:29 +02:00
return static_cast<int32_t>(checksum);
2018-02-01 18:49:14 +01:00
}