mirror of
https://github.com/GravitLauncher/Launcher
synced 2024-11-15 03:31:15 +03:00
Выполнил ревью кода с правками. (#153)
* [ANY] Автоматический запуск task build при запуске gradle без параметров. * [ANY] Пересмотр modulesManager. * [ANY] Закончен review libLauncher`а. * [FIX] Logger и ServerWrapper или e.printStackTrace вместо нормального логгирования. * [ANY] Разгрёб auth* и hwidHandler. * [FEATURE] Modules этап finish. + [FIX] Правки LauncherEngine. * [FIX] Проверки безопасности в loadModuleFull. + [FIX] finishModules в launchServer.
This commit is contained in:
parent
25a0287f1d
commit
f00c0489a7
29 changed files with 120 additions and 233 deletions
|
@ -38,7 +38,6 @@
|
|||
import java.io.IOException;
|
||||
import java.lang.ProcessBuilder.Redirect;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.SocketAddress;
|
||||
import java.nio.file.*;
|
||||
import java.nio.file.attribute.BasicFileAttributes;
|
||||
|
@ -586,6 +585,7 @@ public void run() {
|
|||
JVMHelper.RUNTIME.addShutdownHook(CommonHelper.newThread(null, false, this::close));
|
||||
CommonHelper.newThread("Command Thread", true, commandHandler).start();
|
||||
rebindServerSocket();
|
||||
modulesManager.finishModules();
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -4,7 +4,6 @@
|
|||
import ru.gravit.utils.helper.LogHelper;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
|
@ -66,11 +65,8 @@ protected Entry fetchEntry(UUID uuid) throws IOException {
|
|||
|
||||
private Entry query(String sql, String value) throws IOException {
|
||||
try {
|
||||
Connection c = mySQLHolder.getConnection();
|
||||
PreparedStatement s = c.prepareStatement(sql);
|
||||
PreparedStatement s = mySQLHolder.getConnection().prepareStatement(sql);
|
||||
s.setString(1, value);
|
||||
|
||||
// Execute query
|
||||
s.setQueryTimeout(MySQLSourceConfig.TIMEOUT);
|
||||
try (ResultSet set = s.executeQuery()) {
|
||||
return constructEntry(set);
|
||||
|
@ -83,13 +79,10 @@ private Entry query(String sql, String value) throws IOException {
|
|||
@Override
|
||||
protected boolean updateAuth(UUID uuid, String username, String accessToken) throws IOException {
|
||||
try {
|
||||
Connection c = mySQLHolder.getConnection();
|
||||
PreparedStatement s = c.prepareStatement(updateAuthSQL);
|
||||
PreparedStatement s = mySQLHolder.getConnection().prepareStatement(updateAuthSQL);
|
||||
s.setString(1, username); // Username case
|
||||
s.setString(2, accessToken);
|
||||
s.setString(3, uuid.toString());
|
||||
|
||||
// Execute update
|
||||
s.setQueryTimeout(MySQLSourceConfig.TIMEOUT);
|
||||
return s.executeUpdate() > 0;
|
||||
} catch (SQLException e) {
|
||||
|
@ -100,12 +93,9 @@ protected boolean updateAuth(UUID uuid, String username, String accessToken) thr
|
|||
@Override
|
||||
protected boolean updateServerID(UUID uuid, String serverID) throws IOException {
|
||||
try {
|
||||
Connection c = mySQLHolder.getConnection();
|
||||
PreparedStatement s = c.prepareStatement(updateServerIDSQL);
|
||||
PreparedStatement s = mySQLHolder.getConnection().prepareStatement(updateServerIDSQL);
|
||||
s.setString(1, serverID);
|
||||
s.setString(2, uuid.toString());
|
||||
|
||||
// Execute update
|
||||
s.setQueryTimeout(MySQLSourceConfig.TIMEOUT);
|
||||
return s.executeUpdate() > 0;
|
||||
} catch (SQLException e) {
|
||||
|
|
|
@ -226,6 +226,6 @@ public List<HWID> getHwid(String username) {
|
|||
|
||||
@Override
|
||||
public void close() {
|
||||
// Do nothing
|
||||
mySQLHolder.close();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -7,7 +7,6 @@
|
|||
import ru.gravit.utils.helper.LogHelper;
|
||||
import ru.gravit.utils.helper.SecurityHelper;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
|
@ -28,8 +27,7 @@ public void init() {
|
|||
|
||||
@Override
|
||||
public AuthProviderResult auth(String login, String password, String ip) throws SQLException, AuthException {
|
||||
Connection c = mySQLHolder.getConnection();
|
||||
PreparedStatement s = c.prepareStatement(query);
|
||||
PreparedStatement s = mySQLHolder.getConnection().prepareStatement(query);
|
||||
String[] replaceParams = {"login", login, "password", password, "ip", ip};
|
||||
for (int i = 0; i < queryParams.length; i++)
|
||||
s.setString(i + 1, CommonHelper.replace(queryParams[i], replaceParams));
|
||||
|
@ -43,6 +41,6 @@ public AuthProviderResult auth(String login, String password, String ip) throws
|
|||
|
||||
@Override
|
||||
public void close() {
|
||||
// Do nothing
|
||||
mySQLHolder.close();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,20 +1,16 @@
|
|||
package ru.gravit.launchserver.command.basic;
|
||||
|
||||
import ru.gravit.launcher.profiles.ClientProfile;
|
||||
import ru.gravit.launchserver.LaunchServer;
|
||||
import ru.gravit.launchserver.command.Command;
|
||||
import ru.gravit.launchserver.socket.NettyServerSocketHandler;
|
||||
import ru.gravit.utils.helper.CommonHelper;
|
||||
import ru.gravit.utils.helper.IOHelper;
|
||||
|
||||
import java.io.Writer;
|
||||
|
||||
public class TestCommand extends Command {
|
||||
public TestCommand(LaunchServer server) {
|
||||
super(server);
|
||||
}
|
||||
|
||||
NettyServerSocketHandler handler;
|
||||
private NettyServerSocketHandler handler = null;
|
||||
|
||||
@Override
|
||||
public String getArgsDescription() {
|
||||
|
@ -33,14 +29,6 @@ public void invoke(String... args) throws Exception {
|
|||
handler = new NettyServerSocketHandler(server);
|
||||
if (args[0].equals("start")) {
|
||||
CommonHelper.newThread("Netty Server", true, handler).start();
|
||||
}
|
||||
if (args[0].equals("profile")) {
|
||||
ClientProfile profile = new ClientProfile("1.7.10", "asset1.7.10", 0, "Test1.7.10", "localhost", 25565, true, false, "net.minecraft.launchwrapper.Launch");
|
||||
try (Writer writer = IOHelper.newWriter(server.dir.resolve("profiles").resolve("Test.cfg"))) {
|
||||
LaunchServer.gson.toJson(profile, writer);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
if (args[0].equals("stop")) {
|
||||
handler.close();
|
||||
|
|
|
@ -16,7 +16,6 @@ public StdCommandHandler(LaunchServer server, boolean readCommands) {
|
|||
|
||||
@Override
|
||||
public void bell() {
|
||||
// Do nothing, unsupported
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
package ru.gravit.launchserver.manangers;
|
||||
|
||||
import ru.gravit.launcher.managers.ModulesConfigManager;
|
||||
import ru.gravit.launcher.managers.SimpleModulesConfigManager;
|
||||
import ru.gravit.launcher.managers.SimpleModuleManager;
|
||||
import ru.gravit.launchserver.LaunchServer;
|
||||
import ru.gravit.launchserver.modules.CoreModule;
|
||||
|
@ -11,11 +11,11 @@
|
|||
import java.util.ArrayList;
|
||||
|
||||
public class ModulesManager extends SimpleModuleManager {
|
||||
public ModulesConfigManager configManager;
|
||||
public SimpleModulesConfigManager configManager;
|
||||
|
||||
public ModulesManager(LaunchServer lsrv) {
|
||||
modules = new ArrayList<>(1);
|
||||
configManager = new ModulesConfigManager(lsrv.dir.resolve("config"));
|
||||
configManager = new SimpleModulesConfigManager(lsrv.dir.resolve("config"));
|
||||
classloader = new PublicURLClassLoader(new URL[0], ClassLoader.getSystemClassLoader());
|
||||
context = new LaunchServerModuleContext(lsrv, classloader, configManager);
|
||||
registerCoreModule();
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
package ru.gravit.launchserver.manangers.hook;
|
||||
|
||||
import ru.gravit.launcher.AutogenConfig;
|
||||
import ru.gravit.launcher.modules.TestClientModule;
|
||||
import ru.gravit.launchserver.binary.BuildContext;
|
||||
import ru.gravit.launchserver.binary.JAConfigurator;
|
||||
import ru.gravit.launchserver.binary.tasks.MainBuildTask;
|
||||
|
@ -41,7 +40,6 @@ public BuildHookManager() {
|
|||
registerIgnoredClass("META-INF/DEPENDENCIES");
|
||||
registerIgnoredClass("META-INF/LICENSE");
|
||||
registerIgnoredClass("META-INF/NOTICE");
|
||||
registerClientModuleClass(TestClientModule.class.getName());
|
||||
}
|
||||
|
||||
public void autoRegisterIgnoredClass(String clazz) {
|
||||
|
|
|
@ -1,18 +1,18 @@
|
|||
package ru.gravit.launchserver.modules;
|
||||
|
||||
import ru.gravit.launcher.managers.ModulesConfigManager;
|
||||
import ru.gravit.launcher.managers.SimpleModulesConfigManager;
|
||||
import ru.gravit.launcher.modules.ModuleContext;
|
||||
import ru.gravit.launcher.modules.ModulesConfigManagerInterface;
|
||||
import ru.gravit.launcher.modules.ModulesManagerInterface;
|
||||
import ru.gravit.launcher.modules.ModulesConfigManager;
|
||||
import ru.gravit.launcher.modules.ModulesManager;
|
||||
import ru.gravit.launchserver.LaunchServer;
|
||||
import ru.gravit.utils.PublicURLClassLoader;
|
||||
|
||||
public class LaunchServerModuleContext implements ModuleContext {
|
||||
public final LaunchServer launchServer;
|
||||
public final PublicURLClassLoader classloader;
|
||||
public final ModulesConfigManager modulesConfigManager;
|
||||
public final SimpleModulesConfigManager modulesConfigManager;
|
||||
|
||||
public LaunchServerModuleContext(LaunchServer server, PublicURLClassLoader classloader, ModulesConfigManager modulesConfigManager) {
|
||||
public LaunchServerModuleContext(LaunchServer server, PublicURLClassLoader classloader, SimpleModulesConfigManager modulesConfigManager) {
|
||||
launchServer = server;
|
||||
this.classloader = classloader;
|
||||
this.modulesConfigManager = modulesConfigManager;
|
||||
|
@ -24,12 +24,12 @@ public Type getType() {
|
|||
}
|
||||
|
||||
@Override
|
||||
public ModulesManagerInterface getModulesManager() {
|
||||
public ModulesManager getModulesManager() {
|
||||
return launchServer.modulesManager;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ModulesConfigManagerInterface getModulesConfigManager() {
|
||||
public ModulesConfigManager getModulesConfigManager() {
|
||||
return modulesConfigManager;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -59,7 +59,7 @@ private LauncherEngine() {
|
|||
public void start(String... args) throws Throwable {
|
||||
LogHelper.debug("%d", LauncherSettings.settingsMagic);
|
||||
Launcher.modulesManager = new ClientModuleManager(this);
|
||||
LauncherConfig.getAutogenConfig().initModules(); //INIT
|
||||
LauncherConfig.getAutogenConfig().initModules();
|
||||
Launcher.modulesManager.preInitModules();
|
||||
if (runtimeProvider == null) runtimeProvider = new JSRuntimeProvider();
|
||||
runtimeProvider.init(false);
|
||||
|
@ -67,10 +67,9 @@ public void start(String... args) throws Throwable {
|
|||
if (started.getAndSet(true))
|
||||
throw new IllegalStateException("Launcher has been already started");
|
||||
Launcher.modulesManager.initModules();
|
||||
// Load init.js script
|
||||
runtimeProvider.preLoad();
|
||||
FunctionalBridge.worker = new RequestWorker();
|
||||
CommonHelper.newThread("FX Task Worker", true, FunctionalBridge.worker).start();
|
||||
CommonHelper.newThread("Task Worker", true, FunctionalBridge.worker).start();
|
||||
FunctionalBridge.getHWID = CommonHelper.newThread("GetHWID Thread", true, FunctionalBridge::getHWID);
|
||||
FunctionalBridge.getHWID.start();
|
||||
LogHelper.debug("Dir: %s", DirBridge.dir);
|
||||
|
|
|
@ -3,8 +3,8 @@
|
|||
import ru.gravit.launcher.Launcher;
|
||||
import ru.gravit.launcher.LauncherEngine;
|
||||
import ru.gravit.launcher.modules.ModuleContext;
|
||||
import ru.gravit.launcher.modules.ModulesConfigManagerInterface;
|
||||
import ru.gravit.launcher.modules.ModulesManagerInterface;
|
||||
import ru.gravit.launcher.modules.ModulesConfigManager;
|
||||
import ru.gravit.launcher.modules.ModulesManager;
|
||||
|
||||
public class ClientModuleContext implements ModuleContext {
|
||||
public final LauncherEngine engine;
|
||||
|
@ -19,12 +19,12 @@ public Type getType() {
|
|||
}
|
||||
|
||||
@Override
|
||||
public ModulesManagerInterface getModulesManager() {
|
||||
public ModulesManager getModulesManager() {
|
||||
return Launcher.modulesManager;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ModulesConfigManagerInterface getModulesConfigManager() {
|
||||
public ModulesConfigManager getModulesConfigManager() {
|
||||
return null; // ClientModuleContext не поддерживает modulesConfigManager
|
||||
}
|
||||
}
|
||||
|
|
|
@ -13,12 +13,12 @@ public ClientModuleManager(LauncherEngine engine) {
|
|||
}
|
||||
|
||||
@Override
|
||||
public void loadModule(URL jarpath, boolean preload) {
|
||||
public void loadModule(URL jarpath, String classname) {
|
||||
throw new SecurityException("Custom JAR's load not allowed here");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void loadModule(URL jarpath, String classname, boolean preload) {
|
||||
public void loadModuleFull(URL jarpath) {
|
||||
throw new SecurityException("Custom JAR's load not allowed here");
|
||||
}
|
||||
}
|
||||
|
|
|
@ -147,9 +147,9 @@ private void setScriptBindings() {
|
|||
public void run(String[] args) throws ScriptException, NoSuchMethodException, IOException {
|
||||
loadScript(Launcher.INIT_SCRIPT_FILE);
|
||||
LogHelper.info("Invoking start() function");
|
||||
Invocable invoker = (Invocable) engine;
|
||||
Launcher.modulesManager.postInitModules();
|
||||
invoker.invokeFunction("start", (Object) args);
|
||||
((Invocable) engine).invokeFunction("start", (Object) args);
|
||||
Launcher.modulesManager.finishModules();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
package ru.gravit.launcher.server;
|
||||
|
||||
import ru.gravit.launcher.managers.ModulesConfigManager;
|
||||
import ru.gravit.launcher.managers.SimpleModulesConfigManager;
|
||||
import ru.gravit.launcher.managers.SimpleModuleManager;
|
||||
import ru.gravit.utils.PublicURLClassLoader;
|
||||
|
||||
|
@ -9,11 +9,11 @@
|
|||
import java.util.ArrayList;
|
||||
|
||||
public class ModulesManager extends SimpleModuleManager {
|
||||
public ModulesConfigManager modulesConfigManager;
|
||||
public SimpleModulesConfigManager modulesConfigManager;
|
||||
|
||||
public ModulesManager(ServerWrapper wrapper) {
|
||||
modules = new ArrayList<>();
|
||||
modulesConfigManager = new ModulesConfigManager(Paths.get("modules-config"));
|
||||
modulesConfigManager = new SimpleModulesConfigManager(Paths.get("modules-config"));
|
||||
classloader = new PublicURLClassLoader(new URL[0], ClassLoader.getSystemClassLoader());
|
||||
context = new ServerModuleContext(wrapper, classloader, modulesConfigManager);
|
||||
}
|
||||
|
|
|
@ -63,16 +63,16 @@ public static void premain(String agentArgument, Instrumentation instrumentation
|
|||
Object[] args = {agentArgument, instrumentation};
|
||||
mainMethod.invoke(args);
|
||||
} catch (Throwable e) {
|
||||
e.printStackTrace();
|
||||
LogHelper.error(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void loadLibraries(Path dir) {
|
||||
try {
|
||||
Files.walkFileTree(dir, Collections.singleton(FileVisitOption.FOLLOW_LINKS), Integer.MAX_VALUE, new StarterVisitor());
|
||||
IOHelper.walk(dir, new StarterVisitor(), true);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace(System.err);
|
||||
LogHelper.error(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,18 +1,18 @@
|
|||
package ru.gravit.launcher.server;
|
||||
|
||||
import ru.gravit.launcher.Launcher;
|
||||
import ru.gravit.launcher.managers.ModulesConfigManager;
|
||||
import ru.gravit.launcher.managers.SimpleModulesConfigManager;
|
||||
import ru.gravit.launcher.modules.ModuleContext;
|
||||
import ru.gravit.launcher.modules.ModulesConfigManagerInterface;
|
||||
import ru.gravit.launcher.modules.ModulesManagerInterface;
|
||||
import ru.gravit.launcher.modules.ModulesConfigManager;
|
||||
import ru.gravit.launcher.modules.ModulesManager;
|
||||
import ru.gravit.utils.PublicURLClassLoader;
|
||||
|
||||
public class ServerModuleContext implements ModuleContext {
|
||||
public final PublicURLClassLoader classLoader;
|
||||
public final ServerWrapper wrapper;
|
||||
public final ModulesConfigManager modulesConfigManager;
|
||||
public final SimpleModulesConfigManager modulesConfigManager;
|
||||
|
||||
public ServerModuleContext(ServerWrapper wrapper, PublicURLClassLoader classLoader, ModulesConfigManager modulesConfigManager) {
|
||||
public ServerModuleContext(ServerWrapper wrapper, PublicURLClassLoader classLoader, SimpleModulesConfigManager modulesConfigManager) {
|
||||
this.classLoader = classLoader;
|
||||
this.wrapper = wrapper;
|
||||
this.modulesConfigManager = modulesConfigManager;
|
||||
|
@ -24,12 +24,12 @@ public Type getType() {
|
|||
}
|
||||
|
||||
@Override
|
||||
public ModulesManagerInterface getModulesManager() {
|
||||
public ModulesManager getModulesManager() {
|
||||
return Launcher.modulesManager;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ModulesConfigManagerInterface getModulesConfigManager() {
|
||||
public ModulesConfigManager getModulesConfigManager() {
|
||||
return modulesConfigManager;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -77,6 +77,8 @@ public static boolean loopAuth(ServerWrapper wrapper, int count, int sleeptime)
|
|||
try {
|
||||
Thread.sleep(sleeptime);
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
LogHelper.error(e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
@ -179,10 +181,6 @@ private static void generateConfigIfNotExists() throws IOException {
|
|||
newConfig.reconnectCount = 10;
|
||||
newConfig.reconnectSleep = 1000;
|
||||
newConfig.env = LauncherConfig.LauncherEnvironment.STD;
|
||||
//try(Reader reader = IOHelper.newReader(IOHelper.getResourceURL("ru/gravit/launcher/server/ServerWrapper.cfg")))
|
||||
//{
|
||||
// newConfig = gson.fromJson(reader,Config.class);
|
||||
//}
|
||||
|
||||
LogHelper.warning("Title is not set. Please show ServerWrapper.cfg");
|
||||
|
||||
|
|
|
@ -26,4 +26,6 @@
|
|||
options.encoding = "UTF-8"
|
||||
options.annotationProcessorPath = configurations.aptOnly
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
defaultTasks 'build'
|
||||
|
|
|
@ -2,12 +2,11 @@
|
|||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import ru.gravit.launcher.modules.ModulesManagerInterface;
|
||||
import ru.gravit.launcher.modules.ModulesManager;
|
||||
import ru.gravit.launcher.profiles.ClientProfile;
|
||||
import ru.gravit.launcher.serialize.HInput;
|
||||
import ru.gravit.utils.Version;
|
||||
import ru.gravit.utils.helper.IOHelper;
|
||||
import ru.gravit.utils.helper.JVMHelper;
|
||||
import ru.gravit.utils.helper.LogHelper;
|
||||
import ru.gravit.utils.helper.SecurityHelper;
|
||||
|
||||
|
@ -37,7 +36,7 @@ public final class Launcher {
|
|||
|
||||
private static final AtomicReference<LauncherConfig> CONFIG = new AtomicReference<>();
|
||||
@LauncherAPI
|
||||
public static ModulesManagerInterface modulesManager = null;
|
||||
public static ModulesManager modulesManager = null;
|
||||
@LauncherAPI
|
||||
public static final int PROTOCOL_MAGIC_LEGACY = 0x724724_00 + 24;
|
||||
@LauncherAPI
|
||||
|
@ -126,12 +125,6 @@ public static Version getVersion() {
|
|||
return new Version(MAJOR, MINOR, PATCH, BUILD, RELEASE);
|
||||
}
|
||||
|
||||
public static final boolean useAvanguard = true;
|
||||
|
||||
public static boolean isUsingAvanguard() {
|
||||
return JVMHelper.OS_TYPE == JVMHelper.OS.MUSTDIE && useAvanguard;
|
||||
}
|
||||
|
||||
public static void applyLauncherEnv(LauncherConfig.LauncherEnvironment env)
|
||||
{
|
||||
switch (env)
|
||||
|
|
|
@ -20,10 +20,6 @@ public boolean isAgentStarted() {
|
|||
return isAgentStarted;
|
||||
}
|
||||
|
||||
public static long getObjSize(Object obj) {
|
||||
return inst.getObjectSize(obj);
|
||||
}
|
||||
|
||||
public static void premain(String agentArgument, Instrumentation instrumentation) {
|
||||
System.out.println("Launcher Agent");
|
||||
inst = instrumentation;
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
import ru.gravit.launcher.modules.Module;
|
||||
import ru.gravit.launcher.modules.ModuleContext;
|
||||
import ru.gravit.launcher.modules.ModulesManagerInterface;
|
||||
import ru.gravit.launcher.modules.ModulesManager;
|
||||
import ru.gravit.utils.PublicURLClassLoader;
|
||||
import ru.gravit.utils.helper.IOHelper;
|
||||
import ru.gravit.utils.helper.LogHelper;
|
||||
|
@ -14,26 +14,20 @@
|
|||
import java.nio.file.attribute.BasicFileAttributes;
|
||||
import java.util.ArrayList;
|
||||
import java.util.jar.JarFile;
|
||||
import java.util.jar.Manifest;
|
||||
|
||||
public class SimpleModuleManager implements ModulesManagerInterface, AutoCloseable {
|
||||
public class SimpleModuleManager implements ModulesManager {
|
||||
protected final class ModulesVisitor extends SimpleFileVisitor<Path> {
|
||||
private ModulesVisitor() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
|
||||
try {
|
||||
JarFile f = new JarFile(file.toString());
|
||||
Manifest m = f.getManifest();
|
||||
String mainclass = m.getMainAttributes().getValue("Main-Class");
|
||||
loadModule(file.toUri().toURL(), mainclass, true);
|
||||
f.close();
|
||||
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
// Return result
|
||||
if (file.toFile().getName().endsWith(".jar"))
|
||||
try (JarFile f = new JarFile(file.toFile())) {
|
||||
loadModule(file.toUri().toURL(), f.getManifest().getMainAttributes().getValue("Main-Class"));
|
||||
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) {
|
||||
LogHelper.error(e);
|
||||
}
|
||||
return super.visitFile(file, attrs);
|
||||
}
|
||||
}
|
||||
|
@ -72,7 +66,6 @@ public void close() {
|
|||
}
|
||||
|
||||
@Override
|
||||
|
||||
public void initModules() {
|
||||
for (Module m : modules) {
|
||||
m.init(context);
|
||||
|
@ -81,53 +74,35 @@ public void initModules() {
|
|||
}
|
||||
|
||||
@Override
|
||||
|
||||
public void load(Module module) {
|
||||
modules.add(module);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
public void load(Module module, boolean preload) {
|
||||
load(module);
|
||||
if (!preload) module.init(context);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
|
||||
public void loadModule(URL jarpath, boolean preload) throws ClassNotFoundException, IllegalAccessException, InstantiationException, URISyntaxException, IOException {
|
||||
JarFile f = new JarFile(Paths.get(jarpath.toURI()).toString());
|
||||
Manifest m = f.getManifest();
|
||||
String mainclass = m.getMainAttributes().getValue("Main-Class");
|
||||
loadModule(jarpath, mainclass, preload);
|
||||
f.close();
|
||||
}
|
||||
|
||||
public void loadModuleFull(URL jarpath) throws ClassNotFoundException, IllegalAccessException, InstantiationException, URISyntaxException, IOException {
|
||||
JarFile f = new JarFile(Paths.get(jarpath.toURI()).toString());
|
||||
Manifest m = f.getManifest();
|
||||
String mainclass = m.getMainAttributes().getValue("Main-Class");
|
||||
classloader.addURL(jarpath);
|
||||
Class<?> moduleclass = Class.forName(mainclass, true, classloader);
|
||||
Module module = (Module) moduleclass.newInstance();
|
||||
modules.add(module);
|
||||
module.preInit(context);
|
||||
module.init(context);
|
||||
module.postInit(context);
|
||||
LogHelper.info("Module %s version: %s loaded", module.getName(), module.getVersion());
|
||||
f.close();
|
||||
try (JarFile f = new JarFile(Paths.get(jarpath.toURI()).toFile())) {
|
||||
classloader.addURL(jarpath);
|
||||
Module module = (Module) Class.forName(f.getManifest().getMainAttributes().getValue("Main-Class"), true, classloader).newInstance();
|
||||
modules.add(module);
|
||||
module.preInit(context);
|
||||
module.init(context);
|
||||
module.postInit(context);
|
||||
module.finish(context);
|
||||
LogHelper.info("Module %s version: %s loaded", module.getName(), module.getVersion());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@Override
|
||||
public void loadModule(URL jarpath) throws Exception {
|
||||
try (JarFile f = new JarFile(Paths.get(jarpath.toURI()).toFile())) {
|
||||
loadModule(jarpath, f.getManifest().getMainAttributes().getValue("Main-Class"));
|
||||
}
|
||||
}
|
||||
|
||||
public void loadModule(URL jarpath, String classname, boolean preload) throws ClassNotFoundException, IllegalAccessException, InstantiationException {
|
||||
@Override
|
||||
public void loadModule(URL jarpath, String classname) throws ClassNotFoundException, IllegalAccessException, InstantiationException {
|
||||
classloader.addURL(jarpath);
|
||||
Class<?> moduleclass = Class.forName(classname, true, classloader);
|
||||
Module module = (Module) moduleclass.newInstance();
|
||||
Module module = (Module) Class.forName(classname, true, classloader).newInstance();
|
||||
modules.add(module);
|
||||
module.preInit(context);
|
||||
if (!preload) module.init(context);
|
||||
LogHelper.info("Module %s version: %s loaded", module.getName(), module.getVersion());
|
||||
}
|
||||
|
||||
|
@ -141,7 +116,6 @@ public void postInitModules() {
|
|||
|
||||
|
||||
@Override
|
||||
|
||||
public void preInitModules() {
|
||||
for (Module m : modules) {
|
||||
m.preInit(context);
|
||||
|
@ -150,7 +124,6 @@ public void preInitModules() {
|
|||
}
|
||||
|
||||
@Override
|
||||
|
||||
public void printModules() {
|
||||
for (Module m : modules)
|
||||
LogHelper.info("Module %s version: %s", m.getName(), m.getVersion());
|
||||
|
@ -158,9 +131,16 @@ public void printModules() {
|
|||
}
|
||||
|
||||
@Override
|
||||
|
||||
public void registerModule(Module module, boolean preload) {
|
||||
load(module, preload);
|
||||
public void registerModule(Module module) {
|
||||
load(module);
|
||||
LogHelper.info("Module %s version: %s registered", module.getName(), module.getVersion());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void finishModules() {
|
||||
for (Module m : modules) {
|
||||
m.finish(context);
|
||||
LogHelper.info("Module %s version: %s finished initialization", m.getName(), m.getVersion());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
package ru.gravit.launcher.managers;
|
||||
|
||||
import ru.gravit.launcher.modules.ModulesConfigManagerInterface;
|
||||
import ru.gravit.launcher.modules.ModulesConfigManager;
|
||||
import ru.gravit.utils.helper.IOHelper;
|
||||
import ru.gravit.utils.helper.LogHelper;
|
||||
|
||||
|
@ -8,10 +8,10 @@
|
|||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
|
||||
public class ModulesConfigManager implements ModulesConfigManagerInterface {
|
||||
public class SimpleModulesConfigManager implements ModulesConfigManager {
|
||||
public Path configDir;
|
||||
|
||||
public ModulesConfigManager(Path configDir) {
|
||||
public SimpleModulesConfigManager(Path configDir) {
|
||||
this.configDir = configDir;
|
||||
}
|
||||
|
|
@ -15,4 +15,8 @@ public interface Module extends AutoCloseable {
|
|||
void postInit(ModuleContext context);
|
||||
|
||||
void preInit(ModuleContext context);
|
||||
|
||||
default void finish(ModuleContext context) {
|
||||
// NOP
|
||||
};
|
||||
}
|
||||
|
|
|
@ -7,7 +7,7 @@ enum Type {
|
|||
|
||||
Type getType();
|
||||
|
||||
ModulesManagerInterface getModulesManager();
|
||||
ModulesManager getModulesManager();
|
||||
|
||||
ModulesConfigManagerInterface getModulesConfigManager();
|
||||
ModulesConfigManager getModulesConfigManager();
|
||||
}
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
import java.nio.file.Path;
|
||||
|
||||
public interface ModulesConfigManagerInterface {
|
||||
public interface ModulesConfigManager {
|
||||
Path getModuleConfig(String moduleName);
|
||||
|
||||
Path getModuleConfigDir(String moduleName);
|
|
@ -0,0 +1,25 @@
|
|||
package ru.gravit.launcher.modules;
|
||||
|
||||
import java.net.URL;
|
||||
|
||||
public interface ModulesManager extends AutoCloseable {
|
||||
void initModules();
|
||||
|
||||
void load(Module module);
|
||||
|
||||
void loadModule(URL jarpath) throws Exception;
|
||||
|
||||
void loadModule(URL jarpath, String classname) throws Exception;
|
||||
|
||||
void postInitModules();
|
||||
|
||||
void preInitModules();
|
||||
|
||||
void finishModules();
|
||||
|
||||
void printModules();
|
||||
|
||||
void sort();
|
||||
|
||||
void registerModule(Module module);
|
||||
}
|
|
@ -1,25 +0,0 @@
|
|||
package ru.gravit.launcher.modules;
|
||||
|
||||
import java.net.URL;
|
||||
|
||||
public interface ModulesManagerInterface {
|
||||
void initModules();
|
||||
|
||||
void load(Module module);
|
||||
|
||||
void load(Module module, boolean preload);
|
||||
|
||||
void loadModule(URL jarpath, boolean preload) throws Exception;
|
||||
|
||||
void loadModule(URL jarpath, String classname, boolean preload) throws Exception;
|
||||
|
||||
void postInitModules();
|
||||
|
||||
void preInitModules();
|
||||
|
||||
void printModules();
|
||||
|
||||
void sort();
|
||||
|
||||
void registerModule(Module module, boolean preload);
|
||||
}
|
|
@ -1,42 +0,0 @@
|
|||
package ru.gravit.launcher.modules;
|
||||
|
||||
|
||||
import ru.gravit.launcher.Launcher;
|
||||
import ru.gravit.utils.Version;
|
||||
|
||||
public class TestClientModule implements Module {
|
||||
@Override
|
||||
public void close() {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "TestClientModule";
|
||||
}
|
||||
|
||||
@Override
|
||||
public Version getVersion() {
|
||||
return Launcher.getVersion();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getPriority() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void init(ModuleContext context) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void postInit(ModuleContext context) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void preInit(ModuleContext context) {
|
||||
|
||||
}
|
||||
}
|
|
@ -10,22 +10,6 @@
|
|||
import java.util.*;
|
||||
|
||||
public final class ClientProfile implements Comparable<ClientProfile> {
|
||||
public ClientProfile(String version, String assetIndex, int sortIndex, String title, String serverAddress, int serverPort, boolean updateFastCheck, boolean useWhitelist, String mainClass) {
|
||||
this.version = version;
|
||||
this.assetIndex = assetIndex;
|
||||
this.sortIndex = sortIndex;
|
||||
this.title = title;
|
||||
this.info = info;
|
||||
this.serverAddress = serverAddress;
|
||||
this.serverPort = serverPort;
|
||||
this.updateFastCheck = updateFastCheck;
|
||||
this.useWhitelist = useWhitelist;
|
||||
this.mainClass = mainClass;
|
||||
}
|
||||
|
||||
public ClientProfile() {
|
||||
}
|
||||
|
||||
@LauncherAPI
|
||||
public enum Version {
|
||||
MC147("1.4.7", 51),
|
||||
|
|
Loading…
Reference in a new issue