GuiPanelUpdateCheck.java
7.22 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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/*
* This file is part of LiteLoader.
* Copyright (C) 2012-16 Adam Mummery-Smith
* All Rights Reserved.
*/
package com.mumfrey.liteloader.client.gui;
import java.net.URI;
import org.lwjgl.input.Keyboard;
import com.mumfrey.liteloader.core.LiteLoaderUpdateSite;
import com.mumfrey.liteloader.launch.ClassPathUtilities;
import com.mumfrey.liteloader.launch.LoaderProperties;
import com.mumfrey.liteloader.update.UpdateSite;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.FontRenderer;
import net.minecraft.client.gui.GuiButton;
import net.minecraft.client.gui.ScaledResolution;
import net.minecraft.client.resources.I18n;
/**
* "Check for updates" panel which docks in the mod info screen
*
* @author Adam Mummery-Smith
*/
class GuiPanelUpdateCheck extends GuiPanel
{
private static final int WHITE = 0xFFFFFFFF;
/**
* URI to open if a new version is available
*/
private static final URI DOWNLOAD_URI = URI.create("http://dl.liteloader.com");
private final GuiLiteLoaderPanel parentScreen;
/**
* Update site to contact
*/
private final UpdateSite updateSite;
/**
* Panel title
*/
private final String panelTitle;
/**
* Buttons
*/
private GuiButton btnCheck, btnDownload;
/**
* Throbber frame
*/
private int throb;
private boolean canForceUpdate, updateForced;
public GuiPanelUpdateCheck(GuiLiteLoaderPanel parentScreen, Minecraft minecraft, UpdateSite updateSite, String updateName,
LoaderProperties properties)
{
super(minecraft);
this.parentScreen = parentScreen;
this.updateSite = updateSite;
this.panelTitle = I18n.format("gui.updates.title", updateName);
this.canForceUpdate = (updateSite instanceof LiteLoaderUpdateSite && ((LiteLoaderUpdateSite)updateSite).canForceUpdate(properties));
}
@Override
void setSize(int width, int height)
{
super.setSize(width, height);
this.controls.add(new GuiButton(0, this.width - 99 - MARGIN, this.height - BOTTOM + 9, 100, 20,
this.updateForced ? I18n.format("gui.exitgame") : I18n.format("gui.done")));
this.controls.add(this.btnCheck = new GuiButton(1, MARGIN + 16, TOP + 16, 100, 20,
I18n.format("gui.checknow")));
this.controls.add(this.btnDownload = new GuiButton(2, MARGIN + 16, TOP + 118, 100, 20,
this.canForceUpdate ? I18n.format("gui.forceupdate") : I18n.format("gui.downloadupdate")));
}
@Override
void draw(int mouseX, int mouseY, float partialTicks)
{
FontRenderer fontRenderer = this.mc.fontRendererObj;
// Draw panel title
fontRenderer.drawString(this.panelTitle, MARGIN, TOP - 14, GuiPanelUpdateCheck.WHITE);
// Draw top and bottom horizontal bars
drawRect(MARGIN, TOP - 4, this.width - MARGIN, TOP - 3, 0xFF999999);
drawRect(MARGIN, this.height - BOTTOM + 2, this.width - MARGIN, this.height - BOTTOM + 3, 0xFF999999);
this.btnCheck.enabled = !this.updateForced && !this.updateSite.isCheckInProgress();
this.btnDownload.visible = false;
if (this.updateSite.isCheckInProgress())
{
this.drawThrobber(MARGIN, TOP + 40, this.throb);
fontRenderer.drawString(I18n.format("gui.updates.status.checking", ""), MARGIN + 18, TOP + 44, GuiPanelUpdateCheck.WHITE);
}
else if (this.updateSite.isCheckComplete())
{
boolean success = this.updateSite.isCheckSucceess();
String status = success ? I18n.format("gui.updates.status.success") : I18n.format("gui.updates.status.failed");
fontRenderer.drawString(I18n.format("gui.updates.status.checking", status), MARGIN + 18, TOP + 44, GuiPanelUpdateCheck.WHITE);
if (success)
{
fontRenderer.drawString(I18n.format("gui.updates.available.title"), MARGIN + 18, TOP + 70, GuiPanelUpdateCheck.WHITE);
if (this.updateSite.isUpdateAvailable())
{
this.btnDownload.visible = !this.updateForced;
fontRenderer.drawString(I18n.format("gui.updates.available.newversion"), MARGIN + 18, TOP + 84, GuiPanelUpdateCheck.WHITE);
fontRenderer.drawString(I18n.format("gui.updates.available.version", this.updateSite.getAvailableVersion()),
MARGIN + 18, TOP + 94, GuiPanelUpdateCheck.WHITE);
fontRenderer.drawString(I18n.format("gui.updates.available.date", this.updateSite.getAvailableVersionDate()),
MARGIN + 18, TOP + 104, GuiPanelUpdateCheck.WHITE);
if (this.updateForced)
{
fontRenderer.drawString(I18n.format("gui.updates.forced"), MARGIN + 18, TOP + 144, 0xFFFFAA00);
}
}
else
{
fontRenderer.drawString(I18n.format("gui.updates.available.nonewversion"), MARGIN + 18, TOP + 84, GuiPanelUpdateCheck.WHITE);
}
}
}
else
{
fontRenderer.drawString(I18n.format("gui.updates.status.idle"), MARGIN + 18, TOP + 44, GuiPanelUpdateCheck.WHITE);
}
super.draw(mouseX, mouseY, partialTicks);
}
@Override
public void close()
{
if (this.updateForced)
{
return;
}
super.close();
}
/**
* @param control
*/
@Override
void actionPerformed(GuiButton control)
{
if (control.id == 0)
{
if (this.updateForced)
{
ClassPathUtilities.terminateRuntime(0);
return;
}
this.close();
}
if (control.id == 1) this.updateSite.beginUpdateCheck();
if (control.id == 2)
{
if (this.canForceUpdate && ((LiteLoaderUpdateSite)this.updateSite).forceUpdate())
{
this.updateForced = true;
this.parentScreen.setToggleable(false);
ScaledResolution sr = new ScaledResolution(this.mc);
this.parentScreen.setWorldAndResolution(this.mc, sr.getScaledWidth(), sr.getScaledHeight());
}
else
{
this.openURI(GuiPanelUpdateCheck.DOWNLOAD_URI);
}
this.btnDownload.enabled = false;
}
}
private void openURI(URI uri)
{
try
{
Class<?> desktop = Class.forName("java.awt.Desktop");
Object instance = desktop.getMethod("getDesktop").invoke(null);
desktop.getMethod("browse", URI.class).invoke(instance, uri);
}
catch (Throwable th) {}
}
@Override
void onTick()
{
this.throb++;
}
@Override
void onHidden()
{
}
@Override
void onShown()
{
}
@Override
void keyPressed(char keyChar, int keyCode)
{
if (keyCode == Keyboard.KEY_ESCAPE) this.close();
}
@Override
void mouseMoved(int mouseX, int mouseY)
{
}
@Override
void mouseReleased(int mouseX, int mouseY, int mouseButton)
{
}
@Override
void mouseWheelScrolled(int mouseWheelDelta)
{
}
}