BaseGameServerRegister.java 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632
  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.tools.gsregistering;
  16. import java.awt.HeadlessException;
  17. import java.io.File;
  18. import java.io.FileOutputStream;
  19. import java.io.IOException;
  20. import java.io.OutputStream;
  21. import java.math.BigInteger;
  22. import java.sql.Connection;
  23. import java.sql.PreparedStatement;
  24. import java.sql.SQLException;
  25. import java.sql.Statement;
  26. import java.util.Locale;
  27. import java.util.Map.Entry;
  28. import java.util.Properties;
  29. import java.util.ResourceBundle;
  30. import javax.swing.SwingUtilities;
  31. import javax.swing.UIManager;
  32. import com.l2jserver.Config;
  33. import com.l2jserver.L2DatabaseFactory;
  34. import com.l2jserver.Server;
  35. import com.l2jserver.loginserver.GameServerTable;
  36. import com.l2jserver.tools.i18n.LanguageControl;
  37. import com.l2jserver.util.Util;
  38. /**
  39. * The Class BaseGameServerRegister.
  40. * @author KenM
  41. */
  42. public abstract class BaseGameServerRegister
  43. {
  44. private boolean _loaded = false;
  45. private ResourceBundle _bundle;
  46. /**
  47. * The main method.
  48. * @param args the arguments
  49. */
  50. public static void main(String[] args)
  51. {
  52. Locale locale = null;
  53. boolean gui = true;
  54. boolean interactive = true;
  55. boolean force = false;
  56. boolean fallback = false;
  57. BaseTask task = null;
  58. ResourceBundle bundle = null;
  59. try
  60. {
  61. locale = Locale.getDefault();
  62. bundle = ResourceBundle.getBundle("gsregister.GSRegister", locale, LanguageControl.INSTANCE);
  63. }
  64. catch (Throwable t)
  65. {
  66. System.out.println("FATAL: Failed to load default translation.");
  67. System.exit(666);
  68. }
  69. String arg;
  70. for (int i = 0; i < args.length; i++)
  71. {
  72. arg = args[i];
  73. // --cmd : no gui
  74. if (arg.equals("-c") || arg.equals("--cmd"))
  75. {
  76. gui = false;
  77. }
  78. // --force : Forces GameServer register operations to overwrite a server if necessary
  79. else if (arg.equals("-f") || arg.equals("--force"))
  80. {
  81. force = true;
  82. }
  83. // --fallback : If an register operation fails due to ID already being in use it will then try to register first available ID
  84. else if (arg.equals("-b") || arg.equals("--fallback"))
  85. {
  86. fallback = true;
  87. }
  88. // --register <id> <hexid_dest_dir> : Register GameServer with ID <id> and output hexid on <hexid_dest_dir>
  89. // Fails if <id> already in use, unless -force is used (overwrites)
  90. else if (arg.equals("-r") || arg.equals("--register"))
  91. {
  92. gui = false;
  93. interactive = false;
  94. int id = Integer.parseInt(args[++i]);
  95. String dir = args[++i];
  96. task = new RegisterTask(id, dir, force, fallback);
  97. }
  98. // --unregister <id> : Removes GameServer denoted by <id>
  99. else if (arg.equals("-u") || arg.equals("--unregister"))
  100. {
  101. gui = false;
  102. interactive = false;
  103. String gsId = args[++i];
  104. if (gsId.equalsIgnoreCase("all"))
  105. {
  106. task = new UnregisterAllTask();
  107. }
  108. else
  109. {
  110. try
  111. {
  112. int id = Integer.parseInt(gsId);
  113. task = new UnregisterTask(id);
  114. }
  115. catch (NumberFormatException e)
  116. {
  117. if (bundle != null)
  118. {
  119. System.out.printf(bundle.getString("wrongUnregisterArg") + '\n', gsId);
  120. }
  121. System.exit(1);
  122. }
  123. }
  124. }
  125. // --language <locale> : Sets the app to use the specified locale, overriding auto-detection
  126. else if (arg.equals("-l") || arg.equals("--language"))
  127. {
  128. String loc = args[++i];
  129. Locale[] availableLocales = Locale.getAvailableLocales();
  130. Locale l;
  131. for (int j = 0; (j < availableLocales.length) && (locale == null); j++)
  132. {
  133. l = availableLocales[j];
  134. if (l.toString().equals(loc))
  135. {
  136. locale = l;
  137. }
  138. }
  139. if (locale == null)
  140. {
  141. System.out.println("Specified locale '" + loc + "' was not found, using default behaviour.");
  142. }
  143. else
  144. {
  145. try
  146. {
  147. bundle = ResourceBundle.getBundle("gsregister.GSRegister", locale, LanguageControl.INSTANCE);
  148. }
  149. catch (Throwable t)
  150. {
  151. System.out.println("Failed to load translation ''");
  152. }
  153. }
  154. }
  155. // --help : Prints usage/arguments/credits
  156. else if (arg.equals("-h") || arg.equals("--help"))
  157. {
  158. gui = false;
  159. interactive = false;
  160. BaseGameServerRegister.printHelp(bundle);
  161. }
  162. }
  163. try
  164. {
  165. if (gui)
  166. {
  167. BaseGameServerRegister.startGUI(bundle);
  168. }
  169. else
  170. {
  171. if (interactive)
  172. {
  173. BaseGameServerRegister.startCMD(bundle);
  174. }
  175. else
  176. {
  177. // if there is a task, do it, else the app has already finished
  178. if (task != null)
  179. {
  180. task.setBundle(bundle);
  181. task.run();
  182. }
  183. }
  184. }
  185. }
  186. catch (HeadlessException e)
  187. {
  188. BaseGameServerRegister.startCMD(bundle);
  189. }
  190. }
  191. /**
  192. * Prints the help.
  193. * @param bundle the bundle
  194. */
  195. private static void printHelp(ResourceBundle bundle)
  196. {
  197. String[] help =
  198. {
  199. bundle.getString("purpose"),
  200. "",
  201. bundle.getString("options"),
  202. "-b, --fallback\t\t\t\t" + bundle.getString("fallbackOpt"),
  203. "-c, --cmd\t\t\t\t" + bundle.getString("cmdOpt"),
  204. "-f, --force\t\t\t\t" + bundle.getString("forceOpt"),
  205. "-h, --help\t\t\t\t" + bundle.getString("helpOpt"),
  206. "-l, --language\t\t\t\t" + bundle.getString("languageOpt"),
  207. "-r, --register <id> <hexid_dest_dir>\t" + bundle.getString("registerOpt1"),
  208. "\t\t\t\t\t" + bundle.getString("registerOpt2"),
  209. "\t\t\t\t\t" + bundle.getString("registerOpt3"),
  210. "",
  211. "-u, --unregister <id>|all\t\t" + bundle.getString("unregisterOpt"),
  212. "",
  213. bundle.getString("credits"),
  214. bundle.getString("bugReports") + " http://www.l2jserver.com"
  215. /*
  216. * "-b, --fallback\t\t\t\tIf an register operation fails due to ID already being in use it will then try to register first available ID", "-c, --cmd\t\t\t\tForces application to run in command-line mode even if the GUI is supported.",
  217. * "-f, --force\t\t\t\tForces GameServer register operations to overwrite a server if necessary", "-h, --help\t\t\t\tPrints this help message", "-l, --language <locale>\t\t\t\tAsks the application to use the specified locale, overriding auto-detection",
  218. * "-r, --register <id> <hexid_dest_dir>\tRegister GameServer with ID <id> and output hexid on <hexid_dest_dir>", "\t\t\t\t\tUse a negative value on <id> to register the first available ID", "\t\t\t\t\tFails if <id> already in use, unless --force is used (overwrites)", "",
  219. * "-u, --unregister <id>|all\t\tRemoves GameServer denoted by <id>, use \"all\" for removing all registered GameServers", "", "Copyright (C) L2J Team 2008-2012.", "Report bugs: http://www.l2jserver.com"
  220. */
  221. };
  222. for (String str : help)
  223. {
  224. System.out.println(str);
  225. }
  226. }
  227. /**
  228. * Start the GUI.
  229. * @param bundle the bundle.
  230. */
  231. private static void startGUI(final ResourceBundle bundle)
  232. {
  233. try
  234. {
  235. // avoid that ugly Metal LaF
  236. UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
  237. }
  238. catch (Exception e)
  239. {
  240. // couldn't care less
  241. }
  242. SwingUtilities.invokeLater(new Runnable()
  243. {
  244. @Override
  245. public void run()
  246. {
  247. GUserInterface gui = new GUserInterface(bundle);
  248. gui.getFrame().setVisible(true);
  249. }
  250. });
  251. }
  252. /**
  253. * Start the CMD.
  254. * @param bundle the bundle.
  255. */
  256. private static void startCMD(final ResourceBundle bundle)
  257. {
  258. GameServerRegister cmdUi = new GameServerRegister(bundle);
  259. try
  260. {
  261. cmdUi.consoleUI();
  262. }
  263. catch (IOException e)
  264. {
  265. cmdUi.showError("I/O exception trying to get input from keyboard.", e);
  266. }
  267. }
  268. /**
  269. * Instantiates a new base game server register.
  270. * @param bundle the bundle.
  271. */
  272. public BaseGameServerRegister(ResourceBundle bundle)
  273. {
  274. setBundle(bundle);
  275. }
  276. /**
  277. * Load.
  278. */
  279. public void load()
  280. {
  281. Server.serverMode = Server.MODE_LOGINSERVER;
  282. Config.load();
  283. GameServerTable.getInstance();
  284. _loaded = true;
  285. }
  286. /**
  287. * Checks if is loaded.
  288. * @return true, if is loaded
  289. */
  290. public boolean isLoaded()
  291. {
  292. return _loaded;
  293. }
  294. /**
  295. * Sets the bundle.
  296. * @param bundle the bundle to set.
  297. */
  298. public void setBundle(ResourceBundle bundle)
  299. {
  300. _bundle = bundle;
  301. }
  302. /**
  303. * Gets the bundle.
  304. * @return the bundle.
  305. */
  306. public ResourceBundle getBundle()
  307. {
  308. return _bundle;
  309. }
  310. /**
  311. * Show the error.
  312. * @param msg the msg.
  313. * @param t the t.
  314. */
  315. public abstract void showError(String msg, Throwable t);
  316. /**
  317. * Unregister the game server.
  318. * @param id the game server id.
  319. * @throws SQLException the SQL exception.
  320. */
  321. public static void unregisterGameServer(int id) throws SQLException
  322. {
  323. Connection con = null;
  324. try
  325. {
  326. con = L2DatabaseFactory.getInstance().getConnection();
  327. try (PreparedStatement ps = con.prepareStatement("DELETE FROM gameservers WHERE server_id = ?"))
  328. {
  329. ps.setInt(1, id);
  330. ps.executeUpdate();
  331. }
  332. GameServerTable.getInstance().getRegisteredGameServers().remove(id);
  333. }
  334. finally
  335. {
  336. L2DatabaseFactory.close(con);
  337. }
  338. }
  339. /**
  340. * Unregister all game servers.
  341. * @throws SQLException the SQL exception
  342. */
  343. public static void unregisterAllGameServers() throws SQLException
  344. {
  345. Connection con = null;
  346. try
  347. {
  348. con = L2DatabaseFactory.getInstance().getConnection();
  349. try (Statement s = con.createStatement())
  350. {
  351. s.executeUpdate("DELETE FROM gameservers");
  352. }
  353. GameServerTable.getInstance().getRegisteredGameServers().clear();
  354. }
  355. finally
  356. {
  357. L2DatabaseFactory.close(con);
  358. }
  359. }
  360. /**
  361. * Register a game server.
  362. * @param id the id of the game server.
  363. * @param outDir the out dir.
  364. * @throws IOException Signals that an I/O exception has occurred.
  365. */
  366. public static void registerGameServer(int id, String outDir) throws IOException
  367. {
  368. byte[] hexId = Util.generateHex(16);
  369. GameServerTable.getInstance().registerServerOnDB(hexId, id, "");
  370. Properties hexSetting = new Properties();
  371. File file = new File(outDir, "hexid.txt");
  372. // Create a new empty file only if it doesn't exist
  373. file.createNewFile();
  374. try (OutputStream out = new FileOutputStream(file))
  375. {
  376. hexSetting.setProperty("ServerID", String.valueOf(id));
  377. hexSetting.setProperty("HexID", new BigInteger(hexId).toString(16));
  378. hexSetting.store(out, "The HexId to Auth into LoginServer");
  379. }
  380. }
  381. /**
  382. * Register first available.
  383. * @param outDir the out dir
  384. * @return the int
  385. * @throws IOException Signals that an I/O exception has occurred.
  386. */
  387. public static int registerFirstAvailable(String outDir) throws IOException
  388. {
  389. for (Entry<Integer, String> e : GameServerTable.getInstance().getServerNames().entrySet())
  390. {
  391. if (!GameServerTable.getInstance().hasRegisteredGameServerOnId(e.getKey()))
  392. {
  393. BaseGameServerRegister.registerGameServer(e.getKey(), outDir);
  394. return e.getKey();
  395. }
  396. }
  397. return -1;
  398. }
  399. /**
  400. * The Class BaseTask.
  401. */
  402. protected static abstract class BaseTask implements Runnable
  403. {
  404. private ResourceBundle _bundle;
  405. /**
  406. * Sets the bundle.
  407. * @param bundle The bundle to set.
  408. */
  409. public void setBundle(ResourceBundle bundle)
  410. {
  411. _bundle = bundle;
  412. }
  413. /**
  414. * Gets the bundle.
  415. * @return Returns the bundle.
  416. */
  417. public ResourceBundle getBundle()
  418. {
  419. return _bundle;
  420. }
  421. /**
  422. * Show the error.
  423. * @param msg the msg
  424. * @param t the t
  425. */
  426. public void showError(String msg, Throwable t)
  427. {
  428. String title;
  429. if (getBundle() != null)
  430. {
  431. title = getBundle().getString("error");
  432. msg += '\n' + getBundle().getString("reason") + ' ' + t.getLocalizedMessage();
  433. }
  434. else
  435. {
  436. title = "Error";
  437. msg += "\nCause: " + t.getLocalizedMessage();
  438. }
  439. System.out.println(title + ": " + msg);
  440. }
  441. }
  442. /**
  443. * The Class RegisterTask.
  444. */
  445. private static class RegisterTask extends BaseTask
  446. {
  447. private final int _id;
  448. private final String _outDir;
  449. private boolean _force;
  450. private boolean _fallback;
  451. /**
  452. * Instantiates a new register task.
  453. * @param id the id.
  454. * @param outDir the out dir.
  455. * @param force the force.
  456. * @param fallback the fallback.
  457. */
  458. public RegisterTask(int id, String outDir, boolean force, boolean fallback)
  459. {
  460. _id = id;
  461. _outDir = outDir;
  462. _force = force;
  463. _fallback = fallback;
  464. }
  465. /**
  466. * Sets the actions.
  467. * @param force the force.
  468. * @param fallback the fallback.
  469. */
  470. @SuppressWarnings("unused")
  471. public void setActions(boolean force, boolean fallback)
  472. {
  473. _force = force;
  474. _fallback = fallback;
  475. }
  476. @Override
  477. public void run()
  478. {
  479. try
  480. {
  481. if (_id < 0)
  482. {
  483. int registeredId = BaseGameServerRegister.registerFirstAvailable(_outDir);
  484. if (registeredId < 0)
  485. {
  486. System.out.println(getBundle().getString("noFreeId"));
  487. }
  488. else
  489. {
  490. System.out.printf(getBundle().getString("registrationOk") + '\n', registeredId);
  491. }
  492. }
  493. else
  494. {
  495. System.out.printf(getBundle().getString("checkingIdInUse") + '\n', _id);
  496. if (GameServerTable.getInstance().hasRegisteredGameServerOnId(_id))
  497. {
  498. System.out.println(getBundle().getString("yes"));
  499. if (_force)
  500. {
  501. System.out.printf(getBundle().getString("forcingRegistration") + '\n', _id);
  502. BaseGameServerRegister.unregisterGameServer(_id);
  503. BaseGameServerRegister.registerGameServer(_id, _outDir);
  504. System.out.printf(getBundle().getString("registrationOk") + '\n', _id);
  505. }
  506. else if (_fallback)
  507. {
  508. System.out.println(getBundle().getString("fallingBack"));
  509. int registeredId = BaseGameServerRegister.registerFirstAvailable(_outDir);
  510. if (registeredId < 0)
  511. {
  512. System.out.println(getBundle().getString("noFreeId"));
  513. }
  514. else
  515. {
  516. System.out.printf(getBundle().getString("registrationOk") + '\n', registeredId);
  517. }
  518. }
  519. else
  520. {
  521. System.out.println(getBundle().getString("noAction"));
  522. }
  523. }
  524. else
  525. {
  526. System.out.println(getBundle().getString("no"));
  527. BaseGameServerRegister.registerGameServer(_id, _outDir);
  528. }
  529. }
  530. }
  531. catch (SQLException e)
  532. {
  533. showError(getBundle().getString("sqlErrorRegister"), e);
  534. }
  535. catch (IOException e)
  536. {
  537. showError(getBundle().getString("ioErrorRegister"), e);
  538. }
  539. }
  540. }
  541. /**
  542. * The Class UnregisterTask.
  543. */
  544. private static class UnregisterTask extends BaseTask
  545. {
  546. private final int _id;
  547. /**
  548. * Instantiates a new unregister task.
  549. * @param id the task id.
  550. */
  551. public UnregisterTask(int id)
  552. {
  553. _id = id;
  554. }
  555. @Override
  556. public void run()
  557. {
  558. System.out.printf(getBundle().getString("removingGsId") + '\n', _id);
  559. try
  560. {
  561. BaseGameServerRegister.unregisterGameServer(_id);
  562. }
  563. catch (SQLException e)
  564. {
  565. showError(getBundle().getString("sqlErrorRegister"), e);
  566. }
  567. }
  568. }
  569. /**
  570. * The Class UnregisterAllTask.
  571. */
  572. protected static class UnregisterAllTask extends BaseTask
  573. {
  574. @Override
  575. public void run()
  576. {
  577. try
  578. {
  579. BaseGameServerRegister.unregisterAllGameServers();
  580. }
  581. catch (SQLException e)
  582. {
  583. showError(getBundle().getString("sqlErrorUnregisterAll"), e);
  584. }
  585. }
  586. }
  587. }