Skip to content

Commit

Permalink
Created new config reader
Browse files Browse the repository at this point in the history
Created config reader to work with MC config files outside of the FML
environment
  • Loading branch information
rheimus committed May 10, 2016
1 parent 07e212a commit c171645
Show file tree
Hide file tree
Showing 6 changed files with 233 additions and 73 deletions.
123 changes: 53 additions & 70 deletions src/main/java/com/superzanti/serversync/SyncConfig.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.superzanti.serversync;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
Expand All @@ -12,6 +11,9 @@
import java.util.List;

import com.superzanti.serversync.util.PathUtils;
import com.superzanti.serversync.util.MCConfigReader.MCCArray;
import com.superzanti.serversync.util.MCConfigReader.MCCElement;
import com.superzanti.serversync.util.MCConfigReader.MCCReader;

import cpw.mods.fml.common.event.FMLPreInitializationEvent;
import net.minecraftforge.common.config.ConfigCategory;
Expand Down Expand Up @@ -65,57 +67,34 @@ public static void init(File configFile) {
}
}

public static void getServerDetailsDirty(Path configFile) throws IOException {
public static void getServerDetails(Path configFile) throws IOException {
//TODO read as proper file format?
BufferedReader br = Files.newBufferedReader(configFile);
String chars = "";
while (true) {
if (br.ready()) {
chars += br.readLine();
} else {
break;
}
MCCReader cReader = new MCCReader(Files.newBufferedReader(configFile));
MCCArray eArray = new MCCArray();
MCCElement element;
while ((element = cReader.readNextElement()) != null) {
eArray.add(element);
}
chars = chars.replaceAll("[}{]", " ");
// System.out.println(chars);
MINECRAFT_PORT = Integer.parseInt(getChunk(chars, "MINECRAFT_PORT=").trim());
SERVER_IP = getChunk(chars, "SERVER_IP=").trim();
SERVER_PORT = Integer.parseInt(getChunk(chars, "SERVER_PORT=").trim());
SECURE_CHECK = getChunk(chars, "SECURE_CHECK=").trim();
SECURE_CHECKMODS = getChunk(chars, "SECURE_CHECKMODS=").trim();
SECURE_RECURSIVE = getChunk(chars, "SECURE_RECURSIVE=").trim();
SECURE_CHECKSUM = getChunk(chars, "SECURE_CHECKSUM=").trim();
SECURE_UPDATE = getChunk(chars, "SECURE_UPDATE=").trim();
SECURE_EXISTS = getChunk(chars, "SECURE_EXISTS=").trim();
SECURE_EXIT = getChunk(chars, "SECURE_EXIT=").trim();
LAST_UPDATE = getChunk(chars, "LAST_UPDATE=").trim();
PUSH_CLIENT_MODS = Boolean.valueOf(getChunk(chars, "PushClientMods=").trim());
IGNORE_LIST = getArray(chars, "IGNORE_LIST");
INCLUDE_LIST = getArray(chars, "INCLUDE_LIST");
cReader.close();

MINECRAFT_PORT = eArray.getElementByName("MINECRAFT_PORT").getInt();
SERVER_IP = eArray.getElementByName("SERVER_IP").getString();
SERVER_PORT = eArray.getElementByName("SERVER_PORT").getInt();
SECURE_CHECK = eArray.getElementByName("SECURE_CHECK").getString();
SECURE_CHECKMODS = eArray.getElementByName("SECURE_CHECKMODS").getString();
SECURE_RECURSIVE = eArray.getElementByName("SECURE_RECURSIVE").getString();
SECURE_CHECKSUM = eArray.getElementByName("SECURE_CHECKSUM").getString();
SECURE_UPDATE = eArray.getElementByName("SECURE_UPDATE").getString();
SECURE_EXISTS = eArray.getElementByName("SECURE_EXISTS").getString();
SECURE_EXIT = eArray.getElementByName("SECURE_EXIT").getString();
LAST_UPDATE = eArray.getElementByName("LAST_UPDATE").getString();
PUSH_CLIENT_MODS = eArray.getElementByName("PUSH_CLIENT_MODS").getBoolean();
IGNORE_LIST = eArray.getElementByName("MOD_IGNORE_LIST").getList();
INCLUDE_LIST = eArray.getElementByName("CONFIG_INCLUDE_LIST").getList();

System.out.println("finished loading config");
}

private static String getChunk(String config, String target) {
String proc = "";
proc = config.substring(config.indexOf(target) + target.length());
proc = proc.substring(0, proc.indexOf(" "));

return proc;
}

private static List<String> getArray(String config, String target) {
List<String> proc = new ArrayList<String>();
String _proc = "";
_proc = config.substring(config.indexOf(target) + target.length());
_proc = _proc.substring(2, _proc.indexOf(">"));
String[] dirtyArray = _proc.split(" ");
for (String e : dirtyArray) {
proc.add(e.trim());
}
return proc;
}

private static void setupConfig() throws IOException {
//TODO add accept client mods for client config file
config.load();
Expand Down Expand Up @@ -159,34 +138,38 @@ private static void setupConfig() throws IOException {
if (PUSH_CLIENT_MODS) {
String[] oldList = ignoreList.getStringList();
Path clientMods = Paths.get("clientmods/");
ArrayList<Path> files = PathUtils.fileListDeep(clientMods);
ArrayList<String> saveableFiles = new ArrayList<String>();

for (Path path : files) {
boolean found = false;
String saveable = path.getFileName().toString();
// Duplicate check
for (String oldPath : oldList) {
if (oldPath.equals(saveable)) {
found = true;
break;
if (Files.exists(clientMods)) {
ArrayList<Path> files = PathUtils.fileListDeep(clientMods);
ArrayList<String> saveableFiles = new ArrayList<String>();

for (Path path : files) {
boolean found = false;
String saveable = path.getFileName().toString();
// Duplicate check
for (String oldPath : oldList) {
if (oldPath.equals(saveable)) {
found = true;
break;
}
}
if (!found) {
// file not found in ignore list
saveableFiles.add(saveable);
}
}
if (!found) {
// file not found in ignore list
saveableFiles.add(saveable);

for (String fileName : oldList) {
// add in previous entries
saveableFiles.add(fileName);
}
// for the lulz, should sort files with mods first followed by configs
Collections.sort(saveableFiles);
Collections.reverse(saveableFiles);

ignoreList.set(saveableFiles.toArray(new String[] {}));
} else {
Files.createDirectories(clientMods);
}

for (String path : oldList) {
// add in previous entries
saveableFiles.add(path);
}
// for the lulz, should sort files with mods first followed by configs
Collections.sort(saveableFiles);
Collections.reverse(saveableFiles);

ignoreList.set(saveableFiles.toArray(new String[] {}));
}

IGNORE_LIST = Arrays.asList(ignoreList.getStringList());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.superzanti.serversync.util.MCConfigReader;

import java.util.ArrayList;

public class MCCArray extends ArrayList<MCCElement> {


/**
*
*/
private static final long serialVersionUID = -8982760081192740589L;

public MCCElement getElementByName(String name) {
for (MCCElement e : this) {
if (e.getName().equals(name)) {
return e;
}
}
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package com.superzanti.serversync.util.MCConfigReader;

import java.lang.reflect.Type;
import java.util.ArrayList;

public class MCCElement {
private final String category;
private Type type;
private String value;
private String name;
private ArrayList<String> values;
public final boolean isArray;

public MCCElement(String category,String type,String name,String value) {
this.category = category;
this.name = name;
setType(type);
this.isArray = false;
this.value = value;
this.values = null;
}

public MCCElement(String category,String type,String name, ArrayList<String> values) {
this.category = category;
this.name = name;
setType(type);
this.values = values;
this.isArray = true;
this.value = null;
}

private void setType(String type) {
if (type.equals("B")) {
this.type = Boolean.class;
}
if (type.equals("S")) {
this.type = String.class;
}
if (type.equals("I")) {
this.type = Integer.class;
}
}

public ArrayList<String> getList() {
if (isArray) {
return values;
}
return null;
}

public String getCategoryName() {
return category;
}

public java.lang.reflect.Type getType() {
return type;
}

public boolean getBoolean() {
return Boolean.valueOf(value);
}

public String getName() {
return name;
}

public String getString() {
return value;
}

public int getInt() {
return Integer.parseInt(value);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package com.superzanti.serversync.util.MCConfigReader;

import java.io.BufferedReader;
import java.io.IOException;
import java.util.ArrayList;

public class MCCReader extends BufferedReader {

//TODO create separate server/client config files
//TODO have server send handshake secure codes and remove from clients config

public String category;

public MCCReader(BufferedReader read) throws IOException {
super(read);
}

public MCCElement readNextElement() throws IOException {
String line;
while ((line = this.readLine()) != null) {
if (line.contains("#")) {
continue;
}
if (line.contains("}")) {
category = null;
continue;
}
if (line.contains("{")) {
String[] cat = line.trim().split(" ");
// Should get category name
category = cat[0];
// Move to next line
continue;
}
if (line.contains(":") && line.contains("=")) {
String type = getType(line);
String name = getName(line);
String value = getValue(line);
return new MCCElement(category,type,name,value);
}
if (line.contains(":") && line.contains("<")) {
String type = getType(line);
String name = getName(line);
return new MCCElement(category,type,name,getValues());
}
}
return null;
}

private ArrayList<String> getValues() throws IOException {
ArrayList<String> temp = new ArrayList<String>();
String line;
while (true) {
line = this.readLine();
if (line.contains(">")) {
break;
}
temp.add(line.replace(",", "").trim());
}
return temp;
}

private String getType(String line) {
String sub = line.substring(line.indexOf(":") - 1, line.indexOf(":")).trim();
return sub;
}

private String getName(String line) {
String sub;
if (line.contains("=")) {
sub = line.substring(line.indexOf(":")+1,line.indexOf("=")).trim();
return sub;
}
sub = line.substring(line.indexOf(":")+1,line.indexOf("<")).trim();
return sub;
}

private String getValue(String line) {
String sub = line.substring(line.indexOf("=")+1).trim();
return sub;
}
}
2 changes: 1 addition & 1 deletion src/main/java/com/superzanti/serversync/util/Server.java
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ public boolean getConfig() throws IOException {

logs.updateLogs("Sucessfully updated config");
logs.updateLogs("Reloading config");
SyncConfig.getServerDetailsDirty(config);
SyncConfig.getServerDetails(config);
return true;
}

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/runme/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.superzanti.lib.RefStrings;
import com.superzanti.serversync.ClientWorker;
import com.superzanti.serversync.SyncConfig;
import com.superzanti.serversync.util.MCConfigReader.MCCReader;

public class Main {

Expand Down Expand Up @@ -141,7 +142,7 @@ public void actionPerformed(ActionEvent e) {
System.out.println("attempting to init config file: " + config.toAbsolutePath().toString());
// ServerSyncConfig.init(config.toFile());
try {
SyncConfig.getServerDetailsDirty(config);
SyncConfig.getServerDetails(config);
} catch (IOException e) {
e.printStackTrace();
return;
Expand All @@ -155,7 +156,6 @@ public void actionPerformed(ActionEvent e) {
}

public static void main(String[] args) throws InterruptedException, IOException {

SwingUtilities.invokeLater(new Runnable() {

@Override
Expand Down

0 comments on commit c171645

Please sign in to comment.