2
0

BaseGameServerRegister.java 16 KB

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