BaseGameServerRegister.java 16 KB

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