# WARNING: head commit changed in the meantime

Fixed issues
This commit is contained in:
Speiger 2021-08-30 17:41:05 +02:00
commit d21fc2984c
9 changed files with 73 additions and 13 deletions

1
.gitignore vendored
View File

@ -5,3 +5,4 @@
build build
/bin/ /bin/
/.settings/ /.settings/
/.project

View File

@ -12,6 +12,7 @@ import java.util.zip.ZipFile;
import speiger.src.collections.objects.lists.ObjectArrayList; import speiger.src.collections.objects.lists.ObjectArrayList;
import speiger.src.collections.objects.maps.impl.hash.Object2ObjectLinkedOpenHashMap; import speiger.src.collections.objects.maps.impl.hash.Object2ObjectLinkedOpenHashMap;
import speiger.src.collections.objects.maps.interfaces.Object2ObjectMap;
import speiger.src.coreengine.assets.impl.FolderAssetPackage; import speiger.src.coreengine.assets.impl.FolderAssetPackage;
import speiger.src.coreengine.assets.impl.ZipAssetPackage; import speiger.src.coreengine.assets.impl.ZipAssetPackage;
import speiger.src.coreengine.assets.reloader.IReloadableResource; import speiger.src.coreengine.assets.reloader.IReloadableResource;
@ -19,6 +20,7 @@ import speiger.src.coreengine.assets.reloader.IReloadableResource;
public class AssetManager implements IReloadableResource public class AssetManager implements IReloadableResource
{ {
Map<String, DomainAssets> domains = new Object2ObjectLinkedOpenHashMap<>(); Map<String, DomainAssets> domains = new Object2ObjectLinkedOpenHashMap<>();
Object2ObjectMap<Class<?>, IAssetParser<?>> parsers = new Object2ObjectLinkedOpenHashMap<>();
Path path; Path path;
public AssetManager(File file) public AssetManager(File file)
@ -35,6 +37,12 @@ public class AssetManager implements IReloadableResource
{ {
return path; return path;
} }
public <T> void registerAssetParser(Class<T> clz, IAssetParser<T> parser)
{
if(clz == null || parser == null) return;
parsers.put(clz, parser);
}
@Override @Override
public void reload() public void reload()
{ {
@ -66,6 +74,7 @@ public class AssetManager implements IReloadableResource
} }
entry.addAssets(packages); entry.addAssets(packages);
} }
packages.setParsers(parsers);
} }
public Set<String> getDomains() public Set<String> getDomains()

View File

@ -21,4 +21,6 @@ public interface IAsset extends Closeable
public BufferedReader getStringReader() throws IOException; public BufferedReader getStringReader() throws IOException;
public JsonObject getJsonObject() throws IOException; public JsonObject getJsonObject() throws IOException;
public <T> T getCustom(Class<T> clz) throws IOException;
} }

View File

@ -1,10 +1,11 @@
package speiger.src.coreengine.assets; package speiger.src.coreengine.assets;
import java.util.List; import java.util.List;
import java.util.function.Function;
public interface IAssetPackage public interface IAssetPackage
{ {
public void setParsers(Function<Class<?>, IAssetParser<?>> parsers);
public List<String> getDomains(); public List<String> getDomains();
public IAsset getAsset(AssetLocation location); public IAsset getAsset(AssetLocation location);
} }

View File

@ -0,0 +1,10 @@
package speiger.src.coreengine.assets;
import java.io.Closeable;
import java.nio.file.Path;
import java.util.function.Consumer;
public interface IAssetParser<T>
{
public T parseAsset(Path path, Consumer<Closeable> autoCloser);
}

View File

@ -8,6 +8,7 @@ import java.io.InputStream;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.List; import java.util.List;
import java.util.function.Function;
import javax.imageio.ImageIO; import javax.imageio.ImageIO;
@ -16,23 +17,26 @@ import com.google.gson.JsonObject;
import speiger.src.collections.objects.lists.ObjectArrayList; import speiger.src.collections.objects.lists.ObjectArrayList;
import speiger.src.coreengine.assets.AssetLocation; import speiger.src.coreengine.assets.AssetLocation;
import speiger.src.coreengine.assets.IAsset; import speiger.src.coreengine.assets.IAsset;
import speiger.src.coreengine.assets.IAssetParser;
import speiger.src.coreengine.utils.helpers.JsonUtil; import speiger.src.coreengine.utils.helpers.JsonUtil;
public class FolderAsset implements IAsset public class FolderAsset implements IAsset
{ {
Function<Class<?>, IAssetParser<?>> parsers;
AssetLocation location; AssetLocation location;
Path path; Path path;
List<Closeable> closeable; List<Closeable> closeable;
public FolderAsset(AssetLocation location, Path path) public FolderAsset(AssetLocation location, Path path, Function<Class<?>, IAssetParser<?>> parsers)
{ {
this(location, path, new ObjectArrayList<>()); this(location, path, parsers, new ObjectArrayList<>());
} }
public FolderAsset(AssetLocation location, Path path, List<Closeable> closeable) public FolderAsset(AssetLocation location, Path path, Function<Class<?>, IAssetParser<?>> parsers, List<Closeable> closeable)
{ {
this.location = location; this.location = location;
this.path = path; this.path = path;
this.parsers = parsers;
this.closeable = closeable; this.closeable = closeable;
} }
@ -57,7 +61,7 @@ public class FolderAsset implements IAsset
public IAsset subAsset(String alternative) public IAsset subAsset(String alternative)
{ {
Path newPath = path.resolveSibling(path.getFileName().toString()+"."+alternative); Path newPath = path.resolveSibling(path.getFileName().toString()+"."+alternative);
return Files.exists(newPath) ? new FolderAsset(location.alternate(alternative), newPath, closeable) : null; return Files.exists(newPath) ? new FolderAsset(location.alternate(alternative), newPath, parsers, closeable) : null;
} }
@Override @Override
@ -90,6 +94,12 @@ public class FolderAsset implements IAsset
return JsonUtil.loadFile(path); return JsonUtil.loadFile(path);
} }
@Override
public <T> T getCustom(Class<T> clz) throws IOException
{
return clz.cast(parsers.apply(clz).parseAsset(path, closeable::add));
}
protected <T extends Closeable> T markClosed(T value) protected <T extends Closeable> T markClosed(T value)
{ {
closeable.add(value); closeable.add(value);

View File

@ -6,16 +6,18 @@ import java.nio.file.DirectoryStream;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.List; import java.util.List;
import java.util.function.Function;
import speiger.src.collections.objects.lists.ObjectArrayList; import speiger.src.collections.objects.lists.ObjectArrayList;
import speiger.src.coreengine.assets.AssetLocation; import speiger.src.coreengine.assets.AssetLocation;
import speiger.src.coreengine.assets.IAsset; import speiger.src.coreengine.assets.IAsset;
import speiger.src.coreengine.assets.IAssetPackage; import speiger.src.coreengine.assets.IAssetPackage;
import speiger.src.coreengine.assets.impl.FolderAsset; import speiger.src.coreengine.assets.IAssetParser;
public class FolderAssetPackage implements IAssetPackage public class FolderAssetPackage implements IAssetPackage
{ {
Path baseFolder; Path baseFolder;
Function<Class<?>, IAssetParser<?>> parsers;
public FolderAssetPackage(File baseFolder) public FolderAssetPackage(File baseFolder)
{ {
@ -27,6 +29,12 @@ public class FolderAssetPackage implements IAssetPackage
this.baseFolder = baseFolder; this.baseFolder = baseFolder;
} }
@Override
public void setParsers(Function<Class<?>, IAssetParser<?>> parsers)
{
this.parsers = parsers;
}
@Override @Override
public List<String> getDomains() public List<String> getDomains()
{ {
@ -49,6 +57,6 @@ public class FolderAssetPackage implements IAssetPackage
public IAsset getAsset(AssetLocation location) public IAsset getAsset(AssetLocation location)
{ {
Path path = baseFolder.resolve(location.getActualLocation()); Path path = baseFolder.resolve(location.getActualLocation());
return Files.exists(path) ? new FolderAsset(location, path) : null; return Files.exists(path) ? new FolderAsset(location, path, parsers) : null;
} }
} }

View File

@ -6,6 +6,7 @@ import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.function.Function;
import javax.imageio.ImageIO; import javax.imageio.ImageIO;
@ -13,6 +14,7 @@ import com.google.gson.JsonObject;
import speiger.src.coreengine.assets.AssetLocation; import speiger.src.coreengine.assets.AssetLocation;
import speiger.src.coreengine.assets.IAsset; import speiger.src.coreengine.assets.IAsset;
import speiger.src.coreengine.assets.IAssetParser;
import speiger.src.coreengine.utils.helpers.JsonUtil; import speiger.src.coreengine.utils.helpers.JsonUtil;
public class ZipAsset implements IAsset public class ZipAsset implements IAsset
@ -20,12 +22,14 @@ public class ZipAsset implements IAsset
Runnable closer; Runnable closer;
AssetLocation location; AssetLocation location;
Path path; Path path;
Function<Class<?>, IAssetParser<?>> parsers;
public ZipAsset(Runnable closer, AssetLocation location, Path path) public ZipAsset(Runnable closer, AssetLocation location, Path path, Function<Class<?>, IAssetParser<?>> parsers)
{ {
this.closer = closer; this.closer = closer;
this.location = location; this.location = location;
this.path = path; this.path = path;
this.parsers = parsers;
} }
@Override @Override
@ -52,7 +56,7 @@ public class ZipAsset implements IAsset
{ {
if(closer == null) return null; if(closer == null) return null;
Path newPath = path.resolveSibling(path.getFileName().toString()+"."+alternative); Path newPath = path.resolveSibling(path.getFileName().toString()+"."+alternative);
return Files.exists(newPath) ? new ZipAsset(this::closeInternal, location.alternate(alternative), newPath) : null; return Files.exists(newPath) ? new ZipAsset(this::closeInternal, location.alternate(alternative), newPath, parsers) : null;
} }
@Override @Override
@ -84,4 +88,10 @@ public class ZipAsset implements IAsset
{ {
return JsonUtil.loadFile(path); return JsonUtil.loadFile(path);
} }
@Override
public <T> T getCustom(Class<T> clz) throws IOException
{
return clz.cast(parsers.apply(clz).parseAsset(path, T -> {}));
}
} }

View File

@ -9,15 +9,18 @@ import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.List; import java.util.List;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Function;
import speiger.src.collections.objects.lists.ObjectArrayList; import speiger.src.collections.objects.lists.ObjectArrayList;
import speiger.src.coreengine.assets.AssetLocation; import speiger.src.coreengine.assets.AssetLocation;
import speiger.src.coreengine.assets.IAsset; import speiger.src.coreengine.assets.IAsset;
import speiger.src.coreengine.assets.IAssetPackage; import speiger.src.coreengine.assets.IAssetPackage;
import speiger.src.coreengine.assets.IAssetParser;
public class ZipAssetPackage implements IAssetPackage public class ZipAssetPackage implements IAssetPackage
{ {
Path baseFolder; Path baseFolder;
Function<Class<?>, IAssetParser<?>> parsers;
FileSystem cache; FileSystem cache;
AtomicInteger usedReferences = new AtomicInteger(); AtomicInteger usedReferences = new AtomicInteger();
Runnable referenceCleaner = this::cleanReference; Runnable referenceCleaner = this::cleanReference;
@ -32,6 +35,12 @@ public class ZipAssetPackage implements IAssetPackage
this.baseFolder = baseFolder; this.baseFolder = baseFolder;
} }
@Override
public void setParsers(Function<Class<?>, IAssetParser<?>> parsers)
{
this.parsers = parsers;
}
private void cleanReference() private void cleanReference()
{ {
int left = usedReferences.decrementAndGet(); int left = usedReferences.decrementAndGet();
@ -77,7 +86,7 @@ public class ZipAssetPackage implements IAssetPackage
try try
{ {
Path path = getReference().getPath(location.getActualLocation()); Path path = getReference().getPath(location.getActualLocation());
return Files.exists(path) ? new ZipAsset(referenceCleaner, location, path) : null; return Files.exists(path) ? new ZipAsset(referenceCleaner, location, path, parsers) : null;
} }
catch(Exception e) { return null; } catch(Exception e) { return null; }
} }