From 978de20d5360298b3c5fbbf05a47c82746ab96f9 Mon Sep 17 00:00:00 2001 From: Joe0 Date: Sun, 4 Dec 2011 16:38:08 -0600 Subject: [PATCH] Framework to resolve issue #24 -- uses the DataStore interface and DataStoreFactory to read data from a DataStore. A new DataStore needs to be implemented to remove the XStream dependency. Methods to save each type of data need to be implemented. --- GameServer/conf/world.xml | 3 + GameServer/ivy.xml | 3 + .../src/org/moparscape/msc/config/Config.java | 3 + .../src/org/moparscape/msc/gs/Instance.java | 22 ++ .../moparscape/msc/gs/core/GameEngine.java | 6 +- .../msc/gs/core/LoginConnector.java | 5 +- .../msc/gs/external/EntityHandler.java | 145 ++++---- .../org/moparscape/msc/gs/io/WorldLoader.java | 16 +- .../org/moparscape/msc/gs/model/World.java | 5 +- .../msc/gs/persistence/DataStore.java | 104 ++++++ .../msc/gs/persistence/DataStoreFactory.java | 10 + .../gs/persistence/impl/XMLUsingXStream.java | 309 ++++++++++++++++++ .../msc/gs/util/PersistenceManager.java | 69 ---- .../src/org/moparscape/msc/ls/Server.java | 2 +- .../ls/persistence/StorageMediumFactory.java | 2 +- 15 files changed, 526 insertions(+), 178 deletions(-) create mode 100644 GameServer/src/org/moparscape/msc/gs/persistence/DataStore.java create mode 100644 GameServer/src/org/moparscape/msc/gs/persistence/DataStoreFactory.java create mode 100644 GameServer/src/org/moparscape/msc/gs/persistence/impl/XMLUsingXStream.java delete mode 100644 GameServer/src/org/moparscape/msc/gs/util/PersistenceManager.java diff --git a/GameServer/conf/world.xml b/GameServer/conf/world.xml index 0c2ffc8..4e7ed86 100644 --- a/GameServer/conf/world.xml +++ b/GameServer/conf/world.xml @@ -78,4 +78,7 @@ conf/alert-config.xml conf/command-config.xml + + + org.moparscape.msc.gs.persistence.impl.XMLUsingXStream diff --git a/GameServer/ivy.xml b/GameServer/ivy.xml index b68035c..12a0ddd 100644 --- a/GameServer/ivy.xml +++ b/GameServer/ivy.xml @@ -15,5 +15,8 @@ + + + diff --git a/GameServer/src/org/moparscape/msc/config/Config.java b/GameServer/src/org/moparscape/msc/config/Config.java index e1a050e..066c94e 100644 --- a/GameServer/src/org/moparscape/msc/config/Config.java +++ b/GameServer/src/org/moparscape/msc/config/Config.java @@ -42,6 +42,7 @@ public class Config { public static boolean OS_LEVEL_BLOCKING, APPLICATION_LEVEL_THROTTLE_ALERT, OS_LEVEL_THROTTLE_ALERT, OS_LEVEL_UNBLOCK_FAILED_ALERT, CONGRATS_FOR_MAX_LEVEL; + public static String DATA_STORE; static { loadEnv(); @@ -127,6 +128,8 @@ public class Config { .getProperty("wild-combat-bonus")); CONGRATS_FOR_MAX_LEVEL = Boolean.parseBoolean(props .getProperty("max-level-congrats")); + + DATA_STORE = props.getProperty("data-store"); props.clear(); diff --git a/GameServer/src/org/moparscape/msc/gs/Instance.java b/GameServer/src/org/moparscape/msc/gs/Instance.java index b65d754..8d48c67 100644 --- a/GameServer/src/org/moparscape/msc/gs/Instance.java +++ b/GameServer/src/org/moparscape/msc/gs/Instance.java @@ -1,9 +1,12 @@ package org.moparscape.msc.gs; +import org.moparscape.msc.config.Config; import org.moparscape.msc.gs.core.DelayedEventHandler; import org.moparscape.msc.gs.db.DBConnection; import org.moparscape.msc.gs.db.ReportHandlerQueries; import org.moparscape.msc.gs.model.World; +import org.moparscape.msc.gs.persistence.DataStore; +import org.moparscape.msc.gs.persistence.DataStoreFactory; import org.moparscape.msc.gs.plugins.dependencies.PluginHandler; /** @@ -33,4 +36,23 @@ public class Instance { public static ReportHandlerQueries getReport() { return DBConnection.getReport(); } + + private static DataStore dataStore; + + private static void createDataStore() { + try { + dataStore = DataStoreFactory.create(Config.DATA_STORE); + } catch (Exception e) { + e.printStackTrace(); + System.out.println("Could not create DataStore - " + Config.DATA_STORE); + System.exit(0); + } + } + + public static DataStore getDataStore() { + if(dataStore == null) { + createDataStore(); + } + return dataStore; + } } diff --git a/GameServer/src/org/moparscape/msc/gs/core/GameEngine.java b/GameServer/src/org/moparscape/msc/gs/core/GameEngine.java index 366f056..de40985 100644 --- a/GameServer/src/org/moparscape/msc/gs/core/GameEngine.java +++ b/GameServer/src/org/moparscape/msc/gs/core/GameEngine.java @@ -26,14 +26,13 @@ import org.moparscape.msc.gs.phandler.PacketHandlerDef; import org.moparscape.msc.gs.plugins.dependencies.NpcAI; import org.moparscape.msc.gs.tools.Captcha; import org.moparscape.msc.gs.util.Logger; -import org.moparscape.msc.gs.util.PersistenceManager; /** * The central motor of the game. This class is responsible for the primary * operation of the entire game. */ public final class GameEngine extends Thread { - + private static Captcha captcha; /** * World instance @@ -149,8 +148,7 @@ public final class GameEngine extends Thread { * Loads the packet handling classes from the persistence manager. */ protected void loadPacketHandlers() { - PacketHandlerDef[] handlerDefs = (PacketHandlerDef[]) PersistenceManager - .load("PacketHandlers.xml"); + PacketHandlerDef[] handlerDefs = Instance.getDataStore().loadPacketHandlerDefs(); for (PacketHandlerDef handlerDef : handlerDefs) { try { String className = handlerDef.getClassName(); diff --git a/GameServer/src/org/moparscape/msc/gs/core/LoginConnector.java b/GameServer/src/org/moparscape/msc/gs/core/LoginConnector.java index ff7a630..9e910f8 100644 --- a/GameServer/src/org/moparscape/msc/gs/core/LoginConnector.java +++ b/GameServer/src/org/moparscape/msc/gs/core/LoginConnector.java @@ -11,6 +11,7 @@ import org.apache.mina.transport.socket.nio.SocketConnector; import org.apache.mina.transport.socket.nio.SocketConnectorConfig; import org.apache.mina.transport.socket.nio.SocketSessionConfig; import org.moparscape.msc.config.Config; +import org.moparscape.msc.gs.Instance; import org.moparscape.msc.gs.builders.ls.MiscPacketBuilder; import org.moparscape.msc.gs.connection.LSConnectionHandler; import org.moparscape.msc.gs.connection.LSPacket; @@ -18,7 +19,6 @@ import org.moparscape.msc.gs.connection.PacketQueue; import org.moparscape.msc.gs.phandler.PacketHandler; import org.moparscape.msc.gs.phandler.PacketHandlerDef; import org.moparscape.msc.gs.util.Logger; -import org.moparscape.msc.gs.util.PersistenceManager; public class LoginConnector { /** @@ -139,8 +139,7 @@ public class LoginConnector { } private void loadPacketHandlers() { - PacketHandlerDef[] handlerDefs = (PacketHandlerDef[]) PersistenceManager - .load("LSPacketHandlers.xml"); + PacketHandlerDef[] handlerDefs = Instance.getDataStore().loadLSPacketHandlerDefs(); for (PacketHandlerDef handlerDef : handlerDefs) { try { String className = handlerDef.getClassName(); diff --git a/GameServer/src/org/moparscape/msc/gs/external/EntityHandler.java b/GameServer/src/org/moparscape/msc/gs/external/EntityHandler.java index 05a9612..9c01703 100644 --- a/GameServer/src/org/moparscape/msc/gs/external/EntityHandler.java +++ b/GameServer/src/org/moparscape/msc/gs/external/EntityHandler.java @@ -1,120 +1,95 @@ package org.moparscape.msc.gs.external; -import java.util.HashMap; import java.util.List; +import java.util.Map; +import org.moparscape.msc.gs.Instance; +import org.moparscape.msc.gs.model.InvItem; import org.moparscape.msc.gs.model.Point; import org.moparscape.msc.gs.model.TelePoint; -import org.moparscape.msc.gs.util.PersistenceManager; +import org.moparscape.msc.gs.persistence.DataStore; /** * This class handles the loading of entities from the conf files, and provides * methods for relaying these entities to the user. */ -@SuppressWarnings("unchecked") public class EntityHandler { - - private static HashMap agilityCourses; - private static HashMap agilityObjects; - private static HashMap arrowHeads; - private static HashMap bowString; - private static HashMap certers; - private static HashMap dartTips; + private static Map agilityCourses; + private static Map agilityObjects; + private static Map arrowHeads; + private static Map bowString; + private static Map certers; + private static Map dartTips; private static DoorDef[] doors; - private static HashMap firemaking; + private static Map firemaking; private static GameObjectDef[] gameObjects; - private static HashMap gems; + private static Map gems; private static ItemHerbSecond[] herbSeconds; - private static HashMap itemAffectedTypes; - private static HashMap itemCooking; + private static Map itemAffectedTypes; + private static Map itemCooking; private static ItemCraftingDef[] itemCrafting; - private static HashMap itemEdibleHeals; - private static HashMap itemHerb; + private static Map itemEdibleHeals; + private static Map itemHerb; private static ItemDef[] items; - private static HashMap itemSmelting; + private static Map itemSmelting; private static ItemSmithingDef[] itemSmithing; - private static HashMap itemUnIdentHerb; - private static HashMap itemWieldable; - private static List[] keyChestLoots; - private static HashMap logCut; + private static Map itemUnIdentHerb; + private static Map itemWieldable; + private static List[] keyChestLoots; + private static Map logCut; private static NPCDef[] npcs; - private static HashMap objectFishing; + private static Map objectFishing; - private static HashMap objectMining; - private static HashMap objectTelePoints; - private static HashMap objectWoodcutting; + private static Map objectMining; + private static Map objectTelePoints; + private static Map objectWoodcutting; private static PrayerDef[] prayers; - private static HashMap spellAggressiveLvl; + private static Map spellAggressiveLvl; private static SpellDef[] spells; private static TileDef[] tiles; static { - doors = (DoorDef[]) PersistenceManager.load("defs/DoorDef.xml.gz"); - gameObjects = (GameObjectDef[]) PersistenceManager - .load("defs/GameObjectDef.xml.gz"); - npcs = (NPCDef[]) PersistenceManager.load("defs/NPCDef.xml.gz"); + DataStore dataStore = Instance.getDataStore(); + doors = dataStore.loadDoorDefs(); + gameObjects = dataStore.loadGameObjectDefs(); + npcs = dataStore.loadNPCDefs(); for (NPCDef n : npcs) { if (n.isAttackable()) { n.respawnTime -= (n.respawnTime / 3); } } - prayers = (PrayerDef[]) PersistenceManager - .load("defs/PrayerDef.xml.gz"); - items = (ItemDef[]) PersistenceManager.load("defs/ItemDef.xml.gz"); - spells = (SpellDef[]) PersistenceManager.load("defs/SpellDef.xml.gz"); - tiles = (TileDef[]) PersistenceManager.load("defs/TileDef.xml.gz"); - keyChestLoots = (List[]) PersistenceManager - .load("defs/extras/KeyChestLoot.xml.gz"); - herbSeconds = (ItemHerbSecond[]) PersistenceManager - .load("defs/extras/ItemHerbSecond.xml.gz"); - dartTips = (HashMap) PersistenceManager - .load("defs/extras/ItemDartTipDef.xml.gz"); - gems = (HashMap) PersistenceManager - .load("defs/extras/ItemGemDef.xml.gz"); - logCut = (HashMap) PersistenceManager - .load("defs/extras/ItemLogCutDef.xml.gz"); - bowString = (HashMap) PersistenceManager - .load("defs/extras/ItemBowStringDef.xml.gz"); - arrowHeads = (HashMap) PersistenceManager - .load("defs/extras/ItemArrowHeadDef.xml.gz"); - firemaking = (HashMap) PersistenceManager - .load("defs/extras/FiremakingDef.xml.gz"); - itemAffectedTypes = (HashMap) PersistenceManager - .load("defs/extras/ItemAffectedTypes.xml.gz"); - itemWieldable = (HashMap) PersistenceManager - .load("defs/extras/ItemWieldableDef.xml.gz"); - itemUnIdentHerb = (HashMap) PersistenceManager - .load("defs/extras/ItemUnIdentHerbDef.xml.gz"); - itemHerb = (HashMap) PersistenceManager - .load("defs/extras/ItemHerbDef.xml.gz"); - itemEdibleHeals = (HashMap) PersistenceManager - .load("defs/extras/ItemEdibleHeals.xml.gz"); - itemCooking = (HashMap) PersistenceManager - .load("defs/extras/ItemCookingDef.xml.gz"); - itemSmelting = (HashMap) PersistenceManager - .load("defs/extras/ItemSmeltingDef.xml.gz"); - itemSmithing = (ItemSmithingDef[]) PersistenceManager - .load("defs/extras/ItemSmithingDef.xml.gz"); - itemCrafting = (ItemCraftingDef[]) PersistenceManager - .load("defs/extras/ItemCraftingDef.xml.gz"); - objectMining = (HashMap) PersistenceManager - .load("defs/extras/ObjectMining.xml.gz"); - objectWoodcutting = (HashMap) PersistenceManager - .load("defs/extras/ObjectWoodcutting.xml.gz"); - objectFishing = (HashMap) PersistenceManager - .load("defs/extras/ObjectFishing.xml.gz"); - spellAggressiveLvl = (HashMap) PersistenceManager - .load("defs/extras/SpellAggressiveLvl.xml.gz"); - objectTelePoints = (HashMap) PersistenceManager - .load("locs/extras/ObjectTelePoints.xml.gz"); - certers = (HashMap) PersistenceManager - .load("defs/extras/NpcCerters.xml.gz"); - agilityObjects = (HashMap) PersistenceManager - .load("defs/extras/AgilityDef.xml.gz"); - agilityCourses = (HashMap) PersistenceManager - .load("defs/extras/AgilityCourseDef.xml.gz"); + prayers = dataStore.loadPrayerDefs(); + items = dataStore.loadItemDefs(); + spells = dataStore.loadSpellDefs(); + tiles = dataStore.loadTileDefs(); + keyChestLoots = dataStore.loadKeyChestLoots(); + herbSeconds = dataStore.loadItemHerbSeconds(); + dartTips = dataStore.loadDartTips(); + gems = dataStore.loadGemDefs(); + logCut = dataStore.loadItemLogCutDefs(); + bowString = dataStore.loadItemBowStringDefs(); + arrowHeads = dataStore.loadItemArrowHeadDefs(); + firemaking = dataStore.loadFiremakingDefs(); + itemAffectedTypes = dataStore.loadItemAffectedTypes(); + itemWieldable = dataStore.loadItemWieldableDefs(); + itemUnIdentHerb = dataStore.loadItemUnIdentHerbDefs(); + itemHerb = dataStore.loadItemHerbDefs(); + itemEdibleHeals = dataStore.loadItemEdibleHeals(); + itemCooking = dataStore.loadItemCookingDefs(); + itemSmelting = dataStore.loadItemSmeltingDefs(); + itemSmithing = dataStore.loadItemSmithingDefs(); + itemCrafting = dataStore.loadItemCraftingDefs(); + objectMining = dataStore.loadObjectMiningDefs(); + objectWoodcutting = dataStore.loadObjectWoodcuttingDefs(); + objectFishing = dataStore.loadObjectFishDefs(); + spellAggressiveLvl = dataStore.loadSpellAgressiveLevel(); + objectTelePoints = dataStore.loadTelePoints(); + certers = dataStore.loadCerterDefs(); + agilityObjects = dataStore.loadAgilityDefs(); + agilityCourses = dataStore.loadAgilityCourseDefs(); + dataStore.dispose(); } /** diff --git a/GameServer/src/org/moparscape/msc/gs/io/WorldLoader.java b/GameServer/src/org/moparscape/msc/gs/io/WorldLoader.java index 1e171d0..7f176c8 100644 --- a/GameServer/src/org/moparscape/msc/gs/io/WorldLoader.java +++ b/GameServer/src/org/moparscape/msc/gs/io/WorldLoader.java @@ -3,7 +3,6 @@ package org.moparscape.msc.gs.io; import java.io.BufferedInputStream; import java.io.File; import java.nio.ByteBuffer; -import java.util.List; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; @@ -22,7 +21,6 @@ import org.moparscape.msc.gs.model.Shop; import org.moparscape.msc.gs.model.World; import org.moparscape.msc.gs.tools.DataConversions; import org.moparscape.msc.gs.util.Logger; -import org.moparscape.msc.gs.util.PersistenceManager; public class WorldLoader { private ZipFile tileArchive; @@ -129,7 +127,6 @@ public class WorldLoader { * } */ - @SuppressWarnings("unchecked") public void loadWorld(World world) { try { tileArchive = new ZipFile(new File(Config.CONF_DIR, @@ -154,26 +151,22 @@ public class WorldLoader { } Logger.error((System.currentTimeMillis() - now) / 1000 + "s to parse"); // try { out.close(); } catch(Exception e) { Logger.error(e); } - for (Shop shop : (List) PersistenceManager - .load("locs/Shops.xml.gz")) { + for (Shop shop : Instance.getDataStore().loadShops()) { world.registerShop(shop); } System.gc(); } - @SuppressWarnings("unchecked") public void loadObjects() { World world = Instance.getWorld(); - for (GameObjectLoc gameObject : (List) PersistenceManager - .load("locs/GameObjectLoc.xml.gz")) { + for (GameObjectLoc gameObject : Instance.getDataStore().loadGameObjectLocs()) { if (Config.f2pWildy && Formulae.isP2P(true, gameObject)) continue; if (Formulae.isP2P(gameObject) && !World.isMembers()) continue; world.registerGameObject(new GameObject(gameObject)); } - for (ItemLoc item : (List) PersistenceManager - .load("locs/ItemLoc.xml.gz")) { + for (ItemLoc item : Instance.getDataStore().loadItemLocs()) { if (Config.f2pWildy && Formulae.isP2P(true, item)) continue; if (Formulae.isP2P(item) && !World.isMembers()) @@ -181,8 +174,7 @@ public class WorldLoader { world.registerItem(new Item(item)); }// ember - for (NPCLoc npc : (List) PersistenceManager - .load("locs/NpcLoc.xml.gz")) { + for (NPCLoc npc : Instance.getDataStore().loadNPCLocs()) { if (Config.f2pWildy && Formulae.isP2P(true, npc)) continue; if (Formulae.isP2P(npc) && !World.isMembers()) diff --git a/GameServer/src/org/moparscape/msc/gs/model/World.java b/GameServer/src/org/moparscape/msc/gs/model/World.java index 9848d0f..be76e12 100644 --- a/GameServer/src/org/moparscape/msc/gs/model/World.java +++ b/GameServer/src/org/moparscape/msc/gs/model/World.java @@ -9,6 +9,7 @@ import java.util.List; import java.util.TreeMap; import org.moparscape.msc.config.Config; +import org.moparscape.msc.gs.Instance; import org.moparscape.msc.gs.Server; import org.moparscape.msc.gs.core.ClientUpdater; import org.moparscape.msc.gs.core.DelayedEventHandler; @@ -25,7 +26,6 @@ import org.moparscape.msc.gs.quest.QuestManager; import org.moparscape.msc.gs.states.CombatState; import org.moparscape.msc.gs.util.EntityList; import org.moparscape.msc.gs.util.Logger; -import org.moparscape.msc.gs.util.PersistenceManager; public final class World { @@ -471,8 +471,7 @@ public final class World { */ private void loadNpcHandlers() { - NpcHandlerDef[] handlerDefs = (NpcHandlerDef[]) PersistenceManager - .load("NpcHandlers.xml"); + NpcHandlerDef[] handlerDefs = Instance.getDataStore().loadNpcHandlers(); for (NpcHandlerDef handlerDef : handlerDefs) { try { String className = handlerDef.getClassName(); diff --git a/GameServer/src/org/moparscape/msc/gs/persistence/DataStore.java b/GameServer/src/org/moparscape/msc/gs/persistence/DataStore.java new file mode 100644 index 0000000..a4d836c --- /dev/null +++ b/GameServer/src/org/moparscape/msc/gs/persistence/DataStore.java @@ -0,0 +1,104 @@ +package org.moparscape.msc.gs.persistence; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.moparscape.msc.gs.external.*; +import org.moparscape.msc.gs.model.InvItem; +import org.moparscape.msc.gs.model.Point; +import org.moparscape.msc.gs.model.Shop; +import org.moparscape.msc.gs.model.TelePoint; +import org.moparscape.msc.gs.npchandler.NpcHandlerDef; +import org.moparscape.msc.gs.phandler.PacketHandlerDef; + +/** + * Any retrieval of unchanging data should be done through this interface. + * + * @author Joe Pritzel + * + */ +public interface DataStore { + public PacketHandlerDef[] loadPacketHandlerDefs(); + + public PacketHandlerDef[] loadLSPacketHandlerDefs(); + + public NpcHandlerDef[] loadNpcHandlers(); + + public Map loadTelePoints(); + + public List loadShops(); + + public Map loadCerterDefs(); + + public List loadGameObjectLocs(); + + public List loadItemLocs(); + + public List loadNPCLocs(); + + public TileDef[] loadTileDefs(); + + public GameObjectDef[] loadGameObjectDefs(); + + public DoorDef[] loadDoorDefs(); + + public ItemDef[] loadItemDefs(); + + public PrayerDef[] loadPrayerDefs(); + + public SpellDef[] loadSpellDefs(); + + public NPCDef[] loadNPCDefs(); + + public ItemCraftingDef[] loadItemCraftingDefs(); + + public ItemHerbSecond[] loadItemHerbSeconds(); + + public Map loadItemDartTipDefs(); + + public Map loadGemDefs(); + + public Map loadItemLogCutDefs(); + + public Map loadItemBowStringDefs(); + + public Map loadItemArrowHeadDefs(); + + public Map loadFiremakingDefs(); + + public Map loadItemAffectedTypes(); + + public Map loadItemWieldableDefs(); + + public Map loadItemUnIdentHerbDefs(); + + public Map loadItemHerbDefs(); + + public Map loadItemEdibleHeals(); + + public Map loadItemCookingDefs(); + + public Map loadItemSmeltingDefs(); + + public ItemSmithingDef[] loadItemSmithingDefs(); + + public Map loadObjectMiningDefs(); + + public Map loadObjectWoodcuttingDefs(); + + public Map loadObjectFishDefs(); + + public Map loadSpellAgressiveLevel(); + + public Map loadAgilityDefs(); + + public Map loadAgilityCourseDefs(); + + public List[] loadKeyChestLoots(); + + public HashMap loadDartTips(); + + public void dispose(); + +} diff --git a/GameServer/src/org/moparscape/msc/gs/persistence/DataStoreFactory.java b/GameServer/src/org/moparscape/msc/gs/persistence/DataStoreFactory.java new file mode 100644 index 0000000..b444b1b --- /dev/null +++ b/GameServer/src/org/moparscape/msc/gs/persistence/DataStoreFactory.java @@ -0,0 +1,10 @@ +package org.moparscape.msc.gs.persistence; + +public class DataStoreFactory { + + public static DataStore create(String className) throws Exception { + return Class.forName(className).asSubclass(DataStore.class) + .newInstance(); + } + +} diff --git a/GameServer/src/org/moparscape/msc/gs/persistence/impl/XMLUsingXStream.java b/GameServer/src/org/moparscape/msc/gs/persistence/impl/XMLUsingXStream.java new file mode 100644 index 0000000..84220c0 --- /dev/null +++ b/GameServer/src/org/moparscape/msc/gs/persistence/impl/XMLUsingXStream.java @@ -0,0 +1,309 @@ +package org.moparscape.msc.gs.persistence.impl; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.util.Enumeration; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Properties; +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import java.util.zip.GZIPInputStream; +import java.util.zip.GZIPOutputStream; + +import net.jcip.annotations.NotThreadSafe; + +import org.moparscape.msc.config.Config; +import org.moparscape.msc.gs.external.*; +import org.moparscape.msc.gs.model.InvItem; +import org.moparscape.msc.gs.model.Point; +import org.moparscape.msc.gs.model.Shop; +import org.moparscape.msc.gs.model.TelePoint; +import org.moparscape.msc.gs.npchandler.NpcHandlerDef; +import org.moparscape.msc.gs.persistence.DataStore; +import org.moparscape.msc.gs.phandler.PacketHandlerDef; +import org.moparscape.msc.gs.util.Logger; + +import com.thoughtworks.xstream.XStream; + +/** + * + * A DataStore that parses XML using XStream. + * + * @author Joe Pritzel + * + */ +@NotThreadSafe +@SuppressWarnings("unchecked") +public class XMLUsingXStream implements DataStore { + + private static final XStream xstream = new XStream(); + + static { + try { + setupAliases(); + } catch (Exception e) { + e.printStackTrace(); + } + } + + public static Object load(String filename) { + filename = filename.replaceAll(Pattern.quote("/"), + Matcher.quoteReplacement(File.separator)); + try { + InputStream is = new FileInputStream(new File(Config.CONF_DIR, + filename)); + if (filename.endsWith(".gz")) { + is = new GZIPInputStream(is); + } + Object rv = xstream.fromXML(is); + return rv; + } catch (IOException ioe) { + Logger.error("Filename = " + filename); + Logger.error(ioe); + } + return null; + } + + public static void setupAliases() { + try { + Properties aliases = new Properties(); + FileInputStream fis = new FileInputStream(new File(Config.CONF_DIR, + "aliases.xml")); + aliases.loadFromXML(fis); + for (Enumeration e = aliases.propertyNames(); e + .hasMoreElements();) { + String alias = (String) e.nextElement(); + Class c = Class.forName((String) aliases.get(alias)); + xstream.alias(alias, c); + } + } catch (Exception ioe) { + Logger.error(ioe); + } + } + + public static void write(String filename, Object o) { + filename = filename.replaceAll(Pattern.quote("/"), + Matcher.quoteReplacement(File.separator)); + try { + OutputStream os = new FileOutputStream(new File(Config.CONF_DIR, + filename)); + if (filename.endsWith(".gz")) { + os = new GZIPOutputStream(os); + } + xstream.toXML(o, os); + } catch (IOException ioe) { + Logger.error(ioe); + } + } + + @Override + public PacketHandlerDef[] loadPacketHandlerDefs() { + return (PacketHandlerDef[]) load("PacketHandlers.xml"); + } + + @Override + public PacketHandlerDef[] loadLSPacketHandlerDefs() { + return (PacketHandlerDef[]) load("LSPacketHandlers.xml"); + } + + @Override + public NpcHandlerDef[] loadNpcHandlers() { + return (NpcHandlerDef[]) load("NpcHandlers.xml"); + } + + @Override + public Map loadTelePoints() { + return (Map) load("locs/extras/ObjectTelePoints.xml.gz"); + } + + @Override + public List loadShops() { + return (List) load("locs/Shops.xml.gz"); + } + + @Override + public Map loadCerterDefs() { + return (Map) load("defs/extras/NpcCerters.xml.gz"); + } + + @Override + public List loadGameObjectLocs() { + return (List) load("locs/GameObjectLoc.xml.gz"); + } + + @Override + public List loadItemLocs() { + return (List) load("locs/ItemLoc.xml.gz"); + } + + @Override + public List loadNPCLocs() { + return (List) load("locs/NpcLoc.xml.gz"); + } + + @Override + public TileDef[] loadTileDefs() { + return (TileDef[]) load("defs/TileDef.xml.gz"); + } + + @Override + public GameObjectDef[] loadGameObjectDefs() { + return (GameObjectDef[]) load("defs/GameObjectDef.xml.gz"); + } + + @Override + public DoorDef[] loadDoorDefs() { + return (DoorDef[]) load("defs/DoorDef.xml.gz"); + } + + @Override + public ItemDef[] loadItemDefs() { + return (ItemDef[]) load("defs/ItemDef.xml.gz"); + } + + @Override + public PrayerDef[] loadPrayerDefs() { + return (PrayerDef[]) load("defs/PrayerDef.xml.gz"); + } + + @Override + public SpellDef[] loadSpellDefs() { + return (SpellDef[]) load("defs/SpellDef.xml.gz"); + } + + @Override + public NPCDef[] loadNPCDefs() { + return (NPCDef[]) load("defs/NPCDef.xml.gz"); + } + + @Override + public ItemCraftingDef[] loadItemCraftingDefs() { + return (ItemCraftingDef[]) load("defs/extras/ItemCraftingDef.xml.gz"); + } + + @Override + public ItemHerbSecond[] loadItemHerbSeconds() { + return (ItemHerbSecond[]) load("defs/extras/ItemHerbSecond.xml.gz"); + } + + @Override + public Map loadItemDartTipDefs() { + return (Map) load("defs/extras/ItemDartTipDef.xml.gz"); + } + + @Override + public Map loadGemDefs() { + return (Map) load("defs/extras/ItemGemDef.xml.gz"); + } + + @Override + public Map loadItemLogCutDefs() { + return (Map) load("defs/extras/ItemLogCutDef.xml.gz"); + } + + @Override + public Map loadItemBowStringDefs() { + return (Map) load("defs/extras/ItemBowStringDef.xml.gz"); + } + + @Override + public Map loadItemArrowHeadDefs() { + return (Map) load("defs/extras/ItemArrowHeadDef.xml.gz"); + } + + @Override + public Map loadFiremakingDefs() { + return (Map) load("defs/extras/FiremakingDef.xml.gz"); + } + + @Override + public Map loadItemAffectedTypes() { + return (Map) load("defs/extras/ItemAffectedTypes.xml.gz"); + } + + @Override + public Map loadItemWieldableDefs() { + return (Map) load("defs/extras/ItemWieldableDef.xml.gz"); + } + + @Override + public Map loadItemUnIdentHerbDefs() { + return (Map) load("defs/extras/ItemUnIdentHerbDef.xml.gz"); + } + + @Override + public Map loadItemHerbDefs() { + return (Map) load("defs/extras/ItemHerbDef.xml.gz"); + } + + @Override + public Map loadItemEdibleHeals() { + return (Map) load("defs/extras/ItemEdibleHeals.xml.gz"); + } + + @Override + public Map loadItemCookingDefs() { + return (Map) load("defs/extras/ItemCookingDef.xml.gz"); + } + + @Override + public Map loadItemSmeltingDefs() { + return (Map) load("defs/extras/ItemSmeltingDef.xml.gz"); + } + + @Override + public ItemSmithingDef[] loadItemSmithingDefs() { + return (ItemSmithingDef[]) load("defs/extras/ItemSmithingDef.xml.gz"); + } + + @Override + public Map loadObjectMiningDefs() { + return (Map) load("defs/extras/ObjectMining.xml.gz"); + } + + @Override + public Map loadObjectWoodcuttingDefs() { + return (Map) load("defs/extras/ObjectWoodcutting.xml.gz"); + } + + @Override + public Map loadObjectFishDefs() { + return (Map) load("defs/extras/ObjectFishing.xml.gz"); + } + + @Override + public Map loadSpellAgressiveLevel() { + return (Map) load("defs/extras/SpellAggressiveLvl.xml.gz"); + } + + @Override + public Map loadAgilityDefs() { + return (Map) load("defs/extras/AgilityDef.xml.gz"); + } + + @Override + public Map loadAgilityCourseDefs() { + return (Map) load("defs/extras/AgilityCourseDef.xml.gz"); + } + + @Override + public List[] loadKeyChestLoots() { + return (List[]) load("defs/extras/KeyChestLoot.xml.gz"); + } + + @Override + public HashMap loadDartTips() { + return (HashMap) load("defs/extras/ItemDartTipDef.xml.gz"); + } + + @Override + public void dispose() { + + } +} diff --git a/GameServer/src/org/moparscape/msc/gs/util/PersistenceManager.java b/GameServer/src/org/moparscape/msc/gs/util/PersistenceManager.java deleted file mode 100644 index b1048fe..0000000 --- a/GameServer/src/org/moparscape/msc/gs/util/PersistenceManager.java +++ /dev/null @@ -1,69 +0,0 @@ -package org.moparscape.msc.gs.util; - -import java.io.File; -import java.io.FileInputStream; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; -import java.util.Enumeration; -import java.util.Properties; -import java.util.zip.GZIPInputStream; -import java.util.zip.GZIPOutputStream; - -import org.moparscape.msc.config.Config; - -import com.thoughtworks.xstream.XStream; - -public class PersistenceManager { - private static final XStream xstream = new XStream(); - - static { - setupAliases(); - } - - public static Object load(String filename) { - try { - InputStream is = new FileInputStream(new File(Config.CONF_DIR, - filename)); - if (filename.endsWith(".gz")) { - is = new GZIPInputStream(is); - } - Object rv = xstream.fromXML(is); - return rv; - } catch (IOException ioe) { - Logger.error(ioe); - } - return null; - } - - public static void setupAliases() { - try { - Properties aliases = new Properties(); - FileInputStream fis = new FileInputStream(new File(Config.CONF_DIR, - "aliases.xml")); - aliases.loadFromXML(fis); - for (Enumeration e = aliases.propertyNames(); e - .hasMoreElements();) { - String alias = (String) e.nextElement(); - Class c = Class.forName((String) aliases.get(alias)); - xstream.alias(alias, c); - } - } catch (Exception ioe) { - Logger.error(ioe); - } - } - - public static void write(String filename, Object o) { - try { - OutputStream os = new FileOutputStream(new File(Config.CONF_DIR, - filename)); - if (filename.endsWith(".gz")) { - os = new GZIPOutputStream(os); - } - xstream.toXML(o, os); - } catch (IOException ioe) { - Logger.error(ioe); - } - } -} diff --git a/LoginServer/src/org/moparscape/msc/ls/Server.java b/LoginServer/src/org/moparscape/msc/ls/Server.java index 5910e55..d2724dc 100644 --- a/LoginServer/src/org/moparscape/msc/ls/Server.java +++ b/LoginServer/src/org/moparscape/msc/ls/Server.java @@ -58,7 +58,7 @@ public class Server { System.out.println("Login Server starting up..."); Config.initConfig(configFile); try { - storage = StorageMediumFactory.createMedium(Config.STORAGE_MEDIUM); + storage = StorageMediumFactory.create(Config.STORAGE_MEDIUM); } catch (Exception e) { e.printStackTrace(); return; diff --git a/LoginServer/src/org/moparscape/msc/ls/persistence/StorageMediumFactory.java b/LoginServer/src/org/moparscape/msc/ls/persistence/StorageMediumFactory.java index 51607ba..0e93407 100644 --- a/LoginServer/src/org/moparscape/msc/ls/persistence/StorageMediumFactory.java +++ b/LoginServer/src/org/moparscape/msc/ls/persistence/StorageMediumFactory.java @@ -2,7 +2,7 @@ package org.moparscape.msc.ls.persistence; public class StorageMediumFactory { - public static StorageMedium createMedium(String className) throws Exception { + public static StorageMedium create(String className) throws Exception { return Class.forName(className).asSubclass(StorageMedium.class) .newInstance(); }