mirror of
https://github.com/OpenSpace/OpenSpace.git
synced 2026-02-26 14:58:51 -06:00
Removing some more global state
This commit is contained in:
@@ -73,7 +73,7 @@ public:
|
||||
* Saves all current settings, similar to saveCurrentSettingsToProfile() except the
|
||||
* output is a string without writing to a file.
|
||||
*/
|
||||
std::string saveCurrentSettingsToProfile_string();
|
||||
//std::string saveCurrentSettingsToProfile_string();
|
||||
|
||||
/**
|
||||
* Reads in a .profile file, converts it to scene/asset equivalent syntax, and
|
||||
@@ -97,26 +97,25 @@ protected:
|
||||
std::string _profileBaseDirectory = "${ASSETS}";
|
||||
|
||||
private:
|
||||
ProfileData profile;
|
||||
|
||||
struct AllAssetDetails {
|
||||
std::vector<AssetEvent> base;
|
||||
std::vector<AssetEvent> changed;
|
||||
};
|
||||
virtual bool usingProfile() const;
|
||||
virtual std::string initialProfile() const;
|
||||
virtual std::string profileBaseDirectory() const;
|
||||
virtual std::vector<AssetEvent> assetEvents() const;
|
||||
ProfileStruct collateBaseWithChanges();
|
||||
ProfileData collateBaseWithChanges();
|
||||
|
||||
std::vector<AssetEvent> modifyAssetsToReflectChanges(ProfileStruct& ps);
|
||||
void parseAssetFileLines(std::vector<AssetEvent>& results, ProfileStruct& ps);
|
||||
std::vector<AssetEvent> modifyAssetsToReflectChanges(ProfileData& ps);
|
||||
void parseAssetFileLines(std::vector<AssetEvent>& results, ProfileData& ps);
|
||||
|
||||
void modifyPropertiesToReflectChanges(ProfileStruct& ps);
|
||||
void modifyPropertiesToReflectChanges(ProfileData& ps);
|
||||
virtual std::vector<openspace::properties::Property*> changedProperties();
|
||||
std::string getFullPropertyPath(openspace::properties::Property* prop);
|
||||
virtual std::vector<std::string> changedPropertiesFormatted();
|
||||
virtual std::string currentTimeUTC() const;
|
||||
virtual interaction::NavigationHandler::NavigationState currentCameraState() const;
|
||||
void addCurrentCameraToProfileFile(ProfileStruct& ps) const;
|
||||
};
|
||||
|
||||
} // namespace openspace
|
||||
|
||||
@@ -25,25 +25,14 @@
|
||||
#ifndef __OPENSPACE_CORE___PROFILEFILE___H__
|
||||
#define __OPENSPACE_CORE___PROFILEFILE___H__
|
||||
|
||||
#include <string>
|
||||
#include <istream>
|
||||
#include <fstream>
|
||||
#include <functional>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
#include <variant>
|
||||
#include <vector>
|
||||
|
||||
#include <ghoul/systemcapabilities/version.h>
|
||||
|
||||
namespace ghoul { class Dictionary; }
|
||||
namespace ghoul::opengl { class ProgramObject; }
|
||||
|
||||
namespace openspace {
|
||||
|
||||
namespace documentation { struct Documentation; }
|
||||
namespace scripting { struct LuaLibrary; }
|
||||
|
||||
struct ProfileStruct {
|
||||
struct ProfileData {
|
||||
// Version
|
||||
struct Version {
|
||||
int major = 0;
|
||||
@@ -128,12 +117,10 @@ struct ProfileStruct {
|
||||
std::vector<std::string> markNodes;
|
||||
};
|
||||
|
||||
std::string serialize(const ProfileStruct& ps);
|
||||
ProfileStruct deserialize(const std::vector<std::string>& content);
|
||||
std::string serialize(const ProfileData& ps);
|
||||
ProfileData deserialize(const std::vector<std::string>& content);
|
||||
|
||||
std::string convertToSceneFile(const ProfileStruct& ps);
|
||||
|
||||
ProfileStruct readFromFile(const std::string& filename);
|
||||
std::string convertToSceneFile(const ProfileData& ps);
|
||||
|
||||
} // namespace openspace
|
||||
|
||||
|
||||
@@ -312,6 +312,8 @@ void OpenSpaceEngine::initialize() {
|
||||
);
|
||||
}
|
||||
else {
|
||||
|
||||
|
||||
global::profile.convertToSceneFile(inputProfile, outputAsset);
|
||||
|
||||
// Set asset name to that of the profile because a new scene file will be
|
||||
|
||||
@@ -102,15 +102,15 @@ namespace {
|
||||
}
|
||||
}
|
||||
|
||||
void addAssetsToProfileFile(ProfileStruct& ps,
|
||||
void addAssetsToProfileFile(ProfileData& ps,
|
||||
const std::vector<Profile::AssetEvent>& allAssets)
|
||||
{
|
||||
ps.assets.clear();
|
||||
for (Profile::AssetEvent a : allAssets) {
|
||||
if (a.eventType != Profile::AssetEventType::Ignore) {
|
||||
ProfileStruct::Asset asset;
|
||||
ProfileData::Asset asset;
|
||||
asset.path = a.name;
|
||||
asset.type = ProfileStruct::Asset::Type::Require;
|
||||
asset.type = ProfileData::Asset::Type::Require;
|
||||
ps.assets.push_back(std::move(asset));
|
||||
}
|
||||
}
|
||||
@@ -143,10 +143,31 @@ namespace {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ProfileData readFromFile(const std::string& filename) {
|
||||
std::ifstream inFile;
|
||||
try {
|
||||
inFile.open(filename, std::ifstream::in);
|
||||
}
|
||||
catch (const std::ifstream::failure& e) {
|
||||
throw ghoul::RuntimeError(fmt::format(
|
||||
"Exception opening profile file for read: {} ({})", filename, e.what())
|
||||
);
|
||||
}
|
||||
|
||||
std::vector<std::string> content;
|
||||
std::string line;
|
||||
while (std::getline(inFile, line)) {
|
||||
content.push_back(std::move(line));
|
||||
}
|
||||
|
||||
return deserialize(content);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
void Profile::saveCurrentSettingsToProfile(const std::string& filename) {
|
||||
ProfileStruct ps = collateBaseWithChanges();
|
||||
ProfileData ps = collateBaseWithChanges();
|
||||
|
||||
if (filename.find('/') != std::string::npos) {
|
||||
LERROR("Profile filename must not contain path (/) elements");
|
||||
@@ -192,15 +213,6 @@ void Profile::saveCurrentSettingsToProfile(const std::string& filename) {
|
||||
outFile.close();
|
||||
}
|
||||
|
||||
std::string Profile::saveCurrentSettingsToProfile_string() {
|
||||
ProfileStruct ps = collateBaseWithChanges();
|
||||
return serialize(ps);
|
||||
}
|
||||
|
||||
bool Profile::usingProfile() const {
|
||||
return global::configuration.usingProfile;
|
||||
}
|
||||
|
||||
std::string Profile::initialProfile() const {
|
||||
return global::configuration.profile;
|
||||
}
|
||||
@@ -213,33 +225,47 @@ std::string Profile::profileBaseDirectory() const {
|
||||
return _profileBaseDirectory;
|
||||
}
|
||||
|
||||
ProfileStruct Profile::collateBaseWithChanges() {
|
||||
if (!usingProfile()) {
|
||||
std::string errorMessage = "Program was not started using a profile, "
|
||||
"so cannot use this save-current-settings feature";
|
||||
LERROR(errorMessage);
|
||||
}
|
||||
ProfileData Profile::collateBaseWithChanges() {
|
||||
std::string initProfile = initialProfile();
|
||||
std::string inputProfilePath = absPath(_profileBaseDirectory) + "/" + initProfile
|
||||
+ ".profile";
|
||||
ProfileStruct ps = readFromFile(inputProfilePath);
|
||||
ps.version = ProfileStruct::Version{};
|
||||
ProfileData ps = readFromFile(inputProfilePath);
|
||||
ps.version = ProfileData::Version{};
|
||||
|
||||
std::vector<AssetEvent> ass = modifyAssetsToReflectChanges(ps);
|
||||
addAssetsToProfileFile(ps, ass);
|
||||
modifyPropertiesToReflectChanges(ps);
|
||||
|
||||
// add current time to profile file
|
||||
ProfileStruct::Time time;
|
||||
ProfileData::Time time;
|
||||
time.time = currentTimeUTC();
|
||||
time.type = ProfileStruct::Time::Type::Absolute;
|
||||
time.type = ProfileData::Time::Type::Absolute;
|
||||
ps.time = std::move(time);
|
||||
|
||||
addCurrentCameraToProfileFile(ps);
|
||||
// Camera
|
||||
interaction::NavigationHandler::NavigationState nav = currentCameraState();
|
||||
|
||||
ProfileData::CameraNavState camera;
|
||||
camera.anchor = nav.anchor;
|
||||
camera.aim = nav.aim;
|
||||
camera.referenceFrame = nav.referenceFrame;
|
||||
camera.position = fmt::format(
|
||||
"{},{},{}",
|
||||
nav.position.x, nav.position.y, nav.position.z
|
||||
);
|
||||
if (nav.up.has_value()) {
|
||||
camera.up = fmt::format(
|
||||
"{},{},{}",
|
||||
nav.up->x, nav.up->y, nav.up->z
|
||||
);
|
||||
}
|
||||
camera.yaw = std::to_string(nav.yaw);
|
||||
camera.pitch = std::to_string(nav.pitch);
|
||||
ps.camera = std::move(camera);
|
||||
return ps;
|
||||
}
|
||||
|
||||
std::vector<Profile::AssetEvent> Profile::modifyAssetsToReflectChanges(ProfileStruct& ps) {
|
||||
std::vector<Profile::AssetEvent> Profile::modifyAssetsToReflectChanges(ProfileData& ps) {
|
||||
std::vector<AssetEvent> a;
|
||||
parseAssetFileLines(a, ps);
|
||||
AllAssetDetails assetDetails;
|
||||
@@ -260,14 +286,14 @@ std::vector<Profile::AssetEvent> Profile::modifyAssetsToReflectChanges(ProfileSt
|
||||
return assetDetails.base;
|
||||
}
|
||||
|
||||
void Profile::parseAssetFileLines(std::vector<AssetEvent>& results, ProfileStruct& ps) {
|
||||
for (ProfileStruct::Asset& a : ps.assets) {
|
||||
void Profile::parseAssetFileLines(std::vector<AssetEvent>& results, ProfileData& ps) {
|
||||
for (ProfileData::Asset& a : ps.assets) {
|
||||
AssetEvent assetEvent;
|
||||
assetEvent.name = a.path;
|
||||
assetEvent.eventType = [](ProfileStruct::Asset::Type type) {
|
||||
assetEvent.eventType = [](ProfileData::Asset::Type type) {
|
||||
switch (type) {
|
||||
case ProfileStruct::Asset::Type::Request: return AssetEventType::Request;
|
||||
case ProfileStruct::Asset::Type::Require: return AssetEventType::Require;
|
||||
case ProfileData::Asset::Type::Request: return AssetEventType::Request;
|
||||
case ProfileData::Asset::Type::Require: return AssetEventType::Require;
|
||||
default: throw ghoul::MissingCaseException();
|
||||
}
|
||||
}(a.type);
|
||||
@@ -275,32 +301,19 @@ void Profile::parseAssetFileLines(std::vector<AssetEvent>& results, ProfileStruc
|
||||
}
|
||||
}
|
||||
|
||||
void Profile::modifyPropertiesToReflectChanges(ProfileStruct& ps) {
|
||||
void Profile::modifyPropertiesToReflectChanges(ProfileData& ps) {
|
||||
std::vector<properties::Property*> changedProps = changedProperties();
|
||||
std::vector<std::string> formattedLines;
|
||||
|
||||
for (properties::Property* prop : changedProps) {
|
||||
ProfileStruct::Property p;
|
||||
p.setType = ProfileStruct::Property::SetType::SetPropertyValueSingle;
|
||||
ProfileData::Property p;
|
||||
p.setType = ProfileData::Property::SetType::SetPropertyValueSingle;
|
||||
p.name = getFullPropertyPath(prop);
|
||||
p.value = prop->getStringValue();
|
||||
ps.properties.push_back(std::move(p));
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<std::string> Profile::changedPropertiesFormatted() {
|
||||
std::vector<properties::Property*> changedProps = changedProperties();
|
||||
std::vector<std::string> formattedLines;
|
||||
|
||||
for (properties::Property* prop : changedProps) {
|
||||
std::string newLine = "setPropertyValueSingle\t";
|
||||
newLine += getFullPropertyPath(prop) + "\t";
|
||||
newLine += prop->getStringValue();
|
||||
formattedLines.push_back(newLine);
|
||||
}
|
||||
return formattedLines;
|
||||
}
|
||||
|
||||
std::string Profile::getFullPropertyPath(properties::Property* prop) {
|
||||
return recurseForFullName(prop->owner()) + prop->identifier();
|
||||
}
|
||||
@@ -308,8 +321,8 @@ std::string Profile::getFullPropertyPath(properties::Property* prop) {
|
||||
std::vector<properties::Property*> Profile::changedProperties() {
|
||||
ZoneScoped
|
||||
|
||||
std::vector<SceneGraphNode*> nodes
|
||||
= global::renderEngine.scene()->allSceneGraphNodes();
|
||||
std::vector<SceneGraphNode*> nodes =
|
||||
global::renderEngine.scene()->allSceneGraphNodes();
|
||||
std::vector<properties::Property*> changedProps;
|
||||
|
||||
for (SceneGraphNode* n : nodes) {
|
||||
@@ -326,34 +339,12 @@ interaction::NavigationHandler::NavigationState Profile::currentCameraState() co
|
||||
return global::navigationHandler.navigationState();
|
||||
}
|
||||
|
||||
void Profile::addCurrentCameraToProfileFile(ProfileStruct& ps) const {
|
||||
interaction::NavigationHandler::NavigationState nav = currentCameraState();
|
||||
|
||||
ProfileStruct::CameraNavState camera;
|
||||
camera.anchor = nav.anchor;
|
||||
camera.aim = nav.aim;
|
||||
camera.referenceFrame = nav.referenceFrame;
|
||||
camera.position = fmt::format(
|
||||
"{},{},{}",
|
||||
nav.position.x, nav.position.y, nav.position.z
|
||||
);
|
||||
if (nav.up.has_value()) {
|
||||
camera.up = fmt::format(
|
||||
"{},{},{}",
|
||||
nav.up->x, nav.up->y, nav.up->z
|
||||
);
|
||||
}
|
||||
camera.yaw = nav.yaw;
|
||||
camera.pitch = nav.pitch;
|
||||
ps.camera = std::move(camera);
|
||||
}
|
||||
|
||||
void Profile::convertToSceneFile(const std::string& inProfilePath,
|
||||
const std::string& outFilePath)
|
||||
{
|
||||
ZoneScoped
|
||||
|
||||
ProfileStruct ps = readFromFile(inProfilePath);
|
||||
ProfileData ps = readFromFile(inProfilePath);
|
||||
|
||||
std::ofstream outFile;
|
||||
try {
|
||||
|
||||
@@ -39,6 +39,13 @@ int saveCurrentSettingsToProfile(lua_State* L) {
|
||||
if (saveFilePath.empty()) {
|
||||
return luaL_error(L, "save filepath string is empty");
|
||||
}
|
||||
if (!global::configuration.usingProfile) {
|
||||
return luaL_error(
|
||||
L,
|
||||
"Program was not started with a profile, so cannot use this "
|
||||
"save-current-settings feature"
|
||||
);
|
||||
}
|
||||
global::profile.saveCurrentSettingsToProfile(
|
||||
saveFilePath
|
||||
);
|
||||
|
||||
@@ -24,17 +24,11 @@
|
||||
|
||||
#include <openspace/scene/profilefile.h>
|
||||
|
||||
#include <openspace/scripting/lualibrary.h>
|
||||
#include <ghoul/filesystem/file.h>
|
||||
#include <ghoul/filesystem/filesystem.h>
|
||||
#include <ghoul/logging/logmanager.h>
|
||||
#include <ghoul/misc/assert.h>
|
||||
#include <ghoul/misc/exception.h>
|
||||
#include <ghoul/misc/misc.h>
|
||||
#include <ghoul/misc/profiling.h>
|
||||
#include <ghoul/opengl/programobject.h>
|
||||
#include <ghoul/fmt.h>
|
||||
#include <string>
|
||||
#include <iomanip>
|
||||
#include <cctype>
|
||||
|
||||
namespace openspace {
|
||||
|
||||
@@ -94,7 +88,7 @@ namespace {
|
||||
throw ProfileError(lineNumber, fmt::format("Invalid section header: {}", line));
|
||||
}
|
||||
|
||||
void parseVersion(ProfileStruct& ps, const std::string& line, int lineNumber) {
|
||||
void parseVersion(ProfileData& ps, const std::string& line, int lineNumber) {
|
||||
std::vector<std::string> parts = ghoul::tokenizeString(line, '.');
|
||||
if (parts.empty() || parts.size() > 3) {
|
||||
throw ProfileError(
|
||||
@@ -103,7 +97,7 @@ namespace {
|
||||
);
|
||||
}
|
||||
|
||||
ProfileStruct::Version version;
|
||||
ProfileData::Version version;
|
||||
|
||||
version.major = std::stoi(parts[0]);
|
||||
if (parts.size() > 1) {
|
||||
@@ -115,7 +109,7 @@ namespace {
|
||||
ps.version = std::move(version);
|
||||
}
|
||||
|
||||
void parseModule(ProfileStruct& ps, const std::string& line, int lineNumber) {
|
||||
void parseModule(ProfileData& ps, const std::string& line, int lineNumber) {
|
||||
std::vector<std::string> fields = ghoul::tokenizeString(line, '\t');
|
||||
if (fields.size() != 3) {
|
||||
throw ProfileError(
|
||||
@@ -123,14 +117,14 @@ namespace {
|
||||
fmt::format("Expected 3 fields in a Module entry, got {}", fields.size())
|
||||
);
|
||||
}
|
||||
ProfileStruct::Module m;
|
||||
ProfileData::Module m;
|
||||
m.name = fields[0];
|
||||
m.loadedInstruction = fields[1];
|
||||
m.notLoadedInstruction = fields[2];
|
||||
ps.modules.push_back(std::move(m));
|
||||
}
|
||||
|
||||
void parseAsset(ProfileStruct& ps, const std::string& line, int lineNumber) {
|
||||
void parseAsset(ProfileData& ps, const std::string& line, int lineNumber) {
|
||||
std::vector<std::string> fields = ghoul::tokenizeString(line, '\t');
|
||||
if (fields.size() != 3) {
|
||||
throw ProfileError(
|
||||
@@ -139,14 +133,14 @@ namespace {
|
||||
);
|
||||
}
|
||||
|
||||
ProfileStruct::Asset a;
|
||||
ProfileData::Asset a;
|
||||
a.path = fields[0];
|
||||
a.type = [&](const std::string& type) -> ProfileStruct::Asset::Type {
|
||||
a.type = [&](const std::string& type) -> ProfileData::Asset::Type {
|
||||
if (type == "require") {
|
||||
return ProfileStruct::Asset::Type::Require;
|
||||
return ProfileData::Asset::Type::Require;
|
||||
}
|
||||
if (type == "request") {
|
||||
return ProfileStruct::Asset::Type::Request;
|
||||
return ProfileData::Asset::Type::Request;
|
||||
}
|
||||
throw ProfileError(
|
||||
lineNumber,
|
||||
@@ -157,7 +151,7 @@ namespace {
|
||||
ps.assets.push_back(std::move(a));
|
||||
}
|
||||
|
||||
void parseProperty(ProfileStruct& ps, const std::string& line, int lineNumber) {
|
||||
void parseProperty(ProfileData& ps, const std::string& line, int lineNumber) {
|
||||
std::vector<std::string> fields = ghoul::tokenizeString(line, '\t');
|
||||
if (fields.size() != 3) {
|
||||
throw ProfileError(
|
||||
@@ -165,13 +159,13 @@ namespace {
|
||||
fmt::format("Expected 3 fields in Property entry, got {}", fields.size())
|
||||
);
|
||||
}
|
||||
ProfileStruct::Property p;
|
||||
p.setType = [&](const std::string& type) -> ProfileStruct::Property::SetType {
|
||||
ProfileData::Property p;
|
||||
p.setType = [&](const std::string& type) -> ProfileData::Property::SetType {
|
||||
if (type == "setPropertyValue") {
|
||||
return ProfileStruct::Property::SetType::SetPropertyValue;
|
||||
return ProfileData::Property::SetType::SetPropertyValue;
|
||||
}
|
||||
if (type == "setPropertyValueSingle") {
|
||||
return ProfileStruct::Property::SetType::SetPropertyValueSingle;
|
||||
return ProfileData::Property::SetType::SetPropertyValueSingle;
|
||||
}
|
||||
throw ProfileError(
|
||||
lineNumber,
|
||||
@@ -187,7 +181,7 @@ namespace {
|
||||
ps.properties.push_back(std::move(p));
|
||||
}
|
||||
|
||||
void parseKeybinding(ProfileStruct& ps, const std::string& line, int lineNumber) {
|
||||
void parseKeybinding(ProfileData& ps, const std::string& line, int lineNumber) {
|
||||
std::vector<std::string> fields = ghoul::tokenizeString(line, '\t');
|
||||
if (fields.size() != 6) {
|
||||
throw ProfileError(
|
||||
@@ -195,7 +189,7 @@ namespace {
|
||||
fmt::format("Expected 6 fields in Keybinding entry, got {}", fields.size())
|
||||
);
|
||||
}
|
||||
ProfileStruct::Keybinding kb;
|
||||
ProfileData::Keybinding kb;
|
||||
kb.key = fields[0];
|
||||
kb.documentation = fields[1];
|
||||
kb.name = fields[2];
|
||||
@@ -216,7 +210,7 @@ namespace {
|
||||
ps.keybindings.push_back(std::move(kb));
|
||||
}
|
||||
|
||||
void parseTime(ProfileStruct& ps, const std::string& line, int lineNumber) {
|
||||
void parseTime(ProfileData& ps, const std::string& line, int lineNumber) {
|
||||
std::vector<std::string> fields = ghoul::tokenizeString(line, '\t');
|
||||
if (fields.size() != 2) {
|
||||
throw ProfileError(
|
||||
@@ -224,13 +218,13 @@ namespace {
|
||||
fmt::format("Expected 2 fields in Time entry, got {}", fields.size())
|
||||
);
|
||||
}
|
||||
ProfileStruct::Time time;
|
||||
time.type = [&](const std::string& type) -> ProfileStruct::Time::Type {
|
||||
ProfileData::Time time;
|
||||
time.type = [&](const std::string& type) -> ProfileData::Time::Type {
|
||||
if (type == "absolute") {
|
||||
return ProfileStruct::Time::Type::Absolute;
|
||||
return ProfileData::Time::Type::Absolute;
|
||||
}
|
||||
if (type == "relative") {
|
||||
return ProfileStruct::Time::Type::Relative;
|
||||
return ProfileData::Time::Type::Relative;
|
||||
}
|
||||
throw ProfileError(
|
||||
lineNumber,
|
||||
@@ -241,15 +235,15 @@ namespace {
|
||||
ps.time = std::move(time);
|
||||
}
|
||||
|
||||
void parseCamera(ProfileStruct& ps, const std::string& line, int lineNumber) {
|
||||
void parseCamera(ProfileData& ps, const std::string& line, int lineNumber) {
|
||||
std::vector<std::string> fields = ghoul::tokenizeString(line, '\t');
|
||||
if (fields.empty()) {
|
||||
throw ProfileError(lineNumber, "No values specified for Camera location");
|
||||
}
|
||||
ps.camera = [&](const std::string& type) ->
|
||||
std::variant<ProfileStruct::CameraNavState, ProfileStruct::CameraGoToGeo>
|
||||
std::variant<ProfileData::CameraNavState, ProfileData::CameraGoToGeo>
|
||||
{
|
||||
if (type == ProfileStruct::CameraNavState::Type) {
|
||||
if (type == ProfileData::CameraNavState::Type) {
|
||||
if (fields.size() != 8) {
|
||||
throw ProfileError(
|
||||
lineNumber,
|
||||
@@ -259,7 +253,7 @@ namespace {
|
||||
);
|
||||
}
|
||||
|
||||
ProfileStruct::CameraNavState camera;
|
||||
ProfileData::CameraNavState camera;
|
||||
camera.anchor = fields[1];
|
||||
camera.aim = fields[2];
|
||||
camera.referenceFrame = fields[3];
|
||||
@@ -269,7 +263,7 @@ namespace {
|
||||
camera.pitch = fields[7];
|
||||
return camera;
|
||||
}
|
||||
if (type == ProfileStruct::CameraGoToGeo::Type) {
|
||||
if (type == ProfileData::CameraGoToGeo::Type) {
|
||||
if (fields.size() != 5) {
|
||||
throw ProfileError(
|
||||
lineNumber,
|
||||
@@ -279,7 +273,7 @@ namespace {
|
||||
);
|
||||
}
|
||||
|
||||
ProfileStruct::CameraGoToGeo camera;
|
||||
ProfileData::CameraGoToGeo camera;
|
||||
camera.anchor = fields[1];
|
||||
camera.latitude = std::stod(fields[2]);
|
||||
camera.longitude = std::stod(fields[3]);
|
||||
@@ -291,46 +285,53 @@ namespace {
|
||||
throw ProfileError(
|
||||
lineNumber,
|
||||
fmt::format(
|
||||
"Expected 'setNavigationState' or 'goToGeo' for the type, got {}", fields[0]
|
||||
"Expected 'setNavigationState' or 'goToGeo' for the type, got {}",
|
||||
fields[0]
|
||||
)
|
||||
);
|
||||
}(fields[0]);
|
||||
}
|
||||
|
||||
void parseMarkNodes(ProfileStruct& ps, const std::string& line, int) {
|
||||
void parseMarkNodes(ProfileData& ps, const std::string& line, int) {
|
||||
ps.markNodes.push_back(line);
|
||||
}
|
||||
} // namespace
|
||||
|
||||
std::string serialize(const ProfileStruct& ps) {
|
||||
std::string serialize(const ProfileData& ps) {
|
||||
std::string output;
|
||||
output += fmt::format("{}\n", headerVersion);
|
||||
output += fmt::format("{}.{}.{}\n", ps.version.major, ps.version.minor, ps.version.patch);
|
||||
output += fmt::format(
|
||||
"{}.{}.{}\n",
|
||||
ps.version.major, ps.version.minor, ps.version.patch
|
||||
);
|
||||
|
||||
output += fmt::format("\n{}\n", headerModule);
|
||||
for (const ProfileStruct::Module& m : ps.modules) {
|
||||
output += fmt::format("{}\t{}\t{}\n", m.name, m.loadedInstruction, m.notLoadedInstruction);
|
||||
for (const ProfileData::Module& m : ps.modules) {
|
||||
output += fmt::format(
|
||||
"{}\t{}\t{}\n",
|
||||
m.name, m.loadedInstruction, m.notLoadedInstruction
|
||||
);
|
||||
}
|
||||
|
||||
output += fmt::format("\n{}\n", headerAsset);
|
||||
for (const ProfileStruct::Asset& a : ps.assets) {
|
||||
const std::string type = [](ProfileStruct::Asset::Type t) {
|
||||
for (const ProfileData::Asset& a : ps.assets) {
|
||||
const std::string type = [](ProfileData::Asset::Type t) {
|
||||
switch (t) {
|
||||
case ProfileStruct::Asset::Type::Require: return "require";
|
||||
case ProfileStruct::Asset::Type::Request: return "request";
|
||||
default: throw ghoul::MissingCaseException();
|
||||
case ProfileData::Asset::Type::Require: return "require";
|
||||
case ProfileData::Asset::Type::Request: return "request";
|
||||
default: throw ghoul::MissingCaseException();
|
||||
}
|
||||
}(a.type);
|
||||
output += fmt::format("{}\t{}\n", a.path, type);
|
||||
}
|
||||
|
||||
output += fmt::format("\n{}\n", headerProperty);
|
||||
for (const ProfileStruct::Property& p : ps.properties) {
|
||||
const std::string type = [](ProfileStruct::Property::SetType t) {
|
||||
for (const ProfileData::Property& p : ps.properties) {
|
||||
const std::string type = [](ProfileData::Property::SetType t) {
|
||||
switch (t) {
|
||||
case ProfileStruct::Property::SetType::SetPropertyValue:
|
||||
case ProfileData::Property::SetType::SetPropertyValue:
|
||||
return "setPropertyValue";
|
||||
case ProfileStruct::Property::SetType::SetPropertyValueSingle:
|
||||
case ProfileData::Property::SetType::SetPropertyValueSingle:
|
||||
return "setPropertyValueSingle";
|
||||
default:
|
||||
throw ghoul::MissingCaseException();
|
||||
@@ -340,7 +341,7 @@ std::string serialize(const ProfileStruct& ps) {
|
||||
}
|
||||
|
||||
output += fmt::format("\n{}\n", headerKeybinding);
|
||||
for (const ProfileStruct::Keybinding& k : ps.keybindings) {
|
||||
for (const ProfileData::Keybinding& k : ps.keybindings) {
|
||||
const std::string local = k.isLocal ? "true" : "false";
|
||||
output += fmt::format(
|
||||
"{}\t{}\t{}\t{}\t{}\t{}\n",
|
||||
@@ -350,11 +351,11 @@ std::string serialize(const ProfileStruct& ps) {
|
||||
|
||||
output += fmt::format("\n{}\n", headerTime);
|
||||
{
|
||||
const std::string type = [](ProfileStruct::Time::Type t) {
|
||||
const std::string type = [](ProfileData::Time::Type t) {
|
||||
switch (t) {
|
||||
case ProfileStruct::Time::Type::Absolute: return "absolute";
|
||||
case ProfileStruct::Time::Type::Relative: return "relative";
|
||||
default: throw ghoul::MissingCaseException();
|
||||
case ProfileData::Time::Type::Absolute: return "absolute";
|
||||
case ProfileData::Time::Type::Relative: return "relative";
|
||||
default: throw ghoul::MissingCaseException();
|
||||
}
|
||||
}(ps.time.type);
|
||||
output += fmt::format("{}\t{}\n", type, ps.time.time);
|
||||
@@ -363,15 +364,15 @@ std::string serialize(const ProfileStruct& ps) {
|
||||
output += fmt::format("\n{}\n", headerCamera);
|
||||
output += std::visit(
|
||||
overloaded {
|
||||
[](const ProfileStruct::CameraNavState& camera) {
|
||||
[](const ProfileData::CameraNavState& camera) {
|
||||
return fmt::format(
|
||||
"{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}\n",
|
||||
ProfileStruct::CameraNavState::Type,
|
||||
ProfileData::CameraNavState::Type,
|
||||
camera.anchor, camera.aim, camera.referenceFrame, camera.position,
|
||||
camera.up, camera.yaw, camera.pitch
|
||||
);
|
||||
},
|
||||
[](const ProfileStruct::CameraGoToGeo& camera) {
|
||||
[](const ProfileData::CameraGoToGeo& camera) {
|
||||
std::string altitude;
|
||||
if (camera.altitude.has_value()) {
|
||||
altitude = std::to_string(*camera.altitude);
|
||||
@@ -379,7 +380,7 @@ std::string serialize(const ProfileStruct& ps) {
|
||||
|
||||
return fmt::format(
|
||||
"{}\t{}\t{}\t{}\t{}\n",
|
||||
ProfileStruct::CameraGoToGeo::Type,
|
||||
ProfileData::CameraGoToGeo::Type,
|
||||
camera.anchor, camera.latitude, camera.longitude, altitude
|
||||
);
|
||||
}
|
||||
@@ -395,19 +396,8 @@ std::string serialize(const ProfileStruct& ps) {
|
||||
return output;
|
||||
}
|
||||
|
||||
ProfileStruct deserialize(const std::vector<std::string>& content) {
|
||||
ProfileStruct result;
|
||||
|
||||
//int lineNum = 1;
|
||||
//std::ifstream inFile;
|
||||
//try {
|
||||
// inFile.open(filename, std::ifstream::in);
|
||||
//}
|
||||
//catch (const std::ifstream::failure& e) {
|
||||
// throw ProfileError(fmt::format(
|
||||
// "Exception opening profile file for read: {} ({})", filename, e.what())
|
||||
// );
|
||||
//}
|
||||
ProfileData deserialize(const std::vector<std::string>& content) {
|
||||
ProfileData result;
|
||||
|
||||
Section currentSection = Section::None;
|
||||
for (int lineNum = 1; lineNum <= static_cast<int>(content.size()); ++lineNum) {
|
||||
@@ -453,13 +443,13 @@ ProfileStruct deserialize(const std::vector<std::string>& content) {
|
||||
return result;
|
||||
}
|
||||
|
||||
std::string convertToSceneFile(const ProfileStruct& ps) {
|
||||
std::string convertToSceneFile(const ProfileData& ps) {
|
||||
ZoneScoped
|
||||
|
||||
std::string output;
|
||||
|
||||
// Modules
|
||||
for (const ProfileStruct::Module& m : ps.modules) {
|
||||
for (const ProfileData::Module& m : ps.modules) {
|
||||
output += fmt::format(
|
||||
"if openspace.modules.isLoaded(\"{}\") then {} else {} end\n",
|
||||
m.name, m.loadedInstruction, m.notLoadedInstruction
|
||||
@@ -467,14 +457,14 @@ std::string convertToSceneFile(const ProfileStruct& ps) {
|
||||
}
|
||||
|
||||
// Assets
|
||||
for (const ProfileStruct::Asset& a : ps.assets) {
|
||||
for (const ProfileData::Asset& a : ps.assets) {
|
||||
if (!a.name.empty()) {
|
||||
output += fmt::format("local {} = ", a.name);
|
||||
}
|
||||
std::string type = [](ProfileStruct::Asset::Type t) {
|
||||
std::string type = [](ProfileData::Asset::Type t) {
|
||||
switch (t) {
|
||||
case ProfileStruct::Asset::Type::Request: return "request";
|
||||
case ProfileStruct::Asset::Type::Require: return "require";
|
||||
case ProfileData::Asset::Type::Request: return "request";
|
||||
case ProfileData::Asset::Type::Require: return "require";
|
||||
default: throw ghoul::MissingCaseException();
|
||||
}
|
||||
}(a.type);
|
||||
@@ -484,7 +474,7 @@ std::string convertToSceneFile(const ProfileStruct& ps) {
|
||||
|
||||
output += "asset.onInitialize(function()\n";
|
||||
// Keybindings
|
||||
for (const ProfileStruct::Keybinding& k : ps.keybindings) {
|
||||
for (const ProfileData::Keybinding& k : ps.keybindings) {
|
||||
const std::string name = k.name.empty() ? k.key : k.name;
|
||||
output += fmt::format(
|
||||
k.isLocal ?
|
||||
@@ -496,10 +486,10 @@ std::string convertToSceneFile(const ProfileStruct& ps) {
|
||||
|
||||
// Time
|
||||
switch (ps.time.type) {
|
||||
case ProfileStruct::Time::Type::Absolute:
|
||||
case ProfileData::Time::Type::Absolute:
|
||||
output += fmt::format("openspace.time.setTime(\"{}\")\n", ps.time.time);
|
||||
break;
|
||||
case ProfileStruct::Time::Type::Relative:
|
||||
case ProfileData::Time::Type::Relative:
|
||||
output += "local now = openspace.time.currentWallTime();\n";
|
||||
output += fmt::format(
|
||||
"local prev = openspace.time.advancedTime(now, \"{}\");\n", ps.time.time
|
||||
@@ -519,15 +509,15 @@ std::string convertToSceneFile(const ProfileStruct& ps) {
|
||||
}
|
||||
|
||||
// Properties
|
||||
for (const ProfileStruct::Property& p : ps.properties) {
|
||||
for (const ProfileData::Property& p : ps.properties) {
|
||||
switch (p.setType) {
|
||||
case ProfileStruct::Property::SetType::SetPropertyValue:
|
||||
case ProfileData::Property::SetType::SetPropertyValue:
|
||||
output += fmt::format(
|
||||
"openspace.setPropertyValue(\"{}\", {});\n",
|
||||
p.name, p.value
|
||||
);
|
||||
break;
|
||||
case ProfileStruct::Property::SetType::SetPropertyValueSingle:
|
||||
case ProfileData::Property::SetType::SetPropertyValueSingle:
|
||||
output += fmt::format(
|
||||
"openspace.setPropertyValueSingle(\"{}\", {});\n",
|
||||
p.name, p.value
|
||||
@@ -541,9 +531,9 @@ std::string convertToSceneFile(const ProfileStruct& ps) {
|
||||
// Camera
|
||||
output += std::visit(
|
||||
overloaded {
|
||||
[](const ProfileStruct::CameraNavState& camera) {
|
||||
[](const ProfileData::CameraNavState& camera) {
|
||||
std::string result;
|
||||
result += " openspace.navigation.setNavigationState({";
|
||||
result += "openspace.navigation.setNavigationState({";
|
||||
result += fmt::format("Anchor = {}, ", camera.anchor);
|
||||
if (!camera.aim.empty()) {
|
||||
result += fmt::format("Aim = {}, ", camera.aim);
|
||||
@@ -564,7 +554,7 @@ std::string convertToSceneFile(const ProfileStruct& ps) {
|
||||
result += "})\n";
|
||||
return result;
|
||||
},
|
||||
[](const ProfileStruct::CameraGoToGeo& camera) {
|
||||
[](const ProfileData::CameraGoToGeo& camera) {
|
||||
if (camera.altitude.has_value()) {
|
||||
return fmt::format(
|
||||
"openspace.globebrowsing.goToGeo({}, {}, {}, {});\n",
|
||||
@@ -586,24 +576,4 @@ std::string convertToSceneFile(const ProfileStruct& ps) {
|
||||
return output;
|
||||
}
|
||||
|
||||
ProfileStruct readFromFile(const std::string& filename) {
|
||||
std::ifstream inFile;
|
||||
try {
|
||||
inFile.open(filename, std::ifstream::in);
|
||||
}
|
||||
catch (const std::ifstream::failure& e) {
|
||||
throw ProfileError(fmt::format(
|
||||
"Exception opening profile file for read: {} ({})", filename, e.what())
|
||||
);
|
||||
}
|
||||
|
||||
std::vector<std::string> content;
|
||||
std::string line;
|
||||
while (std::getline(inFile, line)) {
|
||||
content.push_back(std::move(line));
|
||||
}
|
||||
|
||||
return deserialize(content);
|
||||
}
|
||||
|
||||
} // namespace openspace
|
||||
|
||||
Reference in New Issue
Block a user