Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/awe/objectbinaryreadstream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <iterator>

#include "objectbinaryreadstream.h"
#include "src/common/exception.h"

namespace AWE {

Expand All @@ -30,6 +31,11 @@ ObjectBinaryReadStream::ObjectBinaryReadStream(Common::ReadStream &stream) : _st
ObjectBinaryReadStream::ObjectBinaryReadStream(Common::ReadStream &stream, std::shared_ptr<DPFile> dp) : _stream(stream), _dp(dp) {
}

void ObjectBinaryReadStream::assert_dp() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we rename this to expectDP?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Nostritius Thank you for your review. Hope things are working out now. I'll address the comments this week or next week

if (!_dp)
throw CreateException("dp file expected but not defined");
}

void ObjectBinaryReadStream::variable(const std::string &name, bool &value) {
value = _stream.readByte() != 0;
}
Expand All @@ -51,6 +57,7 @@ void ObjectBinaryReadStream::variable(const std::string &name, float &value) {

void ObjectBinaryReadStream::variable(const std::string &name, std::string &value, bool dp) {
if (dp) {
assert_dp();
uint32_t offset = _stream.readUint32LE();
value = _dp->getString(offset);
} else {
Expand Down Expand Up @@ -87,6 +94,7 @@ void ObjectBinaryReadStream::variable(const std::string &name, ObjectID &value)
}

void ObjectBinaryReadStream::variable(const std::string &name, std::vector<int32_t> &value) {
assert_dp();
uint32_t count = _stream.readUint32LE();
uint32_t offset = _stream.readUint32LE();
std::vector<uint32_t> values = _dp->getValues(offset, count);
Expand All @@ -103,6 +111,7 @@ void ObjectBinaryReadStream::variable(const std::string &name, std::vector<bool>
void ObjectBinaryReadStream::variable(const std::string &name, std::vector<uint32_t> &value, bool dp) {
uint32_t count = _stream.readUint32LE();
if (dp) {
assert_dp();
uint32_t offset = _stream.readUint32LE();
std::vector<uint32_t> values = _dp->getValues(offset, count);
} else {
Expand All @@ -121,38 +130,44 @@ void ObjectBinaryReadStream::variable(const std::string &name, std::vector<int32
}

void ObjectBinaryReadStream::variable(const std::string &name, std::vector<rid_t> &value) {
assert_dp();
uint32_t count = _stream.readUint32LE();
uint32_t offset = _stream.readUint32LE();
std::vector<uint32_t> values = _dp->getValues(offset, count);
std::copy(values.begin(), values.end(), std::back_inserter(value));
}

void ObjectBinaryReadStream::variable(const std::string &name, std::vector<glm::vec2> &value) {
assert_dp();
uint32_t count = _stream.readUint32LE();
uint32_t offset = _stream.readUint32LE();
value = _dp->getPositions2D(offset, count);
}

void ObjectBinaryReadStream::variable(const std::string &name, std::vector<float> &value) {
assert_dp();
uint32_t count = _stream.readUint32LE();
uint32_t offset = _stream.readUint32LE();
value = _dp->getFloats(offset, count);
}

void ObjectBinaryReadStream::variable(const std::string &name, std::vector<ObjectID> &value) {
assert_dp();
uint32_t count = _stream.readUint32LE();
uint32_t offset = _stream.readUint32LE();
std::vector<uint32_t> values = _dp->getValues(offset, count);
std::copy(values.begin(), values.end(), std::back_inserter(value));
}

void ObjectBinaryReadStream::variable(const std::string &name, std::vector<GID> &value) {
assert_dp();
uint32_t count = _stream.readUint32LE();
uint32_t offset = _stream.readUint32LE();
value = _dp->getGIDs(offset, count);
}

void ObjectBinaryReadStream::variable(const std::string &name, std::vector<std::string> &value) {
assert_dp();
uint32_t count = _stream.readUint32LE();
value.resize(count);
for (unsigned int i = 0; i < count; ++i) {
Expand All @@ -162,6 +177,7 @@ void ObjectBinaryReadStream::variable(const std::string &name, std::vector<std::
}

void ObjectBinaryReadStream::variable(const std::string &name, std::vector<std::string> &value, size_t fixedSize) {
assert_dp();
value.resize(fixedSize);
for (size_t i = 0; i < fixedSize; ++i) {
uint32_t offset = _stream.readUint32LE();
Expand Down
2 changes: 2 additions & 0 deletions src/awe/objectbinaryreadstream.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ class ObjectBinaryReadStream : public ObjectReadStream {

void skip(size_t s) override;

void assert_dp();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be private, not protected


void variable(const std::string &name, bool &value) override;
void variable(const std::string &name, int32_t &value) override;
void variable(const std::string &name, uint32_t &value, bool bigEndian) override;
Expand Down
68 changes: 37 additions & 31 deletions tools/cid2xml.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

#include <filesystem>

#include <fmt/format.h>
#include <CLI/CLI.hpp>

#include "src/common/writefile.h"
Expand Down Expand Up @@ -51,45 +52,50 @@ int main(int argc, char **argv) {

CLI11_PARSE(app, argc, argv);

// Open cid file as stream
Common::ReadFile cidFileStream(cidFile);
try {
// Open cid file as stream
Common::ReadFile cidFileStream(cidFile);

// Initialize xml object
Common::XML xml;
auto &rootNode = xml.getRootNode();
rootNode.name = "cid";
// Initialize xml object
Common::XML xml;
auto &rootNode = xml.getRootNode();
rootNode.name = "cid";

// Generate stem and deduce object type from it
const std::string stem = std::filesystem::path(cidFile).stem().string();
ObjectType type = AWE::determineObjectTypeByFilename(stem);
// Generate stem and deduce object type from it
const std::string stem = std::filesystem::path(cidFile).stem().string();
ObjectType type = AWE::determineObjectTypeByFilename(stem);

// Load dp file if given
std::shared_ptr<DPFile> dp;
if (!dpFile.empty())
dp = std::make_shared<DPFile>(new Common::ReadFile(dpFile));
// Load dp file if given
std::shared_ptr<DPFile> dp;
if (!dpFile.empty())
dp = std::make_shared<DPFile>(new Common::ReadFile(dpFile));

AWE::ObjectXMLWriteStream xmlWriteStream(xml.getRootNode());
AWE::ObjectXMLWriteStream xmlWriteStream(xml.getRootNode());

// If byte code and bytecode parameters are given, create a bytecode collection
if (!bytecodeFile.empty() && !bytecodeParametersFile.empty()) {
std::shared_ptr<AWE::Script::Collection> collection = std::make_shared<AWE::Script::Collection>(
new Common::ReadFile(bytecodeFile),
new Common::ReadFile(bytecodeParametersFile)
);
xmlWriteStream.setBytecodeCollection(collection);
}
// If byte code and bytecode parameters are given, create a bytecode collection
if (!bytecodeFile.empty() && !bytecodeParametersFile.empty()) {
std::shared_ptr<AWE::Script::Collection> collection = std::make_shared<AWE::Script::Collection>(
new Common::ReadFile(bytecodeFile),
new Common::ReadFile(bytecodeParametersFile)
);
xmlWriteStream.setBytecodeCollection(collection);
}

AWE::CIDFile cid(cidFileStream, type, dp);
AWE::CIDFile cid(cidFileStream, type, dp);

rootNode.properties["version"] = std::to_string(cid.getVersion());
rootNode.properties["version"] = std::to_string(cid.getVersion());

const auto &containers = cid.getContainers();
for (const auto &item: containers) {
xmlWriteStream.writeObject(item, type, cid.getVersion());
}
const auto &containers = cid.getContainers();
for (const auto &item: containers) {
xmlWriteStream.writeObject(item, type, cid.getVersion());
}

Common::WriteFile cidXmlStream(stem + ".xml");
xml.write(cidXmlStream, false);
Common::WriteFile cidXmlStream(stem + ".xml");
xml.write(cidXmlStream, false);

return EXIT_SUCCESS;
return EXIT_SUCCESS;
} catch (std::exception &e) {
fmt::print("Error: {}\n", e.what());
return EXIT_FAILURE;
}
}
Loading