Skip to content

Commit

Permalink
Never download resources
Browse files Browse the repository at this point in the history
  • Loading branch information
ArchLeaders committed May 13, 2023
1 parent e2a4757 commit 20aafc0
Showing 1 changed file with 13 additions and 27 deletions.
40 changes: 13 additions & 27 deletions src/ExKingEditor.Core/Extensions/ResourceExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,8 @@ namespace ExKingEditor.Core.Extensions;

public static class ResourceExtension
{
private const string _url = "https://raw.githubusercontent.com/EXKing-Editor/EXKing-Editor/master/src/ExKingEditor/Resources/";
private static readonly string _path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "EX-King-Editor", "Resources");

public static bool HasConnection =>
#if RELEASE
new Ping().Send("127.0.0.1", 1000).Status == IPStatus.Success;
#else
false;
#endif

static ResourceExtension()
{
Directory.CreateDirectory(_path);
Expand All @@ -24,29 +16,23 @@ static ResourceExtension()
public static Stream? Fetch<T>(string name) => FetchEmbed(typeof(T).Assembly, name);
public static Stream? FetchEmbed(this Assembly assembly, string name)
{
if (HasConnection && !ExConfig.Shared.LoadResourcesFromDisk) {
using HttpClient client = new();
return client.GetStreamAsync(_url + name).Result;
}
else {
Stream? stream = assembly.GetManifestResourceStream($"{assembly.GetName().Name}.Resources.{name}");
if (ExConfig.Shared.LoadResourcesFromDisk) {
string path = Path.Combine(_path, name);
if (!File.Exists(path)) {
if (stream != null) {
using FileStream fs = File.Create(path);
stream.CopyTo(fs);
stream.Seek(0, SeekOrigin.Begin);
}

return stream;
Stream? stream = assembly.GetManifestResourceStream($"{assembly.GetName().Name}.Resources.{name}");
if (ExConfig.Shared.LoadResourcesFromDisk) {
string path = Path.Combine(_path, name);
if (!File.Exists(path)) {
if (stream != null) {
using FileStream fs = File.Create(path);
stream.CopyTo(fs);
stream.Seek(0, SeekOrigin.Begin);
}

return File.OpenRead(path);
}
else {
return stream;
}

return File.OpenRead(path);
}
else {
return stream;
}
}

Expand Down

0 comments on commit 20aafc0

Please sign in to comment.