GameServer.java 18 KB

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