LiteLoaderUpdateSite.java
5.53 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
/*
* This file is part of LiteLoader.
* Copyright (C) 2012-16 Adam Mummery-Smith
* All Rights Reserved.
*/
package com.mumfrey.liteloader.core;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import com.google.common.io.ByteSink;
import com.google.common.io.Files;
import com.mumfrey.liteloader.launch.ClassPathUtilities;
import com.mumfrey.liteloader.launch.LoaderProperties;
import com.mumfrey.liteloader.update.UpdateSite;
import com.mumfrey.liteloader.util.log.LiteLoaderLogger;
public class LiteLoaderUpdateSite extends UpdateSite
{
private static final String UPDATE_SITE_URL = "http://dl.liteloader.com/versions/";
private static final String UPDATE_SITE_VERSIONS_JSON = "versions.json";
private static final String UPDATE_SITE_ARTEFACT_NAME = "com.mumfrey:liteloader";
private String mcVersion;
private File mcDir;
private File jarFile = null;
private boolean updateForced = false;
public LiteLoaderUpdateSite(String targetVersion, long currentTimeStamp)
{
super(LiteLoaderUpdateSite.UPDATE_SITE_URL, LiteLoaderUpdateSite.UPDATE_SITE_VERSIONS_JSON, targetVersion,
LiteLoaderUpdateSite.UPDATE_SITE_ARTEFACT_NAME, currentTimeStamp);
this.mcVersion = targetVersion;
}
public boolean canForceUpdate(LoaderProperties properties)
{
if (!properties.getAndStoreBooleanProperty(LoaderProperties.OPTION_FORCE_UPDATE, false))
{
return false;
}
if (this.hasJarFile()) return true;
return this.findJarFile();
}
/**
*
*/
private boolean findJarFile()
{
// Find the jar containing liteloader
File jarFile = ClassPathUtilities.getPathToResource(LiteLoader.class, "/" + LiteLoader.class.getName().replace('.', '/') + ".class");
if (!jarFile.isFile()) return false;
// Validate that the jar is in the expected name and location
this.mcDir = this.walkAndValidateParents(jarFile, "liteloader-" + this.mcVersion + ".jar", this.mcVersion,
"liteloader", "mumfrey", "com", "libraries");
if (this.mcDir == null) return false;
// Check that the jar we found is actually on the current classpath
if (!ClassPathUtilities.isJarOnClassPath(jarFile)) return false;
this.jarFile = jarFile;
return true;
}
private File walkAndValidateParents(File file, String... breadcrumbs)
{
try
{
for (String breadcrumb : breadcrumbs)
{
if (file == null || !file.exists() || !file.getName().equals(breadcrumb)) return null;
file = file.getParentFile();
}
return file;
}
catch (Exception ex)
{
ex.printStackTrace();
}
return null;
}
public boolean canCheckForUpdate()
{
return !this.updateForced;
}
public boolean hasJarFile()
{
return this.jarFile != null;
}
public File getJarFile()
{
return this.jarFile;
}
public boolean forceUpdate()
{
if (this.jarFile != null)
{
LiteLoaderLogger.info("Attempting to force update, extracting jar assassin...");
File jarAssassinOutput = new File(this.jarFile.getParentFile(), "liteloader-update-agent.jar");
if (!LiteLoaderUpdateSite.extractFile("/update/liteloader-update-agent.jar", jarAssassinOutput) || !jarAssassinOutput.isFile())
{
LiteLoaderLogger.info("Couldn't extract jarassassin jar, can't force update");
return false;
}
File joptSimple = new File(this.mcDir, "libraries/net/sf/jopt-simple/jopt-simple/4.5/jopt-simple-4.5.jar");
ProcessBuilder jarAssassinProcBuilder = new ProcessBuilder(
LiteLoaderUpdateSite.getJavaExecutable().getAbsolutePath(),
"-cp", joptSimple.getAbsolutePath(),
"-jar", jarAssassinOutput.getAbsolutePath(),
"--jarFile", this.jarFile.getAbsolutePath()).directory(this.jarFile.getParentFile());
try
{
System.err.println(jarAssassinProcBuilder.command());
@SuppressWarnings("unused")
Process jarAssassin = jarAssassinProcBuilder.start();
ClassPathUtilities.deleteClassPathJar(this.jarFile.getAbsolutePath());
return true;
}
catch (Throwable th)
{
LiteLoaderLogger.info("Couldn't execute jarassassin jar, can't force update");
return false;
}
}
return false;
}
protected static boolean extractFile(String resourceName, File outputFile)
{
try
{
final InputStream inputStream = LiteLoaderUpdateSite.class.getResourceAsStream(resourceName);
final ByteSink outputSupplier = Files.asByteSink(outputFile);
outputSupplier.writeFrom(inputStream);
}
catch (NullPointerException ex)
{
return false;
}
catch (IOException ex)
{
return false;
}
return true;
}
protected static File getJavaExecutable()
{
File javaBin = new File(new File(System.getProperty("java.home")), "bin");
File javaWin = new File(javaBin, "javaw.exe");
String osName = System.getProperty("os.name").toLowerCase();
return osName.contains("win") && javaWin.isFile() ? javaWin : new File(javaBin, "java");
}
}