Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/PixiEditor/PixiEditor
Browse files Browse the repository at this point in the history
  • Loading branch information
flabbet committed Dec 14, 2023
2 parents a617360 + 0fe3312 commit 8bf53cd
Show file tree
Hide file tree
Showing 20 changed files with 531 additions and 106 deletions.
58 changes: 38 additions & 20 deletions src/PixiEditor/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Windows.Media;
using System.Windows.Threading;
using PixiEditor.Extensions.Common.Localization;
using PixiEditor.Helpers;
using PixiEditor.Models.AppExtensions;
using PixiEditor.Helpers.UI;
using PixiEditor.Models.Controllers;
Expand Down Expand Up @@ -42,34 +43,37 @@ protected override void OnStartup(StartupEventArgs e)

if (ParseArgument("--crash (\"?)([A-z0-9:\\/\\ -_.]+)\\1", arguments, out Group[] groups))
{
CrashReport report = CrashReport.Parse(groups[2].Value);
MainWindow = new CrashReportDialog(report);
MainWindow.Show();
try
{
CrashReport report = CrashReport.Parse(groups[2].Value);
MainWindow = new CrashReportDialog(report);
MainWindow.Show();
}
catch (Exception exception)
{
try
{
CrashHelper.SendExceptionInfoToWebhook(exception, true);
}
finally
{
MessageBox.Show("Fatal error", $"Fatal error while trying to open crash report in App.OnStartup()\n{exception}");
}
}

return;
}

#if !STEAM
#if !STEAM
if (!HandleNewInstance())
{
return;
}
#endif

LoadingWindow.ShowInNewThread();

AddNativeAssets();

InitPlatform();

ExtensionLoader extensionLoader = new ExtensionLoader();
extensionLoader.LoadExtensions();

#endif

var extensionLoader = InitApp();

MainWindow = new MainWindow(extensionLoader);
MainWindow.ContentRendered += (_, _) =>
{
LoadingWindow.Instance.SafeClose();
MainWindow.Activate();
};
MainWindow.Show();
}

Expand All @@ -80,6 +84,20 @@ private void InitPlatform()
platform.PerformHandshake();
}

public ExtensionLoader InitApp()
{
LoadingWindow.ShowInNewThread();

AddNativeAssets();

InitPlatform();

ExtensionLoader extensionLoader = new ExtensionLoader();
extensionLoader.LoadExtensions();

return extensionLoader;
}

private IPlatform GetActivePlatform()
{
#if STEAM
Expand Down
5 changes: 4 additions & 1 deletion src/PixiEditor/Data/Localization/Languages/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -589,5 +589,8 @@
"COPY_COLOR": "Copy color",

"FAILED_DOWNLOADING_TITLE": "Downloading update failed",
"FAILED_DOWNLOADING": "Failed downloading the update, you might not have enough space on the disk"
"FAILED_DOWNLOADING": "Failed downloading the update, you might not have enough space on the disk",

"CRASH_NOT_ALL_DOCUMENTS_RECOVERED_TITLE": "Could not recover all",
"CRASH_NOT_ALL_DOCUMENTS_RECOVERED": "Could not fully recover all files.\nIf you send the crash report to the developers\nthey might be able to help you."
}
16 changes: 13 additions & 3 deletions src/PixiEditor/Helpers/CrashHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,14 +112,24 @@ public static void AddExceptionMessage(StringBuilder builder, Exception e)
}
}

public static async Task SendExceptionInfoToWebhook(Exception e, [CallerFilePath] string filePath = "<unknown>", [CallerMemberName] string memberName = "<unknown>")
public static void SendExceptionInfoToWebhook(Exception e, bool wait = false,
[CallerFilePath] string filePath = "<unknown>", [CallerMemberName] string memberName = "<unknown>")
{
var task = Task.Run(() => SendExceptionInfoToWebhookAsync(e, filePath, memberName));
if (wait)
{
task.Wait();
}
}

public static async Task SendExceptionInfoToWebhookAsync(Exception e, [CallerFilePath] string filePath = "<unknown>", [CallerMemberName] string memberName = "<unknown>")
{
if (DebugViewModel.IsDebugBuild)
return;
await SendReportTextToWebhook(CrashReport.Generate(e), $"{filePath}; Method {memberName}");
await SendReportTextToWebhookAsync(CrashReport.Generate(e), $"{filePath}; Method {memberName}");
}

public static async Task SendReportTextToWebhook(CrashReport report, string catchLocation = null)
public static async Task SendReportTextToWebhookAsync(CrashReport report, string catchLocation = null)
{
string reportText = report.ReportText;
if (catchLocation is not null)
Expand Down
4 changes: 2 additions & 2 deletions src/PixiEditor/Models/AppExtensions/ExtensionLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public void InitializeExtensions(ExtensionServices pixiEditorApi)
}
catch (Exception ex)
{
Task.Run(async () => await CrashHelper.SendExceptionInfoToWebhook(ex));
CrashHelper.SendExceptionInfoToWebhook(ex);
}
}

Expand Down Expand Up @@ -86,7 +86,7 @@ private void LoadExtension(string packageJsonPath)
catch (Exception ex)
{
//MessageBox.Show(new LocalizedString("ERROR_LOADING_PACKAGE", packageJsonPath), "ERROR");
Task.Run(async () => await CrashHelper.SendExceptionInfoToWebhook(ex));
CrashHelper.SendExceptionInfoToWebhook(ex);
}
}

Expand Down
3 changes: 3 additions & 0 deletions src/PixiEditor/Models/Commands/CommandController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ internal class CommandController
public CommandCollection Commands { get; }

public List<CommandGroup> CommandGroups { get; }

public CommandLog.CommandLog Log { get; }

public OneToManyDictionary<string, Command> FilterCommands { get; }

Expand All @@ -37,6 +39,7 @@ internal class CommandController
public CommandController()
{
Current ??= this;
Log = new CommandLog.CommandLog();

ShortcutsPath = Path.Join(
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
Expand Down
43 changes: 43 additions & 0 deletions src/PixiEditor/Models/Commands/CommandLog/CommandLog.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using System.Globalization;
using System.Text;
using PixiEditor.Models.Commands.Commands;

namespace PixiEditor.Models.Commands.CommandLog;

internal class CommandLog
{
private readonly List<CommandLogEntry> list = new(MaxEntries);

private const int MaxEntries = 8;

public void Log(Command command, bool? canExecute)
{
if (canExecute.HasValue && !list[0].CanExecute.HasValue)
{
list[0].CanExecute = canExecute;
return;
}

if (list.Count >= MaxEntries)
{
list.RemoveRange(MaxEntries - 1, list.Count - MaxEntries + 1);
}

list.Insert(0, new CommandLogEntry(command, canExecute, DateTime.Now));
}

public string GetSummary(DateTime relativeTime)
{
var builder = new StringBuilder();

foreach (var entry in list)
{
var relativeSpan = entry.DateTime - relativeTime;
string canExecute = entry.CanExecute.HasValue ? entry.CanExecute.ToString() : "not executed";

builder.AppendLine($"{entry.Command.InternalName} | CanExecute: {canExecute} | {relativeSpan.TotalSeconds.ToString("F3", CultureInfo.InvariantCulture)}s ago | {entry.DateTime.ToString("O", CultureInfo.InvariantCulture)}");
}

return builder.ToString();
}
}
19 changes: 19 additions & 0 deletions src/PixiEditor/Models/Commands/CommandLog/CommandLogEntry.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using PixiEditor.Models.Commands.Commands;

namespace PixiEditor.Models.Commands.CommandLog;

internal class CommandLogEntry
{
public Command Command { get; }

public bool? CanExecute { get; set; }

public DateTime DateTime { get; }

public CommandLogEntry(Command command, bool? commandMethod, DateTime dateTime)
{
Command = command;
CanExecute = commandMethod;
DateTime = dateTime;
}
}
19 changes: 17 additions & 2 deletions src/PixiEditor/Models/Commands/CommandMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,26 @@ public CommandMethods(Command command, Action<object> execute, CanExecuteEvaluat

public void Execute(object parameter)
{
if (CanExecute(parameter))
var log = CommandController.Current?.Log;
ToLog(log, null);

if (!CanExecute(parameter))
{
_execute(parameter);
ToLog(log, false);
return;
}
ToLog(log, true);

_execute(parameter);
}

public bool CanExecute(object parameter) => _canExecute.CallEvaluate(_command, parameter);

private void ToLog(CommandLog.CommandLog? log, bool? canExecute)
{
if (log != null && _command != null)
{
log.Log(_command, canExecute);
}
}
}
Loading

0 comments on commit 8bf53cd

Please sign in to comment.