2018-07-06 15:30:00 +02:00
|
|
|
package com.eu.habbo.core;
|
|
|
|
|
|
|
|
import com.eu.habbo.Emulator;
|
|
|
|
import com.eu.habbo.plugin.events.emulator.EmulatorConfigUpdatedEvent;
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.FileInputStream;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.sql.*;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.Properties;
|
|
|
|
|
|
|
|
public class ConfigurationManager
|
|
|
|
{
|
2018-07-08 23:32:00 +02:00
|
|
|
|
2018-07-06 15:30:00 +02:00
|
|
|
public boolean loaded = false;
|
|
|
|
|
2018-07-08 23:32:00 +02:00
|
|
|
|
2018-07-06 15:30:00 +02:00
|
|
|
public boolean isLoading = false;
|
|
|
|
|
2018-07-08 23:32:00 +02:00
|
|
|
|
2018-07-06 15:30:00 +02:00
|
|
|
private final Properties properties;
|
|
|
|
|
|
|
|
public ConfigurationManager(String path) throws Exception
|
|
|
|
{
|
|
|
|
this.properties = new Properties();
|
|
|
|
|
|
|
|
this.reload();
|
|
|
|
}
|
|
|
|
|
2018-07-08 23:32:00 +02:00
|
|
|
|
2018-07-06 15:30:00 +02:00
|
|
|
public void reload() throws Exception
|
|
|
|
{
|
|
|
|
this.isLoading = true;
|
|
|
|
this.properties.clear();
|
|
|
|
|
|
|
|
InputStream input = null;
|
|
|
|
|
|
|
|
try {
|
|
|
|
File f = new File("config.ini");
|
|
|
|
input = new FileInputStream(f);
|
|
|
|
this.properties.load(input);
|
|
|
|
|
|
|
|
} catch (IOException ex) {
|
|
|
|
Emulator.getLogging().logErrorLine("[CRITICAL] FAILED TO LOAD CONFIG.INI FILE!");
|
|
|
|
} finally {
|
|
|
|
if (input != null) {
|
|
|
|
try {
|
|
|
|
input.close();
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(loaded)
|
|
|
|
{
|
|
|
|
this.loadFromDatabase();
|
|
|
|
}
|
|
|
|
|
|
|
|
this.isLoading = false;
|
|
|
|
Emulator.getLogging().logStart("Configuration Manager -> Loaded!");
|
|
|
|
|
|
|
|
if (Emulator.getPluginManager() != null)
|
|
|
|
{
|
|
|
|
Emulator.getPluginManager().fireEvent(new EmulatorConfigUpdatedEvent());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-08 23:32:00 +02:00
|
|
|
|
2018-07-06 15:30:00 +02:00
|
|
|
public void loadFromDatabase()
|
|
|
|
{
|
|
|
|
Emulator.getLogging().logStart("Loading configuration from database...");
|
|
|
|
|
|
|
|
long millis = System.currentTimeMillis();
|
|
|
|
try (Connection connection = Emulator.getDatabase().getDataSource().getConnection(); Statement statement = connection.createStatement())
|
|
|
|
{
|
|
|
|
if (statement.execute("SELECT * FROM emulator_settings"))
|
|
|
|
{
|
|
|
|
try (ResultSet set = statement.getResultSet())
|
|
|
|
{
|
|
|
|
while (set.next())
|
|
|
|
{
|
|
|
|
this.properties.put(set.getString("key"), set.getString("value"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (SQLException e)
|
|
|
|
{
|
|
|
|
Emulator.getLogging().logSQLException(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
Emulator.getLogging().logStart("Configuration -> loaded! (" + (System.currentTimeMillis() - millis) + " MS)");
|
|
|
|
}
|
|
|
|
|
|
|
|
public void saveToDatabase()
|
|
|
|
{
|
|
|
|
try (Connection connection = Emulator.getDatabase().getDataSource().getConnection(); PreparedStatement statement = connection.prepareStatement("UPDATE emulator_settings SET `value` = ? WHERE `key` = ? LIMIT 1"))
|
|
|
|
{
|
|
|
|
for (Map.Entry<Object, Object> entry : this.properties.entrySet())
|
|
|
|
{
|
|
|
|
statement.setString(1, entry.getValue().toString());
|
|
|
|
statement.setString(2, entry.getKey().toString());
|
|
|
|
statement.executeUpdate();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (SQLException e)
|
|
|
|
{
|
|
|
|
Emulator.getLogging().logSQLException(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-08 23:32:00 +02:00
|
|
|
|
2018-07-06 15:30:00 +02:00
|
|
|
public String getValue(String key)
|
|
|
|
{
|
|
|
|
return getValue(key, "");
|
|
|
|
}
|
|
|
|
|
2018-07-08 23:32:00 +02:00
|
|
|
|
2018-07-06 15:30:00 +02:00
|
|
|
public String getValue(String key, String defaultValue)
|
|
|
|
{
|
|
|
|
if (this.isLoading)
|
|
|
|
return defaultValue;
|
|
|
|
|
|
|
|
if (!this.properties.containsKey(key)) {
|
|
|
|
Emulator.getLogging().logErrorLine("[CONFIG] Key not found: " + key);
|
|
|
|
}
|
|
|
|
return this.properties.getProperty(key, defaultValue);
|
|
|
|
}
|
|
|
|
|
2018-07-08 23:32:00 +02:00
|
|
|
|
2018-07-06 15:30:00 +02:00
|
|
|
public boolean getBoolean(String key)
|
|
|
|
{
|
|
|
|
return getBoolean(key, false);
|
|
|
|
}
|
|
|
|
|
2018-07-08 23:32:00 +02:00
|
|
|
|
2018-07-06 15:30:00 +02:00
|
|
|
public boolean getBoolean(String key, boolean defaultValue)
|
|
|
|
{
|
|
|
|
if (this.isLoading)
|
|
|
|
return defaultValue;
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
return (getValue(key, "0").equals("1")) || (getValue(key, "false").equals("true"));
|
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
Emulator.getLogging().logErrorLine("Failed to parse key " + key + " with value " + getValue(key) + " to type boolean.");
|
|
|
|
}
|
|
|
|
return defaultValue;
|
|
|
|
}
|
|
|
|
|
2018-07-08 23:32:00 +02:00
|
|
|
|
2018-07-06 15:30:00 +02:00
|
|
|
public int getInt(String key)
|
|
|
|
{
|
|
|
|
return getInt(key, 0);
|
|
|
|
}
|
|
|
|
|
2018-07-08 23:32:00 +02:00
|
|
|
|
2018-07-06 15:30:00 +02:00
|
|
|
public int getInt(String key, Integer defaultValue)
|
|
|
|
{
|
|
|
|
if (this.isLoading)
|
|
|
|
return defaultValue;
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
return Integer.parseInt(getValue(key, defaultValue.toString()));
|
|
|
|
} catch (Exception e)
|
|
|
|
{
|
|
|
|
Emulator.getLogging().logErrorLine("Failed to parse key " + key + " with value " + getValue(key) + " to type integer.");
|
|
|
|
}
|
|
|
|
return defaultValue;
|
|
|
|
}
|
|
|
|
|
2018-07-08 23:32:00 +02:00
|
|
|
|
2018-07-06 15:30:00 +02:00
|
|
|
public double getDouble(String key)
|
|
|
|
{
|
|
|
|
return getDouble(key, 0.0);
|
|
|
|
}
|
|
|
|
|
2018-07-08 23:32:00 +02:00
|
|
|
|
2018-07-06 15:30:00 +02:00
|
|
|
public double getDouble(String key, Double defaultValue)
|
|
|
|
{
|
|
|
|
if (this.isLoading)
|
|
|
|
return defaultValue;
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
return Double.parseDouble(getValue(key, defaultValue.toString()));
|
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
Emulator.getLogging().logErrorLine("Failed to parse key " + key + " with value " + getValue(key) + " to type double.");
|
|
|
|
}
|
|
|
|
|
|
|
|
return defaultValue;
|
|
|
|
}
|
|
|
|
|
2018-07-08 23:32:00 +02:00
|
|
|
|
2018-07-06 15:30:00 +02:00
|
|
|
public void update(String key, String value)
|
|
|
|
{
|
|
|
|
this.properties.setProperty(key, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void register(String key, String value)
|
|
|
|
{
|
|
|
|
if (this.properties.getProperty(key, null) != null)
|
|
|
|
return;
|
|
|
|
|
|
|
|
try (Connection connection = Emulator.getDatabase().getDataSource().getConnection(); PreparedStatement statement = connection.prepareStatement("INSERT INTO emulator_settings VALUES (?, ?)"))
|
|
|
|
{
|
|
|
|
statement.setString(1, key);
|
|
|
|
statement.setString(2, value);
|
|
|
|
statement.execute();
|
|
|
|
}
|
|
|
|
catch (SQLException e)
|
|
|
|
{
|
|
|
|
Emulator.getLogging().logSQLException(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.update(key, value);
|
|
|
|
}
|
|
|
|
}
|