Skip to content

Commit d28a3b0

Browse files
committed
Some more refactoring
1 parent 2980fd2 commit d28a3b0

File tree

7 files changed

+147
-91
lines changed

7 files changed

+147
-91
lines changed

src/main/java/fr/catcore/modremapperapi/remapping/RemapUtil.java

+23-31
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import org.jetbrains.annotations.ApiStatus;
2626

2727
import java.io.*;
28-
import java.net.URL;
2928
import java.nio.file.Files;
3029
import java.nio.file.Path;
3130
import java.util.*;
@@ -49,8 +48,6 @@ public class RemapUtil {
4948
public static void init(List<io.github.fabriccompatibiltylayers.modremappingapi.api.v1.ModRemapper> modRemappers) {
5049
remappers = modRemappers;
5150

52-
downloadRemappingLibs();
53-
5451
for (ModRemapper remapper : remappers) {
5552
Optional<String> pkg = remapper.getDefaultPackage();
5653

@@ -65,6 +62,18 @@ public static void init(List<io.github.fabriccompatibiltylayers.modremappingapi.
6562
mappings.ifPresent(inputStreamSupplier -> MappingsUtilsImpl.loadExtraMappings(inputStreamSupplier.get()));
6663
}
6764

65+
Path sourceLibraryPath = CacheUtils.getLibraryPath(MappingsUtilsImpl.getSourceNamespace());
66+
67+
if (!Files.exists(sourceLibraryPath)) {
68+
try {
69+
Files.createDirectories(sourceLibraryPath);
70+
} catch (IOException e) {
71+
throw new RuntimeException(e);
72+
}
73+
}
74+
75+
downloadRemappingLibs();
76+
6877
MINECRAFT_TREE = MappingsUtilsImpl.getMinecraftMappings();
6978

7079
writeMcMappings();
@@ -95,37 +104,20 @@ private static void downloadRemappingLibs() {
95104

96105
remapper.addRemapLibraries(libraries, FabricLoader.getInstance().getEnvironmentType());
97106

98-
for (RemapLibrary library : libraries) {
99-
File libPath = CacheUtils.getLibraryPath(library.fileName).toFile();
107+
Map<RemapLibrary, Path> libraryPaths = CacheUtils.computeExtraLibraryPaths(libraries, MappingsUtilsImpl.getSourceNamespace());
108+
109+
for (Map.Entry<RemapLibrary, Path> entry : libraryPaths.entrySet()) {
110+
RemapLibrary library = entry.getKey();
111+
Path path = entry.getValue();
100112

101-
if (!libPath.exists() && !library.url.isEmpty()) {
113+
if (!library.url.isEmpty()) {
102114
Constants.MAIN_LOGGER.info("Downloading remapping library '" + library.fileName + "' from url '" + library.url + "'");
103-
try (BufferedInputStream inputStream = new BufferedInputStream(new URL(library.url).openStream())) {
104-
try (BufferedOutputStream outputStream = new BufferedOutputStream(Files.newOutputStream(libPath.toPath()))) {
105-
byte[] buffer = new byte[2048];
106-
107-
// Increments file size
108-
int length;
109-
int downloaded = 0;
110-
111-
// Looping until server finishes
112-
while ((length = inputStream.read(buffer)) != -1) {
113-
// Writing data
114-
outputStream.write(buffer, 0, length);
115-
downloaded += length;
116-
// Constants.MAIN_LOGGER.debug("Download Status: " + (downloaded * 100) / (contentLength * 1.0) + "%");
117-
}
118-
119-
outputStream.close();
120-
inputStream.close();
121-
}
122-
}
123-
124-
FileUtils.excludeFromZipFile(libPath, library.toExclude);
115+
io.github.fabriccompatibiltylayers.modremappingapi.impl.utils.FileUtils.downloadFile(library.url, path);
116+
FileUtils.excludeFromZipFile(path.toFile(), library.toExclude);
125117
Constants.MAIN_LOGGER.info("Remapping library ready for use.");
126-
} else if (!libPath.exists() && library.path != null) {
118+
} else if (library.path != null) {
127119
Constants.MAIN_LOGGER.info("Extracting remapping library '" + library.fileName + "' from mod jar.");
128-
FileUtils.copyFile(library.path, libPath.toPath());
120+
io.github.fabriccompatibiltylayers.modremappingapi.impl.utils.FileUtils.copyZipFile(library.path, path);
129121
Constants.MAIN_LOGGER.info("Remapping library ready for use.");
130122
}
131123
}
@@ -346,7 +338,7 @@ private static void preloadClasses() {
346338
"io.github.fabriccompatibiltylayers.modremappingapi.impl.ModDiscoverer",
347339
"io.github.fabriccompatibiltylayers.modremappingapi.impl.ModDiscoverer$1",
348340
"io.github.fabriccompatibiltylayers.modremappingapi.impl.ModEntry",
349-
"fr.catcore.modremapperapi.utils.RefmapJson",
341+
"io.github.fabriccompatibiltylayers.modremappingapi.impl.remapper.resource.RefmapJson",
350342
"fr.catcore.modremapperapi.remapping.MapEntryType",
351343
"fr.catcore.modremapperapi.remapping.MappingBuilder",
352344
"fr.catcore.modremapperapi.remapping.MappingBuilder$Entry",

src/main/java/fr/catcore/modremapperapi/utils/FileUtils.java

+4-27
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
import java.util.zip.ZipOutputStream;
1414

1515
public class FileUtils {
16-
16+
@Deprecated
1717
public static void writeTextFile(Collection<String> lines, File file) {
1818
file.getParentFile().mkdirs();
1919
try {
@@ -30,6 +30,7 @@ public static void writeTextFile(Collection<String> lines, File file) {
3030
}
3131
}
3232

33+
@Deprecated
3334
public static List<String> readTextSource(String path) {
3435
List<String> result = new ArrayList<>();
3536
try {
@@ -90,32 +91,8 @@ public static void excludeFromZipFile(File file, List<String> excluded) throws I
9091
tempFile.delete();
9192
}
9293

94+
@Deprecated
9395
public static void copyFile(Path original, Path copy) throws IOException {
94-
copy.toFile().delete();
95-
96-
ZipInputStream zin = new ZipInputStream(Files.newInputStream(original));
97-
ZipOutputStream zout = new ZipOutputStream(Files.newOutputStream(copy));
98-
99-
ZipEntry entry = zin.getNextEntry();
100-
byte[] buf = new byte[1024];
101-
102-
while (entry != null) {
103-
String zipEntryName = entry.getName();
104-
105-
zout.putNextEntry(new ZipEntry(zipEntryName));
106-
// Transfer bytes from the ZIP file to the output file
107-
int len;
108-
while ((len = zin.read(buf)) > 0) {
109-
zout.write(buf, 0, len);
110-
}
111-
112-
entry = zin.getNextEntry();
113-
}
114-
115-
// Close the streams
116-
zin.close();
117-
// Compress the files
118-
// Complete the ZIP file
119-
zout.close();
96+
io.github.fabriccompatibiltylayers.modremappingapi.impl.utils.FileUtils.copyZipFile(original, copy);
12097
}
12198
}

src/main/java/io/github/fabriccompatibiltylayers/modremappingapi/impl/remapper/minecraft/MinecraftRemapper.java

+4-30
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import io.github.fabriccompatibiltylayers.modremappingapi.impl.MappingsUtilsImpl;
66
import io.github.fabriccompatibiltylayers.modremappingapi.impl.RemapUtils;
77
import io.github.fabriccompatibiltylayers.modremappingapi.impl.utils.CacheUtils;
8+
import io.github.fabriccompatibiltylayers.modremappingapi.impl.utils.FileUtils;
89
import net.fabricmc.loader.api.FabricLoader;
910
import net.fabricmc.tinyremapper.NonClassCopyMode;
1011
import net.fabricmc.tinyremapper.OutputConsumerPath;
@@ -15,7 +16,6 @@
1516
import java.nio.file.Files;
1617
import java.nio.file.Path;
1718
import java.util.*;
18-
import java.util.stream.Collectors;
1919

2020
@ApiStatus.Internal
2121
public class MinecraftRemapper {
@@ -26,11 +26,11 @@ private static Collection<Path> getMinecraftJar(Collection<Path> sourcePaths, St
2626
Files.createDirectories(targetFolder);
2727
}
2828

29-
Map<Path, Path> paths = computeLibraryPaths(sourcePaths, target);
29+
Map<Path, Path> paths = CacheUtils.computeLibraryPaths(sourcePaths, target);
3030

31-
if (exist(paths.values())) return paths.values();
31+
if (FileUtils.exist(paths.values())) return paths.values();
3232

33-
delete(paths.values());
33+
FileUtils.delete(paths.values());
3434

3535
TinyRemapper.Builder builder = TinyRemapper
3636
.newRemapper()
@@ -58,32 +58,6 @@ private static Collection<Path> getMinecraftJar(Collection<Path> sourcePaths, St
5858
return paths.values();
5959
}
6060

61-
@ApiStatus.Internal
62-
public static Map<Path, Path> computeLibraryPaths(Collection<Path> sourcePaths, String target) {
63-
return sourcePaths.stream().collect(Collectors.toMap(p -> p,
64-
p -> CacheUtils.getLibraryPath(target).resolve(p.toFile().getName())));
65-
}
66-
67-
@ApiStatus.Internal
68-
public static boolean exist(Collection<Path> paths) {
69-
for (Path path : paths) {
70-
if (!Files.exists(path)) return false;
71-
}
72-
73-
return true;
74-
}
75-
76-
@ApiStatus.Internal
77-
public static void delete(Collection<Path> paths) {
78-
for (Path path : paths) {
79-
try {
80-
Files.deleteIfExists(path);
81-
} catch (IOException e) {
82-
e.printStackTrace();
83-
}
84-
}
85-
}
86-
8761
@ApiStatus.Internal
8862
public static void addMinecraftJar(TinyRemapper remapper) throws IOException {
8963
Collection<Path> classPath;

src/main/java/fr/catcore/modremapperapi/utils/RefmapJson.java renamed to src/main/java/io/github/fabriccompatibiltylayers/modremappingapi/impl/remapper/resource/RefmapJson.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
package fr.catcore.modremapperapi.utils;
1+
package io.github.fabriccompatibiltylayers.modremappingapi.impl.remapper.resource;
22

3-
import io.github.fabriccompatibiltylayers.modremappingapi.impl.remapper.resource.RefmapRemapper;
43
import net.fabricmc.tinyremapper.TinyRemapper;
54

65
import java.util.HashMap;

src/main/java/io/github/fabriccompatibiltylayers/modremappingapi/impl/remapper/resource/RefmapRemapper.java

-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import com.google.gson.Gson;
44
import fr.catcore.modremapperapi.remapping.RemapUtil;
5-
import fr.catcore.modremapperapi.utils.RefmapJson;
65
import net.fabricmc.tinyremapper.OutputConsumerPath;
76
import net.fabricmc.tinyremapper.TinyRemapper;
87
import net.fabricmc.tinyremapper.api.TrRemapper;

src/main/java/io/github/fabriccompatibiltylayers/modremappingapi/impl/utils/CacheUtils.java

+23
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
package io.github.fabriccompatibiltylayers.modremappingapi.impl.utils;
22

3+
import io.github.fabriccompatibiltylayers.modremappingapi.api.v1.RemapLibrary;
34
import net.fabricmc.loader.api.FabricLoader;
45
import org.jetbrains.annotations.ApiStatus;
56

7+
import java.io.IOException;
8+
import java.nio.file.Files;
69
import java.nio.file.Path;
10+
import java.util.Collection;
11+
import java.util.Map;
12+
import java.util.stream.Collectors;
713

814
@ApiStatus.Internal
915
public class CacheUtils {
@@ -21,6 +27,23 @@ public static Path getLibraryPath(String pathName) {
2127
return LIBRARY_FOLDER.resolve(pathName);
2228
}
2329

30+
@ApiStatus.Internal
31+
public static Map<Path, Path> computeLibraryPaths(Collection<Path> sourcePaths, String target) {
32+
return sourcePaths.stream().collect(Collectors.toMap(p -> p,
33+
p -> CacheUtils.getLibraryPath(target).resolve(p.toFile().getName())));
34+
}
35+
36+
@ApiStatus.Internal
37+
public static Map<RemapLibrary, Path> computeExtraLibraryPaths(Collection<RemapLibrary> sourcePaths, String target) {
38+
return sourcePaths.stream()
39+
.collect(Collectors.toMap(p -> p,
40+
p -> CacheUtils.getLibraryPath(target).resolve(p.fileName)))
41+
.entrySet()
42+
.stream()
43+
.filter(entry -> !Files.exists(entry.getValue()))
44+
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
45+
}
46+
2447
static {
2548
BASE_FOLDER.toFile().mkdirs();
2649
MAIN_FOLDER.toFile().mkdirs();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package io.github.fabriccompatibiltylayers.modremappingapi.impl.utils;
2+
3+
import org.jetbrains.annotations.ApiStatus;
4+
5+
import java.io.BufferedInputStream;
6+
import java.io.BufferedOutputStream;
7+
import java.io.IOException;
8+
import java.net.URL;
9+
import java.nio.file.Files;
10+
import java.nio.file.Path;
11+
import java.util.Collection;
12+
import java.util.zip.ZipEntry;
13+
import java.util.zip.ZipInputStream;
14+
import java.util.zip.ZipOutputStream;
15+
16+
@ApiStatus.Internal
17+
public class FileUtils {
18+
19+
@ApiStatus.Internal
20+
public static boolean exist(Collection<Path> paths) {
21+
for (Path path : paths) {
22+
if (!Files.exists(path)) return false;
23+
}
24+
25+
return true;
26+
}
27+
28+
@ApiStatus.Internal
29+
public static void delete(Collection<Path> paths) {
30+
for (Path path : paths) {
31+
try {
32+
Files.deleteIfExists(path);
33+
} catch (IOException e) {
34+
e.printStackTrace();
35+
}
36+
}
37+
}
38+
39+
@ApiStatus.Internal
40+
public static void downloadFile(String url, Path target) throws IOException {
41+
try (BufferedInputStream inputStream = new BufferedInputStream(new URL(url).openStream())) {
42+
try (BufferedOutputStream outputStream = new BufferedOutputStream(Files.newOutputStream(target))) {
43+
byte[] buffer = new byte[2048];
44+
45+
// Increments file size
46+
int length;
47+
int downloaded = 0;
48+
49+
// Looping until server finishes
50+
while ((length = inputStream.read(buffer)) != -1) {
51+
// Writing data
52+
outputStream.write(buffer, 0, length);
53+
downloaded += length;
54+
// Constants.MAIN_LOGGER.debug("Download Status: " + (downloaded * 100) / (contentLength * 1.0) + "%");
55+
}
56+
57+
outputStream.close();
58+
inputStream.close();
59+
}
60+
}
61+
}
62+
63+
@ApiStatus.Internal
64+
public static void copyZipFile(Path original, Path target) throws IOException {
65+
target.toFile().delete();
66+
67+
ZipInputStream zin = new ZipInputStream(Files.newInputStream(original));
68+
ZipOutputStream zout = new ZipOutputStream(Files.newOutputStream(target));
69+
70+
ZipEntry entry = zin.getNextEntry();
71+
byte[] buf = new byte[1024];
72+
73+
while (entry != null) {
74+
String zipEntryName = entry.getName();
75+
76+
zout.putNextEntry(new ZipEntry(zipEntryName));
77+
// Transfer bytes from the ZIP file to the output file
78+
int len;
79+
while ((len = zin.read(buf)) > 0) {
80+
zout.write(buf, 0, len);
81+
}
82+
83+
entry = zin.getNextEntry();
84+
}
85+
86+
// Close the streams
87+
zin.close();
88+
// Compress the files
89+
// Complete the ZIP file
90+
zout.close();
91+
}
92+
}

0 commit comments

Comments
 (0)