Skip to content

Commit

Permalink
shader stuff + new exception handler
Browse files Browse the repository at this point in the history
  • Loading branch information
phasephasephase committed Jan 10, 2024
1 parent 24b098c commit 5fb5e03
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 45 deletions.
10 changes: 10 additions & 0 deletions JiayiLauncher/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@ protected override void OnStartup(StartupEventArgs e)
if (createdNew)
{
Arguments.Set(string.Join(" ", args));

DispatcherUnhandledException += (_, ex) =>
{
Log.Write(ex.Exception, ex.Exception.ToString(), Log.LogLevel.Error);
MessageBox.Show(
"Jiayi has ran into a problem and needs to close. Please send your log file to the nearest developer.",
"Crash", MessageBoxButton.OK, MessageBoxImage.Error
);
};

return;
}
var argString = string.Join(" ", args);
Expand Down
116 changes: 82 additions & 34 deletions JiayiLauncher/Features/Shaders/ShaderManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ public static class ShaderManager
public static string AppliedShader { get; set; } = string.Empty;
public static List<string> AvailableShaders => Shaders.Where(x => x != AppliedShader).ToList();

private static string[] _blockedFolders = ["iOS", "Android"];

public static async Task BackupVanillaShaders()
{
var info = await PackageData.GetPackage();
Expand All @@ -28,7 +30,7 @@ public static async Task BackupVanillaShaders()
// if the shaders folder doesn't exist, there's nothing to back up
if (!Directory.Exists(path)) return;

if (JiayiSettings.Instance!.ShadersPath == string.Empty)
if (JiayiSettings.Instance.ShadersPath == string.Empty)
{
JiayiSettings.Instance.ShadersPath =
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
Expand All @@ -52,7 +54,7 @@ public static async Task BackupVanillaShaders()
public static async Task DeleteBackupShaders()
{
var version = await PackageData.GetVersion();
var path = Path.Combine(JiayiSettings.Instance!.ShadersPath, "Vanilla", version);
var path = Path.Combine(JiayiSettings.Instance.ShadersPath, "Vanilla", version);
if (Directory.Exists(path)) Directory.Delete(path, true);

Log.Write(nameof(ShaderManager), $"Deleted backup shaders for version {version}");
Expand All @@ -68,7 +70,7 @@ public static async Task RestoreVanillaShaders()
if (!Directory.Exists(path)) return;

var version = await PackageData.GetVersion();
var backupPath = Path.Combine(JiayiSettings.Instance!.ShadersPath, "Vanilla", version);
var backupPath = Path.Combine(JiayiSettings.Instance.ShadersPath, "Vanilla", version);
if (!Directory.Exists(backupPath)) return;

foreach (var file in Directory.GetFiles(backupPath))
Expand All @@ -84,7 +86,7 @@ public static void UpdateShaders()
{
Shaders.Clear();

if (!Directory.Exists(JiayiSettings.Instance!.ShadersPath)
if (!Directory.Exists(JiayiSettings.Instance.ShadersPath)
|| !Directory.Exists(Path.Combine(JiayiSettings.Instance.ShadersPath, "Applied")))
{
var path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
Expand All @@ -96,59 +98,102 @@ public static void UpdateShaders()

JiayiSettings.Instance.Save();
}

var files = Directory.GetFiles(JiayiSettings.Instance.ShadersPath, "*.zip");
Shaders.AddRange(files.Select(Path.GetFileNameWithoutExtension)!);

var applied = Directory.GetFiles(Path.Combine(JiayiSettings.Instance.ShadersPath, "Applied"), "*.zip");
AppliedShader = Path.GetFileNameWithoutExtension(applied.FirstOrDefault() ?? string.Empty);
var folders = Directory.GetDirectories(JiayiSettings.Instance.ShadersPath).Where(x => !x.EndsWith("Applied") && !x.EndsWith("Vanilla"));
Shaders.AddRange(folders.Select(Path.GetFileName)!);

var applied = Directory.GetDirectories(Path.Combine(JiayiSettings.Instance.ShadersPath, "Applied"));
AppliedShader = Path.GetFileName(applied.FirstOrDefault() ?? string.Empty);

Log.Write(nameof(ShaderManager),
$"Updated shaders list. Found {Shaders.Count} shaders. {AppliedShader} is currently applied.");
Log.Write(nameof(ShaderManager), AppliedShader == string.Empty
? $"Updated shaders list. Found {Shaders.Count} shaders. {AppliedShader} is currently applied."
: $"Updated shaders list. Found {Shaders.Count} shaders. No shader is currently applied.");
}

public static async Task AddShader(IBrowserFile file)
{
var path = Path.Combine(JiayiSettings.Instance!.ShadersPath, file.Name);
if (!path.EndsWith(".zip")) return;
var tempPath = Path.Combine(JiayiSettings.Instance.ShadersPath, "Temp");
if (Directory.Exists(tempPath)) Directory.Delete(tempPath, true);
Directory.CreateDirectory(tempPath);

var path = Path.Combine(JiayiSettings.Instance.ShadersPath, file.Name);
if (!path.EndsWith(".zip") && !path.EndsWith(".mcpack")) return;

var stream = file.OpenReadStream(524288000L);
var zipPath = Path.Combine(tempPath, file.Name);
var fs = File.Create(zipPath);
await stream.CopyToAsync(fs);
stream.Close();
fs.Close();

await Task.Run(() => ZipFile.ExtractToDirectory(zipPath, tempPath));
File.Delete(zipPath);

await using var stream = File.Create(path);
await file.OpenReadStream(524288000L).CopyToAsync(stream);
var materialsFolder = FindMaterials(tempPath);
if (materialsFolder == null) return;

Directory.Move(materialsFolder, Path.Combine(JiayiSettings.Instance.ShadersPath, Path.GetFileNameWithoutExtension(file.Name)));
Directory.Delete(tempPath, true);

Log.Write(nameof(ShaderManager), $"Added shader {file.Name}");
UpdateShaders();
}

private static string? FindMaterials(string path)
{
var directories = Directory.GetDirectories(path, "*", SearchOption.AllDirectories).ToList();
for (var i = 0; i < directories.Count; i++)
{
var directory = directories[i];
// skip over any folders that are blocked
if (_blockedFolders.Any(blockedFolder => directory.Contains(blockedFolder)))
{
directories.Remove(directory);
i--;
continue;
}

var files = Directory.GetFiles(directory);
if (files.Any(x => x.EndsWith(".material.bin"))) continue; // folder has shaders

directories.Remove(directory);
i--;
}

// there should only be one folder left
return directories.ElementAtOrDefault(0); // returns null if there are no folders left
}

public static void EnableShader(string shader)
{
if (AppliedShader != string.Empty) DisableShader(AppliedShader);

var path = Path.Combine(JiayiSettings.Instance!.ShadersPath, shader + ".zip");
if (!File.Exists(path)) return;
var path = Path.Combine(JiayiSettings.Instance.ShadersPath, shader);
if (!Directory.Exists(path)) return;

File.Move(path, Path.Combine(JiayiSettings.Instance.ShadersPath, "Applied", shader + ".zip"));
Directory.Move(path, Path.Combine(JiayiSettings.Instance.ShadersPath, "Applied", shader));

Log.Write(nameof(ShaderManager), $"Enabled shader {shader}");
UpdateShaders();
}

public static void DisableShader(string shader)
{
var path = Path.Combine(JiayiSettings.Instance!.ShadersPath, "Applied", shader + ".zip");
if (!File.Exists(path)) return;
var path = Path.Combine(JiayiSettings.Instance.ShadersPath, "Applied", shader);
if (!Directory.Exists(path)) return;

File.Move(path, Path.Combine(JiayiSettings.Instance.ShadersPath, shader + ".zip"));
Directory.Move(path, Path.Combine(JiayiSettings.Instance.ShadersPath, shader));

Log.Write(nameof(ShaderManager), $"Disabled shader {shader}");
UpdateShaders();
}

public static void DeleteShader(string shader)
{
var path = Path.Combine(JiayiSettings.Instance!.ShadersPath, shader + ".zip");
if (!File.Exists(path)) return;
var path = Path.Combine(JiayiSettings.Instance.ShadersPath, shader);
if (!Directory.Exists(path)) return;

File.Delete(path);
Directory.Delete(path, true);

Log.Write(nameof(ShaderManager), $"Deleted shader {shader}");
UpdateShaders();
Expand All @@ -158,8 +203,8 @@ public static async Task ApplyShader()
{
if (AppliedShader == string.Empty) return;

var path = Path.Combine(JiayiSettings.Instance!.ShadersPath, "Applied", AppliedShader + ".zip");
if (!File.Exists(path)) return;
var path = Path.Combine(JiayiSettings.Instance.ShadersPath, "Applied", AppliedShader);
if (!Directory.Exists(path)) return;

var version = await PackageData.GetVersion();
var backupPath = Path.Combine(JiayiSettings.Instance.ShadersPath, "Vanilla", version);
Expand All @@ -174,17 +219,20 @@ public static async Task ApplyShader()
var shaderPath = Path.Combine(installPath, "data", "renderer", "materials");
if (!Directory.Exists(shaderPath)) return;

var shaderFiles = Directory.GetFiles(shaderPath);

using var zip = ZipFile.OpenRead(path);

foreach (var file in shaderFiles)
var vanillaShaders = Directory.GetFiles(shaderPath);
var shaders = Directory.GetFiles(path);

foreach (var shader in shaders)
{
var fileName = Path.GetFileName(file);
if (zip.Entries.Any(x => x.Name == fileName)) File.Delete(file);
var fileName = Path.GetFileName(shader);
if (vanillaShaders.Any(x => Path.GetFileName(x) == fileName))
{
File.Delete(Path.Combine(shaderPath, fileName));
}

File.Copy(shader, Path.Combine(shaderPath, fileName));
}

zip.ExtractToDirectory(shaderPath);
Log.Write(nameof(ShaderManager), $"Applied shader {AppliedShader}");
}
}
13 changes: 2 additions & 11 deletions JiayiLauncher/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.IO;
using System.Runtime.InteropServices;
using System.Security.Principal;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Interop;
Expand All @@ -19,6 +20,7 @@
using JiayiLauncher.Utils;
using Microsoft.AspNetCore.Components.WebView;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Web.WebView2.Core;
using Microsoft.Web.WebView2.Wpf;
using static JiayiLauncher.Utils.Imports;

Expand All @@ -33,15 +35,6 @@ public MainWindow()
InitializeComponent();
Log.CreateLog();

AppDomain.CurrentDomain.UnhandledException += (_, args) =>
{
Log.Write(args.ExceptionObject, ((Exception)args.ExceptionObject).ToString(), Log.LogLevel.Error);
MessageBox.Show(
"Jiayi has ran into a problem and needs to close. Please send your log file to the nearest developer.",
"Crash", MessageBoxButton.OK, MessageBoxImage.Error
);
};

var services = new ServiceCollection();
services.AddWpfBlazorWebView();
services.AddBlazoredModal();
Expand Down Expand Up @@ -143,8 +136,6 @@ private nint WndProc(nint hWnd, int msg, nint wParam, nint lParam, ref bool hand
return 0;
}

// ReSharper disable once UnusedMember.Local
// ReSharper disable once UnusedParameter.Local
private void ModifyWebView(object? _, BlazorWebViewInitializedEventArgs e)
{
_webView = e.WebView;
Expand Down

0 comments on commit 5fb5e03

Please sign in to comment.