Merge branch 'feature/newmodules' into dev

This commit is contained in:
Gravit 2019-08-28 15:33:20 +07:00
commit afae94cffa
No known key found for this signature in database
GPG key ID: 061981E1E85D3216
57 changed files with 817 additions and 368 deletions

View file

@ -38,6 +38,8 @@
import pro.gravit.launcher.hasher.HashedDir;
import pro.gravit.launcher.managers.ConfigManager;
import pro.gravit.launcher.managers.GarbageManager;
import pro.gravit.launcher.modules.LauncherModulesManager;
import pro.gravit.launcher.modules.events.ClosePhase;
import pro.gravit.launcher.profiles.ClientProfile;
import pro.gravit.launchserver.auth.AuthProviderPair;
import pro.gravit.launchserver.binary.EXEL4JLauncherBinary;
@ -51,6 +53,11 @@
import pro.gravit.launchserver.manangers.*;
import pro.gravit.launchserver.manangers.hook.AuthHookManager;
import pro.gravit.launchserver.manangers.hook.BuildHookManager;
import pro.gravit.launchserver.modules.events.LaunchServerFullInitEvent;
import pro.gravit.launchserver.modules.events.LaunchServerInitPhase;
import pro.gravit.launchserver.modules.events.LaunchServerPostInitPhase;
import pro.gravit.launchserver.modules.events.NewLaunchServerInstanceEvent;
import pro.gravit.launchserver.modules.impl.LaunchServerModulesManager;
import pro.gravit.launchserver.socket.handlers.NettyServerSocketHandler;
import pro.gravit.utils.command.*;
import pro.gravit.utils.helper.CommonHelper;
@ -226,7 +233,7 @@ public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IO
public final AuthHookManager authHookManager;
// Server
public final ModulesManager modulesManager;
public final LaunchServerModulesManager modulesManager;
public final MirrorManager mirrorManager;
@ -268,7 +275,7 @@ public void collect()
}
}
public LaunchServer(LaunchServerDirectories directories, LaunchServerEnv env, LaunchServerConfig config, LaunchServerRuntimeConfig runtimeConfig, LaunchServerConfigManager launchServerConfigManager, ModulesManager modulesManager, RSAPublicKey publicKey, RSAPrivateKey privateKey, CommandHandler commandHandler) throws IOException, InvalidKeySpecException {
public LaunchServer(LaunchServerDirectories directories, LaunchServerEnv env, LaunchServerConfig config, LaunchServerRuntimeConfig runtimeConfig, LaunchServerConfigManager launchServerConfigManager, LaunchServerModulesManager modulesManager, RSAPublicKey publicKey, RSAPrivateKey privateKey, CommandHandler commandHandler) throws IOException, InvalidKeySpecException {
this.dir = directories.dir;
this.env = env;
this.config = config;
@ -292,7 +299,7 @@ public LaunchServer(LaunchServerDirectories directories, LaunchServerEnv env, La
serverCertFile = dir.resolve("server.crt");
serverKeyFile = dir.resolve("server.key");
modulesManager.initContext(this);
modulesManager.invokeEvent(new NewLaunchServerInstanceEvent(this));
// Print keypair fingerprints
CRC32 crc = new CRC32();
@ -302,11 +309,6 @@ public LaunchServer(LaunchServerDirectories directories, LaunchServerEnv env, La
// Load class bindings.
launcherEXEBinaryClass = defaultLauncherEXEBinaryClass;
// pre init modules
//modulesManager = new ModulesManager(this);
//modulesManager.autoload(dir.resolve("modules"));
modulesManager.preInitModules();
runtime.verify();
config.verify();
if (config.components != null) {
@ -371,7 +373,7 @@ public LaunchServer(LaunchServerDirectories directories, LaunchServerEnv env, La
pro.gravit.launchserver.command.handler.CommandHandler.registerCommands(commandHandler, this);
// init modules
modulesManager.initModules();
modulesManager.invokeEvent(new LaunchServerInitPhase(this));
if (config.components != null) {
LogHelper.debug("Init components");
config.components.forEach((k, v) -> {
@ -400,7 +402,7 @@ public LaunchServer(LaunchServerDirectories directories, LaunchServerEnv env, La
syncProfilesDir();
// post init modules
modulesManager.postInitModules();
modulesManager.invokeEvent(new LaunchServerPostInitPhase(this));
if (config.components != null) {
LogHelper.debug("PostInit components");
config.components.forEach((k, v) -> {
@ -457,7 +459,7 @@ public void close() throws Exception {
// Close handlers & providers
config.close(ReloadType.FULL);
modulesManager.close();
modulesManager.invokeEvent(new ClosePhase());
LogHelper.info("Save LaunchServer runtime config");
launchServerConfigManager.writeRuntimeConfig(runtime);
// Print last message before death :(
@ -504,7 +506,8 @@ public void run() {
}
if (config.netty != null)
rebindNettyServerSocket();
modulesManager.finishModules();
modulesManager.fullInitializedLaunchServer(this);
modulesManager.invokeEvent(new LaunchServerFullInitEvent(this));
}

View file

@ -2,7 +2,7 @@
import pro.gravit.launchserver.config.LaunchServerConfig;
import pro.gravit.launchserver.config.LaunchServerRuntimeConfig;
import pro.gravit.launchserver.manangers.ModulesManager;
import pro.gravit.launchserver.modules.impl.LaunchServerModulesManager;
import pro.gravit.utils.command.CommandHandler;
import java.nio.file.Path;
@ -14,7 +14,7 @@ public class LaunchServerBuilder {
private LaunchServerRuntimeConfig runtimeConfig;
private CommandHandler commandHandler;
private LaunchServer.LaunchServerEnv env;
private ModulesManager modulesManager;
private LaunchServerModulesManager modulesManager;
private LaunchServer.LaunchServerDirectories directories = new LaunchServer.LaunchServerDirectories();
private RSAPublicKey publicKey;
private RSAPrivateKey privateKey;
@ -30,7 +30,7 @@ public LaunchServerBuilder setEnv(LaunchServer.LaunchServerEnv env) {
return this;
}
public LaunchServerBuilder setModulesManager(ModulesManager modulesManager) {
public LaunchServerBuilder setModulesManager(LaunchServerModulesManager modulesManager) {
this.modulesManager = modulesManager;
return this;
}

View file

@ -2,6 +2,7 @@
import pro.gravit.launcher.Launcher;
import pro.gravit.launcher.hwid.HWIDProvider;
import pro.gravit.launcher.modules.events.PreConfigPhase;
import pro.gravit.launchserver.auth.handler.AuthHandler;
import pro.gravit.launchserver.auth.hwid.HWIDHandler;
import pro.gravit.launchserver.auth.permissions.PermissionsHandler;
@ -13,7 +14,7 @@
import pro.gravit.launchserver.config.LaunchServerRuntimeConfig;
import pro.gravit.launchserver.dao.provider.DaoProvider;
import pro.gravit.launchserver.manangers.LaunchServerGsonManager;
import pro.gravit.launchserver.manangers.ModulesManager;
import pro.gravit.launchserver.modules.impl.LaunchServerModulesManager;
import pro.gravit.launchserver.socket.WebSocketService;
import pro.gravit.utils.command.CommandHandler;
import pro.gravit.utils.command.JLineCommandHandler;
@ -55,10 +56,11 @@ public static void main(String[] args) throws Exception {
LaunchServerRuntimeConfig runtimeConfig;
LaunchServerConfig config;
LaunchServer.LaunchServerEnv env = LaunchServer.LaunchServerEnv.PRODUCTION;
ModulesManager modulesManager = new ModulesManager(dir.resolve("config"));
modulesManager.autoload(dir.resolve("modules"));
LaunchServerModulesManager modulesManager = new LaunchServerModulesManager(dir.resolve("modules"), dir.resolve("config"));
modulesManager.autoload();
modulesManager.initModules(null);
registerAll();
initGson();
initGson(modulesManager);
if (IOHelper.exists(dir.resolve("LaunchServer.conf"))) {
configFile = dir.resolve("LaunchServer.conf");
} else {
@ -97,6 +99,12 @@ public static void main(String[] args) throws Exception {
IOHelper.write(publicKeyFile, publicKey.getEncoded());
IOHelper.write(privateKeyFile, privateKey.getEncoded());
}
modulesManager.invokeEvent(new PreConfigPhase());
generateConfigIfNotExists(configFile, localCommandHandler, env);
LogHelper.info("Reading LaunchServer config file");
try (BufferedReader reader = IOHelper.newReader(configFile)) {
config = Launcher.gsonManager.gson.fromJson(reader, LaunchServerConfig.class);
}
if (!Files.exists(runtimeConfigFile)) {
LogHelper.info("Reset LaunchServer runtime config file");
runtimeConfig = new LaunchServerRuntimeConfig();
@ -107,11 +115,6 @@ public static void main(String[] args) throws Exception {
runtimeConfig = Launcher.gsonManager.gson.fromJson(reader, LaunchServerRuntimeConfig.class);
}
}
generateConfigIfNotExists(configFile, localCommandHandler, env);
LogHelper.info("Reading LaunchServer config file");
try (BufferedReader reader = IOHelper.newReader(configFile)) {
config = Launcher.gsonManager.gson.fromJson(reader, LaunchServerConfig.class);
}
LaunchServer.LaunchServerConfigManager launchServerConfigManager = new LaunchServer.LaunchServerConfigManager() {
@Override
@ -169,8 +172,8 @@ public void writeRuntimeConfig(LaunchServerRuntimeConfig config) throws IOExcept
server.run();
}
public static void initGson() {
Launcher.gsonManager = new LaunchServerGsonManager();
public static void initGson(LaunchServerModulesManager modulesManager) {
Launcher.gsonManager = new LaunchServerGsonManager(modulesManager);
Launcher.gsonManager.initGson();
}

View file

@ -17,16 +17,18 @@
import pro.gravit.launcher.AutogenConfig;
import pro.gravit.launcher.Launcher;
import pro.gravit.launcher.LauncherConfig;
import pro.gravit.launcher.modules.LauncherModule;
import pro.gravit.launcher.modules.LauncherModulesManager;
import pro.gravit.launcher.modules.Module;
import pro.gravit.launcher.modules.ModulesManager;
import pro.gravit.launchserver.asm.ClassMetadataReader;
import pro.gravit.launchserver.asm.SafeClassWriter;
public class JAConfigurator {
private static final String modulesManagerName = Type.getInternalName(ModulesManager.class);
private static final String launcherName = Type.getInternalName(Launcher.class);
private static final String modulesManagerDesc = Type.getDescriptor(ModulesManager.class);
private static final String registerModDesc = Type.getMethodDescriptor(Type.VOID_TYPE, Type.getType(Module.class));
private static final String modulesManagerName = "pro/gravit/launcher/modules/LauncherModulesManager";
private static final String launcherName = "pro/gravit/launcher/LauncherEngine";
private static final String modulesManagerDesc = "Lpro/gravit/launcher/client/ClientModulesManager;";
private static final String registerModDesc = Type.getMethodDescriptor(Type.VOID_TYPE, Type.getType(LauncherModule.class));
private static final String autoGenConfigName = Type.getInternalName(AutogenConfig.class);
private static final String stringDesc = Type.getDescriptor(String.class);
private final ClassNode configclass;
@ -48,7 +50,7 @@ public void addModuleClass(String fullName) {
initModuleMethod.instructions.add(new TypeInsnNode(Opcodes.NEW, fullName.replace('.', '/')));
initModuleMethod.instructions.add(new InsnNode(Opcodes.DUP));
initModuleMethod.instructions.add(new MethodInsnNode(Opcodes.INVOKESPECIAL, fullName.replace('.', '/'), "<init>", "()V"));
initModuleMethod.instructions.add(new MethodInsnNode(Opcodes.INVOKEINTERFACE, modulesManagerName, "registerModule", registerModDesc));
initModuleMethod.instructions.add(new MethodInsnNode(Opcodes.INVOKEINTERFACE, modulesManagerName, "loadModule", registerModDesc));
}
public byte[] getBytecode(ClassMetadataReader reader) {

View file

@ -1,6 +1,7 @@
package pro.gravit.launchserver.command.modules;
import java.net.URI;
import java.nio.file.Path;
import java.nio.file.Paths;
import pro.gravit.launchserver.LaunchServer;
@ -24,7 +25,7 @@ public String getUsageDescription() {
@Override
public void invoke(String... args) throws Exception {
verifyArgs(args, 1);
URI uri = Paths.get(args[0]).toUri();
server.modulesManager.loadModule(uri.toURL());
Path file = Paths.get(args[0]);
server.modulesManager.loadModule(file);
}
}

View file

@ -20,6 +20,6 @@ public String getUsageDescription() {
@Override
public void invoke(String... args) {
server.modulesManager.printModules();
server.modulesManager.printModulesInfo();
}
}

View file

@ -5,6 +5,7 @@
import pro.gravit.launcher.hwid.HWID;
import pro.gravit.launcher.hwid.HWIDProvider;
import pro.gravit.launcher.managers.GsonManager;
import pro.gravit.launcher.modules.events.PreGsonPhase;
import pro.gravit.launcher.request.JsonResultSerializeAdapter;
import pro.gravit.launcher.request.WebSocketEvent;
import pro.gravit.launchserver.auth.handler.AuthHandler;
@ -15,11 +16,18 @@
import pro.gravit.launchserver.auth.texture.TextureProvider;
import pro.gravit.launchserver.components.Component;
import pro.gravit.launchserver.dao.provider.DaoProvider;
import pro.gravit.launchserver.modules.impl.LaunchServerModulesManager;
import pro.gravit.launchserver.socket.WebSocketService;
import pro.gravit.launchserver.socket.response.WebSocketServerResponse;
import pro.gravit.utils.UniversalJsonAdapter;
public class LaunchServerGsonManager extends GsonManager {
private final LaunchServerModulesManager modulesManager;
public LaunchServerGsonManager(LaunchServerModulesManager modulesManager) {
this.modulesManager = modulesManager;
}
@Override
public void registerAdapters(GsonBuilder builder) {
super.registerAdapters(builder);
@ -34,6 +42,7 @@ public void registerAdapters(GsonBuilder builder) {
builder.registerTypeAdapter(HWID.class, new UniversalJsonAdapter<>(HWIDProvider.hwids));
builder.registerTypeAdapter(WebSocketServerResponse.class, new UniversalJsonAdapter<>(WebSocketService.providers));
builder.registerTypeAdapter(WebSocketEvent.class, new JsonResultSerializeAdapter());
modulesManager.invokeEvent(new PreGsonPhase(builder));
//ClientWebSocketService.appendTypeAdapters(builder);
}
}

View file

@ -1,32 +0,0 @@
package pro.gravit.launchserver.manangers;
import java.net.URL;
import java.nio.file.Path;
import java.util.ArrayList;
import pro.gravit.launcher.managers.SimpleModuleManager;
import pro.gravit.launcher.managers.SimpleModulesConfigManager;
import pro.gravit.launchserver.LaunchServer;
import pro.gravit.launchserver.modules.CoreModule;
import pro.gravit.launchserver.modules.LaunchServerModuleContext;
import pro.gravit.utils.PublicURLClassLoader;
public class ModulesManager extends SimpleModuleManager {
public SimpleModulesConfigManager configManager;
public ModulesManager(Path configDir) {
modules = new ArrayList<>(1);
configManager = new SimpleModulesConfigManager(configDir);
classloader = new PublicURLClassLoader(new URL[0], ClassLoader.getSystemClassLoader());
registerCoreModule();
}
public void initContext(LaunchServer server)
{
context = new LaunchServerModuleContext(server, classloader, configManager);
}
private void registerCoreModule() {
load(new CoreModule());
}
}

View file

@ -25,7 +25,7 @@ public Type getType() {
@Override
public ModulesManager getModulesManager() {
return launchServer.modulesManager;
return null;
}
@Override

View file

@ -0,0 +1,12 @@
package pro.gravit.launchserver.modules.events;
import pro.gravit.launcher.modules.LauncherModule;
import pro.gravit.launchserver.LaunchServer;
public class LaunchServerFullInitEvent extends LauncherModule.Event {
public final LaunchServer server;
public LaunchServerFullInitEvent(LaunchServer server) {
this.server = server;
}
}

View file

@ -0,0 +1,12 @@
package pro.gravit.launchserver.modules.events;
import pro.gravit.launcher.modules.events.InitPhase;
import pro.gravit.launchserver.LaunchServer;
public class LaunchServerInitPhase extends InitPhase {
public final LaunchServer server;
public LaunchServerInitPhase(LaunchServer server) {
this.server = server;
}
}

View file

@ -0,0 +1,12 @@
package pro.gravit.launchserver.modules.events;
import pro.gravit.launcher.modules.events.InitPhase;
import pro.gravit.launchserver.LaunchServer;
public class LaunchServerPostInitPhase extends InitPhase {
public final LaunchServer server;
public LaunchServerPostInitPhase(LaunchServer server) {
this.server = server;
}
}

View file

@ -0,0 +1,12 @@
package pro.gravit.launchserver.modules.events;
import pro.gravit.launcher.modules.LauncherModule;
import pro.gravit.launchserver.LaunchServer;
public class NewLaunchServerInstanceEvent extends LauncherModule.Event {
public final LaunchServer launchServer;
public NewLaunchServerInstanceEvent(LaunchServer launchServer) {
this.launchServer = launchServer;
}
}

View file

@ -0,0 +1,24 @@
package pro.gravit.launchserver.modules.impl;
import pro.gravit.launcher.modules.LauncherInitContext;
import pro.gravit.launcher.modules.LauncherModule;
import pro.gravit.launcher.modules.LauncherModuleInfo;
import pro.gravit.launcher.modules.events.InitPhase;
import pro.gravit.utils.Version;
import pro.gravit.utils.helper.LogHelper;
public class LaunchServerCoreModule extends LauncherModule {
public LaunchServerCoreModule() {
super(new LauncherModuleInfo("LaunchServerCore", Version.getVersion()));
}
@Override
public void init(LauncherInitContext initContext) {
registerEvent(this::testEvent, InitPhase.class);
}
public void testEvent(InitPhase event)
{
//LogHelper.debug("[LaunchServerCore] Event LaunchServerInitPhase passed");
}
}

View file

@ -0,0 +1,12 @@
package pro.gravit.launchserver.modules.impl;
import pro.gravit.launcher.modules.LauncherInitContext;
import pro.gravit.launchserver.LaunchServer;
public class LaunchServerInitContext implements LauncherInitContext {
public final LaunchServer server;
public LaunchServerInitContext(LaunchServer server) {
this.server = server;
}
}

View file

@ -0,0 +1,36 @@
package pro.gravit.launchserver.modules.impl;
import pro.gravit.launcher.modules.LauncherModule;
import pro.gravit.launcher.modules.LauncherModuleInfo;
import pro.gravit.launcher.modules.impl.SimpleModuleManager;
import pro.gravit.launchserver.LaunchServer;
import pro.gravit.utils.helper.LogHelper;
import java.nio.file.Path;
import java.util.Arrays;
public class LaunchServerModulesManager extends SimpleModuleManager {
public LaunchServerCoreModule coreModule;
public LaunchServerModulesManager(Path modulesDir, Path configDir) {
super(modulesDir, configDir);
coreModule = new LaunchServerCoreModule();
modules.add(coreModule);
}
public void fullInitializedLaunchServer(LaunchServer server)
{
initContext = new LaunchServerInitContext(server);
}
public void printModulesInfo()
{
for(LauncherModule module : modules)
{
LauncherModuleInfo info = module.getModuleInfo();
LogHelper.info("[MODULE] %s v: %s p: %d deps: %s", info.name, info.version.getVersionString(), info.priority, Arrays.toString(info.dependencies));
}
}
@Override
public LauncherModule getCoreModule() {
return coreModule;
}
}

View file

@ -7,6 +7,8 @@
import pro.gravit.launcher.client.DirBridge;
import pro.gravit.launcher.client.FunctionalBridge;
import pro.gravit.launcher.client.LauncherUpdateController;
import pro.gravit.launcher.client.events.ClientEngineInitPhase;
import pro.gravit.launcher.client.events.ClientPreGuiPhase;
import pro.gravit.launcher.guard.LauncherGuardManager;
import pro.gravit.launcher.gui.JSRuntimeProvider;
import pro.gravit.launcher.gui.RuntimeProvider;
@ -14,6 +16,9 @@
import pro.gravit.launcher.managers.ClientGsonManager;
import pro.gravit.launcher.managers.ClientHookManager;
import pro.gravit.launcher.managers.ConsoleManager;
import pro.gravit.launcher.modules.LauncherModulesManager;
import pro.gravit.launcher.modules.events.PreConfigPhase;
import pro.gravit.launcher.modules.impl.SimpleModuleManager;
import pro.gravit.launcher.request.Request;
import pro.gravit.launcher.request.RequestException;
import pro.gravit.launcher.request.auth.RestoreSessionRequest;
@ -33,10 +38,15 @@ public static void main(String... args) throws Throwable {
//if(!LauncherAgent.isStarted()) throw new SecurityException("JavaAgent not set");
LogHelper.printVersion("Launcher");
LogHelper.printLicense("Launcher");
LauncherEngine.modulesManager = new ClientModuleManager();
LauncherConfig.getAutogenConfig().initModules();
LauncherEngine.modulesManager.initModules(null);
// Start Launcher
initGson();
initGson(LauncherEngine.modulesManager);
ConsoleManager.initConsole();
HWIDProvider.registerHWIDs();
LauncherEngine.modulesManager.invokeEvent(new PreConfigPhase());
LauncherConfig config = Launcher.getConfig();
if (config.environment.equals(LauncherConfig.LauncherEnvironment.PROD)) {
if (!LauncherAgent.isStarted()) throw new SecurityException("LauncherAgent must started");
@ -55,8 +65,8 @@ public static void main(String... args) throws Throwable {
System.exit(0);
}
public static void initGson() {
Launcher.gsonManager = new ClientGsonManager();
public static void initGson(ClientModuleManager modulesManager) {
Launcher.gsonManager = new ClientGsonManager(modulesManager);
Launcher.gsonManager.initGson();
}
@ -64,6 +74,8 @@ public static void initGson() {
private final AtomicBoolean started = new AtomicBoolean(false);
public RuntimeProvider runtimeProvider;
public static ClientModuleManager modulesManager;
private LauncherEngine() {
}
@ -71,9 +83,10 @@ private LauncherEngine() {
@LauncherAPI
public void start(String... args) throws Throwable {
Launcher.modulesManager = new ClientModuleManager(this);
LauncherConfig.getAutogenConfig().initModules();
Launcher.modulesManager.preInitModules();
//Launcher.modulesManager = new ClientModuleManager(this);
ClientPreGuiPhase event = new ClientPreGuiPhase(null);
LauncherEngine.modulesManager.invokeEvent(event);
runtimeProvider = event.runtimeProvider;
if (runtimeProvider == null) runtimeProvider = new JSRuntimeProvider();
ClientHookManager.initGuiHook.hook(runtimeProvider);
runtimeProvider.init(false);
@ -105,7 +118,7 @@ public void start(String... args) throws Throwable {
Objects.requireNonNull(args, "args");
if (started.getAndSet(true))
throw new IllegalStateException("Launcher has been already started");
Launcher.modulesManager.initModules();
LauncherEngine.modulesManager.invokeEvent(new ClientEngineInitPhase(this));
runtimeProvider.preLoad();
FunctionalBridge.getHWID = CommonHelper.newThread("GetHWID Thread", true, FunctionalBridge::getHWID);
FunctionalBridge.getHWID.start();

View file

@ -32,6 +32,7 @@
import pro.gravit.launcher.LauncherAgent;
import pro.gravit.launcher.LauncherConfig;
import pro.gravit.launcher.LauncherEngine;
import pro.gravit.launcher.client.events.ClientLauncherInitPhase;
import pro.gravit.launcher.guard.LauncherGuardManager;
import pro.gravit.launcher.gui.JSRuntimeProvider;
import pro.gravit.launcher.hasher.FileNameMatcher;
@ -39,6 +40,8 @@
import pro.gravit.launcher.hwid.HWIDProvider;
import pro.gravit.launcher.managers.ClientGsonManager;
import pro.gravit.launcher.managers.ClientHookManager;
import pro.gravit.launcher.modules.events.PostInitPhase;
import pro.gravit.launcher.modules.events.PreConfigPhase;
import pro.gravit.launcher.profiles.ClientProfile;
import pro.gravit.launcher.profiles.PlayerProfile;
import pro.gravit.launcher.request.Request;
@ -435,10 +438,13 @@ public static Process launch(
@LauncherAPI
public static void main(String... args) throws Throwable {
LauncherEngine engine = LauncherEngine.clientInstance();
Launcher.modulesManager = new ClientModuleManager(engine);
//Launcher.modulesManager = new ClientModuleManager(engine);
LauncherEngine.modulesManager = new ClientModuleManager();
LauncherConfig.getAutogenConfig().initModules(); //INIT
initGson();
Launcher.modulesManager.preInitModules();
LauncherEngine.modulesManager.initModules(null);
initGson(LauncherEngine.modulesManager);
//Launcher.modulesManager.preInitModules();
LauncherEngine.modulesManager.invokeEvent(new PreConfigPhase());
JVMHelper.verifySystemProperties(ClientLauncher.class, true);
EnvHelper.checkDangerousParams();
JVMHelper.checkStackTrace(ClientLauncher.class);
@ -472,7 +478,7 @@ public static void main(String... args) throws Throwable {
playerProfile = params.pp;
Request.setSession(params.session);
checkJVMBitsAndVersion();
Launcher.modulesManager.initModules();
LauncherEngine.modulesManager.invokeEvent(new ClientLauncherInitPhase());
// Verify ClientLauncher sign and classpath
LogHelper.debug("Verifying ClientLauncher sign and classpath");
LinkedList<Path> classPath = resolveClassPathList(params.clientDir, profile.getClassPath());
@ -524,7 +530,7 @@ public static void main(String... args) throws Throwable {
// else hdir.removeR(s.file);
//}
Launcher.profile.pushOptionalFile(clientHDir, false);
Launcher.modulesManager.postInitModules();
LauncherEngine.modulesManager.invokeEvent(new PostInitPhase());
// Start WatchService, and only then client
CommonHelper.newThread("Asset Directory Watcher", true, assetWatcher).start();
CommonHelper.newThread("Client Directory Watcher", true, clientWatcher).start();
@ -555,8 +561,8 @@ private static Stream<Path> resolveClassPathStream(Path clientDir, String... cla
return builder.build();
}
private static void initGson() {
Launcher.gsonManager = new ClientGsonManager();
private static void initGson(ClientModuleManager moduleManager) {
Launcher.gsonManager = new ClientGsonManager(moduleManager);
Launcher.gsonManager.initGson();
}

View file

@ -1,30 +0,0 @@
package pro.gravit.launcher.client;
import pro.gravit.launcher.Launcher;
import pro.gravit.launcher.LauncherEngine;
import pro.gravit.launcher.modules.ModuleContext;
import pro.gravit.launcher.modules.ModulesConfigManager;
import pro.gravit.launcher.modules.ModulesManager;
public class ClientModuleContext implements ModuleContext {
public final LauncherEngine engine;
ClientModuleContext(LauncherEngine engine) {
this.engine = engine;
}
@Override
public Type getType() {
return Type.CLIENT;
}
@Override
public ModulesManager getModulesManager() {
return Launcher.modulesManager;
}
@Override
public ModulesConfigManager getModulesConfigManager() {
return null; // ClientModuleContext не поддерживает modulesConfigManager
}
}

View file

@ -1,25 +1,28 @@
package pro.gravit.launcher.client;
import java.net.URL;
import java.util.ArrayList;
import pro.gravit.launcher.modules.LauncherModule;
import pro.gravit.launcher.modules.impl.SimpleModuleManager;
import pro.gravit.launcher.LauncherEngine;
import pro.gravit.launcher.managers.SimpleModuleManager;
import java.io.IOException;
import java.nio.file.Path;
public class ClientModuleManager extends SimpleModuleManager {
public ClientModuleManager(LauncherEngine engine) {
context = new ClientModuleContext(engine);
modules = new ArrayList<>();
public ClientModuleManager() {
super(null, null);
}
@Override
public void loadModule(URL jarpath, String classname) {
throw new SecurityException("Custom JAR's load not allowed here");
public void autoload() throws IOException {
throw new UnsupportedOperationException();
}
@Override
public void loadModuleFull(URL jarpath) {
throw new SecurityException("Custom JAR's load not allowed here");
public void autoload(Path dir) throws IOException {
throw new UnsupportedOperationException();
}
@Override
public LauncherModule loadModule(Path file) throws IOException {
throw new UnsupportedOperationException();
}
}

View file

@ -0,0 +1,12 @@
package pro.gravit.launcher.client.events;
import pro.gravit.launcher.LauncherEngine;
import pro.gravit.launcher.modules.events.InitPhase;
public class ClientEngineInitPhase extends InitPhase {
public final LauncherEngine engine;
public ClientEngineInitPhase(LauncherEngine engine) {
this.engine = engine;
}
}

View file

@ -0,0 +1,12 @@
package pro.gravit.launcher.client.events;
import pro.gravit.launcher.gui.RuntimeProvider;
import pro.gravit.launcher.modules.LauncherModule;
public class ClientGuiPhase extends LauncherModule.Event {
public final RuntimeProvider runtimeProvider;
public ClientGuiPhase(RuntimeProvider runtimeProvider) {
this.runtimeProvider = runtimeProvider;
}
}

View file

@ -0,0 +1,6 @@
package pro.gravit.launcher.client.events;
import pro.gravit.launcher.modules.events.InitPhase;
public class ClientLauncherInitPhase extends InitPhase {
}

View file

@ -0,0 +1,12 @@
package pro.gravit.launcher.client.events;
import pro.gravit.launcher.gui.RuntimeProvider;
import pro.gravit.launcher.modules.LauncherModule;
public class ClientPreGuiPhase extends LauncherModule.Event {
public RuntimeProvider runtimeProvider;
public ClientPreGuiPhase(RuntimeProvider runtimeProvider) {
this.runtimeProvider = runtimeProvider;
}
}

View file

@ -11,16 +11,13 @@
import javax.script.ScriptEngine;
import javax.script.ScriptException;
import pro.gravit.launcher.JSApplication;
import pro.gravit.launcher.Launcher;
import pro.gravit.launcher.LauncherAPI;
import pro.gravit.launcher.LauncherConfig;
import pro.gravit.launcher.NewLauncherSettings;
import pro.gravit.launcher.*;
import pro.gravit.launcher.client.ClientLauncher;
import pro.gravit.launcher.client.DirBridge;
import pro.gravit.launcher.client.FunctionalBridge;
import pro.gravit.launcher.client.ServerPinger;
import pro.gravit.launcher.client.UserSettings;
import pro.gravit.launcher.client.events.ClientGuiPhase;
import pro.gravit.launcher.hasher.FileNameMatcher;
import pro.gravit.launcher.hasher.HashedDir;
import pro.gravit.launcher.hasher.HashedEntry;
@ -28,6 +25,7 @@
import pro.gravit.launcher.hwid.NoHWID;
import pro.gravit.launcher.hwid.OshiHWID;
import pro.gravit.launcher.managers.SettingsManager;
import pro.gravit.launcher.modules.events.ClosePhase;
import pro.gravit.launcher.profiles.ClientProfile;
import pro.gravit.launcher.profiles.PlayerProfile;
import pro.gravit.launcher.profiles.Texture;
@ -163,9 +161,9 @@ public void run(String[] args) throws ScriptException, NoSuchMethodException, IO
preLoad();
loadScript(Launcher.INIT_SCRIPT_FILE);
LogHelper.info("Invoking start() function");
Launcher.modulesManager.postInitModules();
LauncherEngine.modulesManager.invokeEvent(new ClientGuiPhase(this));
((Invocable) engine).invokeFunction("start", (Object) args);
Launcher.modulesManager.finishModules();
LauncherEngine.modulesManager.invokeEvent(new ClosePhase());
}
@Override

View file

@ -2,18 +2,27 @@
import com.google.gson.GsonBuilder;
import pro.gravit.launcher.client.ClientModuleManager;
import pro.gravit.launcher.client.UserSettings;
import pro.gravit.launcher.hwid.HWID;
import pro.gravit.launcher.hwid.HWIDProvider;
import pro.gravit.launcher.modules.events.PreGsonPhase;
import pro.gravit.launcher.request.websockets.ClientWebSocketService;
import pro.gravit.utils.UniversalJsonAdapter;
public class ClientGsonManager extends GsonManager {
private final ClientModuleManager moduleManager;
public ClientGsonManager(ClientModuleManager moduleManager) {
this.moduleManager = moduleManager;
}
@Override
public void registerAdapters(GsonBuilder builder) {
super.registerAdapters(builder);
builder.registerTypeAdapter(UserSettings.class, new UniversalJsonAdapter<>(UserSettings.providers));
builder.registerTypeAdapter(HWID.class, new UniversalJsonAdapter<>(HWIDProvider.hwids));
ClientWebSocketService.appendTypeAdapters(builder);
moduleManager.invokeEvent(new PreGsonPhase(builder));
}
}

View file

@ -34,8 +34,6 @@ public final class Launcher {
private static final AtomicReference<LauncherConfig> CONFIG = new AtomicReference<>();
@LauncherAPI
public static ModulesManager modulesManager = null;
@LauncherAPI
public static final int PROTOCOL_MAGIC_LEGACY = 0x724724_00 + 24;
@LauncherAPI
public static final int PROTOCOL_MAGIC = 0xA205B064; // e = 2.718281828

View file

@ -1,158 +0,0 @@
package pro.gravit.launcher.managers;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
import java.util.jar.JarFile;
import pro.gravit.launcher.modules.Module;
import pro.gravit.launcher.modules.ModuleContext;
import pro.gravit.launcher.modules.ModulesManager;
import pro.gravit.utils.PublicURLClassLoader;
import pro.gravit.utils.helper.IOHelper;
import pro.gravit.utils.helper.LogHelper;
public class SimpleModuleManager implements ModulesManager {
protected final class ModulesVisitor extends SimpleFileVisitor<Path> {
private ModulesVisitor() {
}
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
if (file.toFile().getName().endsWith(".jar"))
try (JarFile f = new JarFile(file.toFile())) {
String mainclass = f.getManifest().getMainAttributes().getValue("Main-Class");
if(mainclass == null)
{
LogHelper.error("In module %s Main-Class not found", file.toString());
return super.visitFile(file, attrs);
}
loadModule(file.toUri().toURL(), mainclass);
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) {
LogHelper.error(e);
}
return super.visitFile(file, attrs);
}
}
public ArrayList<Module> modules;
public PublicURLClassLoader classloader;
protected ModuleContext context;
public void autoload(Path dir) throws IOException {
LogHelper.info("Load modules");
if (Files.notExists(dir)) Files.createDirectory(dir);
else {
IOHelper.walk(dir, new ModulesVisitor(), true);
}
sort();
LogHelper.info("Loaded %d modules", modules.size());
}
public void sort() {
modules.sort((m1, m2) -> {
int p1 = m1.getPriority();
int p2 = m2.getPriority();
return Integer.compare(p2, p1);
});
}
@Override
public void close() {
for (Module m : modules)
try {
m.close();
} catch (Throwable t) {
if (m.getName() != null) LogHelper.error("Error in stopping module: %s", m.getName());
else LogHelper.error("Error in stopping one of modules");
LogHelper.error(t);
}
}
@Override
public void initModules() {
for (Module m : modules) {
m.init(context);
LogHelper.info("Module %s version: %s init", m.getName(), m.getVersion());
}
}
@Override
public void load(Module module) {
modules.add(module);
}
public void loadModuleFull(URL jarpath) throws ClassNotFoundException, IllegalAccessException, InstantiationException, URISyntaxException, IOException {
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
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"));
}
}
@Override
public void loadModule(URL jarpath, String classname) throws ClassNotFoundException, IllegalAccessException, InstantiationException {
classloader.addURL(jarpath);
Module module = (Module) Class.forName(classname, true, classloader).newInstance();
modules.add(module);
LogHelper.info("Module %s version: %s loaded", module.getName(), module.getVersion());
}
@Override
public void postInitModules() {
for (Module m : modules) {
m.postInit(context);
LogHelper.info("Module %s version: %s post-init", m.getName(), m.getVersion());
}
}
@Override
public void preInitModules() {
for (Module m : modules) {
m.preInit(context);
LogHelper.info("Module %s version: %s pre-init", m.getName(), m.getVersion());
}
}
@Override
public void printModules() {
for (Module m : modules)
LogHelper.info("Module %s version: %s", m.getName(), m.getVersion());
LogHelper.info("Loaded %d modules", modules.size());
}
@Override
public void registerModule(Module module) {
modules.add(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());
}
}
}

View file

@ -16,14 +16,12 @@ public SimpleModulesConfigManager(Path configDir) {
}
public Path getModuleConfig(String moduleName) {
if (!IOHelper.isDir(configDir)) {
try {
Files.createDirectories(configDir);
} catch (IOException e) {
LogHelper.error(e);
return getModuleConfig(moduleName, "Config");
}
}
return configDir.resolve(moduleName.concat("Config.json"));
@Override
public Path getModuleConfig(String moduleName, String configName) {
return getModuleConfigDir(moduleName).resolve(moduleName.concat(configName.concat(".json")));
}
public Path getModuleConfigDir(String moduleName) {

View file

@ -0,0 +1,4 @@
package pro.gravit.launcher.modules;
public interface LauncherInitContext {
}

View file

@ -0,0 +1,96 @@
package pro.gravit.launcher.modules;
import java.util.HashMap;
import java.util.Map;
public abstract class LauncherModule {
private LauncherModulesContext context;
private Map<Class<? extends Event>, EventHandler> eventMap = new HashMap<>();
protected LauncherModulesManager modulesManager;
protected final LauncherModuleInfo moduleInfo;
protected ModulesConfigManager modulesConfigManager;
protected InitStatus initStatus = InitStatus.CREATED;
protected LauncherModule() {
moduleInfo = new LauncherModuleInfo("UnknownModule");
}
protected LauncherModule(LauncherModuleInfo info) {
moduleInfo = info;
}
public LauncherModuleInfo getModuleInfo() {
return moduleInfo;
}
public enum InitStatus
{
CREATED,
INIT,
FINISH
}
@FunctionalInterface
public interface EventHandler<T extends Event>
{
void event(T e);
}
public static class Event
{
public boolean isCancel() {
return cancel;
}
public Event cancel() {
this.cancel = true;
return this;
}
protected boolean cancel = false;
}
public InitStatus getInitStatus() {
return initStatus;
}
public LauncherModule setInitStatus(InitStatus initStatus) {
this.initStatus = initStatus;
return this;
}
public void setContext(LauncherModulesContext context)
{
if(this.context != null) throw new IllegalStateException("Module already set context");
this.context = context;
this.modulesManager = context.getModulesManager();
this.modulesConfigManager = context.getModulesConfigManager();
}
public void preInit() {
//NOP
}
public abstract void init(LauncherInitContext initContext);
protected <T extends Event> boolean registerEvent(EventHandler<T> handle, Class<T> tClass)
{
eventMap.put(tClass, handle);
return true;
}
@SuppressWarnings("unchecked")
public final <T extends Event> void callEvent(T event)
{
Class<? extends Event> tClass = event.getClass();
for(Map.Entry<Class<? extends Event>, EventHandler> e : eventMap.entrySet())
{
if(e.getKey().isAssignableFrom(tClass))
{
e.getValue().event(event);
if(event.isCancel()) return;
}
}
}
}

View file

@ -0,0 +1,38 @@
package pro.gravit.launcher.modules;
import pro.gravit.utils.Version;
public class LauncherModuleInfo {
public final String name;
public final Version version;
public final int priority;
public final String[] dependencies;
public LauncherModuleInfo(String name, Version version) {
this.name = name;
this.version = version;
this.priority = 0;
this.dependencies = new String[]{};
}
public LauncherModuleInfo(String name) {
this.name = name;
this.version = new Version(1,0,0);
this.priority = 0;
this.dependencies = new String[]{};
}
public LauncherModuleInfo(String name, Version version, String[] dependencies) {
this.name = name;
this.version = version;
this.priority = 0;
this.dependencies = dependencies;
}
public LauncherModuleInfo(String name, Version version, int priority, String[] dependencies) {
this.name = name;
this.version = version;
this.priority = priority;
this.dependencies = dependencies;
}
}

View file

@ -0,0 +1,6 @@
package pro.gravit.launcher.modules;
public interface LauncherModulesContext {
LauncherModulesManager getModulesManager();
ModulesConfigManager getModulesConfigManager();
}

View file

@ -0,0 +1,27 @@
package pro.gravit.launcher.modules;
import pro.gravit.utils.Version;
import java.io.IOException;
import java.nio.file.Path;
import java.util.function.Predicate;
public interface LauncherModulesManager {
LauncherModule loadModule(LauncherModule module);
LauncherModule loadModule(Path file) throws IOException;
LauncherModule getModule(String name);
LauncherModule getCoreModule();
default boolean containsModule(String name)
{
return getModule(name) != null;
}
default <T extends LauncherModule> boolean containsModule(Class<? extends T> clazz)
{
return getModule(clazz) != null;
}
ClassLoader getModuleClassLoader();
<T extends LauncherModule> T getModule(Class<? extends T> clazz);
<T extends LauncherModule> T findModule(Class<? extends T> clazz, Predicate<Version> versionPredicate);
<T extends LauncherModule.Event> void invokeEvent(T event);
}

View file

@ -1,7 +1,7 @@
package pro.gravit.launcher.modules;
import pro.gravit.utils.Version;
@Deprecated
public interface Module extends AutoCloseable {
String getName();

View file

@ -1,5 +1,5 @@
package pro.gravit.launcher.modules;
@Deprecated
public interface ModuleContext {
enum Type {
SERVER, CLIENT, LAUNCHSERVER

View file

@ -5,5 +5,7 @@
public interface ModulesConfigManager {
Path getModuleConfig(String moduleName);
Path getModuleConfig(String moduleName, String configName);
Path getModuleConfigDir(String moduleName);
}

View file

@ -1,7 +1,7 @@
package pro.gravit.launcher.modules;
import java.net.URL;
@Deprecated
public interface ModulesManager extends AutoCloseable {
void initModules();

View file

@ -0,0 +1,6 @@
package pro.gravit.launcher.modules.events;
import pro.gravit.launcher.modules.LauncherModule;
public class ClosePhase extends LauncherModule.Event {
}

View file

@ -0,0 +1,6 @@
package pro.gravit.launcher.modules.events;
import pro.gravit.launcher.modules.LauncherModule;
public class InitPhase extends LauncherModule.Event {
}

View file

@ -0,0 +1,6 @@
package pro.gravit.launcher.modules.events;
import pro.gravit.launcher.modules.LauncherModule;
public class PostInitPhase extends LauncherModule.Event {
}

View file

@ -0,0 +1,6 @@
package pro.gravit.launcher.modules.events;
import pro.gravit.launcher.modules.LauncherModule;
public class PreConfigPhase extends LauncherModule.Event {
}

View file

@ -0,0 +1,12 @@
package pro.gravit.launcher.modules.events;
import com.google.gson.GsonBuilder;
import pro.gravit.launcher.modules.LauncherModule;
public class PreGsonPhase extends LauncherModule.Event {
public GsonBuilder gsonBuilder;
public PreGsonPhase(GsonBuilder gsonBuilder) {
this.gsonBuilder = gsonBuilder;
}
}

View file

@ -0,0 +1,24 @@
package pro.gravit.launcher.modules.impl;
import pro.gravit.launcher.modules.LauncherModulesContext;
import pro.gravit.launcher.modules.LauncherModulesManager;
import pro.gravit.launcher.modules.ModulesConfigManager;
public class SimpleModuleContext implements LauncherModulesContext {
public final LauncherModulesManager modulesManager;
public final ModulesConfigManager configManager;
@Override
public LauncherModulesManager getModulesManager() {
return modulesManager;
}
@Override
public ModulesConfigManager getModulesConfigManager() {
return configManager;
}
public SimpleModuleContext(LauncherModulesManager modulesManager, ModulesConfigManager configManager) {
this.modulesManager = modulesManager;
this.configManager = configManager;
}
}

View file

@ -0,0 +1,203 @@
package pro.gravit.launcher.modules.impl;
import pro.gravit.launcher.managers.SimpleModulesConfigManager;
import pro.gravit.launcher.modules.*;
import pro.gravit.utils.PublicURLClassLoader;
import pro.gravit.utils.Version;
import pro.gravit.utils.helper.IOHelper;
import pro.gravit.utils.helper.LogHelper;
import java.io.IOException;
import java.net.URL;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.function.Predicate;
import java.util.jar.JarFile;
public class SimpleModuleManager implements LauncherModulesManager {
protected final List<LauncherModule> modules = new ArrayList<>();
protected final List<String> moduleNames = new ArrayList<>();
protected final SimpleModuleContext context;
protected final ModulesConfigManager modulesConfigManager;
protected final Path modulesDir;
protected LauncherInitContext initContext;
protected PublicURLClassLoader classLoader = new PublicURLClassLoader(new URL[]{});
protected final class ModulesVisitor extends SimpleFileVisitor<Path> {
private ModulesVisitor() {
}
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
loadModule(file);
return super.visitFile(file, attrs);
}
}
public void autoload() throws IOException {
autoload(modulesDir);
}
public void autoload(Path dir) throws IOException {
if (Files.notExists(dir)) Files.createDirectory(dir);
else {
IOHelper.walk(dir, new ModulesVisitor(), true);
}
}
public void initModules(LauncherInitContext initContext) {
List<LauncherModule> startedModules = Collections.unmodifiableList(new ArrayList<>(modules));
for(LauncherModule module : startedModules)
{
module.preInit();
}
boolean isAnyModuleLoad = true;
modules.sort((m1, m2) -> {
int priority1 = m1.getModuleInfo().priority;
int priority2 = m2.getModuleInfo().priority;
return Integer.compare(priority1, priority2);
});
int modules_size = modules.size();
int loaded = 0;
while(isAnyModuleLoad)
{
isAnyModuleLoad = false;
for(LauncherModule module : modules)
{
if(!module.getInitStatus().equals(LauncherModule.InitStatus.CREATED)) continue;
if(checkDepend(module))
{
isAnyModuleLoad = true;
module.setInitStatus(LauncherModule.InitStatus.INIT);
module.init(initContext);
module.setInitStatus(LauncherModule.InitStatus.FINISH);
loaded++;
}
}
if(modules_size >= loaded) return;
}
for(LauncherModule module : modules)
{
if(module.getInitStatus().equals(LauncherModule.InitStatus.CREATED))
{
LauncherModuleInfo info = module.getModuleInfo();
LogHelper.warning("Module %s required %s. Cyclic dependencies?", info.name, Arrays.toString(info.dependencies));
module.setInitStatus(LauncherModule.InitStatus.INIT);
module.init(initContext);
module.setInitStatus(LauncherModule.InitStatus.FINISH);
}
}
}
private boolean checkDepend(LauncherModule module)
{
LauncherModuleInfo info = module.getModuleInfo();
for(String dep : info.dependencies)
{
LauncherModule depModule = getModule(dep);
if(depModule == null) throw new RuntimeException(String.format("Module %s required %s. %s not found", info.name, dep, dep));
if(depModule.getInitStatus().equals(LauncherModule.InitStatus.CREATED)) return false;
}
return true;
}
public SimpleModuleManager(Path modulesDir, Path configDir) {
modulesConfigManager = new SimpleModulesConfigManager(configDir);
context = new SimpleModuleContext(this, modulesConfigManager);
this.modulesDir = modulesDir;
}
@Override
public LauncherModule loadModule(LauncherModule module) {
if(modules.contains(module)) return module;
modules.add(module);
LauncherModuleInfo info = module.getModuleInfo();
moduleNames.add(info.name);
module.setContext(context);
if(initContext != null)
{
module.preInit();
module.init(initContext);
}
return module;
}
@Override
public LauncherModule loadModule(Path file) throws IOException {
try (JarFile f = new JarFile(file.toFile())) {
String moduleClass = f.getManifest().getMainAttributes().getValue("Module-Main-Class");
if(moduleClass == null)
{
LogHelper.error("In module %s Module-Main-Class not found", file.toString());
return null;
}
classLoader.addURL(file.toUri().toURL());
LauncherModule module = (LauncherModule) Class.forName(moduleClass, true, classLoader).newInstance();
loadModule(module);
return module;
} catch (ClassNotFoundException | IllegalAccessException | InstantiationException e) {
LogHelper.error(e);
LogHelper.error("In module %s Module-Main-Class incorrect", file.toString());
return null;
}
}
@Override
public LauncherModule getModule(String name) {
for(LauncherModule module : modules)
{
LauncherModuleInfo info = module.getModuleInfo();
if(info.name.equals(name)) return module;
}
return null;
}
@Override
public LauncherModule getCoreModule() {
return null;
}
@Override
public ClassLoader getModuleClassLoader() {
return classLoader;
}
@Override
@SuppressWarnings("unchecked")
public <T extends LauncherModule> T getModule(Class<? extends T> clazz) {
for(LauncherModule module : modules)
{
if(clazz.isAssignableFrom(module.getClass())) return (T) module;
}
return null;
}
@Override
@SuppressWarnings("unchecked")
public <T extends LauncherModule> T findModule(Class<? extends T> clazz, Predicate<Version> versionPredicate) {
for(LauncherModule module : modules)
{
LauncherModuleInfo info = module.getModuleInfo();
if(!versionPredicate.test(info.version)) continue;
if(clazz.isAssignableFrom(module.getClass())) return (T) module;
}
return null;
}
@Override
public <T extends LauncherModule.Event> void invokeEvent(T event) {
for(LauncherModule module : modules)
{
module.callEvent(event);
if(event.isCancel()) return;
}
}
}

View file

@ -1,20 +0,0 @@
package pro.gravit.launcher.server;
import java.net.URL;
import java.nio.file.Paths;
import java.util.ArrayList;
import pro.gravit.launcher.managers.SimpleModuleManager;
import pro.gravit.launcher.managers.SimpleModulesConfigManager;
import pro.gravit.utils.PublicURLClassLoader;
public class ModulesManager extends SimpleModuleManager {
public SimpleModulesConfigManager modulesConfigManager;
public ModulesManager(ServerWrapper wrapper) {
modules = new ArrayList<>();
modulesConfigManager = new SimpleModulesConfigManager(Paths.get("modules-config"));
classloader = new PublicURLClassLoader(new URL[0], ClassLoader.getSystemClassLoader());
context = new ServerModuleContext(wrapper, classloader, modulesConfigManager);
}
}

View file

@ -1,35 +0,0 @@
package pro.gravit.launcher.server;
import pro.gravit.launcher.Launcher;
import pro.gravit.launcher.managers.SimpleModulesConfigManager;
import pro.gravit.launcher.modules.ModuleContext;
import pro.gravit.launcher.modules.ModulesConfigManager;
import pro.gravit.launcher.modules.ModulesManager;
import pro.gravit.utils.PublicURLClassLoader;
public class ServerModuleContext implements ModuleContext {
public final PublicURLClassLoader classLoader;
public final ServerWrapper wrapper;
public final SimpleModulesConfigManager modulesConfigManager;
public ServerModuleContext(ServerWrapper wrapper, PublicURLClassLoader classLoader, SimpleModulesConfigManager modulesConfigManager) {
this.classLoader = classLoader;
this.wrapper = wrapper;
this.modulesConfigManager = modulesConfigManager;
}
@Override
public Type getType() {
return Type.SERVER;
}
@Override
public ModulesManager getModulesManager() {
return Launcher.modulesManager;
}
@Override
public ModulesConfigManager getModulesConfigManager() {
return modulesConfigManager;
}
}

View file

@ -17,6 +17,8 @@
import pro.gravit.launcher.LauncherConfig;
import pro.gravit.launcher.config.JsonConfigurable;
import pro.gravit.launcher.events.request.ProfilesRequestEvent;
import pro.gravit.launcher.modules.events.PostInitPhase;
import pro.gravit.launcher.modules.events.PreConfigPhase;
import pro.gravit.launcher.profiles.ClientProfile;
import pro.gravit.launcher.request.Request;
import pro.gravit.launcher.request.RequestException;
@ -30,7 +32,7 @@
import pro.gravit.utils.helper.SecurityHelper;
public class ServerWrapper extends JsonConfigurable<ServerWrapper.Config> {
public ModulesManager modulesManager;
public static ServerWrapperModulesManager modulesManager;
public Config config;
public PublicURLClassLoader ucp;
public ClassLoader loader;
@ -38,6 +40,7 @@ public class ServerWrapper extends JsonConfigurable<ServerWrapper.Config> {
public static ServerWrapper wrapper;
public static Path modulesDir = Paths.get(System.getProperty("serverwrapper.modulesDir", "modules"));
public static Path modulesConfigDir = Paths.get(System.getProperty("serverwrapper.modulesConfigDir", "modules-config"));
public static Path configFile = Paths.get(System.getProperty("serverwrapper.configFile", "ServerWrapperConfig.json"));
public static Path publicKeyFile = Paths.get(System.getProperty("serverwrapper.publicKeyFile", "public.key"));
public static boolean disableSetup = Boolean.valueOf(System.getProperty("serverwrapper.disableSetup", "false"));
@ -93,13 +96,13 @@ public boolean loopAuth(int count, int sleeptime) {
return false;
}
public static void initGson() {
Launcher.gsonManager = new ServerWrapperGsonManager();
public static void initGson(ServerWrapperModulesManager modulesManager) {
Launcher.gsonManager = new ServerWrapperGsonManager(modulesManager);
Launcher.gsonManager.initGson();
}
public void run(String... args) throws Throwable {
initGson();
initGson(modulesManager);
if (args.length > 0 && args[0].equals("setup") && !disableSetup) {
LogHelper.debug("Read ServerWrapperConfig.json");
loadConfig();
@ -107,10 +110,7 @@ public void run(String... args) throws Throwable {
setup.run();
System.exit(0);
}
modulesManager = new ModulesManager(wrapper);
modulesManager.autoload(modulesDir);
Launcher.modulesManager = modulesManager;
modulesManager.preInitModules();
modulesManager.invokeEvent(new PreConfigPhase());
LogHelper.debug("Read ServerWrapperConfig.json");
loadConfig();
updateLauncherConfig();
@ -120,7 +120,7 @@ public void run(String... args) throws Throwable {
if (config.syncAuth) auth();
else
CommonHelper.newThread("Server Auth Thread", true, () -> loopAuth(config.reconnectCount, config.reconnectSleep));
modulesManager.initModules();
modulesManager.invokeEvent(new ServerWrapperInitPhase(this));
String classname = (config.mainclass == null || config.mainclass.isEmpty()) ? args[0] : config.mainclass;
if (classname.length() == 0) {
LogHelper.error("MainClass not found. Please set MainClass for ServerWrapper.cfg or first commandline argument");
@ -156,7 +156,7 @@ public void run(String... args) throws Throwable {
if (loader != null) mainClass = Class.forName(classname, true, loader);
else mainClass = Class.forName(classname);
MethodHandle mainMethod = MethodHandles.publicLookup().findStatic(mainClass, "main", MethodType.methodType(void.class, String[].class));
modulesManager.postInitModules();
modulesManager.invokeEvent(new PostInitPhase());
Request.service.reconnectCallback = () ->
{
LogHelper.debug("WebSocket connect closed. Try reconnect");
@ -202,6 +202,9 @@ public void updateLauncherConfig() {
public static void main(String... args) throws Throwable {
LogHelper.printVersion("ServerWrapper");
LogHelper.printLicense("ServerWrapper");
modulesManager = new ServerWrapperModulesManager(modulesDir, modulesConfigDir);
modulesManager.autoload();
modulesManager.initModules(null);
ServerWrapper.wrapper = new ServerWrapper(ServerWrapper.Config.class, configFile);
ServerWrapper.wrapper.run(args);
}

View file

@ -3,12 +3,20 @@
import com.google.gson.GsonBuilder;
import pro.gravit.launcher.managers.GsonManager;
import pro.gravit.launcher.modules.events.PreGsonPhase;
import pro.gravit.launcher.request.websockets.ClientWebSocketService;
public class ServerWrapperGsonManager extends GsonManager {
private final ServerWrapperModulesManager modulesManager;
public ServerWrapperGsonManager(ServerWrapperModulesManager modulesManager) {
this.modulesManager = modulesManager;
}
@Override
public void registerAdapters(GsonBuilder builder) {
super.registerAdapters(builder);
ClientWebSocketService.appendTypeAdapters(builder);
modulesManager.invokeEvent(new PreGsonPhase(builder));
}
}

View file

@ -0,0 +1,11 @@
package pro.gravit.launcher.server;
import pro.gravit.launcher.modules.LauncherInitContext;
public class ServerWrapperInitContext implements LauncherInitContext {
public final ServerWrapper serverWrapper;
public ServerWrapperInitContext(ServerWrapper serverWrapper) {
this.serverWrapper = serverWrapper;
}
}

View file

@ -0,0 +1,11 @@
package pro.gravit.launcher.server;
import pro.gravit.launcher.modules.LauncherModule;
public class ServerWrapperInitPhase extends LauncherModule.Event {
public final ServerWrapper serverWrapper;
public ServerWrapperInitPhase(ServerWrapper serverWrapper) {
this.serverWrapper = serverWrapper;
}
}

View file

@ -0,0 +1,15 @@
package pro.gravit.launcher.server;
import pro.gravit.launcher.modules.impl.SimpleModuleManager;
import java.nio.file.Path;
public class ServerWrapperModulesManager extends SimpleModuleManager {
public ServerWrapperModulesManager(Path modulesDir, Path configDir) {
super(modulesDir, configDir);
}
public void fullInitializeServerWrapper(ServerWrapper serverWrapper)
{
initContext = new ServerWrapperInitContext(serverWrapper);
}
}

View file

@ -0,0 +1,11 @@
package pro.gravit.launcher.server.setup;
import pro.gravit.launcher.modules.LauncherModule;
public class ServerWrapperPostSetupEvent extends LauncherModule.Event {
public final ServerWrapperSetup setup;
public ServerWrapperPostSetupEvent(ServerWrapperSetup setup) {
this.setup = setup;
}
}

View file

@ -0,0 +1,11 @@
package pro.gravit.launcher.server.setup;
import pro.gravit.launcher.modules.LauncherModule;
public class ServerWrapperPreSetupEvent extends LauncherModule.Event {
public final ServerWrapperSetup setup;
public ServerWrapperPreSetupEvent(ServerWrapperSetup setup) {
this.setup = setup;
}
}

View file

@ -20,6 +20,7 @@ public class ServerWrapperSetup {
public void run() throws IOException {
ServerWrapper wrapper = ServerWrapper.wrapper;
ServerWrapper.modulesManager.invokeEvent(new ServerWrapperPreSetupEvent(this));
System.out.println("Print jar filename:");
String jarName = commands.commandHandler.readLine();
Path jarPath = Paths.get(jarName);
@ -86,6 +87,7 @@ public void run() throws IOException {
Path startScriptBak = Paths.get("start.bak");
IOHelper.move(startScript, startScriptBak);
}
ServerWrapper.modulesManager.invokeEvent(new ServerWrapperSetupEvent(this));
try (Writer writer = IOHelper.newWriter(startScript)) {
if (JVMHelper.OS_TYPE == JVMHelper.OS.LINUX) {
writer.append("#!/bin/sh\n\n");
@ -108,7 +110,7 @@ public void run() throws IOException {
writer.append(ServerWrapper.class.getName());
writer.append("\n");
}
ServerWrapper.modulesManager.invokeEvent(new ServerWrapperPostSetupEvent(this));
}
public ServerWrapperSetup() throws IOException {

View file

@ -0,0 +1,11 @@
package pro.gravit.launcher.server.setup;
import pro.gravit.launcher.modules.LauncherModule;
public class ServerWrapperSetupEvent extends LauncherModule.Event {
public final ServerWrapperSetup setup;
public ServerWrapperSetupEvent(ServerWrapperSetup setup) {
this.setup = setup;
}
}

@ -1 +1 @@
Subproject commit 95154a369e166a3aaaf3e06f015eef270b93cd75
Subproject commit fcc1bae5771eaabbec803ca3b977e871cb457e81