Skip to content

Commit

Permalink
misc: 调整更新检查流程
Browse files Browse the repository at this point in the history
  • Loading branch information
MATRIX-feather committed Nov 2, 2023
1 parent 7a98ace commit cf016a3
Show file tree
Hide file tree
Showing 5 changed files with 151 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/main/java/xiamomc/morph/MorphPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import xiamomc.morph.messages.MorphMessageStore;
import xiamomc.morph.messages.vanilla.VanillaMessageStore;
import xiamomc.morph.misc.PlayerOperationSimulator;
import xiamomc.morph.misc.UpdateHandler;
import xiamomc.morph.updates.UpdateHandler;
import xiamomc.morph.misc.integrations.gsit.GSitCompactProcessor;
import xiamomc.morph.misc.integrations.placeholderapi.PlaceholderIntegration;
import xiamomc.morph.network.server.MorphClientHandler;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import xiamomc.morph.MorphPluginObject;
import xiamomc.morph.messages.MessageUtils;
import xiamomc.morph.messages.UpdateStrings;
import xiamomc.morph.misc.UpdateHandler;
import xiamomc.morph.updates.UpdateHandler;
import xiamomc.morph.misc.permissions.CommonPermissions;
import xiamomc.pluginbase.Annotations.Resolved;
import xiamomc.pluginbase.Command.ISubCommand;
Expand Down
30 changes: 30 additions & 0 deletions src/main/java/xiamomc/morph/updates/Platforms.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package xiamomc.morph.updates;

import java.util.Arrays;

public enum Platforms
{
SPIGOT("Spigot"),
PAPER("Paper"),
PURPUR("Purpur"),
FOLIA("Folia");

private final String implName;

public String getImplName()
{
return implName;
}

Platforms(String implName)
{
this.implName = implName;
}

public static Platforms fromName(String name)
{
return Arrays.stream(Platforms.values())
.filter(v -> v.implName.equalsIgnoreCase(name))
.findFirst().orElse(PAPER);
}
}
95 changes: 95 additions & 0 deletions src/main/java/xiamomc/morph/updates/SingleUpdateInfoMeta.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package xiamomc.morph.updates;

import com.google.gson.annotations.SerializedName;
import org.slf4j.ILoggerFactory;
import xiamomc.morph.MorphPlugin;

import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

public class SingleUpdateInfoMeta
{
@SerializedName("id")
public String id;

@SerializedName("project_id")
public String projectId;

@SerializedName("author_id")
public String authorId;

@SerializedName("featured")
public boolean featured;

@SerializedName("name")
public String displayName;

@SerializedName("version_number")
public String versionNumber;

@SerializedName("changelog")
public String changelog;

@SerializedName("changelog_url")
public String changelogUrl;

@SerializedName("date_published")
public String datePublished;

@SerializedName("downloads")
public double downloads;

@SerializedName("version_type")
public String versionType;

@SerializedName("status")
public String status;

@SerializedName("requested_status")
public String requestedStatus;

@SerializedName("files")
public List<Object> files;

@SerializedName("dependencies")
public List<Object> dependencies;

@SerializedName("game_versions")
public List<String> supportedVersions;

@SerializedName("loaders")
public List<String> supportedLoaders;

public static SingleUpdateInfoMeta fromMap(Map<?, ?> map)
{
var instance = new SingleUpdateInfoMeta();
var fields = instance.getClass().getDeclaredFields();

for (Field field : fields)
{
var serializedName = field.isAnnotationPresent(SerializedName.class)
? field.getAnnotation(SerializedName.class).value()
: field.getName();

var mapValue = map.getOrDefault(serializedName, null);

//MorphPlugin.getInstance().getSLF4JLogger()
// .info("Name: '%s', ST: '%s', VT: '%s'"
// .formatted(field.getName(), field.getType(), (mapValue == null ? "nil" : mapValue.getClass())));

try
{
field.set(instance, mapValue);
}
catch (Throwable t)
{
MorphPlugin.getInstance().getSLF4JLogger().warn("Unable to set '%s' to '%s': %s".formatted(field.getName(), mapValue, t.getMessage()));
//t.printStackTrace();
}
}

return instance;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package xiamomc.morph.misc;
package xiamomc.morph.updates;

import com.google.gson.GsonBuilder;
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
Expand Down Expand Up @@ -155,14 +155,29 @@ private void onUpdateReqFinish(CloseableHttpResponse response, int reqId,
var responseStr = EntityUtils.toString(response.getEntity());
var gson = new GsonBuilder().create();
var versionList = gson.fromJson(responseStr, ArrayList.class);
var objFirst = versionList.stream().findFirst().orElse(null);

if (!(objFirst instanceof Map<?, ?> map))
var metaList = new ObjectArrayList<SingleUpdateInfoMeta>();
for (Object o : versionList)
{
if (objFirst == null)
logger.error("Unable to check update: This version of Minecraft is not listed yet.");
if (o instanceof Map<?,?> map)
metaList.add(SingleUpdateInfoMeta.fromMap(map));
else
logger.error("Unable to check update: Origin server sent an unknown response");
logger.warn("Cant deserialize element to SingleUpdateInfoMeta: Not a map (" + o + ")");
}

var loader = Platforms.fromName(Bukkit.getName());
var matchMeta = metaList.stream()
.filter(m ->
{
var supportedLoaders = m.supportedLoaders;
if (supportedLoaders == null) return false;

return supportedLoaders.stream().anyMatch(s -> s.equalsIgnoreCase(loader.getImplName()));
}).findFirst().orElse(null);

if (matchMeta == null)
{
logger.error("Unable to check update: This version of Minecraft is not listed yet, or your server '%s' is not supported"
.formatted(loader.getImplName()));

if (onFinish != null)
onFinish.accept(CheckResult.FAIL);
Expand All @@ -171,20 +186,19 @@ private void onUpdateReqFinish(CloseableHttpResponse response, int reqId,
}

var currentVersion = plugin.getPluginMeta().getVersion();
var latestVersion = (String) map.getOrDefault("version_number", null);
var latestVersion = matchMeta.versionNumber;
if (latestVersion == null)
{
if (onFinish != null)
onFinish.accept(CheckResult.FAIL);

throw new NullDependencyException("Null version number from response: " + map);
throw new NullDependencyException("Null version number from response: " + gson.toJson(matchMeta));
}

if (currentVersion.equals(latestVersion))
{
logger.info("Already on the latest version");


if (onFinish != null)
onFinish.accept(CheckResult.ALREADY_LATEST);

Expand Down

0 comments on commit cf016a3

Please sign in to comment.