2011-07-27 14:10:20 -06:00
|
|
|
#include "lua_Offsets.h"
|
2011-08-08 13:17:11 -06:00
|
|
|
#include <string.h>
|
2012-03-04 05:45:15 -07:00
|
|
|
#include <stdint.h>
|
2011-07-27 14:10:20 -06:00
|
|
|
//TODO make a seperate module with peeks/pokes and page permisions (linux/windows spec)
|
2011-08-08 13:17:11 -06:00
|
|
|
//TODO maybe remove alltogether- use DFHack::Process instead?
|
2012-03-04 05:45:15 -07:00
|
|
|
template <typename T>
|
|
|
|
T engine_peek(size_t offset)
|
2011-07-27 14:10:20 -06:00
|
|
|
{
|
2012-03-04 05:45:15 -07:00
|
|
|
return *(reinterpret_cast<T*>(offset));
|
2011-07-27 14:10:20 -06:00
|
|
|
}
|
2012-03-04 05:45:15 -07:00
|
|
|
template <typename T>
|
|
|
|
void engine_poke(size_t offset,T val)
|
2011-07-27 14:10:20 -06:00
|
|
|
{
|
2012-03-04 05:45:15 -07:00
|
|
|
*(reinterpret_cast<T*>(offset))=val;
|
2011-07-27 14:10:20 -06:00
|
|
|
}
|
2012-03-04 05:45:15 -07:00
|
|
|
|
2011-07-27 14:41:56 -06:00
|
|
|
void peekarb(size_t offset, void *mem,size_t size)
|
|
|
|
{
|
|
|
|
memcpy(mem,(void*)offset,size);
|
|
|
|
}
|
|
|
|
void peekstr(size_t offset, char* buf, size_t maxsize)
|
|
|
|
{
|
2011-08-08 13:17:11 -06:00
|
|
|
strncpy(buf,(char*)offset,maxsize);
|
2011-07-27 14:41:56 -06:00
|
|
|
}
|
|
|
|
void pokearb(size_t offset, void *mem,size_t size)
|
2011-07-27 14:10:20 -06:00
|
|
|
{
|
2011-07-27 14:41:56 -06:00
|
|
|
memcpy((void*)offset,mem,size);
|
|
|
|
}
|
|
|
|
void pokestr(size_t offset, char* buf, size_t maxsize)
|
|
|
|
{
|
2011-08-08 13:17:11 -06:00
|
|
|
strncpy((char*)offset,buf,maxsize);
|
2011-07-27 14:41:56 -06:00
|
|
|
}
|
2011-07-27 14:10:20 -06:00
|
|
|
|
2011-07-27 14:41:56 -06:00
|
|
|
//// lua stuff here
|
|
|
|
static int lua_peekb(lua_State *L)
|
|
|
|
{
|
|
|
|
lua::state st(L);
|
2012-03-04 05:45:15 -07:00
|
|
|
st.push(engine_peek<uint8_t>(st.as<size_t>(1)));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
static int lua_peekw(lua_State *L)
|
|
|
|
{
|
|
|
|
lua::state st(L);
|
|
|
|
st.push(engine_peek<uint16_t>(st.as<size_t>(1)));
|
2011-07-27 14:41:56 -06:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
static int lua_peekd(lua_State *L)
|
|
|
|
{
|
|
|
|
lua::state st(L);
|
2012-03-04 05:45:15 -07:00
|
|
|
st.push(engine_peek<uint32_t>(st.as<size_t>(1)));
|
2011-07-27 14:41:56 -06:00
|
|
|
return 1;
|
|
|
|
}
|
2012-03-04 05:45:15 -07:00
|
|
|
static int lua_peekq(lua_State *L)
|
|
|
|
{
|
|
|
|
lua::state st(L);
|
|
|
|
st.push(engine_peek<uint64_t>(st.as<size_t>(1)));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
static int lua_peekfloat(lua_State *L)
|
|
|
|
{
|
|
|
|
lua::state st(L);
|
|
|
|
st.push(engine_peek<float>(st.as<size_t>(1)));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
static int lua_peekdouble(lua_State *L)
|
2011-07-27 14:41:56 -06:00
|
|
|
{
|
|
|
|
lua::state st(L);
|
2012-03-04 05:45:15 -07:00
|
|
|
st.push(engine_peek<double>(st.as<size_t>(1)));
|
2011-07-27 14:41:56 -06:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
static int lua_peekarb(lua_State *L)
|
|
|
|
{
|
|
|
|
lua::state st(L);
|
|
|
|
size_t size=st.as<size_t>(2);
|
|
|
|
void *p=st.newuserdata(size);
|
|
|
|
peekarb(st.as<size_t>(1),p,size);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
static int lua_peekstr(lua_State *L)
|
|
|
|
{
|
|
|
|
lua::state st(L);
|
|
|
|
char *buf;
|
|
|
|
buf=new char[256];
|
|
|
|
peekstr(st.as<size_t>(1),buf,256);
|
|
|
|
std::string tstr(buf);
|
|
|
|
st.push(tstr);
|
|
|
|
delete [] buf;
|
|
|
|
return 1;
|
|
|
|
}
|
2012-03-04 05:45:15 -07:00
|
|
|
static int lua_peekstr2(lua_State *L)
|
2011-07-27 14:41:56 -06:00
|
|
|
{
|
|
|
|
lua::state st(L);
|
2012-03-04 05:45:15 -07:00
|
|
|
st.push(engine_peek<std::string>(st.as<size_t>(1)));
|
2011-07-27 14:41:56 -06:00
|
|
|
return 1;
|
2012-03-04 05:45:15 -07:00
|
|
|
}
|
2011-07-27 14:41:56 -06:00
|
|
|
static int lua_pokeb(lua_State *L)
|
|
|
|
{
|
|
|
|
lua::state st(L);
|
2012-03-04 05:45:15 -07:00
|
|
|
engine_poke<uint8_t>(st.as<size_t>(1),st.as<uint8_t>(2));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
static int lua_pokew(lua_State *L)
|
|
|
|
{
|
|
|
|
lua::state st(L);
|
|
|
|
engine_poke<uint16_t>(st.as<size_t>(1),st.as<uint16_t>(2));
|
2011-07-27 14:41:56 -06:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
static int lua_poked(lua_State *L)
|
|
|
|
{
|
|
|
|
lua::state st(L);
|
2012-03-04 05:45:15 -07:00
|
|
|
engine_poke<uint32_t>(st.as<size_t>(1),st.as<uint32_t>(2));
|
2011-07-27 14:41:56 -06:00
|
|
|
return 0;
|
|
|
|
}
|
2012-03-04 05:45:15 -07:00
|
|
|
static int lua_pokeq(lua_State *L)
|
2011-07-27 14:41:56 -06:00
|
|
|
{
|
|
|
|
lua::state st(L);
|
2012-03-04 05:45:15 -07:00
|
|
|
engine_poke<uint64_t>(st.as<size_t>(1),st.as<uint64_t>(2));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
static int lua_pokefloat(lua_State *L)
|
|
|
|
{
|
|
|
|
lua::state st(L);
|
|
|
|
engine_poke<float>(st.as<size_t>(1),st.as<float>(2));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
static int lua_pokedouble(lua_State *L)
|
|
|
|
{
|
|
|
|
lua::state st(L);
|
|
|
|
engine_poke<double>(st.as<size_t>(1),st.as<double>(2));
|
2011-07-27 14:41:56 -06:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
static int lua_pokearb(lua_State *L)
|
|
|
|
{
|
|
|
|
lua::state st(L);
|
|
|
|
void *p=(void *)lua_touserdata(L, 2);//st.as<lua::userdata>(2);
|
|
|
|
size_t size=st.as<size_t>(3);
|
|
|
|
pokearb(st.as<size_t>(1),p,size);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
static int lua_pokestr(lua_State *L)
|
|
|
|
{
|
|
|
|
lua::state st(L);
|
|
|
|
std::string trg=st.as<std::string>(2);
|
|
|
|
pokestr(st.as<size_t>(1),(char*)trg.c_str(),trg.size());
|
|
|
|
return 0;
|
|
|
|
}
|
2012-03-04 05:45:15 -07:00
|
|
|
static int lua_pokestr2(lua_State *L)
|
|
|
|
{
|
|
|
|
lua::state st(L);
|
|
|
|
std::string trg=st.as<std::string>(2);
|
|
|
|
engine_poke<std::string>(st.as<size_t>(1),trg);
|
|
|
|
return 0;
|
|
|
|
}
|
2011-07-27 14:41:56 -06:00
|
|
|
const luaL_Reg lua_engine_func[]=
|
|
|
|
{
|
|
|
|
{"peekb",lua_peekb},
|
|
|
|
{"peekw",lua_peekw},
|
|
|
|
{"peekd",lua_peekd},
|
2012-03-04 05:45:15 -07:00
|
|
|
{"peekq",lua_peekq},
|
|
|
|
{"peekfloat",lua_peekfloat},
|
|
|
|
{"peekdouble",lua_peekdouble},
|
|
|
|
|
2011-07-27 14:41:56 -06:00
|
|
|
{"peekarb",lua_peekarb},
|
|
|
|
{"peekstr",lua_peekstr},
|
2012-03-04 05:45:15 -07:00
|
|
|
{"peekstr2",lua_peekstr2},
|
|
|
|
|
2011-07-27 14:41:56 -06:00
|
|
|
{"pokeb",lua_pokeb},
|
|
|
|
{"pokew",lua_pokew},
|
|
|
|
{"poked",lua_poked},
|
2012-03-04 05:45:15 -07:00
|
|
|
{"pokeq",lua_pokeq},
|
|
|
|
{"pokefloat",lua_pokefloat},
|
|
|
|
{"pokedouble",lua_pokedouble},
|
|
|
|
|
2011-07-27 14:41:56 -06:00
|
|
|
{"pokearb",lua_pokearb},
|
|
|
|
{"pokestr",lua_pokestr},
|
2012-03-04 05:45:15 -07:00
|
|
|
{"pokestr2",lua_pokestr2},
|
2011-07-27 14:41:56 -06:00
|
|
|
{NULL,NULL}
|
|
|
|
};
|
2011-07-27 18:11:33 -06:00
|
|
|
|
2011-07-27 14:41:56 -06:00
|
|
|
void lua::RegisterEngine(lua::state &st)
|
|
|
|
{
|
2011-07-27 18:11:33 -06:00
|
|
|
st.getglobal("engine");
|
|
|
|
if(st.is<lua::nil>())
|
|
|
|
{
|
|
|
|
st.pop();
|
|
|
|
st.newtable();
|
|
|
|
}
|
2011-07-27 14:41:56 -06:00
|
|
|
lua::RegFunctionsLocal(st,lua_engine_func);
|
|
|
|
st.setglobal("engine");
|
2011-08-08 13:17:11 -06:00
|
|
|
}
|