GameServer.java 18 KB

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