-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathscript.cpp
More file actions
212 lines (187 loc) · 6.7 KB
/
Copy pathscript.cpp
File metadata and controls
212 lines (187 loc) · 6.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#include "script.hpp"
#include "lua/lua_manager.hpp"
#include "gta_util.hpp"
#include "memory/pattern.hpp"
#include "util/scripts.hpp"
namespace lua::script
{
static script_util dummy_script_util;
int script_util::yield()
{
return 0;
}
int script_util::sleep(int ms)
{
return ms;
}
// Lua API: Table
// Name: script
// Table containing helper functions related to gta scripts.
// Lua API: Function
// Table: script
// Name: register_looped
// 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.
// **Example 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::protected_function func_, sol::this_state state)
{
big::lua_module* module = sol::state_view(state)["!this"];
std::unique_ptr<big::script> lua_script = std::make_unique<big::script>(
[func_, state]() mutable {
sol::thread t = sol::thread::create(state);
sol::coroutine func = sol::coroutine(t.state(), func_);
while (big::g_running)
{
auto res = func(dummy_script_util);
if (!res.valid())
{
big::g_lua_manager->handle_error(res, res.lua_state());
break;
}
if (func.runnable())
{
big::script::get_current()->yield(std::chrono::milliseconds(res.return_count() ? res[0] : 0));
}
else
{
big::script::get_current()->yield();
}
}
},
name);
module->m_registered_scripts.push_back(std::move(lua_script));
}
// Lua API: Function
// Table: script
// Name: run_in_fiber
// 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.
// **Example 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::protected_function func_, sol::this_state state)
{
big::lua_module* module = sol::state_view(state)["!this"];
static size_t name_i = 0;
std::string job_name = module->module_name() + std::to_string(name_i++);
// We make a new script for lua state destruction timing purposes, see lua_module dctor for more info.
std::unique_ptr<big::script> lua_script = std::make_unique<big::script>(
[func_, state]() mutable {
sol::thread t = sol::thread::create(state);
sol::coroutine func = sol::coroutine(t.state(), func_);
while (big::g_running)
{
auto res = func(dummy_script_util);
if (!res.valid())
{
big::g_lua_manager->handle_error(res, res.lua_state());
break;
}
if (func.runnable())
{
big::script::get_current()->yield(std::chrono::milliseconds(res.return_count() ? res[0] : 0));
}
else
{
break;
}
}
},
job_name);
module->m_registered_scripts.push_back(std::move(lua_script));
}
// Lua API: function
// Table: script
// Name: is_active
// Param: script_name: string: The name of the script.
// Returns true if the specified script is currently active/running.
// **Example Usage:**
// ```lua
// local is_freemode_active = script.is_active("freemode")
// ```
static bool is_active(const std::string& script_name)
{
if (auto script = big::gta_util::find_script_thread(rage::joaat(script_name)))
return true;
return false;
}
// Lua API: function
// Table: script
// Name: execute_as_script
// Param: script_name: string: Target script thread.
// Param: func: function: Function that will be executed once in the script thread.
static void execute_as_script(const std::string& script_name, sol::protected_function func)
{
big::gta_util::execute_as_script(rage::joaat(script_name), func);
}
// Lua API: function
// Table: script
// Name: start_launcher_script
// Param: script_name: string: The name of the script.
// Tries to start a launcher script. Needs to be called in the fiber pool or a loop.
// **Example Usage:**
// ```lua
// script.run_in_fiber(function()
// script.start_launcher_script("am_hunt_the_beast")
// end)
// ```
static void start_launcher_script(const std::string& script_name)
{
LOG(WARNING) << "start_launcher_script is not implemented";
}
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["is_active"] = is_active;
ns["execute_as_script"] = execute_as_script;
ns["start_launcher_script"] = start_launcher_script;
auto usertype = state.new_usertype<script_util>("script_util");
usertype["yield"] = sol::yielding(&script_util::yield);
usertype["sleep"] = sol::yielding(&script_util::sleep);
}
}