Merge branch 'master' of git://github.com/peterix/dfhack
commit
b21d813c73
@ -1,17 +0,0 @@
|
|||||||
#ifndef BIT_H
|
|
||||||
#define BIT_H
|
|
||||||
|
|
||||||
#define LUA_BITOP_VERSION "1.0.1"
|
|
||||||
|
|
||||||
#define LUA_LIB
|
|
||||||
#include "lua.h"
|
|
||||||
#include "lauxlib.h"
|
|
||||||
#ifdef __cplusplus
|
|
||||||
extern "C" {
|
|
||||||
#endif
|
|
||||||
LUALIB_API int luaopen_bit(lua_State *L);
|
|
||||||
#ifdef __cplusplus
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
#endif // BIT_H
|
|
||||||
|
|
@ -0,0 +1,95 @@
|
|||||||
|
/*
|
||||||
|
** $Id: lctype.h,v 1.12 2011/07/15 12:50:29 roberto Exp $
|
||||||
|
** 'ctype' functions for Lua
|
||||||
|
** See Copyright Notice in lua.h
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef lctype_h
|
||||||
|
#define lctype_h
|
||||||
|
|
||||||
|
#include "lua.h"
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** WARNING: the functions defined here do not necessarily correspond
|
||||||
|
** to the similar functions in the standard C ctype.h. They are
|
||||||
|
** optimized for the specific needs of Lua
|
||||||
|
*/
|
||||||
|
|
||||||
|
#if !defined(LUA_USE_CTYPE)
|
||||||
|
|
||||||
|
#if 'A' == 65 && '0' == 48
|
||||||
|
/* ASCII case: can use its own tables; faster and fixed */
|
||||||
|
#define LUA_USE_CTYPE 0
|
||||||
|
#else
|
||||||
|
/* must use standard C ctype */
|
||||||
|
#define LUA_USE_CTYPE 1
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
#if !LUA_USE_CTYPE /* { */
|
||||||
|
|
||||||
|
#include <limits.h>
|
||||||
|
|
||||||
|
#include "llimits.h"
|
||||||
|
|
||||||
|
|
||||||
|
#define ALPHABIT 0
|
||||||
|
#define DIGITBIT 1
|
||||||
|
#define PRINTBIT 2
|
||||||
|
#define SPACEBIT 3
|
||||||
|
#define XDIGITBIT 4
|
||||||
|
|
||||||
|
|
||||||
|
#define MASK(B) (1 << (B))
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** add 1 to char to allow index -1 (EOZ)
|
||||||
|
*/
|
||||||
|
#define testprop(c,p) (luai_ctype_[(c)+1] & (p))
|
||||||
|
|
||||||
|
/*
|
||||||
|
** 'lalpha' (Lua alphabetic) and 'lalnum' (Lua alphanumeric) both include '_'
|
||||||
|
*/
|
||||||
|
#define lislalpha(c) testprop(c, MASK(ALPHABIT))
|
||||||
|
#define lislalnum(c) testprop(c, (MASK(ALPHABIT) | MASK(DIGITBIT)))
|
||||||
|
#define lisdigit(c) testprop(c, MASK(DIGITBIT))
|
||||||
|
#define lisspace(c) testprop(c, MASK(SPACEBIT))
|
||||||
|
#define lisprint(c) testprop(c, MASK(PRINTBIT))
|
||||||
|
#define lisxdigit(c) testprop(c, MASK(XDIGITBIT))
|
||||||
|
|
||||||
|
/*
|
||||||
|
** this 'ltolower' only works for alphabetic characters
|
||||||
|
*/
|
||||||
|
#define ltolower(c) ((c) | ('A' ^ 'a'))
|
||||||
|
|
||||||
|
|
||||||
|
/* two more entries for 0 and -1 (EOZ) */
|
||||||
|
LUAI_DDEC const lu_byte luai_ctype_[UCHAR_MAX + 2];
|
||||||
|
|
||||||
|
|
||||||
|
#else /* }{ */
|
||||||
|
|
||||||
|
/*
|
||||||
|
** use standard C ctypes
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <ctype.h>
|
||||||
|
|
||||||
|
|
||||||
|
#define lislalpha(c) (isalpha(c) || (c) == '_')
|
||||||
|
#define lislalnum(c) (isalnum(c) || (c) == '_')
|
||||||
|
#define lisdigit(c) (isdigit(c))
|
||||||
|
#define lisspace(c) (isspace(c))
|
||||||
|
#define lisprint(c) (isprint(c))
|
||||||
|
#define lisxdigit(c) (isxdigit(c))
|
||||||
|
|
||||||
|
#define ltolower(c) (tolower(c))
|
||||||
|
|
||||||
|
#endif /* } */
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
@ -1,181 +0,0 @@
|
|||||||
/*
|
|
||||||
** Lua BitOp -- a bit operations library for Lua 5.1.
|
|
||||||
** http://bitop.luajit.org/
|
|
||||||
**
|
|
||||||
** Copyright (C) 2008-2009 Mike Pall. All rights reserved.
|
|
||||||
**
|
|
||||||
** Permission is hereby granted, free of charge, to any person obtaining
|
|
||||||
** a copy of this software and associated documentation files (the
|
|
||||||
** "Software"), to deal in the Software without restriction, including
|
|
||||||
** without limitation the rights to use, copy, modify, merge, publish,
|
|
||||||
** distribute, sublicense, and/or sell copies of the Software, and to
|
|
||||||
** permit persons to whom the Software is furnished to do so, subject to
|
|
||||||
** the following conditions:
|
|
||||||
**
|
|
||||||
** The above copyright notice and this permission notice shall be
|
|
||||||
** included in all copies or substantial portions of the Software.
|
|
||||||
**
|
|
||||||
** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
||||||
** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
||||||
** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
||||||
** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
|
||||||
** CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
|
||||||
** TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
|
||||||
** SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
||||||
**
|
|
||||||
** [ MIT license: http://www.opensource.org/licenses/mit-license.php ]
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include "bit.h"
|
|
||||||
#ifdef __cplusplus
|
|
||||||
extern "C" {
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#ifdef _MSC_VER
|
|
||||||
/* MSVC is stuck in the last century and doesn't have C99's stdint.h. */
|
|
||||||
typedef __int32 int32_t;
|
|
||||||
typedef unsigned __int32 uint32_t;
|
|
||||||
typedef unsigned __int64 uint64_t;
|
|
||||||
#else
|
|
||||||
#include <stdint.h>
|
|
||||||
#endif
|
|
||||||
|
|
||||||
typedef int32_t SBits;
|
|
||||||
typedef uint32_t UBits;
|
|
||||||
|
|
||||||
typedef union {
|
|
||||||
lua_Number n;
|
|
||||||
#ifdef LUA_NUMBER_DOUBLE
|
|
||||||
uint64_t b;
|
|
||||||
#else
|
|
||||||
UBits b;
|
|
||||||
#endif
|
|
||||||
} BitNum;
|
|
||||||
|
|
||||||
/* Convert argument to bit type. */
|
|
||||||
static UBits barg(lua_State *L, int idx)
|
|
||||||
{
|
|
||||||
BitNum bn;
|
|
||||||
UBits b;
|
|
||||||
bn.n = lua_tonumber(L, idx);
|
|
||||||
#if defined(LUA_NUMBER_DOUBLE)
|
|
||||||
bn.n += 6755399441055744.0; /* 2^52+2^51 */
|
|
||||||
#ifdef SWAPPED_DOUBLE
|
|
||||||
b = (UBits)(bn.b >> 32);
|
|
||||||
#else
|
|
||||||
b = (UBits)bn.b;
|
|
||||||
#endif
|
|
||||||
#elif defined(LUA_NUMBER_INT) || defined(LUA_NUMBER_LONG) || \
|
|
||||||
defined(LUA_NUMBER_LONGLONG) || defined(LUA_NUMBER_LONG_LONG) || \
|
|
||||||
defined(LUA_NUMBER_LLONG)
|
|
||||||
if (sizeof(UBits) == sizeof(lua_Number))
|
|
||||||
b = bn.b;
|
|
||||||
else
|
|
||||||
b = (UBits)(SBits)bn.n;
|
|
||||||
#elif defined(LUA_NUMBER_FLOAT)
|
|
||||||
#error "A 'float' lua_Number type is incompatible with this library"
|
|
||||||
#else
|
|
||||||
#error "Unknown number type, check LUA_NUMBER_* in luaconf.h"
|
|
||||||
#endif
|
|
||||||
if (b == 0 && !lua_isnumber(L, idx))
|
|
||||||
luaL_typerror(L, idx, "number");
|
|
||||||
return b;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Return bit type. */
|
|
||||||
#define BRET(b) lua_pushnumber(L, (lua_Number)(SBits)(b)); return 1;
|
|
||||||
|
|
||||||
static int bit_tobit(lua_State *L) { BRET(barg(L, 1)) }
|
|
||||||
static int bit_bnot(lua_State *L) { BRET(~barg(L, 1)) }
|
|
||||||
|
|
||||||
#define BIT_OP(func, opr) \
|
|
||||||
static int func(lua_State *L) { int i; UBits b = barg(L, 1); \
|
|
||||||
for (i = lua_gettop(L); i > 1; i--) b opr barg(L, i); BRET(b) }
|
|
||||||
BIT_OP(bit_band, &=)
|
|
||||||
BIT_OP(bit_bor, |=)
|
|
||||||
BIT_OP(bit_bxor, ^=)
|
|
||||||
|
|
||||||
#define bshl(b, n) (b << n)
|
|
||||||
#define bshr(b, n) (b >> n)
|
|
||||||
#define bsar(b, n) ((SBits)b >> n)
|
|
||||||
#define brol(b, n) ((b << n) | (b >> (32-n)))
|
|
||||||
#define bror(b, n) ((b << (32-n)) | (b >> n))
|
|
||||||
#define BIT_SH(func, fn) \
|
|
||||||
static int func(lua_State *L) { \
|
|
||||||
UBits b = barg(L, 1); UBits n = barg(L, 2) & 31; BRET(fn(b, n)) }
|
|
||||||
BIT_SH(bit_lshift, bshl)
|
|
||||||
BIT_SH(bit_rshift, bshr)
|
|
||||||
BIT_SH(bit_arshift, bsar)
|
|
||||||
BIT_SH(bit_rol, brol)
|
|
||||||
BIT_SH(bit_ror, bror)
|
|
||||||
|
|
||||||
static int bit_bswap(lua_State *L)
|
|
||||||
{
|
|
||||||
UBits b = barg(L, 1);
|
|
||||||
b = (b >> 24) | ((b >> 8) & 0xff00) | ((b & 0xff00) << 8) | (b << 24);
|
|
||||||
BRET(b)
|
|
||||||
}
|
|
||||||
|
|
||||||
static int bit_tohex(lua_State *L)
|
|
||||||
{
|
|
||||||
UBits b = barg(L, 1);
|
|
||||||
SBits n = lua_isnone(L, 2) ? 8 : (SBits)barg(L, 2);
|
|
||||||
const char *hexdigits = "0123456789abcdef";
|
|
||||||
char buf[8];
|
|
||||||
int i;
|
|
||||||
if (n < 0) { n = -n; hexdigits = "0123456789ABCDEF"; }
|
|
||||||
if (n > 8) n = 8;
|
|
||||||
for (i = (int)n; --i >= 0; ) { buf[i] = hexdigits[b & 15]; b >>= 4; }
|
|
||||||
lua_pushlstring(L, buf, (size_t)n);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
static const struct luaL_Reg bit_funcs[] = {
|
|
||||||
{ "tobit", bit_tobit },
|
|
||||||
{ "bnot", bit_bnot },
|
|
||||||
{ "band", bit_band },
|
|
||||||
{ "bor", bit_bor },
|
|
||||||
{ "bxor", bit_bxor },
|
|
||||||
{ "lshift", bit_lshift },
|
|
||||||
{ "rshift", bit_rshift },
|
|
||||||
{ "arshift", bit_arshift },
|
|
||||||
{ "rol", bit_rol },
|
|
||||||
{ "ror", bit_ror },
|
|
||||||
{ "bswap", bit_bswap },
|
|
||||||
{ "tohex", bit_tohex },
|
|
||||||
{ NULL, NULL }
|
|
||||||
};
|
|
||||||
|
|
||||||
/* Signed right-shifts are implementation-defined per C89/C99.
|
|
||||||
** But the de facto standard are arithmetic right-shifts on two's
|
|
||||||
** complement CPUs. This behaviour is required here, so test for it.
|
|
||||||
*/
|
|
||||||
#define BAD_SAR (bsar(-8, 2) != (SBits)-2)
|
|
||||||
|
|
||||||
LUALIB_API int luaopen_bit(lua_State *L)
|
|
||||||
{
|
|
||||||
UBits b;
|
|
||||||
lua_pushnumber(L, (lua_Number)1437217655L);
|
|
||||||
b = barg(L, -1);
|
|
||||||
if (b != (UBits)1437217655L || BAD_SAR) { /* Perform a simple self-test. */
|
|
||||||
const char *msg = "compiled with incompatible luaconf.h";
|
|
||||||
#ifdef LUA_NUMBER_DOUBLE
|
|
||||||
#ifdef _WIN32
|
|
||||||
if (b == (UBits)1610612736L)
|
|
||||||
msg = "use D3DCREATE_FPU_PRESERVE with DirectX";
|
|
||||||
#endif
|
|
||||||
if (b == (UBits)1127743488L)
|
|
||||||
msg = "not compiled with SWAPPED_DOUBLE";
|
|
||||||
#endif
|
|
||||||
if (BAD_SAR)
|
|
||||||
msg = "arithmetic right-shift broken";
|
|
||||||
luaL_error(L, "bit library self-test failed (%s)", msg);
|
|
||||||
}
|
|
||||||
luaL_register(L, "bit", bit_funcs);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
#ifdef __cplusplus
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
|
|
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,209 @@
|
|||||||
|
/*
|
||||||
|
** $Id: lbitlib.c,v 1.16 2011/06/20 16:35:23 roberto Exp $
|
||||||
|
** Standard library for bitwise operations
|
||||||
|
** See Copyright Notice in lua.h
|
||||||
|
*/
|
||||||
|
|
||||||
|
#define lbitlib_c
|
||||||
|
#define LUA_LIB
|
||||||
|
|
||||||
|
#include "lua.h"
|
||||||
|
|
||||||
|
#include "lauxlib.h"
|
||||||
|
#include "lualib.h"
|
||||||
|
|
||||||
|
|
||||||
|
/* number of bits to consider in a number */
|
||||||
|
#if !defined(LUA_NBITS)
|
||||||
|
#define LUA_NBITS 32
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
#define ALLONES (~(((~(lua_Unsigned)0) << (LUA_NBITS - 1)) << 1))
|
||||||
|
|
||||||
|
/* macro to trim extra bits */
|
||||||
|
#define trim(x) ((x) & ALLONES)
|
||||||
|
|
||||||
|
|
||||||
|
/* builds a number with 'n' ones (1 <= n <= LUA_NBITS) */
|
||||||
|
#define mask(n) (~((ALLONES << 1) << ((n) - 1)))
|
||||||
|
|
||||||
|
|
||||||
|
typedef lua_Unsigned b_uint;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
static b_uint andaux (lua_State *L) {
|
||||||
|
int i, n = lua_gettop(L);
|
||||||
|
b_uint r = ~(b_uint)0;
|
||||||
|
for (i = 1; i <= n; i++)
|
||||||
|
r &= luaL_checkunsigned(L, i);
|
||||||
|
return trim(r);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int b_and (lua_State *L) {
|
||||||
|
b_uint r = andaux(L);
|
||||||
|
lua_pushunsigned(L, r);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int b_test (lua_State *L) {
|
||||||
|
b_uint r = andaux(L);
|
||||||
|
lua_pushboolean(L, r != 0);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int b_or (lua_State *L) {
|
||||||
|
int i, n = lua_gettop(L);
|
||||||
|
b_uint r = 0;
|
||||||
|
for (i = 1; i <= n; i++)
|
||||||
|
r |= luaL_checkunsigned(L, i);
|
||||||
|
lua_pushunsigned(L, trim(r));
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int b_xor (lua_State *L) {
|
||||||
|
int i, n = lua_gettop(L);
|
||||||
|
b_uint r = 0;
|
||||||
|
for (i = 1; i <= n; i++)
|
||||||
|
r ^= luaL_checkunsigned(L, i);
|
||||||
|
lua_pushunsigned(L, trim(r));
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int b_not (lua_State *L) {
|
||||||
|
b_uint r = ~luaL_checkunsigned(L, 1);
|
||||||
|
lua_pushunsigned(L, trim(r));
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int b_shift (lua_State *L, b_uint r, int i) {
|
||||||
|
if (i < 0) { /* shift right? */
|
||||||
|
i = -i;
|
||||||
|
r = trim(r);
|
||||||
|
if (i >= LUA_NBITS) r = 0;
|
||||||
|
else r >>= i;
|
||||||
|
}
|
||||||
|
else { /* shift left */
|
||||||
|
if (i >= LUA_NBITS) r = 0;
|
||||||
|
else r <<= i;
|
||||||
|
r = trim(r);
|
||||||
|
}
|
||||||
|
lua_pushunsigned(L, r);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int b_lshift (lua_State *L) {
|
||||||
|
return b_shift(L, luaL_checkunsigned(L, 1), luaL_checkint(L, 2));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int b_rshift (lua_State *L) {
|
||||||
|
return b_shift(L, luaL_checkunsigned(L, 1), -luaL_checkint(L, 2));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int b_arshift (lua_State *L) {
|
||||||
|
b_uint r = luaL_checkunsigned(L, 1);
|
||||||
|
int i = luaL_checkint(L, 2);
|
||||||
|
if (i < 0 || !(r & ((b_uint)1 << (LUA_NBITS - 1))))
|
||||||
|
return b_shift(L, r, -i);
|
||||||
|
else { /* arithmetic shift for 'negative' number */
|
||||||
|
if (i >= LUA_NBITS) r = ALLONES;
|
||||||
|
else
|
||||||
|
r = trim((r >> i) | ~(~(b_uint)0 >> i)); /* add signal bit */
|
||||||
|
lua_pushunsigned(L, r);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int b_rot (lua_State *L, int i) {
|
||||||
|
b_uint r = luaL_checkunsigned(L, 1);
|
||||||
|
i &= (LUA_NBITS - 1); /* i = i % NBITS */
|
||||||
|
r = trim(r);
|
||||||
|
r = (r << i) | (r >> (LUA_NBITS - i));
|
||||||
|
lua_pushunsigned(L, trim(r));
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int b_lrot (lua_State *L) {
|
||||||
|
return b_rot(L, luaL_checkint(L, 2));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int b_rrot (lua_State *L) {
|
||||||
|
return b_rot(L, -luaL_checkint(L, 2));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** get field and width arguments for field-manipulation functions,
|
||||||
|
** checking whether they are valid
|
||||||
|
*/
|
||||||
|
static int fieldargs (lua_State *L, int farg, int *width) {
|
||||||
|
int f = luaL_checkint(L, farg);
|
||||||
|
int w = luaL_optint(L, farg + 1, 1);
|
||||||
|
luaL_argcheck(L, 0 <= f, farg, "field cannot be negative");
|
||||||
|
luaL_argcheck(L, 0 < w, farg + 1, "width must be positive");
|
||||||
|
if (f + w > LUA_NBITS)
|
||||||
|
luaL_error(L, "trying to access non-existent bits");
|
||||||
|
*width = w;
|
||||||
|
return f;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int b_extract (lua_State *L) {
|
||||||
|
int w;
|
||||||
|
b_uint r = luaL_checkunsigned(L, 1);
|
||||||
|
int f = fieldargs(L, 2, &w);
|
||||||
|
r = (r >> f) & mask(w);
|
||||||
|
lua_pushunsigned(L, r);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int b_replace (lua_State *L) {
|
||||||
|
int w;
|
||||||
|
b_uint r = luaL_checkunsigned(L, 1);
|
||||||
|
b_uint v = luaL_checkunsigned(L, 2);
|
||||||
|
int f = fieldargs(L, 3, &w);
|
||||||
|
int m = mask(w);
|
||||||
|
v &= m; /* erase bits outside given width */
|
||||||
|
r = (r & ~(m << f)) | (v << f);
|
||||||
|
lua_pushunsigned(L, r);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static const luaL_Reg bitlib[] = {
|
||||||
|
{"arshift", b_arshift},
|
||||||
|
{"band", b_and},
|
||||||
|
{"bnot", b_not},
|
||||||
|
{"bor", b_or},
|
||||||
|
{"bxor", b_xor},
|
||||||
|
{"btest", b_test},
|
||||||
|
{"extract", b_extract},
|
||||||
|
{"lrotate", b_lrot},
|
||||||
|
{"lshift", b_lshift},
|
||||||
|
{"replace", b_replace},
|
||||||
|
{"rrotate", b_rrot},
|
||||||
|
{"rshift", b_rshift},
|
||||||
|
{NULL, NULL}
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
LUAMOD_API int luaopen_bit32 (lua_State *L) {
|
||||||
|
luaL_newlib(L, bitlib);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,154 @@
|
|||||||
|
/*
|
||||||
|
** $Id: lcorolib.c,v 1.3 2011/08/23 17:24:34 roberto Exp $
|
||||||
|
** Coroutine Library
|
||||||
|
** See Copyright Notice in lua.h
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
|
||||||
|
#define lcorolib_c
|
||||||
|
#define LUA_LIB
|
||||||
|
|
||||||
|
#include "lua.h"
|
||||||
|
|
||||||
|
#include "lauxlib.h"
|
||||||
|
#include "lualib.h"
|
||||||
|
|
||||||
|
|
||||||
|
static int auxresume (lua_State *L, lua_State *co, int narg) {
|
||||||
|
int status;
|
||||||
|
if (!lua_checkstack(co, narg)) {
|
||||||
|
lua_pushliteral(L, "too many arguments to resume");
|
||||||
|
return -1; /* error flag */
|
||||||
|
}
|
||||||
|
if (lua_status(co) == LUA_OK && lua_gettop(co) == 0) {
|
||||||
|
lua_pushliteral(L, "cannot resume dead coroutine");
|
||||||
|
return -1; /* error flag */
|
||||||
|
}
|
||||||
|
lua_xmove(L, co, narg);
|
||||||
|
status = lua_resume(co, L, narg);
|
||||||
|
if (status == LUA_OK || status == LUA_YIELD) {
|
||||||
|
int nres = lua_gettop(co);
|
||||||
|
if (!lua_checkstack(L, nres + 1)) {
|
||||||
|
lua_pop(co, nres); /* remove results anyway */
|
||||||
|
lua_pushliteral(L, "too many results to resume");
|
||||||
|
return -1; /* error flag */
|
||||||
|
}
|
||||||
|
lua_xmove(co, L, nres); /* move yielded values */
|
||||||
|
return nres;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
lua_xmove(co, L, 1); /* move error message */
|
||||||
|
return -1; /* error flag */
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int luaB_coresume (lua_State *L) {
|
||||||
|
lua_State *co = lua_tothread(L, 1);
|
||||||
|
int r;
|
||||||
|
luaL_argcheck(L, co, 1, "coroutine expected");
|
||||||
|
r = auxresume(L, co, lua_gettop(L) - 1);
|
||||||
|
if (r < 0) {
|
||||||
|
lua_pushboolean(L, 0);
|
||||||
|
lua_insert(L, -2);
|
||||||
|
return 2; /* return false + error message */
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
lua_pushboolean(L, 1);
|
||||||
|
lua_insert(L, -(r + 1));
|
||||||
|
return r + 1; /* return true + `resume' returns */
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int luaB_auxwrap (lua_State *L) {
|
||||||
|
lua_State *co = lua_tothread(L, lua_upvalueindex(1));
|
||||||
|
int r = auxresume(L, co, lua_gettop(L));
|
||||||
|
if (r < 0) {
|
||||||
|
if (lua_isstring(L, -1)) { /* error object is a string? */
|
||||||
|
luaL_where(L, 1); /* add extra info */
|
||||||
|
lua_insert(L, -2);
|
||||||
|
lua_concat(L, 2);
|
||||||
|
}
|
||||||
|
lua_error(L); /* propagate error */
|
||||||
|
}
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int luaB_cocreate (lua_State *L) {
|
||||||
|
lua_State *NL = lua_newthread(L);
|
||||||
|
luaL_checktype(L, 1, LUA_TFUNCTION);
|
||||||
|
lua_pushvalue(L, 1); /* move function to top */
|
||||||
|
lua_xmove(L, NL, 1); /* move function from L to NL */
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int luaB_cowrap (lua_State *L) {
|
||||||
|
luaB_cocreate(L);
|
||||||
|
lua_pushcclosure(L, luaB_auxwrap, 1);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int luaB_yield (lua_State *L) {
|
||||||
|
return lua_yield(L, lua_gettop(L));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int luaB_costatus (lua_State *L) {
|
||||||
|
lua_State *co = lua_tothread(L, 1);
|
||||||
|
luaL_argcheck(L, co, 1, "coroutine expected");
|
||||||
|
if (L == co) lua_pushliteral(L, "running");
|
||||||
|
else {
|
||||||
|
switch (lua_status(co)) {
|
||||||
|
case LUA_YIELD:
|
||||||
|
lua_pushliteral(L, "suspended");
|
||||||
|
break;
|
||||||
|
case LUA_OK: {
|
||||||
|
lua_Debug ar;
|
||||||
|
if (lua_getstack(co, 0, &ar) > 0) /* does it have frames? */
|
||||||
|
lua_pushliteral(L, "normal"); /* it is running */
|
||||||
|
else if (lua_gettop(co) == 0)
|
||||||
|
lua_pushliteral(L, "dead");
|
||||||
|
else
|
||||||
|
lua_pushliteral(L, "suspended"); /* initial state */
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
default: /* some error occurred */
|
||||||
|
lua_pushliteral(L, "dead");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int luaB_corunning (lua_State *L) {
|
||||||
|
int ismain = lua_pushthread(L);
|
||||||
|
lua_pushboolean(L, ismain);
|
||||||
|
return 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static const luaL_Reg co_funcs[] = {
|
||||||
|
{"create", luaB_cocreate},
|
||||||
|
{"resume", luaB_coresume},
|
||||||
|
{"running", luaB_corunning},
|
||||||
|
{"status", luaB_costatus},
|
||||||
|
{"wrap", luaB_cowrap},
|
||||||
|
{"yield", luaB_yield},
|
||||||
|
{NULL, NULL}
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
LUAMOD_API int luaopen_coroutine (lua_State *L) {
|
||||||
|
luaL_newlib(L, co_funcs);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,52 @@
|
|||||||
|
/*
|
||||||
|
** $Id: lctype.c,v 1.11 2011/10/03 16:19:23 roberto Exp $
|
||||||
|
** 'ctype' functions for Lua
|
||||||
|
** See Copyright Notice in lua.h
|
||||||
|
*/
|
||||||
|
|
||||||
|
#define lctype_c
|
||||||
|
#define LUA_CORE
|
||||||
|
|
||||||
|
#include "lctype.h"
|
||||||
|
|
||||||
|
#if !LUA_USE_CTYPE /* { */
|
||||||
|
|
||||||
|
#include <limits.h>
|
||||||
|
|
||||||
|
LUAI_DDEF const lu_byte luai_ctype_[UCHAR_MAX + 2] = {
|
||||||
|
0x00, /* EOZ */
|
||||||
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0. */
|
||||||
|
0x00, 0x08, 0x08, 0x08, 0x08, 0x08, 0x00, 0x00,
|
||||||
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 1. */
|
||||||
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||||
|
0x0c, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, /* 2. */
|
||||||
|
0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04,
|
||||||
|
0x16, 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, /* 3. */
|
||||||
|
0x16, 0x16, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04,
|
||||||
|
0x04, 0x15, 0x15, 0x15, 0x15, 0x15, 0x15, 0x05, /* 4. */
|
||||||
|
0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
|
||||||
|
0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, /* 5. */
|
||||||
|
0x05, 0x05, 0x05, 0x04, 0x04, 0x04, 0x04, 0x05,
|
||||||
|
0x04, 0x15, 0x15, 0x15, 0x15, 0x15, 0x15, 0x05, /* 6. */
|
||||||
|
0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
|
||||||
|
0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, /* 7. */
|
||||||
|
0x05, 0x05, 0x05, 0x04, 0x04, 0x04, 0x04, 0x00,
|
||||||
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 8. */
|
||||||
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||||
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 9. */
|
||||||
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||||
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* a. */
|
||||||
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||||
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* b. */
|
||||||
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||||
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* c. */
|
||||||
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||||
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* d. */
|
||||||
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||||
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* e. */
|
||||||
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||||
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* f. */
|
||||||
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif /* } */
|
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@ -1,227 +0,0 @@
|
|||||||
/*
|
|
||||||
** $Id: print.c,v 1.55a 2006/05/31 13:30:05 lhf Exp $
|
|
||||||
** print bytecodes
|
|
||||||
** See Copyright Notice in lua.h
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include <ctype.h>
|
|
||||||
#include <stdio.h>
|
|
||||||
|
|
||||||
#define luac_c
|
|
||||||
#define LUA_CORE
|
|
||||||
|
|
||||||
#include "ldebug.h"
|
|
||||||
#include "lobject.h"
|
|
||||||
#include "lopcodes.h"
|
|
||||||
#include "lundump.h"
|
|
||||||
|
|
||||||
#define PrintFunction luaU_print
|
|
||||||
|
|
||||||
#define Sizeof(x) ((int)sizeof(x))
|
|
||||||
#define VOID(p) ((const void*)(p))
|
|
||||||
|
|
||||||
static void PrintString(const TString* ts)
|
|
||||||
{
|
|
||||||
const char* s=getstr(ts);
|
|
||||||
size_t i,n=ts->tsv.len;
|
|
||||||
putchar('"');
|
|
||||||
for (i=0; i<n; i++)
|
|
||||||
{
|
|
||||||
int c=s[i];
|
|
||||||
switch (c)
|
|
||||||
{
|
|
||||||
case '"': printf("\\\""); break;
|
|
||||||
case '\\': printf("\\\\"); break;
|
|
||||||
case '\a': printf("\\a"); break;
|
|
||||||
case '\b': printf("\\b"); break;
|
|
||||||
case '\f': printf("\\f"); break;
|
|
||||||
case '\n': printf("\\n"); break;
|
|
||||||
case '\r': printf("\\r"); break;
|
|
||||||
case '\t': printf("\\t"); break;
|
|
||||||
case '\v': printf("\\v"); break;
|
|
||||||
default: if (isprint((unsigned char)c))
|
|
||||||
putchar(c);
|
|
||||||
else
|
|
||||||
printf("\\%03u",(unsigned char)c);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
putchar('"');
|
|
||||||
}
|
|
||||||
|
|
||||||
static void PrintConstant(const Proto* f, int i)
|
|
||||||
{
|
|
||||||
const TValue* o=&f->k[i];
|
|
||||||
switch (ttype(o))
|
|
||||||
{
|
|
||||||
case LUA_TNIL:
|
|
||||||
printf("nil");
|
|
||||||
break;
|
|
||||||
case LUA_TBOOLEAN:
|
|
||||||
printf(bvalue(o) ? "true" : "false");
|
|
||||||
break;
|
|
||||||
case LUA_TNUMBER:
|
|
||||||
printf(LUA_NUMBER_FMT,nvalue(o));
|
|
||||||
break;
|
|
||||||
case LUA_TSTRING:
|
|
||||||
PrintString(rawtsvalue(o));
|
|
||||||
break;
|
|
||||||
default: /* cannot happen */
|
|
||||||
printf("? type=%d",ttype(o));
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static void PrintCode(const Proto* f)
|
|
||||||
{
|
|
||||||
const Instruction* code=f->code;
|
|
||||||
int pc,n=f->sizecode;
|
|
||||||
for (pc=0; pc<n; pc++)
|
|
||||||
{
|
|
||||||
Instruction i=code[pc];
|
|
||||||
OpCode o=GET_OPCODE(i);
|
|
||||||
int a=GETARG_A(i);
|
|
||||||
int b=GETARG_B(i);
|
|
||||||
int c=GETARG_C(i);
|
|
||||||
int bx=GETARG_Bx(i);
|
|
||||||
int sbx=GETARG_sBx(i);
|
|
||||||
int line=getline(f,pc);
|
|
||||||
printf("\t%d\t",pc+1);
|
|
||||||
if (line>0) printf("[%d]\t",line); else printf("[-]\t");
|
|
||||||
printf("%-9s\t",luaP_opnames[o]);
|
|
||||||
switch (getOpMode(o))
|
|
||||||
{
|
|
||||||
case iABC:
|
|
||||||
printf("%d",a);
|
|
||||||
if (getBMode(o)!=OpArgN) printf(" %d",ISK(b) ? (-1-INDEXK(b)) : b);
|
|
||||||
if (getCMode(o)!=OpArgN) printf(" %d",ISK(c) ? (-1-INDEXK(c)) : c);
|
|
||||||
break;
|
|
||||||
case iABx:
|
|
||||||
if (getBMode(o)==OpArgK) printf("%d %d",a,-1-bx); else printf("%d %d",a,bx);
|
|
||||||
break;
|
|
||||||
case iAsBx:
|
|
||||||
if (o==OP_JMP) printf("%d",sbx); else printf("%d %d",a,sbx);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
switch (o)
|
|
||||||
{
|
|
||||||
case OP_LOADK:
|
|
||||||
printf("\t; "); PrintConstant(f,bx);
|
|
||||||
break;
|
|
||||||
case OP_GETUPVAL:
|
|
||||||
case OP_SETUPVAL:
|
|
||||||
printf("\t; %s", (f->sizeupvalues>0) ? getstr(f->upvalues[b]) : "-");
|
|
||||||
break;
|
|
||||||
case OP_GETGLOBAL:
|
|
||||||
case OP_SETGLOBAL:
|
|
||||||
printf("\t; %s",svalue(&f->k[bx]));
|
|
||||||
break;
|
|
||||||
case OP_GETTABLE:
|
|
||||||
case OP_SELF:
|
|
||||||
if (ISK(c)) { printf("\t; "); PrintConstant(f,INDEXK(c)); }
|
|
||||||
break;
|
|
||||||
case OP_SETTABLE:
|
|
||||||
case OP_ADD:
|
|
||||||
case OP_SUB:
|
|
||||||
case OP_MUL:
|
|
||||||
case OP_DIV:
|
|
||||||
case OP_POW:
|
|
||||||
case OP_EQ:
|
|
||||||
case OP_LT:
|
|
||||||
case OP_LE:
|
|
||||||
if (ISK(b) || ISK(c))
|
|
||||||
{
|
|
||||||
printf("\t; ");
|
|
||||||
if (ISK(b)) PrintConstant(f,INDEXK(b)); else printf("-");
|
|
||||||
printf(" ");
|
|
||||||
if (ISK(c)) PrintConstant(f,INDEXK(c)); else printf("-");
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case OP_JMP:
|
|
||||||
case OP_FORLOOP:
|
|
||||||
case OP_FORPREP:
|
|
||||||
printf("\t; to %d",sbx+pc+2);
|
|
||||||
break;
|
|
||||||
case OP_CLOSURE:
|
|
||||||
printf("\t; %p",VOID(f->p[bx]));
|
|
||||||
break;
|
|
||||||
case OP_SETLIST:
|
|
||||||
if (c==0) printf("\t; %d",(int)code[++pc]);
|
|
||||||
else printf("\t; %d",c);
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
printf("\n");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#define SS(x) (x==1)?"":"s"
|
|
||||||
#define S(x) x,SS(x)
|
|
||||||
|
|
||||||
static void PrintHeader(const Proto* f)
|
|
||||||
{
|
|
||||||
const char* s=getstr(f->source);
|
|
||||||
if (*s=='@' || *s=='=')
|
|
||||||
s++;
|
|
||||||
else if (*s==LUA_SIGNATURE[0])
|
|
||||||
s="(bstring)";
|
|
||||||
else
|
|
||||||
s="(string)";
|
|
||||||
printf("\n%s <%s:%d,%d> (%d instruction%s, %d bytes at %p)\n",
|
|
||||||
(f->linedefined==0)?"main":"function",s,
|
|
||||||
f->linedefined,f->lastlinedefined,
|
|
||||||
S(f->sizecode),f->sizecode*Sizeof(Instruction),VOID(f));
|
|
||||||
printf("%d%s param%s, %d slot%s, %d upvalue%s, ",
|
|
||||||
f->numparams,f->is_vararg?"+":"",SS(f->numparams),
|
|
||||||
S(f->maxstacksize),S(f->nups));
|
|
||||||
printf("%d local%s, %d constant%s, %d function%s\n",
|
|
||||||
S(f->sizelocvars),S(f->sizek),S(f->sizep));
|
|
||||||
}
|
|
||||||
|
|
||||||
static void PrintConstants(const Proto* f)
|
|
||||||
{
|
|
||||||
int i,n=f->sizek;
|
|
||||||
printf("constants (%d) for %p:\n",n,VOID(f));
|
|
||||||
for (i=0; i<n; i++)
|
|
||||||
{
|
|
||||||
printf("\t%d\t",i+1);
|
|
||||||
PrintConstant(f,i);
|
|
||||||
printf("\n");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static void PrintLocals(const Proto* f)
|
|
||||||
{
|
|
||||||
int i,n=f->sizelocvars;
|
|
||||||
printf("locals (%d) for %p:\n",n,VOID(f));
|
|
||||||
for (i=0; i<n; i++)
|
|
||||||
{
|
|
||||||
printf("\t%d\t%s\t%d\t%d\n",
|
|
||||||
i,getstr(f->locvars[i].varname),f->locvars[i].startpc+1,f->locvars[i].endpc+1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static void PrintUpvalues(const Proto* f)
|
|
||||||
{
|
|
||||||
int i,n=f->sizeupvalues;
|
|
||||||
printf("upvalues (%d) for %p:\n",n,VOID(f));
|
|
||||||
if (f->upvalues==NULL) return;
|
|
||||||
for (i=0; i<n; i++)
|
|
||||||
{
|
|
||||||
printf("\t%d\t%s\n",i,getstr(f->upvalues[i]));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void PrintFunction(const Proto* f, int full)
|
|
||||||
{
|
|
||||||
int i,n=f->sizep;
|
|
||||||
PrintHeader(f);
|
|
||||||
PrintCode(f);
|
|
||||||
if (full)
|
|
||||||
{
|
|
||||||
PrintConstants(f);
|
|
||||||
PrintLocals(f);
|
|
||||||
PrintUpvalues(f);
|
|
||||||
}
|
|
||||||
for (i=0; i<n; i++) PrintFunction(f->p[i],full);
|
|
||||||
}
|
|
@ -0,0 +1,92 @@
|
|||||||
|
/*
|
||||||
|
https://github.com/peterix/dfhack
|
||||||
|
Copyright (c) 2009-2011 Petr Mrázek (peterix@gmail.com)
|
||||||
|
|
||||||
|
This software is provided 'as-is', without any express or implied
|
||||||
|
warranty. In no event will the authors be held liable for any
|
||||||
|
damages arising from the use of this software.
|
||||||
|
|
||||||
|
Permission is granted to anyone to use this software for any
|
||||||
|
purpose, including commercial applications, and to alter it and
|
||||||
|
redistribute it freely, subject to the following restrictions:
|
||||||
|
|
||||||
|
1. The origin of this software must not be misrepresented; you must
|
||||||
|
not claim that you wrote the original software. If you use this
|
||||||
|
software in a product, an acknowledgment in the product documentation
|
||||||
|
would be appreciated but is not required.
|
||||||
|
|
||||||
|
2. Altered source versions must be plainly marked as such, and
|
||||||
|
must not be misrepresented as being the original software.
|
||||||
|
|
||||||
|
3. This notice may not be removed or altered from any source
|
||||||
|
distribution.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
#include <sstream>
|
||||||
|
#include <vector>
|
||||||
|
#include <map>
|
||||||
|
|
||||||
|
#include "DataDefs.h"
|
||||||
|
|
||||||
|
#include <lua.h>
|
||||||
|
#include <lauxlib.h>
|
||||||
|
|
||||||
|
namespace DFHack { namespace Lua {
|
||||||
|
/**
|
||||||
|
* Create or initialize a lua interpreter with access to DFHack tools.
|
||||||
|
*/
|
||||||
|
DFHACK_EXPORT lua_State *Open(color_ostream &out, lua_State *state = NULL);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Load a module using require().
|
||||||
|
*/
|
||||||
|
DFHACK_EXPORT bool Require(color_ostream &out, lua_State *state,
|
||||||
|
const std::string &module, bool setglobal = false);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Push the pointer onto the stack as a wrapped DF object of the given type.
|
||||||
|
*/
|
||||||
|
DFHACK_EXPORT void PushDFObject(lua_State *state, type_identity *type, void *ptr);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check that the value is a wrapped DF object of the given type, and if so return the pointer.
|
||||||
|
*/
|
||||||
|
DFHACK_EXPORT void *GetDFObject(lua_State *state, type_identity *type, int val_index, bool exact_type = false);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Push the pointer onto the stack as a wrapped DF object of a specific type.
|
||||||
|
*/
|
||||||
|
template<class T>
|
||||||
|
void PushDFObject(lua_State *state, T *ptr) {
|
||||||
|
PushDFObject(state, df::identity_traits<T>::get(), ptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check that the value is a wrapped DF object of the correct type, and if so return the pointer.
|
||||||
|
*/
|
||||||
|
template<class T>
|
||||||
|
T *GetDFObject(lua_State *state, int val_index, bool exact_type = false) {
|
||||||
|
return (T*)GetDFObject(state, df::identity_traits<T>::get(), val_index, exact_type);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Invoke lua function via pcall. Returns true if success.
|
||||||
|
* If an error is signalled, and perr is true, it is printed and popped from the stack.
|
||||||
|
*/
|
||||||
|
DFHACK_EXPORT bool SafeCall(color_ostream &out, lua_State *state, int nargs, int nres, bool perr = true);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the ostream passed to SafeCall.
|
||||||
|
*/
|
||||||
|
DFHACK_EXPORT color_ostream *GetOutput(lua_State *state);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Run an interactive interpreter loop if possible, or return false.
|
||||||
|
*/
|
||||||
|
DFHACK_EXPORT bool InterpreterLoop(color_ostream &out, lua_State *state,
|
||||||
|
const char *prompt = NULL, int env = 0, const char *hfile = NULL);
|
||||||
|
}}
|
||||||
|
|
@ -0,0 +1,5 @@
|
|||||||
|
-- Common startup file for all dfhack plugins with lua support
|
||||||
|
|
||||||
|
-- The global dfhack table is already created by C++ init
|
||||||
|
-- code. Feed it back to the require() mechanism.
|
||||||
|
return dfhack
|
@ -1 +1 @@
|
|||||||
Subproject commit 3e1c728640d8f5a9501908064a2d5385a156058c
|
Subproject commit 767e2f679471db8a77e923bc5572d859f677e08a
|
@ -1,327 +0,0 @@
|
|||||||
|
|
||||||
|
|
||||||
function f_dwarves()
|
|
||||||
--mov DWORD PTR [ESP+14],7
|
|
||||||
--jmp +0x08
|
|
||||||
pos_=offsets.findall(0,0x24,0x14,0x07,0,0,0) --search pattern
|
|
||||||
for _,v in pairs(pos_) do
|
|
||||||
print(string.format("Possible hit:%x",v))
|
|
||||||
end
|
|
||||||
pos=offsets.find(0,0x24,0x14,0x07,0,0,0,0xeb,0x08,0x8d) --search pattern
|
|
||||||
print(string.format("Start dwarf position:%x",pos))
|
|
||||||
if pos~=0 then
|
|
||||||
return pos+2-offsets.base();
|
|
||||||
else
|
|
||||||
return 0;
|
|
||||||
end
|
|
||||||
end
|
|
||||||
offsets.new("StartDwarfs",f_dwarves) -- finds the starting dwarf count
|
|
||||||
function f_creatures()
|
|
||||||
--01C48034-base=0x1258034
|
|
||||||
local val=0
|
|
||||||
--print("Enter creature count:");
|
|
||||||
--local r=io.stdin:read()
|
|
||||||
for k,v in pairs(offsets.getvectors()) do
|
|
||||||
if (v>60) and (v<100) then --used count changed some time ago... two hits second one is the right one. Maybe some smarter way could be better?
|
|
||||||
--new version first one is right?? needs more testing thou...
|
|
||||||
val= k-offsets.base()
|
|
||||||
print(string.format("%x",val))
|
|
||||||
break;
|
|
||||||
end
|
|
||||||
--vec=engine.peek(k,ptr_vector);
|
|
||||||
|
|
||||||
--[[if(vec:size()==tonumber(r)) then
|
|
||||||
val=k-offsets.base()
|
|
||||||
print(string.format("off:%x",k))
|
|
||||||
end--]]
|
|
||||||
end
|
|
||||||
offsets.new("AdvCreatureVec",val)
|
|
||||||
return val
|
|
||||||
end
|
|
||||||
offsets.new("CreatureVec",f_creatures)
|
|
||||||
function f_words()
|
|
||||||
local val=0
|
|
||||||
for k,v in pairs(offsets.getvectors()) do
|
|
||||||
local toff=engine.peekd(engine.peekd(k))
|
|
||||||
if(engine.peekstr(toff)=="ABBEY") then
|
|
||||||
val=k-offsets.base()
|
|
||||||
end
|
|
||||||
end
|
|
||||||
return val
|
|
||||||
end
|
|
||||||
offsets.newlazy("WordVec",f_words)
|
|
||||||
function f_creatureptr() --a creature number under the pointer
|
|
||||||
pos=offsets.find(0,0xa1,ANYDWORD,0x83,0xf8,0xff,0x75)
|
|
||||||
print("Offset="..pos)
|
|
||||||
if pos~=0 then
|
|
||||||
pos=engine.peekd(pos+1)
|
|
||||||
return pos-offsets.base()
|
|
||||||
else
|
|
||||||
return 0
|
|
||||||
end
|
|
||||||
end
|
|
||||||
offsets.new("CreaturePtr",f_creatureptr)
|
|
||||||
|
|
||||||
function f_creaturegloss() --creature race vector
|
|
||||||
for k,v in pairs(offsets.getvectors()) do
|
|
||||||
local reg
|
|
||||||
reg=GetRegionIn(k)
|
|
||||||
if reg ~=nil then
|
|
||||||
print(string.format("looking into %x wich is in %s",k,reg.name or ""))
|
|
||||||
else
|
|
||||||
print(string.format("looking into %x in nil region",k))
|
|
||||||
end
|
|
||||||
if ValidOffset(k) then
|
|
||||||
print("Looking into:"..string.format("%x",k).." used:"..v)
|
|
||||||
|
|
||||||
local vec=engine.peek(k,ptr_vector)
|
|
||||||
if vec:size()>0 and vec:size()<100000 and vec:getval(0)~=0 then
|
|
||||||
local toff=vec:getval(0)
|
|
||||||
if ValidOffset(toff) then
|
|
||||||
print("\tval:"..string.format("%x",vec:getval(0)))
|
|
||||||
local token=engine.peek(toff,ptt_dfstring)
|
|
||||||
--print("\t\tval:".. token:getval())
|
|
||||||
if token:getval()=="TOAD" then -- more offsets could be found this way
|
|
||||||
return k-offsets.base()
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
return 0
|
|
||||||
end
|
|
||||||
offsets.new("CreatureGloss",f_creaturegloss)
|
|
||||||
--reaction vec: search for TAN_A_HIDE
|
|
||||||
function f_racevec() --current race
|
|
||||||
--find all movsx anyreg,address
|
|
||||||
local den={}
|
|
||||||
local pos=offsets.findall(0,0x0f,0xbf,ANYBYTE,ADDRESS)
|
|
||||||
for k,v in pairs(pos) do
|
|
||||||
local add
|
|
||||||
if v~=0 then
|
|
||||||
add=engine.peekd(v+3)
|
|
||||||
if den[add]~=nil then
|
|
||||||
den[add]= den[add]+1
|
|
||||||
else
|
|
||||||
den[add]=1
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
end
|
|
||||||
|
|
||||||
for k,v in pairs(den) do
|
|
||||||
if v <60 then
|
|
||||||
den[k]=nil
|
|
||||||
end
|
|
||||||
|
|
||||||
end
|
|
||||||
for k,v in pairs(den) do
|
|
||||||
print("Looking into:"..string.format("%x",k).." used:"..v.." Race:"..engine.peekw(k))
|
|
||||||
if engine.peekw(k) >0 and engine.peekw(k)<1000 then
|
|
||||||
|
|
||||||
return k-offsets.base()
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
return 0
|
|
||||||
end
|
|
||||||
offsets.new("CurrentRace",f_racevec)
|
|
||||||
function f_pointer() --adventure (not only?) pointer x,y,z
|
|
||||||
print("\n")
|
|
||||||
local den={}
|
|
||||||
local pos=0
|
|
||||||
repeat
|
|
||||||
pos=offsets.find(pos+3,0x0f,0xb7,ANYBYTE,ADDRESS)
|
|
||||||
local add=engine.peekd(pos+3)
|
|
||||||
local add2=engine.peekd(pos+13)
|
|
||||||
local add3=engine.peekd(pos+23)
|
|
||||||
if( math.abs(add-add2)==4 or math.abs(add-add3)==4) then
|
|
||||||
|
|
||||||
if den[add]~=nil then
|
|
||||||
den[add]= den[add]+1
|
|
||||||
else
|
|
||||||
den[add]=1
|
|
||||||
end
|
|
||||||
end
|
|
||||||
until pos==0
|
|
||||||
for k,v in pairs(den) do
|
|
||||||
print("Looking into:"..string.format("%x",k).." used:"..v)
|
|
||||||
return k-offsets.base()-4
|
|
||||||
end
|
|
||||||
return 0
|
|
||||||
end
|
|
||||||
offsets.new("Xpointer",f_pointer)
|
|
||||||
function f_adventure()
|
|
||||||
RaceTable=RaceTable or BuildNameTable() -- this gets all the races' numbers
|
|
||||||
--[[print("input chars race:")
|
|
||||||
repeat
|
|
||||||
r=io.stdin:read()
|
|
||||||
if RaceTable[r]==nil then print("Incorrect race...") end
|
|
||||||
until RaceTable[r]~=nil -- query till correct race is inputed
|
|
||||||
rnum=RaceTable[r] --get its num
|
|
||||||
print("Race:"..rnum)]]--
|
|
||||||
myoff=0
|
|
||||||
print("input player's creature's name (lowercaps):")
|
|
||||||
r=io.stdin:read()
|
|
||||||
|
|
||||||
for k,v in pairs(offsets.getvectors()) do -- now lets look through all vector offsets
|
|
||||||
off=engine.peekd(k) --get vector start
|
|
||||||
off=engine.peekd(off) --get 0 element (or first) in adventurer mode its our hero
|
|
||||||
name=engine.peekstr(off)
|
|
||||||
if(name==r) then
|
|
||||||
--if engine.peek(off+140)==rnum then -- creature start+140 is the place where race is kept
|
|
||||||
print(string.format("%x race:%x",k,engine.peekw(off+140)))
|
|
||||||
myoff=k -- ok found it
|
|
||||||
break
|
|
||||||
end
|
|
||||||
end
|
|
||||||
if myoff~=0 then
|
|
||||||
crvec=engine.peek(myoff,ptr_vector)
|
|
||||||
print(string.format("player offset:%x",crvec:getval(0)))
|
|
||||||
local legidVec=engine.peek(crvec:getval(0),ptr_Creature.legends)
|
|
||||||
print(string.format("legends offset:%x",legidVec:getval(0)))
|
|
||||||
local vtable=engine.peekd(legidVec:getval(0))
|
|
||||||
print(string.format("vtable offset:%x",vtable))
|
|
||||||
offsets.new("vtableLegends",vtable-offsets.base())
|
|
||||||
return myoff-offsets.base() --save the offset for laters
|
|
||||||
else
|
|
||||||
return 0 --indicate failure
|
|
||||||
end
|
|
||||||
end
|
|
||||||
offsets.newlazy("AdvCreatureVec",f_adventure) -- register a way to find this offset
|
|
||||||
--7127F0
|
|
||||||
function f_legends()
|
|
||||||
pos=1
|
|
||||||
T={}
|
|
||||||
repeat
|
|
||||||
pos=offsets.find(pos+1,0x50,0xb8,ANYDWORD,0xe8,ANYDWORD,0x8b,0xf0)
|
|
||||||
off=engine.peekd(pos+2)
|
|
||||||
vec=engine.peek(off,ptr_vector)
|
|
||||||
if vec:size()~=0 then
|
|
||||||
if T[off]~=nil then
|
|
||||||
T[off].c=T[off].c+1
|
|
||||||
else
|
|
||||||
T[off]={c=1,vsize=vec:size()}
|
|
||||||
end
|
|
||||||
|
|
||||||
end
|
|
||||||
|
|
||||||
until pos==0
|
|
||||||
for k,v in pairs(T) do
|
|
||||||
vec=engine.peek(k,ptr_vector)
|
|
||||||
print(string.format("off:%x used:%i size:%d",k,v.c,v.vsize))
|
|
||||||
print(string.format("fith elements id:%d",engine.peekd(vec:getval(5))))
|
|
||||||
if engine.peekd(vec:getval(5))==5 then
|
|
||||||
--if v.c==2 and v.vsize>1000 then
|
|
||||||
return k-offsets.base()
|
|
||||||
end
|
|
||||||
end
|
|
||||||
return 0
|
|
||||||
end
|
|
||||||
offsets.newlazy("Legends",f_legends)
|
|
||||||
function f_playerlegendid()
|
|
||||||
local off=offsets.getEx("Legends")
|
|
||||||
local pos=1
|
|
||||||
repeat
|
|
||||||
pos=offsets.find(pos+1,0xa1,DWORD_,off+4,0x2b,0x05,DWORD_,off)
|
|
||||||
val=engine.peekd(pos+16)
|
|
||||||
if engine.peekd(val)~=0 then
|
|
||||||
--if val >offsets.base() then
|
|
||||||
return val-offsets.base()
|
|
||||||
end
|
|
||||||
--print(string.format("%x",val))
|
|
||||||
until pos==0
|
|
||||||
return 0
|
|
||||||
end
|
|
||||||
offsets.newlazy("PlayerLegend",f_playerlegendid)
|
|
||||||
|
|
||||||
function f_world()
|
|
||||||
local pos=offsets.base()
|
|
||||||
T={}
|
|
||||||
while pos~=0 do
|
|
||||||
--pos=offsets.find(pos+6,0xa1,DWORD_,mapoffset,0x8b,0x4c,0x88,0xFC)
|
|
||||||
pos=offsets.find(pos+6,0x8b,0x35,ANYDWORD,0x85,0xf6)--,0x8b,0x4c,0x88,0xFC)
|
|
||||||
--pos2=offsets.find(pos,0x8b,0x34,0x88)
|
|
||||||
|
|
||||||
if pos~=0 then
|
|
||||||
add=engine.peekd(pos+2);
|
|
||||||
--if pos2 ~=0 and pos2-pos<25 then
|
|
||||||
-- print(string.format("Address:%x dist:%d Map:%x",pos2,pos2-pos,add))
|
|
||||||
--
|
|
||||||
--end
|
|
||||||
if add~=0 then
|
|
||||||
if T[add]~=nil then
|
|
||||||
T[add]=T[add]+1
|
|
||||||
else
|
|
||||||
T[add]=1
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
end
|
|
||||||
local kk,vv
|
|
||||||
vv=0
|
|
||||||
for k,v in pairs(T) do
|
|
||||||
if v>vv then
|
|
||||||
vv=v
|
|
||||||
kk=k
|
|
||||||
end
|
|
||||||
--print(string.format("Address:%x, times used:%d",k,v))
|
|
||||||
end
|
|
||||||
return kk-offsets.base()
|
|
||||||
end
|
|
||||||
offsets.new("WorldData",f_world)
|
|
||||||
|
|
||||||
function f_sites()
|
|
||||||
local pos=offsets.base()
|
|
||||||
T={}
|
|
||||||
while pos~=0 do
|
|
||||||
|
|
||||||
pos=offsets.find(pos+17,0xA1,ANYDWORD, --mov eax, ptr to some biger thing
|
|
||||||
0x8B,0x90,0x24,0x01,0x00,0x00, --mov edx [eax+0x124]
|
|
||||||
0x2b,0x90,0x20,0x01,0x00,0x00, --sub edx [eax+0x120]
|
|
||||||
EOL)
|
|
||||||
if pos~=0 then
|
|
||||||
add=engine.peekd(pos+1)
|
|
||||||
return add-offsets.base()
|
|
||||||
|
|
||||||
end
|
|
||||||
|
|
||||||
end
|
|
||||||
return 0
|
|
||||||
end
|
|
||||||
offsets.newlazy("SiteData",f_sites) --actually has a lot more data...
|
|
||||||
function f_items()
|
|
||||||
local pos=offsets.base()
|
|
||||||
while pos~= 0 do
|
|
||||||
pos=offsets.find(pos+17,0x8b,0x0d,ANYDWORD, --mov eax, ptr to some biger thing
|
|
||||||
0x8B,0x54,0x24,0x34)
|
|
||||||
if pos~=0 then
|
|
||||||
--print(string.format("%x",engine.peekd(pos+2)))
|
|
||||||
local ret=engine.peekd(pos+2)-offsets.base()
|
|
||||||
return ret
|
|
||||||
end
|
|
||||||
end
|
|
||||||
return 0
|
|
||||||
end
|
|
||||||
offsets.new("Items",f_items)
|
|
||||||
function f_materials()
|
|
||||||
for k,v in pairs(offsets.getvectors()) do
|
|
||||||
--print("Looking into:"..string.format("%x",k).." used:"..v)
|
|
||||||
local vec=engine.peek(k,ptr_vector)
|
|
||||||
if vec:getval(0)~=0 then
|
|
||||||
--print("\tval:"..string.format("%x",vec:getval(0)))
|
|
||||||
local token=engine.peek(vec:getval(0),ptt_dfstring)
|
|
||||||
if token:getval()~=""then
|
|
||||||
--print("\t\tval:".. token:getval())
|
|
||||||
if token:getval()=="IRON" then
|
|
||||||
--print("Found:"..string.format("%x",k).." used:"..v)
|
|
||||||
return k-offsets.base()
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
return 0
|
|
||||||
end
|
|
||||||
offsets.new("Materials",f_materials)
|
|
@ -1,16 +0,0 @@
|
|||||||
AdvCreatureVec : 0x12c44ac
|
|
||||||
CreatureGloss : 0x1308040
|
|
||||||
CreaturePtr : 0xaf2430
|
|
||||||
CreatureVec : 0x12c44ac
|
|
||||||
CurrentRace : 0x10f0c28
|
|
||||||
Items : 0x12c4550
|
|
||||||
Legends : 0x12c451c
|
|
||||||
Materials : 0x1307f50
|
|
||||||
PlayerLegend : 0x145bfec
|
|
||||||
SiteData : 0x1307778
|
|
||||||
WordVec : 0x1308254
|
|
||||||
WorldData : 0x1306148
|
|
||||||
Xpointer : 0x7347f0
|
|
||||||
vtableLegends : 0x6e7594
|
|
||||||
Buildings : 0x12C4DA8
|
|
||||||
StartDwarfs : 0x5DA790
|
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue