Skip to content

Commit

Permalink
Add version comparison
Browse files Browse the repository at this point in the history
  • Loading branch information
ItsNature committed Sep 19, 2023
1 parent 4b6d9ec commit ced19b4
Show file tree
Hide file tree
Showing 7 changed files with 210 additions and 1 deletion.
17 changes: 17 additions & 0 deletions api/src/main/java/com/lunarclient/apollo/ApolloPlatform.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
*/
package com.lunarclient.apollo;

import java.util.logging.Logger;
import org.jetbrains.annotations.ApiStatus;

/**
Expand All @@ -41,6 +42,22 @@ public interface ApolloPlatform {
*/
Kind getKind();

/**
* Returns the current Apollo version.
*
* @return the current apollo version
* @since 1.0.0
*/
String getApolloVersion();

/**
* Returns the servers logger.
*
* @return the servers logger
* @since 1.0.0
*/
Logger getPlatformLogger();

/**
* Represents the kind of server a platform is.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
* This file is part of Apollo, licensed under the MIT License.
*
* Copyright (c) 2023 Moonsworth
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package com.lunarclient.apollo.version;

import java.util.Arrays;
import lombok.Getter;

/**
* Represents an Apollo version.
*
* @since 1.0.0
*/
@Getter
public class ApolloVersion {

private final int major;
private final int minor;
private final int patch;

/**
* Constructs the {@link ApolloVersion} by the
* provided version string.
*
* <p>Divides the version string by the major, minor & patch version.</p>
*
* @param version the version
* @since 1.0.0
*/
public ApolloVersion(String version) {
System.out.println(version);

String[] versions = version
.replaceAll("[^0-9.]", "")
.split("\\.");

System.out.println(Arrays.toString(versions)); // TODO: remove

if (versions.length != 3) {
throw new RuntimeException("Failed to parse Apollo version.");
}

try {
this.major = Integer.parseInt(versions[0]);
this.minor = Integer.parseInt(versions[1]);
this.patch = Integer.parseInt(versions[2]);
} catch (NumberFormatException e) {
throw new RuntimeException("Failed to parse Apollo version.");
}
}

/**
* Checks the current and the latest available for updates.
*
* @param version the latest available version
* @return whether the update is available
* @see com.lunarclient.apollo.version.ApolloVersionManager
* @since 1.0.0
*/
public boolean isUpdateAvailable(ApolloVersion version) {
if (this.major > version.getMajor()) {
return true;
} else if (this.minor > version.getMinor()) {
return true;
}

return this.patch > version.getPatch();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* This file is part of Apollo, licensed under the MIT License.
*
* Copyright (c) 2023 Moonsworth
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package com.lunarclient.apollo.version;

import com.lunarclient.apollo.Apollo;

/**
* Manages Apollo versioning.
*
* @since 1.0.0
*/
public class ApolloVersionManager {

// TODO: add message toggle to config

private static final String DOWNLOAD_URL = "https://lunarclient.dev/apollo/downloads";
private static final String UPDATE_MESSAGE = "[Apollo] You’re running an outdated version, update to the latest version here: " + DOWNLOAD_URL;

/**
* Constructs the {@link ApolloVersionManager}.
*
* @since 1.0.0
*/
public ApolloVersionManager() {
this.checkForUpdates();
}

private void checkForUpdates() {
ApolloVersion currentVersion = new ApolloVersion(Apollo.getPlatform().getApolloVersion());
ApolloVersion latestVersion = new ApolloVersion("v1.0.1"); // TODO: fetch

if (currentVersion.isUpdateAvailable(latestVersion)) {
Apollo.getPlatform().getPlatformLogger().warning(UPDATE_MESSAGE);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@
import com.lunarclient.apollo.world.ApolloWorldManagerImpl;
import com.lunarclient.apollo.wrapper.BukkitApolloPlayer;
import com.lunarclient.apollo.wrapper.BukkitApolloWorld;
import java.util.logging.Logger;
import lombok.Getter;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
Expand Down Expand Up @@ -137,6 +139,16 @@ public Kind getKind() {
return Kind.SERVER;
}

@Override
public String getApolloVersion() {
return this.getDescription().getVersion();
}

@Override
public Logger getPlatformLogger() {
return Bukkit.getServer().getLogger();
}

@EventHandler
private void onWorldLoad(WorldLoadEvent event) {
((ApolloWorldManagerImpl) Apollo.getWorldManager()).addWorld(new BukkitApolloWorld(event.getWorld()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import com.lunarclient.apollo.module.ApolloModuleManagerImpl;
import com.lunarclient.apollo.player.ApolloPlayerManagerImpl;
import com.lunarclient.apollo.wrapper.BungeeApolloPlayer;
import java.util.logging.Logger;
import lombok.Getter;
import net.md_5.bungee.api.ProxyServer;
import net.md_5.bungee.api.connection.ProxiedPlayer;
Expand Down Expand Up @@ -73,6 +74,16 @@ public Kind getKind() {
return Kind.PROXY;
}

@Override
public String getApolloVersion() {
return this.getDescription().getVersion();
}

@Override
public Logger getPlatformLogger() {
return ProxyServer.getInstance().getLogger();
}

@EventHandler
private void onPluginMessage(PluginMessageEvent event) {
if (!(event.getReceiver() instanceof ProxyServer)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import com.lunarclient.apollo.option.config.Serializers;
import com.lunarclient.apollo.player.ApolloPlayerManagerImpl;
import com.lunarclient.apollo.roundtrip.ApolloRoundtripManager;
import com.lunarclient.apollo.version.ApolloVersionManager;
import com.lunarclient.apollo.world.ApolloWorldManagerImpl;
import java.nio.file.Path;
import lombok.Getter;
Expand All @@ -48,6 +49,7 @@ public final class ApolloManager {
public static final String PLUGIN_MESSAGE_CHANNEL = "lunar:apollo";

@Getter private static ApolloNetworkManager networkManager;
@Getter private static ApolloVersionManager versionManager;
@Getter private static CommentedConfigurationNode configurationNode;

private static YamlConfigurationLoader configurationLoader;
Expand All @@ -73,6 +75,7 @@ public static void bootstrap(ApolloPlatform platform) {
new ApolloRoundtripManager()
);

ApolloManager.versionManager = new ApolloVersionManager();
ApolloManager.networkManager = new ApolloNetworkManager();
} catch (Throwable throwable) {
throw new RuntimeException("Unable to bootstrap Apollo!", throwable);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,14 @@
import com.velocitypowered.api.event.proxy.ProxyInitializeEvent;
import com.velocitypowered.api.event.proxy.ProxyShutdownEvent;
import com.velocitypowered.api.plugin.Plugin;
import com.velocitypowered.api.plugin.PluginContainer;
import com.velocitypowered.api.plugin.PluginDescription;
import com.velocitypowered.api.plugin.annotation.DataDirectory;
import com.velocitypowered.api.proxy.Player;
import com.velocitypowered.api.proxy.ProxyServer;
import com.velocitypowered.api.proxy.messages.MinecraftChannelIdentifier;
import java.nio.file.Path;
import java.util.logging.Logger;
import lombok.Getter;

/**
Expand All @@ -56,17 +59,20 @@
)
public final class ApolloVelocityPlatform implements ApolloPlatform {

public static MinecraftChannelIdentifier PLUGIN_CHANNEL = MinecraftChannelIdentifier.from(ApolloManager.PLUGIN_MESSAGE_CHANNEL);
public static final MinecraftChannelIdentifier PLUGIN_CHANNEL = MinecraftChannelIdentifier.from(ApolloManager.PLUGIN_MESSAGE_CHANNEL);

@Getter private static ApolloVelocityPlatform instance;

private final ProxyServer server;
private final Logger logger;
private final Path dataDirectory;

@Inject
private ApolloVelocityPlatform(ProxyServer server,
Logger logger,
@DataDirectory Path dataDirectory) {
this.server = server;
this.logger = logger;
this.dataDirectory = dataDirectory;
}

Expand All @@ -75,6 +81,19 @@ public Kind getKind() {
return Kind.PROXY;
}

@Override
public String getApolloVersion() {
return this.server.getPluginManager().fromInstance(this)
.map(PluginContainer::getDescription)
.flatMap(PluginDescription::getVersion)
.orElse(null);
}

@Override
public Logger getPlatformLogger() {
return this.logger;
}

/**
* Handles initialization of the proxy.
*
Expand Down

0 comments on commit ced19b4

Please sign in to comment.