2
0

GameServer.java 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  1. /*
  2. * This program is free software: you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation, either version 3 of the License, or (at your option) any later
  5. * version.
  6. *
  7. * This program is distributed in the hope that it will be useful, but WITHOUT
  8. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  9. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  10. * details.
  11. *
  12. * You should have received a copy of the GNU General Public License along with
  13. * this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. package net.sf.l2j.gameserver;
  16. import java.io.File;
  17. import java.io.FileInputStream;
  18. import java.io.IOException;
  19. import java.io.InputStream;
  20. import java.net.InetAddress;
  21. import java.net.UnknownHostException;
  22. import java.util.Calendar;
  23. import java.util.logging.Level;
  24. import java.util.logging.LogManager;
  25. import java.util.logging.Logger;
  26. import net.sf.l2j.Config;
  27. import net.sf.l2j.L2DatabaseFactory;
  28. import net.sf.l2j.Server;
  29. import net.sf.l2j.gameserver.cache.CrestCache;
  30. import net.sf.l2j.gameserver.cache.HtmCache;
  31. import net.sf.l2j.gameserver.communitybbs.Manager.ForumsBBSManager;
  32. import net.sf.l2j.gameserver.datatables.AccessLevels;
  33. import net.sf.l2j.gameserver.datatables.AdminCommandAccessRights;
  34. import net.sf.l2j.gameserver.datatables.ArmorSetsTable;
  35. import net.sf.l2j.gameserver.datatables.AugmentationData;
  36. import net.sf.l2j.gameserver.datatables.CharNameTable;
  37. import net.sf.l2j.gameserver.datatables.CharTemplateTable;
  38. import net.sf.l2j.gameserver.datatables.ClanTable;
  39. import net.sf.l2j.gameserver.datatables.DoorTable;
  40. import net.sf.l2j.gameserver.datatables.EventDroplist;
  41. import net.sf.l2j.gameserver.datatables.ExtractableItemsData;
  42. import net.sf.l2j.gameserver.datatables.FishTable;
  43. import net.sf.l2j.gameserver.datatables.HelperBuffTable;
  44. import net.sf.l2j.gameserver.datatables.HennaTable;
  45. import net.sf.l2j.gameserver.datatables.HennaTreeTable;
  46. import net.sf.l2j.gameserver.datatables.HeroSkillTable;
  47. import net.sf.l2j.gameserver.datatables.ItemTable;
  48. import net.sf.l2j.gameserver.datatables.LevelUpData;
  49. import net.sf.l2j.gameserver.datatables.MapRegionTable;
  50. import net.sf.l2j.gameserver.datatables.MerchantPriceConfigTable;
  51. import net.sf.l2j.gameserver.datatables.NobleSkillTable;
  52. import net.sf.l2j.gameserver.datatables.NpcBufferTable;
  53. import net.sf.l2j.gameserver.datatables.NpcTable;
  54. import net.sf.l2j.gameserver.datatables.NpcWalkerRoutesTable;
  55. import net.sf.l2j.gameserver.datatables.PetSkillsTable;
  56. import net.sf.l2j.gameserver.datatables.ResidentialSkillTable;
  57. import net.sf.l2j.gameserver.datatables.SkillSpellbookTable;
  58. import net.sf.l2j.gameserver.datatables.SkillTable;
  59. import net.sf.l2j.gameserver.datatables.SkillTreeTable;
  60. import net.sf.l2j.gameserver.datatables.SpawnTable;
  61. import net.sf.l2j.gameserver.datatables.StaticObjects;
  62. import net.sf.l2j.gameserver.datatables.SummonItemsData;
  63. import net.sf.l2j.gameserver.datatables.TeleportLocationTable;
  64. import net.sf.l2j.gameserver.geoeditorcon.GeoEditorListener;
  65. import net.sf.l2j.gameserver.handler.AdminCommandHandler;
  66. import net.sf.l2j.gameserver.handler.ChatHandler;
  67. import net.sf.l2j.gameserver.handler.ItemHandler;
  68. import net.sf.l2j.gameserver.handler.SkillHandler;
  69. import net.sf.l2j.gameserver.handler.UserCommandHandler;
  70. import net.sf.l2j.gameserver.handler.VoicedCommandHandler;
  71. import net.sf.l2j.gameserver.idfactory.IdFactory;
  72. import net.sf.l2j.gameserver.instancemanager.AirShipManager;
  73. import net.sf.l2j.gameserver.instancemanager.AuctionManager;
  74. import net.sf.l2j.gameserver.instancemanager.BoatManager;
  75. import net.sf.l2j.gameserver.instancemanager.CastleManager;
  76. import net.sf.l2j.gameserver.instancemanager.CastleManorManager;
  77. import net.sf.l2j.gameserver.instancemanager.ClanHallManager;
  78. import net.sf.l2j.gameserver.instancemanager.CoupleManager;
  79. import net.sf.l2j.gameserver.instancemanager.CursedWeaponsManager;
  80. import net.sf.l2j.gameserver.instancemanager.DayNightSpawnManager;
  81. import net.sf.l2j.gameserver.instancemanager.DimensionalRiftManager;
  82. import net.sf.l2j.gameserver.instancemanager.FortManager;
  83. import net.sf.l2j.gameserver.instancemanager.FortSiegeManager;
  84. import net.sf.l2j.gameserver.instancemanager.FourSepulchersManager;
  85. import net.sf.l2j.gameserver.instancemanager.GrandBossManager;
  86. import net.sf.l2j.gameserver.instancemanager.InstanceManager;
  87. import net.sf.l2j.gameserver.instancemanager.ItemsOnGroundManager;
  88. import net.sf.l2j.gameserver.instancemanager.MercTicketManager;
  89. import net.sf.l2j.gameserver.instancemanager.PetitionManager;
  90. import net.sf.l2j.gameserver.instancemanager.QuestManager;
  91. import net.sf.l2j.gameserver.instancemanager.RaidBossPointsManager;
  92. import net.sf.l2j.gameserver.instancemanager.RaidBossSpawnManager;
  93. import net.sf.l2j.gameserver.instancemanager.SiegeManager;
  94. import net.sf.l2j.gameserver.instancemanager.TransformationManager;
  95. import net.sf.l2j.gameserver.instancemanager.ZoneManager;
  96. import net.sf.l2j.gameserver.model.AutoChatHandler;
  97. import net.sf.l2j.gameserver.model.AutoSpawnHandler;
  98. import net.sf.l2j.gameserver.model.L2Manor;
  99. import net.sf.l2j.gameserver.model.L2PetDataTable;
  100. import net.sf.l2j.gameserver.model.L2World;
  101. import net.sf.l2j.gameserver.model.entity.Hero;
  102. import net.sf.l2j.gameserver.model.entity.TvTManager;
  103. import net.sf.l2j.gameserver.model.olympiad.Olympiad;
  104. import net.sf.l2j.gameserver.network.L2GameClient;
  105. import net.sf.l2j.gameserver.network.L2GamePacketHandler;
  106. import net.sf.l2j.gameserver.pathfinding.PathFinding;
  107. import net.sf.l2j.gameserver.script.faenor.FaenorScriptEngine;
  108. import net.sf.l2j.gameserver.scripting.CompiledScriptCache;
  109. import net.sf.l2j.gameserver.scripting.L2ScriptEngineManager;
  110. import net.sf.l2j.gameserver.taskmanager.AutoAnnounceTaskManager;
  111. import net.sf.l2j.gameserver.taskmanager.KnownListUpdateTaskManager;
  112. import net.sf.l2j.gameserver.taskmanager.TaskManager;
  113. import net.sf.l2j.gameserver.util.DynamicExtension;
  114. import net.sf.l2j.status.Status;
  115. import net.sf.l2j.util.DeadLockDetector;
  116. import org.mmocore.network.SelectorConfig;
  117. import org.mmocore.network.SelectorThread;
  118. /**
  119. * This class ...
  120. *
  121. * @version $Revision: 1.29.2.15.2.19 $ $Date: 2005/04/05 19:41:23 $
  122. */
  123. public class GameServer
  124. {
  125. private static final Logger _log = Logger.getLogger(GameServer.class.getName());
  126. private final SelectorThread<L2GameClient> _selectorThread;
  127. private final DeadLockDetector _deadDetectThread;
  128. private final SkillTable _skillTable;
  129. private final ItemTable _itemTable;
  130. private final NpcTable _npcTable;
  131. private final HennaTable _hennaTable;
  132. private final IdFactory _idFactory;
  133. public static GameServer gameServer;
  134. private static ClanHallManager _cHManager;
  135. private final Shutdown _shutdownHandler;
  136. private final DoorTable _doorTable;
  137. private final SevenSigns _sevenSignsEngine;
  138. private final AutoChatHandler _autoChatHandler;
  139. private final AutoSpawnHandler _autoSpawnHandler;
  140. private LoginServerThread _loginThread;
  141. private final HelperBuffTable _helperBuffTable;
  142. private static Status _statusServer;
  143. @SuppressWarnings("unused")
  144. private final ThreadPoolManager _threadpools;
  145. public static final Calendar dateTimeServerStarted = Calendar.getInstance();
  146. public long getUsedMemoryMB()
  147. {
  148. return (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()) / 1048576; // ;
  149. }
  150. public SelectorThread<L2GameClient> getSelectorThread()
  151. {
  152. return _selectorThread;
  153. }
  154. public DeadLockDetector getDeadLockDetectorThread()
  155. {
  156. return _deadDetectThread;
  157. }
  158. public ClanHallManager getCHManager()
  159. {
  160. return _cHManager;
  161. }
  162. public GameServer() throws Exception
  163. {
  164. long serverLoadStart = System.currentTimeMillis();
  165. gameServer = this;
  166. _log.finest("used mem:" + getUsedMemoryMB() + "MB");
  167. if (Config.SERVER_VERSION != null)
  168. {
  169. _log.info("L2J Server Version: " + Config.SERVER_VERSION);
  170. }
  171. if (Config.DATAPACK_VERSION != null)
  172. {
  173. _log.info("L2J Datapack Version: " + Config.DATAPACK_VERSION);
  174. }
  175. _idFactory = IdFactory.getInstance();
  176. if (!_idFactory.isInitialized())
  177. {
  178. _log.severe("Could not read object IDs from DB. Please Check Your Data.");
  179. throw new Exception("Could not initialize the ID factory");
  180. }
  181. _threadpools = ThreadPoolManager.getInstance();
  182. new File(Config.DATAPACK_ROOT, "data/clans").mkdirs();
  183. new File(Config.DATAPACK_ROOT, "data/crests").mkdirs();
  184. // load script engines
  185. L2ScriptEngineManager.getInstance();
  186. // start game time control early
  187. GameTimeController.getInstance();
  188. // keep the references of Singletons to prevent garbage collection
  189. CharNameTable.getInstance();
  190. _skillTable = SkillTable.getInstance();
  191. if (!_skillTable.isInitialized())
  192. {
  193. _log.severe("Could not find the extraced files. Please Check Your Data.");
  194. throw new Exception("Could not initialize the skill table");
  195. }
  196. _itemTable = ItemTable.getInstance();
  197. if (!_itemTable.isInitialized())
  198. {
  199. _log.severe("Could not find the extraced files. Please Check Your Data.");
  200. throw new Exception("Could not initialize the item table");
  201. }
  202. // Load clan hall data before zone data and doors table
  203. _cHManager = ClanHallManager.getInstance();
  204. ExtractableItemsData.getInstance();
  205. SummonItemsData.getInstance();
  206. ZoneManager.getInstance();
  207. MerchantPriceConfigTable.getInstance();
  208. TradeController.getInstance();
  209. InstanceManager.getInstance();
  210. if (Config.ALLOW_NPC_WALKERS)
  211. {
  212. NpcWalkerRoutesTable.getInstance().load();
  213. }
  214. NpcBufferTable.getInstance();
  215. RecipeController.getInstance();
  216. SkillTreeTable.getInstance();
  217. PetSkillsTable.getInstance();
  218. ArmorSetsTable.getInstance();
  219. FishTable.getInstance();
  220. SkillSpellbookTable.getInstance();
  221. CharTemplateTable.getInstance();
  222. NobleSkillTable.getInstance();
  223. HeroSkillTable.getInstance();
  224. ResidentialSkillTable.getInstance();
  225. // Call to load caches
  226. HtmCache.getInstance();
  227. CrestCache.getInstance();
  228. ClanTable.getInstance();
  229. _npcTable = NpcTable.getInstance();
  230. if (!_npcTable.isInitialized())
  231. {
  232. _log.severe("Could not find the extraced files. Please Check Your Data.");
  233. throw new Exception("Could not initialize the npc table");
  234. }
  235. _hennaTable = HennaTable.getInstance();
  236. if (!_hennaTable.isInitialized())
  237. {
  238. throw new Exception("Could not initialize the Henna Table");
  239. }
  240. HennaTreeTable.getInstance();
  241. if (!_hennaTable.isInitialized())
  242. {
  243. throw new Exception("Could not initialize the Henna Tree Table");
  244. }
  245. _helperBuffTable = HelperBuffTable.getInstance();
  246. if (!_helperBuffTable.isInitialized())
  247. {
  248. throw new Exception("Could not initialize the Helper Buff Table");
  249. }
  250. GeoData.getInstance();
  251. if (Config.GEODATA == 2)
  252. PathFinding.getInstance();
  253. CastleManager.getInstance();
  254. SiegeManager.getInstance();
  255. FortManager.getInstance();
  256. FortSiegeManager.getInstance();
  257. TeleportLocationTable.getInstance();
  258. LevelUpData.getInstance();
  259. L2World.getInstance();
  260. SpawnTable.getInstance();
  261. RaidBossSpawnManager.getInstance();
  262. DayNightSpawnManager.getInstance().notifyChangeMode();
  263. GrandBossManager.getInstance().initZones();
  264. RaidBossPointsManager.init();
  265. FourSepulchersManager.getInstance().init();
  266. DimensionalRiftManager.getInstance();
  267. Announcements.getInstance();
  268. AutoAnnounceTaskManager.getInstance();
  269. MapRegionTable.getInstance();
  270. EventDroplist.getInstance();
  271. _doorTable = DoorTable.getInstance();
  272. StaticObjects.getInstance();
  273. /** Load Manor data */
  274. L2Manor.getInstance();
  275. /** Load Manager */
  276. AuctionManager.getInstance();
  277. BoatManager.getInstance();
  278. CastleManorManager.getInstance();
  279. MercTicketManager.getInstance();
  280. // PartyCommandManager.getInstance();
  281. PetitionManager.getInstance();
  282. QuestManager.getInstance();
  283. TransformationManager.getInstance();
  284. AirShipManager.getInstance();
  285. try
  286. {
  287. _log.info("Loading Server Scripts");
  288. File scripts = new File(Config.DATAPACK_ROOT + "/data/scripts.cfg");
  289. L2ScriptEngineManager.getInstance().executeScriptList(scripts);
  290. }
  291. catch (IOException ioe)
  292. {
  293. _log.severe("Failed loading scripts.cfg, no script going to be loaded");
  294. }
  295. try
  296. {
  297. CompiledScriptCache compiledScriptCache = L2ScriptEngineManager.getInstance().getCompiledScriptCache();
  298. if (compiledScriptCache == null)
  299. {
  300. _log.info("Compiled Scripts Cache is disabled.");
  301. }
  302. else
  303. {
  304. compiledScriptCache.purge();
  305. if (compiledScriptCache.isModified())
  306. {
  307. compiledScriptCache.save();
  308. _log.info("Compiled Scripts Cache was saved.");
  309. }
  310. else
  311. {
  312. _log.info("Compiled Scripts Cache is up-to-date.");
  313. }
  314. }
  315. }
  316. catch (IOException e)
  317. {
  318. _log.log(Level.SEVERE, "Failed to store Compiled Scripts Cache.", e);
  319. }
  320. QuestManager.getInstance().report();
  321. TransformationManager.getInstance().report();
  322. AugmentationData.getInstance();
  323. if (Config.SAVE_DROPPED_ITEM)
  324. ItemsOnGroundManager.getInstance();
  325. if (Config.AUTODESTROY_ITEM_AFTER > 0
  326. || Config.HERB_AUTO_DESTROY_TIME > 0)
  327. ItemsAutoDestroy.getInstance();
  328. MonsterRace.getInstance();
  329. _sevenSignsEngine = SevenSigns.getInstance();
  330. SevenSignsFestival.getInstance();
  331. _autoSpawnHandler = AutoSpawnHandler.getInstance();
  332. _autoChatHandler = AutoChatHandler.getInstance();
  333. // Spawn the Orators/Preachers if in the Seal Validation period.
  334. _sevenSignsEngine.spawnSevenSignsNPC();
  335. Olympiad.getInstance();
  336. Hero.getInstance();
  337. FaenorScriptEngine.getInstance();
  338. // Init of a cursed weapon manager
  339. CursedWeaponsManager.getInstance();
  340. _log.config("AutoChatHandler: Loaded " + _autoChatHandler.size() + " handlers in total.");
  341. _log.config("AutoSpawnHandler: Loaded " + _autoSpawnHandler.size() + " handlers in total.");
  342. AdminCommandHandler.getInstance();
  343. ChatHandler.getInstance();
  344. ItemHandler.getInstance();
  345. SkillHandler.getInstance();
  346. UserCommandHandler.getInstance();
  347. VoicedCommandHandler.getInstance();
  348. AccessLevels.getInstance();
  349. AdminCommandAccessRights.getInstance();
  350. if (Config.L2JMOD_ALLOW_WEDDING)
  351. CoupleManager.getInstance();
  352. TaskManager.getInstance();
  353. GmListTable.getInstance();
  354. // read pet stats from db
  355. L2PetDataTable.getInstance().loadPetsData();
  356. Universe.getInstance();
  357. if (Config.ACCEPT_GEOEDITOR_CONN)
  358. GeoEditorListener.getInstance();
  359. _shutdownHandler = Shutdown.getInstance();
  360. Runtime.getRuntime().addShutdownHook(_shutdownHandler);
  361. try
  362. {
  363. _doorTable.getDoor(24190001).openMe();
  364. _doorTable.getDoor(24190002).openMe();
  365. _doorTable.getDoor(24190003).openMe();
  366. _doorTable.getDoor(24190004).openMe();
  367. _doorTable.getDoor(23180001).openMe();
  368. _doorTable.getDoor(23180002).openMe();
  369. _doorTable.getDoor(23180003).openMe();
  370. _doorTable.getDoor(23180004).openMe();
  371. _doorTable.getDoor(23180005).openMe();
  372. _doorTable.getDoor(23180006).openMe();
  373. _doorTable.checkAutoOpen();
  374. }
  375. catch (NullPointerException e)
  376. {
  377. _log.warning("There is errors in your Door.csv file. Update door.csv");
  378. if (Config.DEBUG)
  379. e.printStackTrace();
  380. }
  381. ForumsBBSManager.getInstance();
  382. _log.config("IdFactory: Free ObjectID's remaining: "
  383. + IdFactory.getInstance().size());
  384. // initialize the dynamic extension loader
  385. try
  386. {
  387. DynamicExtension.getInstance();
  388. }
  389. catch (Exception ex)
  390. {
  391. _log.log(Level.WARNING, "DynamicExtension could not be loaded and initialized", ex);
  392. }
  393. TvTManager.getInstance();
  394. KnownListUpdateTaskManager.getInstance();
  395. if (Config.DEADLOCK_DETECTOR)
  396. {
  397. _deadDetectThread = new DeadLockDetector();
  398. _deadDetectThread.setDaemon(true);
  399. _deadDetectThread.start();
  400. }
  401. else
  402. _deadDetectThread = null;
  403. System.gc();
  404. // maxMemory is the upper limit the jvm can use, totalMemory the size of
  405. // the current allocation pool, freeMemory the unused memory in the
  406. // allocation pool
  407. long freeMem = (Runtime.getRuntime().maxMemory() - Runtime.getRuntime().totalMemory() + Runtime.getRuntime().freeMemory()) / 1048576;
  408. long totalMem = Runtime.getRuntime().maxMemory() / 1048576;
  409. _log.info("GameServer Started, free memory " + freeMem + " Mb of " + totalMem + " Mb");
  410. _loginThread = LoginServerThread.getInstance();
  411. _loginThread.start();
  412. L2GamePacketHandler gph = new L2GamePacketHandler();
  413. SelectorConfig<L2GameClient> sc = new SelectorConfig<L2GameClient>(null, null, gph, gph);
  414. sc.setMaxSendPerPass(12);
  415. sc.setSelectorSleepTime(20);
  416. _selectorThread = new SelectorThread<L2GameClient>(sc, gph, gph, null);
  417. InetAddress bindAddress = null;
  418. if (!Config.GAMESERVER_HOSTNAME.equals("*"))
  419. {
  420. try
  421. {
  422. bindAddress = InetAddress.getByName(Config.GAMESERVER_HOSTNAME);
  423. }
  424. catch (UnknownHostException e1)
  425. {
  426. _log.severe("WARNING: The GameServer bind address is invalid, using all avaliable IPs. Reason: " + e1.getMessage());
  427. if (Config.DEVELOPER)
  428. {
  429. e1.printStackTrace();
  430. }
  431. }
  432. }
  433. try
  434. {
  435. _selectorThread.openServerSocket(bindAddress, Config.PORT_GAME);
  436. }
  437. catch (IOException e)
  438. {
  439. _log.severe("FATAL: Failed to open server socket. Reason: " + e.getMessage());
  440. if (Config.DEVELOPER)
  441. {
  442. e.printStackTrace();
  443. }
  444. System.exit(1);
  445. }
  446. _selectorThread.start();
  447. _log.config("Maximum Numbers of Connected Players: " + Config.MAXIMUM_ONLINE_USERS);
  448. long serverLoadEnd = System.currentTimeMillis();
  449. _log.info("Server Loaded in " + ((serverLoadEnd - serverLoadStart) / 1000) + " seconds");
  450. }
  451. public static void main(String[] args) throws Exception
  452. {
  453. Server.serverMode = Server.MODE_GAMESERVER;
  454. // Local Constants
  455. final String LOG_FOLDER = "log"; // Name of folder for log file
  456. final String LOG_NAME = "./log.cfg"; // Name of log file
  457. /*** Main ***/
  458. // Create log folder
  459. File logFolder = new File(Config.DATAPACK_ROOT, LOG_FOLDER);
  460. logFolder.mkdir();
  461. // Create input stream for log file -- or store file data into memory
  462. InputStream is = new FileInputStream(new File(LOG_NAME));
  463. LogManager.getLogManager().readConfiguration(is);
  464. is.close();
  465. // Initialize config
  466. Config.load();
  467. L2DatabaseFactory.getInstance();
  468. gameServer = new GameServer();
  469. if (Config.IS_TELNET_ENABLED)
  470. {
  471. _statusServer = new Status(Server.serverMode);
  472. _statusServer.start();
  473. }
  474. else
  475. {
  476. _log.info("Telnet server is currently disabled.");
  477. }
  478. }
  479. }