Skip to content

Commit

Permalink
forward compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
PTOM76 committed Dec 11, 2022
1 parent 80b467d commit 137738e
Showing 1 changed file with 17 additions and 27 deletions.
44 changes: 17 additions & 27 deletions src/main/java/net/devtech/arrp/impl/RuntimeResourcePackImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,7 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
Expand Down Expand Up @@ -153,7 +148,7 @@ public class RuntimeResourcePackImpl implements RuntimeResourcePack, ResourcePac
private final Lock waiting = new ReentrantLock();
private final Map<Identifier, Supplier<byte[]>> data = new ConcurrentHashMap<>();
private final Map<Identifier, Supplier<byte[]>> assets = new ConcurrentHashMap<>();
private final Map<String, Supplier<byte[]>> root = new ConcurrentHashMap<>();
private final Map<List<String>, Supplier<byte[]>> root = new ConcurrentHashMap<>();
private final Map<Identifier, JLang> langMergable = new ConcurrentHashMap<>();

public RuntimeResourcePackImpl(Identifier id) {
Expand Down Expand Up @@ -243,7 +238,7 @@ public byte[] addResource(ResourceType type, Identifier path, byte[] data) {
@Override
public Future<byte[]> addAsyncRootResource(String path, CallableFunction<String, byte[]> data) {
Future<byte[]> future = EXECUTOR_SERVICE.submit(() -> data.get(path));
this.root.put(path, () -> {
this.root.put(Arrays.asList(path.split("/")), () -> {
try {
return future.get();
} catch(InterruptedException | ExecutionException e) {
Expand All @@ -255,12 +250,12 @@ public Future<byte[]> addAsyncRootResource(String path, CallableFunction<String,

@Override
public void addLazyRootResource(String path, BiFunction<RuntimeResourcePack, String, byte[]> data) {
this.root.put(path, new Memoized<>(data, path));
this.root.put(Arrays.asList(path.split("/")), new Memoized<>(data, path));
}

@Override
public byte[] addRootResource(String path, byte[] data) {
this.root.put(path, () -> data);
this.root.put(Arrays.asList(path.split("/")), () -> data);
return data;
}

Expand Down Expand Up @@ -324,8 +319,8 @@ public void dumpDirect(Path output) {
LOGGER.info("dumping " + this.id + "'s assets and data");
// data dump time
try {
for(Map.Entry<String, Supplier<byte[]>> e : this.root.entrySet()) {
Path root = output.resolve(e.getKey());
for(Map.Entry<List<String>, Supplier<byte[]>> e : this.root.entrySet()) {
Path root = output.resolve(String.join("/", e.getKey()));
Files.createDirectories(root.getParent());
Files.write(root, e.getValue().get());
}
Expand Down Expand Up @@ -359,7 +354,7 @@ public void load(Path dir) throws IOException {
this.load(path, this.data, Files.readAllBytes(file));
} else {
byte[] data = Files.readAllBytes(file);
this.root.put(s, () -> data);
this.root.put(Arrays.asList(s.split("/")), () -> data);
}
}
}
Expand All @@ -372,8 +367,8 @@ public void dump(File output) {
@Override
public void dump(ZipOutputStream zos) throws IOException {
this.lock();
for(Map.Entry<String, Supplier<byte[]>> entry : this.root.entrySet()) {
zos.putNextEntry(new ZipEntry(entry.getKey()));
for(Map.Entry<List<String>, Supplier<byte[]>> entry : this.root.entrySet()) {
zos.putNextEntry(new ZipEntry(String.join("/", entry.getKey())));
zos.write(entry.getValue().get());
zos.closeEntry();
}
Expand Down Expand Up @@ -407,7 +402,7 @@ public void load(ZipInputStream stream) throws IOException {
this.load(path, this.data, this.read(entry, stream));
} else {
byte[] data = this.read(entry, stream);
this.root.put(s, () -> data);
this.root.put(Arrays.asList(s.split("/")), () -> data);
}
}
}
Expand All @@ -425,19 +420,14 @@ public Identifier getId() {
*/
@Override
public InputSupplier<InputStream> openRoot(String... segments) {
if(segments.length == 1) {
String fileName = segments[0];
this.lock();
Supplier<byte[]> supplier = this.root.get(fileName);
if(supplier == null) {
this.waiting.unlock();
return null;
}
this.lock();
Supplier<byte[]> supplier = this.root.get(Arrays.asList(segments));
if(supplier == null) {
this.waiting.unlock();
return () -> new ByteArrayInputStream(supplier.get());
} else {
throw new IllegalArgumentException("File name can't be a path");
return null;
}
this.waiting.unlock();
return () -> new ByteArrayInputStream(supplier.get());
}

@Override
Expand Down

0 comments on commit 137738e

Please sign in to comment.