Skip to content

Commit

Permalink
Dropped Lod::readRaw, resolved a related TODO
Browse files Browse the repository at this point in the history
  • Loading branch information
captainurist committed Oct 14, 2023
1 parent bda3adb commit 0ad29fb
Show file tree
Hide file tree
Showing 10 changed files with 24 additions and 37 deletions.
2 changes: 1 addition & 1 deletion src/Bin/LodTool/LodTool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ int runDump(const LodToolOptions &options) {
fmt::println("Root folder: {}", reader.info().rootName);

for (const std::string &name : reader.ls()) {
Blob data = reader.readRaw(name);
Blob data = reader.read(name);
LodFileFormat format = lod::magic(data, name);
bool isCompressed = format == LOD_FILE_COMPRESSED || format == LOD_FILE_PSEUDO_IMAGE;
if (isCompressed)
Expand Down
2 changes: 1 addition & 1 deletion src/Engine/GameResourceManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ void GameResourceManager::openGameResources() {
}

Blob GameResourceManager::getEventsFile(const std::string &filename) {
return lod::decodeCompressed(_eventsLodReader.readRaw(filename));
return lod::decodeCompressed(_eventsLodReader.read(filename));
}
9 changes: 5 additions & 4 deletions src/Engine/Graphics/Indoor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@

#include "Library/Random/Random.h"
#include "Library/Logger/Logger.h"
#include "Library/LodFormats/LodFormats.h"

#include "Utility/Memory/FreeDeleter.h"
#include "Utility/Math/TrigLut.h"
Expand Down Expand Up @@ -317,7 +318,7 @@ void IndoorLocation::Load(const std::string &filename, int num_days_played, int
bLoaded = true;

IndoorLocation_MM7 location;
deserialize(pGames_LOD->read(blv_filename), &location);
deserialize(lod::decodeCompressed(pGames_LOD->read(blv_filename)), &location);
reconstruct(location, this);

std::string dlv_filename = filename;
Expand All @@ -326,7 +327,7 @@ void IndoorLocation::Load(const std::string &filename, int num_days_played, int
bool respawnInitial = false; // Perform initial location respawn?
bool respawnTimed = false; // Perform timed location respawn?
IndoorDelta_MM7 delta;
if (Blob blob = pSave_LOD->read(dlv_filename)) {
if (Blob blob = lod::decodeCompressed(pSave_LOD->read(dlv_filename))) {
try {
deserialize(blob, &delta, tags::context(location));

Expand All @@ -353,12 +354,12 @@ void IndoorLocation::Load(const std::string &filename, int num_days_played, int
assert(respawnInitial + respawnTimed <= 1);

if (respawnInitial) {
deserialize(pGames_LOD->read(dlv_filename), &delta, tags::context(location));
deserialize(lod::decodeCompressed(pGames_LOD->read(dlv_filename)), &delta, tags::context(location));
*indoor_was_respawned = true;
} else if (respawnTimed) {
auto header = delta.header;
auto visibleOutlines = delta.visibleOutlines;
deserialize(pGames_LOD->read(dlv_filename), &delta, tags::context(location));
deserialize(lod::decodeCompressed(pGames_LOD->read(dlv_filename)), &delta, tags::context(location));
delta.header = header;
delta.visibleOutlines = visibleOutlines;
*indoor_was_respawned = true;
Expand Down
9 changes: 5 additions & 4 deletions src/Engine/Graphics/Outdoor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@

#include "Library/Random/Random.h"
#include "Library/Logger/Logger.h"
#include "Library/LodFormats/LodFormats.h"

#include "Utility/Memory/FreeDeleter.h"
#include "Utility/Math/TrigLut.h"
Expand Down Expand Up @@ -859,7 +860,7 @@ void OutdoorLocation::Load(const std::string &filename, int days_played, int res
odm_filename.replace(odm_filename.length() - 4, 4, ".odm");

OutdoorLocation_MM7 location;
deserialize(pGames_LOD->read(odm_filename), &location);
deserialize(lod::decodeCompressed(pGames_LOD->read(odm_filename)), &location);
reconstruct(location, this);

// ****************.ddm file*********************//
Expand All @@ -870,7 +871,7 @@ void OutdoorLocation::Load(const std::string &filename, int days_played, int res
bool respawnInitial = false; // Perform initial location respawn?
bool respawnTimed = false; // Perform timed location respawn?
OutdoorDelta_MM7 delta;
if (Blob blob = pSave_LOD->read(ddm_filename)) {
if (Blob blob = lod::decodeCompressed(pSave_LOD->read(ddm_filename))) {
try {
deserialize(blob, &delta, tags::context(location));

Expand Down Expand Up @@ -901,13 +902,13 @@ void OutdoorLocation::Load(const std::string &filename, int days_played, int res
assert(respawnInitial + respawnTimed <= 1);

if (respawnInitial) {
deserialize(pGames_LOD->read(ddm_filename), &delta, tags::context(location));
deserialize(lod::decodeCompressed(pGames_LOD->read(ddm_filename)), &delta, tags::context(location));
*outdoors_was_respawned = true;
} else if (respawnTimed) {
auto header = delta.header;
auto fullyRevealedCells = delta.fullyRevealedCells;
auto partiallyRevealedCells = delta.partiallyRevealedCells;
deserialize(pGames_LOD->read(ddm_filename), &delta, tags::context(location));
deserialize(lod::decodeCompressed(pGames_LOD->read(ddm_filename)), &delta, tags::context(location));
delta.header = header;
delta.fullyRevealedCells = fullyRevealedCells;
delta.partiallyRevealedCells = partiallyRevealedCells;
Expand Down
4 changes: 2 additions & 2 deletions src/Engine/SaveLoad.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ SaveGameHeader SaveGame(bool IsAutoSAve, bool NotSaveWorld, const std::string &t

LodReader lodReader(makeDataPath("data", "new.lod"), LOD_ALLOW_DUPLICATES);
for (const std::string &name : lodReader.ls())
lodWriter.write(name, lodReader.readRaw(name));
lodWriter.write(name, lodReader.read(name));
lodReader.close();

lodWriter.write("image.pcx", render->PackScreenshot(150, 112));
Expand Down Expand Up @@ -346,7 +346,7 @@ void SaveNewGame() {
// Copy ddm & dlv files.
for (const std::string &name : pGames_LOD->ls())
if (name.ends_with(".ddm") || name.ends_with(".dlv"))
lodWriter.write(name, pGames_LOD->readRaw(name));
lodWriter.write(name, pGames_LOD->read(name));

pSavegameList->pSavegameHeader[0].locationName = "out01.odm";

Expand Down
2 changes: 1 addition & 1 deletion src/Library/Lod/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ set(LIBRARY_LOD_HEADERS
LodWriter.h)

add_library(library_lod STATIC ${LIBRARY_LOD_SOURCES} ${LIBRARY_LOD_HEADERS})
target_link_libraries(library_lod PUBLIC library_lodformats library_serialization library_binary utility)
target_link_libraries(library_lod PUBLIC library_serialization library_binary utility)
target_check_style(library_lod)

if(OE_BUILD_TESTS)
Expand Down
11 changes: 0 additions & 11 deletions src/Library/Lod/LodReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

#include "Library/Compression/Compression.h"
#include "Library/Snapshots/SnapshotSerialization.h"
#include "Library/LodFormats/LodFormats.h"

#include "Utility/Streams/BlobInputStream.h"
#include "Utility/Exception.h"
Expand Down Expand Up @@ -143,16 +142,6 @@ bool LodReader::exists(const std::string &filename) const {
Blob LodReader::read(const std::string &filename) const {
assert(isOpen());

Blob result = readRaw(filename);
LodFileFormat format = lod::magic(result, filename);
if (format == LOD_FILE_COMPRESSED)
result = lod::decodeCompressed(result); // TODO(captainurist): doesn't belong here.
return result;
}

Blob LodReader::readRaw(const std::string &filename) const {
assert(isOpen());

const auto pos = _files.find(toLower(filename));
if (pos == _files.cend())
throw Exception("Entry '{}' doesn't exist in LOD file '{}'", filename, _path);
Expand Down
4 changes: 0 additions & 4 deletions src/Library/Lod/LodReader.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,13 @@ class LodReader final {
*/
[[nodiscard]] bool exists(const std::string &filename) const;

// TODO(captainurist): compression should be handled at the next layer, we should only have read() here, and it
// should do what readRaw does now.
/**
* @param filename Name of the LOD file entry.
* @return Contents of the file inside the LOD as a `Blob`.
* @throws Exception If file doesn't exist inside the LOD.
*/
[[nodiscard]] Blob read(const std::string &filename) const;

[[nodiscard]] Blob readRaw(const std::string &filename) const;

/**
* @return List of all files in a LOD.
*/
Expand Down
6 changes: 3 additions & 3 deletions src/Library/Lod/Tests/LodReader_ut.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ UNIT_TEST(LodReader, RussianLod) {
EXPECT_TRUE(reader.exists("lolkek"));
EXPECT_FALSE(reader.exists("lolkek1"));
EXPECT_FALSE(reader.exists("lolke"));
EXPECT_EQ(reader.readRaw("lolkek").string_view(), "datadatadatadata");
EXPECT_EQ(reader.read("lolkek").string_view(), "datadatadatadata");

// LODs are case-insensitive.
EXPECT_TRUE(reader.exists("lolKEK"));
EXPECT_EQ(reader.readRaw("LOLkek").string_view(), "datadatadatadata");
EXPECT_EQ(reader.read("LOLkek").string_view(), "datadatadatadata");

// Check that we throw when accessing non-existent files.
EXPECT_THROW((void) reader.readRaw("lolke"), std::exception);
EXPECT_THROW((void) reader.read("lolke"), std::exception);
}
12 changes: 6 additions & 6 deletions src/Library/Lod/Tests/LodWriter_ut.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ UNIT_TEST(LodWriter, TestWrite) {
EXPECT_TRUE(reader.exists("2"));
EXPECT_TRUE(reader.exists("3"));
EXPECT_TRUE(reader.exists("4"));
EXPECT_EQ(reader.readRaw("1").string_view(), file1);
EXPECT_EQ(reader.readRaw("2").string_view(), file2);
EXPECT_EQ(reader.readRaw("3").string_view(), file3);
EXPECT_EQ(reader.readRaw("4").string_view(), file4);
EXPECT_EQ(reader.read("1").string_view(), file1);
EXPECT_EQ(reader.read("2").string_view(), file2);
EXPECT_EQ(reader.read("3").string_view(), file3);
EXPECT_EQ(reader.read("4").string_view(), file4);
}

UNIT_TEST(LodWriter, Overwrite) {
Expand Down Expand Up @@ -85,8 +85,8 @@ UNIT_TEST(LodWriter, Overwrite) {
EXPECT_EQ(reader.info().rootName, info.rootName);
EXPECT_TRUE(reader.exists("1"));
EXPECT_TRUE(reader.exists("2"));
EXPECT_EQ(reader.readRaw("1").string_view(), file1);
EXPECT_EQ(reader.readRaw("2").string_view(), file2_2);
EXPECT_EQ(reader.read("1").string_view(), file1);
EXPECT_EQ(reader.read("2").string_view(), file2_2);

cleanup();
}

0 comments on commit 0ad29fb

Please sign in to comment.