Verified Commit f2b12c0f authored by Bixilon's avatar Bixilon
Browse files

assets: Migrate minosoft to assets manager

parent fb8e29cb
Pipeline #814 passed with stage
in 4 minutes and 17 seconds
# Minecraft Versions
Once mojang releases a new minecraft version, you need to add the version to the [versions.json](/src/main/resources/assets/mapping/versions.json).
Once mojang releases a new minecraft version, you need to add the version to the [versions.json](/src/main/resources/assets/minosoft/mapping/versions.json).
The file is a huge json object, see below:
......
......@@ -118,7 +118,7 @@
<prefix>git</prefix>
<verbose>false</verbose>
<generateGitPropertiesFile>true</generateGitPropertiesFile>
<generateGitPropertiesFilename>${project.build.outputDirectory}/assets/git.json</generateGitPropertiesFilename>
<generateGitPropertiesFilename>${project.build.outputDirectory}/assets/minosoft/git.json</generateGitPropertiesFilename>
<format>json</format>
<gitDescribe>
<skip>false</skip>
......
......@@ -19,8 +19,10 @@ import com.jfoenix.controls.JFXDialogLayout;
import de.bixilon.minosoft.config.Configuration;
import de.bixilon.minosoft.config.StaticConfiguration;
import de.bixilon.minosoft.data.accounts.Account;
import de.bixilon.minosoft.data.assets.JarAssetsManager;
import de.bixilon.minosoft.data.assets.Resources;
import de.bixilon.minosoft.data.locale.LocaleManager;
import de.bixilon.minosoft.data.mappings.ResourceLocation;
import de.bixilon.minosoft.data.mappings.versions.Versions;
import de.bixilon.minosoft.gui.main.GUITools;
import de.bixilon.minosoft.gui.main.Launcher;
......@@ -32,6 +34,7 @@ import de.bixilon.minosoft.modding.loading.ModLoader;
import de.bixilon.minosoft.modding.loading.Priorities;
import de.bixilon.minosoft.protocol.network.Connection;
import de.bixilon.minosoft.protocol.protocol.LANServerListener;
import de.bixilon.minosoft.protocol.protocol.ProtocolDefinition;
import de.bixilon.minosoft.terminal.CLI;
import de.bixilon.minosoft.util.CountUpAndDownLatch;
import de.bixilon.minosoft.util.GitInfo;
......@@ -49,6 +52,7 @@ import javafx.stage.Stage;
import java.util.HashSet;
public final class Minosoft {
public static final JarAssetsManager MINOSOFT_ASSETS_MANAGER = new JarAssetsManager(Minosoft.class);
public static final HashSet<EventManager> EVENT_MANAGERS = new HashSet<>();
public static final HashBiMap<Integer, Connection> CONNECTIONS = HashBiMap.create();
private static final CountUpAndDownLatch START_STATUS_LATCH = new CountUpAndDownLatch(1);
......@@ -125,7 +129,7 @@ public final class Minosoft {
taskWorker.addTask(new Task(progress -> {
Log.info("Loading versions.json...");
long mappingStartLoadingTime = System.currentTimeMillis();
Versions.loadAvailableVersions(Util.readJsonAssetResource("mapping/versions.json"));
Versions.loadAvailableVersions(MINOSOFT_ASSETS_MANAGER.readJsonAsset(new ResourceLocation(ProtocolDefinition.MINOSOFT_NAMESPACE, "mapping/versions.json")));
Log.info(String.format("Loaded %d versions in %dms", Versions.getVersionIdMap().size(), (System.currentTimeMillis() - mappingStartLoadingTime)));
Resources.load();
Log.info("Loaded all resources!");
......
......@@ -23,9 +23,12 @@ import org.lwjgl.BufferUtils
import java.io.BufferedReader
import java.io.InputStream
import java.io.InputStreamReader
import java.net.URL
interface AssetsManager {
fun getAssetURL(resourceLocation: ResourceLocation): URL
fun getAssetSize(resourceLocation: ResourceLocation): Long
fun readAssetAsStream(resourceLocation: ResourceLocation): InputStream
......
/*
* Minosoft
* Copyright (C) 2021 Moritz Zwerger
*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program.If not, see <https://www.gnu.org/licenses/>.
*
* This software is not affiliated with Mojang AB, the original developer of Minecraft.
*/
package de.bixilon.minosoft.data.assets
import de.bixilon.minosoft.data.mappings.ResourceLocation
import java.io.FileNotFoundException
import java.io.InputStream
import java.net.URL
class JarAssetsManager(
private val clazz: Class<*>,
) : AssetsManager {
override fun getAssetURL(resourceLocation: ResourceLocation): URL {
return clazz.getResource("/assets/${resourceLocation.namespace}/${resourceLocation.path}")
}
override fun getAssetSize(resourceLocation: ResourceLocation): Long {
val inputStream = readAssetAsStream(resourceLocation)
val size = inputStream.available()
inputStream.close()
return size.toLong()
}
override fun readAssetAsStream(resourceLocation: ResourceLocation): InputStream {
return clazz.getResourceAsStream("/assets/${resourceLocation.namespace}/${resourceLocation.path}") ?: throw FileNotFoundException("Can not find jar asset $resourceLocation in ${clazz.simpleName}")
}
}
......@@ -26,6 +26,7 @@ import java.io.File
import java.io.FileInputStream
import java.io.FileNotFoundException
import java.io.InputStream
import java.net.URL
import java.util.*
import java.util.zip.GZIPInputStream
import java.util.zip.ZipEntry
......@@ -183,6 +184,10 @@ class MinecraftAssetsManager(
return getAssetSize(getAssetHash(resourceLocation))
}
override fun getAssetURL(resourceLocation: ResourceLocation): URL {
TODO()
}
override fun readAssetAsStream(resourceLocation: ResourceLocation): InputStream {
return readAssetAsStream(getAssetHash(resourceLocation))
}
......
......@@ -16,9 +16,11 @@ package de.bixilon.minosoft.data.assets;
import com.google.common.collect.HashBiMap;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import de.bixilon.minosoft.Minosoft;
import de.bixilon.minosoft.data.mappings.ResourceLocation;
import de.bixilon.minosoft.data.mappings.versions.Version;
import de.bixilon.minosoft.data.mappings.versions.Versions;
import de.bixilon.minosoft.util.Util;
import de.bixilon.minosoft.protocol.protocol.ProtocolDefinition;
import java.io.IOException;
import java.util.HashMap;
......@@ -29,7 +31,7 @@ public class Resources {
private static final HashMap<Version, String> PIXLYZER_VERSIONS = new HashMap<>();
public static void load() throws IOException {
JsonObject json = Util.readJsonAssetResource("mapping/resources.json");
JsonObject json = Minosoft.MINOSOFT_ASSETS_MANAGER.readJsonAsset(new ResourceLocation(ProtocolDefinition.MINOSOFT_NAMESPACE, "mapping/resources.json"));
JsonObject versions = json.getAsJsonObject("versions");
for (Map.Entry<String, JsonElement> versionEntry : versions.entrySet()) {
......@@ -42,7 +44,7 @@ public class Resources {
}
// PixLyzer
JsonObject pixlyzerIndex = Util.readJsonAssetResource("mapping/pixlyzer_index.json");
JsonObject pixlyzerIndex = Minosoft.MINOSOFT_ASSETS_MANAGER.readJsonAsset(new ResourceLocation(ProtocolDefinition.MINOSOFT_NAMESPACE, "mapping/pixlyzer_index.json"));
for (Map.Entry<String, JsonElement> versionEntry : pixlyzerIndex.entrySet()) {
......
......@@ -15,12 +15,11 @@ package de.bixilon.minosoft.data.locale;
import de.bixilon.minosoft.Minosoft;
import de.bixilon.minosoft.ShutdownReasons;
import de.bixilon.minosoft.data.mappings.ResourceLocation;
import de.bixilon.minosoft.data.mappings.versions.Versions;
import de.bixilon.minosoft.util.Util;
import de.bixilon.minosoft.protocol.protocol.ProtocolDefinition;
import de.bixilon.minosoft.util.logging.Log;
import java.io.IOException;
public class LocaleManager {
private static Language fallbackLanguage; // en_US
private static Language language;
......@@ -47,8 +46,8 @@ public class LocaleManager {
return fallbackLanguage.translate(key, data);
}
private static Language loadLanguage(String language) throws IOException {
return new Language(language, Util.readJsonAssetResource(String.format("locale/%s.json", language)));
private static Language loadLanguage(String language) {
return new Language(language, Minosoft.MINOSOFT_ASSETS_MANAGER.readJsonAsset(new ResourceLocation(ProtocolDefinition.MINOSOFT_NAMESPACE, String.format("locale/%s.json", language))));
}
public static void load(String language) {
......
......@@ -32,7 +32,7 @@ public class ResourceLocation implements Comparable<ResourceLocation> {
public ResourceLocation(String full) {
String[] split = full.split(":");
if (split.length == 1) {
this.namespace = ProtocolDefinition.DEFAULT_MOD;
this.namespace = ProtocolDefinition.DEFAULT_NAMESPACE;
this.path = full;
} else {
this.namespace = split[0];
......
......@@ -18,7 +18,9 @@ import de.bixilon.minosoft.ShutdownReasons;
import de.bixilon.minosoft.data.accounts.Account;
import de.bixilon.minosoft.data.locale.LocaleManager;
import de.bixilon.minosoft.data.locale.Strings;
import de.bixilon.minosoft.data.mappings.ResourceLocation;
import de.bixilon.minosoft.gui.main.cells.AccountListCell;
import de.bixilon.minosoft.protocol.protocol.ProtocolDefinition;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.Initializable;
......@@ -51,7 +53,7 @@ public class AccountWindow implements Initializable {
public void addMicrosoftAccount() {
try {
GUITools.showPane("/layout/dialogs/login/microsoft.fxml", Modality.APPLICATION_MODAL, LocaleManager.translate(Strings.LOGIN_MICROSOFT_DIALOG_TITLE));
GUITools.showPane(new ResourceLocation(ProtocolDefinition.MINOSOFT_NAMESPACE, "layout/dialogs/login/microsoft.fxml"), Modality.APPLICATION_MODAL, LocaleManager.translate(Strings.LOGIN_MICROSOFT_DIALOG_TITLE));
} catch (IOException e) {
e.printStackTrace();
Minosoft.shutdown(e.getMessage(), ShutdownReasons.LAUNCHER_FXML_LOAD_ERROR);
......@@ -60,7 +62,7 @@ public class AccountWindow implements Initializable {
public void addMojangAccount() {
try {
GUITools.showPane("/layout/dialogs/login/mojang.fxml", Modality.APPLICATION_MODAL, LocaleManager.translate(Strings.LOGIN_MOJANG_DIALOG_TITLE));
GUITools.showPane(new ResourceLocation(ProtocolDefinition.MINOSOFT_NAMESPACE, "layout/dialogs/login/mojang.fxml"), Modality.APPLICATION_MODAL, LocaleManager.translate(Strings.LOGIN_MOJANG_DIALOG_TITLE));
} catch (IOException e) {
e.printStackTrace();
Minosoft.shutdown(e.getMessage(), ShutdownReasons.LAUNCHER_FXML_LOAD_ERROR);
......@@ -69,7 +71,7 @@ public class AccountWindow implements Initializable {
public void addOfflineAccount() {
try {
GUITools.showPane("/layout/dialogs/login/offline.fxml", Modality.APPLICATION_MODAL, LocaleManager.translate(Strings.LOGIN_OFFLINE_DIALOG_TITLE));
GUITools.showPane(new ResourceLocation(ProtocolDefinition.MINOSOFT_NAMESPACE, "layout/dialogs/login/offline.fxml"), Modality.APPLICATION_MODAL, LocaleManager.translate(Strings.LOGIN_OFFLINE_DIALOG_TITLE));
} catch (IOException e) {
e.printStackTrace();
Minosoft.shutdown(e.getMessage(), ShutdownReasons.LAUNCHER_FXML_LOAD_ERROR);
......
......@@ -14,6 +14,8 @@
package de.bixilon.minosoft.gui.main;
import com.jfoenix.controls.JFXComboBox;
import de.bixilon.minosoft.Minosoft;
import de.bixilon.minosoft.data.mappings.ResourceLocation;
import de.bixilon.minosoft.data.mappings.versions.Version;
import de.bixilon.minosoft.data.mappings.versions.Versions;
import de.bixilon.minosoft.util.logging.LogLevels;
......@@ -33,7 +35,7 @@ import java.util.Arrays;
import java.util.Base64;
public class GUITools {
public static final Image MINOSOFT_LOGO = new Image(GUITools.class.getResourceAsStream("/icons/windowIcon.png"));
public static final Image MINOSOFT_LOGO = new Image(GUITools.class.getResourceAsStream("/assets/minosoft/textures/icons/windowIcon.png"));
public static final ObservableList<Version> VERSIONS = FXCollections.observableArrayList();
public static final JFXComboBox<Version> VERSION_COMBO_BOX = new JFXComboBox<>(VERSIONS);
public static final ObservableList<LogLevels> LOG_LEVELS = FXCollections.observableList(Arrays.asList(LogLevels.values().clone()));
......@@ -75,7 +77,7 @@ public class GUITools {
}
public static Scene initializeScene(Scene scene) {
scene.getStylesheets().add("/layout/style.css");
scene.getStylesheets().add("/assets/minosoft/layout/style.css"); // ToDo: Migrate to minosoft assets manager
if (scene.getWindow() instanceof Stage stage) {
stage.getIcons().add(MINOSOFT_LOGO);
}
......@@ -87,8 +89,8 @@ public class GUITools {
return pane;
}
public static <T> T showPane(String fxmlPath, Modality modality, String title) throws IOException {
FXMLLoader loader = new FXMLLoader(GUITools.class.getResource(fxmlPath));
public static <T> T showPane(ResourceLocation fxmlResourceLocation, Modality modality, String title) throws IOException {
FXMLLoader loader = new FXMLLoader(Minosoft.MINOSOFT_ASSETS_MANAGER.getAssetURL(fxmlResourceLocation));
Parent root = loader.load();
Stage stage = new Stage();
stage.initModality(modality);
......
......@@ -17,8 +17,10 @@ import de.bixilon.minosoft.Minosoft;
import de.bixilon.minosoft.ShutdownReasons;
import de.bixilon.minosoft.data.locale.LocaleManager;
import de.bixilon.minosoft.data.locale.Strings;
import de.bixilon.minosoft.data.mappings.ResourceLocation;
import de.bixilon.minosoft.data.mappings.versions.Version;
import de.bixilon.minosoft.protocol.protocol.LANServerListener;
import de.bixilon.minosoft.protocol.protocol.ProtocolDefinition;
import de.bixilon.minosoft.util.logging.Log;
import javafx.application.Platform;
import javafx.collections.FXCollections;
......@@ -68,7 +70,7 @@ public class Launcher {
ServerListCell.SERVER_LIST_VIEW.setItems(servers);
LANServerListener.removeAll(); // remove all LAN Servers
FXMLLoader loader = new FXMLLoader(Launcher.class.getResource("/layout/main.fxml"));
FXMLLoader loader = new FXMLLoader(Minosoft.MINOSOFT_ASSETS_MANAGER.getAssetURL(new ResourceLocation(ProtocolDefinition.MINOSOFT_NAMESPACE, "layout/main.fxml")));
VBox root;
try {
root = loader.load();
......
......@@ -23,9 +23,11 @@ import de.bixilon.minosoft.ShutdownReasons;
import de.bixilon.minosoft.data.accounts.Account;
import de.bixilon.minosoft.data.locale.LocaleManager;
import de.bixilon.minosoft.data.locale.Strings;
import de.bixilon.minosoft.data.mappings.ResourceLocation;
import de.bixilon.minosoft.data.mappings.versions.Versions;
import de.bixilon.minosoft.data.text.ChatComponent;
import de.bixilon.minosoft.protocol.protocol.LANServerListener;
import de.bixilon.minosoft.protocol.protocol.ProtocolDefinition;
import de.bixilon.minosoft.util.DNSUtil;
import de.bixilon.minosoft.util.logging.Log;
import javafx.application.Platform;
......@@ -63,7 +65,7 @@ public class MainWindow implements Initializable {
public static void manageAccounts() {
try {
Parent parent = new FXMLLoader(MainWindow.class.getResource("/layout/accounts.fxml")).load();
Parent parent = new FXMLLoader(Minosoft.MINOSOFT_ASSETS_MANAGER.getAssetURL(new ResourceLocation(ProtocolDefinition.MINOSOFT_NAMESPACE, "layout/accounts.fxml"))).load();
Stage stage = new Stage();
stage.initModality(Modality.APPLICATION_MODAL);
stage.setTitle(LocaleManager.translate(Strings.MANAGE_ACCOUNTS_NO_ACCOUNT_ERROR_TITLE));
......@@ -284,7 +286,7 @@ public class MainWindow implements Initializable {
public void openSettings() {
try {
Parent parent = new FXMLLoader(MainWindow.class.getResource("/layout/settings.fxml")).load();
Parent parent = new FXMLLoader(Minosoft.MINOSOFT_ASSETS_MANAGER.getAssetURL(new ResourceLocation(ProtocolDefinition.MINOSOFT_NAMESPACE, "layout/settings.fxml"))).load();
Stage stage = new Stage();
stage.initModality(Modality.APPLICATION_MODAL);
stage.setTitle(LocaleManager.translate(Strings.SETTINGS_TITLE));
......
......@@ -20,6 +20,7 @@ import de.bixilon.minosoft.Minosoft;
import de.bixilon.minosoft.data.Player;
import de.bixilon.minosoft.data.locale.LocaleManager;
import de.bixilon.minosoft.data.locale.Strings;
import de.bixilon.minosoft.data.mappings.ResourceLocation;
import de.bixilon.minosoft.data.mappings.versions.Version;
import de.bixilon.minosoft.data.mappings.versions.Versions;
import de.bixilon.minosoft.data.player.PingBars;
......@@ -80,7 +81,7 @@ public class ServerListCell extends ListCell<Server> implements Initializable {
private Server server;
public static ServerListCell newInstance() {
FXMLLoader loader = new FXMLLoader(ServerListCell.class.getResource("/layout/cells/server.fxml"));
FXMLLoader loader = new FXMLLoader(Minosoft.MINOSOFT_ASSETS_MANAGER.getAssetURL(new ResourceLocation(ProtocolDefinition.MINOSOFT_NAMESPACE, "layout/cells/server.fxml")));
try {
loader.load();
return loader.getController();
......@@ -442,7 +443,7 @@ public class ServerListCell extends ListCell<Server> implements Initializable {
public void manageSessions() {
try {
SessionsWindow sessionsWindow = GUITools.showPane("/layout/sessions.fxml", Modality.APPLICATION_MODAL, LocaleManager.translate(Strings.SESSIONS_DIALOG_TITLE, this.server.getName().getMessage()));
SessionsWindow sessionsWindow = GUITools.showPane(new ResourceLocation(ProtocolDefinition.MINOSOFT_NAMESPACE, "layout/sessions.fxml"), Modality.APPLICATION_MODAL, LocaleManager.translate(Strings.SESSIONS_DIALOG_TITLE, this.server.getName().getMessage()));
sessionsWindow.setServer(this.server);
} catch (IOException e) {
e.printStackTrace();
......
......@@ -13,11 +13,14 @@
package de.bixilon.minosoft.gui.main;
import de.bixilon.minosoft.Minosoft;
import de.bixilon.minosoft.data.locale.LocaleManager;
import de.bixilon.minosoft.data.locale.Strings;
import de.bixilon.minosoft.data.mappings.ResourceLocation;
import de.bixilon.minosoft.modding.event.EventInvokerCallback;
import de.bixilon.minosoft.modding.event.events.ConnectionStateChangeEvent;
import de.bixilon.minosoft.protocol.network.Connection;
import de.bixilon.minosoft.protocol.protocol.ProtocolDefinition;
import javafx.application.Platform;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
......@@ -43,7 +46,7 @@ public class SessionListCell extends ListCell<Connection> implements Initializab
private Connection connection;
public static SessionListCell newInstance() {
FXMLLoader loader = new FXMLLoader(SessionListCell.class.getResource("/layout/cells/session.fxml"));
FXMLLoader loader = new FXMLLoader(Minosoft.MINOSOFT_ASSETS_MANAGER.getAssetURL(new ResourceLocation(ProtocolDefinition.MINOSOFT_NAMESPACE, "layout/cells/session.fxml")));
try {
loader.load();
return loader.getController();
......
......@@ -18,7 +18,9 @@ import de.bixilon.minosoft.Minosoft;
import de.bixilon.minosoft.data.accounts.Account;
import de.bixilon.minosoft.data.locale.LocaleManager;
import de.bixilon.minosoft.data.locale.Strings;
import de.bixilon.minosoft.data.mappings.ResourceLocation;
import de.bixilon.minosoft.gui.main.GUITools;
import de.bixilon.minosoft.protocol.protocol.ProtocolDefinition;
import de.bixilon.minosoft.util.logging.Log;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
......@@ -49,7 +51,7 @@ public class AccountListCell extends ListCell<Account> implements Initializable
private Account account;
public static AccountListCell newInstance() {
FXMLLoader loader = new FXMLLoader(AccountListCell.class.getResource("/layout/cells/account.fxml"));
FXMLLoader loader = new FXMLLoader(Minosoft.MINOSOFT_ASSETS_MANAGER.getAssetURL(new ResourceLocation(ProtocolDefinition.MINOSOFT_NAMESPACE, "layout/cells/account.fxml")));
try {
loader.load();
return loader.getController();
......
......@@ -17,6 +17,7 @@ import de.bixilon.minosoft.Minosoft
import de.bixilon.minosoft.config.StaticConfiguration
import de.bixilon.minosoft.config.config.game.controls.KeyBindingsNames
import de.bixilon.minosoft.data.Directions
import de.bixilon.minosoft.data.mappings.ResourceLocation
import de.bixilon.minosoft.data.mappings.blocks.BlockState
import de.bixilon.minosoft.data.text.RGBColor
import de.bixilon.minosoft.data.world.*
......@@ -92,7 +93,7 @@ class WorldRenderer(
blockTextureArray.load()
chunkShader = Shader("chunk_vertex.glsl", "chunk_fragment.glsl")
chunkShader = Shader(ResourceLocation(ProtocolDefinition.MINOSOFT_NAMESPACE, "rendering/shader/chunk_vertex.glsl"), ResourceLocation(ProtocolDefinition.MINOSOFT_NAMESPACE, "rendering/shader/chunk_fragment.glsl"))
chunkShader.load()
// register keybindings
......
......@@ -24,6 +24,7 @@ import de.bixilon.minosoft.gui.rendering.hud.elements.HUDElement
import de.bixilon.minosoft.gui.rendering.shader.Shader
import de.bixilon.minosoft.gui.rendering.textures.TextureArray
import de.bixilon.minosoft.protocol.network.Connection
import de.bixilon.minosoft.protocol.protocol.ProtocolDefinition
import glm_.glm
import glm_.mat4x4.Mat4
import glm_.vec2.Vec2
......@@ -133,7 +134,7 @@ class HUDTextElement(val connection: Connection, val hudRenderer: HUDRenderer, v
hudMeshHUD = HUDFontMesh()
hudMeshHUD.load()
fontShader = Shader("font_vertex.glsl", "font_fragment.glsl")
fontShader = Shader(ResourceLocation(ProtocolDefinition.MINOSOFT_NAMESPACE, "rendering/shader/font_vertex.glsl"), ResourceLocation(ProtocolDefinition.MINOSOFT_NAMESPACE, "rendering/shader/font_fragment.glsl"))
fontShader.load()
......
......@@ -13,6 +13,9 @@
package de.bixilon.minosoft.gui.rendering.shader
import de.bixilon.minosoft.Minosoft
import de.bixilon.minosoft.data.assets.AssetsManager
import de.bixilon.minosoft.data.mappings.ResourceLocation
import de.bixilon.minosoft.gui.rendering.exceptions.ShaderLoadingException
import de.bixilon.minosoft.gui.rendering.textures.TextureArray
import de.bixilon.minosoft.gui.rendering.util.OpenGLUtil
......@@ -28,12 +31,15 @@ import org.lwjgl.opengl.GL11.GL_FALSE
import org.lwjgl.opengl.GL20.*
import org.lwjgl.system.MemoryUtil
class Shader(private val vertexPath: String, private val fragmentPath: String) {
class Shader(
private val vertexPath: ResourceLocation,
private val fragmentPath: ResourceLocation,
) {
private var programId = 0
fun load(): Int {
val vertexShader = ShaderUtil.createShader(vertexPath, GL_VERTEX_SHADER_ARB)
val fragmentShader = ShaderUtil.createShader(fragmentPath, GL_FRAGMENT_SHADER_ARB)
fun load(assetsManager: AssetsManager = Minosoft.MINOSOFT_ASSETS_MANAGER): Int {
val vertexShader = ShaderUtil.createShader(assetsManager, vertexPath, GL_VERTEX_SHADER_ARB)
val fragmentShader = ShaderUtil.createShader(assetsManager, fragmentPath, GL_FRAGMENT_SHADER_ARB)
programId = glCreateProgramObjectARB()
if (programId.toLong() == MemoryUtil.NULL) {
......
......@@ -13,21 +13,23 @@
package de.bixilon.minosoft.gui.rendering.shader
import de.bixilon.minosoft.Minosoft
import de.bixilon.minosoft.data.assets.AssetsManager
import de.bixilon.minosoft.data.mappings.ResourceLocation
import de.bixilon.minosoft.gui.rendering.exceptions.ShaderLoadingException
import de.bixilon.minosoft.gui.rendering.util.OpenGLUtil
import de.bixilon.minosoft.util.Util
import org.lwjgl.opengl.ARBShaderObjects.*
import org.lwjgl.opengl.GL11.GL_FALSE
import org.lwjgl.system.MemoryUtil
object ShaderUtil {
fun createShader(shaderPath: String, shaderType: Int): Int {
fun createShader(assetsManager: AssetsManager = Minosoft.MINOSOFT_ASSETS_MANAGER, resourceLocation: ResourceLocation, shaderType: Int): Int {
val shaderId = glCreateShaderObjectARB(shaderType)
if (shaderId.toLong() == MemoryUtil.NULL) {
throw ShaderLoadingException()
}
glShaderSourceARB(shaderId, Util.readAssetResource("rendering/shader/$shaderPath"))
glShaderSourceARB(shaderId, assetsManager.readStringAsset(resourceLocation))
glCompileShaderARB(shaderId)
if (glGetObjectParameteriARB(shaderId, GL_OBJECT_COMPILE_STATUS_ARB) == GL_FALSE) {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment