[Change] Separated all World-Load related code to a new namespace

This commit is contained in:
Frank 2022-06-21 16:32:45 +02:00
parent 67d09676c4
commit 25fa53541f
79 changed files with 1924 additions and 814 deletions

View file

@ -1,6 +1,8 @@
package org.betterx.bclib.util;
import org.betterx.bclib.BCLib;
import org.betterx.worlds.together.WorldsTogether;
import org.betterx.worlds.together.util.PathUtil;
import net.fabricmc.loader.api.*;
import net.fabricmc.loader.api.metadata.*;
@ -23,26 +25,25 @@ import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* @deprecated Replaced by {@link org.betterx.worlds.together.util.ModUtil}
*/
@Deprecated(forRemoval = true)
public class ModUtil {
private static Map<String, ModInfo> mods;
/**
* Unloads the cache of available mods created from {@link #getMods()}
* @deprecated Replaced by {@link org.betterx.worlds.together.util.ModUtil#invalidateCachedMods()}
*/
@Deprecated(forRemoval = true)
public static void invalidateCachedMods() {
mods = null;
}
/**
* return a map of all mods that were found in the 'mods'-folder.
* <p>
* The method will cache the results. You can clear that cache (and free the memory) by
* calling {@link #invalidateCachedMods()}
* <p>
* An error message is printed if a mod fails to load, but the parsing will continue.
*
* @return A map of all found mods. (key=ModID, value={@link ModInfo})
* @deprecated Replaced by {@link org.betterx.worlds.together.util.ModUtil#getMods()} ()}
*/
@Deprecated(forRemoval = true)
public static Map<String, ModInfo> getMods() {
if (mods != null) return mods;
@ -234,52 +235,49 @@ public class ModUtil {
}
/**
* Returns the {@link ModInfo} or {@code null} if the mod was not found.
* <p>
* The call will also return null if the mode-Version in the jar-File is not the same
* as the version of the loaded Mod.
*
* @param modID The mod ID to query
* @return A {@link ModInfo}-Object for the querried Mod.
* @deprecated Replaced by {@link org.betterx.worlds.together.util.ModUtil#getModInfo(String)}
*/
@Deprecated(forRemoval = true)
public static ModInfo getModInfo(String modID) {
return getModInfo(modID, true);
}
/**
* @deprecated Replaced by {@link org.betterx.worlds.together.util.ModUtil#getModInfo(String, boolean)}
*/
@Deprecated(forRemoval = true)
public static ModInfo getModInfo(String modID, boolean matchVersion) {
getMods();
final ModInfo mi = mods.get(modID);
if (mi == null || (matchVersion && !getModVersion(modID).equals(mi.getVersion()))) return null;
if (mi == null || (matchVersion && !org.betterx.worlds.together.util.ModUtil.getModVersion(modID)
.equals(mi.getVersion())))
return null;
return mi;
}
/**
* Local Mod Version for the queried Mod
*
* @param modID The mod ID to query
* @return The version of the locally installed Mod
* @deprecated Replaced by {@link org.betterx.worlds.together.util.ModUtil#getModVersion(String)}
*/
@Deprecated(forRemoval = true)
public static String getModVersion(String modID) {
if (modID == BCLib.TOGETHER_WORLDS) modID = BCLib.MOD_ID;
if (modID == WorldsTogether.MOD_ID) modID = BCLib.MOD_ID;
Optional<ModContainer> optional = FabricLoader.getInstance()
.getModContainer(modID);
if (optional.isPresent()) {
ModContainer modContainer = optional.get();
return ModInfo.versionToString(modContainer.getMetadata()
.getVersion());
return org.betterx.worlds.together.util.ModUtil.ModInfo.versionToString(modContainer.getMetadata()
.getVersion());
}
return getModVersionFromJar(modID);
return org.betterx.worlds.together.util.ModUtil.getModVersionFromJar(modID);
}
/**
* Local Mod Version for the queried Mod from the Jar-File in the games mod-directory
*
* @param modID The mod ID to query
* @return The version of the locally installed Mod
* @deprecated Replaced by {@link org.betterx.worlds.together.util.ModUtil#getModVersionFromJar(String)}
*/
@Deprecated(forRemoval = true)
public static String getModVersionFromJar(String modID) {
final ModInfo mi = getModInfo(modID, false);
if (mi != null) return mi.getVersion();
@ -288,11 +286,9 @@ public class ModUtil {
}
/**
* Get mod version from string. String should be in format: %d.%d.%d
*
* @param version - {@link String} mod version.
* @return int mod version.
* @deprecated Replaced by {@link org.betterx.worlds.together.util.ModUtil#convertModVersion(String)}
*/
@Deprecated(forRemoval = true)
public static int convertModVersion(String version) {
if (version.isEmpty()) {
return 0;
@ -318,11 +314,9 @@ public class ModUtil {
}
/**
* Get mod version from integer. String will be in format %d.%d.%d
*
* @param version - mod version in integer form.
* @return {@link String} mod version.
* @deprecated Replaced by {@link org.betterx.worlds.together.util.ModUtil#convertModVersion(int)}
*/
@Deprecated(forRemoval = true)
public static String convertModVersion(int version) {
int a = (version >> 22) & 0xFF;
int b = (version >> 14) & 0xFF;
@ -331,25 +325,21 @@ public class ModUtil {
}
/**
* {@code true} if the version v1 is larger than v2
*
* @param v1 A Version string
* @param v2 Another Version string
* @return v1 &gt; v2
* @deprecated Replaced by {@link org.betterx.worlds.together.util.ModUtil#isLargerVersion(String, String)}
*/
@Deprecated(forRemoval = true)
public static boolean isLargerVersion(String v1, String v2) {
return convertModVersion(v1) > convertModVersion(v2);
return org.betterx.worlds.together.util.ModUtil.convertModVersion(v1) > org.betterx.worlds.together.util.ModUtil.convertModVersion(
v2);
}
/**
* {@code true} if the version v1 is larger or equal v2
*
* @param v1 A Version string
* @param v2 Another Version string
* @return v1 &ge; v2
* @deprecated Replaced by {@link org.betterx.worlds.together.util.ModUtil#isLargerOrEqualVersion(String, String)}
*/
@Deprecated(forRemoval = true)
public static boolean isLargerOrEqualVersion(String v1, String v2) {
return convertModVersion(v1) >= convertModVersion(v2);
return org.betterx.worlds.together.util.ModUtil.convertModVersion(v1) >= org.betterx.worlds.together.util.ModUtil.convertModVersion(
v2);
}
private static void accept(Path file) {
@ -387,6 +377,10 @@ public class ModUtil {
}
}
/**
* @deprecated Replaced by {@link org.betterx.worlds.together.util.ModUtil.ModInfo}
*/
@Deprecated(forRemoval = true)
public static class ModInfo {
public final ModMetadata metadata;
public final Path jarPath;
@ -396,13 +390,23 @@ public class ModUtil {
this.jarPath = jarPath;
}
/**
* @deprecated Replaced by {@link org.betterx.worlds.together.util.ModUtil.ModInfo#versionToString(Version)}
*/
@Deprecated(forRemoval = true)
public static String versionToString(Version v) {
if (v instanceof SemanticVersion) {
return versionToString((SemanticVersion) v);
return org.betterx.worlds.together.util.ModUtil.ModInfo.versionToString((SemanticVersion) v);
}
return convertModVersion(convertModVersion(v.toString()));
return org.betterx.worlds.together.util.ModUtil.convertModVersion(
org.betterx.worlds.together.util.ModUtil.convertModVersion(v.toString())
);
}
/**
* @deprecated Replaced by {@link org.betterx.worlds.together.util.ModUtil.ModInfo#versionToString(SemanticVersion)}
*/
@Deprecated(forRemoval = true)
public static String versionToString(SemanticVersion v) {
StringBuilder stringBuilder = new StringBuilder();
boolean first = true;
@ -429,7 +433,7 @@ public class ModUtil {
if (metadata == null) {
return "0.0.0";
}
return versionToString(metadata.getVersion());
return org.betterx.worlds.together.util.ModUtil.ModInfo.versionToString(metadata.getVersion());
}
}
}

View file

@ -1,100 +1,62 @@
package org.betterx.bclib.util;
import net.fabricmc.loader.api.FabricLoader;
import java.io.File;
import java.nio.file.Path;
import java.util.function.Consumer;
/**
* @deprecated replaced by {@link org.betterx.worlds.together.util.PathUtil}
*/
@Deprecated(forRemoval = true)
public class PathUtil {
public final static Path GAME_FOLDER = FabricLoader.getInstance()
.getGameDir()
.normalize();
public final static Path MOD_FOLDER = FabricLoader.getInstance()
.getGameDir()
.resolve("mods")
.normalize();
public final static Path MOD_BAK_FOLDER = MOD_FOLDER.resolve("_bclib_deactivated")
.normalize();
/**
* @deprecated replaced by {@link org.betterx.worlds.together.util.PathUtil#GAME_FOLDER}
*/
@Deprecated(forRemoval = true)
public final static Path GAME_FOLDER = org.betterx.worlds.together.util.PathUtil.GAME_FOLDER;
/**
* Tests if a path is a child-path.
* <p>
* A path is a child of another if it is located in the parent or any of the parents subdirectories
*
* @param parent The folder we search for the {@code child}
* @param child The folder you want to test
* @return {@code true} if {@code child} is in {@code parent} or any of its sub directories
* @deprecated replaced by {@link org.betterx.worlds.together.util.PathUtil#MOD_FOLDER}
*/
@Deprecated(forRemoval = true)
public final static Path MOD_FOLDER = org.betterx.worlds.together.util.PathUtil.MOD_FOLDER;
/**
* @deprecated replaced by {@link org.betterx.worlds.together.util.PathUtil#MOD_BAK_FOLDER}
*/
@Deprecated(forRemoval = true)
public final static Path MOD_BAK_FOLDER = org.betterx.worlds.together.util.PathUtil.MOD_BAK_FOLDER;
/**
* @deprecated replaced by {@link org.betterx.worlds.together.util.PathUtil#isChildOf(Path, Path)}
*/
@Deprecated(forRemoval = true)
public static boolean isChildOf(Path parent, Path child) {
if (child == null || parent == null) return false;
parent = parent.toAbsolutePath().normalize();
child = child.toAbsolutePath().normalize();
final int pCount = parent.getNameCount();
final int cCount = child.getNameCount();
if (cCount > pCount) return isChildOf(parent, child.getParent());
if (cCount < pCount) return false;
return child.equals(parent);
return org.betterx.worlds.together.util.PathUtil.isChildOf(parent, child);
}
/**
* A simple directory walker that ignores dot-files
*
* @param path The path where you want to start
* @param pathConsumer The consumer called for each valid file. The consumer will get an absolute {@link Path}-Object
* for each visited file
* @deprecated replaced by {@link org.betterx.worlds.together.util.PathUtil#fileWalker(File, Consumer)}
*/
@Deprecated(forRemoval = true)
public static void fileWalker(File path, Consumer<Path> pathConsumer) {
fileWalker(path, true, pathConsumer);
org.betterx.worlds.together.util.PathUtil.fileWalker(path, pathConsumer);
}
/**
* A simple directory walker that ignores dot-files
*
* @param path The path where you want to start
* @param recursive if {@code false}, only the {@code path} is traversed
* @param pathConsumer The consumer called for each valid file. The consumer will get an absolute {@link Path}-Object
* for each visited file
* @deprecated replaced by {@link org.betterx.worlds.together.util.PathUtil#fileWalker(File, boolean, Consumer)}
*/
@Deprecated(forRemoval = true)
public static void fileWalker(File path, boolean recursive, Consumer<Path> pathConsumer) {
if (!path.exists()) return;
for (final File f : path.listFiles()) {
if (f.getName()
.startsWith(".")) continue;
if (f.isDirectory()) {
if (recursive) fileWalker(f, pathConsumer);
} else if (f.isFile()) {
pathConsumer.accept(f.toPath());
}
}
org.betterx.worlds.together.util.PathUtil.fileWalker(path, recursive, pathConsumer);
}
/**
* Creates a human readable File-Size
*
* @param size Filesize in bytes
* @return A Human readable String
* @deprecated replaced by {@link org.betterx.worlds.together.util.PathUtil#humanReadableFileSize(long)}
*/
@Deprecated(forRemoval = true)
public static String humanReadableFileSize(long size) {
final int threshold = 2;
final int factor = 1024;
if (size < 0) return "? Byte";
if (size < factor * threshold) {
return size + " Byte";
}
char[] units = {'K', 'M', 'G', 'T', 'P'};
int unitIndex = 0;
double fSize = size;
do {
unitIndex++;
fSize /= 1024;
} while (fSize > factor * threshold && unitIndex < units.length);
return String.format("%.1f %ciB", fSize, units[unitIndex - 1]);
return org.betterx.worlds.together.util.PathUtil.humanReadableFileSize(size);
}
}