קוד PHP:
/* This file is part of the OdinMS Maple Story Server Copyright (C) 2008 Patrick Huy <patrick.huy@frz.cc> Matthias Butz <matze@odinms.de> Jan Christian Meyer <vimes@odinms.de>
This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation version 3 as published by the Free Software Foundation. You may not use, modify or distribute this program under any other version of the GNU Affero General Public License.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License along with this program. If not, see <http://www.gnu.org/licenses/>.*/package net.channel;
import java.io.File;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.IOException;import java.net.InetSocketAddress;import java.rmi.NotBoundException;import java.rmi.RemoteException;import java.rmi.registry.LocateRegistry;import java.rmi.registry.Registry;import java.sql.Connection;import java.sql.PreparedStatement;import java.util.Collection;import java.util.Collections;import java.util.HashMap;import java.util.List;import java.util.Map;import java.util.Properties;import javax.management.InstanceAlreadyExistsException;import javax.management.MalformedObjectNameException;import javax.rmi.ssl.SslRMIClientSocketFactory;import client.MapleCharacter;import client.SkillFactory;import constants.ServerConstants;import java.util.ArrayList;import java.util.LinkedList;import java.util.Map.Entry;import net.world.MapleParty;import tools.DatabaseConnection;import net.MaplePacket;import net.MapleServerHandler;import net.PacketProcessor;import net.ServerMode;import net.ServerMode.Mode;import net.mina.MapleCodecFactory;import server.maps.FakeCharacter;import net.world.MaplePartyCharacter;import net.world.guild.MapleGuild;import net.world.guild.MapleGuildCharacter;import net.world.guild.MapleGuildSummary;import net.world.remote.WorldChannelInterface;import net.world.remote.WorldRegistry;import provider.MapleDataProviderFactory;import scripting.event.EventScriptManager;import server.ShutdownServer;import server.TimerManager;import server.maps.MapleMapFactory;import tools.MaplePacketCreator;import org.apache.mina.core.buffer.IoBuffer;import org.apache.mina.core.buffer.SimpleBufferAllocator;import org.apache.mina.core.filterchain.IoFilter;import org.apache.mina.core.service.IoAcceptor;import org.apache.mina.core.session.IdleStatus;import org.apache.mina.filter.codec.ProtocolCodecFilter;import org.apache.mina.transport.socket.SocketSessionConfig;import org.apache.mina.transport.socket.nio.NioSocketAcceptor;import server.MapleSquad;import server.MapleSquadType;import server.events.MapleEvent;import server.maps.HiredMerchant;import server.maps.MapleMap;
public class ChannelServer implements Runnable { private int port = 7575; private static WorldRegistry worldRegistry; private PlayerStorage players = new PlayerStorage(); private int channel; private String key; private ChannelWorldInterface cwi; private WorldChannelInterface wci = null; private boolean moreThanOne; private IoAcceptor acceptor; private int dropRate; private String ip; private boolean shutdown = false; private String arrayString = ""; private boolean finishedShutdown = false; private MapleMapFactory mapFactory; private String serverName; private EventScriptManager eventSM; private boolean dropUndroppables; private int bossdropRate; public boolean eventOn = false; public int eventMap = 0; private Collection<FakeCharacter> clones = new LinkedList<FakeCharacter>(); private static Map<Integer, ChannelServer> instances = new HashMap<Integer, ChannelServer>(); private static Map<String, ChannelServer> pendingInstances = new HashMap<String, ChannelServer>(); private Map<Integer, MapleGuildSummary> gsStore = new HashMap<Integer, MapleGuildSummary>(); private Map<Integer, HiredMerchant> hiredMerchants = new HashMap<Integer, HiredMerchant>(); private Boolean worldReady = true; private int instanceId = 0; private MapleEvent event; private boolean cooldowns; private Map<MapleSquadType, MapleSquad> mapleSquads = new HashMap<MapleSquadType, MapleSquad>();
private ChannelServer(String key) { mapFactory = new MapleMapFactory(MapleDataProviderFactory.getDataProvider(new File(System.getProperty("wzpath") + "/Map.wz")), MapleDataProviderFactory.getDataProvider(new File(System.getProperty("wzpath") + "/String.wz"))); this.key = key; }
public static WorldRegistry getWorldRegistry() { return worldRegistry; }
public int getInstanceId() { return instanceId; }
public void addInstanceId() { instanceId++; } public void addClone(FakeCharacter fc) { clones.add(fc); } public Collection<FakeCharacter> getAllClones() { return clones; }
public void reconnectWorld() { try { wci.isAvailable(); } catch (RemoteException ex) { synchronized (worldReady) { worldReady = false; } synchronized (cwi) { synchronized (worldReady) { if (worldReady) { return; } } System.out.println("Reconnecting to world server"); synchronized (wci) { try { Registry registry = LocateRegistry.getRegistry(ServerConstants.HOST, Registry.REGISTRY_PORT, new SslRMIClientSocketFactory()); worldRegistry = (WorldRegistry) registry.lookup("WorldRegistry"); cwi = new ChannelWorldInterfaceImpl(this); wci = worldRegistry.registerChannelServer(key, cwi); Properties dbProp = new Properties(); FileReader fr = new FileReader("db.properties"); dbProp.load(fr); fr.close(); DatabaseConnection.initialize(dbProp); DatabaseConnection.getConnection(); wci.serverReady(); } catch (Exception e) { } worldReady = true; } } synchronized (worldReady) { worldReady.notifyAll(); } } }
@Override public void run() { try { cwi = new ChannelWorldInterfaceImpl(this); wci = worldRegistry.registerChannelServer(key, cwi); eventSM = new EventScriptManager(this, ServerConstants.EVENTS.split(" ")); Properties dbProp = new Properties(); FileReader fileReader = new FileReader("db.properties"); dbProp.load(fileReader); fileReader.close(); DatabaseConnection.initialize(dbProp); Connection c = DatabaseConnection.getConnection(); PreparedStatement ps = c.prepareStatement("UPDATE accounts SET loggedin = 0"); ps.executeUpdate(); ps.close(); ps = c.prepareStatement("UPDATE characters SET HasMerchant = 0"); ps.executeUpdate(); ps.close(); port = 7575 + this.channel - 1; ip = ServerConstants.HOST + ":" + port; IoBuffer.setUseDirectBuffer(false); IoBuffer.setAllocator(new SimpleBufferAllocator()); acceptor = new NioSocketAcceptor(); TimerManager tMan = TimerManager.getInstance(); tMan.start(); tMan.register(new respawnMaps(), 10000); acceptor.setHandler(new MapleServerHandler(PacketProcessor.getProcessor(PacketProcessor.Mode.CHANNELSERVER), channel)); acceptor.getSessionConfig().setIdleTime(IdleStatus.BOTH_IDLE, 30); acceptor.getFilterChain().addLast("codec", (IoFilter) new ProtocolCodecFilter(new MapleCodecFactory())); acceptor.bind(new InetSocketAddress(port)); ((SocketSessionConfig) acceptor.getSessionConfig()).setTcpNoDelay(true); SkillFactory.getSkill(9999999); System.out.println("Channel " + getChannel() + ": Listening on port " + port); wci.serverReady(); eventSM.init(); } catch (Exception e) { e.printStackTrace(); } } public void setBossDropRate(int bossdropRate) { this.bossdropRate = bossdropRate; }
public void setDropRate(int dropRate) { this.dropRate = dropRate; } public String getArrayString() { //If you are wondering, this is for the !array command return arrayString; } public void shutdown() { shutdown = true; boolean error = true; while (error) { try { for (MapleCharacter chr : players.getAllCharacters()) { synchronized (chr) { if (chr.getHiredMerchant().isOpen()) { chr.getHiredMerchant().saveItems(); } chr.getClient().disconnect(); } error = false; } } catch (Exception e) { error = true; } } finishedShutdown = true; wci = null; cwi = null; } public boolean allowUndroppablesDrop() { return dropUndroppables; }
public void unbind() { acceptor.unbind(); }
public boolean hasFinishedShutdown() { return finishedShutdown; }
public MapleMapFactory getMapFactory() { return mapFactory; } public String getServerName() { return serverName; } public boolean getCooldowns() { return cooldowns; }
private static ChannelServer newInstance(String key) throws InstanceAlreadyExistsException, MalformedObjectNameException { ChannelServer instance = new ChannelServer(key); pendingInstances.put(key, instance); return instance; }
public static ChannelServer getInstance(int channel) { return instances.get(channel); }
public void addPlayer(MapleCharacter chr) { players.registerPlayer(chr); chr.broadcast(MaplePacketCreator.serverMessage(ServerConstants.SERVER_MESSAGE)); }
public IPlayerStorage getPlayerStorage() { return players; }
public boolean allowMoreThanOne() { return moreThanOne; } public void removePlayer(MapleCharacter chr) { players.deregisterPlayer(chr); }
public int getConnectedClients() { return players.getAllCharacters().size(); }
public void setServerMessage(String newMessage) { ServerConstants.SERVER_MESSAGE = newMessage; broadcastPacket(MaplePacketCreator.serverMessage(ServerConstants.SERVER_MESSAGE)); }
public void broadcastPacket(MaplePacket data) { for (MapleCharacter chr : players.getAllCharacters()) { chr.broadcast(data); } }
public int getChannel() { return channel; }
public void setChannel(int channel) { if (pendingInstances.containsKey(key)) { pendingInstances.remove(key); } if (instances.containsKey(channel)) { instances.remove(channel); } instances.put(channel, this); this.channel = channel; this.mapFactory.setChannel(channel); }
public static Collection<ChannelServer> getAllInstances() { return Collections.unmodifiableCollection(instances.values()); }
public String getIP() { return ip; }
public String getIP(int channel) { try { return getWorldInterface().getIP(channel); } catch (RemoteException e) { System.out.println("Lost connection to world server " + e); throw new RuntimeException("Lost connection to world server"); } }
public WorldChannelInterface getWorldInterface() { synchronized (worldReady) { while (!worldReady) { try { worldReady.wait(); } catch (InterruptedException e) { } } } return wci; }
public boolean isShutdown() { return shutdown; }
public void shutdown(int time) { TimerManager.getInstance().schedule(new ShutdownServer(getChannel()), time); }
public MapleEvent getEvent() { return event; } public void broadcastSMega(MaplePacket data) { for (MapleCharacter chr : players.getAllCharacters()) { if (chr.getSmegaEnabled()) { chr.getClient().getSession().write(data); } } }
public void setEvent(MapleEvent event) { this.event = event; }
public EventScriptManager getEventSM() { return eventSM; }
public MapleGuild getGuild(MapleGuildCharacter mgc) { int gid = mgc.getGuildId(); MapleGuild g = null; try { g = this.getWorldInterface().getGuild(gid, mgc); } catch (RemoteException re) { System.out.println("RemoteException while fetching MapleGuild. " + re); return null; } if (gsStore.get(gid) == null) { gsStore.put(gid, new MapleGuildSummary(g)); } return g; }
public MapleGuildSummary getGuildSummary(int gid) { if (gsStore.containsKey(gid)) { return gsStore.get(gid); } else { try { MapleGuild g = this.getWorldInterface().getGuild(gid, null); if (g != null) { gsStore.put(gid, new MapleGuildSummary(g)); } return gsStore.get(gid); } catch (RemoteException re) { System.out.println("RemoteException while fetching GuildSummary. " + re); return null; } } }
public void updateGuildSummary(int gid, MapleGuildSummary mgs) { gsStore.put(gid, mgs); }
public void reloadGuildSummary() { try { MapleGuild g; for (int i : gsStore.keySet()) { g = this.getWorldInterface().getGuild(i, null); if (g != null) { gsStore.put(i, new MapleGuildSummary(g)); } else { gsStore.remove(i); } } } catch (RemoteException re) { System.out.println("RemoteException while reloading GuildSummary." + re); } }
public static void main(String args[]) throws FileNotFoundException, IOException, NotBoundException, InstanceAlreadyExistsException, MalformedObjectNameException { ServerMode.setServerMode(Mode.CHANNEL); Registry registry = LocateRegistry.getRegistry("localhost", Registry.REGISTRY_PORT, new SslRMIClientSocketFactory()); worldRegistry = (WorldRegistry) registry.lookup("WorldRegistry"); for (int i = 0; i < ServerConstants.CHANNEL_NUMBER; i++) { newInstance("release" + (i + 1)).run(); } DatabaseConnection.getConnection(); Runtime.getRuntime().addShutdownHook(new Thread() { @Override public void run() { for (ChannelServer channel : getAllInstances()) { for (MapleCharacter mc : channel.getPlayerStorage().getAllCharacters()) { mc.getClient().disconnect(); } } } }); }
public void broadcastGMPacket(MaplePacket data) { for (MapleCharacter chr : players.getAllCharacters()) { if (chr.isGM()) { chr.broadcast(data); } } }
public void yellowWorldMessage(String msg) { for (MapleCharacter mc : getPlayerStorage().getAllCharacters()) { mc.broadcast(MaplePacketCreator.sendYellowTip(msg)); } }
public void worldMessage(String msg) { for (MapleCharacter mc : getPlayerStorage().getAllCharacters()) { mc.dropMessage(msg); } }
public List<MapleCharacter> getPartyMembers(MapleParty party) { List<MapleCharacter> partym = new ArrayList<MapleCharacter>(8); for (MaplePartyCharacter partychar : party.getMembers()) { if (partychar.getChannel() == getChannel()) { MapleCharacter chr = getPlayerStorage().getCharacterByName(partychar.getName()); if (chr != null) { partym.add(chr); } } } return partym; }
public class respawnMaps implements Runnable { @Override public void run() { for (Entry<Integer, MapleMap> map : mapFactory.getMaps().entrySet()) { map.getValue().respawn(); } } }
public final MapleCharacter getCharacterFromAllServers(int id) { MapleCharacter ret = players.getCharacterById(id); if (ret != null) { return ret; } return null; }
public MapleSquad getMapleSquad(MapleSquadType type) { return mapleSquads.get(type); }
public boolean addMapleSquad(MapleSquad squad, MapleSquadType type) { if (mapleSquads.get(type) == null) { mapleSquads.remove(type); mapleSquads.put(type, squad); return true; } else { return false; } }
public Map<Integer, HiredMerchant> getHiredMerchants() { return hiredMerchants; }
public void addHiredMerchant(int chrid, HiredMerchant hm) { hiredMerchants.put(chrid, hm); }
public void removeHiredMerchant(int chrid) { hiredMerchants.remove(chrid); }}