summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJonathan Bradley <jcb@pikum.xyz>2025-03-20 15:56:35 -0400
committerJonathan Bradley <jcb@pikum.xyz>2025-03-21 11:06:05 -0400
commitcae76dd98e301a4560bb46ecb59b5952dff04149 (patch)
tree8e85d17234c6838fd24925889cd784296df56956
parent9f036b05d36203465ca481f39d2cd51233e82b9e (diff)
pke: cleanup std stream usage
-rw-r--r--src/font.cpp5
-rw-r--r--src/font.hpp4
-rw-r--r--src/game.cpp9
-rw-r--r--src/plugin-types.hpp6
-rw-r--r--src/project.cpp138
5 files changed, 92 insertions, 70 deletions
diff --git a/src/font.cpp b/src/font.cpp
index 0a4ccbd..64728ee 100644
--- a/src/font.cpp
+++ b/src/font.cpp
@@ -7,6 +7,7 @@
#include "static-plane.hpp"
#include "vendor-stb-image-include.h"
#include "game-settings.hpp"
+
#include <vulkan/vulkan_core.h>
// TODO threading
@@ -312,7 +313,7 @@ void FontType_Tick(double delta) {
}
}
-void FontType_Serialize(std::ofstream &stream, FontType *ft) {
+void FontType_Serialize(std::ostream &stream, FontType *ft) {
FontTypeSpacing sp{};
FontTypeMSDFSettings msdf{};
memset(&sp, 0, sizeof(sp));
@@ -355,7 +356,7 @@ void FontType_Serialize(std::ofstream &stream, FontType *ft) {
AM_Release(ft->fontTextureAssetHandle);
}
-void FontType_Deserialize(std::ifstream &stream) {
+void FontType_Deserialize(std::istream &stream) {
uint64_t i;
NULL_CHAR_ARR(readLine, 128);
FontTypeSpacing sp{};
diff --git a/src/font.hpp b/src/font.hpp
index f935f94..562ecae 100644
--- a/src/font.hpp
+++ b/src/font.hpp
@@ -106,8 +106,8 @@ struct FontType : public Entity_Base {
void FontType_Init();
void FontType_Teardown();
void FontType_Tick(double delta);
-void FontType_Serialize(std::ofstream &stream, FontType *ft);
-void FontType_Deserialize(std::ifstream &stream);
+void FontType_Serialize(std::ostream &stream, FontType *ft);
+void FontType_Deserialize(std::istream &stream);
FontType* FontType_GetFonts(FontTypeIndex &count);
FontTypeIndex FontType_RegisterFont(pk_cstr title, AssetHandle fontTextureHandle, AssetHandle glyphsHandle, FontTypeMSDFSettings *msdf_settings, FontTypeSpacing *spacing);
void FontType_Unload(FontTypeIndex idx);
diff --git a/src/game.cpp b/src/game.cpp
index 690292c..3c39018 100644
--- a/src/game.cpp
+++ b/src/game.cpp
@@ -29,7 +29,6 @@
#include <iomanip>
#include <ostream>
#include <fstream>
-#include <sstream>
#include <thread>
const long readLineLength = 128;
@@ -73,7 +72,7 @@ const char *PKE_FILE_CAMERA_INSTANCE_HANDLE = "Cam::InstanceHandle: ";
const char *PKE_FILE_CAMERA_TARGET_INSTANCE_HANDLE = "Cam::TargetInstanceHandle: ";
const char *PKE_FILE_CAMERA_IS_PRIMARY = "Cam::IsPrimary: ";
-void SerializeCamera(std::ostringstream &stream, const PkeCamera &cam) {
+void SerializeCamera(std::ostream &stream, const PkeCamera &cam) {
NULL_CHAR_ARR(handleStr, 23);
PkeCamera c{};
if (cam.type != c.type) {
@@ -95,7 +94,7 @@ void SerializeCamera(std::ostringstream &stream, const PkeCamera &cam) {
}
}
-void SerializeInstance(std::ostringstream &stream, const CompInstance &comp) {
+void SerializeInstance(std::ostream &stream, const CompInstance &comp) {
NULL_CHAR_ARR(handleStr, 23);
EntityType *et = nullptr;
if (comp.grBindsHandle != GrBindsHandle_MAX) {
@@ -163,7 +162,7 @@ bool FindFirstInstanceHandle(void *handle, void *mapping) {
InstMapping *inst_mapping = reinterpret_cast<InstMapping *>(mapping);
return inst_mapping->origHandle == *reinterpret_cast<InstanceHandle *>(handle);
}
-void DeserializeCamera(PkeLevel *level, std::ifstream &stream) {
+void DeserializeCamera(PkeLevel *level, std::istream &stream) {
PkeCamera cam{};
InstanceHandle instanceHandle = InstanceHandle_MAX;
InstanceHandle targetInstanceHandle = InstanceHandle_MAX;
@@ -248,7 +247,7 @@ void DeserializeCamera(PkeLevel *level, std::ifstream &stream) {
}
}
-void DeserializeInstance(Entity_Base *parentEntity, std::ifstream &stream) {
+void DeserializeInstance(Entity_Base *parentEntity, std::istream &stream) {
CompInstance comp{};
InstMapping mapping {
.origHandle = InstanceHandle_MAX,
diff --git a/src/plugin-types.hpp b/src/plugin-types.hpp
index 319960d..f7a119b 100644
--- a/src/plugin-types.hpp
+++ b/src/plugin-types.hpp
@@ -2,13 +2,13 @@
#define PKE_PLUGIN_TYPES_HPP
#include <cstdint>
-#include <fstream>
+#include <istream>
constexpr int64_t CallbackSignatureLength = 16;
using CallbackSignature = char[CallbackSignatureLength];
-typedef void (*DeserializeDelegate)(std::ifstream *stream);
-typedef void (*SerializeDelegate)(std::ifstream *stream);
+typedef void (*DeserializeDelegate)(std::istream *stream);
+typedef void (*SerializeDelegate)(std::istream *stream);
typedef void (*CollisionDelegate)(const void *entHandleLeft, const void *entHandleRight);
struct PkeEntityTypeInterface {
diff --git a/src/project.cpp b/src/project.cpp
index ce9ba58..e8679c8 100644
--- a/src/project.cpp
+++ b/src/project.cpp
@@ -44,7 +44,7 @@ const char* const PKE_PROJ_FILE_ASSET_BASE_PATH = "Asset::BasePath: ";
const char* const PKE_PROJ_FILE_ASSET_TYPE = "Asset::Type: ";
/*
-void Proj_SerializeProjectSettings(std::ofstream &stream) {
+void Proj_SerializeProjectSettings(std::ostream &stream) {
PkeProjectSettings ps{};
if (strncmp(ps.defaultSceneName, pkeProjectSettings.defaultSceneName, strlen(pkeProjectSettings.defaultSceneName)) != 0) {
stream << PKE_PROJ_FILE_PROJ_SETTINGS_DEFAULT_SCENE_NAME << ps.defaultSceneName << std::endl;
@@ -64,7 +64,7 @@ void Proj_SerializeProjectSettings(std::ofstream &stream) {
}
*/
-void Proj_SerializeEntityType(std::ofstream &stream, const EntityType &et) {
+void Proj_SerializeEntityType(std::ostream &stream, const EntityType &et) {
NULL_CHAR_ARR(handleStr, 23);
NULL_CHAR_ARR(modelAssetKey, AssetKeyLength + 1);
NULL_CHAR_ARR(textureAssetKey, AssetKeyLength + 1);
@@ -104,7 +104,7 @@ void Proj_SerializeEntityType(std::ofstream &stream, const EntityType &et) {
}
}
-void Proj_SerializeAsset(std::ofstream &stream, const Asset &asset) {
+void Proj_SerializeAsset(std::ostream &stream, const Asset &asset) {
NULL_CHAR_ARR(keyStr, AssetKeyLength + 1);
snprintf(keyStr, AssetKeyLength + 1, "%s", asset.key);
Asset a{};
@@ -120,7 +120,7 @@ void Proj_SerializeAsset(std::ofstream &stream, const Asset &asset) {
}
/*
-void Proj_DeserializeProjectSettings(std::ifstream &stream) {
+void Proj_DeserializeProjectSettings(std::istream &stream) {
while (memset(projReadLine, 0, projReadLineLength), stream.getline(projReadLine, projReadLineLength)) {
if (strcmp(PKE_PROJ_FILE_OBJ_END, projReadLine) == 0) {
return;
@@ -157,7 +157,7 @@ void Proj_DeserializeProjectSettings(std::ifstream &stream) {
}
*/
-void Proj_DeserializeEntityType(std::ifstream &stream) {
+void Proj_DeserializeEntityType(std::istream &stream) {
char collisionSig[CallbackSignatureLength + 1];
collisionSig[0] = '\0';
char createInstanceSig[CallbackSignatureLength + 1];
@@ -276,7 +276,7 @@ void Proj_DeserializeEntityType(std::ifstream &stream) {
}
}
-void Proj_DeserializeAssset(std::ifstream &stream) {
+void Proj_DeserializeAssset(std::istream &stream) {
char keyStr[AssetKeyLength + 1];
keyStr[AssetKeyLength] = '\0';
char basePath[256];
@@ -344,68 +344,90 @@ void PkeProject_Load(const char *filePath) {
}
void PkeProject_Save(const char *filePath) {
- const char *safeFilePath = filePath == nullptr ? PKE_PROJ_DEFAULT_FILENAME : filePath;
- std::ofstream f(safeFilePath);
- if (!f.is_open()) {
- fprintf(stderr, "While attempting to save project file, failed to open requested file for writing: %s", safeFilePath);
- return;
- }
+ bool failed = false;
+ const char *saveFilePath = filePath == nullptr ? PKE_PROJ_DEFAULT_FILENAME : filePath;
+ std::ostringstream stream{};
- f << PKE_PROJ_FILE_BEGIN << std::endl;
- f << PKE_PROJ_FILE_VERSION << std::endl;
- f << "" << std::endl;
+ try {
- /*
- f << PKE_PROJ_FILE_OBJ_PROJECT_SETTINGS << std::endl;
- Proj_SerializeProjectSettings(f);
- f << PKE_PROJ_FILE_OBJ_END << std::endl;
- */
+ stream << PKE_PROJ_FILE_BEGIN << std::endl;
+ stream << PKE_PROJ_FILE_VERSION << std::endl;
+ stream << "" << std::endl;
- pk_handle_bucket_index_T assetB = AM_GetBucketCount();
- for (pk_handle_bucket_index_T b = 0; b < assetB; ++b) {
- pk_handle_item_index_T assetI = 0;
- auto *assets = AM_GetAssets(b, assetI);
- for (pk_handle_item_index_T i = 0; i < assetI; ++i) {
- bool isGlobalAsset = false;
- for (long k = 0; k < EngineDefinedAssetCount; ++k) {
- if (strncmp(EngineDefinedAssets[k], assets[i].key, AssetKeyLength) == 0) {
- isGlobalAsset = true;
- break;
+ /*
+ f << PKE_PROJ_FILE_OBJ_PROJECT_SETTINGS << std::endl;
+ Proj_SerializeProjectSettings(f);
+ f << PKE_PROJ_FILE_OBJ_END << std::endl;
+ */
+
+ pk_handle_bucket_index_T assetB = AM_GetBucketCount();
+ for (pk_handle_bucket_index_T b = 0; b < assetB; ++b) {
+ pk_handle_item_index_T assetI = 0;
+ auto *assets = AM_GetAssets(b, assetI);
+ for (pk_handle_item_index_T i = 0; i < assetI; ++i) {
+ bool isGlobalAsset = false;
+ for (long k = 0; k < EngineDefinedAssetCount; ++k) {
+ if (strncmp(EngineDefinedAssets[k], assets[i].key, AssetKeyLength) == 0) {
+ isGlobalAsset = true;
+ break;
+ }
}
+ if (isGlobalAsset) continue;
+ stream << PKE_PROJ_FILE_OBJ_ASSET << std::endl;
+ Proj_SerializeAsset(stream, assets[i]);
+ stream << PKE_PROJ_FILE_OBJ_END << std::endl;
}
- if (isGlobalAsset) continue;
- f << PKE_PROJ_FILE_OBJ_ASSET << std::endl;
- Proj_SerializeAsset(f, assets[i]);
- f << PKE_PROJ_FILE_OBJ_END << std::endl;
}
- }
- const auto entBucketCount = EntityType_GetBucketCount();
- for (pk_handle_bucket_index_T b = 0; b < entBucketCount; ++b) {
- pk_handle_item_index_T itemCount = 0;
- auto *entities = EntityType_GetEntityTypes(b, itemCount);
- for (pk_handle_item_index_T i = 0; i < itemCount; ++i) {
- if (entities[i].modelAssetKey[0] == '\0') {
- continue;
+ const auto entBucketCount = EntityType_GetBucketCount();
+ for (pk_handle_bucket_index_T b = 0; b < entBucketCount; ++b) {
+ pk_handle_item_index_T itemCount = 0;
+ auto *entities = EntityType_GetEntityTypes(b, itemCount);
+ for (pk_handle_item_index_T i = 0; i < itemCount; ++i) {
+ if (entities[i].modelAssetKey[0] == '\0') {
+ continue;
+ }
+ stream << PKE_PROJ_FILE_OBJ_ENTITY_TYPE << std::endl;
+ Proj_SerializeEntityType(stream, entities[i]);
+ stream << PKE_PROJ_FILE_OBJ_END << std::endl;
}
- f << PKE_PROJ_FILE_OBJ_ENTITY_TYPE << std::endl;
- Proj_SerializeEntityType(f, entities[i]);
- f << PKE_PROJ_FILE_OBJ_END << std::endl;
}
- }
- FontTypeIndex font_count;
- FontType *fonts = FontType_GetFonts(font_count);
- for (FontTypeIndex b = FontTypeIndex{0}; b < font_count; ++b) {
- FontType *ft = &fonts[(FontTypeIndex_T)b];
- if (ft->title.val == nullptr) continue;
- f << PKE_PROJ_FILE_OBJ_FONT << std::endl;
- FontType_Serialize(f, ft);
- f << PKE_PROJ_FILE_OBJ_END << std::endl;
- }
+ FontTypeIndex font_count;
+ FontType *fonts = FontType_GetFonts(font_count);
+ for (FontTypeIndex b = FontTypeIndex{0}; b < font_count; ++b) {
+ FontType *ft = &fonts[(FontTypeIndex_T)b];
+ if (ft->title.val == nullptr) continue;
+ stream << PKE_PROJ_FILE_OBJ_FONT << std::endl;
+ FontType_Serialize(stream, ft);
+ stream << PKE_PROJ_FILE_OBJ_END << std::endl;
+ }
- f << PKE_PROJ_FILE_END << std::endl;
+ stream << PKE_PROJ_FILE_END << std::endl;
+ } catch (...) {
+ failed = false;
+ }
- f.flush();
- f.close();
+ if (failed == false) {
+ std::ofstream f(saveFilePath);
+ if (!f.is_open()) {
+ failed = true;
+ }
+ f.flush();
+ f.close();
+ }
+ if (failed) {
+ NULL_CHAR_ARR(errFileName, 256);
+ strncpy(errFileName, saveFilePath, 256);
+ strncpy(errFileName + strlen(saveFilePath), ".err", 256 - strlen(saveFilePath));
+ std::ofstream errF(saveFilePath);
+ if (errF.is_open()) {
+ errF << stream.str();
+ errF.flush();
+ errF.close();
+ fprintf(stderr, "Failed to save Projection file '%s', partial output saved to '%s'\n", saveFilePath, errFileName);
+ } else {
+ fprintf(stderr, "Failed to save Projection file '%s' and also failed to write failed output\n", saveFilePath);
+ }
+ }
}