| 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.aeinstaller.backend.Paths; |
| 24 |
import net.oni2.aeinstaller.backend.Settings; |
| 25 |
import net.oni2.aeinstaller.backend.Settings.Platform; |
| 26 |
import net.oni2.aeinstaller.backend.SizeFormatter; |
| 27 |
import net.oni2.aeinstaller.backend.depot.DepotManager; |
| 28 |
import net.oni2.aeinstaller.backend.oni.Installer; |
| 29 |
import net.oni2.aeinstaller.backend.oni.OniSplit; |
| 30 |
import net.oni2.aeinstaller.gui.HTMLLinkLabel; |
| 31 |
import net.oni2.aeinstaller.gui.MainWin; |
| 32 |
|
| 33 |
import org.javabuilders.swing.SwingJavaBuilder; |
| 34 |
import org.simplericity.macify.eawt.Application; |
| 35 |
import org.simplericity.macify.eawt.DefaultApplication; |
| 36 |
|
| 37 |
/** |
| 38 |
* @author Christian Illy |
| 39 |
*/ |
| 40 |
public class AEInstaller2 { |
| 41 |
|
| 42 |
private static ResourceBundle imagesBundle; |
| 43 |
private static ResourceBundle basicBundle; |
| 44 |
private static ResourceBundle globalBundle; |
| 45 |
|
| 46 |
private static Application app = null; |
| 47 |
|
| 48 |
private static void initMacOS() { |
| 49 |
System.setProperty("apple.laf.useScreenMenuBar", "true"); |
| 50 |
System.setProperty("com.apple.mrj.application.apple.menu.about.name", |
| 51 |
basicBundle.getString("appname")); |
| 52 |
app = new DefaultApplication(); |
| 53 |
|
| 54 |
URL icon = AEInstaller2.class.getResource(imagesBundle |
| 55 |
.getString("img.ae")); |
| 56 |
try { |
| 57 |
BufferedImage img = ImageIO.read(icon); |
| 58 |
app.setApplicationIconImage(img); |
| 59 |
} catch (IOException e) { |
| 60 |
e.printStackTrace(); |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
private static void addClassPath(File dir) { |
| 65 |
try { |
| 66 |
URL u = dir.toURI().toURL(); |
| 67 |
URLClassLoader urlClassLoader = (URLClassLoader) ClassLoader |
| 68 |
.getSystemClassLoader(); |
| 69 |
Class<URLClassLoader> urlClass = URLClassLoader.class; |
| 70 |
Method method = urlClass.getDeclaredMethod("addURL", |
| 71 |
new Class[] { URL.class }); |
| 72 |
method.setAccessible(true); |
| 73 |
method.invoke(urlClassLoader, new Object[] { u }); |
| 74 |
} catch (MalformedURLException e) { |
| 75 |
} catch (SecurityException e) { |
| 76 |
} catch (NoSuchMethodException e) { |
| 77 |
} catch (IllegalArgumentException e) { |
| 78 |
} catch (IllegalAccessException e) { |
| 79 |
} catch (InvocationTargetException e) { |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* @param args |
| 85 |
* Command line arguments |
| 86 |
*/ |
| 87 |
public static void main(String[] args) { |
| 88 |
Paths.getPrefsPath().mkdirs(); |
| 89 |
Paths.getDownloadPath().mkdirs(); |
| 90 |
|
| 91 |
boolean debug = false; |
| 92 |
boolean noCacheUpdate = false; |
| 93 |
for (String a : args) { |
| 94 |
if (a.equalsIgnoreCase("-debug")) |
| 95 |
debug = true; |
| 96 |
if (a.equalsIgnoreCase("-nocacheupdate")) |
| 97 |
noCacheUpdate = true; |
| 98 |
} |
| 99 |
if (!debug) { |
| 100 |
try { |
| 101 |
PrintStream ps = new PrintStream(new File(Paths.getPrefsPath(), |
| 102 |
"aei_output.log")); |
| 103 |
System.setOut(ps); |
| 104 |
System.setErr(ps); |
| 105 |
} catch (FileNotFoundException e1) { |
| 106 |
e1.printStackTrace(); |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
if (Settings.getPlatform() == Platform.MACOS) |
| 111 |
initMacOS(); |
| 112 |
|
| 113 |
Settings.setDebug(debug); |
| 114 |
Settings.deserializeFromFile(); |
| 115 |
Settings.setDebug(debug); |
| 116 |
Settings.getInstance().setNoCacheUpdateMode(noCacheUpdate); |
| 117 |
|
| 118 |
File localesDir = new File(Paths.getInstallerPath(), "locales"); |
| 119 |
if (localesDir.isDirectory()) |
| 120 |
addClassPath(localesDir); |
| 121 |
imagesBundle = ResourceBundle.getBundle("net.oni2.aeinstaller.Images"); |
| 122 |
basicBundle = ResourceBundle |
| 123 |
.getBundle("net.oni2.aeinstaller.AEInstaller"); |
| 124 |
globalBundle = ResourceBundle |
| 125 |
.getBundle("net.oni2.aeinstaller.localization.Global"); |
| 126 |
|
| 127 |
SwingJavaBuilder.getConfig().addResourceBundle(imagesBundle); |
| 128 |
SwingJavaBuilder.getConfig().addResourceBundle(basicBundle); |
| 129 |
SwingJavaBuilder.getConfig().addResourceBundle(globalBundle); |
| 130 |
SwingJavaBuilder.getConfig().setMarkInvalidResourceBundleKeys(true); |
| 131 |
SwingJavaBuilder.getConfig().addType("JToolBarSeparator", |
| 132 |
JToolBar.Separator.class); |
| 133 |
|
| 134 |
System.setProperty("networkaddress.cache.ttl", "5"); |
| 135 |
System.setProperty("networkaddress.cache.negative.ttl", "1"); |
| 136 |
|
| 137 |
try { |
| 138 |
String laf = Settings.getInstance().get("lookandfeel", |
| 139 |
(String) null); |
| 140 |
if (laf == null) { |
| 141 |
if (Settings.getPlatform() != Platform.LINUX) { |
| 142 |
laf = UIManager.getSystemLookAndFeelClassName(); |
| 143 |
} else { |
| 144 |
for (LookAndFeelInfo lafInfo : UIManager |
| 145 |
.getInstalledLookAndFeels()) { |
| 146 |
if (lafInfo.getName().equals("Nimbus")) |
| 147 |
laf = lafInfo.getClassName(); |
| 148 |
} |
| 149 |
} |
| 150 |
} |
| 151 |
if (laf == null) |
| 152 |
laf = UIManager.getSystemLookAndFeelClassName(); |
| 153 |
UIManager.setLookAndFeel(laf); |
| 154 |
} catch (Exception e) { |
| 155 |
e.printStackTrace(); |
| 156 |
} |
| 157 |
JFrame.setDefaultLookAndFeelDecorated(true); |
| 158 |
|
| 159 |
System.out.println(basicBundle.getString("appname") + " " |
| 160 |
+ basicBundle.getString("appversion")); |
| 161 |
System.out.println("JarPath: " + Paths.getInstallerPath()); |
| 162 |
System.out.println("PrefsPath: " + Paths.getPrefsPath()); |
| 163 |
System.out.println("DataPath: " + Paths.getModsPath()); |
| 164 |
System.out.println("DownPath: " + Paths.getDownloadPath()); |
| 165 |
System.out.println("TempPath: " + Paths.getTempPath()); |
| 166 |
System.out.println("ValidPath: " + Installer.verifyRunningDirectory()); |
| 167 |
System.out.println("Platform: " + Settings.getPlatform()); |
| 168 |
System.out.println("Architect: " + Settings.getArchitecture()); |
| 169 |
System.out.println(".NET: " + OniSplit.isDotNETInstalled()); |
| 170 |
System.out.println("OniSplit: " + OniSplit.isOniSplitInstalled()); |
| 171 |
System.out.println("Globalized:" + Installer.isEditionInitialized()); |
| 172 |
|
| 173 |
// TODO: Check available space |
| 174 |
System.out |
| 175 |
.println("Free space on temp: " |
| 176 |
+ SizeFormatter.format(Paths.getTempPath() |
| 177 |
.getUsableSpace(), 3)); |
| 178 |
System.out.println("Free space on Jar: " |
| 179 |
+ SizeFormatter.format(Paths.getInstallerPath() |
| 180 |
.getUsableSpace(), 3)); |
| 181 |
System.out.println(); |
| 182 |
|
| 183 |
if (!OniSplit.isDotNETInstalled()) { |
| 184 |
HTMLLinkLabel hll = new HTMLLinkLabel(); |
| 185 |
String dlUrl = ""; |
| 186 |
switch (Settings.getPlatform()) { |
| 187 |
case WIN: |
| 188 |
switch (Settings.getArchitecture()) { |
| 189 |
case X86: |
| 190 |
dlUrl = "http://download.microsoft.com/download/c/6/e/c6e88215-0178-4c6c-b5f3-158ff77b1f38/NetFx20SP2_x86.exe"; |
| 191 |
break; |
| 192 |
case AMD64: |
| 193 |
dlUrl = "http://download.microsoft.com/download/c/6/e/c6e88215-0178-4c6c-b5f3-158ff77b1f38/NetFx20SP2_x64.exe"; |
| 194 |
break; |
| 195 |
} |
| 196 |
break; |
| 197 |
default: |
| 198 |
dlUrl = "http://www.go-mono.com/mono-downloads/download.html"; |
| 199 |
} |
| 200 |
hll.setText(globalBundle |
| 201 |
.getString("dotNetMissing.text") |
| 202 |
.replaceAll( |
| 203 |
"%1", |
| 204 |
String.format("<a href=\"%s\">%s</a>", dlUrl, dlUrl))); |
| 205 |
JOptionPane.showMessageDialog(null, hll, |
| 206 |
globalBundle.getString("dotNetMissing.title"), |
| 207 |
JOptionPane.ERROR_MESSAGE); |
| 208 |
return; |
| 209 |
} |
| 210 |
|
| 211 |
if (!Installer.verifyRunningDirectory()) { |
| 212 |
JOptionPane.showMessageDialog(null, |
| 213 |
globalBundle.getString("invalidPath.text"), |
| 214 |
globalBundle.getString("invalidPath.title"), |
| 215 |
JOptionPane.ERROR_MESSAGE); |
| 216 |
if (!Settings.isDebug()) { |
| 217 |
return; |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
boolean offline = false; |
| 222 |
for (String a : args) |
| 223 |
if (a.equalsIgnoreCase("-offline")) |
| 224 |
offline = true; |
| 225 |
if (!offline) { |
| 226 |
offline = !DepotManager.getInstance().checkConnection(); |
| 227 |
} |
| 228 |
if (offline) { |
| 229 |
JOptionPane.showMessageDialog(null, |
| 230 |
globalBundle.getString("offlineModeStartup.text"), |
| 231 |
globalBundle.getString("offlineModeStartup.title"), |
| 232 |
JOptionPane.INFORMATION_MESSAGE); |
| 233 |
} |
| 234 |
Settings.getInstance().setOfflineMode(offline); |
| 235 |
|
| 236 |
SwingUtilities.invokeLater(new Runnable() { |
| 237 |
public void run() { |
| 238 |
try { |
| 239 |
MainWin mw = new MainWin(); |
| 240 |
if (app != null) { |
| 241 |
app.addAboutMenuItem(); |
| 242 |
app.setEnabledAboutMenu(true); |
| 243 |
app.addPreferencesMenuItem(); |
| 244 |
app.setEnabledPreferencesMenu(true); |
| 245 |
app.addApplicationListener(mw); |
| 246 |
} |
| 247 |
mw.setVisible(true); |
| 248 |
} catch (Exception e) { |
| 249 |
e.printStackTrace(); |
| 250 |
} |
| 251 |
} |
| 252 |
}); |
| 253 |
|
| 254 |
} |
| 255 |
} |