Выполнил ревью кода с правками. (#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:
Zaxar163 2019-01-18 01:30:55 +03:00 committed by Gravit
parent 25a0287f1d
commit f00c0489a7
29 changed files with 120 additions and 233 deletions

View file

@ -38,7 +38,6 @@
import java.io.IOException; import java.io.IOException;
import java.lang.ProcessBuilder.Redirect; import java.lang.ProcessBuilder.Redirect;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
import java.net.MalformedURLException;
import java.net.SocketAddress; import java.net.SocketAddress;
import java.nio.file.*; import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes; import java.nio.file.attribute.BasicFileAttributes;
@ -586,6 +585,7 @@ public void run() {
JVMHelper.RUNTIME.addShutdownHook(CommonHelper.newThread(null, false, this::close)); JVMHelper.RUNTIME.addShutdownHook(CommonHelper.newThread(null, false, this::close));
CommonHelper.newThread("Command Thread", true, commandHandler).start(); CommonHelper.newThread("Command Thread", true, commandHandler).start();
rebindServerSocket(); rebindServerSocket();
modulesManager.finishModules();
} }

View file

@ -4,7 +4,6 @@
import ru.gravit.utils.helper.LogHelper; import ru.gravit.utils.helper.LogHelper;
import java.io.IOException; import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement; import java.sql.PreparedStatement;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
@ -66,11 +65,8 @@ protected Entry fetchEntry(UUID uuid) throws IOException {
private Entry query(String sql, String value) throws IOException { private Entry query(String sql, String value) throws IOException {
try { try {
Connection c = mySQLHolder.getConnection(); PreparedStatement s = mySQLHolder.getConnection().prepareStatement(sql);
PreparedStatement s = c.prepareStatement(sql);
s.setString(1, value); s.setString(1, value);
// Execute query
s.setQueryTimeout(MySQLSourceConfig.TIMEOUT); s.setQueryTimeout(MySQLSourceConfig.TIMEOUT);
try (ResultSet set = s.executeQuery()) { try (ResultSet set = s.executeQuery()) {
return constructEntry(set); return constructEntry(set);
@ -83,13 +79,10 @@ private Entry query(String sql, String value) throws IOException {
@Override @Override
protected boolean updateAuth(UUID uuid, String username, String accessToken) throws IOException { protected boolean updateAuth(UUID uuid, String username, String accessToken) throws IOException {
try { try {
Connection c = mySQLHolder.getConnection(); PreparedStatement s = mySQLHolder.getConnection().prepareStatement(updateAuthSQL);
PreparedStatement s = c.prepareStatement(updateAuthSQL);
s.setString(1, username); // Username case s.setString(1, username); // Username case
s.setString(2, accessToken); s.setString(2, accessToken);
s.setString(3, uuid.toString()); s.setString(3, uuid.toString());
// Execute update
s.setQueryTimeout(MySQLSourceConfig.TIMEOUT); s.setQueryTimeout(MySQLSourceConfig.TIMEOUT);
return s.executeUpdate() > 0; return s.executeUpdate() > 0;
} catch (SQLException e) { } catch (SQLException e) {
@ -100,12 +93,9 @@ protected boolean updateAuth(UUID uuid, String username, String accessToken) thr
@Override @Override
protected boolean updateServerID(UUID uuid, String serverID) throws IOException { protected boolean updateServerID(UUID uuid, String serverID) throws IOException {
try { try {
Connection c = mySQLHolder.getConnection(); PreparedStatement s = mySQLHolder.getConnection().prepareStatement(updateServerIDSQL);
PreparedStatement s = c.prepareStatement(updateServerIDSQL);
s.setString(1, serverID); s.setString(1, serverID);
s.setString(2, uuid.toString()); s.setString(2, uuid.toString());
// Execute update
s.setQueryTimeout(MySQLSourceConfig.TIMEOUT); s.setQueryTimeout(MySQLSourceConfig.TIMEOUT);
return s.executeUpdate() > 0; return s.executeUpdate() > 0;
} catch (SQLException e) { } catch (SQLException e) {

View file

@ -226,6 +226,6 @@ public List<HWID> getHwid(String username) {
@Override @Override
public void close() { public void close() {
// Do nothing mySQLHolder.close();
} }
} }

View file

@ -7,7 +7,6 @@
import ru.gravit.utils.helper.LogHelper; import ru.gravit.utils.helper.LogHelper;
import ru.gravit.utils.helper.SecurityHelper; import ru.gravit.utils.helper.SecurityHelper;
import java.sql.Connection;
import java.sql.PreparedStatement; import java.sql.PreparedStatement;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
@ -28,8 +27,7 @@ public void init() {
@Override @Override
public AuthProviderResult auth(String login, String password, String ip) throws SQLException, AuthException { public AuthProviderResult auth(String login, String password, String ip) throws SQLException, AuthException {
Connection c = mySQLHolder.getConnection(); PreparedStatement s = mySQLHolder.getConnection().prepareStatement(query);
PreparedStatement s = c.prepareStatement(query);
String[] replaceParams = {"login", login, "password", password, "ip", ip}; String[] replaceParams = {"login", login, "password", password, "ip", ip};
for (int i = 0; i < queryParams.length; i++) for (int i = 0; i < queryParams.length; i++)
s.setString(i + 1, CommonHelper.replace(queryParams[i], replaceParams)); s.setString(i + 1, CommonHelper.replace(queryParams[i], replaceParams));
@ -43,6 +41,6 @@ public AuthProviderResult auth(String login, String password, String ip) throws
@Override @Override
public void close() { public void close() {
// Do nothing mySQLHolder.close();
} }
} }

View file

@ -1,20 +1,16 @@
package ru.gravit.launchserver.command.basic; package ru.gravit.launchserver.command.basic;
import ru.gravit.launcher.profiles.ClientProfile;
import ru.gravit.launchserver.LaunchServer; import ru.gravit.launchserver.LaunchServer;
import ru.gravit.launchserver.command.Command; import ru.gravit.launchserver.command.Command;
import ru.gravit.launchserver.socket.NettyServerSocketHandler; import ru.gravit.launchserver.socket.NettyServerSocketHandler;
import ru.gravit.utils.helper.CommonHelper; import ru.gravit.utils.helper.CommonHelper;
import ru.gravit.utils.helper.IOHelper;
import java.io.Writer;
public class TestCommand extends Command { public class TestCommand extends Command {
public TestCommand(LaunchServer server) { public TestCommand(LaunchServer server) {
super(server); super(server);
} }
NettyServerSocketHandler handler; private NettyServerSocketHandler handler = null;
@Override @Override
public String getArgsDescription() { public String getArgsDescription() {
@ -33,14 +29,6 @@ public void invoke(String... args) throws Exception {
handler = new NettyServerSocketHandler(server); handler = new NettyServerSocketHandler(server);
if (args[0].equals("start")) { if (args[0].equals("start")) {
CommonHelper.newThread("Netty Server", true, handler).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")) { if (args[0].equals("stop")) {
handler.close(); handler.close();

View file

@ -16,7 +16,6 @@ public StdCommandHandler(LaunchServer server, boolean readCommands) {
@Override @Override
public void bell() { public void bell() {
// Do nothing, unsupported
} }
@Override @Override

View file

@ -1,6 +1,6 @@
package ru.gravit.launchserver.manangers; 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.launcher.managers.SimpleModuleManager;
import ru.gravit.launchserver.LaunchServer; import ru.gravit.launchserver.LaunchServer;
import ru.gravit.launchserver.modules.CoreModule; import ru.gravit.launchserver.modules.CoreModule;
@ -11,11 +11,11 @@
import java.util.ArrayList; import java.util.ArrayList;
public class ModulesManager extends SimpleModuleManager { public class ModulesManager extends SimpleModuleManager {
public ModulesConfigManager configManager; public SimpleModulesConfigManager configManager;
public ModulesManager(LaunchServer lsrv) { public ModulesManager(LaunchServer lsrv) {
modules = new ArrayList<>(1); 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()); classloader = new PublicURLClassLoader(new URL[0], ClassLoader.getSystemClassLoader());
context = new LaunchServerModuleContext(lsrv, classloader, configManager); context = new LaunchServerModuleContext(lsrv, classloader, configManager);
registerCoreModule(); registerCoreModule();

View file

@ -1,7 +1,6 @@
package ru.gravit.launchserver.manangers.hook; package ru.gravit.launchserver.manangers.hook;
import ru.gravit.launcher.AutogenConfig; import ru.gravit.launcher.AutogenConfig;
import ru.gravit.launcher.modules.TestClientModule;
import ru.gravit.launchserver.binary.BuildContext; import ru.gravit.launchserver.binary.BuildContext;
import ru.gravit.launchserver.binary.JAConfigurator; import ru.gravit.launchserver.binary.JAConfigurator;
import ru.gravit.launchserver.binary.tasks.MainBuildTask; import ru.gravit.launchserver.binary.tasks.MainBuildTask;
@ -41,7 +40,6 @@ public BuildHookManager() {
registerIgnoredClass("META-INF/DEPENDENCIES"); registerIgnoredClass("META-INF/DEPENDENCIES");
registerIgnoredClass("META-INF/LICENSE"); registerIgnoredClass("META-INF/LICENSE");
registerIgnoredClass("META-INF/NOTICE"); registerIgnoredClass("META-INF/NOTICE");
registerClientModuleClass(TestClientModule.class.getName());
} }
public void autoRegisterIgnoredClass(String clazz) { public void autoRegisterIgnoredClass(String clazz) {

View file

@ -1,18 +1,18 @@
package ru.gravit.launchserver.modules; 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.ModuleContext;
import ru.gravit.launcher.modules.ModulesConfigManagerInterface; import ru.gravit.launcher.modules.ModulesConfigManager;
import ru.gravit.launcher.modules.ModulesManagerInterface; import ru.gravit.launcher.modules.ModulesManager;
import ru.gravit.launchserver.LaunchServer; import ru.gravit.launchserver.LaunchServer;
import ru.gravit.utils.PublicURLClassLoader; import ru.gravit.utils.PublicURLClassLoader;
public class LaunchServerModuleContext implements ModuleContext { public class LaunchServerModuleContext implements ModuleContext {
public final LaunchServer launchServer; public final LaunchServer launchServer;
public final PublicURLClassLoader classloader; 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; launchServer = server;
this.classloader = classloader; this.classloader = classloader;
this.modulesConfigManager = modulesConfigManager; this.modulesConfigManager = modulesConfigManager;
@ -24,12 +24,12 @@ public Type getType() {
} }
@Override @Override
public ModulesManagerInterface getModulesManager() { public ModulesManager getModulesManager() {
return launchServer.modulesManager; return launchServer.modulesManager;
} }
@Override @Override
public ModulesConfigManagerInterface getModulesConfigManager() { public ModulesConfigManager getModulesConfigManager() {
return modulesConfigManager; return modulesConfigManager;
} }
} }

View file

@ -59,7 +59,7 @@ private LauncherEngine() {
public void start(String... args) throws Throwable { public void start(String... args) throws Throwable {
LogHelper.debug("%d", LauncherSettings.settingsMagic); LogHelper.debug("%d", LauncherSettings.settingsMagic);
Launcher.modulesManager = new ClientModuleManager(this); Launcher.modulesManager = new ClientModuleManager(this);
LauncherConfig.getAutogenConfig().initModules(); //INIT LauncherConfig.getAutogenConfig().initModules();
Launcher.modulesManager.preInitModules(); Launcher.modulesManager.preInitModules();
if (runtimeProvider == null) runtimeProvider = new JSRuntimeProvider(); if (runtimeProvider == null) runtimeProvider = new JSRuntimeProvider();
runtimeProvider.init(false); runtimeProvider.init(false);
@ -67,10 +67,9 @@ public void start(String... args) throws Throwable {
if (started.getAndSet(true)) if (started.getAndSet(true))
throw new IllegalStateException("Launcher has been already started"); throw new IllegalStateException("Launcher has been already started");
Launcher.modulesManager.initModules(); Launcher.modulesManager.initModules();
// Load init.js script
runtimeProvider.preLoad(); runtimeProvider.preLoad();
FunctionalBridge.worker = new RequestWorker(); 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 = CommonHelper.newThread("GetHWID Thread", true, FunctionalBridge::getHWID);
FunctionalBridge.getHWID.start(); FunctionalBridge.getHWID.start();
LogHelper.debug("Dir: %s", DirBridge.dir); LogHelper.debug("Dir: %s", DirBridge.dir);

View file

@ -3,8 +3,8 @@
import ru.gravit.launcher.Launcher; import ru.gravit.launcher.Launcher;
import ru.gravit.launcher.LauncherEngine; import ru.gravit.launcher.LauncherEngine;
import ru.gravit.launcher.modules.ModuleContext; import ru.gravit.launcher.modules.ModuleContext;
import ru.gravit.launcher.modules.ModulesConfigManagerInterface; import ru.gravit.launcher.modules.ModulesConfigManager;
import ru.gravit.launcher.modules.ModulesManagerInterface; import ru.gravit.launcher.modules.ModulesManager;
public class ClientModuleContext implements ModuleContext { public class ClientModuleContext implements ModuleContext {
public final LauncherEngine engine; public final LauncherEngine engine;
@ -19,12 +19,12 @@ public Type getType() {
} }
@Override @Override
public ModulesManagerInterface getModulesManager() { public ModulesManager getModulesManager() {
return Launcher.modulesManager; return Launcher.modulesManager;
} }
@Override @Override
public ModulesConfigManagerInterface getModulesConfigManager() { public ModulesConfigManager getModulesConfigManager() {
return null; // ClientModuleContext не поддерживает modulesConfigManager return null; // ClientModuleContext не поддерживает modulesConfigManager
} }
} }

View file

@ -13,12 +13,12 @@ public ClientModuleManager(LauncherEngine engine) {
} }
@Override @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"); throw new SecurityException("Custom JAR's load not allowed here");
} }
@Override @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"); throw new SecurityException("Custom JAR's load not allowed here");
} }
} }

View file

@ -147,9 +147,9 @@ private void setScriptBindings() {
public void run(String[] args) throws ScriptException, NoSuchMethodException, IOException { public void run(String[] args) throws ScriptException, NoSuchMethodException, IOException {
loadScript(Launcher.INIT_SCRIPT_FILE); loadScript(Launcher.INIT_SCRIPT_FILE);
LogHelper.info("Invoking start() function"); LogHelper.info("Invoking start() function");
Invocable invoker = (Invocable) engine;
Launcher.modulesManager.postInitModules(); Launcher.modulesManager.postInitModules();
invoker.invokeFunction("start", (Object) args); ((Invocable) engine).invokeFunction("start", (Object) args);
Launcher.modulesManager.finishModules();
} }
@Override @Override

View file

@ -1,6 +1,6 @@
package ru.gravit.launcher.server; 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.launcher.managers.SimpleModuleManager;
import ru.gravit.utils.PublicURLClassLoader; import ru.gravit.utils.PublicURLClassLoader;
@ -9,11 +9,11 @@
import java.util.ArrayList; import java.util.ArrayList;
public class ModulesManager extends SimpleModuleManager { public class ModulesManager extends SimpleModuleManager {
public ModulesConfigManager modulesConfigManager; public SimpleModulesConfigManager modulesConfigManager;
public ModulesManager(ServerWrapper wrapper) { public ModulesManager(ServerWrapper wrapper) {
modules = new ArrayList<>(); 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()); classloader = new PublicURLClassLoader(new URL[0], ClassLoader.getSystemClassLoader());
context = new ServerModuleContext(wrapper, classloader, modulesConfigManager); context = new ServerModuleContext(wrapper, classloader, modulesConfigManager);
} }

View file

@ -63,16 +63,16 @@ public static void premain(String agentArgument, Instrumentation instrumentation
Object[] args = {agentArgument, instrumentation}; Object[] args = {agentArgument, instrumentation};
mainMethod.invoke(args); mainMethod.invoke(args);
} catch (Throwable e) { } catch (Throwable e) {
e.printStackTrace(); LogHelper.error(e);
} }
} }
} }
public static void loadLibraries(Path dir) { public static void loadLibraries(Path dir) {
try { try {
Files.walkFileTree(dir, Collections.singleton(FileVisitOption.FOLLOW_LINKS), Integer.MAX_VALUE, new StarterVisitor()); IOHelper.walk(dir, new StarterVisitor(), true);
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(System.err); LogHelper.error(e);
} }
} }
} }

View file

@ -1,18 +1,18 @@
package ru.gravit.launcher.server; package ru.gravit.launcher.server;
import ru.gravit.launcher.Launcher; 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.ModuleContext;
import ru.gravit.launcher.modules.ModulesConfigManagerInterface; import ru.gravit.launcher.modules.ModulesConfigManager;
import ru.gravit.launcher.modules.ModulesManagerInterface; import ru.gravit.launcher.modules.ModulesManager;
import ru.gravit.utils.PublicURLClassLoader; import ru.gravit.utils.PublicURLClassLoader;
public class ServerModuleContext implements ModuleContext { public class ServerModuleContext implements ModuleContext {
public final PublicURLClassLoader classLoader; public final PublicURLClassLoader classLoader;
public final ServerWrapper wrapper; 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.classLoader = classLoader;
this.wrapper = wrapper; this.wrapper = wrapper;
this.modulesConfigManager = modulesConfigManager; this.modulesConfigManager = modulesConfigManager;
@ -24,12 +24,12 @@ public Type getType() {
} }
@Override @Override
public ModulesManagerInterface getModulesManager() { public ModulesManager getModulesManager() {
return Launcher.modulesManager; return Launcher.modulesManager;
} }
@Override @Override
public ModulesConfigManagerInterface getModulesConfigManager() { public ModulesConfigManager getModulesConfigManager() {
return modulesConfigManager; return modulesConfigManager;
} }
} }

View file

@ -77,6 +77,8 @@ public static boolean loopAuth(ServerWrapper wrapper, int count, int sleeptime)
try { try {
Thread.sleep(sleeptime); Thread.sleep(sleeptime);
} catch (InterruptedException e) { } catch (InterruptedException e) {
Thread.currentThread().interrupt();
LogHelper.error(e);
return false; return false;
} }
} }
@ -179,10 +181,6 @@ private static void generateConfigIfNotExists() throws IOException {
newConfig.reconnectCount = 10; newConfig.reconnectCount = 10;
newConfig.reconnectSleep = 1000; newConfig.reconnectSleep = 1000;
newConfig.env = LauncherConfig.LauncherEnvironment.STD; 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"); LogHelper.warning("Title is not set. Please show ServerWrapper.cfg");

View file

@ -27,3 +27,5 @@
options.annotationProcessorPath = configurations.aptOnly options.annotationProcessorPath = configurations.aptOnly
} }
} }
defaultTasks 'build'

View file

@ -2,12 +2,11 @@
import com.google.gson.Gson; import com.google.gson.Gson;
import com.google.gson.GsonBuilder; 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.profiles.ClientProfile;
import ru.gravit.launcher.serialize.HInput; import ru.gravit.launcher.serialize.HInput;
import ru.gravit.utils.Version; import ru.gravit.utils.Version;
import ru.gravit.utils.helper.IOHelper; import ru.gravit.utils.helper.IOHelper;
import ru.gravit.utils.helper.JVMHelper;
import ru.gravit.utils.helper.LogHelper; import ru.gravit.utils.helper.LogHelper;
import ru.gravit.utils.helper.SecurityHelper; import ru.gravit.utils.helper.SecurityHelper;
@ -37,7 +36,7 @@ public final class Launcher {
private static final AtomicReference<LauncherConfig> CONFIG = new AtomicReference<>(); private static final AtomicReference<LauncherConfig> CONFIG = new AtomicReference<>();
@LauncherAPI @LauncherAPI
public static ModulesManagerInterface modulesManager = null; public static ModulesManager modulesManager = null;
@LauncherAPI @LauncherAPI
public static final int PROTOCOL_MAGIC_LEGACY = 0x724724_00 + 24; public static final int PROTOCOL_MAGIC_LEGACY = 0x724724_00 + 24;
@LauncherAPI @LauncherAPI
@ -126,12 +125,6 @@ public static Version getVersion() {
return new Version(MAJOR, MINOR, PATCH, BUILD, RELEASE); 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) public static void applyLauncherEnv(LauncherConfig.LauncherEnvironment env)
{ {
switch (env) switch (env)

View file

@ -20,10 +20,6 @@ public boolean isAgentStarted() {
return isAgentStarted; return isAgentStarted;
} }
public static long getObjSize(Object obj) {
return inst.getObjectSize(obj);
}
public static void premain(String agentArgument, Instrumentation instrumentation) { public static void premain(String agentArgument, Instrumentation instrumentation) {
System.out.println("Launcher Agent"); System.out.println("Launcher Agent");
inst = instrumentation; inst = instrumentation;

View file

@ -2,7 +2,7 @@
import ru.gravit.launcher.modules.Module; import ru.gravit.launcher.modules.Module;
import ru.gravit.launcher.modules.ModuleContext; 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.PublicURLClassLoader;
import ru.gravit.utils.helper.IOHelper; import ru.gravit.utils.helper.IOHelper;
import ru.gravit.utils.helper.LogHelper; import ru.gravit.utils.helper.LogHelper;
@ -14,26 +14,20 @@
import java.nio.file.attribute.BasicFileAttributes; import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.jar.JarFile; 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> { protected final class ModulesVisitor extends SimpleFileVisitor<Path> {
private ModulesVisitor() { private ModulesVisitor() {
} }
@Override @Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
try { if (file.toFile().getName().endsWith(".jar"))
JarFile f = new JarFile(file.toString()); try (JarFile f = new JarFile(file.toFile())) {
Manifest m = f.getManifest(); loadModule(file.toUri().toURL(), f.getManifest().getMainAttributes().getValue("Main-Class"));
String mainclass = m.getMainAttributes().getValue("Main-Class"); } catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) {
loadModule(file.toUri().toURL(), mainclass, true); LogHelper.error(e);
f.close(); }
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) {
e.printStackTrace();
}
// Return result
return super.visitFile(file, attrs); return super.visitFile(file, attrs);
} }
} }
@ -72,7 +66,6 @@ public void close() {
} }
@Override @Override
public void initModules() { public void initModules() {
for (Module m : modules) { for (Module m : modules) {
m.init(context); m.init(context);
@ -81,53 +74,35 @@ public void initModules() {
} }
@Override @Override
public void load(Module module) { public void load(Module module) {
modules.add(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 { public void loadModuleFull(URL jarpath) throws ClassNotFoundException, IllegalAccessException, InstantiationException, URISyntaxException, IOException {
JarFile f = new JarFile(Paths.get(jarpath.toURI()).toString()); try (JarFile f = new JarFile(Paths.get(jarpath.toURI()).toFile())) {
Manifest m = f.getManifest(); classloader.addURL(jarpath);
String mainclass = m.getMainAttributes().getValue("Main-Class"); Module module = (Module) Class.forName(f.getManifest().getMainAttributes().getValue("Main-Class"), true, classloader).newInstance();
classloader.addURL(jarpath); modules.add(module);
Class<?> moduleclass = Class.forName(mainclass, true, classloader); module.preInit(context);
Module module = (Module) moduleclass.newInstance(); module.init(context);
modules.add(module); module.postInit(context);
module.preInit(context); module.finish(context);
module.init(context); LogHelper.info("Module %s version: %s loaded", module.getName(), module.getVersion());
module.postInit(context); }
LogHelper.info("Module %s version: %s loaded", module.getName(), module.getVersion());
f.close();
} }
@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); classloader.addURL(jarpath);
Class<?> moduleclass = Class.forName(classname, true, classloader); Module module = (Module) Class.forName(classname, true, classloader).newInstance();
Module module = (Module) moduleclass.newInstance();
modules.add(module); modules.add(module);
module.preInit(context);
if (!preload) module.init(context);
LogHelper.info("Module %s version: %s loaded", module.getName(), module.getVersion()); LogHelper.info("Module %s version: %s loaded", module.getName(), module.getVersion());
} }
@ -141,7 +116,6 @@ public void postInitModules() {
@Override @Override
public void preInitModules() { public void preInitModules() {
for (Module m : modules) { for (Module m : modules) {
m.preInit(context); m.preInit(context);
@ -150,7 +124,6 @@ public void preInitModules() {
} }
@Override @Override
public void printModules() { public void printModules() {
for (Module m : modules) for (Module m : modules)
LogHelper.info("Module %s version: %s", m.getName(), m.getVersion()); LogHelper.info("Module %s version: %s", m.getName(), m.getVersion());
@ -158,9 +131,16 @@ public void printModules() {
} }
@Override @Override
public void registerModule(Module module) {
public void registerModule(Module module, boolean preload) { load(module);
load(module, preload);
LogHelper.info("Module %s version: %s registered", module.getName(), module.getVersion()); 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());
}
}
} }

View file

@ -1,6 +1,6 @@
package ru.gravit.launcher.managers; 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.IOHelper;
import ru.gravit.utils.helper.LogHelper; import ru.gravit.utils.helper.LogHelper;
@ -8,10 +8,10 @@
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
public class ModulesConfigManager implements ModulesConfigManagerInterface { public class SimpleModulesConfigManager implements ModulesConfigManager {
public Path configDir; public Path configDir;
public ModulesConfigManager(Path configDir) { public SimpleModulesConfigManager(Path configDir) {
this.configDir = configDir; this.configDir = configDir;
} }

View file

@ -15,4 +15,8 @@ public interface Module extends AutoCloseable {
void postInit(ModuleContext context); void postInit(ModuleContext context);
void preInit(ModuleContext context); void preInit(ModuleContext context);
default void finish(ModuleContext context) {
// NOP
};
} }

View file

@ -7,7 +7,7 @@ enum Type {
Type getType(); Type getType();
ModulesManagerInterface getModulesManager(); ModulesManager getModulesManager();
ModulesConfigManagerInterface getModulesConfigManager(); ModulesConfigManager getModulesConfigManager();
} }

View file

@ -2,7 +2,7 @@
import java.nio.file.Path; import java.nio.file.Path;
public interface ModulesConfigManagerInterface { public interface ModulesConfigManager {
Path getModuleConfig(String moduleName); Path getModuleConfig(String moduleName);
Path getModuleConfigDir(String moduleName); Path getModuleConfigDir(String moduleName);

View file

@ -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);
}

View file

@ -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);
}

View file

@ -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) {
}
}

View file

@ -10,22 +10,6 @@
import java.util.*; import java.util.*;
public final class ClientProfile implements Comparable<ClientProfile> { 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 @LauncherAPI
public enum Version { public enum Version {
MC147("1.4.7", 51), MC147("1.4.7", 51),