v2 (WPF)
This commit is contained in:
85
LeagueAPI/ResourceService.cs
Normal file
85
LeagueAPI/ResourceService.cs
Normal file
@@ -0,0 +1,85 @@
|
||||
using System.Text.Json;
|
||||
using LeagueAPI.ARAM;
|
||||
|
||||
namespace LeagueAPI;
|
||||
|
||||
public static class ResourceService
|
||||
{
|
||||
private const string DDRAGON_CHAMPION_URL = "https://raw.communitydragon.org/latest/plugins/rcp-be-lol-game-data/global/default/v1/champion-icons/{0}.png";
|
||||
private const string CHAMPION_FILENAME_FORMAT = "{0}.png";
|
||||
private const string ARAM_BALANCE_FILENAME = "aram.json";
|
||||
private const string HTTP_CLIENT_CACHE_FILENAME = "httpcache.json";
|
||||
|
||||
private static readonly DirectoryInfo AssetDirectory = new("./assets");
|
||||
|
||||
static ResourceService()
|
||||
{
|
||||
if (!AssetDirectory.Exists)
|
||||
{
|
||||
AssetDirectory.Create();
|
||||
}
|
||||
}
|
||||
|
||||
public static async Task<string> GetChampionIconPathAsync(int championId = -1)
|
||||
{
|
||||
FileInfo assetFile = new(Path.Combine(AssetDirectory.FullName, CHAMPION_FILENAME_FORMAT.AsFormatWith(championId)));
|
||||
if (assetFile.Exists)
|
||||
{
|
||||
return assetFile.FullName;
|
||||
}
|
||||
|
||||
using HttpClient client = new();
|
||||
HttpResponseMessage response = await client.GetAsync(DDRAGON_CHAMPION_URL.AsFormatWith(championId));
|
||||
if (!response.IsSuccessStatusCode)
|
||||
{
|
||||
return string.Empty;
|
||||
}
|
||||
byte[] buffer = await response.Content.ReadAsByteArrayAsync();
|
||||
try
|
||||
{
|
||||
if (!assetFile.Exists)
|
||||
{
|
||||
await File.WriteAllBytesAsync(assetFile.FullName, buffer);
|
||||
}
|
||||
}
|
||||
catch { }
|
||||
|
||||
return assetFile.FullName;
|
||||
}
|
||||
|
||||
public static ARAMBalanceLookup? GetARAMBalanceLookup()
|
||||
{
|
||||
FileInfo aramFile = new(Path.Combine(AssetDirectory.FullName, ARAM_BALANCE_FILENAME));
|
||||
if (!aramFile.Exists)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
string json = File.ReadAllText(aramFile.FullName);
|
||||
return JsonSerializer.Deserialize<ARAMBalanceLookup>(json);
|
||||
}
|
||||
|
||||
public static void SetARAMBalanceLookup(ARAMBalanceLookup champions)
|
||||
{
|
||||
FileInfo aramFile = new(Path.Combine(AssetDirectory.FullName, ARAM_BALANCE_FILENAME));
|
||||
string json = JsonSerializer.Serialize(champions);
|
||||
File.WriteAllText(aramFile.FullName, json);
|
||||
}
|
||||
|
||||
public static HttpCache? GetHttpCache()
|
||||
{
|
||||
FileInfo cacheFile = new(Path.Combine(AssetDirectory.FullName, HTTP_CLIENT_CACHE_FILENAME));
|
||||
if (!cacheFile.Exists)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
string json = File.ReadAllText(cacheFile.FullName);
|
||||
return JsonSerializer.Deserialize<HttpCache>(json);
|
||||
}
|
||||
|
||||
public static void SetHttpCache(HttpCache cache)
|
||||
{
|
||||
FileInfo cacheFile = new(Path.Combine(AssetDirectory.FullName, HTTP_CLIENT_CACHE_FILENAME));
|
||||
string json = JsonSerializer.Serialize(cache);
|
||||
File.WriteAllText(cacheFile.FullName, json);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user