@@ -22,6 +22,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
22
22
#include " common/c_converter.h"
23
23
#include " common/c_content.h"
24
24
#include " cpp_api/s_async.h"
25
+ #include " serialization.h"
25
26
#include " debug.h"
26
27
#include " porting.h"
27
28
#include " log.h"
@@ -283,6 +284,40 @@ int ModApiUtil::l_get_builtin_path(lua_State *L)
283
284
return 1 ;
284
285
}
285
286
287
+ // compress(data, method, level)
288
+ int ModApiUtil::l_compress (lua_State *L)
289
+ {
290
+ size_t size;
291
+ const char *data = luaL_checklstring (L, 1 , &size);
292
+
293
+ int level = -1 ;
294
+ if (!lua_isnone (L, 3 ) && !lua_isnil (L, 3 ))
295
+ level = luaL_checknumber (L, 3 );
296
+
297
+ std::ostringstream os;
298
+ compressZlib (std::string (data, size), os, level);
299
+
300
+ std::string out = os.str ();
301
+
302
+ lua_pushlstring (L, out.data (), out.size ());
303
+ return 1 ;
304
+ }
305
+
306
+ // decompress(data, method)
307
+ int ModApiUtil::l_decompress (lua_State *L)
308
+ {
309
+ size_t size;
310
+ const char * data = luaL_checklstring (L, 1 , &size);
311
+
312
+ std::istringstream is (std::string (data, size));
313
+ std::ostringstream os;
314
+ decompressZlib (is, os);
315
+
316
+ std::string out = os.str ();
317
+
318
+ lua_pushlstring (L, out.data (), out.size ());
319
+ return 1 ;
320
+ }
286
321
287
322
void ModApiUtil::Initialize (lua_State *L, int top)
288
323
{
@@ -306,6 +341,9 @@ void ModApiUtil::Initialize(lua_State *L, int top)
306
341
API_FCT (is_yes);
307
342
308
343
API_FCT (get_builtin_path);
344
+
345
+ API_FCT (compress);
346
+ API_FCT (decompress);
309
347
}
310
348
311
349
void ModApiUtil::InitializeAsync (AsyncEngine& engine)
@@ -325,5 +363,8 @@ void ModApiUtil::InitializeAsync(AsyncEngine& engine)
325
363
ASYNC_API_FCT (is_yes);
326
364
327
365
ASYNC_API_FCT (get_builtin_path);
366
+
367
+ ASYNC_API_FCT (compress);
368
+ ASYNC_API_FCT (decompress);
328
369
}
329
370
4 commit comments
4aiman commentedon Oct 31, 2014
What's the purpose of this addition?
(No offence, I really can't get it.)
ShadowNinja commentedon Oct 31, 2014
@4aiman: It's for mods that write a lot of data to the disk (or network with LuaSocket, if the endoint supports it).
For example this can be used to compress WorldEdit schematics.
4aiman commentedon Nov 5, 2014
So, I can write a mod to compress some schematics and use those compressed files within another mod?
ShadowNinja commentedon Nov 6, 2014
@4aiman: Yes, although you should provide an API for loading the schematics and not expose the underlying format.