GameServer.java 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  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 com.l2jserver.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 org.mmocore.network.SelectorConfig;
  27. import org.mmocore.network.SelectorThread;
  28. import com.l2jserver.Config;
  29. import com.l2jserver.L2DatabaseFactory;
  30. import com.l2jserver.Server;
  31. import com.l2jserver.gameserver.cache.CrestCache;
  32. import com.l2jserver.gameserver.cache.HtmCache;
  33. import com.l2jserver.gameserver.datatables.AccessLevels;
  34. import com.l2jserver.gameserver.datatables.AdminCommandAccessRights;
  35. import com.l2jserver.gameserver.datatables.ArmorSetsTable;
  36. import com.l2jserver.gameserver.datatables.AugmentationData;
  37. import com.l2jserver.gameserver.datatables.CharNameTable;
  38. import com.l2jserver.gameserver.datatables.CharTemplateTable;
  39. import com.l2jserver.gameserver.datatables.ClanTable;
  40. import com.l2jserver.gameserver.datatables.DoorTable;
  41. import com.l2jserver.gameserver.datatables.EnchantGroupsTable;
  42. import com.l2jserver.gameserver.datatables.EnchantHPBonusData;
  43. import com.l2jserver.gameserver.datatables.EventDroplist;
  44. import com.l2jserver.gameserver.datatables.ExtractableItemsData;
  45. import com.l2jserver.gameserver.datatables.ExtractableSkillsData;
  46. import com.l2jserver.gameserver.datatables.FishTable;
  47. import com.l2jserver.gameserver.datatables.GMSkillTable;
  48. import com.l2jserver.gameserver.datatables.HelperBuffTable;
  49. import com.l2jserver.gameserver.datatables.HennaTable;
  50. import com.l2jserver.gameserver.datatables.HennaTreeTable;
  51. import com.l2jserver.gameserver.datatables.HeroSkillTable;
  52. import com.l2jserver.gameserver.datatables.ItemTable;
  53. import com.l2jserver.gameserver.datatables.LevelUpData;
  54. import com.l2jserver.gameserver.datatables.MapRegionTable;
  55. import com.l2jserver.gameserver.datatables.MerchantPriceConfigTable;
  56. import com.l2jserver.gameserver.datatables.NobleSkillTable;
  57. import com.l2jserver.gameserver.datatables.NpcBufferTable;
  58. import com.l2jserver.gameserver.datatables.NpcTable;
  59. import com.l2jserver.gameserver.datatables.NpcWalkerRoutesTable;
  60. import com.l2jserver.gameserver.datatables.PetDataTable;
  61. import com.l2jserver.gameserver.datatables.PetSkillsTable;
  62. import com.l2jserver.gameserver.datatables.ResidentialSkillTable;
  63. import com.l2jserver.gameserver.datatables.SkillSpellbookTable;
  64. import com.l2jserver.gameserver.datatables.SkillTable;
  65. import com.l2jserver.gameserver.datatables.SkillTreeTable;
  66. import com.l2jserver.gameserver.datatables.SpawnTable;
  67. import com.l2jserver.gameserver.datatables.StaticObjects;
  68. import com.l2jserver.gameserver.datatables.SummonItemsData;
  69. import com.l2jserver.gameserver.datatables.TeleportLocationTable;
  70. import com.l2jserver.gameserver.datatables.UITable;
  71. import com.l2jserver.gameserver.geoeditorcon.GeoEditorListener;
  72. import com.l2jserver.gameserver.handler.AdminCommandHandler;
  73. import com.l2jserver.gameserver.handler.ChatHandler;
  74. import com.l2jserver.gameserver.handler.ItemHandler;
  75. import com.l2jserver.gameserver.handler.SkillHandler;
  76. import com.l2jserver.gameserver.handler.UserCommandHandler;
  77. import com.l2jserver.gameserver.handler.VoicedCommandHandler;
  78. import com.l2jserver.gameserver.idfactory.IdFactory;
  79. import com.l2jserver.gameserver.instancemanager.AirShipManager;
  80. import com.l2jserver.gameserver.instancemanager.AuctionManager;
  81. import com.l2jserver.gameserver.instancemanager.BoatManager;
  82. import com.l2jserver.gameserver.instancemanager.CastleManager;
  83. import com.l2jserver.gameserver.instancemanager.CastleManorManager;
  84. import com.l2jserver.gameserver.instancemanager.ClanHallManager;
  85. import com.l2jserver.gameserver.instancemanager.CoupleManager;
  86. import com.l2jserver.gameserver.instancemanager.CursedWeaponsManager;
  87. import com.l2jserver.gameserver.instancemanager.DayNightSpawnManager;
  88. import com.l2jserver.gameserver.instancemanager.DimensionalRiftManager;
  89. import com.l2jserver.gameserver.instancemanager.FortManager;
  90. import com.l2jserver.gameserver.instancemanager.FortSiegeManager;
  91. import com.l2jserver.gameserver.instancemanager.FourSepulchersManager;
  92. import com.l2jserver.gameserver.instancemanager.GrandBossManager;
  93. import com.l2jserver.gameserver.instancemanager.HellboundManager;
  94. import com.l2jserver.gameserver.instancemanager.InstanceManager;
  95. import com.l2jserver.gameserver.instancemanager.ItemsOnGroundManager;
  96. import com.l2jserver.gameserver.instancemanager.MailManager;
  97. import com.l2jserver.gameserver.instancemanager.MercTicketManager;
  98. import com.l2jserver.gameserver.instancemanager.PetitionManager;
  99. import com.l2jserver.gameserver.instancemanager.QuestManager;
  100. import com.l2jserver.gameserver.instancemanager.RaidBossPointsManager;
  101. import com.l2jserver.gameserver.instancemanager.RaidBossSpawnManager;
  102. import com.l2jserver.gameserver.instancemanager.SiegeManager;
  103. import com.l2jserver.gameserver.instancemanager.TerritoryWarManager;
  104. import com.l2jserver.gameserver.instancemanager.TransformationManager;
  105. import com.l2jserver.gameserver.instancemanager.ZoneManager;
  106. import com.l2jserver.gameserver.model.AutoChatHandler;
  107. import com.l2jserver.gameserver.model.AutoSpawnHandler;
  108. import com.l2jserver.gameserver.model.L2Manor;
  109. import com.l2jserver.gameserver.model.L2Multisell;
  110. import com.l2jserver.gameserver.model.L2World;
  111. import com.l2jserver.gameserver.model.PartyMatchRoomList;
  112. import com.l2jserver.gameserver.model.PartyMatchWaitingList;
  113. import com.l2jserver.gameserver.model.entity.Hero;
  114. import com.l2jserver.gameserver.model.entity.TvTManager;
  115. import com.l2jserver.gameserver.model.olympiad.Olympiad;
  116. import com.l2jserver.gameserver.network.L2GameClient;
  117. import com.l2jserver.gameserver.network.L2GamePacketHandler;
  118. import com.l2jserver.gameserver.network.communityserver.CommunityServerThread;
  119. import com.l2jserver.gameserver.pathfinding.PathFinding;
  120. import com.l2jserver.gameserver.script.faenor.FaenorScriptEngine;
  121. import com.l2jserver.gameserver.scripting.CompiledScriptCache;
  122. import com.l2jserver.gameserver.scripting.L2ScriptEngineManager;
  123. import com.l2jserver.gameserver.taskmanager.AutoAnnounceTaskManager;
  124. import com.l2jserver.gameserver.taskmanager.KnownListUpdateTaskManager;
  125. import com.l2jserver.gameserver.taskmanager.TaskManager;
  126. import com.l2jserver.gameserver.util.DynamicExtension;
  127. import com.l2jserver.status.Status;
  128. import com.l2jserver.util.DeadLockDetector;
  129. import com.l2jserver.util.IPv4Filter;
  130. /**
  131. * This class ...
  132. *
  133. * @version $Revision: 1.29.2.15.2.19 $ $Date: 2005/04/05 19:41:23 $
  134. */
  135. public class GameServer
  136. {
  137. private static final Logger _log = Logger.getLogger(GameServer.class.getName());
  138. private final SelectorThread<L2GameClient> _selectorThread;
  139. private final DeadLockDetector _deadDetectThread;
  140. private final IdFactory _idFactory;
  141. public static GameServer gameServer;
  142. private LoginServerThread _loginThread;
  143. private static Status _statusServer;
  144. public static final Calendar dateTimeServerStarted = Calendar.getInstance();
  145. public long getUsedMemoryMB()
  146. {
  147. return (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()) / 1048576; // ;
  148. }
  149. public SelectorThread<L2GameClient> getSelectorThread()
  150. {
  151. return _selectorThread;
  152. }
  153. public DeadLockDetector getDeadLockDetectorThread()
  154. {
  155. return _deadDetectThread;
  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. ThreadPoolManager.getInstance();
  177. new File(Config.DATAPACK_ROOT, "data/crests").mkdirs();
  178. new File("log/game").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. EnchantGroupsTable.getInstance();
  186. SkillTable.getInstance();
  187. ItemTable.getInstance();
  188. if (!ItemTable.getInstance().isInitialized())
  189. {
  190. _log.severe("Could not find the extraced files. Please Check Your Data.");
  191. throw new Exception("Could not initialize the item table");
  192. }
  193. // Load clan hall data before zone data and doors table
  194. ClanHallManager.getInstance();
  195. ExtractableItemsData.getInstance();
  196. ExtractableSkillsData.getInstance();
  197. SummonItemsData.getInstance();
  198. ZoneManager.getInstance();
  199. MerchantPriceConfigTable.getInstance().loadInstances();
  200. EnchantHPBonusData.getInstance();
  201. TradeController.getInstance();
  202. L2Multisell.getInstance();
  203. InstanceManager.getInstance();
  204. if (Config.ALLOW_NPC_WALKERS)
  205. {
  206. NpcWalkerRoutesTable.getInstance().load();
  207. }
  208. NpcBufferTable.getInstance();
  209. RecipeController.getInstance();
  210. SkillTreeTable.getInstance();
  211. PetSkillsTable.getInstance();
  212. ArmorSetsTable.getInstance();
  213. FishTable.getInstance();
  214. SkillSpellbookTable.getInstance();
  215. CharTemplateTable.getInstance();
  216. NobleSkillTable.getInstance();
  217. GMSkillTable.getInstance();
  218. HeroSkillTable.getInstance();
  219. ResidentialSkillTable.getInstance();
  220. // Call to load caches
  221. HtmCache.getInstance();
  222. CrestCache.getInstance();
  223. ClanTable.getInstance();
  224. NpcTable.getInstance();
  225. HennaTable.getInstance();
  226. HennaTreeTable.getInstance();
  227. HelperBuffTable.getInstance();
  228. GeoData.getInstance();
  229. if (Config.GEODATA == 2)
  230. PathFinding.getInstance();
  231. CastleManager.getInstance().loadInstances();
  232. SiegeManager.getInstance().getSieges();
  233. FortManager.getInstance().loadInstances();
  234. FortSiegeManager.getInstance();
  235. TerritoryWarManager.getInstance();
  236. TeleportLocationTable.getInstance();
  237. LevelUpData.getInstance();
  238. L2World.getInstance();
  239. SpawnTable.getInstance();
  240. RaidBossSpawnManager.getInstance();
  241. DayNightSpawnManager.getInstance().notifyChangeMode();
  242. GrandBossManager.getInstance().initZones();
  243. RaidBossPointsManager.init();
  244. FourSepulchersManager.getInstance().init();
  245. DimensionalRiftManager.getInstance();
  246. Announcements.getInstance();
  247. MapRegionTable.getInstance();
  248. EventDroplist.getInstance();
  249. DoorTable.getInstance();
  250. StaticObjects.getInstance();
  251. UITable.getInstance();
  252. /** Load Manor data */
  253. L2Manor.getInstance();
  254. /** Load Manager */
  255. AuctionManager.getInstance();
  256. BoatManager.getInstance();
  257. CastleManorManager.getInstance();
  258. MercTicketManager.getInstance();
  259. // PartyCommandManager.getInstance();
  260. PartyMatchWaitingList.getInstance();
  261. PartyMatchRoomList.getInstance();
  262. PetitionManager.getInstance();
  263. QuestManager.getInstance();
  264. TransformationManager.getInstance();
  265. AirShipManager.getInstance();
  266. try
  267. {
  268. _log.info("Loading Server Scripts");
  269. File scripts = new File(Config.DATAPACK_ROOT + "/data/scripts.cfg");
  270. if(!Config.ALT_DEV_NO_HANDLERS || !Config.ALT_DEV_NO_QUESTS)
  271. L2ScriptEngineManager.getInstance().executeScriptList(scripts);
  272. }
  273. catch (IOException ioe)
  274. {
  275. _log.severe("Failed loading scripts.cfg, no script going to be loaded");
  276. }
  277. try
  278. {
  279. CompiledScriptCache compiledScriptCache = L2ScriptEngineManager.getInstance().getCompiledScriptCache();
  280. if (compiledScriptCache == null)
  281. {
  282. _log.info("Compiled Scripts Cache is disabled.");
  283. }
  284. else
  285. {
  286. compiledScriptCache.purge();
  287. if (compiledScriptCache.isModified())
  288. {
  289. compiledScriptCache.save();
  290. _log.info("Compiled Scripts Cache was saved.");
  291. }
  292. else
  293. {
  294. _log.info("Compiled Scripts Cache is up-to-date.");
  295. }
  296. }
  297. }
  298. catch (IOException e)
  299. {
  300. _log.log(Level.SEVERE, "Failed to store Compiled Scripts Cache.", e);
  301. }
  302. QuestManager.getInstance().report();
  303. TransformationManager.getInstance().report();
  304. AugmentationData.getInstance();
  305. if (Config.SAVE_DROPPED_ITEM)
  306. ItemsOnGroundManager.getInstance();
  307. if (Config.AUTODESTROY_ITEM_AFTER > 0 || Config.HERB_AUTO_DESTROY_TIME > 0)
  308. ItemsAutoDestroy.getInstance();
  309. MonsterRace.getInstance();
  310. SevenSigns.getInstance().spawnSevenSignsNPC();
  311. SevenSignsFestival.getInstance();
  312. AutoSpawnHandler.getInstance();
  313. AutoChatHandler.getInstance();
  314. Olympiad.getInstance();
  315. Hero.getInstance();
  316. FaenorScriptEngine.getInstance();
  317. // Init of a cursed weapon manager
  318. CursedWeaponsManager.getInstance();
  319. _log.info("AutoChatHandler: Loaded " + AutoChatHandler.getInstance().size() + " handlers in total.");
  320. _log.info("AutoSpawnHandler: Loaded " + AutoSpawnHandler.getInstance().size() + " handlers in total.");
  321. AdminCommandHandler.getInstance();
  322. ChatHandler.getInstance();
  323. ItemHandler.getInstance();
  324. SkillHandler.getInstance();
  325. UserCommandHandler.getInstance();
  326. VoicedCommandHandler.getInstance();
  327. AccessLevels.getInstance();
  328. AdminCommandAccessRights.getInstance();
  329. if (Config.L2JMOD_ALLOW_WEDDING)
  330. CoupleManager.getInstance();
  331. TaskManager.getInstance();
  332. GmListTable.getInstance();
  333. // read pet stats from db
  334. PetDataTable.getInstance().loadPetsData();
  335. MerchantPriceConfigTable.getInstance().updateReferences();
  336. CastleManager.getInstance().activateInstances();
  337. FortManager.getInstance().activateInstances();
  338. HellboundManager.getInstance();
  339. if (Config.ALLOW_MAIL)
  340. MailManager.getInstance();
  341. //Universe.getInstance();
  342. if (Config.ACCEPT_GEOEDITOR_CONN)
  343. GeoEditorListener.getInstance();
  344. Runtime.getRuntime().addShutdownHook(Shutdown.getInstance());
  345. _log.info("IdFactory: Free ObjectID's remaining: " + IdFactory.getInstance().size());
  346. // initialize the dynamic extension loader
  347. try
  348. {
  349. DynamicExtension.getInstance();
  350. }
  351. catch (Exception ex)
  352. {
  353. _log.log(Level.WARNING, "DynamicExtension could not be loaded and initialized", ex);
  354. }
  355. TvTManager.getInstance();
  356. KnownListUpdateTaskManager.getInstance();
  357. if (Config.DEADLOCK_DETECTOR)
  358. {
  359. _deadDetectThread = new DeadLockDetector();
  360. _deadDetectThread.setDaemon(true);
  361. _deadDetectThread.start();
  362. }
  363. else
  364. _deadDetectThread = null;
  365. System.gc();
  366. // maxMemory is the upper limit the jvm can use, totalMemory the size of
  367. // the current allocation pool, freeMemory the unused memory in the
  368. // allocation pool
  369. long freeMem = (Runtime.getRuntime().maxMemory() - Runtime.getRuntime().totalMemory() + Runtime.getRuntime().freeMemory()) / 1048576;
  370. long totalMem = Runtime.getRuntime().maxMemory() / 1048576;
  371. _log.info("GameServer Started, free memory " + freeMem + " Mb of " + totalMem + " Mb");
  372. _loginThread = LoginServerThread.getInstance();
  373. _loginThread.start();
  374. CommunityServerThread.initialize();
  375. final SelectorConfig sc = new SelectorConfig();
  376. sc.MAX_READ_PER_PASS = Config.MMO_MAX_READ_PER_PASS;
  377. sc.MAX_SEND_PER_PASS = Config.MMO_MAX_SEND_PER_PASS;
  378. sc.SLEEP_TIME = Config.MMO_SELECTOR_SLEEP_TIME;
  379. sc.HELPER_BUFFER_COUNT = Config.MMO_HELPER_BUFFER_COUNT;
  380. final L2GamePacketHandler gph = new L2GamePacketHandler();
  381. _selectorThread = new SelectorThread<L2GameClient>(sc, gph, gph, gph, new IPv4Filter());
  382. InetAddress bindAddress = null;
  383. if (!Config.GAMESERVER_HOSTNAME.equals("*"))
  384. {
  385. try
  386. {
  387. bindAddress = InetAddress.getByName(Config.GAMESERVER_HOSTNAME);
  388. }
  389. catch (UnknownHostException e1)
  390. {
  391. _log.log(Level.SEVERE, "WARNING: The GameServer bind address is invalid, using all avaliable IPs. Reason: " + e1.getMessage(), e1);
  392. }
  393. }
  394. try
  395. {
  396. _selectorThread.openServerSocket(bindAddress, Config.PORT_GAME);
  397. }
  398. catch (IOException e)
  399. {
  400. _log.log(Level.SEVERE, "FATAL: Failed to open server socket. Reason: " + e.getMessage(), e);
  401. System.exit(1);
  402. }
  403. _selectorThread.start();
  404. _log.info("Maximum Numbers of Connected Players: " + Config.MAXIMUM_ONLINE_USERS);
  405. long serverLoadEnd = System.currentTimeMillis();
  406. _log.info("Server Loaded in " + ((serverLoadEnd - serverLoadStart) / 1000) + " seconds");
  407. AutoAnnounceTaskManager.getInstance();
  408. }
  409. public static void main(String[] args) throws Exception
  410. {
  411. Server.serverMode = Server.MODE_GAMESERVER;
  412. // Local Constants
  413. final String LOG_FOLDER = "log"; // Name of folder for log file
  414. final String LOG_NAME = "./log.cfg"; // Name of log file
  415. /*** Main ***/
  416. // Create log folder
  417. File logFolder = new File(Config.DATAPACK_ROOT, LOG_FOLDER);
  418. logFolder.mkdir();
  419. // Create input stream for log file -- or store file data into memory
  420. InputStream is = new FileInputStream(new File(LOG_NAME));
  421. LogManager.getLogManager().readConfiguration(is);
  422. is.close();
  423. // Initialize config
  424. Config.load();
  425. L2DatabaseFactory.getInstance();
  426. gameServer = new GameServer();
  427. if (Config.IS_TELNET_ENABLED)
  428. {
  429. _statusServer = new Status(Server.serverMode);
  430. _statusServer.start();
  431. }
  432. else
  433. {
  434. _log.info("Telnet server is currently disabled.");
  435. }
  436. }
  437. }