1 |
package net.oni2.aeinstaller; |
2 |
|
3 |
import java.awt.image.BufferedImage; |
4 |
import java.io.File; |
5 |
import java.io.FileNotFoundException; |
6 |
import java.io.IOException; |
7 |
import java.io.PrintStream; |
8 |
import java.lang.reflect.InvocationTargetException; |
9 |
import java.lang.reflect.Method; |
10 |
import java.net.MalformedURLException; |
11 |
import java.net.URL; |
12 |
import java.net.URLClassLoader; |
13 |
import java.util.ResourceBundle; |
14 |
|
15 |
import javax.imageio.ImageIO; |
16 |
import javax.swing.JFrame; |
17 |
import javax.swing.JOptionPane; |
18 |
import javax.swing.JToolBar; |
19 |
import javax.swing.SwingUtilities; |
20 |
import javax.swing.UIManager; |
21 |
import javax.swing.UIManager.LookAndFeelInfo; |
22 |
|
23 |
import net.oni2.ProxySettings; |
24 |
import net.oni2.SettingsManager; |
25 |
import net.oni2.aeinstaller.backend.CaseInsensitiveFile; |
26 |
import net.oni2.aeinstaller.backend.Paths; |
27 |
import net.oni2.aeinstaller.backend.RuntimeOptions; |
28 |
import net.oni2.aeinstaller.backend.SizeFormatter; |
29 |
import net.oni2.aeinstaller.backend.oni.OniSplit; |
30 |
import net.oni2.aeinstaller.backend.oni.management.Installer; |
31 |
import net.oni2.aeinstaller.gui.MainWin; |
32 |
import net.oni2.moddepot.DepotManager; |
33 |
import net.oni2.platformtools.PlatformInformation; |
34 |
import net.oni2.platformtools.PlatformInformation.Platform; |
35 |
import net.oni2.platformtools.applicationinvoker.ApplicationInvoker; |
36 |
import net.oni2.platformtools.applicationinvoker.DotNet; |
37 |
import net.oni2.platformtools.applicationinvoker.EExeType; |
38 |
import net.oni2.resourcebundle.UTF8ResourceBundleLoader; |
39 |
import net.oni2.svnaccess.SVN; |
40 |
import net.oni2.swingcomponents.HTMLLinkLabel; |
41 |
|
42 |
import org.javabuilders.swing.SwingJavaBuilder; |
43 |
import org.simplericity.macify.eawt.Application; |
44 |
import org.simplericity.macify.eawt.DefaultApplication; |
45 |
|
46 |
/** |
47 |
* @author Christian Illy |
48 |
*/ |
49 |
public class AEInstaller2 { |
50 |
|
51 |
private static ResourceBundle imagesBundle = ResourceBundle |
52 |
.getBundle("net.oni2.aeinstaller.Images"); |
53 |
private static ResourceBundle basicBundle = ResourceBundle |
54 |
.getBundle("net.oni2.aeinstaller.AEInstaller"); |
55 |
private static ResourceBundle globalBundle; |
56 |
|
57 |
private static Application app = null; |
58 |
|
59 |
private static void initMacOS() { |
60 |
System.setProperty("apple.laf.useScreenMenuBar", "true"); |
61 |
System.setProperty("com.apple.mrj.application.apple.menu.about.name", |
62 |
basicBundle.getString("appname")); |
63 |
app = new DefaultApplication(); |
64 |
|
65 |
URL icon = AEInstaller2.class.getResource(imagesBundle |
66 |
.getString("img.ae")); |
67 |
try { |
68 |
BufferedImage img = ImageIO.read(icon); |
69 |
app.setApplicationIconImage(img); |
70 |
} catch (IOException e) { |
71 |
e.printStackTrace(); |
72 |
} |
73 |
} |
74 |
|
75 |
private static void initBundles() { |
76 |
File localesDir = CaseInsensitiveFile.getCaseInsensitiveFile( |
77 |
Paths.getInstallerPath(), "locales"); |
78 |
if (localesDir.isDirectory()) |
79 |
addClassPath(localesDir); |
80 |
else { |
81 |
File localesSubDir = CaseInsensitiveFile.getCaseInsensitiveFile( |
82 |
CaseInsensitiveFile.getCaseInsensitiveFile( |
83 |
Paths.getInstallerPath(), "bin"), "locales"); |
84 |
if (localesSubDir.isDirectory()) { |
85 |
addClassPath(localesSubDir); |
86 |
} |
87 |
} |
88 |
globalBundle = UTF8ResourceBundleLoader |
89 |
.getBundle("net.oni2.aeinstaller.localization.Global"); |
90 |
} |
91 |
|
92 |
private static void addClassPath(File dir) { |
93 |
try { |
94 |
URL u = dir.toURI().toURL(); |
95 |
URLClassLoader urlClassLoader = (URLClassLoader) ClassLoader |
96 |
.getSystemClassLoader(); |
97 |
Class<URLClassLoader> urlClass = URLClassLoader.class; |
98 |
Method method = urlClass.getDeclaredMethod("addURL", |
99 |
new Class[] { URL.class }); |
100 |
method.setAccessible(true); |
101 |
method.invoke(urlClassLoader, new Object[] { u }); |
102 |
} catch (MalformedURLException e) { |
103 |
} catch (SecurityException e) { |
104 |
} catch (NoSuchMethodException e) { |
105 |
} catch (IllegalArgumentException e) { |
106 |
} catch (IllegalAccessException e) { |
107 |
} catch (InvocationTargetException e) { |
108 |
} |
109 |
} |
110 |
|
111 |
/** |
112 |
* @param args |
113 |
* Command line arguments |
114 |
*/ |
115 |
public static void main(String[] args) { |
116 |
Paths.getPrefsPath().mkdirs(); |
117 |
Paths.getDownloadPath().mkdirs(); |
118 |
|
119 |
for (String a : args) { |
120 |
if (a.equalsIgnoreCase("-debug")) |
121 |
RuntimeOptions.setDebug(true); |
122 |
if (a.equalsIgnoreCase("-nocacheupdate")) |
123 |
RuntimeOptions.setNoCacheUpdateMode(true); |
124 |
if (a.equalsIgnoreCase("-offline")) |
125 |
RuntimeOptions.setOfflineMode(true); |
126 |
if (a.equalsIgnoreCase("-usewd")) |
127 |
RuntimeOptions.setUseWorkingDir(true); |
128 |
} |
129 |
if (!RuntimeOptions.isDebug()) { |
130 |
try { |
131 |
PrintStream ps = new PrintStream(new File(Paths.getPrefsPath(), |
132 |
"aei_output.log")); |
133 |
System.setOut(ps); |
134 |
System.setErr(ps); |
135 |
} catch (FileNotFoundException e1) { |
136 |
e1.printStackTrace(); |
137 |
} |
138 |
} |
139 |
|
140 |
if (PlatformInformation.getPlatform() == Platform.MACOS) |
141 |
initMacOS(); |
142 |
|
143 |
SettingsManager.deserializeFromFile(Paths.getSettingsFilename()); |
144 |
|
145 |
if (Paths.getProxySettingsFilename().exists()) { |
146 |
ProxySettings.deserializeFromFile(Paths.getProxySettingsFilename()); |
147 |
} |
148 |
|
149 |
initBundles(); |
150 |
|
151 |
SwingJavaBuilder.getConfig().addResourceBundle(imagesBundle); |
152 |
SwingJavaBuilder.getConfig().addResourceBundle(basicBundle); |
153 |
SwingJavaBuilder.getConfig().addResourceBundle(globalBundle); |
154 |
SwingJavaBuilder.getConfig().setMarkInvalidResourceBundleKeys(true); |
155 |
SwingJavaBuilder.getConfig().addType("JToolBarSeparator", |
156 |
JToolBar.Separator.class); |
157 |
|
158 |
System.setProperty("networkaddress.cache.ttl", "5"); |
159 |
System.setProperty("networkaddress.cache.negative.ttl", "1"); |
160 |
|
161 |
try { |
162 |
String laf = SettingsManager.getInstance().get("lookandfeel", |
163 |
(String) null); |
164 |
if (laf == null) { |
165 |
if (PlatformInformation.getPlatform() != Platform.LINUX) { |
166 |
laf = UIManager.getSystemLookAndFeelClassName(); |
167 |
} else { |
168 |
for (LookAndFeelInfo lafInfo : UIManager |
169 |
.getInstalledLookAndFeels()) { |
170 |
if (lafInfo.getName().equals("Nimbus")) |
171 |
laf = lafInfo.getClassName(); |
172 |
} |
173 |
} |
174 |
} |
175 |
if (laf == null) |
176 |
laf = UIManager.getSystemLookAndFeelClassName(); |
177 |
UIManager.setLookAndFeel(laf); |
178 |
} catch (Exception e) { |
179 |
e.printStackTrace(); |
180 |
} |
181 |
JFrame.setDefaultLookAndFeelDecorated(true); |
182 |
|
183 |
System.out.println(basicBundle.getString("appname") |
184 |
+ basicBundle.getString("appversion")); |
185 |
System.out.println("JarPath: " + Paths.getJarPath()); |
186 |
System.out.println("PrefsPath: " + Paths.getPrefsPath()); |
187 |
System.out.println("DataPath: " + Paths.getModsPath()); |
188 |
System.out.println("DownPath: " + Paths.getDownloadPath()); |
189 |
System.out.println("TempPath: " + Paths.getTempPath()); |
190 |
System.out.println("ValidPath: " + Installer.verifyRunningDirectory()); |
191 |
System.out.println("Platform: " + PlatformInformation.getPlatform()); |
192 |
System.out.println("Architect: " |
193 |
+ PlatformInformation.getArchitecture()); |
194 |
System.out.println(".NET: " + DotNet.isInstalled()); |
195 |
System.out.println("OniSplit: " + OniSplit.isOniSplitInstalled()); |
196 |
System.out.println("Globalized:" + Installer.isEditionInitialized()); |
197 |
|
198 |
// TODO: Check available space |
199 |
System.out |
200 |
.println("Free space on temp: " |
201 |
+ SizeFormatter.format(Paths.getTempPath() |
202 |
.getUsableSpace(), 3)); |
203 |
System.out.println("Free space on Jar: " |
204 |
+ SizeFormatter.format(Paths.getInstallerPath() |
205 |
.getUsableSpace(), 3)); |
206 |
System.out.println(); |
207 |
|
208 |
if (!DotNet.isInstalled()) { |
209 |
HTMLLinkLabel hll = new HTMLLinkLabel(); |
210 |
String dlUrl = ""; |
211 |
switch (PlatformInformation.getPlatform()) { |
212 |
case WIN: |
213 |
switch (PlatformInformation.getArchitecture()) { |
214 |
case X86: |
215 |
dlUrl = "http://download.microsoft.com/download/c/6/e/c6e88215-0178-4c6c-b5f3-158ff77b1f38/NetFx20SP2_x86.exe"; |
216 |
break; |
217 |
case AMD64: |
218 |
dlUrl = "http://download.microsoft.com/download/c/6/e/c6e88215-0178-4c6c-b5f3-158ff77b1f38/NetFx20SP2_x64.exe"; |
219 |
break; |
220 |
} |
221 |
break; |
222 |
default: |
223 |
dlUrl = "http://www.go-mono.com/mono-downloads/download.html"; |
224 |
} |
225 |
hll.setText(globalBundle |
226 |
.getString("dotNetMissing.text") |
227 |
.replaceAll( |
228 |
"%1", |
229 |
String.format("<a href=\"%s\">%s</a>", dlUrl, dlUrl))); |
230 |
JOptionPane.showMessageDialog(null, hll, |
231 |
globalBundle.getString("dotNetMissing.title"), |
232 |
JOptionPane.ERROR_MESSAGE); |
233 |
return; |
234 |
} |
235 |
|
236 |
if (!Installer.verifyRunningDirectory()) { |
237 |
JOptionPane.showMessageDialog(null, |
238 |
globalBundle.getString("invalidPath.text"), |
239 |
globalBundle.getString("invalidPath.title"), |
240 |
JOptionPane.ERROR_MESSAGE); |
241 |
if (!RuntimeOptions.isDebug()) { |
242 |
return; |
243 |
} |
244 |
} |
245 |
|
246 |
if (!RuntimeOptions.isOfflineMode()) { |
247 |
RuntimeOptions.setOfflineMode(!DepotManager.getInstance() |
248 |
.checkConnection()); |
249 |
} |
250 |
if (RuntimeOptions.isOfflineMode()) { |
251 |
JOptionPane.showMessageDialog(null, |
252 |
globalBundle.getString("offlineModeStartup.text"), |
253 |
globalBundle.getString("offlineModeStartup.title"), |
254 |
JOptionPane.INFORMATION_MESSAGE); |
255 |
} |
256 |
|
257 |
if (!RuntimeOptions.isOfflineMode()) { |
258 |
SVN svn = new SVN(); |
259 |
try { |
260 |
int x = svn.checkSVN("http://svn.aei.oni2.net", |
261 |
Paths.getJarPath()); |
262 |
if (x > 0) { |
263 |
// Update available or missing files |
264 |
int res = JOptionPane.showConfirmDialog(null, |
265 |
globalBundle.getString("aeiUpdateAvailable.text"), |
266 |
globalBundle.getString("aeiUpdateAvailable.title"), |
267 |
JOptionPane.YES_NO_OPTION, |
268 |
JOptionPane.INFORMATION_MESSAGE); |
269 |
if (res == JOptionPane.YES_OPTION) { |
270 |
File updater = new File(Paths.getInstallerPath(), |
271 |
"AEInstaller2Updater.jar"); |
272 |
ApplicationInvoker.execute(EExeType.JAR, null, updater, |
273 |
null, false); |
274 |
return; |
275 |
} |
276 |
} else if (x == 0) { |
277 |
// Up to date |
278 |
} else if (x < 0) { |
279 |
// No WC at given path |
280 |
} |
281 |
} catch (Exception e) { |
282 |
e.printStackTrace(); |
283 |
} |
284 |
} |
285 |
|
286 |
SwingUtilities.invokeLater(new Runnable() { |
287 |
public void run() { |
288 |
try { |
289 |
MainWin mw = new MainWin(); |
290 |
if (app != null) { |
291 |
app.addAboutMenuItem(); |
292 |
app.setEnabledAboutMenu(true); |
293 |
app.addPreferencesMenuItem(); |
294 |
app.setEnabledPreferencesMenu(true); |
295 |
app.addApplicationListener(mw); |
296 |
} |
297 |
mw.setVisible(true); |
298 |
} catch (Exception e) { |
299 |
e.printStackTrace(); |
300 |
} |
301 |
} |
302 |
}); |
303 |
|
304 |
} |
305 |
} |