Skip to content

Commit

Permalink
The default config is no longer extracted into the config folder, it …
Browse files Browse the repository at this point in the history
…is loaded straight from the jar instead.
  • Loading branch information
Lunatrius committed Aug 14, 2014
1 parent 6c73f8e commit 526f082
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 77 deletions.
77 changes: 36 additions & 41 deletions src/main/java/com/github/lunatrius/ingameinfo/InGameInfoCore.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import com.github.lunatrius.ingameinfo.printer.json.JsonPrinter;
import com.github.lunatrius.ingameinfo.printer.text.TextPrinter;
import com.github.lunatrius.ingameinfo.printer.xml.XmlPrinter;
import com.github.lunatrius.ingameinfo.reference.Reference;
import com.github.lunatrius.ingameinfo.tag.Tag;
import com.github.lunatrius.ingameinfo.tag.registry.TagRegistry;
import cpw.mods.fml.common.registry.GameData;
Expand All @@ -32,13 +33,9 @@
import net.minecraft.world.World;
import org.lwjgl.opengl.GL11;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
Expand Down Expand Up @@ -94,6 +91,9 @@ public boolean setConfigFile(String filename) {
return true;
}
}

this.configFile = null;
this.parser = new XmlParser();
return false;
}

Expand All @@ -108,11 +108,11 @@ public void onTickClient() {
}
Tag.setWorld(world);

EntityClientPlayerMP player1 = this.minecraft.thePlayer;
if (player1 == null) {
EntityClientPlayerMP player = this.minecraft.thePlayer;
if (player == null) {
return;
}
Tag.setPlayer(player1);
Tag.setPlayer(player);

this.info.clear();
int x, y;
Expand Down Expand Up @@ -190,35 +190,6 @@ public void onTickRender() {
GL11.glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
}

public void copyDefaultConfig() {
File configFile = new File(this.configDirectory, "InGameInfo.xml");

if (!configFile.exists()) {
try {
ResourceLocation resourceLocation = new ResourceLocation("ingameinfo", "InGameInfo.xml");
IResource resource = this.minecraft.getResourceManager().getResource(resourceLocation);
InputStream inputStream = resource.getInputStream();

BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
String defaultConfig = "";
String line;

while ((line = reader.readLine()) != null) {
defaultConfig += line + System.getProperty("line.separator");
}

inputStream.close();

FileWriter fileWriter = new FileWriter(configFile);
BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
bufferedWriter.write(defaultConfig);
bufferedWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

public boolean loadConfig(String filename) {
return setConfigFile(filename) && reloadConfig();
}
Expand All @@ -232,13 +203,37 @@ public boolean reloadConfig() {
return false;
}

this.parser.load(this.configFile);
if (!this.parser.parse(this.format)) {
this.format.clear();
final InputStream inputStream = getInputStream();
if (inputStream == null) {
return false;
}

return true;
if (this.parser.load(inputStream) && this.parser.parse(this.format)) {
return true;
}

this.format.clear();
return false;
}

private InputStream getInputStream() {
InputStream inputStream = null;

try {
if (this.configFile != null && this.configFile.exists()) {
Reference.logger.debug("Loading file config...");
inputStream = new FileInputStream(this.configFile);
} else {
Reference.logger.debug("Loading default config...");
ResourceLocation resourceLocation = new ResourceLocation("ingameinfo", "InGameInfo.xml");
IResource resource = this.minecraft.getResourceManager().getResource(resourceLocation);
inputStream = resource.getInputStream();
}
} catch (Exception e) {
Reference.logger.error("", e);
}

return inputStream;
}

public boolean saveConfig(String filename) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
import com.github.lunatrius.ingameinfo.Alignment;
import com.github.lunatrius.ingameinfo.Value;

import java.io.File;
import java.io.InputStream;
import java.util.List;
import java.util.Map;

public interface IParser {
public boolean load(File file);
public boolean load(InputStream inputStream);

public boolean parse(Map<Alignment, List<List<Value>>> format);
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;

import java.io.File;
import java.io.FileReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
Expand All @@ -22,25 +22,28 @@ public class JsonParser implements IParser {
private JsonElement element;

@Override
public boolean load(File file) {
public boolean load(InputStream inputStream) {
try {
FileReader fileReader = new FileReader(file);
InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "UTF-8");
com.google.gson.JsonParser parser = new com.google.gson.JsonParser();

this.element = parser.parse(fileReader);
this.element = parser.parse(inputStreamReader);

fileReader.close();

return true;
inputStreamReader.close();
} catch (Exception e) {
Reference.logger.fatal("Could not read json configuration file!", e);
return false;
}

return false;
return true;
}

@Override
public boolean parse(Map<Alignment, List<List<Value>>> format) {
if (!this.element.isJsonObject()) {
return false;
}

JsonObject config = this.element.getAsJsonObject();
Set<Map.Entry<String, JsonElement>> entries = config.entrySet();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
import com.github.lunatrius.ingameinfo.reference.Reference;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
Expand All @@ -32,27 +32,26 @@ private Token nextToken() {
}

@Override
public boolean load(File file) {
if (file.exists()) {
try {
FileReader fileReader = new FileReader(file);
BufferedReader reader = new BufferedReader(fileReader);
String line, content = "";

while ((line = reader.readLine()) != null) {
content += line + "\n";
}

reader.close();
fileReader.close();

this.tokenizer.tokenize(content);
} catch (Exception e) {
Reference.logger.fatal("Could not read text configuration file!", e);
public boolean load(InputStream inputStream) {
try {
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
BufferedReader reader = new BufferedReader(inputStreamReader);
String line, content = "";

while ((line = reader.readLine()) != null) {
content += line + "\n";
}

reader.close();
inputStreamReader.close();

this.tokenizer.tokenize(content);
} catch (Exception e) {
Reference.logger.fatal("Could not read text configuration file!", e);
return false;
}

return false;
return true;
}

@Override
Expand All @@ -64,7 +63,7 @@ public boolean parse(Map<Alignment, List<List<Value>>> format) {
expr = alignments(format) && this.token.isEof();
} catch (Exception e) {
expr = false;
e.printStackTrace();
Reference.logger.error("Parsing failed!", e);
}

return expr;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.File;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
Expand All @@ -23,15 +23,17 @@ public class XmlParser implements IParser {
private Document document;

@Override
public boolean load(File file) {
public boolean load(InputStream inputStream) {
try {
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
this.document = dBuilder.parse(file);
this.document = dBuilder.parse(inputStream);
this.document.getDocumentElement().normalize();
} catch (Exception e) {
Reference.logger.fatal("Could not read xml configuration file!", e);
return false;
}

return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ public class ClientProxy extends CommonProxy {
@Override
public void setupConfig(File file) {
this.core.setConfigDirectory(file);
this.core.copyDefaultConfig();
this.core.setConfigFile(ConfigurationHandler.configName);
this.core.reloadConfig();
}
Expand Down

0 comments on commit 526f082

Please sign in to comment.