aboutsummaryrefslogtreecommitdiff
path: root/source
diff options
context:
space:
mode:
authorrtk0c <[email protected]>2023-06-13 16:46:47 -0700
committerrtk0c <[email protected]>2023-06-13 16:46:47 -0700
commit21192a63e1411134b3096da4e5f9a511f913c9b9 (patch)
treec19ceb7fbdc9e0ce226e0dfb4d7a1132c52e7854 /source
parent32e74b9c81b57a9c0284f5374536439d581bc4b6 (diff)
Changeset: 95 Change Uid's serialization format from JSON array to just a string
Diffstat (limited to 'source')
-rw-r--r--source/10-common/Uid.cpp28
-rw-r--r--source/10-common/Uid.hpp4
-rw-r--r--source/30-game/EditorCorePrivate.cpp11
-rw-r--r--source/30-game/Ires.cpp12
-rw-r--r--source/30-game/Ires.hpp2
-rw-r--r--source/30-game/main.cpp86
6 files changed, 100 insertions, 43 deletions
diff --git a/source/10-common/Uid.cpp b/source/10-common/Uid.cpp
index 7f8fd9d..58dfffd 100644
--- a/source/10-common/Uid.cpp
+++ b/source/10-common/Uid.cpp
@@ -34,21 +34,33 @@ std::string Uid::WriteString() {
}
void Uid::Read(const rapidjson::Value& value) {
- assert(value.IsArray());
- assert(value.Size() == 2);
- auto& upper = value[0];
- assert(upper.IsUint64());
- auto& lower = value[1];
- assert(lower.IsUint64());
+ if (value.IsString()) {
+ ReadString(rapidjson::AsStringView(value));
+ } else if (value.IsArray()) {
+ // Compatibility support
+ assert(value.Size() == 2);
+ auto& upper = value[0];
+ assert(upper.IsUint64());
+ auto& lower = value[1];
+ assert(lower.IsUint64());
- this->upper = upper.GetUint64();
- this->lower = lower.GetUint64();
+ this->upper = upper.GetUint64();
+ this->lower = lower.GetUint64();
+ } else {
+ assert(false);
+ }
}
void Uid::WriteInto(rapidjson::Value& value, rapidjson::Document& root) const {
+#if BRUSSEL_Uid_WRITE_USE_ARRAY
value.Reserve(2, root.GetAllocator());
value.PushBack((uint64_t)upper, root.GetAllocator());
value.PushBack((uint64_t)lower, root.GetAllocator());
+#else
+ char buf[256];
+ int len = snprintf(buf, sizeof(buf), BRUSSEL_Uid_FORMAT_STR, upper, lower);
+ value.SetString(buf, len, root.GetAllocator());
+#endif
}
rapidjson::Value Uid::Write(rapidjson::Document& root) const {
diff --git a/source/10-common/Uid.hpp b/source/10-common/Uid.hpp
index 539de03..a691911 100644
--- a/source/10-common/Uid.hpp
+++ b/source/10-common/Uid.hpp
@@ -13,10 +13,14 @@
#define BRUSSEL_Uid_FORMAT_STR "%016" PRIx64 "-%016" PRIx64
#define BRUSSEL_Uid_FORMAT_EXPAND(uid) (uid).upper, (uid).lower
+// Serialize Uid object as an array with two elements, instead of the simple string format
+#define BRUSSEL_Uid_WRITE_USE_ARRAY 0
+
struct Uid {
uint64_t upper = 0;
uint64_t lower = 0;
+ // Generate a random Uid
static Uid Create();
bool IsNull() const;
diff --git a/source/30-game/EditorCorePrivate.cpp b/source/30-game/EditorCorePrivate.cpp
index e52c795..3efa33c 100644
--- a/source/30-game/EditorCorePrivate.cpp
+++ b/source/30-game/EditorCorePrivate.cpp
@@ -212,6 +212,17 @@ void EditorContentBrowser::Show(bool* open) {
ImGui::EndPopup();
}
+ ImGui::SameLine();
+ if (ImGui::Button("...")) {
+ ImGui::OpenPopup("More Actions");
+ }
+ if (ImGui::BeginPopup("More Actions")) {
+ if (ImGui::MenuItem("Rewrite all Ires to disk")) {
+ IresManager::instance->OverwriteAllToDisk();
+ }
+ ImGui::EndPopup();
+ }
+
auto& objects = IresManager::instance->GetObjects();
for (auto it = objects.begin(); it != objects.end(); ++it) {
auto ires = it->second.Get();
diff --git a/source/30-game/Ires.cpp b/source/30-game/Ires.cpp
index bfa4cdf..4f8da85 100644
--- a/source/30-game/Ires.cpp
+++ b/source/30-game/Ires.cpp
@@ -393,10 +393,20 @@ void IresManager::Save(IresObject* ires, const fs::path& filePath) {
char writerBuffer[65536];
rapidjson::FileWriteStream stream(file, writerBuffer, sizeof(writerBuffer));
rapidjson::PrettyWriter<rapidjson::FileWriteStream> writer(stream);
- writer.SetFormatOptions(rapidjson::PrettyFormatOptions::kFormatSingleLineArray);
+ // We no longer need this after disabling BRUSSEL_Uid_WRITE_USE_ARRAY
+// writer.SetFormatOptions(rapidjson::PrettyFormatOptions::kFormatSingleLineArray);
+#if BRUSSEL_Uid_WRITE_USE_ARRAY
+# warning "Writing Uid in array format but single line formatting isn't enabled, this might cause excessively long ires files."
+#endif
root.Accept(writer);
}
+void IresManager::OverwriteAllToDisk() {
+ for (const auto& [DISCARD, ires] : mObjByUid) {
+ Save(ires.Get());
+ }
+}
+
IresObject* IresManager::FindIres(const Uid& uid) const {
auto iter = mObjByUid.find(uid);
if (iter != mObjByUid.end()) {
diff --git a/source/30-game/Ires.hpp b/source/30-game/Ires.hpp
index 22018cd..e2e79bd 100644
--- a/source/30-game/Ires.hpp
+++ b/source/30-game/Ires.hpp
@@ -121,6 +121,8 @@ public:
void Save(IresObject* ires);
void Save(IresObject* ires, const std::filesystem::path& filePath);
+ void OverwriteAllToDisk();
+
const auto& GetObjects() const { return mObjByUid; }
virtual IresObject* FindIres(const Uid& uid) const override;
};
diff --git a/source/30-game/main.cpp b/source/30-game/main.cpp
index 55d2586..77c4674 100644
--- a/source/30-game/main.cpp
+++ b/source/30-game/main.cpp
@@ -98,6 +98,7 @@ void GlfwKeyCallback(GLFWwindow* window, int key, int scancode, int action, int
}
}
+// For platform data path selection below
// https://stackoverflow.com/questions/54499256/how-to-find-the-saved-games-folder-programmatically-in-c-c
#if defined(_WIN32)
# if defined(__MINGW32__)
@@ -173,46 +174,53 @@ int main(int argc, char* argv[]) {
AppConfig::assetDir = std::move(assetDir);
AppConfig::assetDirPath = std::move(assetDirPath);
} else {
-#if defined(_WIN32)
- fs::path dataDir;
+ AppConfig::assetDir = ".";
+ AppConfig::assetDirPath = fs::path(".");
+ }
+
+ if (args.count(kGameDataDir) > 0) {
+ auto dataDir = args[kGameDataDir].as<std::string>();
+
+ fs::path dataDirPath(dataDir);
+ fs::create_directories(dataDir);
+
+ AppConfig::dataDir = std::move(dataDir);
+ AppConfig::dataDirPath = std::move(dataDirPath);
+ } else {
+#if BRUSSEL_DEV_ENV
+ AppConfig::dataDir = ".";
+ AppConfig::dataDirPath = fs::path(".");
+#else
+// In a regular build, use default platform data paths
+# if defined(_WIN32)
+ fs::path dataDirPath;
PWSTR path = nullptr;
HRESULT hr = SHGetKnownFolderPath(FOLDERID_RoamingAppData, KF_FLAG_CREATE, nullptr, &path);
if (SUCCEEDED(hr)) {
- dataDir = fs::path(path) / AppConfig::kAppName;
+ dataDirPath = fs::path(path) / AppConfig::kAppName;
CoTaskMemFree(path);
- fs::create_directories(dataDir);
+ fs::create_directories(dataDirPath);
} else {
std::string msg;
msg += "Failed to find/create the default user data directory at %APPDATA%. Error code: ";
msg += hr;
throw std::runtime_error(msg);
}
-#elif defined(__APPLE__)
+# elif defined(__APPLE__)
// MacOS programming guide recommends apps to hardcode the path - user customization of "where data are stored" is done in Finder
- auto dataDir = fs::path("~/Library/Application Support/") / AppConfig::kAppName;
- fs::create_directories(dataDir);
-#elif defined(__linux__)
- auto dataDir = GetEnvVar("XDG_DATA_HOME", "~/.local/share") / AppConfig::kAppName;
- fs::create_directories(dataDir);
+ auto dataDirPath = fs::path("~/Library/Application Support/") / AppConfig::kAppName;
+ fs::create_directories(dataDirPath);
+# elif defined(__linux__)
+ auto dataDirPath = GetEnvVar("XDG_DATA_HOME", "~/.local/share") / AppConfig::kAppName;
+ fs::create_directories(dataDirPath);
+# endif
+ AppConfig::dataDir = dataDirPath.string();
+ AppConfig::dataDirPath = dataDirPath;
#endif
}
- if (args.count(kGameDataDir) > 0) {
- auto dataDir = args[kGameDataDir].as<std::string>();
-
- fs::path dataDirPath(dataDir);
- fs::create_directories(dataDir);
-
- AppConfig::dataDir = std::move(dataDir);
- AppConfig::dataDirPath = std::move(dataDirPath);
- } else {
- // TODO platform default path
- AppConfig::dataDir = ".";
- AppConfig::dataDirPath = fs::path(".");
- }
-
if (!glfwInit()) {
return -1;
}
@@ -237,7 +245,7 @@ int main(int argc, char* argv[]) {
GlfwUserData glfwUserData;
- GLFWwindow* window = glfwCreateWindow(1280, 720, "Project Brussel", nullptr, nullptr);
+ GLFWwindow* window = glfwCreateWindow(1280, 720, AppConfig::kAppNameC, nullptr, nullptr);
if (window == nullptr) {
return -2;
}
@@ -264,17 +272,15 @@ int main(int argc, char* argv[]) {
}
#if defined(BRUSSEL_DEV_ENV)
- {
- auto glVersionString = glGetString(GL_VERSION);
+ auto glVersionString = glGetString(GL_VERSION);
- int glMajorVersion;
- glGetIntegerv(GL_MAJOR_VERSION, &glMajorVersion);
- int glMinorVersion;
- glGetIntegerv(GL_MINOR_VERSION, &glMinorVersion);
+ int glMajorVersion;
+ glGetIntegerv(GL_MAJOR_VERSION, &glMajorVersion);
+ int glMinorVersion;
+ glGetIntegerv(GL_MINOR_VERSION, &glMinorVersion);
- printf("OpenGL version (via glGetString(GL_VERSION)): %s\n", glVersionString);
- printf("OpenGL version (via glGetIntegerv() with GL_MAJOR_VERSION and GL_MINOR_VERSION): %d.%d\n", glMajorVersion, glMinorVersion);
- }
+ printf("OpenGL version (via glGetString(GL_VERSION)): %s\n", glVersionString);
+ printf("OpenGL version (via glGetIntegerv() with GL_MAJOR_VERSION and GL_MINOR_VERSION): %d.%d\n", glMajorVersion, glMinorVersion);
#endif
bool useOpenGLDebug = args[kOpenGLDebug].as<bool>();
@@ -351,6 +357,18 @@ int main(int argc, char* argv[]) {
.semantic = VES_Color1,
});
+ gVformatLines.Attach(new VertexFormat());
+ gVformatLines->AddElement(VertexElementFormat{
+ .bindingIndex = 0,
+ .type = VET_Float3,
+ .semantic = VES_Position,
+ });
+ gVformatLines->AddElement(VertexElementFormat{
+ .bindingIndex = 0,
+ .type = VET_Ubyte4Norm,
+ .semantic = VES_Color1,
+ });
+
// Matches gVformatStandard
gDefaultShader.Attach(new Shader());
gDefaultShader->InitFromSources(Shader::ShaderSources{