GameServer.java 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  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.LevelUpData;
  48. import net.sf.l2j.gameserver.datatables.MapRegionTable;
  49. import net.sf.l2j.gameserver.datatables.MerchantPriceConfigTable;
  50. import net.sf.l2j.gameserver.datatables.NobleSkillTable;
  51. import net.sf.l2j.gameserver.datatables.NpcBufferTable;
  52. import net.sf.l2j.gameserver.datatables.NpcTable;
  53. import net.sf.l2j.gameserver.datatables.NpcWalkerRoutesTable;
  54. import net.sf.l2j.gameserver.datatables.SkillSpellbookTable;
  55. import net.sf.l2j.gameserver.datatables.SkillTable;
  56. import net.sf.l2j.gameserver.datatables.SkillTreeTable;
  57. import net.sf.l2j.gameserver.datatables.SpawnTable;
  58. import net.sf.l2j.gameserver.datatables.StaticObjects;
  59. import net.sf.l2j.gameserver.datatables.SummonItemsData;
  60. import net.sf.l2j.gameserver.datatables.TeleportLocationTable;
  61. import net.sf.l2j.gameserver.geoeditorcon.GeoEditorListener;
  62. import net.sf.l2j.gameserver.handler.AdminCommandHandler;
  63. import net.sf.l2j.gameserver.handler.ChatHandler;
  64. import net.sf.l2j.gameserver.handler.ItemHandler;
  65. import net.sf.l2j.gameserver.handler.SkillHandler;
  66. import net.sf.l2j.gameserver.handler.UserCommandHandler;
  67. import net.sf.l2j.gameserver.handler.VoicedCommandHandler;
  68. import net.sf.l2j.gameserver.idfactory.IdFactory;
  69. import net.sf.l2j.gameserver.instancemanager.AuctionManager;
  70. import net.sf.l2j.gameserver.instancemanager.BoatManager;
  71. import net.sf.l2j.gameserver.instancemanager.CastleManager;
  72. import net.sf.l2j.gameserver.instancemanager.CastleManorManager;
  73. import net.sf.l2j.gameserver.instancemanager.ClanHallManager;
  74. import net.sf.l2j.gameserver.instancemanager.CoupleManager;
  75. import net.sf.l2j.gameserver.instancemanager.CursedWeaponsManager;
  76. import net.sf.l2j.gameserver.instancemanager.DayNightSpawnManager;
  77. import net.sf.l2j.gameserver.instancemanager.DimensionalRiftManager;
  78. import net.sf.l2j.gameserver.instancemanager.FortManager;
  79. import net.sf.l2j.gameserver.instancemanager.FortSiegeManager;
  80. import net.sf.l2j.gameserver.instancemanager.FourSepulchersManager;
  81. import net.sf.l2j.gameserver.instancemanager.GrandBossManager;
  82. import net.sf.l2j.gameserver.instancemanager.ItemsOnGroundManager;
  83. import net.sf.l2j.gameserver.instancemanager.MercTicketManager;
  84. import net.sf.l2j.gameserver.instancemanager.PetitionManager;
  85. import net.sf.l2j.gameserver.instancemanager.QuestManager;
  86. import net.sf.l2j.gameserver.instancemanager.RaidBossPointsManager;
  87. import net.sf.l2j.gameserver.instancemanager.RaidBossSpawnManager;
  88. import net.sf.l2j.gameserver.instancemanager.SiegeManager;
  89. import net.sf.l2j.gameserver.instancemanager.TransformationManager;
  90. import net.sf.l2j.gameserver.instancemanager.ZoneManager;
  91. import net.sf.l2j.gameserver.model.AutoChatHandler;
  92. import net.sf.l2j.gameserver.model.AutoSpawnHandler;
  93. import net.sf.l2j.gameserver.model.L2AdminCommandAccessRights;
  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; // 1024 * 1024 = 1048576;
  142. }
  143. public SelectorThread<L2GameClient> getSelectorThread()
  144. {
  145. return _selectorThread;
  146. }
  147. public ClanHallManager getCHManager(){
  148. return _cHManager;
  149. }
  150. public GameServer() throws Exception
  151. {
  152. long serverLoadStart = System.currentTimeMillis();
  153. gameServer = this;
  154. _log.finest("used mem:" + getUsedMemoryMB()+"MB" );
  155. if (Config.SERVER_VERSION != null)
  156. {
  157. _log.info("L2J Server Version: "+Config.SERVER_VERSION);
  158. }
  159. if (Config.DATAPACK_VERSION != null)
  160. {
  161. _log.info("L2J Datapack Version: "+Config.DATAPACK_VERSION);
  162. }
  163. _idFactory = IdFactory.getInstance();
  164. if (!_idFactory.isInitialized())
  165. {
  166. _log.severe("Could not read object IDs from DB. Please Check Your Data.");
  167. throw new Exception("Could not initialize the ID factory");
  168. }
  169. _threadpools = ThreadPoolManager.getInstance();
  170. new File(Config.DATAPACK_ROOT, "data/clans").mkdirs();
  171. new File(Config.DATAPACK_ROOT, "data/crests").mkdirs();
  172. // load script engines
  173. L2ScriptEngineManager.getInstance();
  174. // start game time control early
  175. GameTimeController.getInstance();
  176. // keep the references of Singletons to prevent garbage collection
  177. CharNameTable.getInstance();
  178. _skillTable = SkillTable.getInstance();
  179. if (!_skillTable.isInitialized())
  180. {
  181. _log.severe("Could not find the extraced files. Please Check Your Data.");
  182. throw new Exception("Could not initialize the skill table");
  183. }
  184. _itemTable = ItemTable.getInstance();
  185. if (!_itemTable.isInitialized())
  186. {
  187. _log.severe("Could not find the extraced files. Please Check Your Data.");
  188. throw new Exception("Could not initialize the item table");
  189. }
  190. // Load clan hall data before zone data and doors table
  191. _cHManager = ClanHallManager.getInstance();
  192. _doorTable = DoorTable.getInstance();
  193. _doorTable.parseData();
  194. StaticObjects.getInstance();
  195. ExtractableItemsData.getInstance();
  196. SummonItemsData.getInstance();
  197. MerchantPriceConfigTable.getInstance();
  198. TradeController.getInstance();
  199. if(Config.ALLOW_NPC_WALKERS)
  200. {
  201. NpcWalkerRoutesTable.getInstance().load();
  202. }
  203. NpcBufferTable.getInstance();
  204. RecipeController.getInstance();
  205. SkillTreeTable.getInstance();
  206. ArmorSetsTable.getInstance();
  207. FishTable.getInstance();
  208. SkillSpellbookTable.getInstance();
  209. CharTemplateTable.getInstance();
  210. NobleSkillTable.getInstance();
  211. HeroSkillTable.getInstance();
  212. //Call to load caches
  213. HtmCache.getInstance();
  214. CrestCache.getInstance();
  215. ClanTable.getInstance();
  216. _npcTable = NpcTable.getInstance();
  217. if (!_npcTable.isInitialized())
  218. {
  219. _log.severe("Could not find the extraced files. Please Check Your Data.");
  220. throw new Exception("Could not initialize the npc table");
  221. }
  222. _hennaTable = HennaTable.getInstance();
  223. if (!_hennaTable.isInitialized())
  224. {
  225. throw new Exception("Could not initialize the Henna Table");
  226. }
  227. HennaTreeTable.getInstance();
  228. if (!_hennaTable.isInitialized())
  229. {
  230. throw new Exception("Could not initialize the Henna Tree Table");
  231. }
  232. _helperBuffTable = HelperBuffTable.getInstance();
  233. if (!_helperBuffTable.isInitialized())
  234. {
  235. throw new Exception("Could not initialize the Helper Buff Table");
  236. }
  237. GeoData.getInstance();
  238. if (Config.GEODATA == 2)
  239. GeoPathFinding.getInstance();
  240. CastleManager.getInstance();
  241. SiegeManager.getInstance();
  242. FortManager.getInstance();
  243. FortSiegeManager.getInstance();
  244. TeleportLocationTable.getInstance();
  245. LevelUpData.getInstance();
  246. L2World.getInstance();
  247. SpawnTable.getInstance();
  248. ZoneManager.getInstance();
  249. RaidBossSpawnManager.getInstance();
  250. DayNightSpawnManager.getInstance().notifyChangeMode();
  251. GrandBossManager.getInstance();
  252. RaidBossPointsManager.init();
  253. FourSepulchersManager.getInstance().init();
  254. DimensionalRiftManager.getInstance();
  255. Announcements.getInstance();
  256. MapRegionTable.getInstance();
  257. EventDroplist.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 || Config.HERB_AUTO_DESTROY_TIME > 0)
  310. ItemsAutoDestroy.getInstance();
  311. MonsterRace.getInstance();
  312. _sevenSignsEngine = SevenSigns.getInstance();
  313. SevenSignsFestival.getInstance();
  314. _autoSpawnHandler = AutoSpawnHandler.getInstance();
  315. _autoChatHandler = AutoChatHandler.getInstance();
  316. // Spawn the Orators/Preachers if in the Seal Validation period.
  317. _sevenSignsEngine.spawnSevenSignsNPC();
  318. Olympiad.getInstance();
  319. Hero.getInstance();
  320. FaenorScriptEngine.getInstance();
  321. // Init of a cursed weapon manager
  322. CursedWeaponsManager.getInstance();
  323. _log.config("AutoChatHandler: Loaded " + _autoChatHandler.size() + " handlers in total.");
  324. _log.config("AutoSpawnHandler: Loaded " + _autoSpawnHandler.size() + " handlers in total.");
  325. AdminCommandHandler.getInstance();
  326. ChatHandler.getInstance();
  327. ItemHandler.getInstance();
  328. SkillHandler.getInstance();
  329. UserCommandHandler.getInstance();
  330. VoicedCommandHandler.getInstance();
  331. AccessLevels.getInstance();
  332. L2AdminCommandAccessRights.getInstance();
  333. if(Config.L2JMOD_ALLOW_WEDDING)
  334. CoupleManager.getInstance();
  335. TaskManager.getInstance();
  336. GmListTable.getInstance();
  337. // read pet stats from db
  338. L2PetDataTable.getInstance().loadPetsData();
  339. Universe.getInstance();
  340. if (Config.ACCEPT_GEOEDITOR_CONN)
  341. GeoEditorListener.getInstance();
  342. _shutdownHandler = Shutdown.getInstance();
  343. Runtime.getRuntime().addShutdownHook(_shutdownHandler);
  344. try
  345. {
  346. _doorTable.getDoor(24190001).openMe();
  347. _doorTable.getDoor(24190002).openMe();
  348. _doorTable.getDoor(24190003).openMe();
  349. _doorTable.getDoor(24190004).openMe();
  350. _doorTable.getDoor(23180001).openMe();
  351. _doorTable.getDoor(23180002).openMe();
  352. _doorTable.getDoor(23180003).openMe();
  353. _doorTable.getDoor(23180004).openMe();
  354. _doorTable.getDoor(23180005).openMe();
  355. _doorTable.getDoor(23180006).openMe();
  356. _doorTable.checkAutoOpen();
  357. }
  358. catch (NullPointerException e)
  359. {
  360. _log.warning("There is errors in your Door.csv file. Update door.csv");
  361. if (Config.DEBUG)
  362. e.printStackTrace();
  363. }
  364. ForumsBBSManager.getInstance();
  365. _log.config("IdFactory: Free ObjectID's remaining: " + IdFactory.getInstance().size());
  366. // initialize the dynamic extension loader
  367. try
  368. {
  369. DynamicExtension.getInstance();
  370. }
  371. catch (Exception ex)
  372. {
  373. _log.log(Level.WARNING, "DynamicExtension could not be loaded and initialized", ex);
  374. }
  375. FloodProtector.getInstance();
  376. TvTManager.getInstance();
  377. KnownListUpdateTaskManager.getInstance();
  378. System.gc();
  379. // maxMemory is the upper limit the jvm can use, totalMemory the size of the current allocation pool, freeMemory the unused memory in the allocation pool
  380. long freeMem = (Runtime.getRuntime().maxMemory()-Runtime.getRuntime().totalMemory()+Runtime.getRuntime().freeMemory()) / 1048576; // 1024 * 1024 = 1048576;
  381. long totalMem = Runtime.getRuntime().maxMemory() / 1048576;
  382. _log.info("GameServer Started, free memory "+freeMem+" Mb of "+totalMem+" Mb");
  383. _loginThread = LoginServerThread.getInstance();
  384. _loginThread.start();
  385. L2GamePacketHandler gph = new L2GamePacketHandler();
  386. SelectorConfig<L2GameClient> sc = new SelectorConfig<L2GameClient>(null, null, gph, gph);
  387. sc.setMaxSendPerPass(12);
  388. sc.setSelectorSleepTime(20);
  389. _selectorThread = new SelectorThread<L2GameClient>(sc, gph, gph, null);
  390. InetAddress bindAddress = null;
  391. if (!Config.GAMESERVER_HOSTNAME.equals("*"))
  392. {
  393. try
  394. {
  395. bindAddress = InetAddress.getByName(Config.GAMESERVER_HOSTNAME);
  396. }
  397. catch (UnknownHostException e1)
  398. {
  399. _log.severe("WARNING: The GameServer bind address is invalid, using all avaliable IPs. Reason: "+e1.getMessage());
  400. if (Config.DEVELOPER)
  401. {
  402. e1.printStackTrace();
  403. }
  404. }
  405. }
  406. try
  407. {
  408. _selectorThread.openServerSocket(bindAddress, Config.PORT_GAME);
  409. }
  410. catch (IOException e)
  411. {
  412. _log.severe("FATAL: Failed to open server socket. Reason: "+e.getMessage());
  413. if (Config.DEVELOPER)
  414. {
  415. e.printStackTrace();
  416. }
  417. System.exit(1);
  418. }
  419. _selectorThread.start();
  420. _log.config("Maximum Numbers of Connected Players: " + Config.MAXIMUM_ONLINE_USERS);
  421. long serverLoadEnd = System.currentTimeMillis();
  422. _log.info("Server Loaded in "+((serverLoadEnd - serverLoadStart) / 1000)+" seconds");
  423. }
  424. public static void main(String[] args) throws Exception
  425. {
  426. Server.serverMode = Server.MODE_GAMESERVER;
  427. // Local Constants
  428. final String LOG_FOLDER = "log"; // Name of folder for log file
  429. final String LOG_NAME = "./log.cfg"; // Name of log file
  430. /*** Main ***/
  431. // Create log folder
  432. File logFolder = new File(Config.DATAPACK_ROOT, LOG_FOLDER);
  433. logFolder.mkdir();
  434. // Create input stream for log file -- or store file data into memory
  435. InputStream is = new FileInputStream(new File(LOG_NAME));
  436. LogManager.getLogManager().readConfiguration(is);
  437. is.close();
  438. // Initialize config
  439. Config.load();
  440. L2DatabaseFactory.getInstance();
  441. gameServer = new GameServer();
  442. if ( Config.IS_TELNET_ENABLED ) {
  443. _statusServer = new Status(Server.serverMode);
  444. _statusServer.start();
  445. }
  446. else {
  447. _log.info("Telnet server is currently disabled.");
  448. }
  449. }
  450. }