forked from yasirkula/UnitySimplePatchTool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PatcherUI.cs
176 lines (146 loc) · 4.27 KB
/
PatcherUI.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#if UNITY_EDITOR || UNITY_STANDALONE
using SimplePatchToolCore;
using System.Collections;
using System.IO;
using UnityEngine.UI;
#endif
using UnityEngine;
namespace SimplePatchToolUnity
{
[HelpURL( "https://github.com/yasirkula/UnitySimplePatchTool" )]
public class PatcherUI : MonoBehaviour
{
// SimplePatchTool works on only standalone platforms
#if UNITY_EDITOR || UNITY_STANDALONE
#pragma warning disable 0649
[SerializeField]
[Tooltip( "Should SimplePatchTool logs be logged to console" )]
private bool logToConsole = false;
[SerializeField]
private GameObject patchPanel;
[SerializeField]
private GameObject patchResultPanel;
[SerializeField]
private Text logText;
[SerializeField]
private Slider progressSlider;
[SerializeField]
private Slider overallProgressSlider;
[SerializeField]
private Text progressText;
[SerializeField]
private Text patchResultText;
[SerializeField]
private Button patcherButton;
[SerializeField]
private Text patcherButtonLabel;
#pragma warning restore 0649
private string selfPatcherExecutable;
public SimplePatchTool Patcher { get; private set; }
private void Awake()
{
DontDestroyOnLoad( gameObject );
patcherButton.onClick.AddListener( PatchResultButtonClicked );
}
public void Initialize( SimplePatchTool patcher, string selfPatcherExecutable = "SelfPatcher.exe" )
{
if( patcher == null )
return;
patchPanel.SetActive( true );
patchResultPanel.SetActive( false );
logText.text = string.Empty;
progressText.text = string.Empty;
progressSlider.value = 0f;
overallProgressSlider.value = 0f;
patcherButtonLabel.text = "Cancel";
Patcher = patcher;
this.selfPatcherExecutable = selfPatcherExecutable;
StartCoroutine( PatchCoroutine() );
}
private IEnumerator PatchCoroutine()
{
while( Patcher.IsRunning )
{
FetchLogsFromPatcher();
yield return null;
}
FetchLogsFromPatcher();
string resultText, resultButtonLabel;
if( Patcher.Result == PatchResult.Failed )
{
resultText = Patcher.FailDetails;
resultButtonLabel = "Dismiss"; // "Try Again";
}
else if( Patcher.Result == PatchResult.AlreadyUpToDate )
{
resultText = "Already up-to-date";
resultButtonLabel = "Dismiss";
}
else if( Patcher.Operation == PatchOperation.SelfPatching )
{
resultText = "Restart the game to finish updating";
resultButtonLabel = "Restart Now";
}
else
{
resultText = "Patch is successful";
resultButtonLabel = "Dismiss";
}
patchResultText.text = resultText;
patcherButtonLabel.text = resultButtonLabel;
patcherButton.interactable = true;
patchPanel.SetActive( false );
patchResultPanel.SetActive( true );
}
private void FetchLogsFromPatcher()
{
string log = Patcher.FetchLog();
while( log != null )
{
if( logToConsole )
Debug.Log( log );
logText.text = log;
log = Patcher.FetchLog();
}
IOperationProgress progress = Patcher.FetchProgress();
while( progress != null )
{
if( logToConsole )
Debug.Log( string.Concat( progress.Percentage, "% ", progress.ProgressInfo ) );
progressText.text = progress.ProgressInfo;
progressSlider.value = progress.Percentage;
progress = Patcher.FetchProgress();
}
IOperationProgress overallProgress = Patcher.FetchOverallProgress();
while( overallProgress != null )
{
overallProgressSlider.value = overallProgress.Percentage;
overallProgress = Patcher.FetchOverallProgress();
}
}
private void PatchResultButtonClicked()
{
if( Patcher.IsRunning )
{
Patcher.Cancel();
patcherButton.interactable = false;
}
//else if( patcher.Result == PatchResult.Failed )
//{
// patcher.Run( patcher.Operation == PatchOperation.SelfPatching );
// Initialize( patcher );
//}
else if( Patcher.Result == PatchResult.Success && Patcher.Operation == PatchOperation.SelfPatching )
{
string selfPatcherPath = PatchUtils.GetDefaultSelfPatcherExecutablePath( selfPatcherExecutable );
if( !string.IsNullOrEmpty( selfPatcherPath ) && File.Exists( selfPatcherPath ) )
Patcher.ApplySelfPatch( selfPatcherPath, PatchUtils.GetCurrentExecutablePath() );
else
patchResultText.text = "Self patcher does not exist!";
}
else
Destroy( gameObject );
}
#endif
}
}