Moved code to resume/tick script to celx.cpp
parent
e0817161f2
commit
d133e6138c
|
@ -310,7 +310,6 @@ CelestiaCore::CelestiaCore() :
|
|||
execEnv(NULL),
|
||||
#ifdef CELX
|
||||
celxScript(NULL),
|
||||
scriptAwakenTime(0.0),
|
||||
#endif // CELX
|
||||
timeZoneBias(0),
|
||||
showFPSCounter(false),
|
||||
|
@ -491,7 +490,6 @@ void CelestiaCore::cancelScript()
|
|||
delete celxScript;
|
||||
celxScript = NULL;
|
||||
scriptPaused = false;
|
||||
scriptAwakenTime = 0.0;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
@ -575,12 +573,7 @@ void CelestiaCore::runScript(const string& filename)
|
|||
{
|
||||
// Coroutine execution; control may be transferred between the
|
||||
// script and Celestia's event loop
|
||||
if (celxScript->createThread())
|
||||
{
|
||||
// Awaken the script ASAP (0.0 is invalid)
|
||||
scriptAwakenTime = 0.1;
|
||||
}
|
||||
else
|
||||
if (!celxScript->createThread())
|
||||
{
|
||||
const char* errMsg = "Script coroutine initialization failed";
|
||||
if (alerter != NULL)
|
||||
|
@ -603,42 +596,6 @@ void CelestiaCore::runScript(const string& filename)
|
|||
}
|
||||
|
||||
|
||||
void CelestiaCore::resumeScript()
|
||||
{
|
||||
#ifdef CELX
|
||||
if (celxScript != NULL)
|
||||
{
|
||||
int nArgs = celxScript->resume();
|
||||
if (celxScript == NULL) {
|
||||
return;
|
||||
}
|
||||
if (!celxScript->isAlive())
|
||||
{
|
||||
// The script is complete
|
||||
cancelScript();
|
||||
}
|
||||
else
|
||||
{
|
||||
// The script has returned control to us, but it is not completed.
|
||||
lua_State* state = celxScript->getState();
|
||||
|
||||
// The values on the stack indicate what event will wake up the
|
||||
// script. For now, we just support wait()
|
||||
double delay;
|
||||
if (nArgs == 1 && lua_isnumber(state, -1))
|
||||
delay = lua_tonumber(state, -1);
|
||||
else
|
||||
delay = 0.0;
|
||||
scriptAwakenTime = currentTime + delay;
|
||||
|
||||
// Clean up the stack
|
||||
lua_pop(state, nArgs);
|
||||
}
|
||||
}
|
||||
#endif // CELX
|
||||
}
|
||||
|
||||
|
||||
bool checkMask(int modifiers, int mask)
|
||||
{
|
||||
return (modifiers & mask) == mask;
|
||||
|
@ -2146,10 +2103,11 @@ void CelestiaCore::tick()
|
|||
}
|
||||
|
||||
#ifdef CELX
|
||||
if (celxScript != NULL && scriptAwakenTime != 0.0)
|
||||
if (celxScript != NULL)
|
||||
{
|
||||
if (currentTime >= scriptAwakenTime)
|
||||
resumeScript();
|
||||
bool finished = celxScript->tick(dt);
|
||||
if (finished)
|
||||
cancelScript();
|
||||
}
|
||||
#endif // CELX
|
||||
|
||||
|
|
|
@ -353,7 +353,6 @@ class CelestiaCore // : public Watchable<CelestiaCore>
|
|||
|
||||
#ifdef CELX
|
||||
LuaState* celxScript;
|
||||
double scriptAwakenTime;
|
||||
#endif // CELX
|
||||
|
||||
int timeZoneBias; // Diff in secs between local time and GMT
|
||||
|
|
|
@ -351,7 +351,8 @@ LuaState::LuaState() :
|
|||
state(NULL),
|
||||
costate(NULL),
|
||||
alive(false),
|
||||
timer(NULL)
|
||||
timer(NULL),
|
||||
scriptAwakenTime(0.1)
|
||||
{
|
||||
state = lua_open();
|
||||
timer = CreateTimer();
|
||||
|
@ -683,6 +684,49 @@ static void doError(lua_State* l, const char* errorMsg)
|
|||
}
|
||||
|
||||
|
||||
bool LuaState::tick(double dt)
|
||||
{
|
||||
// Due to the way CelestiaCore::tick is called (at least for KDE),
|
||||
// this method may be entered a second time when we show the error-alerter
|
||||
// Workaround: check if we are alive, return true(!) when we aren't anymore
|
||||
// this way the script isn't deleted after the second enter, but only
|
||||
// when we return from the first enter. OMG.
|
||||
|
||||
// better Solution: defer showing the error-alterter to CelestiaCore, using
|
||||
// getErrorMessage()
|
||||
if (!isAlive())
|
||||
return false;
|
||||
|
||||
if (scriptAwakenTime > getTime())
|
||||
return false;
|
||||
|
||||
int nArgs = resume();
|
||||
if (!isAlive())
|
||||
{
|
||||
// The script is complete
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
// The script has returned control to us, but it is not completed.
|
||||
lua_State* state = getState();
|
||||
|
||||
// The values on the stack indicate what event will wake up the
|
||||
// script. For now, we just support wait()
|
||||
double delay;
|
||||
if (nArgs == 1 && lua_isnumber(state, -1))
|
||||
delay = lua_tonumber(state, -1);
|
||||
else
|
||||
delay = 0.0;
|
||||
scriptAwakenTime = getTime() + delay;
|
||||
|
||||
// Clean up the stack
|
||||
lua_pop(state, nArgs);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Check if the number of arguments on the stack matches
|
||||
// the allowed range [minArgs, maxArgs]. Cause an error if not.
|
||||
static void checkArgs(lua_State* l,
|
||||
|
|
|
@ -39,6 +39,7 @@ public:
|
|||
|
||||
bool createThread();
|
||||
int resume();
|
||||
bool tick(double);
|
||||
void cleanup();
|
||||
bool isAlive() const;
|
||||
bool timesliceExpired();
|
||||
|
@ -52,6 +53,7 @@ private:
|
|||
lua_State* costate; // coroutine stack
|
||||
bool alive;
|
||||
Timer* timer;
|
||||
double scriptAwakenTime;
|
||||
};
|
||||
|
||||
View* getViewByObserver(CelestiaCore*, Observer*);
|
||||
|
|
Loading…
Reference in New Issue