Skip to content

Commit

Permalink
Manage lua scripts and change the callback system, to prepare for #519
Browse files Browse the repository at this point in the history
  • Loading branch information
clementgallet committed Feb 5, 2023
1 parent fcf913e commit 1149da9
Show file tree
Hide file tree
Showing 12 changed files with 445 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
* Implement XIQueryDevice()
* Implement SDL_JoystickGetDeviceInstanceID()
* Add a window for lua output
* Manage lua scripts and change the callback system

### Changed

Expand Down
33 changes: 31 additions & 2 deletions docs/guides/lua.md
Original file line number Diff line number Diff line change
Expand Up @@ -261,8 +261,37 @@ Returns 1 of the current frame is a draw frame, or 0 if not.

### Callbacks

These functions, if defined in the lua script, are called at specific moments
during the game execution.
#### callback.onStartup

None callback.onStartup(Function f)

Registers `f` to be called when the game has started, before the end of the first frame.

#### callback.onInput

None callback.onInput(Function f)

Registers `f` to be called when input for the current frame is decided. Change the movie input if
in recording mode. Not called when movie is in playback mode.

#### callback.onFrame

None callback.onFrame(Function f)

Registers `f` to be called after frame has completed.

#### callback.onPaint

None callback.onPaint(Function f)

Registers `f` to be called at the end of the frame before the screen rendering
is performed. All Gui functions must be executed inside this callback. To
include Lua draws in encodes, `Video > OSD > OSD on video encode` can be checked.

### Callbacks (obsolete)

The old callback method consists on defining functions with specific names above,
which will be called at specific moments during the game execution.

#### onStartup

Expand Down
13 changes: 7 additions & 6 deletions src/program/GameLoop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@
// #include "SaveState.h"
#include "SaveStateList.h"
#include "lua/Input.h"
#include "lua/Main.h"
#include "lua/Callbacks.h"
#include "lua/NamedLuaFunction.h"
#include "ramsearch/MemAccess.h"

#include "../shared/sockethelpers.h"
Expand Down Expand Up @@ -70,7 +71,7 @@ void GameLoop::start()
init();
initProcessMessages();

Lua::Main::callLua("onStartup");
Lua::Callbacks::call(Lua::NamedLuaFunction::CallbackStartup);

while (1)
{
Expand Down Expand Up @@ -140,7 +141,7 @@ void GameLoop::start()
}
}

Lua::Main::callLua("onFrame");
Lua::Callbacks::call(Lua::NamedLuaFunction::CallbackFrame);

endFrameMessages(ai);

Expand Down Expand Up @@ -526,7 +527,7 @@ bool GameLoop::startFrameMessages()
}

/* Execute the lua callback onPaint here */
Lua::Main::callLua("onPaint");
Lua::Callbacks::call(Lua::NamedLuaFunction::CallbackPaint);

sendMessage(MSGN_START_FRAMEBOUNDARY);

Expand Down Expand Up @@ -611,7 +612,7 @@ void GameLoop::processInputs(AllInputs &ai)

/* Call lua onInput() here so that a script can modify inputs */
Lua::Input::registerInputs(&ai);
Lua::Main::callLua("onInput");
Lua::Callbacks::call(Lua::NamedLuaFunction::CallbackInput);

if (context->config.sc.recording == SharedConfig::RECORDING_WRITE) {
/* If the input editor is visible, we should keep future inputs.
Expand Down Expand Up @@ -724,7 +725,7 @@ void GameLoop::processInputs(AllInputs &ai)

/* Call lua onInput() here so that a script can modify inputs */
Lua::Input::registerInputs(&ai);
Lua::Main::callLua("onInput");
Lua::Callbacks::call(Lua::NamedLuaFunction::CallbackInput);

/* Update controller inputs if controller window is shown */
emit showControllerInputs(ai);
Expand Down
3 changes: 3 additions & 0 deletions src/program/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,10 @@ libTAS_SOURCES = \
SaveState.cpp \
SaveStateList.cpp \
utils.cpp \
lua/Callbacks.cpp \
lua/Gui.cpp \
lua/LuaFunctionList.cpp \
lua/NamedLuaFunction.cpp \
lua/Input.cpp \
lua/Main.cpp \
lua/Memory.cpp \
Expand Down
86 changes: 86 additions & 0 deletions src/program/lua/Callbacks.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
Copyright 2015-2020 Clément Gallet <clement.gallet@ens-lyon.org>
This file is part of libTAS.
libTAS is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
libTAS is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with libTAS. If not, see <http://www.gnu.org/licenses/>.
*/

#include "Callbacks.h"
#include "Main.h"
#include "../Context.h"
#include "NamedLuaFunction.h"
#include "LuaFunctionList.h"

#include <iostream>
extern "C" {
#include <lua.h>
#include <lauxlib.h>
}

namespace Lua {

static LuaFunctionList lfl;

/* List of functions to register */
static const luaL_Reg callback_functions[] =
{
{ "onStartup", Callbacks::onStartup},
{ "onInput", Callbacks::onInput},
{ "onFrame", Callbacks::onFrame},
{ "onPaint", Callbacks::onPaint},
{ NULL, NULL }
};

void Callbacks::registerFunctions(lua_State *L)
{
luaL_newlib(L, callback_functions);
lua_setglobal(L, "callback");
}

void Callbacks::clear()
{
lfl.clear();
}

int Callbacks::onStartup(lua_State *L)
{
lfl.add(L, NamedLuaFunction::CallbackStartup);
return 0;
}

int Callbacks::onInput(lua_State *L)
{
lfl.add(L, NamedLuaFunction::CallbackInput);
return 0;
}

int Callbacks::onFrame(lua_State *L)
{
lfl.add(L, NamedLuaFunction::CallbackFrame);
return 0;
}

int Callbacks::onPaint(lua_State *L)
{
lfl.add(L, NamedLuaFunction::CallbackPaint);
return 0;
}

void Callbacks::call(NamedLuaFunction::CallbackType type)
{
lfl.call(type);
}

}
50 changes: 50 additions & 0 deletions src/program/lua/Callbacks.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
Copyright 2015-2020 Clément Gallet <clement.gallet@ens-lyon.org>
This file is part of libTAS.
libTAS is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
libTAS is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with libTAS. If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef LIBTAS_LUACALLBACKS_H_INCLUDED
#define LIBTAS_LUACALLBACKS_H_INCLUDED

#include "NamedLuaFunction.h"

extern "C" {
#include <lua.h>
}

namespace Lua {

namespace Callbacks {

/* Register all functions */
void registerFunctions(lua_State *L);

void clear();

int onStartup(lua_State *L);

int onInput(lua_State *L);

int onFrame(lua_State *L);

int onPaint(lua_State *L);

void call(NamedLuaFunction::CallbackType type);
}
}

#endif
48 changes: 48 additions & 0 deletions src/program/lua/LuaFunctionList.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
Copyright 2015-2020 Clément Gallet <clement.gallet@ens-lyon.org>
This file is part of libTAS.
libTAS is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
libTAS is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with libTAS. If not, see <http://www.gnu.org/licenses/>.
*/

#include "LuaFunctionList.h"

namespace Lua {

void LuaFunctionList::add(lua_State *L, NamedLuaFunction::CallbackType t)
{
functions.emplace_back(L, t);
}

void LuaFunctionList::removeForFile(const std::string& file)
{
functions.remove_if([&file](const NamedLuaFunction& nlf){ return 0 == file.compare(nlf.file); });
}

void LuaFunctionList::call(NamedLuaFunction::CallbackType c)
{
for (auto& nlf : functions) {
if (nlf.type == c) {
nlf.call();
}
}
}

void LuaFunctionList::clear()
{
functions.clear();
}

}
54 changes: 54 additions & 0 deletions src/program/lua/LuaFunctionList.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
Copyright 2015-2020 Clément Gallet <clement.gallet@ens-lyon.org>
This file is part of libTAS.
libTAS is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
libTAS is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with libTAS. If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef LIBTAS_LUAFUNCTIONLIST_H_INCLUDED
#define LIBTAS_LUAFUNCTIONLIST_H_INCLUDED

#include "NamedLuaFunction.h"

extern "C" {
#include <lua.h>
}
#include <list>
#include <string>

namespace Lua {

class LuaFunctionList {

public:
/* Add a named function from lua stack */
void add(lua_State *L, NamedLuaFunction::CallbackType t);

/* Remove all callbacks from a file */
void removeForFile(const std::string& file);

/* Call all callbacks from a type */
void call(NamedLuaFunction::CallbackType c);

/* Clear all callbacks */
void clear();

private:
std::list<NamedLuaFunction> functions;

};
}

#endif
Loading

0 comments on commit 1149da9

Please sign in to comment.