Redo file loading / builds proper jar now

This commit is contained in:
austin 2021-06-20 14:29:54 -04:00
parent aba735dac0
commit 64928852ba
16 changed files with 237 additions and 175 deletions

7
.gitignore vendored
View File

@ -4,5 +4,10 @@
/src/main/resources/Config/localconfig.json
/src/main/resources/Config/keybinds.json
/dependency-reduced-pom.xml
/Models
/Models/**.*
/Telephone-*.jar
/hs_err_pid*
/hs_err_pid*

20
pom.xml
View File

@ -171,6 +171,26 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.4</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>electrosphere.main.Main</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

View File

@ -6,6 +6,7 @@ import electrosphere.game.server.creature.type.CreatureType;
import electrosphere.game.server.creature.type.model.CreatureTypeMap;
import electrosphere.game.server.culture.Culture;
import electrosphere.game.server.culture.religion.Religion;
import electrosphere.util.FileLoadingUtils;
import electrosphere.util.Utilities;
import java.util.LinkedList;
import java.util.List;
@ -23,8 +24,8 @@ public class Simulation {
}
void init(){
CreatureTypeMap creatureTypeMap = Utilities.loadObjectFromBakedJsonFile("Data/creatures.json", CreatureTypeMap.class);
CivilizationMap civilizationMap = Utilities.loadObjectFromBakedJsonFile("Data/civilization.json", CivilizationMap.class);
CreatureTypeMap creatureTypeMap = FileLoadingUtils.loadModelObjectFromBakedJsonFile("Data/creatures.json", CreatureTypeMap.class);
CivilizationMap civilizationMap = FileLoadingUtils.loadModelObjectFromBakedJsonFile("Data/civilization.json", CivilizationMap.class);
}
public void simulate(){

View File

@ -5,6 +5,7 @@ import electrosphere.game.terrain.processing.TerrainInterpolator;
import electrosphere.main.Globals;
import electrosphere.game.server.terrain.generation.TerrainGen;
import electrosphere.game.server.terrain.models.TerrainModel;
import electrosphere.util.FileLoadingUtils;
import electrosphere.util.Utilities;
import java.io.File;
import java.io.IOException;
@ -90,7 +91,7 @@ public class ServerTerrainManager {
public void load(){
Gson gson = new Gson();
model = gson.fromJson(Utilities.readFileToString(new File(Globals.mainConfig.loadTerrainLocation)), TerrainModel.class);
model = FileLoadingUtils.loadModelObjectFromBakedJsonFile(Globals.mainConfig.loadTerrainLocation, TerrainModel.class);
}
public float[][] getTerrainAtChunk(int x, int y){

View File

@ -40,6 +40,7 @@ import electrosphere.renderer.ui.WidgetUtils;
import electrosphere.renderer.ui.font.FontUtils;
import electrosphere.renderer.ui.font.RawFontMap;
import electrosphere.renderer.ui.font.TextBox;
import electrosphere.util.FileLoadingUtils;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
@ -236,7 +237,7 @@ public class Globals {
// try {
//deserializes the texture map from its default path using gson
//also done in one line
textureMapDefault = Utilities.loadObjectFromBakedJsonFile("/Textures/default_texture_map.json", TextureMap.class);
textureMapDefault = FileLoadingUtils.loadModelObjectFromBakedJsonFile("Textures/default_texture_map.json", TextureMap.class);
// textureMapDefault = gson.fromJson(Files.newBufferedReader(new File(Thread.currentThread().getContextClassLoader().getResource("Textures/default_texture_map.json").getFile()).toPath()), TextureMap.class); //only the best of coding practices :)
// } catch (IOException ex) { ex.printStackTrace(); } //TODO: handle better :tm:
//entity type map
@ -266,7 +267,7 @@ public class Globals {
//create default lights
lightDirectionalDefault = new DirectionalLight(new Vector3f(0,-1f,0));
assetManager.registerModelToSpecificString(ModelUtils.createBitmapDisplay(), AssetDataStrings.ASSET_STRING_BITMAP_FONT);
RawFontMap fontMap = Utilities.loadObjectFromBakedJsonFile("/Textures/Fonts/myFontMap.json", RawFontMap.class);
RawFontMap fontMap = FileLoadingUtils.loadModelObjectFromBakedJsonFile("Textures/Fonts/myFontMap.json", RawFontMap.class);
FontUtils.setFontDataMap(fontMap);
//black texture for backgrouns
blackTexture = new Texture("Textures/b1.png");
@ -285,7 +286,7 @@ public class Globals {
}
static void initEntityTypeMap(){
CreatureTypeList typeList = Utilities.loadObjectFromBakedJsonFile("/Data/entity_map.json", CreatureTypeList.class);
CreatureTypeList typeList = FileLoadingUtils.loadModelObjectFromBakedJsonFile("Data/entity_map.json", CreatureTypeList.class);
for(CreatureType type : typeList.getTypes()){
entityTypeMap.put(type.getId(), type);
}

View File

@ -18,6 +18,7 @@ import electrosphere.game.state.LoadingThread;
import electrosphere.game.state.SimulationState;
import electrosphere.game.state.SimulationState.SimulationStateMachine;
import electrosphere.renderer.RenderingEngine;
import electrosphere.util.FileLoadingUtils;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Random;
@ -286,7 +287,7 @@ public class Main {
public static void initControlHandler(){
// ControlHandler.generateExampleControlsMap();
Globals.controlHandler = Utilities.loadObjectFromBakedJsonFile("/Config/keybinds.json",ControlHandler.class);
Globals.controlHandler = FileLoadingUtils.loadModelObjectFromBakedJsonFile("/Config/keybinds.json",ControlHandler.class);
}

View File

@ -56,7 +56,7 @@ public class Model {
AnimNode root_anim_node;
public static Model create_model_from_aiscene(AIScene s){
public static Model createModelFromAiscene(AIScene s){
Model rVal = new Model();
//
//set the scene

View File

@ -154,7 +154,7 @@ public class ModelUtils {
m.parent = rVal;
Material groundMat = new Material();
Texture groundTex = new Texture("Textures/Ground/Dirt1.png");
Texture groundTex = new Texture("/Textures/Ground/Dirt1.png");
groundMat.set_diffuse(groundTex);
groundMat.set_specular(groundTex);
m.set_material(groundMat);
@ -318,7 +318,7 @@ public class ModelUtils {
m.parent = rVal;
Material groundMat = new Material();
Texture groundTex = new Texture("Textures/Ground/Dirt1.png");
Texture groundTex = new Texture("/Textures/Ground/Dirt1.png");
groundMat.set_diffuse(groundTex);
groundMat.set_specular(groundTex);
m.set_material(groundMat);
@ -486,7 +486,7 @@ public class ModelUtils {
m.parent = rVal;
Material groundMat = new Material();
Texture groundTex = new Texture("Textures/Ground/Dirt1.png");
Texture groundTex = new Texture("/Textures/Ground/Dirt1.png");
groundMat.set_diffuse(groundTex);
groundMat.set_specular(groundTex);
m.set_material(groundMat);
@ -606,7 +606,7 @@ public class ModelUtils {
m.nodeID = AssetDataStrings.ASSET_STRING_BITMAP_FONT_MESH_NAME;
Material uiMat = new Material();
Texture uiTex = new Texture("Textures/Fonts/myfont1-harsher.png");
Texture uiTex = new Texture("/Textures/Fonts/myfont1-harsher.png");
uiMat.set_diffuse(uiTex);
uiMat.set_specular(uiTex);
m.set_material(uiMat);

View File

@ -1,9 +1,11 @@
package electrosphere.renderer;
import electrosphere.main.Main;
import electrosphere.util.FileLoadingUtils;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import org.lwjgl.opengl.GL20;
@ -63,7 +65,7 @@ public class ShaderProgram {
//
String tempForReadingShaders = "";
try {
BufferedReader br = new BufferedReader(new FileReader(Main.class.getResource(vertex_shader_path).getFile()));
BufferedReader br = new BufferedReader(new InputStreamReader(Main.class.getResourceAsStream(vertex_shader_path)));
try {
StringBuilder sb = new StringBuilder();
String line = br.readLine();
@ -82,7 +84,7 @@ public class ShaderProgram {
String vertexShaderSource = tempForReadingShaders;
//This try-catch block reads the FragmentShader source into memory
try {
BufferedReader br = new BufferedReader(new FileReader(Main.class.getResource(fragment_shader_path).getFile()));
BufferedReader br = new BufferedReader(new InputStreamReader(Main.class.getResourceAsStream(fragment_shader_path)));
try {
StringBuilder sb = new StringBuilder();
String line = br.readLine();
@ -280,43 +282,8 @@ public class ShaderProgram {
//
//Read in shader programs
//
String tempForReadingShaders = "";
try {
BufferedReader br = new BufferedReader(new FileReader(Main.class.getResource(vertex_shader_path).getFile()));
try {
StringBuilder sb = new StringBuilder();
String line = br.readLine();
while (line != null) {
sb.append(line);
sb.append(System.lineSeparator());
line = br.readLine();
}
tempForReadingShaders = sb.toString();
} finally {
br.close();
}
} catch (IOException e) {
}
String vertexShaderSource = tempForReadingShaders;
//This try-catch block reads the FragmentShader source into memory
try {
BufferedReader br = new BufferedReader(new FileReader(Main.class.getResource(fragment_shader_path).getFile()));
try {
StringBuilder sb = new StringBuilder();
String line = br.readLine();
while (line != null) {
sb.append(line);
sb.append(System.lineSeparator());
line = br.readLine();
}
tempForReadingShaders = sb.toString();
} finally {
br.close();
}
} catch (IOException e) {
}
String fragmentShaderSource = tempForReadingShaders;
String vertexShaderSource = FileLoadingUtils.readStringFromBakedFile(vertex_shader_path);
String fragmentShaderSource = FileLoadingUtils.readStringFromBakedFile(fragment_shader_path);
//Creates a new shader object and assigns its 'pointer' to the integer "vertexShader"
rVal.vertexShader = glCreateShader(GL_VERTEX_SHADER);
//This alerts openGL to the presence of a vertex shader and points the shader at its source

View File

@ -5,6 +5,8 @@
*/
package electrosphere.renderer.texture;
import electrosphere.main.Main;
import electrosphere.util.FileLoadingUtils;
import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
@ -49,7 +51,7 @@ public class Texture {
width = 1;
height = 1;
try {
BufferedImage image_data = ImageIO.read(new File(Thread.currentThread().getContextClassLoader().getResource(path).getFile()));
BufferedImage image_data = ImageIO.read(Main.class.getResourceAsStream(FileLoadingUtils.sanitizeBakedFilePath(path)));
if (
image_data.getType() == BufferedImage.TYPE_3BYTE_BGR ||
image_data.getType() == BufferedImage.TYPE_INT_RGB

View File

@ -0,0 +1,154 @@
package electrosphere.util;
import com.google.gson.Gson;
import electrosphere.main.Main;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.concurrent.TimeUnit;
public class FileLoadingUtils {
static final int maxReadFails = 3;
static final int READ_TIMEOUT_DURATION = 5;
public static String readFileToString(File f){
String rVal = "";
BufferedReader reader;
try {
reader = Files.newBufferedReader(f.toPath());
int failCounter = 0;
boolean reading = true;
StringBuilder builder = new StringBuilder("");
while(reading){
if(reader.ready()){
failCounter = 0;
int nextValue = reader.read();
if(nextValue == -1){
reading = false;
} else {
builder.append((char)nextValue);
}
} else {
failCounter++;
if(failCounter > maxReadFails){
reading = false;
} else {
try {
TimeUnit.MILLISECONDS.sleep(READ_TIMEOUT_DURATION);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
}
}
rVal = builder.toString();
} catch (IOException ex) {
ex.printStackTrace();
}
return rVal;
}
public static String readStreamToString(InputStream resourceInputStream){
String rVal = "";
BufferedReader reader;
try {
reader = new BufferedReader(new InputStreamReader(resourceInputStream));
int failCounter = 0;
boolean reading = true;
StringBuilder builder = new StringBuilder("");
while(reading){
if(reader.ready()){
failCounter = 0;
int nextValue = reader.read();
if(nextValue == -1){
reading = false;
} else {
builder.append((char)nextValue);
}
} else {
failCounter++;
if(failCounter > maxReadFails){
reading = false;
} else {
try {
TimeUnit.MILLISECONDS.sleep(READ_TIMEOUT_DURATION);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
}
}
rVal = builder.toString();
} catch (IOException ex) {
ex.printStackTrace();
}
return rVal;
}
public static String sanitizeBakedFilePath(String filePath){
String rVal = new String(filePath);
rVal = rVal.trim();
if(!rVal.startsWith("/")){
rVal = "/" + rVal;
}
return rVal;
}
public static String readStringFromBakedFile(String bakedFilePath){
String rVal = "";
String sanitizedFilePath = sanitizeBakedFilePath(bakedFilePath);
rVal = readStreamToString(Main.class.getResourceAsStream(sanitizedFilePath));
return rVal;
}
public static void serializeObjectToFilePath(String filePath, Object object){
Path path = new File(filePath).toPath();
Gson gson = new Gson();
try {
Files.write(path, gson.toJson(object).getBytes());
} catch (IOException ex) {
ex.printStackTrace();
System.exit(1);
}
}
public static <T>T loadModelObjectFromBakedJsonFile(String fileName, Class<T> className){
T rVal = null;
String sanitizedFilePath = sanitizeBakedFilePath(fileName);
String rawJSON = readStreamToString(Main.class.getResourceAsStream(sanitizedFilePath));
Gson gson = new Gson();
rVal = gson.fromJson(rawJSON, className);
return rVal;
}
public static File unpackBakedFileToFilePath(String bakedFilePath){
String sanitizedFilePath = sanitizeBakedFilePath(bakedFilePath);
System.out.println(bakedFilePath);
if(!Files.exists(new File("./Models").toPath())){
try {
Files.createDirectory(new File("./Models").toPath());
} catch (IOException ex) {
ex.printStackTrace();
}
}
File targetFile = new File("." + sanitizedFilePath);
try {
Files.write(targetFile.toPath(), Main.class.getResourceAsStream(sanitizedFilePath).readAllBytes(),StandardOpenOption.CREATE,StandardOpenOption.WRITE);
} catch (IOException ex) {
ex.printStackTrace();
}
return targetFile;
}
}

View File

@ -1,6 +1,7 @@
package electrosphere.util;
import electrosphere.main.Globals;
import electrosphere.main.Main;
import electrosphere.renderer.Material;
import electrosphere.renderer.Mesh;
import electrosphere.renderer.Model;
@ -11,6 +12,8 @@ import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.StandardOpenOption;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map;
@ -24,8 +27,10 @@ public class ModelLoader {
public static Model load_Model_From_File(String fileName){
Model rVal;
AIScene scene;
File file = new File(Thread.currentThread().getContextClassLoader().getResource(fileName).getFile());
scene = aiImportFile(file.getAbsolutePath(),
// File file = new File(Thread.currentThread().getContextClassLoader().getResource(fileName).getFile());
// Main.class.getResourceAsStream(fileName).readAllBytes();
File toRead = FileLoadingUtils.unpackBakedFileToFilePath(fileName);
scene = aiImportFile(toRead.getAbsolutePath(),
aiProcess_GenSmoothNormals |
aiProcess_JoinIdenticalVertices |
aiProcess_Triangulate |
@ -34,14 +39,14 @@ public class ModelLoader {
if(scene == null){
throw new IllegalStateException(aiGetErrorString());
}
rVal = Model.create_model_from_aiscene(scene);
attempt_add_textures_from_pathname(fileName, rVal);
rVal = Model.createModelFromAiscene(scene);
attemptAddTexturesFromPathname(fileName, rVal);
return rVal;
}
//TODO: this logic should exclusively use functions provided in the TextureMap class
//this way if we change the underlying structure of the TextureMap it doesn't fuck over this logic
static void attempt_add_textures_from_pathname(String path, Model m){
static void attemptAddTexturesFromPathname(String path, Model m){
//first we get the default texture map that's global
TextureMap global_map = Globals.textureMapDefault;
//then we try to get the path of our model from the map

View File

@ -82,7 +82,7 @@ public class Utilities {
public static void save_test_texture_map_to_location(String s){
public static void saveTestTextureMapToLocation(String s){
TextureMap t = new TextureMap();
t.add_model("model1");
t.add_mesh_to_model("model1", "mesh1");
@ -104,87 +104,11 @@ public class Utilities {
}
}
static final int maxReadFails = 3;
static final int READ_TIMEOUT_DURATION = 5;
public static String readFileToString(File f){
String rVal = "";
BufferedReader reader;
try {
reader = Files.newBufferedReader(f.toPath());
int failCounter = 0;
boolean reading = true;
StringBuilder builder = new StringBuilder("");
while(reading){
if(reader.ready()){
failCounter = 0;
int nextValue = reader.read();
if(nextValue == -1){
reading = false;
} else {
builder.append((char)nextValue);
}
} else {
failCounter++;
if(failCounter > maxReadFails){
reading = false;
} else {
try {
TimeUnit.MILLISECONDS.sleep(READ_TIMEOUT_DURATION);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
}
}
rVal = builder.toString();
} catch (IOException ex) {
ex.printStackTrace();
}
return rVal;
}
public static String readBakedResourceToString(InputStream resourceInputStream){
String rVal = "";
BufferedReader reader;
try {
reader = new BufferedReader(new InputStreamReader(resourceInputStream));
int failCounter = 0;
boolean reading = true;
StringBuilder builder = new StringBuilder("");
while(reading){
if(reader.ready()){
failCounter = 0;
int nextValue = reader.read();
if(nextValue == -1){
reading = false;
} else {
builder.append((char)nextValue);
}
} else {
failCounter++;
if(failCounter > maxReadFails){
reading = false;
} else {
try {
TimeUnit.MILLISECONDS.sleep(READ_TIMEOUT_DURATION);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
}
}
rVal = builder.toString();
} catch (IOException ex) {
ex.printStackTrace();
}
return rVal;
}
public static void loadMainConfig(){
if(Main.class.getResource("/Config/localconfig.json") != null){
Globals.mainConfig = loadObjectFromBakedJsonFile("/Config/localconfig.json", MainConfig.class);
Globals.mainConfig = FileLoadingUtils.loadModelObjectFromBakedJsonFile("/Config/localconfig.json", MainConfig.class);
if(Globals.mainConfig.version != MainConfig.CONFIG_FILE_VERSION){
//dynamically generate config and save it
MainConfig.generateMainConfig();
@ -194,15 +118,6 @@ public class Utilities {
}
}
public static <T>T loadObjectFromBakedJsonFile(String fileName, Class<T> className){
T rVal = null;
String rawJSON = Utilities.readBakedResourceToString(Main.class.getResourceAsStream(fileName));
Gson gson = new Gson();
rVal = gson.fromJson(rawJSON, className);
return rVal;
}
public static void saveObjectToBakedJsonFile(String fileName, Object object){
URL resourceUrl = Main.class.getResource(fileName);
File file = new File("");
@ -219,15 +134,4 @@ public class Utilities {
System.exit(1);
}
}
public static void saveObjectToJsonFile(String fileName, Object object){
Path path = new File(Main.class.getResource(fileName).getPath()).toPath();
Gson gson = new Gson();
try {
Files.write(path, gson.toJson(object).getBytes());
} catch (IOException ex) {
ex.printStackTrace();
System.exit(1);
}
}
}

View File

@ -6,6 +6,7 @@ import electrosphere.game.server.terrain.manager.ServerTerrainManager;
import electrosphere.game.server.terrain.models.TerrainModel;
import electrosphere.main.Globals;
import electrosphere.main.Main;
import electrosphere.util.FileLoadingUtils;
import electrosphere.util.Utilities;
import java.awt.Color;
import java.awt.Graphics;
@ -38,7 +39,7 @@ public class TerrainViewer {
// terrainModel = terrainManager.getModel();
// Utilities.saveObjectToBakedJsonFile("/Config/testingTerrain.json", terrainModel);
terrainModel = Utilities.loadObjectFromBakedJsonFile("/Config/testingTerrain.json", TerrainModel.class);
terrainModel = FileLoadingUtils.loadModelObjectFromBakedJsonFile("/Config/testingTerrain.json", TerrainModel.class);
Simulation simulation = new Simulation();

View File

@ -2,7 +2,7 @@
"types": [
{
"id" : 0,
"modelPath" : "Models/person1walkanim.fbx",
"modelPath" : "/Models/person1walkanim.fbx",
"hitboxes" : [
{
"type": "hurt",
@ -58,7 +58,7 @@
},
{
"id" : 2,
"modelPath" : "Models/katana1alt.fbx",
"modelPath" : "/Models/katana1alt.fbx",
"hitboxes" : [
{
"type": "hit",

View File

@ -2,48 +2,48 @@
"texture_map": {
"Models/plane.fbx": {
"Cube": [
"Textures/Ground/Dirt1.png",
"Textures/Ground/Dirt1.png"
"/Textures/Ground/Dirt1.png",
"/Textures/Ground/Dirt1.png"
]
},
"Models/arcdock5deg1notex.fbx": {
"Cube": [
"Textures/w1.png",
"Textures/w1.png"
"/Textures/w1.png",
"/Textures/w1.png"
]
},
"Models/Wheat1.fbx": {
"Stalk": [
"Textures/Wheat1stretch.png",
"Textures/Wheat1stretch.png"
"/Textures/Wheat1stretch.png",
"/Textures/Wheat1stretch.png"
]
},
"Models/unitsphere.fbx": {
"Sphere": [
"Textures/transparent_blue.png",
"Textures/transparent_blue.png"
"/Textures/transparent_blue.png",
"/Textures/transparent_blue.png"
]
},
"Models/unitsphere_1.fbx": {
"Sphere": [
"Textures/transparent_red.png",
"Textures/transparent_red.png"
"/Textures/transparent_red.png",
"/Textures/transparent_red.png"
]
},
"Models/katana1alt.fbx": {
"SwordMesh": [
"Textures/katana1.png",
"Textures/katana1.png"
"/Textures/katana1.png",
"/Textures/katana1.png"
]
},
"Models/tree1.fbx": {
"Cube.002": [
"Textures/Branch.png",
"Textures/Branch.png"
"/Textures/Branch.png",
"/Textures/Branch.png"
],
"Cylinder": [
"Textures/Branch.png",
"Textures/Branch.png"
"/Textures/Branch.png",
"/Textures/Branch.png"
]
}
}