GameServer.java 18 KB

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