From 79dcc39826e7b1d20d60b787d34cab873f497503 Mon Sep 17 00:00:00 2001 From: WeeXnes Date: Thu, 8 Jun 2023 16:45:48 +0200 Subject: [PATCH] added import/export to discord rpc + fixes --- WeeXnes/App.xaml.cs | 10 +- WeeXnes/Core/CustomConsole.cs | 130 ++++++++++++++++++ WeeXnes/Core/Global.cs | 2 +- WeeXnes/Views/DiscordRPC/DiscordRPCView.xaml | 10 +- .../Views/DiscordRPC/DiscordRPCView.xaml.cs | 59 +++++++- WeeXnes/Views/KeyManager/KeyItem.cs | 22 ++- WeeXnes/Views/KeyManager/KeyManagerView.xaml | 1 + WeeXnes/WeeXnes.csproj | 3 +- 8 files changed, 220 insertions(+), 17 deletions(-) create mode 100644 WeeXnes/Core/CustomConsole.cs diff --git a/WeeXnes/App.xaml.cs b/WeeXnes/App.xaml.cs index 6e5ce37..cf4f9e9 100644 --- a/WeeXnes/App.xaml.cs +++ b/WeeXnes/App.xaml.cs @@ -44,7 +44,7 @@ namespace WeeXnes } catch (Exception ex) { - Console.WriteLine(ex); + WeeXnes.Core.CustomConsole.Error(ex.ToString()); } } } @@ -101,11 +101,11 @@ namespace WeeXnes { Game newGame = Game.Methods.GameFromIni(new INIFile(file.FullName)); DiscordRPCView.Data.Games.Add(newGame); - Console.WriteLine(file.Name + " loaded"); + WeeXnes.Core.CustomConsole.WriteLine(file.Name + " loaded"); } catch (Exception ex) { - Console.WriteLine(file.Name + ": " + ex.Message); + WeeXnes.Core.CustomConsole.Error(file.Name + ": " + ex.Message); MessageBox.Show(file.Name + ": " + ex.Message); } } @@ -127,12 +127,12 @@ namespace WeeXnes ); newItem.Filename = file.Name; KeyManagerView.Data.KeyItemsList.Add(newItem); - Console.WriteLine(file.Name + " loaded"); + WeeXnes.Core.CustomConsole.WriteLine(file.Name + " loaded"); } catch (Exception ex) { - Console.WriteLine(file.Name + ": " + ex.Message); + WeeXnes.Core.CustomConsole.Error(file.Name + ": " + ex.Message); } } } diff --git a/WeeXnes/Core/CustomConsole.cs b/WeeXnes/Core/CustomConsole.cs new file mode 100644 index 0000000..d7568e5 --- /dev/null +++ b/WeeXnes/Core/CustomConsole.cs @@ -0,0 +1,130 @@ +using System; +using System.Collections.Generic; +using System.Runtime.CompilerServices; +using System.Text; +using VanillaConsole = System.Console; + +namespace WeeXnes.Core +{ + public class CustomConsole + { + public static class Data + { + public static class Colors + { + public static bool colored_output = true; + public static ConsoleColor int_color = ConsoleColor.Blue; + public static ConsoleColor double_color = ConsoleColor.Cyan; + public static ConsoleColor float_color = ConsoleColor.DarkCyan; + } + + public static class Formatting + { + public static string success_char = "✓"; + public static string warning_char = "⌬"; + public static string info_char = "◈"; + public static string error_char = "☓"; + public static string writeline_char = "•"; + } + } + + private static void ConfiguredWriteline( + string text, + ConsoleColor color, + ConsoleColor foregroundColor = ConsoleColor.White) + { + VanillaConsole.OutputEncoding = Encoding.UTF8; + ConsoleColor prevColor = VanillaConsole.BackgroundColor; + ConsoleColor prevForeColor = VanillaConsole.ForegroundColor; + if (Data.Colors.colored_output) + { + VanillaConsole.BackgroundColor = color; + VanillaConsole.ForegroundColor = foregroundColor; + } + + VanillaConsole.WriteLine(text + " "); + if (Data.Colors.colored_output) + { + + VanillaConsole.BackgroundColor = prevColor; + VanillaConsole.ForegroundColor = prevForeColor; + } + } + + public static void WriteLine(string text, + [CallerLineNumber] int lineNumber = 0, + [CallerMemberName] string caller = null) + { + ConfiguredWriteline(" " + Data.Formatting.writeline_char + " (" + lineNumber + "|" + caller + ") " + text,VanillaConsole.BackgroundColor, ConsoleColor.White); + } + public static void WriteLine(float text, + [CallerLineNumber] int lineNumber = 0, + [CallerMemberName] string caller = null) + { + ConfiguredWriteline(" " + Data.Formatting.writeline_char + " (" + lineNumber + "|" + caller + ") " + text,Data.Colors.float_color, ConsoleColor.White); + } + public static void WriteLine(double text, + [CallerLineNumber] int lineNumber = 0, + [CallerMemberName] string caller = null) + { + ConfiguredWriteline(" " + Data.Formatting.writeline_char + " (" + lineNumber + "|" + caller + ") " + text,Data.Colors.double_color, ConsoleColor.White); + } + public static void WriteLine(int text, + [CallerLineNumber] int lineNumber = 0, + [CallerMemberName] string caller = null) + { + ConfiguredWriteline(" " + Data.Formatting.writeline_char + " (" + lineNumber + "|" + caller + ") " + text,Data.Colors.int_color, ConsoleColor.White); + } + public static void Success(string text, + [CallerLineNumber] int lineNumber = 0, + [CallerMemberName] string caller = null) + { + ConfiguredWriteline(" " + Data.Formatting.success_char + " (" + lineNumber + "|" + caller + ") " + text, ConsoleColor.Green, + ConsoleColor.Black); + } + + public static void Info(string text, + [CallerLineNumber] int lineNumber = 0, + [CallerMemberName] string caller = null) + { + ConfiguredWriteline(" " + Data.Formatting.info_char + " (" + lineNumber + "|" + caller + ") " + text, ConsoleColor.Blue, + ConsoleColor.Black); + } + + public static void Error(string text, + [CallerLineNumber] int lineNumber = 0, + [CallerMemberName] string caller = null) + { + ConfiguredWriteline(" " + Data.Formatting.error_char + " (" + lineNumber + "|" + caller + ") " + text, ConsoleColor.DarkRed, + ConsoleColor.Black); + } + + public static void Warning(string text, + [CallerLineNumber] int lineNumber = 0, + [CallerMemberName] string caller = null) + { + ConfiguredWriteline(" " + Data.Formatting.warning_char + " (" + lineNumber + "|" + caller + ") " + text, ConsoleColor.DarkYellow, + ConsoleColor.Black); + } + + public static void WriteLine(List List, bool verbose = true) + { + ConfiguredWriteline("List contains " + typeof(T) + "(" + List.Count + ")", ConsoleColor.DarkMagenta, + ConsoleColor.Black); + if (!verbose) + return; + + for (int i = 0; i < List.Count; i++) + { + if (i % 2 == 0) + { + ConfiguredWriteline("(" + i + "): " + List[i], ConsoleColor.DarkGray); + } + else + { + ConfiguredWriteline("(" + i + "): " + List[i], ConsoleColor.Black); + } + } + } + } +} \ No newline at end of file diff --git a/WeeXnes/Core/Global.cs b/WeeXnes/Core/Global.cs index e54d8c9..0bc0467 100644 --- a/WeeXnes/Core/Global.cs +++ b/WeeXnes/Core/Global.cs @@ -8,7 +8,7 @@ namespace WeeXnes.Core { public class Information { - public const string Version = "4.3.0"; + public const string Version = "4.3.1"; public const string EncryptionHash = "8zf5#RdyQ]$4x4_"; public const string ApiUrl = "https://api.github.com/repos/weexnes/weexnessuite/releases/latest"; } diff --git a/WeeXnes/Views/DiscordRPC/DiscordRPCView.xaml b/WeeXnes/Views/DiscordRPC/DiscordRPCView.xaml index 69d5760..c460917 100644 --- a/WeeXnes/Views/DiscordRPC/DiscordRPCView.xaml +++ b/WeeXnes/Views/DiscordRPC/DiscordRPCView.xaml @@ -24,11 +24,17 @@ + Click="ContextMenu_Edit"/> + + + Click="ContextMenu_Remove"/> diff --git a/WeeXnes/Views/DiscordRPC/DiscordRPCView.xaml.cs b/WeeXnes/Views/DiscordRPC/DiscordRPCView.xaml.cs index a654403..d3c4454 100644 --- a/WeeXnes/Views/DiscordRPC/DiscordRPCView.xaml.cs +++ b/WeeXnes/Views/DiscordRPC/DiscordRPCView.xaml.cs @@ -4,7 +4,10 @@ using System.IO; using System.Net; using System.Windows; using System.Windows.Controls; +using Microsoft.Win32; +using Nocksoft.IO.ConfigFiles; using WeeXnes.Core; +using CConsole = WeeXnes.Core.CustomConsole; namespace WeeXnes.Views.DiscordRPC { @@ -42,14 +45,66 @@ namespace WeeXnes.Views.DiscordRPC Data.SelectedItem = (Game)listBox.SelectedItem; } - private void ContextEdit_OnClick(object sender, RoutedEventArgs e) + private void ContextMenu_Edit(object sender, RoutedEventArgs e) { if(Data.SelectedItem == null) return; NavigationService.Navigate(new Uri("/Views/DiscordRPC/EditRPCView.xaml",UriKind.Relative)); } + private void ContextMenu_Export(object sender, RoutedEventArgs e) + { + Game selectedCache = Data.SelectedItem; + if(selectedCache == null) + return; + string filepath = Global.AppDataPathRPC.Value + "\\" + selectedCache.UUID + ".rpc"; + + SaveFileDialog dialog = new SaveFileDialog() + { + FileName = selectedCache.UUID, + Filter = "RPC File (*.rpc)|*.rpc", + Title = "Export RPC File" + }; + if (dialog.ShowDialog() == true) + { + File.Copy(filepath, dialog.FileName, true); + CustomConsole.WriteLine("Exported to: " + dialog.FileName); + } + + } + private void ContextMenu_Import(object sender, RoutedEventArgs e) + { + Game selectedCache = Data.SelectedItem; + if(selectedCache == null) + return; + + + OpenFileDialog dialog = new OpenFileDialog() + { + Filter = "RPC File (*.rpc)|*.rpc", + Title = "Import RPC File" + }; + if (dialog.ShowDialog() == true) + { + + Game newGame = Game.Methods.GameFromIni(new INIFile(dialog.FileName)); + + if (!File.Exists(Global.AppDataPathRPC.Value + "\\" + newGame.UUID + ".rpc")) + { + File.Copy(dialog.FileName, Global.AppDataPathRPC.Value + "\\" + newGame.UUID + ".rpc", true); + DiscordRPCView.Data.Games.Add(newGame); + CustomConsole.WriteLine("Imported: " + dialog.FileName); + } + else + { + CustomConsole.Error("not imported: " + dialog.FileName); + } + + + } + + } - private void ContextRemove_OnClick(object sender, RoutedEventArgs e) + private void ContextMenu_Remove(object sender, RoutedEventArgs e) { Game selectedCache = Data.SelectedItem; if(selectedCache == null) diff --git a/WeeXnes/Views/KeyManager/KeyItem.cs b/WeeXnes/Views/KeyManager/KeyItem.cs index d699424..d4cfb0d 100644 --- a/WeeXnes/Views/KeyManager/KeyItem.cs +++ b/WeeXnes/Views/KeyManager/KeyItem.cs @@ -6,6 +6,7 @@ using System.Net; using Wpf.Ui.Controls; using Microsoft.Win32; using Path = System.Windows.Shapes.Path; +using Console = WeeXnes.Core.CustomConsole; namespace WeeXnes.Views.KeyManager { @@ -34,6 +35,7 @@ namespace WeeXnes.Views.KeyManager SaveFileDialog dialog = new SaveFileDialog() { + FileName = this.Filename, Filter = "WXFiles (*.wx)|*.wx", Title = "Export KeyFile" }; @@ -48,10 +50,12 @@ namespace WeeXnes.Views.KeyManager { OpenFileDialog dialog = new OpenFileDialog() { - Filter = "WXFiles (*.wx)|*.wx" + Filter = "WXFiles (*.wx)|*.wx", + Title = "Import KeyFile" }; if (dialog.ShowDialog() == true) { + string filename = System.IO.Path.GetFileName(dialog.FileName); WXFile wxFile = new WXFile(dialog.FileName); KeyItem newItem = new KeyItem( wxFile.GetName(), @@ -60,11 +64,17 @@ namespace WeeXnes.Views.KeyManager wxFile.GetValue() ) ); - newItem.Filename = System.IO.Path.GetFileName(dialog.FileName); - WXFile newWxFile = new WXFile(Global.AppDataPathKEY.Value + "\\" + newItem.Filename); - WXFile.Methods.WriteFile(newItem, newWxFile); - KeyManagerView.Data.KeyItemsList.Add(newItem); - Console.WriteLine("Imported: " + dialog.FileName); + newItem.Filename = filename; + if (!File.Exists(Global.AppDataPathKEY.Value + "\\" + filename)) + { + File.Copy(dialog.FileName, Global.AppDataPathKEY.Value + "\\" + filename, true); + KeyManagerView.Data.KeyItemsList.Add(newItem); + Console.WriteLine("Imported: " + dialog.FileName); + } + else + { + Console.Error("Not Imported, already exists: " + dialog.FileName); + } } } } diff --git a/WeeXnes/Views/KeyManager/KeyManagerView.xaml b/WeeXnes/Views/KeyManager/KeyManagerView.xaml index a0f8916..3f8ea68 100644 --- a/WeeXnes/Views/KeyManager/KeyManagerView.xaml +++ b/WeeXnes/Views/KeyManager/KeyManagerView.xaml @@ -22,6 +22,7 @@ + diff --git a/WeeXnes/WeeXnes.csproj b/WeeXnes/WeeXnes.csproj index 6f6b99c..021d188 100644 --- a/WeeXnes/WeeXnes.csproj +++ b/WeeXnes/WeeXnes.csproj @@ -4,7 +4,7 @@ Debug AnyCPU - 4.3.0 + 4.3.1 {4B33CEE7-C74D-43B9-B99A-8B273D5195BC} WinExe WeeXnes @@ -68,6 +68,7 @@ MSBuild:Compile Designer +