Verified Commit b0f98384 authored by RobLoach's avatar RobLoach
Browse files

data: Move love.math.compress to love.data.compress()

parent 2cfc3da5
......@@ -9,6 +9,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- [Semantic Versioning](https://semver.org/) checks
- Use `t.version = "0.18.0"` in `conf(t)` to specify which ChaiLove version you're targeting
### Updated
- `love.math.compress()` is now `love.data.compress()`
### Fixed
- Documentation
......
......@@ -79,6 +79,7 @@
#include "libretro.h"
#include "love/keyboard.h"
#include "love/config.h"
#include "love/data.h"
#include "love/script.h"
#include "love/filesystem.h"
#include "love/graphics.h"
......@@ -113,6 +114,7 @@ class ChaiLove {
love::console console;
love::keyboard keyboard;
love::script* script;
love::data data;
love::filesystem filesystem;
love::graphics graphics;
love::image image;
......
#include "math.h"
#include <cstdlib>
#include <ctime>
#include <cstring>
#include <string>
#include <sstream>
#include <iostream>
#include "zlib.h"
std::string data::compress(const std::string& str) {
return compress(str, Z_BEST_SPEED);
}
std::string data::compress(const std::string& str, int compressionlevel) {
if (compressionlevel < 0 || compressionlevel > 9) {
compressionlevel = Z_BEST_SPEED;
}
z_stream zs;
memset(&zs, 0, sizeof(zs));
if (deflateInit(&zs, compressionlevel) != Z_OK) {
std::cout << "[ChaiLove] [data] deflateInit failed while compressing." << std::endl;
return str;
}
zs.next_in = (Bytef*)str.data();
zs.avail_in = str.size();
int ret;
char outbuffer[32768];
std::string outstring;
do {
zs.next_out = reinterpret_cast<Bytef*>(outbuffer);
zs.avail_out = sizeof(outbuffer);
ret = deflate(&zs, Z_FINISH);
if (outstring.size() < zs.total_out) {
outstring.append(outbuffer,
zs.total_out - outstring.size());
}
} while (ret == Z_OK);
deflateEnd(&zs);
if (ret != Z_STREAM_END) {
std::cout << "[ChaiLove] [data] Exception during zlib compression: (" << ret << ") " << zs.msg << std::endl;
return str;
}
return outstring;
}
std::string data::decompress(const std::string& str) {
z_stream zs;
memset(&zs, 0, sizeof(zs));
if (inflateInit(&zs) != Z_OK)
throw(std::runtime_error("inflateInit failed while decompressing."));
zs.next_in = (Bytef*)str.data();
zs.avail_in = str.size();
int ret;
char outbuffer[32768];
std::string outstring;
do {
zs.next_out = reinterpret_cast<Bytef*>(outbuffer);
zs.avail_out = sizeof(outbuffer);
ret = inflate(&zs, 0);
if (outstring.size() < zs.total_out) {
outstring.append(outbuffer,
zs.total_out - outstring.size());
}
} while (ret == Z_OK);
inflateEnd(&zs);
if (ret != Z_STREAM_END) {
std::ostringstream oss;
oss << "[ChaiLove] [data] Exception during zlib decompression: (" << ret << ") " << zs.msg;
throw(std::runtime_error(oss.str()));
}
return outstring;
}
#ifndef SRC_LOVE_DATA_H_
#define SRC_LOVE_DATA_H_
#include <string>
namespace love {
/**
* @brief Provides access to information about the user's system.
*/
class data {
public:
/**
* @brief Compresses a string or data using a specific compression level.
*
* @param str The raw (un-compressed) string to compress.
* @param level (-1) The level of compression to use, between 0 and 9. -1 indicates the default level.
*
* @return Compressed data in the form of a string.
*
* @see love.data.decompress
*/
std::string compress(const std::string& str, int level);
std::string compress(const std::string& str);
/**
* @brief Decompresses a compressed string.
*
* @param str A string containing data previously compressed with math.compress().
*
* @return A string containing the raw decompressed data.
*
* @see love.data.compress
*/
std::string decompress(const std::string& str);
};
} // namespace love
#endif // SRC_LOVE_DATA_H_
......@@ -6,7 +6,6 @@
#include <sstream>
#include <iostream>
#include "effolkronium/random.hpp"
#include "zlib.h"
using Random = effolkronium::random_static;
......@@ -58,88 +57,6 @@ int math::getRandomSeed() {
return m_seed;
}
std::string math::compress(const std::string& str) {
return compress(str, Z_BEST_SPEED);
}
std::string math::compress(const std::string& str, int compressionlevel) {
if (compressionlevel < 0 || compressionlevel > 9) {
compressionlevel = Z_BEST_SPEED;
}
z_stream zs;
memset(&zs, 0, sizeof(zs));
if (deflateInit(&zs, compressionlevel) != Z_OK) {
std::cout << "[ChaiLove] [math] deflateInit failed while compressing." << std::endl;
return str;
}
zs.next_in = (Bytef*)str.data();
zs.avail_in = str.size();
int ret;
char outbuffer[32768];
std::string outstring;
do {
zs.next_out = reinterpret_cast<Bytef*>(outbuffer);
zs.avail_out = sizeof(outbuffer);
ret = deflate(&zs, Z_FINISH);
if (outstring.size() < zs.total_out) {
outstring.append(outbuffer,
zs.total_out - outstring.size());
}
} while (ret == Z_OK);
deflateEnd(&zs);
if (ret != Z_STREAM_END) {
std::cout << "[ChaiLove] [math] Exception during zlib compression: (" << ret << ") " << zs.msg << std::endl;
return str;
}
return outstring;
}
std::string math::decompress(const std::string& str) {
z_stream zs;
memset(&zs, 0, sizeof(zs));
if (inflateInit(&zs) != Z_OK)
throw(std::runtime_error("inflateInit failed while decompressing."));
zs.next_in = (Bytef*)str.data();
zs.avail_in = str.size();
int ret;
char outbuffer[32768];
std::string outstring;
do {
zs.next_out = reinterpret_cast<Bytef*>(outbuffer);
zs.avail_out = sizeof(outbuffer);
ret = inflate(&zs, 0);
if (outstring.size() < zs.total_out) {
outstring.append(outbuffer,
zs.total_out - outstring.size());
}
} while (ret == Z_OK);
inflateEnd(&zs);
if (ret != Z_STREAM_END) {
std::ostringstream oss;
oss << "Exception during zlib decompression: (" << ret << ") " << zs.msg;
throw(std::runtime_error(oss.str()));
}
return outstring;
}
float math::rad(float degress) {
return degress * pi / 180.0f;
}
......
......@@ -70,30 +70,6 @@ class math {
*/
float degrees(float rad);
/**
* @brief Compresses a string or data using a specific compression level.
*
* @param str The raw (un-compressed) string to compress.
* @param level (-1) The level of compression to use, between 0 and 9. -1 indicates the default level.
*
* @return Compressed data in the form of a string.
*
* @see love.math.decompress
*/
std::string compress(const std::string& str, int level);
std::string compress(const std::string& str);
/**
* @brief Decompresses a compressed string.
*
* @param str A string containing data previously compressed with math.compress().
*
* @return A string containing the raw decompressed data.
*
* @see love.math.compress
*/
std::string decompress(const std::string& str);
/**
* @brief e
*
......
......@@ -86,6 +86,7 @@ script::script(const std::string& file) {
love["audio"] = var(std::ref(app->audio));
love["config"] = var(std::ref(app->config));
love["console"] = var(std::ref(app->console));
love["data"] = var(std::ref(app->data));
love["event"] = var(std::ref(app->event));
love["filesystem"] = var(std::ref(app->filesystem));
love["font"] = var(std::ref(app->font));
......@@ -316,9 +317,11 @@ script::script(const std::string& file) {
chai.add(fun<love::math&, math, int, int>(&math::setRandomSeed), "setRandomSeed");
chai.add(fun<love::math&, math, int>(&math::setRandomSeed), "setRandomSeed");
chai.add(fun(&math::getRandomSeed), "getRandomSeed");
chai.add(fun<std::string, math, const std::string&>(&math::compress), "compress");
chai.add(fun<std::string, math, const std::string&, int>(&math::compress), "compress");
chai.add(fun(&math::decompress), "decompress");
// Data
chai.add(fun<std::string, data, const std::string&>(&data::compress), "compress");
chai.add(fun<std::string, data, const std::string&, int>(&data::compress), "compress");
chai.add(fun(&data::decompress), "decompress");
// Ensure the love namespace is imported and ready.
chai.import("love");
......
var text = "Hello World! Hello World! Hello World!"
var compressed = love.data.compress(text)
assert_not_equal(text, compressed, "love.data.compress()")
var decompressed = love.data.decompress(compressed)
assert_equal(decompressed, text, "love.data.decompress()")
......@@ -10,6 +10,7 @@ def load() {
love.filesystem.load("assert")
love.filesystem.load("audio")
love.filesystem.load("console")
love.filesystem.load("data")
love.filesystem.load("filesystem")
love.filesystem.load("font")
love.filesystem.load("graphics")
......
......@@ -26,12 +26,5 @@ love.math.setRandomSeed(10)
assert(true, "love.math.setRandomSeed(10)")
assert_equal(love.math.getRandomSeed(), 10, "love.math.getRandomSeed()")
var text = "Hello World! Hello World! Hello World!"
var compressed = love.math.compress(text)
assert_not_equal(text, compressed, "love.math.compress()")
var decompressed = love.math.decompress(compressed)
assert_equal(decompressed, text, "love.math.decompress()")
assert(love.math.pi > 3.0f && love.math.pi < 4.0f, "love.math.pi")
assert(love.math.e > 2.0f, "love.math.e")
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment