Skip to content

Commit

Permalink
Enhancement (ASP): Create config file if it does not exist
Browse files Browse the repository at this point in the history
Boolean values are now supported.
  • Loading branch information
leojonathanoh committed Jan 28, 2024
1 parent 6269050 commit 0823af1
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 9 deletions.
16 changes: 9 additions & 7 deletions config/ASP/docker-entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,10 @@ echo "Setting permissions on backup volume"
setup /src/ASP/system/database/backups 1

echo "Setting up config file"
CONFIG_FILE_SAMPLE=/config.sample/config.php
CONFIG_FILE=/src/ASP/system/config/config.php
if [ ! -f "$CONFIG_FILE" ]; then
echo "Creating new config file: $CONFIG_FILE"
cp -v "$CONFIG_FILE_SAMPLE" "$CONFIG_FILE"
fi
php /src/ASP/index.php > /dev/null
ls -al $CONFIG_FILE

echo "Setting permissions on config volume"
setup /src/ASP/system/config 1

Expand All @@ -49,7 +47,9 @@ write_config() {
VAL=$( printenv "$ENVVAR" || true )
if [ -n "$VAL" ]; then
echo "Writing key '$KEY' of type '$TYPE' to config file"
if [ "$TYPE" == 'string' ]; then
if [ "$TYPE" == 'boolean' ]; then
sed -i "s|^.$KEY =.*|\$$KEY = $VAL;|" "$CONFIG_FILE" # E.g. $bfhq_hide_bots = false;
elif [ "$TYPE" == 'string' ]; then
sed -i "s|^.$KEY =.*|\$$KEY = '$VAL';|" "$CONFIG_FILE" # E.g. $db_host = '127.0.0.1';
elif [ "$TYPE" == 'int' ]; then
sed -i "s|^.$KEY =.*|\$$KEY = $VAL;|" "$CONFIG_FILE" # E.g. $db_port = 3306;
Expand All @@ -75,7 +75,9 @@ echo "$CONFILE_FILE_CONTENT" | grep -E '^\$' | while read -r KEY EQUALS VALUE; d
KEY=$( echo "$KEY" | sed 's/^\$//' ) # Strip the dollar sign. E.g. '$db_host' -> 'db_host'
VALUE=$( echo "$VALUE" | rev | sed 's/^;//' | rev ) # Strip the trailing semicolon
TYPE=
if echo "$VALUE" | grep -E "^'" > /dev/null; then
if echo "$VALUE" | grep -E "^false|true$" > /dev/null; then
TYPE=boolean
elif echo "$VALUE" | grep -E "^'" > /dev/null; then
TYPE=string
elif echo "$VALUE" | grep -E "^array\(" > /dev/null; then
TYPE=array
Expand Down
75 changes: 73 additions & 2 deletions src/ASP/system/core/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,71 @@ public static function Init()
{
if(empty(self::$configFile))
{
// Load config defaults
if ( !self::LoadDefault() ) {
throw new Exception('Failed to load default configuration!');
}

// Load the config File
self::$configFile = SYSTEM_PATH . DS . 'config'. DS . 'config.php';
if( !self::Load() )
{
throw new Exception('Failed to load config file!');
// Create the config file
self::Save();

// throw new Exception('Failed to load config file!');
}
}
}

/*
| ---------------------------------------------------------------
| Method: getDefault()
| ---------------------------------------------------------------
|
| Returns the default configuration.
|
| @Return: array of the default configuration.
|
*/
private static function getDefault()
{
$cfg = array(
'db_host' => '127.0.0.1',
'db_port' => 3306,
'db_name' => 'bf2stats',
'db_user' => 'admin',
'db_pass' => 'admin',
'admin_user' => 'admin',
'admin_pass' => 'admin',
'admin_hosts' => array('127.0.0.1','192.168.2.0/24','localhost','192.168.1.102','192.168.1.110','0.0.0.0'),
'admin_backup_path' => 'C:/wamp/www/ASP/system/database/backups/',
'admin_backup_ext' => '.bak',
'admin_ignore_ai' => 0,
'stats_ignore_ai' => 0,
'stats_min_game_time' => 0,
'stats_min_player_game_time' => 0,
'stats_players_min' => 1,
'stats_players_max' => 256,
'stats_rank_check' => 0,
'stats_rank_tenure' => 7,
'stats_process_smoc' => 1,
'stats_process_gen' => 1,
'stats_awds_complete' => 0,
'stats_lan_override' => '174.49.21.221',
'stats_local_pids' => array('LocalPlayer01','210.84.29.151','LocalPlayer02','210.84.29.151'),
'debug_lvl' => 2,
'game_hosts' => array('127.0.0.1','192.168.2.0/24','192.168.1.102','192.168.1.110','localhost','::1'),
'game_custom_mapid' => 700,
'game_unlocks' => 0,
'game_unlocks_bonus' => 2,
'game_unlocks_bonus_min' => 1,
'game_awds_ignore_time' => 0,
'game_default_pid' => 29000000,
);
return $cfg;
}

/*
| ---------------------------------------------------------------
| Method: get()
Expand Down Expand Up @@ -131,6 +187,16 @@ public static function Save()
if (is_numeric($val))
{
$cfg .= "\$$key = " . $val . ";\n";
}

// If the value is boolean, then put in a "boolean" value
elseif (is_bool($val) || $val === 'true' || $val === 'false')
{
if (!$val || $val === 'false') {
$cfg .= "\$$key = false;\n";
}else {
$cfg .= "\$$key = true;\n";
}
}

// Check for array values (admin_hosts, game_hosts, and stats_local_pids)
Expand Down Expand Up @@ -203,7 +269,12 @@ protected static function Load()
return false;
}
}

protected static function LoadDefault() {
self::$data = self::getDefault();
return true;
}
}

Config::Init();
?>
?>

0 comments on commit 0823af1

Please sign in to comment.