Skip to content

Commit

Permalink
removal of commons-io: 1/2
Browse files Browse the repository at this point in the history
- removal of all reliance on its dependencies
  • Loading branch information
SrRapero720 committed Aug 21, 2024
1 parent dd6c48c commit 7bcab68
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 31 deletions.
5 changes: 2 additions & 3 deletions src/main/java/me/srrapero720/watermedia/Main.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package me.srrapero720.watermedia;

import me.srrapero720.watermedia.api.math.MathAPI;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.comparator.LastModifiedFileComparator;
import me.srrapero720.watermedia.tools.IOTool;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
Expand Down Expand Up @@ -221,7 +220,7 @@ public void run() {
File wroot = root.resolve("watermedia_diagnosis").toFile().getAbsoluteFile();

if (wroot.exists()) {
FileUtils.deleteDirectory(wroot);
IOTool.rmdirs(wroot);
}
if (!wroot.exists() && !wroot.mkdirs()) {
throw new IOException("Cannot mkdir collected files dir");
Expand Down
16 changes: 8 additions & 8 deletions src/main/java/me/srrapero720/watermedia/WaterMedia.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import me.srrapero720.watermedia.tools.JarTool;
import me.srrapero720.watermedia.loader.ILoader;
import me.srrapero720.watermedia.loader.IModule;
import org.apache.commons.lang3.tuple.Pair;
import me.srrapero720.watermedia.tools.PairTool;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.Marker;
Expand All @@ -22,8 +22,8 @@ public final class WaterMedia {
public static final Logger LOGGER = LogManager.getLogger(ID);
private static final Set<IModule> MODULES = new HashSet<>();

private static final Pair<String, Boolean> NO_BOOT = getArgument("watermedia.disableBoot");
private static final Pair<String, Boolean> HARDFAIL = getArgument("watermedia.hardFail");
private static final PairTool<String, Boolean> NO_BOOT = getArgument("watermedia.disableBoot");
private static final PairTool<String, Boolean> HARDFAIL = getArgument("watermedia.hardFail");
private static ILoader bootstrap;
private static WaterMedia instance;

Expand Down Expand Up @@ -84,8 +84,8 @@ public static String asResource(String path) {
return WaterMedia.ID + ":" + path;
}

public static Pair<String, Boolean> getArgument(String argument) {
return Pair.of(argument, Boolean.parseBoolean(System.getProperty(argument)));
public static PairTool<String, Boolean> getArgument(String argument) {
return new PairTool<>(argument, Boolean.parseBoolean(System.getProperty(argument)));
}

public static class UnsupportedSideException extends RuntimeException {
Expand All @@ -103,10 +103,10 @@ public static class UnsupportedTLException extends Exception {
public UnsupportedTLException() {
super("TLauncher is NOT supported by " + NAME + ", please stop using it (and consider safe alternatives like SKLauncher or MultiMC)");
LOGGER.fatal(IT, "############################## ILLEGAL LAUNCHER DETECTED ######################################");
LOGGER.fatal(IT, "{} refuses to load sensitive modules in a INFECTED launcher, please stop using TLauncher", NAME);
LOGGER.fatal(IT, "{} refuses to load sensitive modules in a INFECTED launcher, please stop using TLauncher dammit", NAME);
LOGGER.fatal(IT, "Because TLauncher infects sensitive files (which {} includes) and we prefer avoid any risk", NAME);
LOGGER.fatal(IT, "Consider use safe alternative like SKLauncher, MultiMC or Buy the game and use original launcher");
LOGGER.fatal(IT, "And please avoid Feather Launcher, TLauncher Legacy or any CRACKED LAUNCHER (except SKLauncher)");
LOGGER.fatal(IT, "Consider use safe alternative like SKLauncher or BUY the game and use the CurseForge Launcher");
LOGGER.fatal(IT, "And please avoid Feather Launcher, TLauncher Legacy or any CRACKED LAUNCHER WITH A BAD REPUTATION");
LOGGER.fatal(IT, "############################## ILLEGAL LAUNCHER DETECTED ######################################");
}
}
Expand Down
21 changes: 3 additions & 18 deletions src/main/java/me/srrapero720/watermedia/api/player/PlayerAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,16 @@
import me.srrapero720.watermedia.WaterMedia;
import me.srrapero720.watermedia.api.WaterMediaAPI;
import me.srrapero720.watermedia.api.player.vlc.SimplePlayer;
import me.srrapero720.watermedia.tools.DataTool;
import me.srrapero720.watermedia.tools.IOTool;
import me.srrapero720.watermedia.tools.JarTool;
import me.srrapero720.watermedia.loader.ILoader;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.file.PathUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.logging.log4j.Marker;
import org.apache.logging.log4j.MarkerManager;
import uk.co.caprica.vlcj.factory.MediaPlayerFactory;
import uk.co.caprica.vlcj.discovery.NativeDiscovery;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.*;

Expand Down Expand Up @@ -130,7 +126,7 @@ public boolean prepare(ILoader bootCore) throws Exception {
if (zipOutput.getParentFile().exists()) {
LOGGER.warn(IT, "Detected an old installation, cleaning up...");
try {
FileUtils.deleteDirectory(zipOutput.getParentFile());
IOTool.rmdirs(zipOutput.getParentFile());
} catch (Exception e) {
LOGGER.error(IT, "Failed to delete directories", e);
}
Expand Down Expand Up @@ -164,21 +160,10 @@ public void start(ILoader bootCore) throws Exception {
}
}

// LOGGER INIT
LOGGER.info(IT, "Processing VideoLAN log files...");
if (Files.exists(dir.resolve("logs/videolan.log"))) {
Path parent = dir.resolve("logs");
try {
PathUtils.deleteDirectory(parent);
} catch (IOException e) {
LOGGER.warn(IT, "Failed to delete VLC logs directory", e);
}
}

try {
String[] args = JarTool.readArray("videolan/arguments.json");
registerFactory(WaterMedia.asResource("default"), args);
registerFactory(WaterMedia.asResource("sound_only"), ArrayUtils.addAll(args, "--vout=none"));
registerFactory(WaterMedia.asResource("sound_only"), DataTool.concatArray(args, "--vout=none"));

Runtime.getRuntime().addShutdownHook(new Thread(this::release));
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import me.srrapero720.watermedia.api.image.ImageRenderer;
import me.srrapero720.watermedia.api.rendering.memory.MemoryAlloc;
import me.srrapero720.watermedia.loader.ILoader;
import org.apache.commons.lang3.NotImplementedException;
import org.apache.logging.log4j.Marker;
import org.apache.logging.log4j.MarkerManager;
import org.lwjgl.opengl.GL11;
Expand Down Expand Up @@ -37,7 +36,7 @@ public static ByteBuffer createByteBuffer(int size) {
/**
* Resizes direct buffer unsafe using {@link org.lwjgl.system.MemoryUtil.MemoryAllocator MemoryAllocator}
*
* <p>In case class was missing causes a {@link NotImplementedException}</p>
* <p>In case class was missing causes a {@link UnsupportedOperationException}</p>
* @param buffer buffer to be resized
* @param newSize new size of the buffer
* @return resized DirectByteBuffer
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/me/srrapero720/watermedia/tools/DataTool.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
Expand Down Expand Up @@ -34,6 +35,13 @@ public static int[] filterValue(int[] its, int v) {
return result;
}

public static <T> T[] concatArray(T[] array, T... values) {
Object t = Array.newInstance(array.getClass().getComponentType(), array.length + values.length);
System.arraycopy(array, 0, t, 0, array.length);
System.arraycopy(values, 0, t, array.length, values.length);
return (T[]) t;
}

public static <T> List<T> toList(ServiceLoader<T> s) {
List<T> r = new ArrayList<>();
for (T t: s) r.add(t);
Expand Down
16 changes: 16 additions & 0 deletions src/main/java/me/srrapero720/watermedia/tools/IOTool.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,22 @@ public static GifDecoder readGif(Path path) {
return null;
}

public static boolean rmdirs(Path path) {
return rmdirs(path.toFile());
}

public static boolean rmdirs(File root) {
File[] files = root.listFiles();

if (files == null || files.length == 0) return root.delete();
for (File f: files) {
File[] childs = f.listFiles();
if (childs != null && childs.length != 0 && !rmdirs(f)) return false;
if (!f.delete()) return false;
}
return true;
}

public static void un7zip(Marker it, Path zipFilePath) throws IOException { un7zip(it, zipFilePath, zipFilePath.getParent()); }
public static void un7zip(Marker it, Path zipFilePath, Path destDirectory) throws IOException {
LOGGER.debug(it, "Un7zipping file from '{}' to directory '{}'", zipFilePath, destDirectory);
Expand Down
34 changes: 34 additions & 0 deletions src/main/java/me/srrapero720/watermedia/tools/PairTool.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package me.srrapero720.watermedia.tools;

import java.util.function.Supplier;

public class PairTool<K, V> implements Supplier<V> {

private final K left;
private final V right;
public PairTool(K left, V right) {
this.left = left;
this.right = right;
}

public K getLeft() {
return left;
}

public V getRight() {
return right;
}

public K getKey() {
return left;
}

public V getValue() {
return right;
}

@Override
public V get() {
return right;
}
}

0 comments on commit 7bcab68

Please sign in to comment.