English Encoding of the dedicated server/game

7 replies
Goto Page
To the start Previous 1 Next To the start
Up
Marcell
Super User
Offline Off
Hi,

I am creating a quick RCon Terminal in PHP and kind of it works fine. But I met some issues with the colors because of the encoding. (Even though the PHP/HTML file has the right encoding set)

The output can be seen here which is not good:

IMG:https://media.discordapp.net/attachments/641771891421347890/793208660284538970/unknown.png


Wonder if this related to the encoding or the RCon PHP itself.

Any suggestions?
29.12.20 01:10:41 am
Up
mrc
User
Offline Off
use \169 instead the copyright char.

example for white: \169255255255
CS2D Global Community - http://cs2dg.tk
29.12.20 09:51:11 am
Up
MikuAuahDark
User
Offline Off
By the looks of it, you need to convert from Windows-1252 to UTF-8. However, if you want to display colors then that's another story.
file cs2d LuaJIT for Dedicated Server (12) JIT POWER! | Know your Lua errors! | Part of LÖVE development team since 11.3
29.12.20 02:45:38 pm
Up
Marcell
Super User
Offline Off
No of course, I meant the encoding, the color code I know it's different as I have to search for the output content and then change things accordingly.
31.12.20 05:42:23 pm
Up
Hajt
User
Offline Off
I wrote simple function to parse colors for You, enjoy.

Code:
1
2
3
4
5
6
7
8
9
10
11
12
function color($string)
{
     $string = iconv('ASCII', 'UTF-8//IGNORE', $string);
     $pattern = '/([0-9]{3})([0-9]{3})([0-9]{3})/i';
     $replacement = '<span style="color: rgb(${1}, ${2}, ${3});">';
     $html = preg_replace($pattern, $replacement, $string);
     if (!$html) {
          return $string;
     } else {
          return $html . '</span>';
     }
}
03.01.21 06:46:14 pm
Up
Marcell
Super User
Offline Off
Hajt, you are a god, thanks a lot. I promsie not to forget this. For some reason it does not work tho. It appears to be still something with the encoding, I believe.

The cron PHP

Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
function send_commands($addr, $rcon, $cmds)
{
     $pieces = explode(":", $addr);
     $ip = $pieces[0];
     $port = $pieces[1];

     $fp = fsockopen("udp://$ip", $port, $errno, $errstr);

     $string = chr(1).chr(0).chr(242).chr(strlen($rcon)).$rcon.pack("S", strlen($cmds)).$cmds;
     fwrite($fp, $string);
     stream_set_timeout($fp, 1);
     $buf = fread($fp, 1024);
     fclose($fp);

     if (bin2hex($buf) == "0100f201340000") {
          return "RCon: Wrong rcon password.";
     }

     if (bin2hex($buf) == "0100f201330000") {
          return "RCon: Too many remote control attempts with wrong password!";
     }

     $buf = preg_split("/\x{F0}\x{00}\x{03}/", $buf);

     unset($buf[0]);

    
     return implode("<br>", $buf); 
     

    }


The form with the action:

Code:
1
2
3
4
5
6
7
8
9
<?php  
    if(isset($_POST['command'])) {
      $cmds = $_POST['command'];
      //do action
      $sentcommand = send_commands($addr, $rcon, $cmds);
      $output = color($sentcommand);  
      echo "<span class='text-white'>localhost@</span>devtest:~$<br>" . $output;
     } 
?>


https://imgur.com/a/dCm4Ged
edited 2×, last 03.01.21 07:30:30 pm
04.01.21 01:17:05 am
Up
Hajt
User
Offline Off
Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<?php
function color($string)
{
     $string = iconv('ASCII', 'UTF-8//IGNORE', $string);
     $pattern = '/([0-9]{3})([0-9]{3})([0-9]{3})/i';
     $replacement = '<span style="color: rgb(${1}, ${2}, ${3});">';
     $html = preg_replace($pattern, $replacement, $string);
     if (!$html) {
          return $string;
     } else {
          return $html . '</span>';
     }
}

function readByteFast()
{
     global $d;
     global $dOS;
     $byte = ord(substr($d, $dOS, 1));
     $dOS++;
     return $byte;
}

function send_commands($addr, $rcon, $cmds)
{
     global $d;
     global $dOS;

     $output = "";
     $pieces = explode(":", $addr);
     $ip = $pieces[0];
     $port = $pieces[1];

     // Send multiple copies of the request packet, because cs2d likes to just ignore them randomly
     for ($i = 0; $i < 3; $i++) {
          $fp = fsockopen("udp://$ip", $port, $errno, $errstr);
          $string = chr(1) . chr(0) . chr(242) . chr(strlen($rcon)) . $rcon . pack("S", strlen($cmds)) . $cmds;
          fwrite($fp, $string);
          stream_set_timeout($fp, 1);
          $d = fread($fp, 4096);
          $dOS = 0;
          fclose($fp);

          if (!empty($d)) {
               break;
          }
     }

     readByteFast(); // 1
     readByteFast(); // 0
     readByteFast(); // 240
     readByteFast(); // 0
     readByteFast(); // 3
     $size = readByteFast();

     while ($size >= 1) {
          readByteFast();
          $arr[] = readByteFast();
          if ($size == 1) {
               $string = implode(array_map("chr", $arr));
               $output .= color($string) . "<br>";
               unset($arr);
               readByteFast();
               readByteFast();
               readByteFast();
               readByteFast();
               $size = readByteFast();
               continue;
          }
          $size--;
     }
     return $output;
}
?>
<!DOCTYPE html>
<html>
<body>

<?php
if (isset($_POST['cmds']) && !empty($_POST['cmds'])) {
     $addr = $_POST['addr'];
     $rcon = $_POST['rcon'];
     $cmds = $_POST['cmds'];

     print(send_commands($addr, $rcon, $cmds));
}
?>

<form method="POST">
     <label for="addr">addr:</label><br>
     <input type="text" id="addr" name="addr"><br>
     <label for="rcon">rcon:</label><br>
     <input type="text" id="rcon" name="rcon"><br>
     <label for="rcon">cmds:</label><br>
     <input type="text" id="cmds" name="cmds"><br><br>
     <input type="submit" value="Submit">
</form>

</body>
</html>
04.01.21 11:51:59 pm
Up
Marcell
Super User
Offline Off
Had to adjust a bit, but thank you it is now working perfectly.
To the start Previous 1 Next To the start