ソースを参照

Make hexid.txt to be written to custom folder when L2J_HOME is defined.

HorridoJoho 2 年 前
コミット
e03feea843

+ 19 - 1
src/main/java/com/l2jserver/gameserver/GameServer.java

@@ -1,5 +1,5 @@
 /*
- * Copyright © 2004-2021 L2J Server
+ * Copyright © 2004-2022 L2J Server
  * 
  * This file is part of L2J Server.
  * 
@@ -164,6 +164,8 @@ public final class GameServer {
 	
 	private static final String GEODATA = "-gd";
 	
+	private static final String L2JHOME_VARIABLE = "L2J_HOME";
+	
 	private final SelectorThread<L2GameClient> _selectorThread;
 	
 	private final L2GamePacketHandler _gamePacketHandler;
@@ -497,4 +499,20 @@ public final class GameServer {
 		s = sBuilder.toString();
 		LOG.info(s);
 	}
+	
+	/**
+	 * Returns either the value of the L2J_HOME variable or null.
+	 * <br><br>
+	 * When the L2J_HOME variable can be found as system property or environment
+	 * variable, it's value is returned (favoring the system property). If the
+	 * variable is not available, null is returned.
+	 * @return returns the value of the L2J_HOME variable or null
+	 */
+	public static String getL2jHomeVariable() {
+		String l2jHome = System.getProperty(L2JHOME_VARIABLE);
+		if (l2jHome == null) {
+			l2jHome = System.getenv(L2JHOME_VARIABLE);
+		}
+		return l2jHome;
+	}
 }

+ 18 - 21
src/main/java/com/l2jserver/gameserver/LoginServerThread.java

@@ -1,5 +1,5 @@
 /*
- * Copyright © 2004-2021 L2J Server
+ * Copyright © 2004-2022 L2J Server
  * 
  * This file is part of L2J Server.
  * 
@@ -24,8 +24,6 @@ import static com.l2jserver.gameserver.config.Configuration.ip;
 import static com.l2jserver.gameserver.config.Configuration.server;
 
 import java.io.BufferedOutputStream;
-import java.io.File;
-import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
@@ -33,6 +31,9 @@ import java.math.BigInteger;
 import java.net.Socket;
 import java.net.SocketException;
 import java.net.UnknownHostException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.StandardOpenOption;
 import java.security.GeneralSecurityException;
 import java.security.KeyFactory;
 import java.security.interfaces.RSAPublicKey;
@@ -42,7 +43,6 @@ import java.sql.SQLException;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Map;
-import java.util.Properties;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.CopyOnWriteArrayList;
 
@@ -54,6 +54,8 @@ import com.l2jserver.commons.network.BaseSendablePacket;
 import com.l2jserver.commons.security.crypt.NewCrypt;
 import com.l2jserver.commons.util.Rnd;
 import com.l2jserver.commons.util.Util;
+import com.l2jserver.gameserver.config.Configuration;
+import com.l2jserver.gameserver.config.HexIdConfiguration;
 import com.l2jserver.gameserver.model.L2World;
 import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
 import com.l2jserver.gameserver.network.L2GameClient;
@@ -88,8 +90,6 @@ public class LoginServerThread extends Thread {
 	
 	protected static final Logger LOG_ACCOUNTING = LoggerFactory.getLogger("accounting");
 	
-	private static final String HEXID_FILE = "./config/hexid.txt";
-	
 	private static final int TEENAGER = 15;
 	
 	private static final int ADULT = 18;
@@ -222,13 +222,13 @@ public class LoginServerThread extends Thread {
 						}
 						case 0x01 -> {
 							LoginServerFail lsf = new LoginServerFail(incoming);
-							LOG.info("Damn! Registeration Failed: {}", lsf.getReasonString());
+							LOG.info("Damn! Registration Failed: {}", lsf.getReasonString());
 						}
 						case 0x02 -> {
 							AuthResponse aresp = new AuthResponse(incoming);
 							int serverID = aresp.getServerId();
 							_serverName = aresp.getServerName();
-							saveHexid(serverID, hexToString(_hexID), HEXID_FILE);
+							saveHexid(serverID, hexToString(_hexID));
 							LOG.info("Registered on login as Server {}: {}", serverID, _serverName);
 							ServerStatus st = new ServerStatus();
 							if (general().getServerListBrackets()) {
@@ -674,21 +674,18 @@ public class LoginServerThread extends Thread {
 	 * Save hexadecimal ID of the server in the L2Properties file.
 	 * @param serverId the ID of the server whose hexId to save
 	 * @param hexId the hexadecimal ID to store
-	 * @param fileName name of the L2Properties file
 	 */
-	public static void saveHexid(int serverId, String hexId, String fileName) {
-		try {
-			Properties hexSetting = new Properties();
-			File file = new File(fileName);
-			// Create a new empty file only if it doesn't exist
-			file.createNewFile();
-			try (OutputStream out = new FileOutputStream(file)) {
-				hexSetting.setProperty("ServerID", String.valueOf(serverId));
-				hexSetting.setProperty("HexID", hexId);
-				hexSetting.store(out, "the hexID to auth into login");
-			}
+	public static void saveHexid(int serverId, String hexId) {
+		Path hexIdFilePath = Configuration.getCustomOrDefaultPath(HexIdConfiguration.FILENAME);
+
+		hexId().setProperty(HexIdConfiguration.SERVERID_KEY, String.valueOf(serverId));
+		hexId().setProperty(HexIdConfiguration.HEXID_KEY, hexId);
+
+		try (OutputStream out = Files.newOutputStream(hexIdFilePath, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING)) {
+			hexId().store(out, "the hexID to auth into login");
+			LOG.info("Saved {}.", hexIdFilePath);
 		} catch (Exception ex) {
-			LOG.warn("Failed to save hex Id to {} file.", fileName, ex);
+			LOG.warn("Failed to save {}.", hexIdFilePath, ex);
 		}
 	}
 	

+ 35 - 1
src/main/java/com/l2jserver/gameserver/config/Configuration.java

@@ -1,5 +1,5 @@
 /*
- * Copyright © 2004-2021 L2J Server
+ * Copyright © 2004-2022 L2J Server
  *
  * This file is part of L2J Server.
  *
@@ -18,8 +18,12 @@
  */
 package com.l2jserver.gameserver.config;
 
+import java.nio.file.Path;
+
 import org.aeonbits.owner.ConfigFactory;
 
+import com.l2jserver.gameserver.GameServer;
+
 /**
  * Configuration.
  * @author Zoey76
@@ -27,6 +31,10 @@ import org.aeonbits.owner.ConfigFactory;
  */
 public class Configuration {
 	
+	public static final String DEFAULT_PATH = "config/";
+	
+	public static final String CUSTOM_SUBPATH = "custom/game/config/";
+	
 	public static final String EOL = System.lineSeparator();
 	
 	private static final IPConfigData ipConfigData = new IPConfigData();
@@ -218,4 +226,30 @@ public class Configuration {
 	public static DiscordConfiguration discord() {
 		return discord;
 	}
+	
+	public static String getDefaultPath(String filename) {
+		return DEFAULT_PATH + filename;
+	}
+	
+	public static String getCustomSubpath(String filename) {
+		return CUSTOM_SUBPATH + filename;
+	}
+	
+	/**
+	 * Returns either the custom or default path of the config file.
+	 * <br><br>
+	 * When the L2J_HOME variable is defined, the custom path to the config
+	 * file is returned. If not, the default path to the config file is
+	 * returned.
+	 * @param filename the filename
+	 * @return either the custom or default path to the config file
+	 */
+	public static Path getCustomOrDefaultPath(String filename) {
+		String l2jHome = GameServer.getL2jHomeVariable();
+		if (l2jHome == null) {
+			return Path.of(getDefaultPath(filename));
+		}
+
+		return Path.of(l2jHome, getCustomSubpath(filename));
+	}
 }

+ 12 - 7
src/main/java/com/l2jserver/gameserver/config/HexIdConfiguration.java

@@ -1,5 +1,5 @@
 /*
- * Copyright © 2004-2021 L2J Server
+ * Copyright © 2004-2022 L2J Server
  *
  * This file is part of L2J Server.
  *
@@ -24,6 +24,7 @@ import static org.aeonbits.owner.Config.LoadType.MERGE;
 
 import java.math.BigInteger;
 
+import org.aeonbits.owner.Accessible;
 import org.aeonbits.owner.Config.HotReload;
 import org.aeonbits.owner.Config.LoadPolicy;
 import org.aeonbits.owner.Config.Sources;
@@ -38,18 +39,22 @@ import com.l2jserver.gameserver.config.converter.HexIdConverter;
  * @version 2.6.1.0
  */
 @Sources({
-	"file:${L2J_HOME}/custom/game/config/hexid.txt",
-	"file:./config/hexid.txt",
-	"classpath:config/hexid.txt"
+	"file:${L2J_HOME}/" + Configuration.CUSTOM_SUBPATH + HexIdConfiguration.FILENAME,
+	"file:" + Configuration.DEFAULT_PATH + HexIdConfiguration.FILENAME,
+	"classpath:" + Configuration.DEFAULT_PATH + HexIdConfiguration.FILENAME
 })
 @LoadPolicy(MERGE)
 @HotReload(value = 20, unit = MINUTES, type = ASYNC)
-public interface HexIdConfiguration extends Mutable, Reloadable {
+public interface HexIdConfiguration extends Mutable, Reloadable, Accessible {
+	public static final String FILENAME = "hexid.txt";
 	
-	@Key("ServerID")
+	public static final String SERVERID_KEY = "ServerID";
+	public static final String HEXID_KEY = "HexID";
+	
+	@Key(SERVERID_KEY)
 	Integer getServerID();
 	
-	@Key("HexID")
+	@Key(HEXID_KEY)
 	@ConverterClass(HexIdConverter.class)
 	BigInteger getHexID();
 }