Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public ModsConfig() throws IOException, DuplicateKeyException, YamlReaderExcepti
"If there are mods that weren't found by the search-algorithm, you can add an id (spigot or bukkit) and a custom link (optional & must be a static link to the latest mod jar).\n" +
"modrinth-id: Is the 'Project-ID' and can be found on the mods modrinth site inside of the 'About' box, under 'Technical Information' at the bottom left.\n" +
"curseforge-id: Is also called 'Project-ID' and can be found on the mods curseforge site inside of the 'About' box at the right.\n" +
"steam-workshop-id: The Steam Workshop item id read from the mods metadata. Requires server-updater.software to be a numeric Steam app-id.\n" +
"ignore-content-type: If true, does not check if the downloaded file is of type jar or zip, and downloads it anyway.\n" +
"force-latest: If true, does not search for updates compatible with this Minecraft version and simply picks the latest release.\n" +
"force-update: If true, downloads the update every time even if its already on the latest version.\n" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,9 @@ public UpdaterConfig() throws IOException, DuplicateKeyException, YamlReaderExce
"Updates your mods and the results are sent to AutoPlug-Web. You can configure this in the web-config.",
"Note that there is a web-cool-down (that cannot be changed) of a few hours, to prevent spamming of results to AutoPlug-Web.");
mods_updater_profile = put(name, "mods-updater", "profile").setDefValues("AUTOMATIC");
mods_updater_path = put(name, "mods-updater", "path").setDefValues("./mods");
mods_updater_path = put(name, "mods-updater", "path").setDefValues("./mods").setComments(
"Path to your mods folder.",
"Steam Workshop mods with supported metadata (meta.cpp) can be updated through SteamCMD when server-updater.software is set to a numeric Steam app-id.");
mods_updater_version = put(name, "mods-updater", "version").setComments("The Minecraft version to check and download mods for.",
"If left empty, taken from server-updater.version above, if also empty, taken from general.yml, if also empty, taken from server jar.");
mods_updater_async = put(name, "mods-updater", "async").setDefValues("true").setComments(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright (c) 2024 Osiris-Team.
* All rights reserved.
*
* This software is copyrighted work, licensed under the terms
* of the MIT-License. Consult the "LICENSE" file for details.
*/

package com.osiris.autoplug.client.tasks.updater.mods;

import com.osiris.autoplug.client.tasks.updater.search.SearchResult;

interface ModDownloadTask {
void start();

boolean isAlive();

String getPlName();

SearchResult getSearchResult();

boolean isDownloadSuccessful();

boolean isInstallSuccessful();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/*
* Copyright (c) 2024 Osiris-Team.
* All rights reserved.
*
* This software is copyrighted work, licensed under the terms
* of the MIT-License. Consult the "LICENSE" file for details.
*/

package com.osiris.autoplug.client.tasks.updater.mods;

import org.jetbrains.annotations.NotNull;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

/**
* A Steam Workshop mod living in its own directory inside the mods folder.
* It is identified by the {@code publishedid} inside its {@code meta.cpp} file.
* The mod's version is the {@code timestamp} from that file (when present) or
* the {@code time_updated} value returned by the Steam Web API after the first
* update check. Note that the version is intentionally left empty when the
* meta.cpp provides no timestamp, so the first update check always runs.
*/
public class SteamWorkshopMod extends MinecraftMod {
private final File directory;
private String publishedId;
private final String timestamp;

public SteamWorkshopMod(File directory, String name, String publishedId) {
this(directory, name, publishedId, null);
}

public SteamWorkshopMod(File directory, String name, String publishedId, String timestamp) {
super(directory.getAbsolutePath(), name, timestamp, "Steam Workshop", null, null, null);
this.directory = directory;
this.publishedId = publishedId;
this.timestamp = timestamp;
}

public File getDirectory() {
return directory;
}

public String getPublishedId() {
return publishedId;
}

public void setPublishedId(String publishedId) {
this.publishedId = publishedId;
}

public String getTimestamp() {
return timestamp;
}

@NotNull
public static List<SteamWorkshopMod> findIn(File dir) throws IOException {
if (!dir.exists()) throw new FileNotFoundException("Directory does not exist: " + dir);
List<SteamWorkshopMod> mods = new ArrayList<>();
File[] files = dir.listFiles();
if (files == null) return mods;
Arrays.sort(files, Comparator.comparing(File::getName));
for (File file : files) {
if (!file.isDirectory()) continue;
File metaFile = new File(file, "meta.cpp");
if (metaFile.exists())
mods.add(readFromMeta(file, metaFile));
}
return mods;
}

static SteamWorkshopMod readFromMeta(File modDir, File metaFile) throws IOException {
String name = modDir.getName();
String publishedId = null;
String timestamp = null;
for (String line : Files.readAllLines(metaFile.toPath(), StandardCharsets.UTF_8)) {
String trimmedLine = line.trim();
if (trimmedLine.isEmpty() || trimmedLine.startsWith("//")) continue;

int equalsIndex = trimmedLine.indexOf('=');
if (equalsIndex < 0) continue;

String key = trimmedLine.substring(0, equalsIndex).trim();
String value = trimmedLine.substring(equalsIndex + 1).trim();
int semicolonIndex = value.indexOf(';');
if (semicolonIndex < 0) continue;
value = value.substring(0, semicolonIndex).trim();
if (value.startsWith("\"") && value.endsWith("\"") && value.length() >= 2)
value = value.substring(1, value.length() - 1);

if (key.equals("name")) name = value;
if (key.equals("publishedid")) publishedId = value;
if (key.equals("timestamp")) timestamp = value;
}

if (publishedId == null || !publishedId.matches("\\d+"))
throw new IOException("Failed to read publishedid from " + metaFile);

return new SteamWorkshopMod(modDir, name, publishedId, timestamp);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
* Copyright (c) 2024 Osiris-Team.
* All rights reserved.
*
* This software is copyrighted work, licensed under the terms
* of the MIT-License. Consult the "LICENSE" file for details.
*/

package com.osiris.autoplug.client.tasks.updater.mods;

import com.osiris.autoplug.client.configs.UpdaterConfig;
import com.osiris.autoplug.client.tasks.updater.search.SearchResult;
import com.osiris.autoplug.client.utils.SteamCMD;
import org.jetbrains.annotations.NotNull;

/**
* Finds updates for Steam Workshop mods via the Steam Web API, similar to how
* {@link com.osiris.autoplug.client.tasks.updater.plugins.ResourceFinder}
* finds updates for regular mods. Only reports an update when the Workshop
* item was actually updated after the currently cached version, so mods are
* not re-downloaded on every run.
*/
public class SteamWorkshopUpdateFinder {
private final UpdaterConfig updaterConfig;
private final SteamCMD steamCMD;

public SteamWorkshopUpdateFinder(UpdaterConfig updaterConfig, SteamCMD steamCMD) {
this.updaterConfig = updaterConfig;
this.steamCMD = steamCMD;
}

public SearchResult find(@NotNull SteamWorkshopMod mod) {
SearchResult result = new SearchResult(null, SearchResult.Type.UP_TO_DATE, mod.getVersion(), null, "steam-workshop", null, null, false);
result.mod = mod;
String workshopAppId = getWorkshopAppId();
if (workshopAppId == null) {
result.type = SearchResult.Type.API_ERROR;
result.setException(new Exception("Steam Workshop mod '" + mod.getName() + "' was found, but server-updater.software is not a numeric Steam app-id."));
return result;
}

try {
SteamCMD.SteamWorkshopItemDetails details = steamCMD.getWorkshopItemDetails(mod.getPublishedId());
result.latestVersion = details.getTimeUpdated();
result.downloadUrl = details.getFileUrl();
if (hasUpdate(mod, details.getTimeUpdated()))
result.type = SearchResult.Type.UPDATE_AVAILABLE;
} catch (Exception e) {
result.type = SearchResult.Type.API_ERROR;
result.setException(e);
}
return result;
}

/**
* Returns the Steam app-id configured in server-updater.software,
* or null when it is not a numeric Steam app-id (e.g. "paper").
*/
public String getWorkshopAppId() {
String workshopAppId = updaterConfig.server_software.asString();
if (workshopAppId == null || !workshopAppId.matches("\\d+"))
return null;
return workshopAppId;
}

/**
* Compares the cached version with the time_updated value of the Workshop item.
* A missing cached version means the mod was never update-checked before.
*/
boolean hasUpdate(SteamWorkshopMod mod, String latestTimeUpdated) {
if (latestTimeUpdated == null || latestTimeUpdated.isEmpty())
return false;
String currentVersion = mod.getVersion();
if (currentVersion == null || currentVersion.isEmpty())
return true;
if (latestTimeUpdated.equals(currentVersion))
return false;
try {
return Long.parseLong(latestTimeUpdated) > Long.parseLong(currentVersion);
} catch (NumberFormatException e) {
// Versions are not numerically comparable (e.g. a meta.cpp FILETIME
// timestamp vs the unix time_updated from Steam). Assume an update.
return true;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import java.io.FileOutputStream;


public class TaskModDownload extends BThread {
public class TaskModDownload extends BThread implements ModDownloadTask {
private final String plName;
private final String plLatestVersion;
private final String url;
Expand Down Expand Up @@ -207,6 +207,10 @@ public File getDownloadDest() {
return dest;
}

public SearchResult getSearchResult() {
return searchResult;
}

public boolean isDownloadSuccessful() {
return isDownloadSuccessful;
}
Expand Down
Loading