-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFileSelectControl.cs
121 lines (104 loc) · 3.46 KB
/
FileSelectControl.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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ToVPatcher.Properties;
namespace ToVPatcher {
public partial class FileSelectControl : UserControl {
public string LabelText {
get { return nameLabel.Text; }
set { nameLabel.Text = value; }
}
public string FilePath {
get { return textBox1.Text; }
set { textBox1.Text = value; }
}
public string PatchDir;
public string OutputChecksum = null;
public bool Successful;
public FileSelectControl() {
InitializeComponent();
pictureBox1.Hide();
Successful = false;
backgroundWorker.WorkerReportsProgress = true;
}
private void selectFileButton_Click( object sender, EventArgs e ) {
var dialog = new OpenFileDialog();
dialog.FileName = LabelText;
var result = dialog.ShowDialog();
if ( result == DialogResult.OK ) {
FilePath = dialog.FileName;
Successful = false;
ShowIconNone();
}
}
public void ShowIconNone() {
pictureBox1.Hide();
}
public void ShowIconSuccess() {
pictureBox1.Image = Resources.dialog_clean;
pictureBox1.Show();
}
public void ShowIconError() {
pictureBox1.Image = Resources.exclamation;
pictureBox1.Show();
}
public void ShowIconLoading() {
pictureBox1.Image = Resources.loading;
pictureBox1.Show();
}
public delegate void PatchDelegate( string file, string patchDir, string outDir, string outMd5 = null, BackgroundWorker worker = null );
public PatchDelegate PatchFunction;
public string OutDir;
public void StartWorker() {
if ( !Successful && !backgroundWorker.IsBusy ) {
backgroundWorker.RunWorkerAsync();
}
}
public bool IsWorkerRunning() {
return backgroundWorker.IsBusy;
}
public delegate void VoidDelegate();
public delegate void StringDelegate( string text );
private void UpdateStatusMessage( string text ) {
labelStatusMessage.Text = text;
}
private void backgroundWorker_DoWork( object sender, DoWorkEventArgs e ) {
BackgroundWorker worker = ( (BackgroundWorker)sender );
Invoke( new VoidDelegate( ShowIconLoading ) );
worker.ReportProgress( 0, "Patching " + LabelText + "..." );
// if file is readonly remove that property
if ( System.IO.File.Exists( FilePath ) ) {
var attr = System.IO.File.GetAttributes( FilePath );
if ( ( attr & System.IO.FileAttributes.ReadOnly ) == System.IO.FileAttributes.ReadOnly ) {
System.IO.File.SetAttributes( FilePath, attr & ~System.IO.FileAttributes.ReadOnly );
}
}
PatchFunction( FilePath, PatchDir, OutDir, OutputChecksum, worker );
worker.ReportProgress( 100, "Successfully patched " + LabelText + "!" );
}
private void backgroundWorker_ProgressChanged( object sender, ProgressChangedEventArgs e ) {
if ( e.UserState != null ) {
Invoke( new StringDelegate( UpdateStatusMessage ), (string)e.UserState );
}
}
private void backgroundWorker_RunWorkerCompleted( object sender, RunWorkerCompletedEventArgs e ) {
if ( e.Error != null ) {
Invoke( new VoidDelegate( ShowIconError ) );
Invoke( new StringDelegate( UpdateStatusMessage ), "Error: " + e.Error.GetType() + ": " + e.Error.Message );
Logger.LogError( e.Error.ToString() );
} else {
Invoke( new VoidDelegate( ShowIconSuccess ) );
Successful = true;
}
}
public void SetInteractionEnabled( bool value ) {
selectFileButton.Enabled = value;
textBox1.Enabled = value;
}
}
}