View Javadoc

1   package net.sourceforge.queried.gametypes;
2   
3   import java.net.InetAddress;
4   import java.net.UnknownHostException;
5   import java.util.ArrayList;
6   import java.util.Collections;
7   import java.io.UnsupportedEncodingException;
8   import net.sourceforge.queried.KillsComparator;
9   import net.sourceforge.queried.PlayerInfo;
10  import net.sourceforge.queried.ServerInfo;
11  import net.sourceforge.queried.Util;
12  
13  
14  public class HLServerInfo {
15  
16      public static ArrayList getPlayers(int localPort, String ipStr, int port, int infoType, int queryType,
17          int gameType) {
18          /** On some operating systems the following code will return the wrong number of bytes 
19           * in Unicode to UTF-8 conversion
20           * 
21           * a) byte.getBytes("UTF-8")
22           * 
23           * b) byte.getBytes() if LANG=en_US.UTF-8 is set in the OS enviroment
24           * 
25           * The two possible workarounds are:
26           * 
27           * 'unset LANG' in the OS shell before running your Java code
28           * 
29           * or 
30           * 
31           * use byte.getBytes("ISO-8859-1") or any other encoding than UTF-8
32           * 
33           */
34          byte[] buf = null;
35          try
36          {
37              buf = Util.getInfo(localPort, ipStr, port, "UA2S_PLAYER", infoType, queryType, gameType).getBytes("ISO-8859-1");
38          }
39          catch(UnsupportedEncodingException ex)
40          {
41              ex.printStackTrace();
42          }
43          if(buf.length == 0) {
44              return null;            
45          } 
46          else if(buf[0] != buf[1] || buf[1] != buf[2] || buf[2] != buf[3] || buf[4] != 'D') {
47              return null;            
48          }
49  
50          ArrayList sorted = new ArrayList();
51          int playerCount = buf[5] & 255;
52          int off = 6;
53          for(int i=0; i<playerCount; ++i) {
54              PlayerInfo playerInfo = new PlayerInfo();
55              StringBuffer playerName = new StringBuffer(20);
56              while(buf[off] != 0) {
57                  playerName.append((char)(buf[off++] & 255));
58              }
59              off++;
60              playerInfo.setName(playerName.toString().trim());
61              playerInfo.setKills((buf[off] & 255) | ((buf[off+1] & 255) << 8) | 
62                  ((buf[off+2] & 255) << 16) | ((buf[off+3] & 255) << 24));
63              sorted.add(playerInfo);
64              off += 8;
65          }
66          Collections.sort(sorted, new KillsComparator());
67          
68          return sorted;
69      }
70  
71      public static ServerInfo getDetails(int localPort, String ipStr, int port, int infoType, int queryType,
72          int gameType) {
73          /** On some operating systems the following code will return the wrong number of bytes 
74           * in Unicode to UTF-8 conversion
75           * 
76           * a) byte.getBytes("UTF-8")
77           * 
78           * b) byte.getBytes() if LANG=en_US.UTF-8 is set in the OS enviroment
79           * 
80           * The two possible workarounds are:
81           * 
82           * 'unset LANG' in the OS shell before running your Java code
83           * 
84           * or 
85           * 
86           * use byte.getBytes("ISO-8859-1") or any other encoding than UTF-8
87           * 
88           */
89          byte[] buf = null;
90          try
91          {
92              buf = Util.getInfo(localPort, ipStr, port, "TSource Engine Query", infoType, queryType, gameType).getBytes("ISO-8859-1");
93          }
94          catch(UnsupportedEncodingException ex)
95          {
96              ex.printStackTrace();
97          }
98          
99  
100         if(buf.length == 0) {
101             return null;            
102         } else if(buf[0] != buf[1] || buf[1] != buf[2] || buf[2] != buf[3] || buf[4] != 'm') { 
103             return null;            
104         } 
105         
106         ServerInfo serverInfo = new ServerInfo();
107         
108         int off = 5;
109 
110         InetAddress inettst;
111         try {
112             inettst = InetAddress.getByName(ipStr);
113             serverInfo.setIp(inettst.getHostAddress());
114         } catch (UnknownHostException e) {
115             serverInfo.setIp(ipStr);
116         }
117 
118         serverInfo.setPort(port +"");
119 
120         while(buf[off] != 0) {
121             off++;
122         }
123 
124         off++;
125 
126         StringBuffer netName = new StringBuffer(20);
127         while(buf[off] != 0) {
128             netName.append((char)(buf[off++] & 255));
129         }
130         serverInfo.setName(netName.toString());
131         
132         off++;
133         
134         StringBuffer mapName = new StringBuffer(20);
135         while(buf[off] != 0) {
136             mapName.append((char)(buf[off++] & 255));
137         }
138         serverInfo.setMap(mapName.toString());
139         
140         off++;
141 
142         // skip game directory
143         while(buf[off] != 0) {
144             off++;
145         }
146         
147         off++;
148 
149         StringBuffer gameDesc = new StringBuffer(20);
150         while(buf[off] != 0) {
151             gameDesc.append((char)(buf[off++] & 255));
152         }
153         serverInfo.setGame(gameDesc.toString());
154 
155         off++;
156         
157         int playerCount = buf[off] & 255;
158         serverInfo.setPlayerCount(playerCount +"");
159         
160         off++;
161         
162         int maxPlayerCount = buf[off] & 255;
163         serverInfo.setMaxPlayers(maxPlayerCount +"");
164         serverInfo.setFullResponse(new String(buf));
165 
166         return serverInfo;
167     }
168     
169 }