-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathApp.xaml.cs
More file actions
184 lines (162 loc) · 6.76 KB
/
App.xaml.cs
File metadata and controls
184 lines (162 loc) · 6.76 KB
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
using System.Globalization;
using System.IO;
using System.Text.Json;
using System.Windows;
using XColumn.Models;
// 曖昧さ回避
using MessageBox = System.Windows.MessageBox;
namespace XColumn
{
/// <summary>
/// アプリケーションのエントリポイント。
/// </summary>
public partial class App : System.Windows.Application
{
private string _userDataFolder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "XColumn");
/// <summary>
/// スタートアップ処理。
/// </summary>
/// <param name="e"></param>
protected override void OnStartup(StartupEventArgs e)
{
// 言語設定の適用(UI表示前に行う)
ApplyLanguageSettings();
// 起動前に、保留されていたプロファイル複製処理を実行
ProcessPendingProfileClone();
base.OnStartup(e);
// プロファイル指定用変数
string? targetProfile = null;
// DevToolsおよびGPU設定用変数
bool enableDevTools = false;
bool disableGpu = false;
// コマンドライン引数の解析
for (int i = 0; i < e.Args.Length; i++)
{
if (e.Args[i] == "--profile" && i + 1 < e.Args.Length)
{
targetProfile = e.Args[i + 1];
break;
}
// DevTools有効化オプション
else if (e.Args[i] == "--enable-devtools")
{
enableDevTools = true;
}
// GPU無効化オプション
else if (e.Args[i] == "--disable-gpu")
{
disableGpu = true;
}
}
// 引数でプロファイルが指定されておらず、設定でデフォルトプロファイルが指定されている場合
if (string.IsNullOrEmpty(targetProfile))
{
string appConfigPath = Path.Combine(_userDataFolder, "app_config.json");
if (File.Exists(appConfigPath))
{
try
{
string json = File.ReadAllText(appConfigPath);
var config = JsonSerializer.Deserialize<AppConfig>(json);
if (config != null && !string.IsNullOrEmpty(config.StartupProfile))
{
targetProfile = config.StartupProfile;
}
}
catch { }
}
}
// メインウィンドウを起動
var mainWindow = new MainWindow(targetProfile, enableDevTools, disableGpu);
mainWindow.Show();
}
/// <summary>
/// app_config.json を読み込み、設定された言語をスレッドに適用します。
/// </summary>
private void ApplyLanguageSettings()
{
string userDataFolder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "XColumn");
string appConfigPath = Path.Combine(userDataFolder, "app_config.json");
string language = "ja-JP"; // デフォルト
if (File.Exists(appConfigPath))
{
try
{
string json = File.ReadAllText(appConfigPath);
var config = JsonSerializer.Deserialize<AppConfig>(json);
if (config != null && !string.IsNullOrEmpty(config.Language))
{
language = config.Language;
}
}
catch { }
}
try
{
var culture = new CultureInfo(language);
Thread.CurrentThread.CurrentCulture = culture;
Thread.CurrentThread.CurrentUICulture = culture;
}
catch { }
}
/// <summary>
/// 再起動前に予約されたプロファイル複製処理(フォルダコピー)を実行します。
/// </summary>
private void ProcessPendingProfileClone()
{
string pendingFile = Path.Combine(_userDataFolder, "pending_clone.json");
if (!File.Exists(pendingFile)) return;
try
{
string json = File.ReadAllText(pendingFile);
var info = JsonSerializer.Deserialize<CloneInfo>(json);
if (info != null && !string.IsNullOrEmpty(info.SourcePath) && !string.IsNullOrEmpty(info.DestPath))
{
if (Directory.Exists(info.SourcePath))
{
// WebView2が起動していない今なら確実にコピーできる
CopyDirectory(info.SourcePath, info.DestPath);
}
}
}
catch (Exception ex)
{
string msg = string.Format(XColumn.Properties.Resources.Msg_Err_ProfileCloneFailed, ex.Message);
MessageWindow.Show(msg, XColumn.Properties.Resources.Title_Error, MessageBoxButton.OK, MessageBoxImage.Error);
}
finally
{
// 処理が終わったら指示書を削除
try { File.Delete(pendingFile); } catch { }
}
}
private void CopyDirectory(string sourceDir, string destinationDir)
{
var dir = new DirectoryInfo(sourceDir);
if (!dir.Exists) return;
Directory.CreateDirectory(destinationDir);
foreach (FileInfo file in dir.GetFiles())
{
// Lockfile等は不要
if (file.Name.Equals("lockfile", StringComparison.OrdinalIgnoreCase)) continue;
try
{
file.CopyTo(Path.Combine(destinationDir, file.Name), true);
}
catch { /* アクセス拒否等は無視 */ }
}
foreach (DirectoryInfo subDir in dir.GetDirectories())
{
// Cacheフォルダ等は容量削減のため除外しても良いが、完全複製の観点で含める
// ただし "EBWebView" フォルダ配下のロックされやすい一時ファイルはエラーになりがちなので注意
CopyDirectory(subDir.FullName, Path.Combine(destinationDir, subDir.Name));
}
}
// コピー指示書用データクラス
private class CloneInfo
{
public string SourcePath { get; set; } = "";
public string DestPath { get; set; } = "";
}
}
}