Skip to content

Commit

Permalink
Refactor configuration for the gamemode
Browse files Browse the repository at this point in the history
  • Loading branch information
MegaThorx committed Jul 10, 2023
1 parent eda074f commit c2104c0
Show file tree
Hide file tree
Showing 10 changed files with 155 additions and 101 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ vrp_assets
/tools/ml_gps/*.sdf
/tools/ml_gps/*.suo
/vrp/server/config/config.json
/vrp/server/config/config.ini

# IntelliJ IDEA 15 files
.idea/
Expand Down
1 change: 0 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ ADD build/docker-entrypoint.sh /docker-entrypoint.sh
# Add MTA configs and modules
ADD build/config/* /var/lib/mtasa/mods/deathmatch/
ADD build/modules/* /var/lib/mtasa/x64/modules/
ADD vrp/server/config/config.json.dist /var/lib/mtasa/config.json.dist

# Add MTA resources
ADD artifacts.tar.gz /var/lib/mtasa/mods/deathmatch/resources/
Expand Down
14 changes: 8 additions & 6 deletions build/docker-entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ CONFIG_DIR=/var/lib/mtasa/mods/deathmatch/resources/vrp_build/server/config
# Exit immediately if something fails
set -e

# Create config if not exists
echo "Checking config file integrity"
if [ ! -f "$CONFIG_DIR/config.json" ]; then
echo "Copying default config"
cp /var/lib/mtasa/config.json.dist "$CONFIG_DIR/"
cp /var/lib/mtasa/config.json.dist "$CONFIG_DIR/config.json"
# If the config does not exist populate it from env variables
if [ ! -f "$CONFIG_DIR/config.ini" ]; then
echo "Populating config"
env | while IFS= read -r line; do
if [[ $line = VRP_* ]] then
echo "$line\n" | sed "s/VRP_//g" >> $CONFIG_DIR/config.ini
fi
done
fi

# Start worker server
Expand Down
43 changes: 43 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
services:
mtasa:
image: ghcr.io/exo-opensource/mta-gamemode:latest
restart: unless-stopped
ports:
- 22003:22003/udp
- 22005:22005
- 22126:22126/udp
- 127.0.0.1:5000:8080
environment:
- "API_SECRET=PLEASE_CHANGE_ME"
- "MTA_GAME_PORT=22003"
- "MTA_HTTP_PORT=22005"
- "MTA_SERVER_NAME=vRP Server"
- "START_MTA=please"
- "VRP_MYSQL_MAIN_HOST=mariadb"
- "VRP_MYSQL_MAIN_PORT=3306"
- "VRP_MYSQL_MAIN_USERNAME=root"
- "VRP_MYSQL_MAIN_PASSWORD=root"
- "VRP_MYSQL_MAIN_DATABASE=vrp"
- "VRP_MYSQL_LOGS_HOST=mariadb"
- "VRP_MYSQL_LOGS_PORT=3306"
- "VRP_MYSQL_LOGS_USERNAME=root"
- "VRP_MYSQL_LOGS_PASSWORD=root"
- "VRP_MYSQL_LOGS_DATABASE=vrp"
- "VRP_MYSQL_PREMIUM_HOST=mariadb"
- "VRP_MYSQL_PREMIUM_PORT=3306"
- "VRP_MYSQL_PREMIUM_USERNAME=root"
- "VRP_MYSQL_PREMIUM_PASSWORD=root"
- "VRP_MYSQL_PREMIUM_DATABASE=vrp"
- "VRP_BOARD_BASE_URL=https://forum.exo-reallife.de/index.php"
- "VRP_BOARD_SECRET="

mariadb:
image: mariadb:11
restart: unless-stopped
# ports:
# - "127.0.0.1:3306:3306" # For local access
volumes:
- ./data:/var/lib/mysql
environment:
- "MYSQL_DATABASE=vrp"
- "MYSQL_ROOT_PASSWORD=root"
2 changes: 1 addition & 1 deletion vrp/meta.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,14 @@

<!-- Server -->
<script src="server/utils.lua" type="server"/>
<script src="server/classes/Config.lua" type="server"/>
<script src="server/mapEditorScriptingExtension_s.lua" type="server"/>
<script src="server/constants/main.lua" type="server"/>
<script src="server/constants/factions.lua" type="server"/>
<script src="server/constants/companies.lua" type="server"/>
<script src="server/constants/beggar.lua" type="server"/>
<script src="server/constants/tollstations.lua" type="server"/>
<script src="server/classes/Debugging.lua" type="server"/>
<script src="server/classes/Config.lua" type="server"/>
<script src="server/classes/FileLogger.lua" type="server"/>
<script src="server/classes/Storage/SQL.lua" type="server"/>
<script src="server/classes/Storage/MySQL.lua" type="server"/>
Expand Down
76 changes: 37 additions & 39 deletions vrp/server/classes/Config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,53 +7,51 @@
-- ****************************************************************************
Config = inherit(Singleton)

function Config:constructor()
if fileExists('/server/config/config.json') then
local config = Config.check('/server/config/config.json.dist', '/server/config/config.json')
Config.data = config
else
error('No config to load. Please add a config at \'/server/config/config.json\'')
end
Config.File = "/server/config/config.ini"
Config.Options = {}
Config.Values = {}

--[[
type = "string" OR "number" are supported
]]
function Config.register(key, type, defaultValue)
Config.Options[key] = {type = type, defaultValue = defaultValue}
end

function Config.get(section)
if section then
return Config.data[section]
else
return Config.data
end
function Config:constructor()
if fileExists(Config.File) then
self:load()
end
end

function Config.load(path)
local file = fileOpen(path, true)
local data = fileRead(file, fileGetSize(file))
local toReturn = fromJSON(data)
fileClose(file)
function Config:load()
local file = File.Open(Config.File)
local lines = split(file:getContent(), "\n")
file:close()

return toReturn
for _, line in ipairs(lines) do
if line ~= "" then
local option = split(line, "=")
Config.Values[option[1]] = option[2]
end
end
end

function Config.check(distFile, configFile)
local checkSource = Config.load(distFile)
local checkTarget = Config.load(configFile)
-- Run check
Config.checkInternal(checkSource, checkTarget, "#normal")
Config.checkInternal(checkTarget, checkSource, "#dist")
function Config.get(key)
local option = Config.Options[key]

return checkTarget
end

function Config.checkInternal(dist, config, preString)
for i, v in pairs(dist) do
if not config[i] then
error(('Element \'%s\' is missing!'):format(("%s.%s"):format(preString, i)))
end
if type(v) ~= type(config[i]) then
outputDebugString(('Element-Typo of \'%s\' is incorrect! [Expected: %s, got: %s]'):format(("%s.%s"):format(preString, i), type(v), type(config[i])), 2)
end
if type(v) == "table" then
Config.checkInternal(v, config[i], ("%s.%s"):format(preString, i))
end
if not option then
outputServerLog(("WARNING: Using unregistered config key '%s'"):format(key))
end

if not Config.Values[key] then
return option.defaultValue
end

if option.type == "number" then
return tonumber(Config.Values[key]) or option.defaultValue
else -- assume string
return Config.Values[key]
end
return true;
end
60 changes: 40 additions & 20 deletions vrp/server/classes/Core.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,27 @@
Core = inherit(Object)
addEvent("Core.onClientInternalError", true)

Config.register("MYSQL_MAIN_HOST", "string", "127.0.0.1")
Config.register("MYSQL_MAIN_PORT", "number", "3306")
Config.register("MYSQL_MAIN_USERNAME", "string", "root")
Config.register("MYSQL_MAIN_PASSWORD", "string", "")
Config.register("MYSQL_MAIN_DATABASE", "string", "vrp")

Config.register("MYSQL_LOGS_HOST", "string", "127.0.0.1")
Config.register("MYSQL_LOGS_PORT", "number", "3306")
Config.register("MYSQL_LOGS_USERNAME", "string", "root")
Config.register("MYSQL_LOGS_PASSWORD", "string", "")
Config.register("MYSQL_LOGS_DATABASE", "string", "vrp")

Config.register("MYSQL_PREMIUM_HOST", "string", "127.0.0.1")
Config.register("MYSQL_PREMIUM_PORT", "number", "3306")
Config.register("MYSQL_PREMIUM_USERNAME", "string", "root")
Config.register("MYSQL_PREMIUM_PASSWORD", "string", "")
Config.register("MYSQL_PREMIUM_DATABASE", "string", "vrp")

Config.register("WEB_ACCOUNT_USERNAME", "string", "")
Config.register("WEB_ACCOUNT_PASSWORD", "string", "")

function Core:constructor()
outputServerLog("Initializing core...")
nextframe(function() --small hack to override the name meta-name
Expand Down Expand Up @@ -36,32 +57,31 @@ function Core:constructor()
end

-- Establish database connection
sql = MySQL:new(Config.get('mysql')['main']['host'], Config.get('mysql')['main']['port'], Config.get('mysql')['main']['username'], Config.get('mysql')['main']['password'], Config.get('mysql')['main']['database'], Config.get('mysql')['main']['socket'])
sql = MySQL:new(Config.get("MYSQL_MAIN_HOST"), Config.get("MYSQL_MAIN_PORT"), Config.get("MYSQL_MAIN_USERNAME"), Config.get("MYSQL_MAIN_PASSWORD"), Config.get("MYSQL_MAIN_DATABASE"), nil)
sql:setPrefix("vrp")
-- board = MySQL:new(Config.get('mysql')['board']['host'], Config.get('mysql')['board']['port'], Config.get('mysql')['board']['username'], Config.get('mysql')['board']['password'], Config.get('mysql')['board']['database'], Config.get('mysql')['board']['socket'])
sqlPremium = MySQL:new(Config.get('mysql')['premium']['host'], Config.get('mysql')['premium']['port'], Config.get('mysql')['premium']['username'], Config.get('mysql')['premium']['password'], Config.get('mysql')['premium']['database'], Config.get('mysql')['premium']['socket'])
sqlLogs = MySQL:new(Config.get('mysql')['logs']['host'], Config.get('mysql')['logs']['port'], Config.get('mysql')['logs']['username'], Config.get('mysql')['logs']['password'], Config.get('mysql')['logs']['database'], Config.get('mysql')['logs']['socket'])
sqlPremium = MySQL:new(Config.get("MYSQL_PREMIUM_HOST"), Config.get("MYSQL_PREMIUM_PORT"), Config.get("MYSQL_PREMIUM_USERNAME"), Config.get("MYSQL_PREMIUM_PASSWORD"), Config.get("MYSQL_PREMIUM_DATABASE"), nil)
sqlLogs = MySQL:new(Config.get("MYSQL_LOGS_HOST"), Config.get("MYSQL_LOGS_PORT"), Config.get("MYSQL_LOGS_USERNAME"), Config.get("MYSQL_LOGS_PASSWORD"), Config.get("MYSQL_LOGS_DATABASE"), nil)
sqlLogs:setPrefix("vrpLogs")

MigrationManager:new()

-- Create ACL user for web-access
--[[
self.m_ACLAccount = addAccount("", "")
local aclGroup = aclGetGroup("web")
if not aclGroup then aclGroup = aclCreateGroup("web") end
local acl = aclGet("web")
if not acl then acl = aclCreate("web") end
aclGroupAddACL(aclGroup, acl)
acl:setRight("general.http", true)
acl:setRight("function.callRemote", true)
acl:setRight("function.fetchRemote", true)
if Config.get("WEB_ACCOUNT_USERNAME") ~= "" and Config.get("WEB_ACCOUNT_PASSWORD") ~= "" then
self.m_ACLAccount = Account.add(Config.get("WEB_ACCOUNT_USERNAME"), Config.get("WEB_ACCOUNT_PASSWORD"))

local aclGroup = aclGetGroup("web")
if not aclGroup then aclGroup = aclCreateGroup("web") end

local acl = aclGet("web")
if not acl then acl = aclCreate("web") end

aclGroupAddACL(aclGroup, acl)
acl:setRight("general.http", true)
acl:setRight("function.callRemote", true)
acl:setRight("function.fetchRemote", true)

aclGroup:addObject(("user.%s"):format(Config.get("WEB_ACCOUNT_USERNAME")))
end

aclGroup:addObject("user.exo_web")
]]
ACLGroup.get("Admin"):addObject("resource.admin_exo")

if GIT_BRANCH == "release/production" then
Expand Down
7 changes: 5 additions & 2 deletions vrp/server/classes/Forum.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@
-- ****************************************************************************
Forum = inherit(Singleton)

Config.register("BOARD_BASE_URL", "string", "")
Config.register("BOARD_SECRET", "string", "")

function Forum:constructor()
self.m_BaseUrl = Config.get('board')['baseurl']
self.m_Secret = Config.get('board')['secret']
self.m_BaseUrl = Config.get("BOARD_BASE_URL")
self.m_Secret = Config.get("BOARD_SECRET")
end

function Forum:destructor()
Expand Down
20 changes: 20 additions & 0 deletions vrp/server/config/config.ini.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
MYSQL_MAIN_HOST=127.0.0.1
MYSQL_MAIN_PORT=3306
MYSQL_MAIN_USERNAME=root
MYSQL_MAIN_PASSWORD=root
MYSQL_MAIN_DATABASE=vrp

MYSQL_LOGS_HOST=127.0.0.1
MYSQL_LOGS_PORT=3306
MYSQL_LOGS_USERNAME=root
MYSQL_LOGS_PASSWORD=root
MYSQL_LOGS_DATABASE=vrp

MYSQL_PREMIUM_HOST=127.0.0.1
MYSQL_PREMIUM_PORT=3306
MYSQL_PREMIUM_USERNAME=root
MYSQL_PREMIUM_PASSWORD=root
MYSQL_PREMIUM_DATABASE=vrp

BOARD_BASE_URL=https://forum.exo-reallife.de/index.php
BOARD_SECRET=
32 changes: 0 additions & 32 deletions vrp/server/config/config.json.dist

This file was deleted.

0 comments on commit c2104c0

Please sign in to comment.