1
0
Fork 0
mirror of https://github.com/opentx/opentx.git synced 2025-07-16 04:45:17 +03:00

[Lua] Allow global functions to play scripts. (#5440)

This commit is contained in:
Max Paperno 2017-11-30 13:10:11 -05:00 committed by Bertrand Songis
parent 1d5c06ccd6
commit ba1073ace6
2 changed files with 10 additions and 8 deletions

View file

@ -112,7 +112,7 @@ void luaHook(lua_State * L, lua_Debug *ar)
else if (instructionsPercent < 10) {
max = 0;
}
#else
#else
if (instructionsPercent > 100) {
// From now on, as soon as a line is executed, error
// keep erroring until you're script reaches the top
@ -631,14 +631,14 @@ bool luaLoadMixScript(uint8_t index)
return true;
}
bool luaLoadFunctionScript(uint8_t index)
bool luaLoadFunctionScript(uint8_t index, uint8_t ref)
{
CustomFunctionData & fn = g_model.customFn[index];
CustomFunctionData & fn = (ref < SCRIPT_GFUNC_FIRST ? g_model.customFn[index] : g_eeGeneral.customFn[index]);
if (fn.func == FUNC_PLAY_SCRIPT && ZEXIST(fn.play.name)) {
if (luaScriptsCount < MAX_SCRIPTS) {
ScriptInternalData & sid = scriptInternalData[luaScriptsCount++];
sid.reference = SCRIPT_FUNC_FIRST+index;
sid.reference = ref + index;
sid.state = SCRIPT_NOFILE;
char filename[sizeof(SCRIPTS_FUNCS_PATH)+sizeof(fn.play.name)+sizeof(SCRIPT_EXT)] = SCRIPTS_FUNCS_PATH "/";
strncpy(filename+sizeof(SCRIPTS_FUNCS_PATH), fn.play.name, sizeof(fn.play.name));
@ -712,7 +712,7 @@ void luaLoadPermanentScripts()
// Load custom function scripts
for (int i=0; i<MAX_SPECIAL_FUNCTIONS; i++) {
if (!luaLoadFunctionScript(i)) {
if (!luaLoadFunctionScript(i, SCRIPT_FUNC_FIRST) || !luaLoadFunctionScript(i, SCRIPT_GFUNC_FIRST)) {
return;
}
}
@ -923,8 +923,8 @@ bool luaDoOneRunPermanentScript(event_t evt, int i, uint32_t scriptType)
lua_pushinteger(lsScripts, sd.inputs[j].value + sio->inputs[j].def);
}
}
else if ((scriptType & RUN_FUNC_SCRIPT) && (sid.reference >= SCRIPT_FUNC_FIRST && sid.reference <= SCRIPT_FUNC_LAST)) {
CustomFunctionData & fn = g_model.customFn[sid.reference-SCRIPT_FUNC_FIRST];
else if ((scriptType & RUN_FUNC_SCRIPT) && (sid.reference >= SCRIPT_FUNC_FIRST && sid.reference <= SCRIPT_GFUNC_LAST)) {
CustomFunctionData & fn = (sid.reference < SCRIPT_GFUNC_FIRST ? g_model.customFn[sid.reference-SCRIPT_FUNC_FIRST] : g_eeGeneral.customFn[sid.reference-SCRIPT_GFUNC_FIRST]);
#if defined(SIMU) || defined(DEBUG)
filename = fn.play.name;
#endif

View file

@ -98,7 +98,9 @@ enum ScriptReference {
SCRIPT_MIX_FIRST,
SCRIPT_MIX_LAST=SCRIPT_MIX_FIRST+MAX_SCRIPTS-1,
SCRIPT_FUNC_FIRST,
SCRIPT_FUNC_LAST=SCRIPT_FUNC_FIRST+MAX_SPECIAL_FUNCTIONS-1,
SCRIPT_FUNC_LAST=SCRIPT_FUNC_FIRST+MAX_SPECIAL_FUNCTIONS-1, // model functions
SCRIPT_GFUNC_FIRST,
SCRIPT_GFUNC_LAST=SCRIPT_GFUNC_FIRST+MAX_SPECIAL_FUNCTIONS-1, // global functions
SCRIPT_TELEMETRY_FIRST,
SCRIPT_TELEMETRY_LAST=SCRIPT_TELEMETRY_FIRST+MAX_SCRIPTS, // telem0 and telem1 .. telem7
};