Skip to content

Commit

Permalink
Fixed Lua game init completely being interrupted when database is una…
Browse files Browse the repository at this point in the history
…vailable (refs #59)

* database module will throw debug messages when it cannot connect or read
* ACL module will enter 'dumb' mode and disallow any permissions (should be changed in future)
  • Loading branch information
timosmit committed Jan 1, 2019
1 parent aa67685 commit 98d4a30
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 7 deletions.
9 changes: 9 additions & 0 deletions luamods/wolfadmin/auth/auth.lua
Expand Up @@ -15,6 +15,8 @@
-- You should have received a copy of the GNU General Public License
-- along with this program. If not, see <http://www.gnu.org/licenses/>.

local db = require (wolfa_getLuaPath()..".db.db")

local events = require (wolfa_getLuaPath()..".util.events")
local settings = require (wolfa_getLuaPath()..".util.settings")

Expand Down Expand Up @@ -116,6 +118,13 @@ auth.PERM_IMMUNE = "immune"
-- this, but it will suffice.
function auth.onGameInit()
if settings.get("g_standalone") == 1 then
if not db.isConnected() then
-- FIXME simple workaround to deny any commands
function auth.isPlayerAllowed() return false end

return
end

srv = require (wolfa_getLuaPath()..".auth.acl")

srv.readPermissions()
Expand Down
4 changes: 3 additions & 1 deletion luamods/wolfadmin/commands/server/acl.lua
Expand Up @@ -17,6 +17,8 @@

local acl = require (wolfa_getLuaPath()..".auth.acl")

local db = require (wolfa_getLuaPath()..".db.db")

local commands = require (wolfa_getLuaPath()..".commands.commands")

local settings = require (wolfa_getLuaPath()..".util.settings")
Expand Down Expand Up @@ -195,4 +197,4 @@ function commandAcl(command, action, ...)

return true
end
commands.addserver("acl", commandAcl, (settings.get("g_standalone") == 0))
commands.addserver("acl", commandAcl, (settings.get("g_standalone") == 0 or not db.isConnected()))
8 changes: 6 additions & 2 deletions luamods/wolfadmin/db/db.lua
Expand Up @@ -36,12 +36,16 @@ function db.oninit()
elseif settings.get("db_type") == "mysql" then
con = require (wolfa_getLuaPath()..".db.mysql")
else
error("invalid database system (none|sqlite3|mysql)")
outputDebug("Invalid database system (none|sqlite3|mysql), defaulting to 'none'.")

return
end

setmetatable(db, {__index = con})

db.start()
if not db.start() then
outputDebug("Database could not be loaded, only limited functionality is available.", 3)
end
end
end
events.handle("onGameInit", db.oninit)
Expand Down
11 changes: 9 additions & 2 deletions luamods/wolfadmin/db/mysql.lua
Expand Up @@ -514,11 +514,18 @@ function mysql.start()
con = env:connect(settings.get("db_database"), settings.get("db_username"), settings.get("db_password"), settings.get("db_hostname"), settings.get("db_port"))

if not con then
error("could not connect to database")
outputDebug("Could not connect to database.", 3)

return false
elseif not mysql.isSchemaExistent() then
mysql.close()
error("schema does not exist")

outputDebug("Database schema does not exist.", 3)

return false
end

return true
end

function mysql.close(doSave)
Expand Down
11 changes: 9 additions & 2 deletions luamods/wolfadmin/db/sqlite3.lua
Expand Up @@ -522,15 +522,22 @@ function sqlite3.start()
con = env:connect(uri)

if not con then
error("could not connect to database")
outputDebug("Could not connect to database.", 3)

return false
elseif not sqlite3.isSchemaExistent() then
sqlite3.close()
error("schema does not exist")

outputDebug("Database schema does not exist.", 3)

return false
end

-- enable foreign key enforcement
assert(con:execute("PRAGMA foreign_keys=1"))
assert(con:execute("PRAGMA synchronous=0"))

return true
end

function sqlite3.close(doSave)
Expand Down

0 comments on commit 98d4a30

Please sign in to comment.