using System; using System.Collections.ObjectModel; using System.IO; using System.Net.Http; using System.Text.Json; using System.Threading.Tasks; using Saradomin.Model; namespace Saradomin.Infrastructure.Services { public class RemoteConfigService : IRemoteConfigService { private const string ServerProfilesURL = "https://emoscape.org/server_profiles.json"; public ObservableCollection AvailableProfiles { get; private set; } = new(); public async Task FetchServerProfileConfig(string outputPath) { using (var httpClient = new HttpClient { Timeout = TimeSpan.FromSeconds(5) }) { var configJson = await httpClient.GetStringAsync(ServerProfilesURL); if (File.Exists(outputPath)) { File.Delete(outputPath); } else { await File.WriteAllTextAsync(outputPath!, configJson); } } } public async Task LoadServerProfileConfig(string filePath) { using (var sr = new StreamReader(filePath)) { AvailableProfiles = JsonSerializer.Deserialize>( await sr.ReadToEndAsync() ); } } public void LoadFailsafeDefaults() { AvailableProfiles = new ObservableCollection { new() { Name = "Local server", ManagementServerAddress = "localhost", GameServerAddress = "localhost", GameServerPort = 43594, WorldListServerPort = 43595, CacheServerPort = 43595 }, new() { Name = "EmoScape Live Game", ManagementServerAddress = "play.emoscape.org", GameServerAddress = "play.emoscape.org", GameServerPort = 43594, WorldListServerPort = 43595, CacheServerPort = 43595 }, new() { Name = "Original 2009Scape Live Server", ManagementServerAddress = "play.2009scape.org", GameServerAddress = "play.2009scape.org", GameServerPort = 43594, WorldListServerPort = 43595, CacheServerPort = 43595 }, }; } } }