git repos / blockattack-game

commit 1a6e4354

Poul Sander · 2024-05-12 21:28
1a6e43547fa3ad857295a819008499a3d788c554 patch · browse files
parent 9f711750d1aead8d5cda23707d398d88fd67e51e

Removed more Cereal

Changed files

M source/code/BlockGame.hpp before
M source/code/replayhandler.cpp before
M source/code/replayhandler.hpp before
diff --git a/source/code/BlockGame.hpp b/source/code/BlockGame.hpp index d467f2c..17fcb4e 100644 --- a/source/code/BlockGame.hpp +++ b/source/code/BlockGame.hpp
@@ -27,9 +27,7 @@ http://www.blockattack.net
#include "stats.h"
#include "common.h"
#include <deque>
-#include "cereal/cereal.hpp"
-#include "cereal/types/deque.hpp"
-#include "cereal/types/string.hpp"
+#include "nlohmann/json.hpp"
#define NUMBEROFCHAINS 100
#define BLOCKWAIT 100000
@@ -70,12 +68,7 @@ struct BlockGameStartInfo {
int handicap = 0;
int gameSpeed = 0;
int basicBlockVariants = 6;
- template <class Archive>
- void serialize( Archive& ar ) {
- ar( CEREAL_NVP(ticks), CEREAL_NVP(timeTrial), CEREAL_NVP(stageClear), CEREAL_NVP(puzzleMode), CEREAL_NVP(singlePuzzle),
- CEREAL_NVP(level), CEREAL_NVP(AI), CEREAL_NVP(recordStats), CEREAL_NVP(vsMode), CEREAL_NVP(vsAI),
- CEREAL_NVP(startBlocks), CEREAL_NVP(handicap), CEREAL_NVP(gameSpeed), CEREAL_NVP(basicBlockVariants) );
- }
+ NLOHMANN_DEFINE_TYPE_INTRUSIVE(BlockGameStartInfo, ticks, timeTrial, stageClear, puzzleMode, singlePuzzle, level, AI, recordStats, vsMode, vsAI, startBlocks, handicap, gameSpeed, basicBlockVariants);
};
struct GarbageStruct {
@@ -87,10 +80,7 @@ struct GarbageStruct {
width = w;
height = h;
}
- template <class Archive>
- void serialize( Archive& ar ) {
- ar( CEREAL_NVP(greyGarbage), CEREAL_NVP(width), CEREAL_NVP(height) );
- }
+ NLOHMANN_DEFINE_TYPE_INTRUSIVE(GarbageStruct, greyGarbage, width, height);
};
struct BlockGameAction {
@@ -101,30 +91,34 @@ struct BlockGameAction {
int x = 0;
int y = 0;
std::vector<GarbageStruct> garbage;
- template <class Archive>
- void serialize( Archive& ar ) {
- ar( CEREAL_NVP(action), CEREAL_NVP(tick), CEREAL_NVP(way), CEREAL_NVP(x), CEREAL_NVP(y), CEREAL_NVP(garbage) );
- }
};
+static void to_json(nlohmann::json& j, const BlockGameAction& p) {
+ j = nlohmann::json{ {"action", p.action}, {"tick", p.tick}, {"way", p.way}, {"x", p.x}, {"y", p.y}, {"garbage", p.garbage} };
+}
+
+static void from_json(const nlohmann::json& j, BlockGameAction& p) {
+ j.at("action").get_to(p.action);
+ j.at("tick").get_to(p.tick);
+ j.at("way").get_to(p.way);
+ j.at("x").get_to(p.x);
+ j.at("y").get_to(p.y);
+ j.at("garbage").get_to(p.garbage);
+}
+
+
struct BlockGameInfoExtra {
std::string name;
int score = 0;
int seconds = 0;
- template <class Archive>
- void serialize( Archive& ar ) {
- ar( CEREAL_NVP(name), CEREAL_NVP(score), CEREAL_NVP(seconds) );
- }
+ NLOHMANN_DEFINE_TYPE_INTRUSIVE(BlockGameInfoExtra, name, score, seconds);
};
struct BlockGameInfo {
BlockGameStartInfo startInfo;
std::deque<BlockGameAction> actions;
BlockGameInfoExtra extra;
- template <class Archive>
- void serialize( Archive& ar ) {
- ar( CEREAL_NVP(startInfo), CEREAL_NVP(actions), CEREAL_NVP(extra) );
- }
+ NLOHMANN_DEFINE_TYPE_INTRUSIVE(BlockGameInfo, startInfo, actions, extra);
};
////////////////////////////////////////////////////////////////////////////////
diff --git a/source/code/replayhandler.cpp b/source/code/replayhandler.cpp index d67e16d..be1d40b 100644 --- a/source/code/replayhandler.cpp +++ b/source/code/replayhandler.cpp
@@ -22,10 +22,12 @@ http://www.blockattack.net
*/
#include "replayhandler.hpp"
+#include "nlohmann/json.hpp"
#include <sstream>
-#include "cereal/archives/json.hpp"
#include "sago/SagoMisc.hpp"
+using json = nlohmann::json;
+
static std::tm GetLocalTime() {
std::time_t t = std::time(nullptr);
std::tm ret = *std::localtime(&t);
@@ -40,21 +42,17 @@ static std::string CreateFileName(const std::tm& t ) {
}
static void SaveReplayToFile(const SavedReplayStruct& sr, const std::string& filename) {
- std::stringstream ss;
- {
- cereal::JSONOutputArchive archive(ss,cereal::JSONOutputArchive::Options::NoIndent());
- archive(cereal::make_nvp("savedReplay", sr));
- }
- sago::WriteFileContent(filename.c_str(), ss.str());
+ json j = json{
+ {"savedReplay", sr}
+ };
+ std::string s = j.dump(4);
+ sago::WriteFileContent(filename.c_str(), s);
}
static void LoadReplayFromPhysFile(SavedReplayStruct& sr, const std::string& filename) {
std::string filecontent = sago::GetFileContent(filename.c_str());
- std::stringstream ss(filecontent);
- {
- cereal::JSONInputArchive archive(ss);
- archive(cereal::make_nvp("savedReplay", sr));
- }
+ json j = json::parse(filecontent);
+ j.at("savedReplay").get_to(sr);
}
void LoadReplay(const std::string& filename, BlockGameInfo& game1, BlockGameInfo& game2) {
diff --git a/source/code/replayhandler.hpp b/source/code/replayhandler.hpp index 3d06165..2d4f78a 100644 --- a/source/code/replayhandler.hpp +++ b/source/code/replayhandler.hpp
@@ -22,15 +22,12 @@ http://www.blockattack.net
*/
#include "BlockGame.hpp"
-#include "cereal/types/vector.hpp"
+#include "nlohmann/json.hpp"
struct SavedReplayStruct {
int numberOfPlayers = 0;
std::vector<BlockGameInfo> playerInfo;
- template <class Archive>
- void serialize( Archive& ar ) {
- ar( CEREAL_NVP(numberOfPlayers), CEREAL_NVP(playerInfo) );
- }
+ NLOHMANN_DEFINE_TYPE_INTRUSIVE(SavedReplayStruct, numberOfPlayers, playerInfo);
};
void SaveReplay(const BlockGameInfo& game1);