Exemple #1
0
    App()
        : console(glm::ivec2(80, 4))
        , luaVm([this](std::string const& error) {
            this->console.write(error);
        })
        , world(world_shader)
        , fence("../Data/models/fence2.obj", texturing_shader)
        , player([this](Minefield::WorldCoord const& wc) {
            return (world.get(wc).value) ? false : true;
        })
    {
        console.setCallback([this](std::string const& s){
            this->luaVm.eval(s);
        });

        luaVm.register_function("exit", [this]{ this->console.write("OMG EXIT"); });
        luaVm.register_function("print", [this](std::string s){ this->console.write(std::move(s)); });
        luaVm.register_function("recalc", [this]{ this->world.recalcInstances(); });
        luaVm.register_function("recalc_full", [this]{ this->world.recalcInstances(true); });
        luaVm.register_function("save", [this]{ this->world.saveToFile("world.mcw"); });
        luaVm.register_function("load", [this]{ this->world.loadFromFile("world.mcw"); });
        luaVm.register_function("set", [this](int x, int y, int z, int v) { world.set(Minefield::WorldCoord(x,y,z), v); });
        luaVm.register_function("get", [this](int x, int y, int z) { return static_cast<int>(world.get(Minefield::WorldCoord(x,y,z)).value); });
        luaVm.register_function("fly", [this]{ player.flying = !player.flying; });

        init();
        initShadersEngine();
        initResources();

        world.init();
        world.recalcInstances();
    }
Exemple #2
0
void lua::RegisterEngine(lua::state &st)
{
	st.getglobal("engine");
	if(st.is<lua::nil>())
	{
		st.pop();
		st.newtable();
	}
	lua::RegFunctionsLocal(st,lua_engine_func);
	st.setglobal("engine");
}
Exemple #3
0
 int index(lua::state& L, lua::parameters& P)
 {
     const char* c = L.tostring(2);
     if(!c) {
         L.pushnil();
         return 1;
     }
     std::string c2(c);
     if(!mappings.count(c2)) {
         L.pushnil();
         return 1;
     }
     auto& x = mappings[c2];
     x.rw(L, x.addr, false);
     return 1;
 }
Exemple #4
0
void handle_unregisterX(lua::state& L, uint64_t addr, int lfn)
{
    lua_debug_callback_dict* Dx;
    lua_debug_callback2* D = NULL;
    L.pushlightuserdata(&CONST_lua_cb_list_key);
    L.rawget(LUA_REGISTRYINDEX);
    if(!L.isnil(-1)) {
        Dx = (lua_debug_callback_dict*)L.touserdata(-1);
        auto key = std::make_pair(type, addr);
        if(Dx->cblist.count(key))
            D = Dx->cblist[key];
        L.pop(1);
        while(D) {
            if(D->dead || D->type != type || D->addr != addr || L.topointer(lfn) != D->lua_fn) {
                D = D->next;
                continue;
            }
            //Remove this.
            auto Dold = D;
            D = D->next;
            Dold->unregister();
        }
    } else
        L.pop(1);
}
Exemple #5
0
 int newindex(lua::state& L, lua::parameters& P)
 {
     const char* c = L.tostring(2);
     if(!c)
         return 0;
     std::string c2(c);
     if(!mappings.count(c2))
         return 0;
     auto& x = mappings[c2];
     x.rw(L, x.addr, true);
     return 0;
 }
Exemple #6
0
void lua::RegisterProcess(lua::state &st,DFHack::Process *p)
{
	st.getglobal("Process");
	if(st.is<lua::nil>())
	{
		st.pop();
		st.newtable();
	}

	st.pushlightuserdata(p);
	st.setfield("__pointer");
	
	lua::RegFunctionsLocal(st, lua_process_func);

	st.setglobal("Process");
}
Exemple #7
0
void lua::RegCommon(lua::state &L)
{
    L.newtable();
    //L.push(&GetOffset);
    lua_pushcfunction(L,&GetOffset);
    L.setfield("get");
    lua_pushcfunction(L,&GetOffset2);
    L.setfield("getEx");
    lua_pushcfunction(L,&Find);
    L.setfield("find");
    lua_pushcfunction(L,&NewOffset);
    L.setfield("new");
    lua_pushcfunction(L,&NewLazyOffset);
    L.setfield("newlazy");
    lua_pushcfunction(L,&GetBase);
    L.setfield("base");
    lua_pushcfunction(L,&Save);
    L.setfield("save");
    lua_pushcfunction(L,&GetVecs);
    L.setfield("getvectors");
    lua_pushcfunction(L,&GetCalls);
    L.setfield("getcalls");
    L.setglobal("offsets");

    //find functionality...
    /*
        #define EOL			0x100
        #define ANYBYTE		0x101
        //#define FF_OR_00	0x102				// deprecated
        #define HERE		0x103
        #define EITHER		0x104
        #define SKIP_UP_TO	0x105
        #define RANGE_LO	0x106				// deprecated
        #define RANGE_HI	0x107				// deprecated
        #define DWORD_		0x108
        #define	ANYDWORD	0x109
        #define ADDRESS		0x10A
        #define	BYTERANGE	0x10B
        #define DWORDRANGE	0x10C
        #define JZ			0x174
        #define JNZ			0x175
        #define JCC			0x170
        #define CALL		0x1E8
        #define JUMP		0x1E9
    */
    L.push(EOL);
    L.setglobal("EOL");
    L.push(ANYBYTE);
    L.setglobal("ANYBYTE");
    L.push(HERE);
    L.setglobal("HERE");
    L.push(EITHER);
    L.setglobal("EITHER");
    L.push(SKIP_UP_TO);
    L.setglobal("SKIP_UP_TO");
    L.push(DWORD_);
    L.setglobal("DWORD_");
    L.push(ANYDWORD);
    L.setglobal("ANYDWORD");
    L.push(ADDRESS);
    L.setglobal("ADDRESS");
    L.push(BYTERANGE);
    L.setglobal("BYTERANGE");
    L.push(DWORDRANGE);
    L.setglobal("DWORDRANGE");
    L.push(JZ);
    L.setglobal("JZ");
    L.push(JNZ);
    L.setglobal("JNZ");
    L.push(JCC);
    L.setglobal("JCC");
    L.push(CALL);
    L.setglobal("CALL");
    L.push(JUMP);
    L.setglobal("JUMP");
    luaopen_bit(L);

}
Exemple #8
0
void handle_registerX(lua::state& L, uint64_t addr, int lfn)
{
    //Put the context in userdata so it can be gc'd when Lua context is terminated.
    lua_debug_callback2* D = (lua_debug_callback2*)L.newuserdata(sizeof(lua_debug_callback2));
    new(D) lua_debug_callback2;
    L.newtable();
    L.pushstring("__gc");
    L.push_trampoline(&lua_debug_callback2::on_lua_gc, 0);
    L.rawset(-3);
    L.setmetatable(-2);
    L.pushlightuserdata(D);
    L.pushvalue(-2);
    L.rawset(LUA_REGISTRYINDEX);
    L.pop(1); //Pop the copy of object.

    D->L = &L.get_master();
    D->addr = addr;
    D->type = type;
    D->dead = false;
    D->set_lua_fn(lfn);
    D->link_to_list();

    CORE().dbg->add_callback(addr, type, *D);
}
Exemple #9
0
    return boost::get<T>(variant) && boost::get<T>(variant) == value; 
}

//[](std::string const& s) { std::cerr << "Lua ERROR : " << s << std::endl; }
void defaultErrorReporter(std::string const& s) {
    std::cerr << "Lua ERROR : " << s << std::endl; 
}

void exceptionErrorReporter(std::string const& s) {
    throw lua::exception(s);
}

}

TEST_CASE( "simple/set_global", "Check if the set_global works properly." ) {
    lua::state lua(&exceptionErrorReporter);

    lua.set_global("a", 9);
    REQUIRE_NOTHROW( lua.eval("if a ~= 9 then error('wrong value') end") );

    lua.set_global("d", "hello");
    REQUIRE_NOTHROW( lua.eval("if d ~= 'hello' then error('expected \\'hello\\', got '.. tostring(d)) end") );

    lua.set_global("e", std::string("hello"));
    REQUIRE_NOTHROW( lua.eval("if d ~= 'hello' then error('expected \\'hello\\', got '.. tostring(d)) end") );

    lua.set_global("f", true);
    REQUIRE_NOTHROW( lua.eval("if f ~= true then error('wrong value') end") );
}

TEST_CASE( "simple/get_global", "Tests if the get_global function works properly." ) {