86 lines
2.8 KiB
C#
86 lines
2.8 KiB
C#
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);
|
|
}
|
|
}
|