GameServer.java 19 KB

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