public static void ParseServers() { //Empty the list of our active servesr ActiveServers = new List <ServerInfo>(); //Grab the string from the RenX Website. string serverText = new WebClient().DownloadString(RenXWebLinks.RENX_ACTIVE_SERVERS_LIST_URL); //Turn it into a JSon object that we can parse. string[] results = serverText.Split(RenXWebLinks.RENX_SERVER_INFO_BREAK_SYMBOL, StringSplitOptions.RemoveEmptyEntries); //We start at 2 to skip the dev notes. //We end one early to skip the html tags. for (int i = 2; i < results.Length - 1; i++) { string[] info = results[i].Split(RenXWebLinks.RENX_SERVER_INFO_SPACER_SYMBOL); ServerInfo newServer = new ServerInfo(); // SERVER NAME newServer.ServerName = info[(int)SERVER_INFO_POSITIONS.Server_Name]; // SERVER IP & PORT string[] address = info[(int)SERVER_INFO_POSITIONS.Server_IP].Split(':'); newServer.IPAddress = address[0]; newServer.Port = ParseInt(address[1]); //BOT COUNT newServer.BotCount = ParseInt(info[(int)SERVER_INFO_POSITIONS.Bots]); //PASSWORD REQUIRED newServer.PasswordProtected = ParseBool(info[(int)SERVER_INFO_POSITIONS.Password_Required]); //MAP newServer.MapName = MapPreviewSettings.GetPrettyMapName(info[(int)SERVER_INFO_POSITIONS.Map_Name]); //SERVER SETTINGS { //Break down the settings string[] serverSettings = info[(int)SERVER_INFO_POSITIONS.Server_Settings].Split(RenXWebLinks.RENX_SERVER_SETTING_SPACE_SYMBOL); newServer.MaxPlayers = ParseInt(serverSettings[(int)SERVER_SETTINGS_POSTIONS.VehicleLimit]); newServer.VehicleLimit = ParseInt(serverSettings[(int)SERVER_SETTINGS_POSTIONS.MaxPlayers]); newServer.MineLimit = ParseInt(serverSettings[(int)SERVER_SETTINGS_POSTIONS.MineLimit]); newServer.SpawnCrates = ParseBool(serverSettings[(int)SERVER_SETTINGS_POSTIONS.SpawnCreate]); newServer.CrateRespawnRate = ParseInt(serverSettings[(int)SERVER_SETTINGS_POSTIONS.CrateRespawnTime]); newServer.AutoBalance = ParseBool(serverSettings[(int)SERVER_SETTINGS_POSTIONS.AutoBalance]); newServer.TimeLimit = ParseInt(serverSettings[(int)SERVER_SETTINGS_POSTIONS.TimeLimit]); newServer.AllowPM = ParseBool(serverSettings[(int)SERVER_SETTINGS_POSTIONS.AllowPm]); newServer.PmTeamOnly = ParseBool(serverSettings[(int)SERVER_SETTINGS_POSTIONS.PmTeamOnly]); newServer.SteamRequired = ParseBool(serverSettings[(int)SERVER_SETTINGS_POSTIONS.SteamRequired]); newServer.GameVersion = serverSettings[(int)SERVER_SETTINGS_POSTIONS.Version]; } // PLAYER COUNT newServer.PlayerCount = ParseInt(info[(int)SERVER_INFO_POSITIONS.Player_Count]); ActiveServers.Add(newServer); } }
/// <summary> /// This function will request all the server info on the RenXServers. This /// must be called to populate the server list. /// </summary> public static async Task ParseJsonServersAsync() { var newActiveServers = new List <ServerInfo>(); try { string jsonText = ""; //Grab the string from the RenX Website. RxLogger.Logger.Instance.Write($"Downloading RenxActiveServerJsonUrl {RenXWebLinks.RenxActiveServerJsonUrl}"); try { jsonText = await new WebClient().DownloadStringTaskAsync(RenXWebLinks.RenxActiveServerJsonUrl); } catch (Exception ex) { RxLogger.Logger.Instance.Write($"Server JSON Downloading Issue: {ex.Message}\r\nStack Trace:\r\n{ex.StackTrace}"); } RxLogger.Logger.Instance.Write($"Downloading RenxActiveServerJsonUrl {RenXWebLinks.RenxActiveServerJsonUrl} Complete"); if (jsonText == "") { return; } //Turn it into a JSon object that we can parse. var results = JsonConvert.DeserializeObject <dynamic>(jsonText); //For each object we have to try to get its components. #region -= Parse JSon Collection foreach (var data in results) { try { ServerInfo newServer = new ServerInfo(); //SET STRINGS newServer.ServerName = data["Name"] ?? "Missing"; newServer.MapName = data["Current Map"] ?? "Missing"; NewServer.MapName = MapPreviewSettings.GetPrettyMapName(NewServer.MapName); newServer.IpAddress = data["IP"] ?? "Missing"; //SET INTS newServer.BotCount = data["Bots"] ?? -1; newServer.PlayerCount = data["Players"] ?? -1; newServer.MineLimit = data.Variables["Mine Limit"] ?? -1; newServer.MaxPlayers = data.Variables["Player Limit"] ?? -1; newServer.VehicleLimit = data.Variables["Vehicle Limit"] ?? -1; newServer.CrateRespawnRate = data.Variables["CrateRespawnAfterPickup"] ?? -1; newServer.TimeLimit = data.Variables["Time Limit"] ?? -1; newServer.Port = data["Port"] ?? -1; //SET BOOLS newServer.SteamRequired = data.Variables["bSteamRequired"] ?? false; newServer.PasswordProtected = data.Variables["bPassworded"] ?? false; newServer.AutoBalance = data.Variables["bAutoBalanceTeams"] ?? false; newServer.SpawnCrates = data.Variables["bSpawnCrates"] ?? false; newServer.Ranked = data.Variables["bRanked"] ?? false; //SET COUNTRYINFO string[] countryInfo = await GrabCountry(newServer.IpAddress); newServer.CountryName = countryInfo[0]; newServer.CountryCode = countryInfo[1]; //All work done, add current serverinfo to the main list newActiveServers.Add(newServer); } catch (Exception ex) { RxLogger.Logger.Instance.Write($"Server JSON Loading Issue: {ex.Message}\r\nStack Trace:\r\n{ex.StackTrace}"); } } #endregion } catch { // If a global error occurred (e.g. connectivity/JSON parse error), clear the whole list. newActiveServers.Clear(); } ActiveServers = newActiveServers; }