Add a few MD5 and thread functions to the Lua API

develop
lethosor 2017-07-01 17:39:17 -04:00
parent 7a7f1df908
commit 6100074ba6
2 changed files with 76 additions and 0 deletions

@ -2090,6 +2090,24 @@ and are only documented here for completeness:
This requires an extension to be specified (``.lua`` or ``.rb``) - use
``dfhack.findScript()`` to include the ``.lua`` extension automatically.
* ``dfhack.internal.md5(string)``
Returns the MD5 hash of the given string.
* ``dfhack.internal.md5File(filename[,first_kb])``
Computes the MD5 hash of the given file. Returns ``hash, length`` on success
(where ``length`` is the number of bytes read from the file), or ``nil,
error`` on failure.
If the parameter ``first_kb`` is specified and evaluates to ``true``, and the
hash was computed successfully, a table containing the first 1024 bytes of the
file is returned as the third return value.
* ``dfhack.internal.threadid()``
Returns a numeric identifier of the current thread.
Core interpreter context
========================

@ -40,6 +40,8 @@ distribution.
#include "DataFuncs.h"
#include "DFHackVersion.h"
#include "PluginManager.h"
#include "tinythread.h"
#include "md5wrapper.h"
#include "modules/World.h"
#include "modules/Gui.h"
@ -2443,16 +2445,20 @@ static void *checkaddr(lua_State *L, int idx, bool allow_null = false)
return rv;
}
static md5wrapper md5_wrap;
static uintptr_t getImageBase() { return Core::getInstance().p->getBase(); }
static intptr_t getRebaseDelta() { return Core::getInstance().vinfo->getRebaseDelta(); }
static int8_t getModstate() { return Core::getInstance().getModstate(); }
static std::string internal_strerror(int n) { return strerror(n); }
static std::string internal_md5(std::string s) { return md5_wrap.getHashFromString(s); }
static const LuaWrapper::FunctionReg dfhack_internal_module[] = {
WRAP(getImageBase),
WRAP(getRebaseDelta),
WRAP(getModstate),
WRAPN(strerror, internal_strerror),
WRAPN(md5, internal_md5),
{ NULL, NULL }
};
@ -2855,6 +2861,56 @@ static int internal_findScript(lua_State *L)
return 1;
}
static int internal_threadid(lua_State *L)
{
std::stringstream ss;
ss << tthread::this_thread::get_id();
int i;
ss >> i;
lua_pushinteger(L, i);
return 1;
}
static int internal_md5file(lua_State *L)
{
const char *s = luaL_checkstring(L, 1);
uint32_t len;
char *first_kb_raw = nullptr;
std::vector<char> first_kb;
if (lua_toboolean(L, 2))
first_kb_raw = new char[1024];
std::string hash = md5_wrap.getHashFromFile(s, len, first_kb_raw);
bool err = (hash.find("file") != std::string::npos);
if (first_kb_raw)
{
first_kb.assign(first_kb_raw, first_kb_raw + 1024);
delete[] first_kb_raw;
}
if (err)
{
lua_pushnil(L);
lua_pushstring(L, hash.c_str());
return 2;
}
else
{
lua_pushstring(L, hash.c_str());
lua_pushinteger(L, len);
if (!first_kb.empty())
{
Lua::PushVector(L, first_kb);
return 3;
}
else
{
return 2;
}
}
}
static const luaL_Reg dfhack_internal_funcs[] = {
{ "getPE", internal_getPE },
{ "getMD5", internal_getmd5 },
@ -2876,6 +2932,8 @@ static const luaL_Reg dfhack_internal_funcs[] = {
{ "removeScriptPath", internal_removeScriptPath },
{ "getScriptPaths", internal_getScriptPaths },
{ "findScript", internal_findScript },
{ "threadid", internal_threadid },
{ "md5File", internal_md5file },
{ NULL, NULL }
};