Skip to content

Commit

Permalink
Split LuaHandle into Draw/Sim (can be disabled via DUAL_LUA_STATES)
Browse files Browse the repository at this point in the history
  • Loading branch information
zerver committed Sep 23, 2010
1 parent 781290e commit a628b04
Show file tree
Hide file tree
Showing 30 changed files with 746 additions and 459 deletions.
31 changes: 18 additions & 13 deletions rts/Game/UI/KeyAutoBinder.cpp
Expand Up @@ -11,6 +11,7 @@
#include "KeyBindings.h"
#include "Game/GameSetup.h"
#include "Sim/Misc/Team.h"
#include "Lua/LuaCallInCheck.h"
#include "Lua/LuaConstGame.h"
#include "Lua/LuaUnitDefs.h"
#include "Lua/LuaWeaponDefs.h"
Expand Down Expand Up @@ -54,11 +55,12 @@ static const string endlStr = "\r\n";
CKeyAutoBinder::CKeyAutoBinder()
: CLuaHandle("KeyAutoBinder", 1234, false)
{
if (L == NULL) {
if (!IsValid())
return;
}

LoadCompareFunc();
BEGIN_ITERATE_LUA_STATES();

LoadCompareFunc(L);

// load the standard libraries
LUA_OPEN_LIB(L, luaopen_base);
Expand All @@ -77,7 +79,10 @@ CKeyAutoBinder::CKeyAutoBinder()
!AddEntriesToTable(L, "WeaponDefs", LuaWeaponDefs::PushEntries)) {
logOutput.Print("KeyAutoBinder: error loading lua libraries\n");
}
lua_settop(L, 0);

lua_settop(L, 0);

END_ITERATE_LUA_STATES();
}


Expand All @@ -98,11 +103,10 @@ string CKeyAutoBinder::LoadFile(const string& filename) const
}


bool CKeyAutoBinder::LoadCode(const string& code, const string& debug)
bool CKeyAutoBinder::LoadCode(lua_State *L, const string& code, const string& debug)
{
if (L == NULL) {
if (!IsValid())
return false;
}

lua_settop(L, 0);

Expand All @@ -126,7 +130,7 @@ bool CKeyAutoBinder::LoadCode(const string& code, const string& debug)
}


bool CKeyAutoBinder::LoadCompareFunc()
bool CKeyAutoBinder::LoadCompareFunc(lua_State *L)
{
string code = endlStr;

Expand All @@ -149,7 +153,7 @@ bool CKeyAutoBinder::LoadCompareFunc()
logOutput.Print(code);
}

if (!LoadCode(code, "Compare()")) {
if (!LoadCode(L, code, "Compare()")) {
return false;
}

Expand Down Expand Up @@ -177,9 +181,10 @@ bool CKeyAutoBinder::BindBuildType(const string& keystr,
const vector<string>& sortCriteria,
const vector<string>& chords)
{
if (L == NULL) {
if (!IsValid())
return false;
}

SELECT_LUA_STATE();

lua_settop(L, 0);

Expand All @@ -193,8 +198,8 @@ bool CKeyAutoBinder::BindBuildType(const string& keystr,
logOutput.Print(sortCall);
}

if (!LoadCode(reqCall, keystr + ":HasReqs()") ||
!LoadCode(sortCall, keystr + ":IsBetter()")) {
if (!LoadCode(L, reqCall, keystr + ":HasReqs()") ||
!LoadCode(L, sortCall, keystr + ":IsBetter()")) {
return false;
}

Expand Down
4 changes: 2 additions & 2 deletions rts/Game/UI/KeyAutoBinder.h
Expand Up @@ -26,8 +26,8 @@ class CKeyAutoBinder : public CLuaHandle

private:
string LoadFile(const string& filename) const;
bool LoadCode(const string& code, const string& debug);
bool LoadCompareFunc();
bool LoadCode(lua_State *L, const string& code, const string& debug);
bool LoadCompareFunc(lua_State *L);
string MakeRequirementCall(const vector<string>& requirements);
string MakeSortCriteriaCall(const vector<string>& sortCriteria);
string AddUnitDefPrefix(const string& text, const string& pre) const;
Expand Down
43 changes: 25 additions & 18 deletions rts/Game/UI/LuaUI.cpp
Expand Up @@ -97,7 +97,7 @@ void CLuaUI::LoadHandler()

new CLuaUI();

if (luaUI->L == NULL) {
if (!luaUI->IsValid()) {
delete luaUI;
}
}
Expand All @@ -121,7 +121,9 @@ CLuaUI::CLuaUI()
{
luaUI = this;

if (L == NULL) {
BEGIN_ITERATE_LUA_STATES();

if (!IsValid()) {
return;
}

Expand Down Expand Up @@ -172,7 +174,7 @@ CLuaUI::CLuaUI()

lua_pushvalue(L, LUA_GLOBALSINDEX);

AddBasicCalls(); // into Global
AddBasicCalls(L); // into Global

lua_pushstring(L, "Script");
lua_rawget(L, -2);
Expand Down Expand Up @@ -203,7 +205,7 @@ CLuaUI::CLuaUI()
}

lua_settop(L, 0);
if (!LoadCode(code, "luaui.lua")) {
if (!LoadCode(L, code, "luaui.lua")) {
KillLua();
return;
}
Expand All @@ -212,16 +214,18 @@ CLuaUI::CLuaUI()
eventHandler.AddClient(this);

// update extra call-ins
UnsyncedUpdateCallIn("WorldTooltip");
UnsyncedUpdateCallIn("MapDrawCmd");
UnsyncedUpdateCallIn(L, "WorldTooltip");
UnsyncedUpdateCallIn(L, "MapDrawCmd");

lua_settop(L, 0);

END_ITERATE_LUA_STATES();
}


CLuaUI::~CLuaUI()
{
if (L != NULL) {
if (L_Sim != NULL || L_Draw != NULL) {
Shutdown();
KillLua();
}
Expand Down Expand Up @@ -253,9 +257,9 @@ string CLuaUI::LoadFile(const string& filename) const
}


bool CLuaUI::HasCallIn(const string& name)
bool CLuaUI::HasCallIn(lua_State *L, const string& name)
{
if (L == NULL) {
if (!IsValid()) {
return false;
}

Expand All @@ -274,14 +278,14 @@ bool CLuaUI::HasCallIn(const string& name)
}


bool CLuaUI::UnsyncedUpdateCallIn(const string& name)
bool CLuaUI::UnsyncedUpdateCallIn(lua_State *L, const string& name)
{
// never allow this call-in
if (name == "Explosion") {
return false;
}

if (HasCallIn(name)) {
if (HasCallIn(L, name)) {
eventHandler.InsertEvent(this, name);
} else {
eventHandler.RemoveEvent(this, name);
Expand All @@ -293,13 +297,13 @@ bool CLuaUI::UnsyncedUpdateCallIn(const string& name)
void CLuaUI::UpdateTeams()
{
if (luaUI) {
luaUI->fullCtrl = gs->godMode;
luaUI->ctrlTeam = gs->godMode ? AllAccessTeam :
(gu->spectating ? NoAccessTeam : gu->myTeam);
luaUI->fullRead = gu->spectatingFullView;
luaUI->readTeam = luaUI->fullRead ? AllAccessTeam : gu->myTeam;
luaUI->readAllyTeam = luaUI->fullRead ? AllAccessTeam : gu->myAllyTeam;
luaUI->selectTeam = gu->spectatingFullSelect ? AllAccessTeam : gu->myTeam;
luaUI->SetFullCtrl(gs->godMode);
luaUI->SetCtrlTeam(gs->godMode ? AllAccessTeam :
(gu->spectating ? NoAccessTeam : gu->myTeam));
luaUI->SetFullRead(gu->spectatingFullView);
luaUI->SetReadTeam(luaUI->GetFullRead() ? AllAccessTeam : gu->myTeam);
luaUI->SetReadAllyTeam(luaUI->GetFullRead() ? AllAccessTeam : gu->myAllyTeam);
luaUI->SetSelectTeam(gu->spectatingFullSelect ? AllAccessTeam : gu->myTeam);
}
}

Expand Down Expand Up @@ -475,6 +479,7 @@ void CLuaUI::ShockFront(float power, const float3& pos, float areaOfEffect)
bool CLuaUI::HasLayoutButtons()
{
GML_RECMUTEX_LOCK(lua); // HasLayoutButtons
SELECT_LUA_STATE();

lua_checkstack(L, 2);

Expand Down Expand Up @@ -833,6 +838,8 @@ bool CLuaUI::GetLuaCmdDescList(lua_State* L, int index,

bool CLuaUI::HasUnsyncedXCall(const string& funcName)
{
SELECT_LUA_STATE();

lua_getglobal(L, funcName.c_str());
const bool haveFunc = lua_isfunction(L, -1);
lua_pop(L, 1);
Expand Down
4 changes: 2 additions & 2 deletions rts/Game/UI/LuaUI.h
Expand Up @@ -43,8 +43,8 @@ class CLuaUI : public CLuaHandle
};

public: // call-ins
bool HasCallIn(const string& name);
bool UnsyncedUpdateCallIn(const string& name);
bool HasCallIn(lua_State *L, const string& name);
bool UnsyncedUpdateCallIn(lua_State *L, const string& name);

void GameFrame(int frameNum);

Expand Down
36 changes: 32 additions & 4 deletions rts/Lua/LuaCallInCheck.h
Expand Up @@ -18,10 +18,38 @@ class LuaCallInCheck {
};


#define DUAL_LUA_STATES 1

#if DUAL_LUA_STATES
#define BEGIN_ITERATE_LUA_STATES() lua_State *L_Cur = L_Sim; do { lua_State * const L = L_Cur
#define END_ITERATE_LUA_STATES() if(L_Cur == L_Draw) break; L_Cur = L_Draw; } while(true)
#ifndef LUA_SYNCED_ONLY
#define SELECT_LUA_STATE() lua_State * const L = Threading::IsSimThread() ? L_Sim : L_Draw
#else
#define SELECT_LUA_STATE()
#endif
#if defined(USE_GML) && GML_ENABLE_SIM
#define GML_DRCMUTEX_LOCK(name) boost::recursive_mutex::scoped_lock name##lock(Threading::IsSimThread() ? name##simmutex : name##drawmutex )
#else
#define GML_DRCMUTEX_LOCK(name)
#endif

#else

#define BEGIN_ITERATE_LUA_STATES() lua_State * const L = L_Sim
#define END_ITERATE_LUA_STATES()
#ifndef LUA_SYNCED_ONLY
#define SELECT_LUA_STATE() lua_State * const L = L_Sim
#else
#define SELECT_LUA_STATE()
#endif
#define GML_DRCMUTEX_LOCK(name) GML_RECMUTEX_LOCK(name)
#endif

#if DEBUG_LUA
# define LUA_CALL_IN_CHECK(L) LuaCallInCheck ciCheck((L), __FUNCTION__);
# define LUA_CALL_IN_CHECK(L) SELECT_LUA_STATE(); LuaCallInCheck ciCheck((L), __FUNCTION__)
#else
# define LUA_CALL_IN_CHECK(L)
# define LUA_CALL_IN_CHECK(L) SELECT_LUA_STATE()
#endif

#ifdef USE_GML // hack to add some degree of thread safety to LUA
Expand All @@ -30,9 +58,9 @@ class LuaCallInCheck {
# if GML_ENABLE_SIM
# undef LUA_CALL_IN_CHECK
# if DEBUG_LUA
# define LUA_CALL_IN_CHECK(L) GML_RECMUTEX_LOCK(lua); GML_CALL_DEBUGGER(); LuaCallInCheck ciCheck((L), __FUNCTION__);
# define LUA_CALL_IN_CHECK(L) GML_DRCMUTEX_LOCK(lua); SELECT_LUA_STATE(); GML_CALL_DEBUGGER(); LuaCallInCheck ciCheck((L), __FUNCTION__);
# else
# define LUA_CALL_IN_CHECK(L) GML_RECMUTEX_LOCK(lua); GML_CALL_DEBUGGER();
# define LUA_CALL_IN_CHECK(L) GML_DRCMUTEX_LOCK(lua); SELECT_LUA_STATE(); GML_CALL_DEBUGGER();
# endif
# endif
#endif
Expand Down
22 changes: 11 additions & 11 deletions rts/Lua/LuaGaia.cpp
Expand Up @@ -47,7 +47,7 @@ void CLuaGaia::LoadHandler()

new CLuaGaia();

if (luaGaia->L == NULL) {
if (!luaGaia->IsValid()) {
delete luaGaia;
}
}
Expand All @@ -67,40 +67,40 @@ CLuaGaia::CLuaGaia()
{
luaGaia = this;

if (L == NULL) {
if (!IsValid()) {
return;
}

teamsLocked = true;

fullCtrl = true;
fullRead = true;
ctrlTeam = AllAccessTeam; //teamHandler->GaiaTeamID();
readTeam = AllAccessTeam;
readAllyTeam = AllAccessTeam;
selectTeam = teamHandler->GaiaTeamID();
SetFullCtrl(true, true);
SetFullRead(true, true);
SetCtrlTeam(AllAccessTeam, true); //teamHandler->GaiaTeamID();
SetReadTeam(AllAccessTeam, true);
SetReadAllyTeam(AllAccessTeam, true);
SetSelectTeam(teamHandler->GaiaTeamID(), true);

Init(LuaGaiaSyncedFilename, LuaGaiaUnsyncedFilename, SPRING_VFS_MAP);
}


CLuaGaia::~CLuaGaia()
{
if (L != NULL) {
if (L_Sim != NULL || L_Draw != NULL) {
Shutdown();
KillLua();
}
luaGaia = NULL;
}


bool CLuaGaia::AddSyncedCode()
bool CLuaGaia::AddSyncedCode(lua_State *L)
{
return true;
}


bool CLuaGaia::AddUnsyncedCode()
bool CLuaGaia::AddUnsyncedCode(lua_State *L)
{
/*lua_pushstring(L, "UNSYNCED");
lua_gettable(L, LUA_REGISTRYINDEX);*/
Expand Down
4 changes: 2 additions & 2 deletions rts/Lua/LuaGaia.h
Expand Up @@ -16,8 +16,8 @@ class CLuaGaia : public CLuaHandleSynced
static void FreeHandler();

protected:
bool AddSyncedCode();
bool AddUnsyncedCode();
bool AddSyncedCode(lua_State *L);
bool AddUnsyncedCode(lua_State *L);

private:
CLuaGaia();
Expand Down

0 comments on commit a628b04

Please sign in to comment.