Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Async resource loading #2026

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
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
2 changes: 1 addition & 1 deletion src/celengine/dsodb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ bool DSODatabase::load(std::istream& in, const fs::path& resourcePath)
const Hash* objParams = objParamsValue.getHash();
if (objParams == nullptr)
{
GetLogger()->error("Error parsing deep sky catalog entry {}\n", objName.c_str());
GetLogger()->error("Error parsing deep sky catalog entry {}\n", objName);
return false;
}

Expand Down
2 changes: 1 addition & 1 deletion src/celengine/meshmanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,6 @@ inline bool operator<(const GeometryInfo::ResourceKey& k0,
std::tie(k1.resolvedPath, k1.center.x(), k1.center.y(), k1.center.z(), k1.scale, k1.isNormalized);
}

using GeometryManager = ResourceManager<GeometryInfo>;
using GeometryManager = ResourceManager<GeometryInfo, true>;

extern GeometryManager* GetGeometryManager();
61 changes: 45 additions & 16 deletions src/celestia/celestiacore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <cassert>
#include <ctime>
#include <fstream>
#include <future>
#include <iomanip>
#include <iostream>
#include <iterator>
Expand Down Expand Up @@ -2307,6 +2308,11 @@ void CelestiaCore::updateFOV(float newFOV, std::optional<Eigen::Vector2f> focus,
}
}

template<typename T>
bool ready(const std::future<T> &future)
{
return future.wait_for(std::chrono::seconds(1)) == std::future_status::ready;
}

bool CelestiaCore::initSimulation(const fs::path& configFileName,
const vector<fs::path>& extrasDirs,
Expand Down Expand Up @@ -2391,23 +2397,46 @@ bool CelestiaCore::initSimulation(const fs::path& configFileName,

StarDetails::SetStarTextures(config->starTextures);

std::unique_ptr<StarDatabase> starCatalog = loadStars(*config, progressNotifier);
if (starCatalog == nullptr)
{
fatalError(_("Cannot read star database."), false);
return false;
}
universe->setStarCatalog(std::move(starCatalog));
progressNotifier->update(_("Deep Space Objects and Star catalogs"));

/***** Load the deep sky catalogs *****/
auto *cfg = config.get();

std::unique_ptr<DSODatabase> dsoCatalog = loadDSO(*config, progressNotifier);
if (dsoCatalog == nullptr)
std::future<std::unique_ptr<StarDatabase>> starsLoader = std::async(std::launch::async, [cfg]() {
return loadStars(*cfg, nullptr);
});

std::future<std::unique_ptr<DSODatabase>> dsoLoader = std::async(std::launch::async, [cfg]() {
return loadDSO(*cfg, nullptr);
});

for (bool starsReady = false, dsoReady = false; !starsReady || !dsoReady;)
{
fatalError(_("Cannot read DSO database."), false);
return false;
if (!starsReady && ready(starsLoader))
{
starsReady = true;

std::unique_ptr<StarDatabase> starCatalog = starsLoader.get();
if (starCatalog == nullptr)
{
fatalError(_("Cannot read star database."), false);
return false;
}
universe->setStarCatalog(std::move(starCatalog));
}

if (!dsoReady && ready(dsoLoader))
{
dsoReady = true;

std::unique_ptr<DSODatabase> dsoCatalog = dsoLoader.get();
if (dsoCatalog == nullptr)
{
fatalError(_("Cannot read DSO database."), false);
return false;
}
universe->setDSOCatalog(std::move(dsoCatalog));
}
}
universe->setDSOCatalog(std::move(dsoCatalog));

/***** Load the solar system catalogs *****/

Expand Down Expand Up @@ -2669,7 +2698,7 @@ void CelestiaCore::fatalError(const string& msg, bool visual)
if (visual)
flash(msg);
else
GetLogger()->error(msg.c_str());
GetLogger()->error(msg);
}
else
{
Expand Down Expand Up @@ -3270,14 +3299,14 @@ void CelestiaCore::setAudioNoPause(int channel, bool nopause)

void CelestiaCore::pauseAudioIfNeeded()
{
for (auto const &[_, value] : audioSessions)
for (const auto &[_, value] : audioSessions)
if (!value->nopause())
value->stop();
}

void CelestiaCore::resumeAudioIfNeeded()
{
for (auto const &[_, value] : audioSessions)
for (const auto &[_, value] : audioSessions)
if (!value->nopause())
value->play();
}
Expand Down
58 changes: 42 additions & 16 deletions src/celutil/logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,48 +9,74 @@
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.

#include <cstdlib>
#include <iostream>
#include <memory>

#ifdef _MSC_VER
#include <windows.h>
# include <windows.h>
#endif
#include <fmt/ostream.h>
#include "logger.h"

#include <fmt/ostream.h>

namespace celestia::util
{

Logger* Logger::g_logger = nullptr;
namespace
{
std::unique_ptr<Logger> globalLogger = nullptr;
}

Logger* GetLogger()
Logger *
GetLogger()
{
return Logger::g_logger;
return globalLogger.get();
}

Logger* CreateLogger(Level level)
Logger *
CreateLogger(Level level)
{
return CreateLogger(level, std::clog, std::cerr);
}

Logger* CreateLogger(Level level, Logger::Stream &log, Logger::Stream &err)
Logger *
CreateLogger(Level level, Logger::Stream &log, Logger::Stream &err)
{
if (Logger::g_logger == nullptr)
Logger::g_logger = new Logger(level, log, err);
return Logger::g_logger;
if (globalLogger == nullptr)
{
globalLogger = std::make_unique<Logger>(level, log, err);
std::atexit(DestroyLogger);
}
return globalLogger.get();
}

void DestroyLogger()
void
DestroyLogger()
{
delete Logger::g_logger;
globalLogger = nullptr;
}

Logger::Logger() :
m_log(std::clog),
m_err(std::cerr)
Logger(Level::Info, std::clog, std::cerr)
{
}

Logger::Logger(Level level, Stream &log, Stream &err) :
m_log(log),
m_err(err),
m_level(level)
{
}

void
Logger::setLevel(Level level)
{
m_level = level;
}

void Logger::vlog(Level level, fmt::string_view format, fmt::format_args args) const
void
Logger::vlog(Level level, std::string_view format, fmt::format_args args) const
{
#ifdef _MSC_VER
if (level == Level::Debug && IsDebuggerPresent())
Expand All @@ -61,7 +87,7 @@ void Logger::vlog(Level level, fmt::string_view format, fmt::format_args args) c
#endif

auto &stream = (level <= Level::Warning || level == Level::Debug) ? m_err : m_log;
fmt::vprint(stream, format, args);
fmt::print(stream, fmt::vformat(format, args));
}

} // end namespace celestia::util
Loading