mirror of
https://github.com/OpenRCT2/OpenRCT2
synced 2026-01-18 04:23:20 +01:00
Use FileStream instead of fstream
This commit is contained in:
@@ -12,10 +12,10 @@
|
||||
#include "ShortcutIds.h"
|
||||
|
||||
#include <SDL.h>
|
||||
#include <fstream>
|
||||
#include <openrct2/Context.h>
|
||||
#include <openrct2/PlatformEnvironment.h>
|
||||
#include <openrct2/core/DataSerialiser.h>
|
||||
#include <openrct2/core/FileStream.h>
|
||||
#include <openrct2/core/FileSystem.hpp>
|
||||
#include <openrct2/core/Json.hpp>
|
||||
#include <openrct2/core/String.hpp>
|
||||
@@ -246,34 +246,26 @@ std::optional<ShortcutInput> ShortcutManager::ConvertLegacyBinding(uint16_t bind
|
||||
void ShortcutManager::LoadLegacyBindings(const fs::path& path)
|
||||
{
|
||||
constexpr int32_t SUPPORTED_FILE_VERSION = 1;
|
||||
constexpr int32_t MAX_LEGACY_SHORTCUTS = 85;
|
||||
|
||||
auto fs = std::ifstream(path);
|
||||
if (fs)
|
||||
auto fs = FileStream(path, FILE_MODE_OPEN);
|
||||
auto version = fs.ReadValue<uint16_t>();
|
||||
if (version == SUPPORTED_FILE_VERSION)
|
||||
{
|
||||
auto br = BinaryReader(&fs);
|
||||
|
||||
uint16_t version{};
|
||||
br << version;
|
||||
|
||||
if (version == SUPPORTED_FILE_VERSION)
|
||||
for (size_t i = 0; i < MAX_LEGACY_SHORTCUTS; i++)
|
||||
{
|
||||
for (size_t i = 0; i < 85; i++)
|
||||
auto value = fs.ReadValue<uint16_t>();
|
||||
auto shortcutId = GetLegacyShortcutId(i);
|
||||
if (!shortcutId.empty())
|
||||
{
|
||||
uint16_t value{};
|
||||
br << value;
|
||||
|
||||
auto shortcutId = GetLegacyShortcutId(i);
|
||||
if (!shortcutId.empty())
|
||||
auto shortcut = GetShortcut(shortcutId);
|
||||
if (shortcut != nullptr)
|
||||
{
|
||||
auto shortcut = GetShortcut(shortcutId);
|
||||
if (shortcut != nullptr)
|
||||
shortcut->Current.clear();
|
||||
auto input = ConvertLegacyBinding(value);
|
||||
if (input)
|
||||
{
|
||||
shortcut->Current.clear();
|
||||
auto input = ConvertLegacyBinding(value);
|
||||
if (input)
|
||||
{
|
||||
shortcut->Current.push_back(std::move(*input));
|
||||
}
|
||||
shortcut->Current.push_back(std::move(*input));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,67 +12,7 @@
|
||||
#include "DataSerialiserTraits.h"
|
||||
#include "MemoryStream.h"
|
||||
|
||||
#include <iostream>
|
||||
#include <type_traits>
|
||||
#include <variant>
|
||||
|
||||
class BinarySerialiser
|
||||
{
|
||||
private:
|
||||
std::variant<std::istream*, std::ostream*> _stream{};
|
||||
|
||||
public:
|
||||
BinarySerialiser(std::istream* stream)
|
||||
: _stream(stream)
|
||||
{
|
||||
}
|
||||
|
||||
BinarySerialiser(std::ostream* stream)
|
||||
: _stream(stream)
|
||||
{
|
||||
}
|
||||
|
||||
bool IsReading() const
|
||||
{
|
||||
return std::holds_alternative<std::istream*>(_stream);
|
||||
}
|
||||
|
||||
bool IsWriting() const
|
||||
{
|
||||
return std::holds_alternative<std::ostream*>(_stream);
|
||||
}
|
||||
|
||||
template<typename T> BinarySerialiser& operator<<(const T& data)
|
||||
{
|
||||
if (auto istream = std::get_if<std::istream*>(&_stream))
|
||||
{
|
||||
DataSerializerTraits<T>::decode(**istream, const_cast<T&>(data));
|
||||
}
|
||||
else if (auto ostream = std::get_if<std::ostream*>(&_stream))
|
||||
{
|
||||
DataSerializerTraits<T>::encode(**ostream, data);
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
};
|
||||
|
||||
class BinaryReader : public BinarySerialiser
|
||||
{
|
||||
public:
|
||||
BinaryReader(std::istream* stream)
|
||||
: BinarySerialiser(stream)
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
class BinaryWriter : public BinarySerialiser
|
||||
{
|
||||
public:
|
||||
BinaryWriter(std::ostream* stream)
|
||||
: BinarySerialiser(stream)
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
class DataSerialiser
|
||||
{
|
||||
|
||||
@@ -73,14 +73,6 @@ template<typename T> struct DataSerializerTraitsIntegral
|
||||
stream->Read(&temp);
|
||||
val = ByteSwapBE(temp);
|
||||
}
|
||||
static void encode(std::ostream& stream, const T& val)
|
||||
{
|
||||
stream.write(reinterpret_cast<const char*>(&val), sizeof(val));
|
||||
}
|
||||
static void decode(std::istream& stream, T& val)
|
||||
{
|
||||
stream.read(reinterpret_cast<char*>(&val), sizeof(val));
|
||||
}
|
||||
static void log(OpenRCT2::IStream* stream, const T& val)
|
||||
{
|
||||
std::stringstream ss;
|
||||
|
||||
@@ -30,6 +30,11 @@
|
||||
|
||||
namespace OpenRCT2
|
||||
{
|
||||
FileStream::FileStream(const fs::path& path, int32_t fileMode)
|
||||
: FileStream(path.u8string(), fileMode)
|
||||
{
|
||||
}
|
||||
|
||||
FileStream::FileStream(const std::string& path, int32_t fileMode)
|
||||
: FileStream(path.c_str(), fileMode)
|
||||
{
|
||||
|
||||
@@ -10,6 +10,7 @@
|
||||
#pragma once
|
||||
|
||||
#include "../common.h"
|
||||
#include "FileSystem.hpp"
|
||||
#include "IStream.hpp"
|
||||
|
||||
namespace OpenRCT2
|
||||
@@ -35,6 +36,7 @@ namespace OpenRCT2
|
||||
uint64_t _fileSize = 0;
|
||||
|
||||
public:
|
||||
FileStream(const fs::path& path, int32_t fileMode);
|
||||
FileStream(const std::string& path, int32_t fileMode);
|
||||
FileStream(const utf8* path, int32_t fileMode);
|
||||
~FileStream() override;
|
||||
|
||||
Reference in New Issue
Block a user