GameServer.java 18 KB

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