Skip to content

Commit

Permalink
What
Browse files Browse the repository at this point in the history
  • Loading branch information
phasephasephase committed Oct 16, 2024
1 parent 4f08566 commit 7d540b8
Show file tree
Hide file tree
Showing 8 changed files with 120 additions and 6 deletions.
3 changes: 1 addition & 2 deletions JiayiLauncher/Features/Shaders/ShaderManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,8 @@ public async Task BackupVanillaShaders()
_log.Write(nameof(ShaderManager), $"Backed up vanilla shaders for version {await _packageData.GetVersion()}");
}

public async Task DeleteBackupShaders()
public async Task DeleteBackupShaders(string version)
{
var version = await _packageData.GetVersion();
var path = Path.Combine(JiayiSettings.Instance.ShadersPath, "Vanilla", version);
if (Directory.Exists(path)) Directory.Delete(path, true);

Expand Down
2 changes: 1 addition & 1 deletion JiayiLauncher/Features/Versions/VersionManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ await Task.Run(() =>
Directory.Delete(folder, true);
});

await _shaderManager.DeleteBackupShaders();
await _shaderManager.DeleteBackupShaders(ver);
}

// my favorite part of this class
Expand Down
4 changes: 4 additions & 0 deletions JiayiLauncher/JiayiLauncher.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,8 @@
</Compile>
</ItemGroup>

<ItemGroup>
<Folder Include="Shared\Components\Settings\" />
</ItemGroup>

</Project>
1 change: 1 addition & 0 deletions JiayiLauncher/MainPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ public MainPage()
Singletons.Add<Launcher>();
Singletons.Add<ModImporter>();
Singletons.Add<RichPresence>();
Singletons.Add<JiayiTasks>();
Singletons.Add(this);

WebView.BlazorWebViewInitialized += (_, e) =>
Expand Down
15 changes: 15 additions & 0 deletions JiayiLauncher/Pages/TestPlace.razor
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@
<JiayiButton OnClick="EscalateModal">Ask for permissions</JiayiButton>
<JiayiButton OnClick="EscalateImmediately">Escalate immediately</JiayiButton>

<h4>Tasks</h4>
<JiayiButton OnClick="CreateTask">Create 5 second task</JiayiButton>

<h4>Themes Page</h4>
<a href="/Themes">Themes</a>

Expand Down Expand Up @@ -185,4 +188,16 @@
_privileges.Escalate();
}

private void CreateTask()
{
var tasks = Singletons.Get<JiayiTasks>();
var task = tasks.AddTask(new Task(async () =>
{
await Task.Delay(5000);
Console.WriteLine("Task completed.");
}), "Dummy task", "TestPlace");

task.Start();
}

}
4 changes: 2 additions & 2 deletions JiayiLauncher/Platforms/Windows/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public App()
{
"TimeoutException",
"Object name: 'System.Net.Sockets.NetworkStream'.",
"at WinRT.ExceptionHelpers.<ThrowExceptionForHR>g__Throw|39_0(Int32 hr)",
"at WinRT.ExceptionHelpers.<ThrowExceptionForHR>g__Throw",
"at Microsoft.AspNetCore.Components.WebView.WebView2.WebView2WebViewManager.SendMessage(String message)",
"System.Net.Sockets.SocketException (995): The I/O operation has been aborted because of either a thread exit or an application request."
};
Expand Down Expand Up @@ -64,7 +64,7 @@ Would you like to open the log folder? Your log file is saved as 'Current.log'.
Process.Start("explorer.exe", Log.LogPath);
}

Microsoft.Maui.Controls.Application.Current!.Quit();
Environment.Exit(1);
};

return;
Expand Down
2 changes: 1 addition & 1 deletion JiayiLauncher/Utils/InternetManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace JiayiLauncher.Utils;

public class InternetManager
{
private const string IP = "https://1.1.1.1";
private const string IP = "https://phased.tech";

public bool OfflineMode { get; private set; }
public HttpClient Client { get; } = new();
Expand Down
95 changes: 95 additions & 0 deletions JiayiLauncher/Utils/JiayiTasks.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
using Blazored.Toast;
using Blazored.Toast.Services;
using JiayiLauncher.Shared.Components.Toasts;
using Microsoft.AspNetCore.Components;

namespace JiayiLauncher.Utils;

public class JiayiTask
{
public Task Task { get; set; }
public string Name { get; set; }
public string FromPage { get; set; }

private readonly CancellationTokenSource _cts = new();
private readonly TaskFactory _factory;

public JiayiTask(Task task, string name, string fromPage)
{
Task = task;
FromPage = fromPage;
_factory = new TaskFactory(_cts.Token);
}

public void Start()
{
_factory.StartNew(() => Task.Start());
}

public void Cancel()
{
_cts.Cancel();
}
}

public class JiayiTasks
{
private readonly List<JiayiTask> _tasks = new();

private readonly Log _log = Singletons.Get<Log>();

public JiayiTasks()
{
var mainPage = (MainPage?)Application.Current!.MainPage;
if (mainPage == null)
{
_log.Write(nameof(BlazorBridge), "MainPage was null", Log.LogLevel.Error);
return;
}

var dispatched = mainPage.BlazorWebView.TryDispatchAsync(sp =>
{
var navigation = sp.GetRequiredService<NavigationManager>();
navigation.LocationChanged += (_, e) =>
{
foreach (var task in _tasks)
{
if (e.Location.Contains(task.FromPage))
{
var bridge = Singletons.Get<BlazorBridge>();

var toastParams = new ToastParameters()
.Add(nameof(JiayiToast.Level), ToastLevel.Info)
.Add(nameof(JiayiToast.Title), task.Name)
.Add(nameof(JiayiToast.Content), new RenderFragment(builder =>
{
builder.OpenElement(0, "p");
builder.AddContent(1, "This task is running in the background.");
builder.CloseElement();
}));

bridge.ShowToast(toastParams, settings =>
{
settings.Timeout = 3;
settings.ShowCloseButton = false;
});
}
}
};
}).Result;

if (!dispatched)
_log.Write(nameof(JiayiTasks), "Some background tasks may not work properly.", Log.LogLevel.Warning);
}

public JiayiTask AddTask(Task task, string name, string fromPage)
{
var jiayiTask = new JiayiTask(task, name, fromPage);
_tasks.Add(jiayiTask);

// remove task from list when it's done
task.ContinueWith(_ => _tasks.Remove(jiayiTask));

return jiayiTask;
}
}

0 comments on commit 7d540b8

Please sign in to comment.