Rename blob to stream

This commit is contained in:
Ted John 2018-12-30 02:03:24 +00:00
parent 61e726478d
commit d1185dc73e
1 changed files with 174 additions and 174 deletions

View File

@ -84,27 +84,27 @@ namespace OpenRCT2
std::vector<rct_object_entry> RequiredObjects;
private:
std::unique_ptr<OrcaStream> _blob;
std::unique_ptr<OrcaStream> _os;
public:
void Load(const std::string_view& path)
{
_blob = std::make_unique<OrcaStream>(path, OrcaStream::Mode::READING);
_os = std::make_unique<OrcaStream>(path, OrcaStream::Mode::READING);
RequiredObjects.clear();
WriteObjectsChunk(*_blob);
WriteObjectsChunk(*_os);
}
void Import()
{
auto& blob = *_blob;
WriteTilesChunk(blob);
WriteScenarioChunk(blob);
WriteGeneralChunk(blob);
WriteParkChunk(blob);
WriteClimateChunk(blob);
WriteResearch(blob);
WriteNotifications(blob);
WriteInterfaceChunk(blob);
auto& os = *_os;
WriteTilesChunk(os);
WriteScenarioChunk(os);
WriteGeneralChunk(os);
WriteParkChunk(os);
WriteClimateChunk(os);
WriteResearch(os);
WriteNotifications(os);
WriteInterfaceChunk(os);
// Initial cash will eventually be removed
gInitialCash = gCash;
@ -114,52 +114,52 @@ namespace OpenRCT2
void Save(const std::string_view& path)
{
OrcaStream blob(path, OrcaStream::Mode::WRITING);
OrcaStream os(path, OrcaStream::Mode::WRITING);
auto& header = blob.GetHeader();
auto& header = os.GetHeader();
header.Magic = PARK_FILE_MAGIC;
header.TargetVersion = PARK_FILE_CURRENT_VERSION;
header.MinVersion = PARK_FILE_MIN_VERSION;
WriteAuthoringChunk(blob);
WriteObjectsChunk(blob);
WriteTilesChunk(blob);
WriteScenarioChunk(blob);
WriteGeneralChunk(blob);
WriteParkChunk(blob);
WriteClimateChunk(blob);
WriteResearch(blob);
WriteNotifications(blob);
WriteInterfaceChunk(blob);
WriteAuthoringChunk(os);
WriteObjectsChunk(os);
WriteTilesChunk(os);
WriteScenarioChunk(os);
WriteGeneralChunk(os);
WriteParkChunk(os);
WriteClimateChunk(os);
WriteResearch(os);
WriteNotifications(os);
WriteInterfaceChunk(os);
}
private:
void WriteAuthoringChunk(OrcaStream& blob)
void WriteAuthoringChunk(OrcaStream& os)
{
// Write-only for now
if (blob.GetMode() == OrcaStream::Mode::WRITING)
if (os.GetMode() == OrcaStream::Mode::WRITING)
{
blob.ReadWriteChunk(ParkFileChunkType::AUTHORING, [](OrcaStream::ChunkStream& b) {
b.Write(std::string_view(gVersionInfoFull));
os.ReadWriteChunk(ParkFileChunkType::AUTHORING, [](OrcaStream::ChunkStream& cs) {
cs.Write(std::string_view(gVersionInfoFull));
std::vector<std::string> authors;
b.ReadWriteArray(authors, [](std::string& s) {});
b.Write(std::string_view()); // custom notes that can be attached to the save
b.Write<uint64_t>(std::time(0)); // date started
b.Write<uint64_t>(std::time(0)); // date modified
cs.ReadWriteArray(authors, [](std::string& s) {});
cs.Write(std::string_view()); // custom notes that can be attached to the save
cs.Write<uint64_t>(std::time(0)); // date started
cs.Write<uint64_t>(std::time(0)); // date modified
});
}
}
void WriteObjectsChunk(OrcaStream& blob)
void WriteObjectsChunk(OrcaStream& os)
{
if (blob.GetMode() == OrcaStream::Mode::READING)
if (os.GetMode() == OrcaStream::Mode::READING)
{
std::vector<rct_object_entry> entries;
blob.ReadWriteChunk(ParkFileChunkType::OBJECTS, [&entries](OrcaStream::ChunkStream& b) {
b.ReadWriteArray(entries, [&b](rct_object_entry& entry) {
auto type = b.Read<uint16_t>();
auto id = b.Read<std::string>();
auto version = b.Read<std::string>();
os.ReadWriteChunk(ParkFileChunkType::OBJECTS, [&entries](OrcaStream::ChunkStream& cs) {
cs.ReadWriteArray(entries, [&cs](rct_object_entry& entry) {
auto type = cs.Read<uint16_t>();
auto id = cs.Read<std::string>();
auto version = cs.Read<std::string>();
entry.flags = type & 0x7FFF;
strncpy(entry.name, id.c_str(), 8);
@ -171,90 +171,90 @@ namespace OpenRCT2
{
std::vector<size_t> objectIds(OBJECT_ENTRY_COUNT);
std::iota(objectIds.begin(), objectIds.end(), 0);
blob.ReadWriteChunk(ParkFileChunkType::OBJECTS, [&objectIds](OrcaStream::ChunkStream& b) {
os.ReadWriteChunk(ParkFileChunkType::OBJECTS, [&objectIds](OrcaStream::ChunkStream& cs) {
auto& objManager = GetContext()->GetObjectManager();
b.ReadWriteArray(objectIds, [&b, &objManager](size_t& i) {
cs.ReadWriteArray(objectIds, [&cs, &objManager](size_t& i) {
auto obj = objManager.GetLoadedObject(i);
if (obj != nullptr)
{
auto entry = obj->GetObjectEntry();
auto type = (uint16_t)(entry->flags & 0x0F);
type |= 0x8000; // Make as legacy object
b.Write(type);
b.Write(std::string_view(entry->name, 8));
b.Write("");
cs.Write(type);
cs.Write(std::string_view(entry->name, 8));
cs.Write("");
}
else
{
b.Write<uint16_t>(0);
b.Write("");
b.Write("");
cs.Write<uint16_t>(0);
cs.Write("");
cs.Write("");
}
});
});
}
}
void WriteScenarioChunk(OrcaStream& blob)
void WriteScenarioChunk(OrcaStream& os)
{
blob.ReadWriteChunk(ParkFileChunkType::SCENARIO, [](OrcaStream::ChunkStream& b) {
b.ReadWriteAs<uint8_t, uint32_t>(gS6Info.category);
ReadWriteStringTable(b, gScenarioName, "en-GB");
os.ReadWriteChunk(ParkFileChunkType::SCENARIO, [](OrcaStream::ChunkStream& cs) {
cs.ReadWriteAs<uint8_t, uint32_t>(gS6Info.category);
ReadWriteStringTable(cs, gScenarioName, "en-GB");
std::string parkName(128, '\0');
format_string(parkName.data(), parkName.size(), gParkName, &gParkNameArgs);
ReadWriteStringTable(b, parkName, "en-GB");
ReadWriteStringTable(cs, parkName, "en-GB");
ReadWriteStringTable(b, gScenarioDetails, "en-GB");
ReadWriteStringTable(cs, gScenarioDetails, "en-GB");
b.ReadWriteAs<uint8_t, uint32_t>(gScenarioObjectiveType);
b.ReadWriteAs<uint8_t, uint16_t>(gScenarioObjectiveYear); // year
b.ReadWriteAs<uint16_t, uint32_t>(gScenarioObjectiveNumGuests); // guests
b.Write<uint16_t>(600); // rating
b.ReadWriteAs<money32, uint16_t>(gScenarioObjectiveCurrency); // excitement
b.ReadWriteAs<uint16_t, uint32_t>(gScenarioObjectiveNumGuests); // length
b.ReadWrite<money32>(gScenarioObjectiveCurrency); // park value
b.ReadWrite<money32>(gScenarioObjectiveCurrency); // ride profit
b.ReadWrite<money32>(gScenarioObjectiveCurrency); // shop profit
cs.ReadWriteAs<uint8_t, uint32_t>(gScenarioObjectiveType);
cs.ReadWriteAs<uint8_t, uint16_t>(gScenarioObjectiveYear); // year
cs.ReadWriteAs<uint16_t, uint32_t>(gScenarioObjectiveNumGuests); // guests
cs.Write<uint16_t>(600); // rating
cs.ReadWriteAs<money32, uint16_t>(gScenarioObjectiveCurrency); // excitement
cs.ReadWriteAs<uint16_t, uint32_t>(gScenarioObjectiveNumGuests); // length
cs.ReadWrite<money32>(gScenarioObjectiveCurrency); // park value
cs.ReadWrite<money32>(gScenarioObjectiveCurrency); // ride profit
cs.ReadWrite<money32>(gScenarioObjectiveCurrency); // shop profit
b.ReadWrite(gScenarioParkRatingWarningDays);
cs.ReadWrite(gScenarioParkRatingWarningDays);
b.ReadWrite(gScenarioCompletedCompanyValue);
cs.ReadWrite(gScenarioCompletedCompanyValue);
if (gScenarioCompletedCompanyValue == MONEY32_UNDEFINED
|| gScenarioCompletedCompanyValue == (money32)0x80000001)
{
b.Write("");
cs.Write("");
}
else
{
b.ReadWrite(gScenarioCompletedBy);
cs.ReadWrite(gScenarioCompletedBy);
}
});
}
void WriteGeneralChunk(OrcaStream& blob)
void WriteGeneralChunk(OrcaStream& os)
{
auto found = blob.ReadWriteChunk(ParkFileChunkType::GENERAL, [](OrcaStream::ChunkStream& b) {
b.ReadWriteAs<uint32_t, uint64_t>(gScenarioTicks);
b.ReadWriteAs<uint16_t, uint32_t>(gDateMonthTicks);
b.ReadWrite(gDateMonthsElapsed);
b.ReadWrite(gScenarioSrand0);
b.ReadWrite(gScenarioSrand1);
b.ReadWrite(gGuestInitialCash);
b.ReadWrite(gGuestInitialHunger);
b.ReadWrite(gGuestInitialThirst);
auto found = os.ReadWriteChunk(ParkFileChunkType::GENERAL, [](OrcaStream::ChunkStream& cs) {
cs.ReadWriteAs<uint32_t, uint64_t>(gScenarioTicks);
cs.ReadWriteAs<uint16_t, uint32_t>(gDateMonthTicks);
cs.ReadWrite(gDateMonthsElapsed);
cs.ReadWrite(gScenarioSrand0);
cs.ReadWrite(gScenarioSrand1);
cs.ReadWrite(gGuestInitialCash);
cs.ReadWrite(gGuestInitialHunger);
cs.ReadWrite(gGuestInitialThirst);
b.ReadWrite(gNextGuestNumber);
b.ReadWriteArray(gPeepSpawns, [&b](PeepSpawn& spawn) {
b.ReadWrite(spawn.x);
b.ReadWrite(spawn.y);
b.ReadWrite(spawn.z);
b.ReadWrite(spawn.direction);
cs.ReadWrite(gNextGuestNumber);
cs.ReadWriteArray(gPeepSpawns, [&cs](PeepSpawn& spawn) {
cs.ReadWrite(spawn.x);
cs.ReadWrite(spawn.y);
cs.ReadWrite(spawn.z);
cs.ReadWrite(spawn.direction);
});
b.ReadWrite(gLandPrice);
b.ReadWrite(gConstructionRightsPrice);
b.ReadWrite(gGrassSceneryTileLoopPosition); // TODO (this needs to be xy32)
cs.ReadWrite(gLandPrice);
cs.ReadWrite(gConstructionRightsPrice);
cs.ReadWrite(gGrassSceneryTileLoopPosition); // TODO (this needs to be xy32)
});
if (!found)
{
@ -262,66 +262,66 @@ namespace OpenRCT2
}
}
void WriteInterfaceChunk(OrcaStream& blob)
void WriteInterfaceChunk(OrcaStream& os)
{
blob.ReadWriteChunk(ParkFileChunkType::INTERFACE, [](OrcaStream::ChunkStream& b) {
b.ReadWrite(gSavedViewX);
b.ReadWrite(gSavedViewY);
b.ReadWrite(gSavedViewZoom);
b.ReadWrite(gSavedViewRotation);
b.ReadWriteAs<uint8_t, uint32_t>(gLastEntranceStyle);
os.ReadWriteChunk(ParkFileChunkType::INTERFACE, [](OrcaStream::ChunkStream& cs) {
cs.ReadWrite(gSavedViewX);
cs.ReadWrite(gSavedViewY);
cs.ReadWrite(gSavedViewZoom);
cs.ReadWrite(gSavedViewRotation);
cs.ReadWriteAs<uint8_t, uint32_t>(gLastEntranceStyle);
});
}
void WriteClimateChunk(OrcaStream& blob)
void WriteClimateChunk(OrcaStream& os)
{
blob.ReadWriteChunk(ParkFileChunkType::CLIMATE, [](OrcaStream::ChunkStream& b) {
b.ReadWrite(gClimate);
b.ReadWrite(gClimateUpdateTimer);
os.ReadWriteChunk(ParkFileChunkType::CLIMATE, [](OrcaStream::ChunkStream& cs) {
cs.ReadWrite(gClimate);
cs.ReadWrite(gClimateUpdateTimer);
for (const auto* cs : { &gClimateCurrent, &gClimateNext })
for (const auto* cl : { &gClimateCurrent, &gClimateNext })
{
b.ReadWrite(cs->Weather);
b.ReadWrite(cs->Temperature);
b.ReadWrite(cs->WeatherEffect);
b.ReadWrite(cs->WeatherGloom);
b.ReadWrite(cs->RainLevel);
cs.ReadWrite(cl->Weather);
cs.ReadWrite(cl->Temperature);
cs.ReadWrite(cl->WeatherEffect);
cs.ReadWrite(cl->WeatherGloom);
cs.ReadWrite(cl->RainLevel);
}
});
}
void WriteParkChunk(OrcaStream& blob)
void WriteParkChunk(OrcaStream& os)
{
blob.ReadWriteChunk(ParkFileChunkType::PARK, [](OrcaStream::ChunkStream& b) {
b.ReadWrite(gParkNameArgs);
b.ReadWrite(gCash);
b.ReadWrite(gBankLoan);
b.ReadWrite(gMaxBankLoan);
b.ReadWriteAs<uint8_t, uint16_t>(gBankLoanInterestRate);
b.ReadWriteAs<uint32_t, uint64_t>(gParkFlags);
b.ReadWriteAs<money16, money32>(gParkEntranceFee);
b.ReadWrite(gStaffHandymanColour);
b.ReadWrite(gStaffMechanicColour);
b.ReadWrite(gStaffSecurityColour);
os.ReadWriteChunk(ParkFileChunkType::PARK, [](OrcaStream::ChunkStream& cs) {
cs.ReadWrite(gParkNameArgs);
cs.ReadWrite(gCash);
cs.ReadWrite(gBankLoan);
cs.ReadWrite(gMaxBankLoan);
cs.ReadWriteAs<uint8_t, uint16_t>(gBankLoanInterestRate);
cs.ReadWriteAs<uint32_t, uint64_t>(gParkFlags);
cs.ReadWriteAs<money16, money32>(gParkEntranceFee);
cs.ReadWrite(gStaffHandymanColour);
cs.ReadWrite(gStaffMechanicColour);
cs.ReadWrite(gStaffSecurityColour);
// TODO use a uint64 or a list of active items
b.ReadWrite(gSamePriceThroughoutParkA);
b.ReadWrite(gSamePriceThroughoutParkB);
cs.ReadWrite(gSamePriceThroughoutParkA);
cs.ReadWrite(gSamePriceThroughoutParkB);
// Marketing
std::vector<std::tuple<uint32_t, uint32_t>> marketing;
if (b.GetMode() != OrcaStream::Mode::READING)
if (cs.GetMode() != OrcaStream::Mode::READING)
{
for (size_t i = 0; i < std::size(gMarketingCampaignDaysLeft); i++)
{
marketing.push_back(std::make_tuple(gMarketingCampaignDaysLeft[i], gMarketingCampaignRideIndex[i]));
}
}
b.ReadWriteArray(marketing, [&b](std::tuple<uint32_t, uint32_t>& m) {
b.ReadWrite(std::get<0>(m));
b.ReadWrite(std::get<1>(m));
cs.ReadWriteArray(marketing, [&cs](std::tuple<uint32_t, uint32_t>& m) {
cs.ReadWrite(std::get<0>(m));
cs.ReadWrite(std::get<1>(m));
});
if (b.GetMode() == OrcaStream::Mode::READING)
if (cs.GetMode() == OrcaStream::Mode::READING)
{
auto count = std::min(std::size(gMarketingCampaignDaysLeft), marketing.size());
for (size_t i = 0; i < count; i++)
@ -333,11 +333,11 @@ namespace OpenRCT2
// Awards
std::vector<Award> awards(std::begin(gCurrentAwards), std::end(gCurrentAwards));
b.ReadWriteArray(awards, [&b](Award& award) {
b.ReadWrite(award.Time);
b.ReadWrite(award.Type);
cs.ReadWriteArray(awards, [&cs](Award& award) {
cs.ReadWrite(award.Time);
cs.ReadWrite(award.Type);
});
if (b.GetMode() == OrcaStream::Mode::READING)
if (cs.GetMode() == OrcaStream::Mode::READING)
{
for (size_t i = 0; i < std::size(gCurrentAwards); i++)
{
@ -353,26 +353,26 @@ namespace OpenRCT2
}
}
b.ReadWrite(gParkRatingCasualtyPenalty);
b.ReadWrite(gCurrentExpenditure);
b.ReadWrite(gCurrentProfit);
b.ReadWrite(gTotalAdmissions);
b.ReadWrite(gTotalIncomeFromAdmissions);
cs.ReadWrite(gParkRatingCasualtyPenalty);
cs.ReadWrite(gCurrentExpenditure);
cs.ReadWrite(gCurrentProfit);
cs.ReadWrite(gTotalAdmissions);
cs.ReadWrite(gTotalIncomeFromAdmissions);
});
}
void WriteResearch(OrcaStream& blob)
void WriteResearch(OrcaStream& os)
{
blob.ReadWriteChunk(ParkFileChunkType::RESEARCH, [](OrcaStream::ChunkStream& b) {
os.ReadWriteChunk(ParkFileChunkType::RESEARCH, [](OrcaStream::ChunkStream& cs) {
// Research status
b.ReadWrite(gResearchFundingLevel);
b.ReadWrite(gResearchPriorities);
b.ReadWrite(gResearchProgressStage);
b.ReadWrite(gResearchProgress);
b.ReadWrite(gResearchExpectedMonth);
b.ReadWrite(gResearchExpectedDay);
b.ReadWrite(gResearchLastItem);
b.ReadWrite(gResearchNextItem);
cs.ReadWrite(gResearchFundingLevel);
cs.ReadWrite(gResearchPriorities);
cs.ReadWrite(gResearchProgressStage);
cs.ReadWrite(gResearchProgress);
cs.ReadWrite(gResearchExpectedMonth);
cs.ReadWrite(gResearchExpectedDay);
cs.ReadWrite(gResearchLastItem);
cs.ReadWrite(gResearchNextItem);
// Research order
// type (uint8_t)
@ -381,28 +381,28 @@ namespace OpenRCT2
});
}
void WriteNotifications(OrcaStream& blob)
void WriteNotifications(OrcaStream& os)
{
blob.ReadWriteChunk(ParkFileChunkType::NOTIFICATIONS, [](OrcaStream::ChunkStream& b) {
os.ReadWriteChunk(ParkFileChunkType::NOTIFICATIONS, [](OrcaStream::ChunkStream& cs) {
std::vector<NewsItem> notifications(std::begin(gNewsItems), std::end(gNewsItems));
b.ReadWriteArray(notifications, [&b](NewsItem& notification) {
b.ReadWrite(notification.Type);
b.ReadWrite(notification.Flags);
b.ReadWrite(notification.Assoc);
b.ReadWrite(notification.Ticks);
b.ReadWrite(notification.MonthYear);
b.ReadWrite(notification.Day);
if (b.GetMode() == OrcaStream::Mode::READING)
cs.ReadWriteArray(notifications, [&cs](NewsItem& notification) {
cs.ReadWrite(notification.Type);
cs.ReadWrite(notification.Flags);
cs.ReadWrite(notification.Assoc);
cs.ReadWrite(notification.Ticks);
cs.ReadWrite(notification.MonthYear);
cs.ReadWrite(notification.Day);
if (cs.GetMode() == OrcaStream::Mode::READING)
{
auto s = b.Read<std::string>();
auto s = cs.Read<std::string>();
String::Set(notification.Text, sizeof(notification.Text), s.c_str());
}
else
{
b.Write(std::string(notification.Text));
cs.Write(std::string(notification.Text));
}
});
if (b.GetMode() == OrcaStream::Mode::READING)
if (cs.GetMode() == OrcaStream::Mode::READING)
{
for (size_t i = 0; i < std::size(gNewsItems); i++)
{
@ -420,34 +420,34 @@ namespace OpenRCT2
});
}
void WriteDerivedChunk(OrcaStream& blob)
void WriteDerivedChunk(OrcaStream& os)
{
if (blob.GetMode() == OrcaStream::Mode::WRITING)
if (os.GetMode() == OrcaStream::Mode::WRITING)
{
blob.ReadWriteChunk(ParkFileChunkType::NOTIFICATIONS, [](OrcaStream::ChunkStream& b) {
b.Write<uint32_t>(gParkSize);
b.Write<uint32_t>(gNumGuestsInPark);
b.Write<uint32_t>(gNumGuestsHeadingForPark);
b.Write<uint32_t>(gCompanyValue);
b.Write<uint32_t>(gParkValue);
b.Write<uint32_t>(gParkRating);
os.ReadWriteChunk(ParkFileChunkType::NOTIFICATIONS, [](OrcaStream::ChunkStream& cs) {
cs.Write<uint32_t>(gParkSize);
cs.Write<uint32_t>(gNumGuestsInPark);
cs.Write<uint32_t>(gNumGuestsHeadingForPark);
cs.Write<uint32_t>(gCompanyValue);
cs.Write<uint32_t>(gParkValue);
cs.Write<uint32_t>(gParkRating);
});
}
}
void WriteTilesChunk(OrcaStream& blob)
void WriteTilesChunk(OrcaStream& os)
{
auto found = blob.ReadWriteChunk(ParkFileChunkType::TILES, [](OrcaStream::ChunkStream& b) {
b.ReadWriteAs<int16_t, uint32_t>(gMapSize); // x
b.Write<uint32_t>(gMapSize); // y
auto found = os.ReadWriteChunk(ParkFileChunkType::TILES, [](OrcaStream::ChunkStream& cs) {
cs.ReadWriteAs<int16_t, uint32_t>(gMapSize); // x
cs.Write<uint32_t>(gMapSize); // y
if (b.GetMode() == OrcaStream::Mode::READING)
if (cs.GetMode() == OrcaStream::Mode::READING)
{
OpenRCT2::GetContext()->GetGameState()->InitAll(gMapSize);
}
std::vector<TileElement> tiles(std::begin(gTileElements), std::end(gTileElements));
b.ReadWriteArray(tiles, [&b](TileElement& el) { b.ReadWrite(&el, sizeof(TileElement)); });
cs.ReadWriteArray(tiles, [&cs](TileElement& el) { cs.ReadWrite(&el, sizeof(TileElement)); });
std::copy_n(tiles.data(), std::min(tiles.size(), std::size(gTileElements)), gTileElements);
map_update_tile_pointers();
@ -459,18 +459,18 @@ namespace OpenRCT2
}
}
static void ReadWriteStringTable(OrcaStream::ChunkStream& b, std::string& value, const std::string_view& lcode)
static void ReadWriteStringTable(OrcaStream::ChunkStream& cs, std::string& value, const std::string_view& lcode)
{
std::vector<std::tuple<std::string, std::string>> table;
if (b.GetMode() != OrcaStream::Mode::READING)
if (cs.GetMode() != OrcaStream::Mode::READING)
{
table.push_back(std::make_tuple(std::string(lcode), value));
}
b.ReadWriteArray(table, [&b](std::tuple<std::string, std::string>& v) {
b.ReadWrite(std::get<0>(v));
b.ReadWrite(std::get<1>(v));
cs.ReadWriteArray(table, [&cs](std::tuple<std::string, std::string>& v) {
cs.ReadWrite(std::get<0>(v));
cs.ReadWrite(std::get<1>(v));
});
if (b.GetMode() == OrcaStream::Mode::READING)
if (cs.GetMode() == OrcaStream::Mode::READING)
{
auto fr = std::find_if(table.begin(), table.end(), [&lcode](const std::tuple<std::string, std::string>& v) {
return std::get<0>(v) == lcode;