Skip to content

Commit

Permalink
Got new mod loader fully working.
Browse files Browse the repository at this point in the history
  • Loading branch information
Hilligans committed Mar 26, 2024
1 parent 8d9b981 commit bd3a338
Show file tree
Hide file tree
Showing 14 changed files with 290 additions and 115 deletions.
113 changes: 44 additions & 69 deletions src/main/java/dev/hilligans/ourcraft/GameInstance.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import dev.hilligans.ourcraft.block.blockstate.IBlockState;
import dev.hilligans.ourcraft.block.blockstate.IBlockStateTable;
import dev.hilligans.ourcraft.client.audio.SoundBuffer;
import dev.hilligans.ourcraft.client.audio.SoundCategory;
import dev.hilligans.ourcraft.client.input.Input;
import dev.hilligans.ourcraft.client.input.InputHandlerProvider;
import dev.hilligans.ourcraft.client.rendering.graphics.*;
Expand All @@ -26,6 +27,7 @@
import dev.hilligans.ourcraft.mod.handler.content.ContentPack;
import dev.hilligans.ourcraft.mod.handler.content.ModContent;
import dev.hilligans.ourcraft.mod.handler.EventBus;
import dev.hilligans.ourcraft.mod.handler.content.ModList;
import dev.hilligans.ourcraft.mod.handler.events.common.RegistryClearEvent;
import dev.hilligans.ourcraft.mod.handler.ModLoader;
import dev.hilligans.ourcraft.mod.handler.pipeline.InstanceLoaderPipeline;
Expand Down Expand Up @@ -64,7 +66,7 @@ public class GameInstance {
public final ResourceManager RESOURCE_MANAGER = new ResourceManager(this);
public final ModContent OURCRAFT = new ModContent("ourcraft",this).addClassLoader(new URLClassLoader(new URL[]{Ourcraft.class.getProtectionDomain().getCodeSource().getLocation()})).addMainClass(Ourcraft.class);
public final ContentPack CONTENT_PACK = new ContentPack(this);
public final AtomicBoolean REBUILDING = new AtomicBoolean(false);
public final ModList MOD_LIST = new ModList(this);
public final UniversalResourceLoader RESOURCE_LOADER = new UniversalResourceLoader();
public final ArgumentContainer ARGUMENTS = new ArgumentContainer();
public final DataLoader DATA_LOADER = new DataLoader();
Expand All @@ -82,47 +84,19 @@ public class GameInstance {

public GameInstance() {
gameInstanceUniversalID = getNewGameInstanceUniversalID();

/*
REGISTRIES.put(BLOCKS);
REGISTRIES.put(ITEMS);
REGISTRIES.put(BIOMES);
REGISTRIES.put(TAGS);
REGISTRIES.put(RECIPES);
REGISTRIES.put(RECIPE_VIEWS);
REGISTRIES.put(GRAPHICS_ENGINES);
REGISTRIES.put(SETTINGS);
REGISTRIES.put(PROTOCOLS);
REGISTRIES.put(COMMANDS);
REGISTRIES.put(RESOURCE_LOADERS);
REGISTRIES.put(SOUNDS);
REGISTRIES.put(TOOL_MATERIALS);
REGISTRIES.put(SCREEN_BUILDERS);
REGISTRIES.put(FEATURES);
REGISTRIES.put(RENDER_TARGETS);
REGISTRIES.put(RENDER_PIPELINES);
REGISTRIES.put(RENDER_TASK);
REGISTRIES.put(KEY_BINDS);
REGISTRIES.put(VERTEX_FORMATS);
REGISTRIES.put(INPUT_HANDLER_PROVIDERS);
REGISTRIES.put(TEXTURES);
REGISTRIES.put(SHADERS);
REGISTRIES.put(LAYOUT_ENGINES);
*/
}

public void loadContent() {
registerDefaultContent();
CONTENT_PACK.registerModContent(OURCRAFT);
//registerDefaultContent();
//CONTENT_PACK.registerModContent(OURCRAFT);
//CONTENT_PACK.mods.put("ourcraft",OURCRAFT);
MOD_LOADER.loadDefaultMods();
CONTENT_PACK.buildVital();
CONTENT_PACK.mods.forEach((s, modContent) -> modContent.invokeRegistryLoaders());
REBUILDING.set(true);
CONTENT_PACK.generateData();
buildBlockStates();
REBUILDING.set(false);
//MOD_LOADER.loadDefaultMods();
//CONTENT_PACK.buildVital();
//CONTENT_PACK.mods.forEach((s, modContent) -> modContent.invokeRegistryLoaders());
//REBUILDING.set(true);
//CONTENT_PACK.generateData();
//buildBlockStates();
//REBUILDING.set(false);
}

public void build(IGraphicsEngine<?,?,?> graphicsEngine, GraphicsContext graphicsContext) {
Expand All @@ -149,31 +123,33 @@ public void cleanupGraphics(IGraphicsEngine<?,?,?> graphicsEngine, GraphicsConte

public final Registry<Registry<?>> REGISTRIES = new Registry<>(this, Registry.class, "registry");

public Registry<Block> BLOCKS = new Registry<>(this, Block.class, "block");
public Registry<Item> ITEMS = new Registry<>(this, Item.class, "item");
public Registry<Biome> BIOMES = new Registry<>(this, Biome.class, "biome");
public Registry<Tag> TAGS = new Registry<>(this, Tag.class, "tag");
public Registry<IRecipe<?>> RECIPES = new Registry<>(this, IRecipe.class, "recipe");
public Registry<RecipeView<?>> RECIPE_VIEWS = new Registry<>(this, RecipeView.class, "recipe_view");
public Registry<IGraphicsEngine<?,?,?>> GRAPHICS_ENGINES = new Registry<>(this, IGraphicsEngine.class, "graphics_engine");
public Registry<CommandHandler> COMMANDS = new Registry<>(this, CommandHandler.class, "command");
public Registry<Protocol> PROTOCOLS = new Registry<>(this, Protocol.class, "protocol");
public Registry<Setting> SETTINGS = new Registry<>(this, Setting.class, "setting");
public Registry<ResourceLoader<?>> RESOURCE_LOADERS = new Registry<>(this, ResourceLoader.class, "resource_loader");
public Registry<SoundBuffer> SOUNDS = new Registry<>(this, SoundBuffer.class, "sound");
public Registry<ToolLevel> TOOL_MATERIALS = new Registry<>(this, ToolLevel.class, "tool_level");
public Registry<RegistryLoader> DATA_LOADERS = new Registry<>(this, RegistryLoader.class, "registry_loader");
public Registry<ScreenBuilder> SCREEN_BUILDERS = new Registry<>(this, ScreenBuilder.class, "screen");
public Registry<Feature> FEATURES = new Registry<>(this, Feature.class, "feature");
public Registry<RenderTarget> RENDER_TARGETS = new Registry<>(this, RenderTarget.class, "render_target");
public Registry<RenderPipeline> RENDER_PIPELINES = new Registry<>(this, RenderPipeline.class, "render_pipeline");
public Registry<RenderTaskSource> RENDER_TASK = new Registry<>(this, RenderTaskSource.class, "render_task");
public Registry<Input> KEY_BINDS = new Registry<>(this, Input.class, "key_bind");
public Registry<VertexFormat> VERTEX_FORMATS = new Registry<>(this, VertexFormat.class, "vertex_format");
public Registry<InputHandlerProvider> INPUT_HANDLER_PROVIDERS = new Registry<>(this, InputHandlerProvider.class, "input");
public Registry<Texture> TEXTURES = new Registry<>(this, Texture.class, "texture");
public Registry<ShaderSource> SHADERS = new Registry<>(this, ShaderSource.class, "shader");
public Registry<ILayoutEngine<?>> LAYOUT_ENGINES = new Registry<>(this, ILayoutEngine.class, "layout_engine");
public Registry<Block> BLOCKS;
public Registry<Item> ITEMS;
public Registry<Biome> BIOMES;
public Registry<Tag> TAGS;
public Registry<IRecipe<?>> RECIPES;
public Registry<RecipeView<?>> RECIPE_VIEWS;
public Registry<IGraphicsEngine<?,?,?>> GRAPHICS_ENGINES;
public Registry<CommandHandler> COMMANDS;
public Registry<Protocol> PROTOCOLS;
public Registry<Setting> SETTINGS;
public Registry<ResourceLoader<?>> RESOURCE_LOADERS;
public Registry<SoundBuffer> SOUNDS;
public Registry<SoundCategory> SOUND_CATEGORIES;
public Registry<ToolLevel> TOOL_MATERIALS;
public Registry<RegistryLoader> DATA_LOADERS;
public Registry<ScreenBuilder> SCREEN_BUILDERS;
public Registry<Feature> FEATURES;
public Registry<RenderTarget> RENDER_TARGETS;
public Registry<RenderPipeline> RENDER_PIPELINES;
public Registry<RenderTaskSource> RENDER_TASK;
public Registry<Input> KEY_BINDS;
public Registry<VertexFormat> VERTEX_FORMATS;
public Registry<InputHandlerProvider> INPUT_HANDLER_PROVIDERS;
public Registry<Texture> TEXTURES;
public Registry<ShaderSource> SHADERS;
public Registry<ILayoutEngine<?>> LAYOUT_ENGINES;

public ArrayList<IBlockState> BLOCK_STATES;

public void copyRegistries() {
Expand Down Expand Up @@ -202,6 +178,7 @@ public void copyRegistries() {
TEXTURES = (Registry<Texture>) REGISTRIES.getExcept("ourcraft:texture");
SHADERS = (Registry<ShaderSource>) REGISTRIES.getExcept("ourcraft:shader");
LAYOUT_ENGINES = (Registry<ILayoutEngine<?>>) REGISTRIES.getExcept("ourcraft:layout_engine");
SOUND_CATEGORIES = (Registry<SoundCategory>) REGISTRIES.getExcept("ourcraft:sound_category");
}

public void finishBuild() {
Expand Down Expand Up @@ -232,6 +209,10 @@ public void buildBlockStates() {
}
}

public boolean built() {
return builtSemaphore.availablePermits() == 1;
}

public void clear() {
BLOCKS.clear();
ITEMS.clear();
Expand Down Expand Up @@ -289,12 +270,6 @@ public void registerToolLevels(ToolLevel... toolLevels) {
}
}

public void registerRegistryLoader(RegistryLoader... loaders) {
for(RegistryLoader loader : loaders) {
DATA_LOADERS.put(loader.name.getName(),loader);
}
}

public void register(IRegistryElement registryElement) {
register(registryElement.getIdentifierName(), registryElement);
}
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/dev/hilligans/ourcraft/Ourcraft.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import dev.hilligans.ourcraft.biome.Biomes;
import dev.hilligans.ourcraft.block.Block;
import dev.hilligans.ourcraft.client.audio.SoundBuffer;
import dev.hilligans.ourcraft.client.audio.SoundCategory;
import dev.hilligans.ourcraft.client.audio.Sounds;
import dev.hilligans.ourcraft.client.ChatWindow;
import dev.hilligans.ourcraft.client.Client;
Expand Down Expand Up @@ -133,7 +134,8 @@ public void registerRegistries(RegistryView view) {
new Tuple(InputHandlerProvider.class, "input"),
new Tuple(Texture.class, "texture"),
new Tuple(ShaderSource.class, "shader"),
new Tuple(ILayoutEngine.class, "layout_engine")
new Tuple(ILayoutEngine.class, "layout_engine"),
new Tuple(SoundCategory.class, "sound_category")
};

for(Tuple<Class<? extends IRegistryElement>, String> element : elements) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package dev.hilligans.ourcraft.client.audio;

import dev.hilligans.ourcraft.util.registry.IRegistryElement;

import java.util.ArrayList;

public class SoundCategory {
public class SoundCategory implements IRegistryElement {

public static final ArrayList<SoundCategory> soundCategories = new ArrayList<>();

Expand Down Expand Up @@ -67,4 +69,19 @@ public SoundSource getRandomSource(boolean loop, boolean relative) {
SoundBuffer soundBuffer = getRandomBuffer();
return soundBuffer.createNewSound(loop,relative, this);
}

@Override
public String getResourceName() {
return null;
}

@Override
public String getResourceOwner() {
return null;
}

@Override
public String getResourceType() {
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,9 @@ public void swapBuffers(GraphicsContext graphicsContext) {
glfwSwapBuffers(window);
}
client.rendering = false;
client.soundEngine.tick();
if(getGameInstance().built()) {
client.soundEngine.tick();
}
// if(client.screenShot) {
// client.screenShot = false;
// ScreenShot.takeScreenShot(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,11 @@ public abstract class ModClass {
public abstract String getModID();

public void registerRegistries(RegistryView view) {

}

public void registerCoreExtensions(CoreExtensionView view) {

}

public void registerContent(ModContainer container) {

}
}
15 changes: 12 additions & 3 deletions src/main/java/dev/hilligans/ourcraft/mod/handler/ModLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import dev.hilligans.ourcraft.GameInstance;
import dev.hilligans.ourcraft.data.primitives.Triplet;
import dev.hilligans.ourcraft.mod.handler.content.ModContainer;
import dev.hilligans.ourcraft.mod.handler.content.ModContent;
import dev.hilligans.ourcraft.resource.dataloader.FolderResourceDirectory;
import dev.hilligans.ourcraft.resource.dataloader.ResourceDirectory;
Expand Down Expand Up @@ -40,7 +41,7 @@ public void loadDefaultMods() {
//System.out.println("Java " + System.getProperty("java.version"));
loadAllMods(new File("mods/"));
if(true) {
loadClasses(new File("target/classes/"), "");
// loadClasses1(new File("target/classes/"), "");
}

if(new File("ourcraft-1.0.3-jar-with-dependencies.jar").exists()) {
Expand All @@ -50,7 +51,7 @@ public void loadDefaultMods() {
} else {
gameInstance.DATA_LOADER.addFolder("target/classes/", "ourcraft");
}
gameInstance.CONTENT_PACK.load();
//gameInstance.CONTENT_PACK.load();
}

public void loadAllMods(File folder) {
Expand Down Expand Up @@ -89,7 +90,7 @@ public void loadClasses(File folder, String topName) {
modContent.addClassLoader(child);
mainClasses.put(modID,new Triplet<>(testClass,mod.getAbsolutePath(),false));
gameInstance.DATA_LOADER.add(modID, resourceDirectory);
gameInstance.CONTENT_PACK.registerModContent(modContent);
//gameInstance.MOD_LIST.registerMod(modContent);
}
} catch (Exception ignored) {}
}
Expand All @@ -98,6 +99,7 @@ public void loadClasses(File folder, String topName) {
}



public boolean loadMod(File file) {
try {
URLClassLoader child = new URLClassLoader(new URL[]{file.toURI().toURL()}, this.getClass().getClassLoader());
Expand Down Expand Up @@ -159,6 +161,13 @@ public ArrayList<String> getClassNames(File file) {
return classNames;
}

public <T extends Class<ModClass>> T testClass(Class<?> clazz) {
if(ModClass.class.isAssignableFrom(clazz)) {
return (T)clazz;
}
return null;
}

public String getModID(Class<?> val) {
for(Annotation annotation : val.getAnnotations()) {
if(annotation.annotationType().equals(Mod.class)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@ public void load() {
//private boolean waiting = false;

public void generateData() {
gameInstance.REBUILDING.set(true);
//gameInstance.REBUILDING.set(true);
///TODO Could potentially rebuild when rendering but very unlikely
//if(!Settings.isServer && !(ClientMain.getClient() == null || ClientMain.getClient().rendering)) {
// waiting = true;
//} else {
rebuild();
// rebuild();
//}
}

Expand Down Expand Up @@ -88,21 +88,21 @@ public void buildVital() {
public boolean built = false;

private void rebuild() {
gameInstance.clear();
gameInstance.RESOURCE_MANAGER.clearData();
// gameInstance.clear();
// gameInstance.RESOURCE_MANAGER.clearData();

if(!built) {
buildVital();
// buildVital();
}

for(String string : mods.keySet()) {
if(shouldLoad.get(string)) {
ModContent mod = mods.get(string);
for(RegistryLoader registryLoader : mod.registryLoaders) {
if(registryLoader.rerunOnInstanceClear) {
registryLoader.run();
// registryLoader.run();
}
gameInstance.registerRegistryLoader(registryLoader);
// gameInstance.registerRegistryLoader(registryLoader);
}
}
}
Expand Down Expand Up @@ -167,7 +167,7 @@ private void rebuild() {
for(Registry<?> registry : gameInstance.REGISTRIES.ELEMENTS) {
for(Object o : registry.ELEMENTS) {
if(o instanceof IRegistryElement) {
((IRegistryElement) o).load(gameInstance);
// ((IRegistryElement) o).load(gameInstance);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import dev.hilligans.ourcraft.util.registry.Registry;
import dev.hilligans.ourcraft.world.Feature;

import java.lang.reflect.InvocationTargetException;
import java.net.URLClassLoader;
import java.util.Arrays;
import java.util.List;
Expand Down Expand Up @@ -70,6 +71,11 @@ public class ModContainer {
public Registry<ShaderSource> shaderSourceRegistry;
public Registry<ILayoutEngine<?>> layoutEngineRegistry;

public ModContainer(Class<? extends ModClass> clazz, URLClassLoader classLoader) throws InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
this.modClass = clazz.getConstructor().newInstance();
this.classLoader = classLoader;
}

public ModContainer(ModClass modClass) {
this.modClass = modClass;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,6 @@ public void readData(JSONObject jsonObject) {
}
version = jsonObject.has("version") ? jsonObject.getInt("version") : -1;
description = jsonObject.has("description") ? jsonObject.getString("description") : "";

}

public GameInstance getGameInstance() {
Expand Down
Loading

0 comments on commit bd3a338

Please sign in to comment.