Skip to content

Commit

Permalink
close EE-285
Browse files Browse the repository at this point in the history
  • Loading branch information
stohrendorf committed May 15, 2021
1 parent 3531810 commit 936706b
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 26 deletions.
2 changes: 1 addition & 1 deletion src/engine/audioengine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ gsl::not_null<std::shared_ptr<audio::Voice>> AudioEngine::playStream(size_t trac
else
{
auto wav = std::make_shared<SoLoud::WavStream>();
wav->load((m_rootPath / (boost::format("%03d.ogg") % trackId).str()).string().c_str());
wav->load(util::ensureFileExists(m_rootPath / (boost::format("%03d.ogg") % trackId).str()).string().c_str());
auto voice = m_soundEngine->playBackground(wav, m_streamVolume);
voice->setProtect(true);
return voice;
Expand Down
4 changes: 2 additions & 2 deletions src/engine/engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ Engine::Engine(const std::filesystem::path& rootPath, const glm::ivec2& resoluti

try
{
pybind11::eval_file((m_rootPath / "scripts" / "main.py").string());
pybind11::eval_file(util::ensureFileExists(m_rootPath / "scripts" / "main.py").string());
}
catch(std::exception& e)
{
Expand Down Expand Up @@ -345,7 +345,7 @@ std::pair<RunResult, std::optional<size_t>> Engine::runTitleMenu(world::World& w
m_presenter->apply(m_engineConfig.renderSettings);

const auto backdrop = std::make_shared<gl::TextureHandle<gl::Texture2D<gl::SRGBA8>>>(
gl::CImgWrapper{m_rootPath / "data" / "tr1" / "DATA" / "TITLEH.PCX"}.toTexture());
gl::CImgWrapper{util::ensureFileExists(m_rootPath / "data" / "tr1" / "DATA" / "TITLEH.PCX")}.toTexture());
const auto menu = std::make_shared<menu::MenuDisplay>(menu::InventoryMode::TitleMode, world);
Throttler throttler;
while(true)
Expand Down
8 changes: 5 additions & 3 deletions src/engine/presenter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ namespace engine
{
void Presenter::playVideo(const std::filesystem::path& path)
{
util::ensureFileExists(path);

m_soundEngine->getSoLoud().setGlobalVolume(1.0f);

auto mesh = createScreenQuad(m_materialManager->getFlat(false, true, true), "video");
Expand Down Expand Up @@ -341,9 +343,9 @@ Presenter::Presenter(const std::filesystem::path& rootPath, const glm::ivec2& re
, m_renderer{std::make_shared<render::scene::Renderer>(std::make_shared<render::scene::Camera>(
DefaultFov, m_window->getViewport(), DefaultNearPlane, DefaultFarPlane))}
, m_splashImage{std::make_shared<gl::TextureHandle<gl::Texture2D<gl::SRGBA8>>>(
gl::CImgWrapper{rootPath / "splash.png"}.toTexture())}
, m_trTTFFont{std::make_unique<gl::Font>(rootPath / "trfont.ttf")}
, m_debugFont{std::make_unique<gl::Font>(rootPath / "DroidSansMono.ttf")}
gl::CImgWrapper{util::ensureFileExists(rootPath / "splash.png")}.toTexture())}
, m_trTTFFont{std::make_unique<gl::Font>(util::ensureFileExists(rootPath / "trfont.ttf"))}
, m_debugFont{std::make_unique<gl::Font>(util::ensureFileExists(rootPath / "DroidSansMono.ttf"))}
, m_inputHandler{std::make_unique<hid::InputHandler>(m_window->getWindow())}
, m_shaderCache{std::make_shared<render::scene::ShaderCache>(rootPath / "shaders")}
, m_materialManager{std::make_unique<render::scene::MaterialManager>(m_shaderCache, m_renderer)}
Expand Down
2 changes: 2 additions & 0 deletions src/loader/file/level/level.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ void Level::readMeshData(io::SDLReader& reader)

std::unique_ptr<Level> Level::createLoader(const std::filesystem::path& filename, Game gameVersion)
{
util::ensureFileExists(filename);

std::filesystem::path sfxPath = filename;
sfxPath.replace_filename("MAIN.SFX");

Expand Down
24 changes: 4 additions & 20 deletions src/loader/trx/trx.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "trx.h"

#include "core/i18n.h"
#include "util/helpers.h"

#include <boost/lexical_cast.hpp>
#include <boost/log/trivial.hpp>
Expand All @@ -17,12 +18,7 @@ std::filesystem::path readSymlink(const std::filesystem::path& root,
if(ref.extension() != ".txt")
return root / ref;

std::ifstream txt{(root / ref).string()};
if(!txt.is_open())
{
BOOST_THROW_EXCEPTION(std::runtime_error("Failed to open Glidos text file"));
}

std::ifstream txt{util::ensureFileExists(root / ref)};
std::string head;
std::getline(txt, head);
boost::algorithm::trim(head);
Expand Down Expand Up @@ -173,16 +169,9 @@ void Equiv::resolve(const std::filesystem::path& root,
auto& ts = timestamps[part.getId()];
ts = std::max(ts, rootTimestamp);
const auto linked = readSymlink(root, relative(partFile, root), ts).lexically_normal();
if(linked.empty())
{
BOOST_LOG_TRIVIAL(error) << "Invalid TLNK in " << root / partFile;
continue;
}

auto& existing = filesByPart[part];
if(!existing.empty() && existing != linked)
{
//BOOST_THROW_EXCEPTION( std::runtime_error( "Equiv set references already mapped texture part" ) );
BOOST_LOG_TRIVIAL(error) << "Equiv set references already mapped texture part: " << linked << " vs. "
<< existing;
}
Expand All @@ -204,12 +193,7 @@ PathMap::PathMap(const std::filesystem::path& baseTxtName,
const std::filesystem::file_time_type& rootTimestamp,
std::map<TexturePart, std::filesystem::path>& filesByPart)
{
std::ifstream txt{baseTxtName.string()};
if(!txt.is_open())
{
BOOST_THROW_EXCEPTION(std::runtime_error("Failed to open Glidos base file"));
}

std::ifstream txt{util::ensureFileExists(baseTxtName)};
const auto baseTxtDir = baseTxtName.parent_path();

std::string head;
Expand Down Expand Up @@ -316,7 +300,7 @@ Glidos::Glidos(std::filesystem::path baseDir, const std::function<void(const std

BOOST_LOG_TRIVIAL(debug) << "Loading Glidos texture pack from " << m_baseDir;

m_rootTimestamp = last_write_time(m_baseDir / "equiv.txt");
m_rootTimestamp = last_write_time(util::ensureFileExists(m_baseDir / "equiv.txt"));

statusCallback(_("Glidos - Loading equiv.txt"));

Expand Down
5 changes: 5 additions & 0 deletions src/render/scene/shaderprogram.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "shaderprogram.h"

#include "util/helpers.h"

#include <boost/algorithm/string.hpp>
#include <boost/log/trivial.hpp>
#include <fstream>
Expand Down Expand Up @@ -142,6 +144,9 @@ gsl::not_null<std::shared_ptr<ShaderProgram>> ShaderProgram::createFromFile(cons
const std::filesystem::path& fshPath,
const std::vector<std::string>& defines)
{
util::ensureFileExists(vshPath);
util::ensureFileExists(fshPath);

const std::string vshSource = readAll(vshPath);
if(vshSource.empty())
{
Expand Down
10 changes: 10 additions & 0 deletions src/util/helpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,14 @@ std::string unescape(const std::string& escaped)

return result;
}

std::filesystem::path ensureFileExists(const std::filesystem::path& path)
{
if(!std::filesystem::is_regular_file(path))
{
BOOST_LOG_TRIVIAL(fatal) << "Could not find required file " << path;
BOOST_THROW_EXCEPTION(std::runtime_error("required file not found"));
}
return path;
}
} // namespace util
3 changes: 3 additions & 0 deletions src/util/helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "core/angle.h"
#include "core/vec.h"

#include <filesystem>
#include <glm/glm.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <gsl/gsl-lite.hpp>
Expand Down Expand Up @@ -101,4 +102,6 @@ auto bits(T value, uint8_t shr, uint8_t n) -> std::enable_if_t<std::is_unsigned_
}

extern std::string unescape(const std::string& escaped);

extern std::filesystem::path ensureFileExists(const std::filesystem::path& path);
} // namespace util

0 comments on commit 936706b

Please sign in to comment.