Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add json as a dep, Scritpz to pack an unpack zipped files #155

Merged
merged 5 commits into from
Jan 24, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ IF (TEST_SUITE)
add_executable(testSuite
${TEST_SRC}
"src/World/mapFile.cpp"
"src/Util/z.cpp"
)

target_compile_definitions(testSuite PUBLIC GLSL_VERSION="330")
Expand Down
33 changes: 33 additions & 0 deletions include/Console/console.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <System/Physics/sCollision.h>
#include <jLog/jLog.h>
#include <Object/id.h>
#include <Console/scriptz.h>

#include <memory>
#include <vector>
Expand Down Expand Up @@ -263,6 +264,36 @@ namespace Hop
return;
}

void loadPackedScripts(std::string file)
{
scripts.load(file);
}

int require(lua_State * lua)
Jerboa-app marked this conversation as resolved.
Show resolved Hide resolved
{
int n = lua_gettop(lua);

if (n != 1)
{
lua_pushliteral(lua, "expected a string script name as argument");
return lua_error(lua);
}

LuaString script;

script.read(lua, 1);

std::string body = scripts.get(script.characters);

if (body == "")
{
lua_pushliteral(lua, "script not found");
return lua_error(lua);
}

luaL_loadbuffer(lua, body.c_str(), body.size(), script.characters.c_str());
}

private:

lua_State * lua;
Expand All @@ -273,6 +304,8 @@ namespace Hop

Log & log;

Scriptz scripts;

static int traceback(lua_State * lua) {
if (lua_isstring(lua, -1))
{
Expand Down
112 changes: 112 additions & 0 deletions include/Console/scriptz.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
#ifndef SCRIPTZ_H
#define SCRIPTZ_H

#include <Util/z.h>

#include <unordered_map>
#include <string>
#include <vector>
#include <fstream>

#include <vendored/json.hpp>
using json = nlohmann::json;

namespace Hop
{
class Scriptz
{

public:

const char * FILE_EXTENSION = ".scriptz";
const char * HEADER = "Hop scriptz file, a zlib compressed JSON dump of lua scripts, next line is the uncompressed size";

Scriptz()
{}

void load(std::string file)
{
std::vector<uint8_t> data = Hop::Util::Z::load(file);

json parsed = json::parse(data.cbegin(), data.cend());

for (auto c : parsed.items())
{
if (c.value().is_string())
{
add(c.key(), c.value());
}
}
}

void save(std::string file)
Jerboa-app marked this conversation as resolved.
Show resolved Hide resolved
{
if (size() > 0)
{
json data;

for (auto s : scripts)
{
data[s.first] = s.second;
}

std::string dump = data.dump();
std::vector<uint8_t> bytes(dump.begin(), dump.end());

std::string end = FILE_EXTENSION;

if (file.size() > end.size())
{
if (!std::equal(end.rbegin(), end.rend(), file.rbegin()))
{
file = file + FILE_EXTENSION;
}
}
else
{
file = file + FILE_EXTENSION;
}

Hop::Util::Z::save(file, bytes, HEADER);
}
}

void add(std::string script, std::string name)
{
scripts[name] = script;
}

void remove(std::string name){ scripts.erase(name); }

std::string get(std::string name) { return scripts[name]; }

std::unordered_map<std::string, std::string>::const_iterator cbegin() const { return scripts.cbegin(); }
std::unordered_map<std::string, std::string>::const_iterator cend() const { return scripts.cend(); }

const size_t size() const { return scripts.size(); }

private:

std::unordered_map<std::string, std::string> scripts;

class ScriptzIOError: public std::exception
{

public:

ScriptzIOError(std::string msg)
: msg(msg)
{}

private:

virtual const char * what() const throw()
{
return msg.c_str();
}
std::string msg;
};

};
}
#endif /* SCRIPTPACK_H */
41 changes: 41 additions & 0 deletions include/Util/z.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#ifndef Z_H
#define Z_H

#include <string>
#include <vector>
#include <exception>
#include <fstream>
#include <zlib.h>

namespace Hop::Util::Z
{

class CompressionIOError: public std::exception
{

public:

CompressionIOError(std::string msg)
: msg(msg)
{}

private:

virtual const char * what() const throw()
{
return msg.c_str();
}
std::string msg;
};

std::vector<uint8_t> load(std::string file);

void save
(
std::string file,
std::vector<uint8_t> data,
std::string header = "compresses file, next line is uncompressed size"
Jerboa-app marked this conversation as resolved.
Show resolved Hide resolved
);
}

#endif /* Z_H */
2 changes: 1 addition & 1 deletion include/World/mapFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include <vector>
#include <bitset>

#include <zlib.h>
#include <Util/z.h>

#include <Util/sparseData.h>
#include <utility>
Expand Down
Loading