-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add json as a dep, Scritpz to pack an unpack zipped files
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
1 parent
8bd06da
commit 8437f87
Showing
12 changed files
with
25,205 additions
and
148 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.