package net.sourceforge.pain; import net.sourceforge.pain.data.*; import net.sourceforge.pain.db.*; import net.sourceforge.pain.logic.*; import net.sourceforge.pain.network.console.*; import net.sourceforge.pain.network.guitool.*; import net.sourceforge.pain.plugin.*; import net.sourceforge.pain.util.*; import java.io.*; import java.util.*; public final class Core { public static final Date serverStartTime = new Date(); public static final String VERSION = "0.44"; private static World world = null; private static net.sourceforge.pain.Time time = new net.sourceforge.pain.Time(); private static ConsoleManager cm = null; private static GuiToolSessionManager gts = null; private static PluginManager plm = null; private static PainDB db = null; private static LogicLoadingManager logicManager = new LogicLoadingManager(getApplicationPath() + "/classes"); private Core() { } public static PainDB getDB() { return db; } public static void setDB(PainDB db) { if (Core.db != null) { throw new RuntimeException("DB already inited!"); } Core.db = db; time.addListener(new WorldSaver()); } public static World getWorld() { return world; } protected static void setWorld(World world) { Core.world = world; } static void setConsoleManager(ConsoleManager consoleManager) { consoleManager.init(); cm = consoleManager; time.addListener(cm); } public static ConsoleManager getConsoleManager() { return cm; } public static net.sourceforge.pain.Time getTime() { return time; } /** * * @param eventClassSuffix event class name suffix within 'net.sourceforge.pain.logic' package * @param params - passed to event * @throws Exception */ public static void processEvent(String eventClassSuffix, Object params) throws Exception { processEvent(logicManager.provideEventClass(eventClassSuffix), params); } public static void processEvent(Class eventImplClass, Object params) throws Exception { Event e = (Event) eventImplClass.newInstance(); e.processEvent(params); } public static LogicLoadingManager getLogicLoader() { return logicManager; } public static String getApplicationPath() { String path = System.getProperty("net.sourceforge.pain.home"); if (path == null || path.length() == 0) { throw new RuntimeException("'net.sourceforge.pain.home' is null"); } return path; } public static byte[] getFileData(String name) throws IOException { Log.debug("Loading file:" + name); InputStream is = new FileInputStream(name); try { byte[] data = new byte[is.available()]; is.read(data); return data; } finally { is.close(); } } public static PluginManager getPluginManager() { return plm; } protected static void setPluginManager(PluginManager plm) { Core.plm = plm; } public static void timeStopped() { shutdown(); } public static void shutdown() { Log.debug("Flushing all data before exit!"); try { db.flush(); } catch (Exception e) { Log.error(e.getMessage(), e); } db.close(); Log.debug("Shutted down."); System.exit(0); } public static Iterator extentIterator(Class clazz) { return db.getDbClass(clazz).extentIterator(false); } public static Iterator extentWeakIterator(Class clazz) { return db.getDbClass(clazz).extentIterator(true); } static void setGuiToolSessionManager(GuiToolSessionManager guiToolSessionManager) { gts = guiToolSessionManager; time.addListener(gts); } public GuiToolSessionManager getGuiToolSessionManager() { return gts; } }