summaryrefslogtreecommitdiffstats
path: root/EssentialsAntiCheat/src/com/earth2me/essentials/anticheat/config/ConfigurationManager.java
blob: 283ad88d2a407a845a7755008ab28190d67de531 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
package com.earth2me.essentials.anticheat.config;

import com.earth2me.essentials.anticheat.NoCheat;
import java.io.File;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.logging.*;


/**
 * Central location for everything that's described in the configuration file(s)
 *
 */
public class ConfigurationManager
{
	private final static String configFileName = "config.yml";
	private final Map<String, ConfigurationCacheStore> worldnameToConfigCacheMap = new HashMap<String, ConfigurationCacheStore>();
	private FileHandler fileHandler;
	private final NoCheat plugin;


	private static class LogFileFormatter extends Formatter
	{
		private final SimpleDateFormat date;

		public LogFileFormatter()
		{
			date = new SimpleDateFormat("yy.MM.dd HH:mm:ss");
		}

		@Override
		public String format(LogRecord record)
		{
			StringBuilder builder = new StringBuilder();
			Throwable ex = record.getThrown();

			builder.append(date.format(record.getMillis()));
			builder.append(" [");
			builder.append(record.getLevel().getLocalizedName().toUpperCase());
			builder.append("] ");
			builder.append(record.getMessage());
			builder.append('\n');

			if (ex != null)
			{
				StringWriter writer = new StringWriter();
				ex.printStackTrace(new PrintWriter(writer));
				builder.append(writer);
			}

			return builder.toString();
		}
	}

	public ConfigurationManager(NoCheat plugin, File rootConfigFolder)
	{

		this.plugin = plugin;

		// Setup the real configuration
		initializeConfig(rootConfigFolder);

	}

	/**
	 * Read the configuration file and assign either standard values or whatever is declared in the file
	 *
	 * @param configurationFile
	 */
	private void initializeConfig(File rootConfigFolder)
	{

		// First try to obtain and parse the global config file
		NoCheatConfiguration root = new NoCheatConfiguration();
		root.setDefaults(new DefaultConfiguration());
		root.options().copyDefaults(true);
		root.options().copyHeader(true);

		File globalConfigFile = getGlobalConfigFile(rootConfigFolder);

		if (globalConfigFile.exists())
		{
			try
			{
				root.load(globalConfigFile);
			}
			catch (Exception e)
			{
				e.printStackTrace();
			}
		}

		try
		{
			root.save(globalConfigFile);
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}

		root.regenerateActionLists();

		// Create a corresponding Configuration Cache
		// put the global config on the config map
		worldnameToConfigCacheMap.put(null, new ConfigurationCacheStore(root));

		plugin.setFileLogger(setupFileLogger(new File(rootConfigFolder, root.getString(ConfPaths.LOGGING_FILENAME))));

		// Try to find world-specific config files
		Map<String, File> worldFiles = getWorldSpecificConfigFiles(rootConfigFolder);

		for (Entry<String, File> worldEntry : worldFiles.entrySet())
		{

			File worldConfigFile = worldEntry.getValue();

			NoCheatConfiguration world = new NoCheatConfiguration();
			world.setDefaults(root);

			try
			{
				world.load(worldConfigFile);

				worldnameToConfigCacheMap.put(worldEntry.getKey(), new ConfigurationCacheStore(world));

				// write the config file back to disk immediately
				world.save(worldConfigFile);

			}
			catch (Exception e)
			{
				plugin.getLogger().warning("Couldn't load world-specific config for " + worldEntry.getKey());
				e.printStackTrace();
			}

			world.regenerateActionLists();
		}
	}

	private static File getGlobalConfigFile(File rootFolder)
	{

		File globalConfig = new File(rootFolder, configFileName);

		return globalConfig;
	}

	private static Map<String, File> getWorldSpecificConfigFiles(File rootFolder)
	{

		HashMap<String, File> files = new HashMap<String, File>();

		if (rootFolder.isDirectory())
		{
			for (File f : rootFolder.listFiles())
			{
				if (f.isFile())
				{
					String filename = f.getName();
					if (filename.matches(".+_" + configFileName + "$"))
					{
						// Get the first part = world name
						String worldname = filename.substring(0, filename.length() - (configFileName.length() + 1));
						files.put(worldname, f);
					}
				}
			}
		}
		return files;
	}

	private Logger setupFileLogger(File logfile)
	{

		Logger l = Logger.getAnonymousLogger();
		l.setLevel(Level.INFO);
		// Ignore parent's settings
		l.setUseParentHandlers(false);
		for (Handler h : l.getHandlers())
		{
			l.removeHandler(h);
		}

		if (fileHandler != null)
		{
			fileHandler.close();
			l.removeHandler(fileHandler);
			fileHandler = null;
		}

		try
		{
			try
			{
				logfile.getParentFile().mkdirs();
			}
			catch (Exception e)
			{
				e.printStackTrace();
			}
			fileHandler = new FileHandler(logfile.getCanonicalPath(), true);
			fileHandler.setLevel(Level.ALL);
			fileHandler.setFormatter(new LogFileFormatter());

			l.addHandler(fileHandler);
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}

		return l;
	}

	/**
	 * Reset the loggers and flush and close the fileHandlers to be able to use them next time without problems
	 */
	public void cleanup()
	{
		fileHandler.flush();
		fileHandler.close();
		Logger l = Logger.getLogger("NoCheat");
		l.removeHandler(fileHandler);
		fileHandler = null;
	}

	/**
	 * Get the cache of the specified world, or the default cache, if no cache exists for that world.
	 *
	 * @param worldname
	 * @return
	 */
	public ConfigurationCacheStore getConfigurationCacheForWorld(String worldname)
	{

		ConfigurationCacheStore cache = worldnameToConfigCacheMap.get(worldname);

		if (cache != null)
		{
			return cache;
		}
		else
		{
			// Enter a reference to the cache under the new name
			// to be faster in looking it up later
			cache = worldnameToConfigCacheMap.get(null);
			worldnameToConfigCacheMap.put(worldname, cache);

			return cache;
		}
	}
}