-
Notifications
You must be signed in to change notification settings - Fork 1
/
App.xaml.cs
161 lines (136 loc) · 5.13 KB
/
App.xaml.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
using System.IO;
using System.Windows;
using Newtonsoft.Json;
using System;
using System.Threading;
namespace Discord_Custom_RPC
{
public partial class App : Application
{
readonly Logger logger = new();
private Mutex? mutex;
protected override void OnStartup(StartupEventArgs e)
{
const string mutexName = "Discord Custom RPC";
bool createdNew;
mutex = new(true, mutexName, out createdNew);
if (!createdNew)
{
MessageBox.Show("Application is already running.", "Warning", MessageBoxButton.OK, MessageBoxImage.Information);
Shutdown();
}
base.OnStartup(e);
string appDataFolder = AppDataFolder.Path();
string iconFilePath = Path.Combine(appDataFolder, "tray_icon.ico");
// Копировать tray_icon.ico в AppData
if (!File.Exists(iconFilePath))
{
string sourceIconPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "tray_icon.ico");
if (File.Exists(sourceIconPath))
{
try
{
Directory.CreateDirectory(appDataFolder);
File.Copy(sourceIconPath, iconFilePath);
File.Delete(sourceIconPath);
logger.Log("Successful copying of tray icon in AppData folder");
}
catch (Exception ex)
{
logger.Log($"Error when copying tray icon in AppData folder: {ex.Message}");
}
}
}
// Логгирование ошибок
AppDomain.CurrentDomain.UnhandledException += (sender, args) =>
{
Exception exception = (Exception)args.ExceptionObject;
logger.Log($"An error has occurred: {exception.Message}\n\nLocation: {exception.StackTrace}");
};
CreateJSON();
DiscordConnect();
}
protected override void OnExit(ExitEventArgs e)
{
mutex?.ReleaseMutex();
mutex?.Dispose();
base.OnExit(e);
}
private static void CreateJSON()
{
string appDataFolder = AppDataFolder.Path();
// Полный путь к файлу lastConfig.json
string jsonFilePath = Path.Combine(appDataFolder, "lastConfig.json");
if (!Directory.Exists(appDataFolder))
Directory.CreateDirectory(appDataFolder);
if (!File.Exists(jsonFilePath))
{
// Объект, который будет сериализован в JSON
LastConfigData configData = new()
{
ApplicationId = "",
Details = "",
State = "",
Timestamp = "",
LargeImageKey = "",
LargeImageText = "",
SmallImageKey = "",
SmallImageText = "",
ButtonFText = "",
ButtonFLink = "",
ButtonSText = "",
ButtonSLink = "",
Autostart = "0"
};
// Запись JSON в файл
string json = JsonConvert.SerializeObject(configData, Formatting.Indented);
File.WriteAllText(jsonFilePath, json);
}
}
private static void DiscordConnect()
{
DiscordConnect discordConnect = new();
discordConnect.InjectLogin();
}
}
public class AppDataFolder
{
// Путь к директории в AppData
public static string Path()
{
return System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Discord Custom RPC");
}
}
public class Logger
{
private readonly string logFilePath;
public Logger()
{
string logFolder = AppDataFolder.Path();
Directory.CreateDirectory(logFolder);
logFilePath = Path.Combine(logFolder, "logger.log");
}
public void Log(string message)
{
using StreamWriter writer = File.AppendText(logFilePath);
string logEntry = $"{DateTime.Now:yyyy-MM-dd HH:mm:ss:FFF} - {message}";
writer.WriteLine(logEntry);
}
}
}
class LastConfigData
{
public string? ApplicationId { get; set; }
public string? Details { get; set; }
public string? State { get; set; }
public string? Timestamp { get; set; }
public string? LargeImageKey { get; set; }
public string? LargeImageText { get; set; }
public string? SmallImageKey { get; set; }
public string? SmallImageText { get; set; }
public string? ButtonFText { get; set; }
public string? ButtonFLink { get; set; }
public string? ButtonSText { get; set; }
public string? ButtonSLink { get; set; }
public string? Autostart { get; set; }
}