-
Notifications
You must be signed in to change notification settings - Fork 0
/
UpdatingForm.cs
105 lines (95 loc) · 4.04 KB
/
UpdatingForm.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
using Microsoft.WindowsAPICodePack.Taskbar;
using System;
using System.Windows.Forms;
using TSB_Updater.util;
namespace TSB_Updater
{
public partial class UpdatingForm : Form
{
public int Result = UpdateResult.CANCEL;
private string worldPath;
private Release release;
private bool asServer;
private bool updating = true;
private UpdateRunner updateRunner;
public UpdatingForm(string worldPath, Release release, bool asServer = false)
{
InitializeComponent();
this.worldPath = worldPath;
this.release = release;
this.asServer = asServer;
}
private async void UpdatingForm_Load(object sender, EventArgs e)
{
this.FormBorderStyle = FormBorderStyle.FixedSingle;
updating = true;
updateRunner = new UpdateRunner(worldPath, release, asServer);
updateRunner.UpdateProgressChanged += UpdateRunner_ChangedUpdateProgress;
updateRunner.Completed += UpdateRunner_Completed;
try
{
TaskbarManager.Instance.SetProgressState(TaskbarProgressBarState.Normal);
await updateRunner.Run();
}
catch (Exception)
{
TaskbarManager.Instance.SetProgressState(TaskbarProgressBarState.Error);
MessageBox.Show("エラーが発生しました。", "エラー", MessageBoxButtons.OK, MessageBoxIcon.Error);
this.updating = false;
this.Result = UpdateResult.FAILED;
TaskbarManager.Instance.SetProgressState(TaskbarProgressBarState.NoProgress);
this.Close();
}
}
private void UpdateRunner_Completed(object sender, EventArgs e)
{
updating = false;
updateRunner.Dispose();
this.Invoke((MethodInvoker)delegate ()
{
this.Result = UpdateResult.OK;
TaskbarManager.Instance.SetProgressState(TaskbarProgressBarState.NoProgress);
this.Close();
});
}
private void UpdateRunner_ChangedUpdateProgress(object sender, UpdateProgressArgs e)
{
if (!((UpdateRunner)sender).IsDisposed)
{
this.Invoke((MethodInvoker)delegate ()
{
switch (e.State)
{
case UpdateState.DownloadingDatapacks:
infoLabel.Text = $"更新をダウンロード中 {Decimal.Divide(e.Processed, e.Total) * 100:0}% ({e.Processed / 1000000.0d:0.0}/{e.Total / 1000000.0d:0.0} MB)";
break;
case UpdateState.Extracting:
infoLabel.Text = $"更新を適用中 {Decimal.Divide(e.Processed, e.Total) * 100:0}% ({e.Processed}/{e.Total})";
break;
case UpdateState.DownloadingResoursepack:
infoLabel.Text = $"リソースパックをダウンロード中 {Decimal.Divide(e.Processed, e.Total) * 100:0}% ({e.Processed / 1000000.0d:0.0}/{e.Total / 1000000.0d:0.0} MB)";
break;
}
progressBar.Value = (int)(Decimal.Divide(e.Processed, e.Total) * 100);
TaskbarManager.Instance.SetProgressValue((int)(Decimal.Divide(e.Processed, e.Total) * 100), 100);
});
}
}
private void UpdatingForm_FormClosing(object sender, FormClosingEventArgs e)
{
if (updating)
{
DialogResult dr = MessageBox.Show("更新を中断してよろしいですか?", "確認", MessageBoxButtons.YesNo);
if (dr == DialogResult.Yes)
{
updateRunner.Cancel();
this.Result = UpdateResult.CANCEL;
}
else
{
e.Cancel = true;
}
}
}
}
}