forked from Reloaded-Project/Reloaded-II
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added: Overhauled Download Menu [Part 1]
- Loading branch information
Showing
24 changed files
with
571 additions
and
142 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
using System.Reactive.Linq; | ||
using Akavache; | ||
using Akavache.Sqlite3; | ||
|
||
namespace Reloaded.Mod.Launcher.Lib.Utility; | ||
|
||
/// <summary> | ||
/// Service that provides caching support for fetching images via URL. | ||
/// Used as a singleton. | ||
/// </summary> | ||
public class ImageCacheService | ||
{ | ||
private readonly SqlRawPersistentBlobCache _cache; | ||
|
||
/// <summary/> | ||
public ImageCacheService() | ||
{ | ||
var directory = Paths.ImageCachePath; | ||
Directory.CreateDirectory(directory); | ||
var filePath = Path.Combine(directory, "Cache.db"); | ||
_cache = new SqlRawPersistentBlobCache(filePath); | ||
} | ||
|
||
/// <summary> | ||
/// Shuts down the service. Call this on application exit. | ||
/// </summary> | ||
public void Shutdown() | ||
{ | ||
_cache.Flush().Wait(); | ||
_cache.Vacuum().Wait(); | ||
_cache.Connection.ExecuteScalar<int>("PRAGMA wal_checkpoint(TRUNCATE)", Array.Empty<object>()); | ||
_cache.Dispose(); | ||
_cache.Shutdown.Wait(); | ||
} | ||
|
||
/// <summary> | ||
/// Retrieves the image for a given URI. | ||
/// </summary> | ||
/// <param name="uri">The URI to get image for.</param> | ||
/// <param name="expiration">How long should the file persist in the cache.</param> | ||
public async ValueTask<byte[]> GetImage(Uri uri, TimeSpan expiration) | ||
{ | ||
// Check the cache. | ||
var key = uri.ToString(); | ||
var observable = _cache.Get(key); | ||
var bytes = await observable.Catch(Observable.Return<byte[]?>(null!)); | ||
|
||
if (bytes == null) | ||
{ | ||
// Get it, and put into cache. | ||
using var client = new WebClient(); | ||
var data = await client.DownloadDataTaskAsync(uri); | ||
_ = _cache.Insert(key, data, expiration); | ||
return data; | ||
} | ||
|
||
return bytes; | ||
} | ||
|
||
/// <summary> | ||
/// Gets the time a mod preview should expire from current time. | ||
/// </summary> | ||
public TimeSpan ModPreviewExpiration => TimeSpan.FromDays(14); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
source/Reloaded.Mod.Launcher/Controls/Properties/AutoAdjustColumnCount.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
namespace Reloaded.Mod.Launcher.Controls.Properties; | ||
|
||
/// <summary> | ||
/// An attached WPF property that auto adjusts column count of a uniform grid based on a min width. | ||
/// </summary> | ||
public class AutoAdjustColumnCount : WPF.MVVM.AttachedPropertyBase<AutoAdjustColumnCount, int> | ||
{ | ||
public override void OnValueChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e) | ||
{ | ||
if (e.NewValue == null) | ||
return; | ||
|
||
// If attached to tooltip, handle via parent. | ||
if (sender is UniformGrid uniformGrid) | ||
{ | ||
// Remove if already present, then re-add. | ||
uniformGrid.SizeChanged -= OnSizeChanged; | ||
uniformGrid.SizeChanged += OnSizeChanged; | ||
OnSizeChanged(sender, null!); | ||
} | ||
} | ||
|
||
private void OnSizeChanged(object sender, SizeChangedEventArgs e) | ||
{ | ||
var grid = (UniformGrid)sender; | ||
var minWidth = GetValue(grid); | ||
var numColumns = (int)grid.RenderSize.Width / minWidth; | ||
if (numColumns != grid.Columns) | ||
grid.Columns = numColumns; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
namespace Reloaded.Mod.Launcher; | ||
|
||
internal class ModuleInitialiser | ||
{ | ||
[ModuleInitializer] | ||
public static void Init() | ||
{ | ||
// Raise maximum number of WebRequest connections | ||
ServicePointManager.DefaultConnectionLimit = int.MaxValue; | ||
} | ||
} |
Oops, something went wrong.