Skip to content

Commit

Permalink
Colored server names for FiveM & UrbanTerror
Browse files Browse the repository at this point in the history
  • Loading branch information
tltneon committed Dec 27, 2024
1 parent dfa8173 commit 5586089
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 28 deletions.
13 changes: 9 additions & 4 deletions src/lgsl_class.php
Original file line number Diff line number Diff line change
Expand Up @@ -817,14 +817,19 @@ public function getHistoryArray() {
public function getExtrasArray() {
return $this->_extra ?? [];
}
public function getName($html = false) {
public function getName($htmlSafe = false) {
if ($this->isPending()) {
return LGSL::NONE;
}
if ($html) {
return htmlspecialchars($this->_server['name'], ENT_QUOTES);
$name = Helper::lgslHtmlColor($this->_server['name'], true);
if ($htmlSafe) {
$name = preg_replace('/([\x{0001F000}-\x{0001FAFF}])/mu', '', $name);
return htmlspecialchars($name, ENT_QUOTES);
}
return $this->_server['name'];
return $name;
}
public function getColoredName() {
return Helper::lgslHtmlColor($this->_server['name']);
}
public function setName($name) {
$this->_server['name'] = $name;
Expand Down
1 change: 1 addition & 0 deletions src/lgsl_config.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@
$lgsl_config['preloader'] = true; // true=using ajax to faster loading page
$lgsl_config['disabled_types']= false; // allow to exclude some protocols (games) from list. usage: $lgsl_config['disabled_types']= ['warsowold', 'halflifewon', 'test'];
$lgsl_config['select_lang'] = true; // allow to select language for users
$lgsl_config['remove_colors'] = false; // remove colors from the server name

//------------------------------------------------------------------------------------------------------------+
//[ TRANSLATION ]
Expand Down
2 changes: 1 addition & 1 deletion src/lgsl_details.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
// SHOW THE STANDARD INFO

$output .= "
<div id='servername_{$server->getStatus()}'> {$server->getName()} </div>
<div id='servername_{$server->getStatus()}'> {$server->getColoredName()} </div>
<div class='details_info'>
<div class='details_info_column'>
<a id='gamelink' href='{$server->getConnectionLink()}'>{$lang->get('slk')}</a>
Expand Down
4 changes: 2 additions & 2 deletions src/lgsl_list.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,11 @@
<td title='{$server->getName()}' class='servername_cell'>
<div class='servername_nolink'>
{$server->getName()}
{$server->getColoredName()}
</div>
<div class='servername_link'>
<a href='".LGSL::link($server->getIp(), $server->getConnectionPort())."'>
{$server->getName()}
{$server->getColoredName()}
</a>
</div>
</td>
Expand Down
77 changes: 57 additions & 20 deletions src/lgsl_protocol.php
Original file line number Diff line number Diff line change
Expand Up @@ -589,8 +589,8 @@ public function process() {
$type = $this->_server->getType();
$isMoh = strpos($type, "moh") !== FALSE; // mohaa_ mohaab_ mohaas_ mohpa_
$mohSymbol = $isMoh ? "\x02" : "";
if ($type === PROTOCOL::QUAKE2) { $this->_fp->write("\xFF\xFF\xFF\xFFstatus"); }
elseif ($type === PROTOCOL::CALLOFDUTYIW) { $this->_fp->write("\xFF\xFF\xFF\xFFgetinfo LGSL"); }
if ($type === PROTOCOL::QUAKE2) { $this->_fp->write("\xFF\xFF\xFF\xFFstatus"); }
elseif ($type === PROTOCOL::CALLOFDUTYIW) { $this->_fp->write("\xFF\xFF\xFF\xFFgetinfo LGSL"); }
else { $this->_fp->write("\xFF\xFF\xFF\xFF{$mohSymbol}getstatus"); }

$buffer = $this->_fp->read();
Expand All @@ -607,13 +607,15 @@ public function process() {

$s = 1;
if ($item[0]) $s = 0; // IW4 HAS NO EXTRA "\"
global $lgsl_config;
for ($i = $s; $i < count($item); $i += 2) { // SKIP EVEN KEYS
$data_key = strtolower(Helper::lgslParseColor($item[$i], "1"));
$this->_data['e'][$data_key] = Helper::lgslParseColor($item[$i+1], "1");
$data_key = strtolower(Helper::lgslParseColor($item[$i], "1"));
$this->_data['e'][$data_key] = Helper::lgslParseColor($item[$i+1], "1", false);
}
}

$this->_data['s']['name'] = $this->_data['e']['hostname'] ?? $this->_data['e']['sv_hostname'] ?? LGSL::NONE;
if (isset($this->_data['e']['sv_hostname'])) { $this->_data['e']['sv_hostname'] = strtolower(Helper::lgslHtmlColor($this->_data['e']['sv_hostname'], "1", true)); }
if (isset($this->_data['e']['protocol']) && $type === PROTOCOL::CALLOFDUTYIW) {
$games = ['1' => 'IW6', '2' => 'H1', '6' => 'IW3', '7' => 'T7', '20604' => 'IW5', '151' => 'IW4', '101' => 'T4'];
$this->_data['s']['game'] = $games[$this->_data['e']['protocol']] ?? "Unknown {$this->_data['e']['protocol']}";
Expand Down Expand Up @@ -2412,7 +2414,7 @@ class Query35 extends QueryJson { // FiveM / RedM
public function process() {
$buffer = $this->fetch("http://{$this->_server->getIp()}:{$this->_server->getQueryPort()}/dynamic.json");
if (!$buffer) return $this::NO_RESPOND;
$this->_data['s']['name'] = Helper::lgslParseColor($buffer['hostname'], 'fivem');
$this->_data['s']['name'] = Helper::lgslParseColor($buffer['hostname'], 'fivem', false);
$this->_data['s']['players'] = $buffer['clients'];
$this->_data['s']['playersmax'] = $buffer['sv_maxclients'];
$this->_data['s']['map'] = $buffer['mapname'];
Expand Down Expand Up @@ -3132,7 +3134,10 @@ private function _isHttp() {
public function open(&$server = null) {
global $lgsl_config;
if ($this->_isHttp()) {
if (!LGSL::isEnabled("curl")) return false;
if (!LGSL::isEnabled("curl")) {
LGSL::showWarning("Curl is not enabled");
return false;
}
$this->_stream = curl_init('');
curl_setopt($this->_stream, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($this->_stream, CURLOPT_SSL_VERIFYPEER, 0);
Expand Down Expand Up @@ -3213,31 +3218,63 @@ public function close() {
//------------------------------------------------------------------------------------------------------------+
//------------------------------------------------------------------------------------------------------------+
class Helper {
static function lgslParseColor($string, $type) {
static function lgslHtmlColor($string, $removeColors = false) {
global $lgsl_config;
if ($lgsl_config['remove_colors'] || $removeColors) {
return preg_replace('/##([0-9a-fA-F]{6})/', "", $string);
}
$outputText = preg_replace_callback('/##([0-9a-fA-F]{6})([0-9a-zA-Z !@$%&-*+|\/\.]+)?/', function($matches) {
$text = $matches[2] ?? '';
return "<span style='color: #{$matches[1]};'>{$text}</span>";
}, $string);
return $outputText;
}
static function lgslColorParser($string, $pattern, &$colors) {
return preg_replace_callback($pattern, function($matches) use (&$colors) {
$code = $matches[1];
$text = $matches[2] ?? '';
return "##{$colors[$code]}{$text}";
}, $string);
}
static function lgslParseColor($string, $type, $needRemove = true) {
global $lgsl_config;
$needRemove &= !$lgsl_config['remove_colors'];
switch ($type) {
case "2": return preg_replace("/\^[\x20-\x7E]/", "", $string);
case "doomskulltag": return preg_replace("/\\x1c./", "", $string);
case "farcry": return preg_replace("/\\$\d/", "", $string);
case "fivem": return preg_replace("/\^\d/", "", $string);
case "fivem":
if ($needRemove) {
return preg_replace("/\^\d/", "", $string);
}
$colors = ['0' => '888888', '1' => 'ff0000', '2' => 'b5dfb7', '3' => 'DDCC00', '4' => 'd3eafd', '5' => '00eeee', '6' => 'DD55DD', '7' => 'fffff0', '8' => 'ffcbbb', '9' => '808080'];
$pattern = '/\^(\d{1})([0-9a-zA-Z !@$%&-*+|\/\.]+)?/';
return Helper::lgslColorParser($string, $pattern, $colors);
case "painkiller": return preg_replace("/#./", "", $string);
case "swat4": return preg_replace("/\[c=......\]/Usi", "", $string);
case "minecraft": return preg_replace("/(§.)/S", "", $string);
case "factorio": return preg_replace("/\[[-a-z=0-9\#\/\.,\s?]*\]/S", "", $string);
case "bbcode": return preg_replace('/\[[^\]]+\]/', '', $string);

case "1":
$string = preg_replace("/\^x.../", "", $string);
$string = preg_replace("/\^./", "", $string);

$string_length = strlen($string);
for ($i=0; $i<$string_length; $i++) {
$char = ord($string[$i]);
if ($char > 160) { $char = $char - 128; }
if ($char > 126) { $char = 46; }
if ($char == 16) { $char = 91; }
if ($char == 17) { $char = 93; }
if ($char < 32) { $char = 46; }
$string[$i] = chr($char);
if ($needRemove) {
$string = preg_replace("/\^x.../", "", $string);
$string = preg_replace("/\^./", "", $string);

$string_length = strlen($string);
for ($i=0; $i<$string_length; $i++) {
$char = ord($string[$i]);
if ($char > 160) { $char = $char - 128; }
if ($char > 126) { $char = 46; }
if ($char == 16) { $char = 91; }
if ($char == 17) { $char = 93; }
if ($char < 32) { $char = 46; }
$string[$i] = chr($char);
}
} else {
$colors = ['0' => '888888', '1' => 'ff0000', '2' => '00ff00', '3' => 'DDCC00', '4' => '3377EE', '5' => '00eeee', '6' => 'DD55DD', '7' => 'ffffff', '9' => '808080'];
$pattern = '/\^(\d{1})([0-9a-zA-Z !@$%&-*+|\/\.]+)?/';
return Helper::lgslColorParser($string, $pattern, $colors);
}
break;

Expand Down
2 changes: 1 addition & 1 deletion userbar.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ function drawHistory(&$im, $x, $y, $w, $h, &$server) {
$game_id = @Image::makeImage($server->getGameIcon('src/'), 32, 32); // create game icon
imagecopy($im, $game_id, 16, 16, 0, 0, 32, 32); // place game icon

imagettftext($im, 10, 0, 62, 19, $color_nm, $font, /* name */ $server->getName(false));
imagettftext($im, 10, 0, 62, 19, $color_nm, $font, /* name */ $server->getName(true));
imagettftext($im, 8, 0, 62, 32, $color_ip, $font, /* ip&port */ str_replace('https://', '', $link));
imagettftext($im, 7, 0, 154, 47, $color_mp, $font, /* map */ "{$lgsl_config['text']['map']}:{$server->getMap()}");
imagettftext($im, 7, 0, 62, 48, $color_pl, $font, /* players */ "{$lgsl_config['text']['plr']}:{$server->getPlayersCount()}{$max}");
Expand Down

0 comments on commit 5586089

Please sign in to comment.