Lua: refactor script api, more doc, add button for Open Lua Scripts Folder (#1588)

* lua manager: pass down the scripts folder to the instance instead of hard coding calls to the file manager everywhere
* lua: add open lua scripts folder button
* lua api: change script binding so that user cannot by mistake try to sleep or yield in a non script context
This commit is contained in:
Quentin
2023-07-03 13:01:12 +02:00
committed by GitHub
parent 7c927e0cfb
commit 6d6848c2fb
8 changed files with 186 additions and 58 deletions

View File

@ -6,6 +6,33 @@
namespace lua::script
{
// Lua API: Class
// Name: script_util
// Class for gta script utils, the instance is usually given to you.
class script_util
{
public:
// Lua API: Function
// Class: script_util
// Name: yield
// Yield execution.
void yield()
{
big::script::get_current()->yield();
}
// Lua API: Function
// Class: script_util
// Name: sleep
// Param: ms: integer: The amount of time in milliseconds that we will sleep for.
// Sleep for the given amount of time, time is in milliseconds.
void sleep(int ms)
{
big::script::get_current()->yield(std::chrono::milliseconds(ms));
}
};
static script_util dummy_script_util;
// Lua API: Table
// Name: script
// Table containing helper functions related to gta scripts.
@ -16,6 +43,29 @@ namespace lua::script
// Param: name: string: name of your new looped script
// Param: func: function: function that will be executed in a forever loop.
// Registers a function that will be looped as a gta script.
// **Exemple Usage:**
// ```lua
// script.register_looped("nameOfMyLoopedScript", function (script)
// -- sleep until next game frame
// script:yield()
//
// local ModelHash = joaat("adder")
// if not STREAMING.IS_MODEL_IN_CDIMAGE(ModelHash) then return end
// STREAMING.REQUEST_MODEL(ModelHash) -- Request the model
// while not STREAMING.HAS_MODEL_LOADED(ModelHash) do -- Waits for the model to load
// script:yield()
// end
// local myPed = PLAYER.PLAYER_PED_ID()
// local myCoords = ENTITY.GET_ENTITY_COORDS(myPed, true)
// -- Spawns a networked vehicle on your current coords
// local spawnedVehicle = VEHICLE.CREATE_VEHICLE(ModelHash, myCoords.x, myCoords.y, myCoords.z, ENTITY.GET_ENTITY_HEADING(myPed), true, false)
// -- removes model from game memory as we no longer need it
// STREAMING.SET_MODEL_AS_NO_LONGER_NEEDED(ModelHash)
// -- sleep for 2s
// script:sleep(2000)
// ENTITY.DELETE_ENTITY(spawnedVehicle)
// end)
// ```
static void register_looped(const std::string& name, sol::function func, sol::this_state state)
{
auto module = sol::state_view(state)["!this"].get<big::lua_module*>();
@ -24,7 +74,8 @@ namespace lua::script
[func] {
while (big::g_running)
{
auto res = func();
auto res = func(dummy_script_util);
if (!res.valid())
big::g_lua_manager->handle_error(res, res.lua_state());
@ -37,42 +88,51 @@ namespace lua::script
// Lua API: Function
// Table: script
// Name: run_in_fiber
// Param: func: function: function that will be executed once in the fiber pool, you can call natives inside it.
// Executes a function inside the fiber pool, you can call natives inside it.
// Param: func: function: function that will be executed once in the fiber pool.
// Executes a function once inside the fiber pool, you can call natives inside it and yield or sleep.
// **Exemple Usage:**
// ```lua
// script.run_in_fiber(function (script)
// -- sleep until next game frame
// script:yield()
//
// local ModelHash = joaat("adder")
// if not STREAMING.IS_MODEL_IN_CDIMAGE(ModelHash) then return end
// STREAMING.REQUEST_MODEL(ModelHash) -- Request the model
// while not STREAMING.HAS_MODEL_LOADED(ModelHash) do -- Waits for the model to load
// script:yield()
// end
// local myPed = PLAYER.PLAYER_PED_ID()
// local myCoords = ENTITY.GET_ENTITY_COORDS(myPed, true)
// -- Spawns a networked vehicle on your current coords
// local spawnedVehicle = VEHICLE.CREATE_VEHICLE(ModelHash, myCoords.x, myCoords.y, myCoords.z, ENTITY.GET_ENTITY_HEADING(myPed), true, false)
// -- removes model from game memory as we no longer need it
// STREAMING.SET_MODEL_AS_NO_LONGER_NEEDED(ModelHash)
// -- sleep for 2s
// script:sleep(2000)
// ENTITY.DELETE_ENTITY(spawnedVehicle)
// end)
// ```
static void run_in_fiber(sol::function func)
{
big::g_fiber_pool->queue_job([func] {
auto res = func();
auto res = func(dummy_script_util);
if (!res.valid())
big::g_lua_manager->handle_error(res, res.lua_state());
});
}
// Lua API: Function
// Table: script
// Name: yield
// Yield execution.
static void yield()
{
big::script::get_current()->yield();
}
// Lua API: Function
// Table: script
// Name: sleep
// Param: ms: integer: The amount of time in milliseconds that we will sleep for.
// Sleep for the given amount of time, time is in milliseconds.
static void sleep(int ms)
{
big::script::get_current()->yield(std::chrono::milliseconds(ms));
}
static void bind(sol::state& state)
{
auto ns = state["script"].get_or_create<sol::table>();
ns["register_looped"] = register_looped;
ns["run_in_fiber"] = run_in_fiber;
ns["yield"] = yield;
ns["sleep"] = sleep;
//clang-format off
state.new_usertype<script_util>("script_util",
"yield", &script_util::yield,
"sleep", &script_util::sleep);
//clang-format on
}
}