Cleaned up code for Mixer/Custom/Model scripts and added compiler directives

This commit is contained in:
jfrickmann
2021-07-22 15:11:12 -04:00
parent 76d2ef54e3
commit 5671838864
2 changed files with 97 additions and 81 deletions
+55 -41
View File
@@ -137,34 +137,35 @@ void luaEmptyEventBuffer()
events[1] = 0; events[1] = 0;
} }
int luaGetInputs(lua_State * L2, ScriptInputsOutputs & sid) #if defined(LUA_MODEL_SCRIPTS)
void luaGetInputs(ScriptInputsOutputs & sid)
{ {
if (!lua_istable(L2, -1)) if (!lua_istable(lsScripts, -1))
return -1; return;
memclear(sid.inputs, sizeof(sid.inputs)); memclear(sid.inputs, sizeof(sid.inputs));
sid.inputsCount = 0; sid.inputsCount = 0;
for (lua_pushnil(L2); lua_next(L2, -2); lua_pop(L2, 1)) { for (lua_pushnil(lsScripts); lua_next(lsScripts, -2); lua_pop(lsScripts, 1)) {
luaL_checktype(L2, -2, LUA_TNUMBER); // key is number luaL_checktype(lsScripts, -2, LUA_TNUMBER); // key is number
luaL_checktype(L2, -1, LUA_TTABLE); // value is table luaL_checktype(lsScripts, -1, LUA_TTABLE); // value is table
if (sid.inputsCount<MAX_SCRIPT_INPUTS) { if (sid.inputsCount<MAX_SCRIPT_INPUTS) {
uint8_t field = 0; uint8_t field = 0;
int type = 0; int type = 0;
ScriptInput * si = &sid.inputs[sid.inputsCount]; ScriptInput * si = &sid.inputs[sid.inputsCount];
for (lua_pushnil(L2); lua_next(L2, -2) && field<5; lua_pop(L2, 1), field++) { for (lua_pushnil(lsScripts); lua_next(lsScripts, -2) && field<5; lua_pop(lsScripts, 1), field++) {
switch (field) { switch (field) {
case 0: case 0:
luaL_checktype(L2, -2, LUA_TNUMBER); // key is number luaL_checktype(lsScripts, -2, LUA_TNUMBER); // key is number
luaL_checktype(L2, -1, LUA_TSTRING); // value is string luaL_checktype(lsScripts, -1, LUA_TSTRING); // value is string
lua_xmove(lsScripts, L, 1); // To preserve the string value, move it to the main stack lua_xmove(lsScripts, L, 1); // To preserve the string value, move it to the main stack
lua_pushnil(lsScripts); // Keep the stack balanced lua_pushnil(lsScripts); // Keep the stack balanced
lua_insert(L, -2); // Keep the coroutine at the top of the main stack lua_insert(L, -2); // Keep the coroutine at the top of the main stack
si->name = lua_tostring(L, -2); si->name = lua_tostring(L, -2);
break; break;
case 1: case 1:
luaL_checktype(L2, -2, LUA_TNUMBER); // key is number luaL_checktype(lsScripts, -2, LUA_TNUMBER); // key is number
luaL_checktype(L2, -1, LUA_TNUMBER); // value is number luaL_checktype(lsScripts, -1, LUA_TNUMBER); // value is number
type = lua_tointeger(L2, -1); type = lua_tointeger(lsScripts, -1);
if (type >= INPUT_TYPE_FIRST && type <= INPUT_TYPE_LAST) { if (type >= INPUT_TYPE_FIRST && type <= INPUT_TYPE_LAST) {
si->type = type; si->type = type;
} }
@@ -177,24 +178,24 @@ int luaGetInputs(lua_State * L2, ScriptInputsOutputs & sid)
} }
break; break;
case 2: case 2:
luaL_checktype(L2, -2, LUA_TNUMBER); // key is number luaL_checktype(lsScripts, -2, LUA_TNUMBER); // key is number
luaL_checktype(L2, -1, LUA_TNUMBER); // value is number luaL_checktype(lsScripts, -1, LUA_TNUMBER); // value is number
if (si->type == INPUT_TYPE_VALUE) { if (si->type == INPUT_TYPE_VALUE) {
si->min = lua_tointeger(L2, -1); si->min = lua_tointeger(lsScripts, -1);
} }
break; break;
case 3: case 3:
luaL_checktype(L2, -2, LUA_TNUMBER); // key is number luaL_checktype(lsScripts, -2, LUA_TNUMBER); // key is number
luaL_checktype(L2, -1, LUA_TNUMBER); // value is number luaL_checktype(lsScripts, -1, LUA_TNUMBER); // value is number
if (si->type == INPUT_TYPE_VALUE) { if (si->type == INPUT_TYPE_VALUE) {
si->max = lua_tointeger(L2, -1); si->max = lua_tointeger(lsScripts, -1);
} }
break; break;
case 4: case 4:
luaL_checktype(L2, -2, LUA_TNUMBER); // key is number luaL_checktype(lsScripts, -2, LUA_TNUMBER); // key is number
luaL_checktype(L2, -1, LUA_TNUMBER); // value is number luaL_checktype(lsScripts, -1, LUA_TNUMBER); // value is number
if (si->type == INPUT_TYPE_VALUE) { if (si->type == INPUT_TYPE_VALUE) {
si->def = lua_tointeger(L2, -1); si->def = lua_tointeger(lsScripts, -1);
} }
break; break;
} }
@@ -202,19 +203,17 @@ int luaGetInputs(lua_State * L2, ScriptInputsOutputs & sid)
sid.inputsCount++; sid.inputsCount++;
} }
} }
return 0;
} }
int luaGetOutputs(lua_State * L2, ScriptInputsOutputs & sid) void luaGetOutputs(ScriptInputsOutputs & sid)
{ {
if (!lua_istable(L2, -1)) if (!lua_istable(lsScripts, -1))
return -1; return;
sid.outputsCount = 0; sid.outputsCount = 0;
for (lua_pushnil(L2); lua_next(L2, -2); ) { for (lua_pushnil(lsScripts); lua_next(lsScripts, -2); ) {
luaL_checktype(L2, -2, LUA_TNUMBER); // key is number luaL_checktype(lsScripts, -2, LUA_TNUMBER); // key is number
luaL_checktype(L2, -1, LUA_TSTRING); // value is string luaL_checktype(lsScripts, -1, LUA_TSTRING); // value is string
if (sid.outputsCount < MAX_SCRIPT_OUTPUTS) { if (sid.outputsCount < MAX_SCRIPT_OUTPUTS) {
lua_xmove(lsScripts, L, 1); // To preserve the string value, move it to the main stack lua_xmove(lsScripts, L, 1); // To preserve the string value, move it to the main stack
lua_insert(L, -2); // Keep the coroutine at the top of the main stack lua_insert(L, -2); // Keep the coroutine at the top of the main stack
@@ -222,9 +221,8 @@ int luaGetOutputs(lua_State * L2, ScriptInputsOutputs & sid)
} }
else lua_pop(lsScripts, 1); else lua_pop(lsScripts, 1);
} }
return 0;
} }
#endif
void luaDisable() void luaDisable()
{ {
@@ -540,10 +538,12 @@ static const char * getScriptName(uint8_t idx)
{ {
int ref = scriptInternalData[idx].reference; int ref = scriptInternalData[idx].reference;
#if defined(LUA_MODEL_SCRIPTS)
if (ref <= SCRIPT_MIX_LAST) { if (ref <= SCRIPT_MIX_LAST) {
return g_model.scriptsData[ref - SCRIPT_MIX_FIRST].file; return g_model.scriptsData[ref - SCRIPT_MIX_FIRST].file;
} } else
else if (ref <= SCRIPT_FUNC_LAST) { #endif
if (ref <= SCRIPT_FUNC_LAST) {
return g_model.customFn[ref - SCRIPT_FUNC_FIRST].play.name; return g_model.customFn[ref - SCRIPT_FUNC_FIRST].play.name;
} }
else if (ref <= SCRIPT_GFUNC_LAST) { else if (ref <= SCRIPT_GFUNC_LAST) {
@@ -571,6 +571,7 @@ static bool luaLoad(const char * filename, ScriptInternalData & sid)
return false; return false;
} }
#if defined(LUA_MODEL_SCRIPTS)
static bool luaLoadMixScript(uint8_t ref) static bool luaLoadMixScript(uint8_t ref)
{ {
uint8_t idx = ref - SCRIPT_MIX_FIRST; uint8_t idx = ref - SCRIPT_MIX_FIRST;
@@ -589,6 +590,7 @@ static bool luaLoadMixScript(uint8_t ref)
} }
return false; return false;
} }
#endif
static bool luaLoadFunctionScript(uint8_t ref) static bool luaLoadFunctionScript(uint8_t ref)
{ {
@@ -811,7 +813,11 @@ static void luaLoadScripts(bool init, const char * filename = nullptr)
ref = SCRIPT_STANDALONE; ref = SCRIPT_STANDALONE;
} }
else { else {
#if defined(LUA_MODEL_SCRIPTS)
ref = SCRIPT_MIX_FIRST; ref = SCRIPT_MIX_FIRST;
#else
ref = SCRIPT_FUNC_FIRST;
#endif
} }
} }
@@ -822,10 +828,12 @@ static void luaLoadScripts(bool init, const char * filename = nullptr)
// If Lua is not yielded, then find the next script to load // If Lua is not yielded, then find the next script to load
if (luaStatus == LUA_OK) { if (luaStatus == LUA_OK) {
#if defined(LUA_MODEL_SCRIPTS)
if (ref <= SCRIPT_MIX_LAST) { if (ref <= SCRIPT_MIX_LAST) {
if (luaLoadMixScript(ref)) continue; // If error then skip the rest of the loop if (luaLoadMixScript(ref)) continue; // If error then skip the rest of the loop
} } else
else if (ref <= SCRIPT_GFUNC_LAST) { #endif
if (ref <= SCRIPT_GFUNC_LAST) {
if (luaLoadFunctionScript(ref)) continue; if (luaLoadFunctionScript(ref)) continue;
} }
#if defined(PCBTARANIS) #if defined(PCBTARANIS)
@@ -880,16 +888,18 @@ static void luaLoadScripts(bool init, const char * filename = nullptr)
TRACE_ERROR("luaLoadScripts(%s): No run function\n", getScriptName(idx)); TRACE_ERROR("luaLoadScripts(%s): No run function\n", getScriptName(idx));
sid.state = SCRIPT_SYNTAX_ERROR; sid.state = SCRIPT_SYNTAX_ERROR;
} }
#if defined(LUA_MODEL_SCRIPTS)
// Get input/output tables for mixer scripts // Get input/output tables for mixer scripts
if (ref <= SCRIPT_MIX_LAST) { if (ref <= SCRIPT_MIX_LAST) {
ScriptInputsOutputs * sio = & scriptInputsOutputs[idx]; ScriptInputsOutputs * sio = & scriptInputsOutputs[idx];
lua_getfield(lsScripts, -1, "input"); lua_getfield(lsScripts, -1, "input");
luaGetInputs(lsScripts, *sio); luaGetInputs(*sio);
lua_pop(lsScripts, 1); lua_pop(lsScripts, 1);
lua_getfield(lsScripts, -1, "output"); lua_getfield(lsScripts, -1, "output");
luaGetOutputs(lsScripts, *sio); luaGetOutputs(*sio);
lua_pop(lsScripts, 1); lua_pop(lsScripts, 1);
} }
#endif
} }
else { else {
TRACE_ERROR("luaLoadScripts(%s): The script did not return a table\n", getScriptName(idx)); TRACE_ERROR("luaLoadScripts(%s): The script did not return a table\n", getScriptName(idx));
@@ -994,6 +1004,7 @@ static bool resumeLua(bool init, bool allowLcdUsage)
else continue; else continue;
} }
else { else {
#if defined(LUA_MODEL_SCRIPTS)
if (ref <= SCRIPT_MIX_LAST) { if (ref <= SCRIPT_MIX_LAST) {
lua_rawgeti(lsScripts, LUA_REGISTRYINDEX, sid.run); lua_rawgeti(lsScripts, LUA_REGISTRYINDEX, sid.run);
@@ -1007,8 +1018,9 @@ static bool resumeLua(bool init, bool allowLcdUsage)
else else
lua_pushinteger(lsScripts, sd.inputs[j].value + sio -> inputs[j].def); lua_pushinteger(lsScripts, sd.inputs[j].value + sio -> inputs[j].def);
} }
} } else
else if (ref <= SCRIPT_GFUNC_LAST) { #endif
if (ref <= SCRIPT_GFUNC_LAST) {
CustomFunctionData * fn; CustomFunctionData * fn;
if (ref <= SCRIPT_FUNC_LAST) if (ref <= SCRIPT_FUNC_LAST)
@@ -1049,6 +1061,7 @@ static bool resumeLua(bool init, bool allowLcdUsage)
// Coroutine returned // Coroutine returned
scriptWasRun = true; scriptWasRun = true;
#if defined(LUA_MODEL_SCRIPTS)
if (ref <= SCRIPT_MIX_LAST) { if (ref <= SCRIPT_MIX_LAST) {
ScriptInputsOutputs * sio = & scriptInputsOutputs[ref - SCRIPT_MIX_FIRST]; ScriptInputsOutputs * sio = & scriptInputsOutputs[ref - SCRIPT_MIX_FIRST];
lua_settop(lsScripts, sio -> outputsCount); lua_settop(lsScripts, sio -> outputsCount);
@@ -1062,8 +1075,9 @@ static bool resumeLua(bool init, bool allowLcdUsage)
sio -> outputs[j].value = lua_tointeger(lsScripts, -1); sio -> outputs[j].value = lua_tointeger(lsScripts, -1);
lua_pop(lsScripts, 1); lua_pop(lsScripts, 1);
} }
} } else
else if (ref == SCRIPT_STANDALONE) { #endif
if (ref == SCRIPT_STANDALONE) {
lua_settop(lsScripts, 1); lua_settop(lsScripts, 1);
if (lua_isnumber(lsScripts, -1)) { if (lua_isnumber(lsScripts, -1)) {
int scriptResult = lua_tointeger(lsScripts, -1); int scriptResult = lua_tointeger(lsScripts, -1);
+2
View File
@@ -111,8 +111,10 @@ enum ScriptState {
}; };
enum ScriptReference { enum ScriptReference {
#if defined(LUA_MODEL_SCRIPTS)
SCRIPT_MIX_FIRST, SCRIPT_MIX_FIRST,
SCRIPT_MIX_LAST=SCRIPT_MIX_FIRST+MAX_SCRIPTS-1, SCRIPT_MIX_LAST=SCRIPT_MIX_FIRST+MAX_SCRIPTS-1,
#endif
SCRIPT_FUNC_FIRST, SCRIPT_FUNC_FIRST,
SCRIPT_FUNC_LAST=SCRIPT_FUNC_FIRST+MAX_SPECIAL_FUNCTIONS-1, // model functions SCRIPT_FUNC_LAST=SCRIPT_FUNC_FIRST+MAX_SPECIAL_FUNCTIONS-1, // model functions
SCRIPT_GFUNC_FIRST, SCRIPT_GFUNC_FIRST,