Skip to content

Commit

Permalink
add gdr
Browse files Browse the repository at this point in the history
  • Loading branch information
FireMario211 committed Mar 30, 2024
1 parent 2165e5c commit edbcae9
Show file tree
Hide file tree
Showing 3 changed files with 24,946 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ svgs/
compile_commands.json
resources/fontvers/

tmp/

notas.h
resources/gatos/
server/character.js
179 changes: 179 additions & 0 deletions includes/gdr/gdr.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
// fixed it up a bit because android doesnt like try catch
#pragma once
#include <vector>
#include "json.hpp"

namespace gdr {

using namespace nlohmann;

struct Bot {
std::string name;
std::string version;

inline Bot(std::string const& name, std::string const& version)
: name(name), version(version) {}
};

struct Level {
uint32_t id;
std::string name;

Level() = default;

inline Level(std::string const& name, uint32_t id = 0)
: name(name), id(id) {}
};

class Input {
protected:
Input() = default;
template <typename, typename>
friend class Replay;
public:
uint32_t frame;
int button;
bool player2;
bool down;

inline virtual void parseExtension(json::object_t obj) {}
inline virtual json::object_t saveExtension() const {
return {};
}

inline Input(int frame, int button, bool player2, bool down)
: frame(frame), button(button), player2(player2), down(down) {}


inline static Input hold(int frame, int button, bool player2 = false) {
return Input(frame, button, player2, true);
}

inline static Input release(int frame, int button, bool player2 = false) {
return Input(frame, button, player2, false);
}

inline bool operator<(Input const& other) const {
return frame < other.frame;
}
};

template <typename S = void, typename T = Input>
class Replay {
Replay() = default;
public:
using InputType = T;
using Self = std::conditional_t<std::is_same_v<S, void>, Replay<S, T>, S>;

std::string author;
std::string description;

float duration;
float gameVersion;
float version = 1.0;

float framerate = 240.f;

int seed = 0;
int coins = 0;

bool ldm = false;

Bot botInfo;
Level levelInfo;

std::vector<InputType> inputs;

uint32_t frameForTime(double time)
{
return static_cast<uint32_t>(time * (double)framerate);
}

virtual void parseExtension(json::object_t obj) {}
virtual json::object_t saveExtension() const {
return {};
}

Replay(std::string const& botName, std::string const& botVersion)
: botInfo(botName, botVersion) {}

static Self importData(std::vector<uint8_t> const& data, bool importInputs = true, bool isJson = false) {
Self replay;
json replayJson;

if (!isJson) {
replayJson = json::from_msgpack(data);
} else {
replayJson = json::parse(data);
}

replay.gameVersion = replayJson["gameVersion"];
replay.description = replayJson["description"];
replay.version = replayJson["version"];
replay.duration = replayJson["duration"];
replay.botInfo.name = replayJson["bot"]["name"];
replay.botInfo.version = replayJson["bot"]["version"];
replay.levelInfo.id = replayJson["level"]["id"];
replay.levelInfo.name = replayJson["level"]["name"];
replay.author = replayJson["author"];
replay.seed = replayJson["seed"];
replay.coins = replayJson["coins"];
replay.ldm = replayJson["ldm"];

if(replayJson.contains("framerate"))
replay.framerate = replayJson["framerate"];
replay.parseExtension(replayJson.get<json::object_t>());

if(!importInputs)
return replay;

for (json const& inputJson : replayJson["inputs"]) {
InputType input;
input.frame = inputJson["frame"];
input.button = inputJson["btn"];
input.player2 = inputJson["2p"];
input.down = inputJson["down"];
input.parseExtension(inputJson.get<json::object_t>());

replay.inputs.push_back(input);
}

return replay;
}

std::vector<uint8_t> exportData(bool exportJson = false) {
json replayJson = saveExtension();
replayJson["gameVersion"] = gameVersion;
replayJson["description"] = description;
replayJson["version"] = version;
replayJson["duration"] = duration;
replayJson["bot"]["name"] = botInfo.name;
replayJson["bot"]["version"] = botInfo.version;
replayJson["level"]["id"] = levelInfo.id;
replayJson["level"]["name"] = levelInfo.name;
replayJson["author"] = author;
replayJson["seed"] = seed;
replayJson["coins"] = coins;
replayJson["ldm"] = ldm;
replayJson["framerate"] = framerate;

for (InputType const& input : inputs) {
json inputJson = input.saveExtension();
inputJson["frame"] = input.frame;
inputJson["btn"] = input.button;
inputJson["2p"] = input.player2;
inputJson["down"] = input.down;

replayJson["inputs"].push_back(inputJson);
}

if (exportJson) {
std::string replayString = replayJson.dump();
return std::vector<uint8_t>(replayString.begin(), replayString.end());
} else {
return json::to_msgpack(replayJson);
}
}
};

} // namespace gdr
Loading

0 comments on commit edbcae9

Please sign in to comment.