public static void UpdateAPI() { #if DEBUG apidata = JsonConvert.DeserializeObject <APIRoot>(System.IO.File.ReadAllText("api.json")); global.items = JsonConvert.DeserializeObject <Items.Root>(System.IO.File.ReadAllText("items.json")); #else for (int i = 0; i < hosturls.Length; i++) { try { using (System.Net.WebClient web = new System.Net.WebClient()) apidata = JsonConvert.DeserializeObject <APIRoot>(web.DownloadString($"{hosturls[i]}{endpoint}")); break; } catch (System.Exception ex) { if (i != hosturls.Length) { continue; } else { Main.ThrowError("Pro Swapper needs an internet connection to run, if you are already connected to the internet Pro Swapper severs may be blocked in your country, please use a VPN or try disabling your firewall, if you are already doing this please refer to this error: \n\n" + ex); } } } global.items = JsonConvert.DeserializeObject <Items.Root>(global.Decompress(apidata.items)); #endif }
public static void UpdateAPI() { string rawAPIFile = global.ProSwapperFolder + "api.ProSwapper"; string rawGlobalFile = global.ProSwapperFolder + "global.ProSwapper"; GlobalAPI.Root globalapi = null; #if DEBUG apidata = JsonConvert.DeserializeObject <APIRoot>(File.ReadAllText("api.json")); apidata.timestamp = global.GetEpochTime(); globalapi = JsonConvert.DeserializeObject <GlobalAPI.Root>(File.ReadAllText("global.json")); string json = JsonConvert.SerializeObject(apidata, Formatting.None, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore//Makes filesize smaller hehe }); byte[] compressedapi = MessagePackSerializer.ConvertFromJson(json, MessagePackSerializerOptions.Standard); File.WriteAllBytes($"{global.version}.json", ByteCompression.Compress(compressedapi)); #else try { download : double TimeNow = global.GetEpochTime(); if (global.CurrentConfig.LastOpenedAPI + 3600 < TimeNow) { //Get api coz it wasnt fetched more than an hour ago using (WebClient web = new WebClient()) { //Download api & global byte[] rawAPI = web.DownloadData($"{ProSwapperEndpoint}/{global.version}.json"); string rawGlobal = web.DownloadString($"{ProSwapperEndpoint}/global.json"); //Decompress api & set apidata = JsonConvert.DeserializeObject <APIRoot>(MessagePackSerializer.ConvertToJson(ByteCompression.Decompress(rawAPI))); globalapi = JsonConvert.DeserializeObject <GlobalAPI.Root>(rawGlobal); File.WriteAllBytes(rawAPIFile, rawAPI); File.WriteAllText(rawGlobalFile, rawGlobal); } global.CurrentConfig.LastOpenedAPI = TimeNow; } else { if (!File.Exists(rawAPIFile) || !File.Exists(rawGlobalFile)) { global.CurrentConfig.LastOpenedAPI = 0; goto download; } //Was fetched within the hour //Download api & global byte[] rawAPI = File.ReadAllBytes(rawAPIFile); string rawGlobal = File.ReadAllText(rawGlobalFile); //Decompress api & set apidata = JsonConvert.DeserializeObject <APIRoot>(MessagePackSerializer.ConvertToJson(ByteCompression.Decompress(rawAPI))); globalapi = JsonConvert.DeserializeObject <GlobalAPI.Root>(rawGlobal); } } catch (Exception ex) { Program.logger.LogError(ex.Message); Main.ThrowError($"Pro Swapper needs an Internet connection to run, if you are already connected to the Internet Pro Swapper's API may be blocked in your country, please use a VPN or try disabling your firewall, if you are already doing this please refer to this error: \n\n{ex.Message}", true); } #endif apidata.discordurl = globalapi.discordurl; apidata.version = globalapi.version; apidata.status[0] = globalapi.status[0]; }