#include "Template.hpp" #include "Model/GlobalStates.hpp" #include "Model/Project.hpp" #include "UI/UI.hpp" #include "Utils/I18n.hpp" #include "Utils/UUID.hpp" #include #include #include #include #include using namespace std::literals::string_view_literals; namespace fs = std::filesystem; Template::Template(Kind kind) : mKind{ kind } { } Template::Kind Template::GetKind() const { return mKind; } void TemplateAssetList::DiscoverFiles(const std::function& callback) const { auto dir = GetConnectedProject().GetTemplatesDirectory(); DiscoverFilesByExtension(callback, dir, ".cplt-template"sv); } std::string TemplateAssetList::RetrieveNameFromFile(const fs::path& file) const { std::ifstream ifs(file); if (!ifs) return ""; uint64_t len; ifs >> len; std::string name; name.reserve(len); std::copy_n(std::istreambuf_iterator(ifs), len, std::back_inserter(name)); return name; } uuids::uuid TemplateAssetList::RetrieveUuidFromFile(const fs::path& file) const { return uuids::uuid::from_string(file.stem().string()); } fs::path TemplateAssetList::RetrievePathFromAsset(const SavedAsset& asset) const { auto fileName = uuids::to_string(asset.Uuid); return GetConnectedProject().GetTemplatePath(fileName); } bool TemplateAssetList::SaveInstance(const SavedAsset& assetInfo, const Asset* asset) const { auto path = RetrievePathFromAsset(assetInfo); std::ofstream ofs(path, std::ios::binary); if (!ofs) return false; ofs << (uint64_t)assetInfo.Name.size(); ofs << assetInfo.Name; ofs << static_cast(assetInfo.Payload); if (auto tmpl = static_cast(asset)) { tmpl->WriteTo(ofs); } return true; } static std::unique_ptr