summaryrefslogtreecommitdiff
path: root/src/package.cpp
diff options
context:
space:
mode:
authorilotterytea <iltsu@alright.party>2025-02-02 22:41:32 +0500
committerilotterytea <iltsu@alright.party>2025-02-02 22:41:32 +0500
commitf27bf2dfa4975c7366013bb2f729a9823105361c (patch)
tree47938d971e1bebb36765fdd44ed37680147e87b9 /src/package.cpp
parent4466b394cbdd584d70f83024852a710a6460212e (diff)
feat: export package
Diffstat (limited to 'src/package.cpp')
-rw-r--r--src/package.cpp50
1 files changed, 50 insertions, 0 deletions
diff --git a/src/package.cpp b/src/package.cpp
index 379aaeb..e958a19 100644
--- a/src/package.cpp
+++ b/src/package.cpp
@@ -1,6 +1,9 @@
#include "package.hpp"
#include <algorithm>
+#include <filesystem>
+#include <fstream>
+#include <iostream>
namespace silly::editor {
TileSet &LevelPackage::get_tileset() { return this->tileset; }
@@ -26,4 +29,51 @@ namespace silly::editor {
}
const std::string &LevelPackage::get_name() const { return this->name; }
+
+ std::string LevelPackage::export_to_string() const {
+ std::ostringstream oss;
+
+ // package data
+ oss << "[package]\n"
+ << "# name,version\n"
+ << name << ","
+ << "0\n";
+
+ // tileset data
+ oss << this->tileset.export_to_string() << "\n";
+
+ // level data
+ for (auto it = this->levels.begin(); it != this->levels.end(); ++it) {
+ oss << it->export_to_string() << "\n";
+ }
+
+ return oss.str();
+ }
+
+ void LevelPackage::save(LevelPackageFormat format,
+ std::string &file_path) const {
+ if (format != PACKAGE_TXT) {
+ return;
+ }
+
+ // writing package file
+ std::stringstream pkg_file_path;
+
+ pkg_file_path << file_path << "/" << this->name << ".txt";
+
+ std::string package = this->export_to_string();
+
+ std::ofstream ifs(pkg_file_path.str());
+ ifs << package;
+ ifs.close();
+
+ // copying resource files
+ for (auto it = this->tileset.get_entries().begin();
+ it != this->tileset.get_entries().end(); ++it) {
+ TilesetTile *t = it->get();
+ std::string old_path = t->path;
+ std::string new_path = file_path + "/" + t->name + "." + t->extension;
+ std::filesystem::copy_file(old_path, new_path);
+ }
+ }
} \ No newline at end of file