Skip to content

Commit

Permalink
add json as a dep, Scritpz to pack an unpack zipped files
Browse files Browse the repository at this point in the history
Console now uses scriptz to load a file and load each by a custom require to be tested

https://stackoverflow.com/questions/18965489/lua-require-but-files-are-only-in-memory
  • Loading branch information
Jerboa-app committed Jan 22, 2024
1 parent 8bd06da commit 8437f87
Show file tree
Hide file tree
Showing 12 changed files with 25,205 additions and 148 deletions.
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)
{
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)
{
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"
);
}

#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

0 comments on commit 8437f87

Please sign in to comment.