mirror of
https://github.com/VolmitSoftware/Iris.git
synced 2025-07-18 18:23:06 +00:00
Data loader abstraction
This commit is contained in:
parent
eb82b44726
commit
b02f61ef21
@ -22,6 +22,7 @@ import com.volmit.iris.Iris;
|
||||
import com.volmit.iris.engine.data.loader.ObjectResourceLoader;
|
||||
import com.volmit.iris.engine.data.loader.ResourceLoader;
|
||||
import com.volmit.iris.engine.object.*;
|
||||
import com.volmit.iris.util.collection.KMap;
|
||||
import com.volmit.iris.util.math.RNG;
|
||||
import lombok.Data;
|
||||
|
||||
@ -43,7 +44,9 @@ public class IrisDataManager {
|
||||
private ResourceLoader<IrisSpawner> spawnerLoader;
|
||||
private ResourceLoader<IrisMod> modLoader;
|
||||
private ResourceLoader<IrisBlockData> blockLoader;
|
||||
private ObjectResourceLoader objectLoader;
|
||||
private ResourceLoader<IrisExpression> expressionLoader;
|
||||
private ResourceLoader<IrisObject> objectLoader;
|
||||
private KMap<Class<? extends IrisRegistrant>, ResourceLoader<? extends IrisRegistrant>> loaders = new KMap<>();
|
||||
private boolean closed;
|
||||
private final File dataFolder;
|
||||
private final int id;
|
||||
@ -62,19 +65,7 @@ public class IrisDataManager {
|
||||
public void close() {
|
||||
closed = true;
|
||||
dump();
|
||||
this.lootLoader = null;
|
||||
this.entityLoader = null;
|
||||
this.regionLoader = null;
|
||||
this.biomeLoader = null;
|
||||
this.modLoader = null;
|
||||
this.dimensionLoader = null;
|
||||
this.spawnerLoader = null;
|
||||
this.jigsawPoolLoader = null;
|
||||
this.jigsawPieceLoader = null;
|
||||
this.generatorLoader = null;
|
||||
this.jigsawStructureLoader = null;
|
||||
this.blockLoader = null;
|
||||
this.objectLoader = null;
|
||||
loaders.clear();
|
||||
}
|
||||
|
||||
private static void printData(ResourceLoader<?> rl) {
|
||||
@ -85,45 +76,68 @@ public class IrisDataManager {
|
||||
return new IrisDataManager(dataFolder);
|
||||
}
|
||||
|
||||
public void hotloaded() {
|
||||
private <T extends IrisRegistrant> ResourceLoader<T> registerLoader(Class<T> registrant)
|
||||
{
|
||||
try
|
||||
{
|
||||
IrisRegistrant rr = registrant.getConstructor().newInstance();
|
||||
ResourceLoader<T> r = null;
|
||||
if(registrant.equals(IrisObject.class))
|
||||
{
|
||||
r = (ResourceLoader<T>) new ObjectResourceLoader(dataFolder, this, rr.getFolderName(), rr.getTypeName());
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
r = new ResourceLoader<T>(dataFolder, this, rr.getFolderName(), rr.getTypeName(), registrant);
|
||||
}
|
||||
|
||||
loaders.put(registrant, r);
|
||||
|
||||
return r;
|
||||
}
|
||||
|
||||
catch(Throwable e)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public synchronized void hotloaded() {
|
||||
if (closed) {
|
||||
return;
|
||||
}
|
||||
|
||||
loaders.clear();
|
||||
File packs = dataFolder;
|
||||
packs.mkdirs();
|
||||
this.lootLoader = new ResourceLoader<>(packs, this, "loot", "Loot", IrisLootTable.class);
|
||||
this.spawnerLoader = new ResourceLoader<>(packs, this, "spawners", "Spawner", IrisSpawner.class);
|
||||
this.entityLoader = new ResourceLoader<>(packs, this, "entities", "Entity", IrisEntity.class);
|
||||
this.regionLoader = new ResourceLoader<>(packs, this, "regions", "Region", IrisRegion.class);
|
||||
this.biomeLoader = new ResourceLoader<>(packs, this, "biomes", "Biome", IrisBiome.class);
|
||||
this.modLoader = new ResourceLoader<>(packs, this, "mods", "Mod", IrisMod.class);
|
||||
this.dimensionLoader = new ResourceLoader<>(packs, this, "dimensions", "Dimension", IrisDimension.class);
|
||||
this.jigsawPoolLoader = new ResourceLoader<>(packs, this, "jigsaw-pools", "Jigsaw Pool", IrisJigsawPool.class);
|
||||
this.jigsawStructureLoader = new ResourceLoader<>(packs, this, "jigsaw-structures", "Jigsaw Structure", IrisJigsawStructure.class);
|
||||
this.jigsawPieceLoader = new ResourceLoader<>(packs, this, "jigsaw-pieces", "Jigsaw Piece", IrisJigsawPiece.class);
|
||||
this.generatorLoader = new ResourceLoader<>(packs, this, "generators", "Generator", IrisGenerator.class);
|
||||
this.blockLoader = new ResourceLoader<>(packs, this, "blocks", "Block", IrisBlockData.class);
|
||||
this.objectLoader = new ObjectResourceLoader(packs, this, "objects", "Object");
|
||||
this.lootLoader = registerLoader(IrisLootTable.class);
|
||||
this.spawnerLoader = registerLoader(IrisSpawner.class);
|
||||
this.entityLoader = registerLoader(IrisEntity.class);
|
||||
this.regionLoader = registerLoader(IrisRegion.class);
|
||||
this.biomeLoader = registerLoader(IrisBiome.class);
|
||||
this.modLoader = registerLoader(IrisMod.class);
|
||||
this.dimensionLoader = registerLoader(IrisDimension.class);
|
||||
this.jigsawPoolLoader = registerLoader(IrisJigsawPool.class);
|
||||
this.jigsawStructureLoader = registerLoader(IrisJigsawStructure.class);
|
||||
this.jigsawPieceLoader = registerLoader(IrisJigsawPiece.class);
|
||||
this.generatorLoader = registerLoader(IrisGenerator.class);
|
||||
this.blockLoader = registerLoader(IrisBlockData.class);
|
||||
this.expressionLoader = registerLoader(IrisExpression.class);
|
||||
this.objectLoader = registerLoader(IrisObject.class);
|
||||
}
|
||||
|
||||
public void dump() {
|
||||
if (closed) {
|
||||
return;
|
||||
}
|
||||
biomeLoader.clearCache();
|
||||
blockLoader.clearCache();
|
||||
lootLoader.clearCache();
|
||||
objectLoader.clearCache();
|
||||
spawnerLoader.clearCache();
|
||||
jigsawPieceLoader.clearCache();
|
||||
jigsawPoolLoader.clearCache();
|
||||
modLoader.clearCache();
|
||||
jigsawStructureLoader.clearCache();
|
||||
regionLoader.clearCache();
|
||||
dimensionLoader.clearCache();
|
||||
entityLoader.clearCache();
|
||||
generatorLoader.clearCache();
|
||||
|
||||
for(ResourceLoader<?> i : loaders.values())
|
||||
{
|
||||
i.clearCache();
|
||||
}
|
||||
}
|
||||
|
||||
public void clearLists() {
|
||||
@ -131,19 +145,10 @@ public class IrisDataManager {
|
||||
return;
|
||||
}
|
||||
|
||||
lootLoader.clearList();
|
||||
blockLoader.clearList();
|
||||
entityLoader.clearList();
|
||||
biomeLoader.clearList();
|
||||
modLoader.clearList();
|
||||
spawnerLoader.clearList();
|
||||
regionLoader.clearList();
|
||||
dimensionLoader.clearList();
|
||||
generatorLoader.clearList();
|
||||
jigsawStructureLoader.clearList();
|
||||
jigsawPoolLoader.clearList();
|
||||
jigsawPieceLoader.clearList();
|
||||
objectLoader.clearList();
|
||||
for(ResourceLoader<?> i : loaders.values())
|
||||
{
|
||||
i.clearList();
|
||||
}
|
||||
}
|
||||
|
||||
public static IrisObject loadAnyObject(String key) {
|
||||
@ -154,6 +159,10 @@ public class IrisDataManager {
|
||||
return loadAny(key, (dm) -> dm.getBiomeLoader().load(key, false));
|
||||
}
|
||||
|
||||
public static IrisExpression loadAnyExpression(String key) {
|
||||
return loadAny(key, (dm) -> dm.getExpressionLoader().load(key, false));
|
||||
}
|
||||
|
||||
public static IrisMod loadAnyMod(String key) {
|
||||
return loadAny(key, (dm) -> dm.getModLoader().load(key, false));
|
||||
}
|
||||
|
@ -46,6 +46,10 @@ public class ObjectResourceLoader extends ResourceLoader<IrisObject> {
|
||||
unload = new AtomicInteger(0);
|
||||
}
|
||||
|
||||
public boolean supportsSchemas() {
|
||||
return false;
|
||||
}
|
||||
|
||||
public int getSize() {
|
||||
return loadCache.size();
|
||||
}
|
||||
|
@ -22,6 +22,7 @@ import com.google.common.util.concurrent.AtomicDouble;
|
||||
import com.google.gson.Gson;
|
||||
import com.volmit.iris.Iris;
|
||||
import com.volmit.iris.core.IrisDataManager;
|
||||
import com.volmit.iris.core.project.SchemaBuilder;
|
||||
import com.volmit.iris.engine.object.IrisRegistrant;
|
||||
import com.volmit.iris.util.collection.KList;
|
||||
import com.volmit.iris.util.collection.KMap;
|
||||
@ -29,6 +30,8 @@ import com.volmit.iris.util.collection.KSet;
|
||||
import com.volmit.iris.util.format.C;
|
||||
import com.volmit.iris.util.format.Form;
|
||||
import com.volmit.iris.util.io.IO;
|
||||
import com.volmit.iris.util.json.JSONArray;
|
||||
import com.volmit.iris.util.json.JSONObject;
|
||||
import com.volmit.iris.util.scheduling.ChronoLatch;
|
||||
import com.volmit.iris.util.scheduling.IrisLock;
|
||||
import com.volmit.iris.util.scheduling.J;
|
||||
@ -72,6 +75,47 @@ public class ResourceLoader<T extends IrisRegistrant> {
|
||||
Iris.debug("Loader<" + C.GREEN + resourceTypeName + C.LIGHT_PURPLE + "> created in " + C.RED + "IDM/" + manager.getId() + C.LIGHT_PURPLE + " on " + C.WHITE + manager.getDataFolder().getPath());
|
||||
}
|
||||
|
||||
public JSONObject buildSchema()
|
||||
{
|
||||
Iris.debug("Building Schema " + objectClass.getSimpleName() + " " + root.getPath());
|
||||
JSONObject o = new JSONObject();
|
||||
KList<String> fm = new KList<>();
|
||||
|
||||
for(int g = 1; g < 8; g++)
|
||||
{
|
||||
fm.add("/" + root.getName() + Form.repeat("/*", g) + ".json");
|
||||
}
|
||||
|
||||
o.put("fileMatch", new JSONArray(fm.toArray()));
|
||||
o.put("schema", new SchemaBuilder(objectClass, manager).compute());
|
||||
|
||||
return o;
|
||||
}
|
||||
|
||||
public File findFile(String name) {
|
||||
lock.lock();
|
||||
for (File i : getFolders(name)) {
|
||||
for (File j : i.listFiles()) {
|
||||
if (j.isFile() && j.getName().endsWith(".json") && j.getName().split("\\Q.\\E")[0].equals(name)) {
|
||||
lock.unlock();
|
||||
return j;
|
||||
}
|
||||
}
|
||||
|
||||
File file = new File(i, name + ".json");
|
||||
|
||||
if (file.exists()) {
|
||||
lock.unlock();
|
||||
return file;
|
||||
}
|
||||
}
|
||||
|
||||
Iris.warn("Couldn't find " + resourceTypeName + ": " + name);
|
||||
|
||||
lock.unlock();
|
||||
return null;
|
||||
}
|
||||
|
||||
public void logLoad(File path, T t) {
|
||||
loads.getAndIncrement();
|
||||
|
||||
@ -305,4 +349,20 @@ public class ResourceLoader<T extends IrisRegistrant> {
|
||||
|
||||
return f;
|
||||
}
|
||||
|
||||
public boolean supportsSchemas() {
|
||||
return true;
|
||||
}
|
||||
|
||||
public void clean() {
|
||||
|
||||
}
|
||||
|
||||
public int getSize() {
|
||||
return loadCache.size();
|
||||
}
|
||||
|
||||
public int getTotalStorage() {
|
||||
return getSize();
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user