2
0

BaseGameServerRegister.java 16 KB

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