-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9ab942c
commit a8aa047
Showing
1 changed file
with
48 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,72 @@ | ||
package glowredman.defaultserverlist; | ||
|
||
import java.io.File; | ||
import java.io.InputStream; | ||
import java.io.Reader; | ||
import java.lang.reflect.Type; | ||
import java.net.URL; | ||
import java.nio.charset.StandardCharsets; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Map.Entry; | ||
|
||
import org.apache.commons.io.IOUtils; | ||
|
||
import com.google.common.reflect.TypeToken; | ||
import com.google.gson.Gson; | ||
import com.google.gson.GsonBuilder; | ||
|
||
import cpw.mods.fml.common.FMLLog; | ||
import net.minecraft.client.multiplayer.ServerData; | ||
import net.minecraftforge.common.config.Configuration; | ||
|
||
public class Config { | ||
|
||
public static final List<ServerData> SERVERS = new ArrayList<>(); | ||
|
||
public static void preInit(File configDir) { | ||
Configuration config = new Configuration(new File(configDir, "defaultserverlist.cfg")); | ||
config.load(); | ||
|
||
boolean useURL = config.get("general", "useURL", false).getBoolean(); | ||
String url = config.get("general", "url", "").getString(); | ||
String[] servers = config.get("general", "servers", new String[0], "Pattern: NAME;URL").getStringList(); | ||
|
||
if(config.hasChanged()) { | ||
config.save(); | ||
} | ||
|
||
if(useURL) { | ||
InputStream inputStream = null; | ||
try { | ||
inputStream = new URL(url).openStream(); | ||
servers = IOUtils.toString(inputStream, StandardCharsets.UTF_8).split("\n"); | ||
} catch (Exception e) { | ||
FMLLog.severe("Could not parse default server list from URL!"); | ||
e.printStackTrace(); | ||
} finally { | ||
IOUtils.closeQuietly(inputStream); | ||
Reader fileReader = null; | ||
try { | ||
Gson gson = new GsonBuilder().setPrettyPrinting().create(); | ||
Path path = configDir.toPath().resolve("defaultserverlist.json"); | ||
ConfigObj config = new ConfigObj(); | ||
|
||
if(!Files.exists(path)) { | ||
Files.write(path, Arrays.asList(gson.toJson(config)), StandardCharsets.UTF_8); | ||
} else { | ||
fileReader = Files.newBufferedReader(path, StandardCharsets.UTF_8); | ||
config = gson.fromJson(fileReader, ConfigObj.class); | ||
} | ||
} | ||
for(String s : servers) { | ||
String[] info = s.split(";", 2); | ||
if(info.length < 2) { | ||
continue; | ||
|
||
Map<String, String> servers; | ||
if(config.useURL) { | ||
Type type = new TypeToken<Map<String, String>>() {}.getType(); | ||
servers = gson.fromJson(IOUtils.toString(new URL(config.url), StandardCharsets.UTF_8), type); | ||
} else { | ||
servers = config.servers; | ||
} | ||
SERVERS.add(new ServerData(info[0], info[1])); | ||
|
||
for(Entry<String, String> e : servers.entrySet()) { | ||
SERVERS.add(new ServerData(e.getKey(), e.getValue())); | ||
} | ||
|
||
} catch (Exception e) { | ||
FMLLog.severe("Could not parse default server list from URL!"); | ||
e.printStackTrace(); | ||
} finally { | ||
IOUtils.closeQuietly(fileReader); | ||
} | ||
} | ||
|
||
private static final class ConfigObj { | ||
|
||
public boolean useURL = false; | ||
public String url = ""; | ||
public Map<String, String> servers = new HashMap<>(); | ||
|
||
} | ||
|
||
} |