summaryrefslogtreecommitdiff
path: root/tools/src/asset_processor
diff options
context:
space:
mode:
authoroctorock <79596758+octorock@users.noreply.github.com>2021-11-24 13:02:16 +0100
committeroctorock <79596758+octorock@users.noreply.github.com>2021-11-24 13:02:16 +0100
commitbb425249204147db4aa2c22ff20b68909478ad26 (patch)
tree44e212e150efd808c31b45c3deeeab26762bd014 /tools/src/asset_processor
parent794d5fc97cb01a70c8579dc08962b667edf5a29e (diff)
Fix offset calculation for asset build mode
Diffstat (limited to 'tools/src/asset_processor')
-rw-r--r--tools/src/asset_processor/main.cpp34
-rw-r--r--tools/src/asset_processor/offsets.cpp2
-rw-r--r--tools/src/asset_processor/offsets.h9
3 files changed, 39 insertions, 6 deletions
diff --git a/tools/src/asset_processor/main.cpp b/tools/src/asset_processor/main.cpp
index c62860a0..c04ab3d1 100644
--- a/tools/src/asset_processor/main.cpp
+++ b/tools/src/asset_processor/main.cpp
@@ -149,10 +149,16 @@ int main(int argc, char** argv) {
currentOffset = asset["offsets"][gVariant];
}
} else if (asset.contains("calculateOffsets")) { // Start offset calculation
- std::filesystem::path path = gAssetsFolder;
- path = path / asset["calculateOffsets"];
- int baseOffset = asset["start"].get<int>() + currentOffset;
- offsetCalculator = std::make_unique<OffsetCalculator>(path, baseOffset);
+ if (gMode == EXTRACT || gMode == BUILD) {
+ std::filesystem::path path = gAssetsFolder;
+ path = path / asset["calculateOffsets"];
+ int baseOffset = 0;
+ // During build mode the offsets are calculated directly instead of from a base address.
+ if (gMode == EXTRACT) {
+ baseOffset = asset["start"].get<int>() + currentOffset;
+ }
+ offsetCalculator = std::make_unique<OffsetCalculator>(path, baseOffset);
+ }
} else if (asset.contains("path")) { // Asset definition
if (asset.contains("variants")) {
@@ -182,6 +188,10 @@ int main(int argc, char** argv) {
}
case CONVERT: {
std::unique_ptr<BaseAsset> assetHandler = getAssetHandlerByType(path, asset, currentOffset);
+ if (!std::filesystem::exists(assetHandler->getBuildPath())) {
+ std::cerr << "Error: Extracted binary file " << assetHandler->getBuildPath() << " does not exist. Run `make` first." << std::endl;
+ std::exit(1);
+ }
if (shouldConvertAsset(assetHandler)) {
if (gVerbose) {
std::cout << "Converting " << assetHandler->getAssetPath() << "..." << std::endl;
@@ -192,12 +202,28 @@ int main(int argc, char** argv) {
}
case BUILD: {
std::unique_ptr<BaseAsset> assetHandler = getAssetHandlerByType(path, asset, currentOffset);
+ if (!std::filesystem::exists(assetHandler->getAssetPath())) {
+ std::cerr << "Error: Extracted asset file " << assetHandler->getAssetPath() << " does not exist. Run `make extractassets` first." << std::endl;
+ std::exit(1);
+ }
if (shouldBuildAsset(assetHandler)) {
if (gVerbose) {
std::cout << "Building " << assetHandler->getAssetPath() << "..." << std::endl;
}
buildAsset(assetHandler);
}
+ if (offsetCalculator != nullptr) {
+ // New start is the end of the previous asset.
+ int start = offsetCalculator->getLastEnd();
+ // Get the size of the current asset and calculate the end position.
+ int filesize = static_cast<int>(std::filesystem::file_size(assetHandler->getBuildPath()));
+ // Align by four bytes.
+ if (filesize % 4 != 0) {
+ filesize += 4 - (filesize % 4);
+ }
+ offsetCalculator->setLastEnd(start + filesize);
+ offsetCalculator->addAsset(start, assetHandler->getSymbol());
+ }
break;
}
}
diff --git a/tools/src/asset_processor/offsets.cpp b/tools/src/asset_processor/offsets.cpp
index bff5b1a5..827fe7a6 100644
--- a/tools/src/asset_processor/offsets.cpp
+++ b/tools/src/asset_processor/offsets.cpp
@@ -1,7 +1,7 @@
#include "offsets.h"
OffsetCalculator::OffsetCalculator(const std::filesystem::path& outputFile, int baseOffset_)
- : output(outputFile), baseOffset(baseOffset_) {
+ : output(outputFile), baseOffset(baseOffset_), lastEnd(0) {
}
void OffsetCalculator::addAsset(int start, const std::string& symbol) {
diff --git a/tools/src/asset_processor/offsets.h b/tools/src/asset_processor/offsets.h
index 8a90cfea..2117e804 100644
--- a/tools/src/asset_processor/offsets.h
+++ b/tools/src/asset_processor/offsets.h
@@ -9,10 +9,17 @@ class OffsetCalculator {
public:
OffsetCalculator(const std::filesystem::path& offsetsFile, int baseOffset_);
void addAsset(int start, const std::string& symbol);
-
+ int getLastEnd() const {
+ return lastEnd;
+ }
+ void setLastEnd(int lastEnd_) {
+ this->lastEnd = lastEnd_;
+ }
private:
std::ofstream output;
int baseOffset;
+ // Store the end of the previously added asset
+ int lastEnd;
};
#endif \ No newline at end of file