Added Custom Parsers for Assets

This commit is contained in:
Speiger 2021-08-30 17:38:11 +02:00
parent b00e330db9
commit 6258dc8c72
9 changed files with 86 additions and 26 deletions

View File

@ -1,22 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>SimpleJavaEngine</name>
<name>ModelLoader-SimpleJavaEngine</name>
<comment></comment>
<projects/>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.buildship.core.gradleprojectbuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.buildship.core.gradleprojectnature</nature>
</natures>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments/>
</buildCommand>
<buildCommand>
<name>org.eclipse.buildship.core.gradleprojectbuilder</name>
<arguments/>
</buildCommand>
</buildSpec>
<linkedResources/>
<filteredResources/>
</projectDescription>

View File

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

View File

@ -21,4 +21,6 @@ public interface IAsset extends Closeable
public BufferedReader getStringReader() 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;
import java.util.List;
import java.util.function.Function;
public interface IAssetPackage
{
public void setParsers(Function<Class<?>, IAssetParser<?>> parsers);
public List<String> getDomains();
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.Path;
import java.util.List;
import java.util.function.Function;
import javax.imageio.ImageIO;
@ -16,23 +17,26 @@ import com.google.gson.JsonObject;
import speiger.src.collections.objects.lists.ObjectArrayList;
import speiger.src.coreengine.assets.AssetLocation;
import speiger.src.coreengine.assets.IAsset;
import speiger.src.coreengine.assets.IAssetParser;
import speiger.src.coreengine.utils.helpers.JsonUtil;
public class FolderAsset implements IAsset
{
Function<Class<?>, IAssetParser<?>> parsers;
AssetLocation location;
Path path;
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.path = path;
this.parsers = parsers;
this.closeable = closeable;
}
@ -57,7 +61,7 @@ public class FolderAsset implements IAsset
public IAsset subAsset(String 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
@ -90,6 +94,12 @@ public class FolderAsset implements IAsset
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)
{
closeable.add(value);

View File

@ -6,16 +6,18 @@ import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.function.Function;
import speiger.src.collections.objects.lists.ObjectArrayList;
import speiger.src.coreengine.assets.AssetLocation;
import speiger.src.coreengine.assets.IAsset;
import speiger.src.coreengine.assets.IAssetPackage;
import speiger.src.coreengine.assets.impl.FolderAsset;
import speiger.src.coreengine.assets.IAssetParser;
public class FolderAssetPackage implements IAssetPackage
{
Path baseFolder;
Function<Class<?>, IAssetParser<?>> parsers;
public FolderAssetPackage(File baseFolder)
{
@ -27,6 +29,12 @@ public class FolderAssetPackage implements IAssetPackage
this.baseFolder = baseFolder;
}
@Override
public void setParsers(Function<Class<?>, IAssetParser<?>> parsers)
{
this.parsers = parsers;
}
@Override
public List<String> getDomains()
{
@ -49,6 +57,6 @@ public class FolderAssetPackage implements IAssetPackage
public IAsset getAsset(AssetLocation location)
{
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.nio.file.Files;
import java.nio.file.Path;
import java.util.function.Function;
import javax.imageio.ImageIO;
@ -13,6 +14,7 @@ import com.google.gson.JsonObject;
import speiger.src.coreengine.assets.AssetLocation;
import speiger.src.coreengine.assets.IAsset;
import speiger.src.coreengine.assets.IAssetParser;
import speiger.src.coreengine.utils.helpers.JsonUtil;
public class ZipAsset implements IAsset
@ -20,12 +22,14 @@ public class ZipAsset implements IAsset
Runnable closer;
AssetLocation location;
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.location = location;
this.path = path;
this.parsers = parsers;
}
@Override
@ -52,7 +56,7 @@ public class ZipAsset implements IAsset
{
if(closer == null) return null;
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
@ -84,4 +88,10 @@ public class ZipAsset implements IAsset
{
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.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Function;
import speiger.src.collections.objects.lists.ObjectArrayList;
import speiger.src.coreengine.assets.AssetLocation;
import speiger.src.coreengine.assets.IAsset;
import speiger.src.coreengine.assets.IAssetPackage;
import speiger.src.coreengine.assets.IAssetParser;
public class ZipAssetPackage implements IAssetPackage
{
Path baseFolder;
Function<Class<?>, IAssetParser<?>> parsers;
FileSystem cache;
AtomicInteger usedReferences = new AtomicInteger();
Runnable referenceCleaner = this::cleanReference;
@ -32,6 +35,12 @@ public class ZipAssetPackage implements IAssetPackage
this.baseFolder = baseFolder;
}
@Override
public void setParsers(Function<Class<?>, IAssetParser<?>> parsers)
{
this.parsers = parsers;
}
private void cleanReference()
{
int left = usedReferences.decrementAndGet();
@ -77,7 +86,7 @@ public class ZipAssetPackage implements IAssetPackage
try
{
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; }
}