CardsXmlParser bugfix, and fs

This commit is contained in:
Michael Wain 2024-06-15 19:13:20 +03:00
parent 362655c389
commit 2b40efda5f
4 changed files with 62 additions and 8 deletions

View File

@ -1,6 +1,7 @@
package com.alterdekim.hearthhack.component;
import com.alterdekim.hearthhack.config.DBFConfig;
import com.alterdekim.hearthhack.config.FS;
import com.alterdekim.hearthhack.config.ServerConfig;
import com.alterdekim.hearthhack.parser.CardsXmlParser;
import com.alterdekim.hearthhack.parser.DBFParser;
@ -10,8 +11,10 @@ import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;
import java.nio.file.Files;
import java.nio.file.Path;
import java.io.File;
import java.io.IOException;
import java.nio.file.*;
import java.util.Map;
@Slf4j
@Component
@ -26,7 +29,6 @@ public class StartupListener {
@EventListener
public void onApplicationEvent(ContextRefreshedEvent event) {
try {
dbfConfig.setCards(DBFParser.parseCards(serverConfig.getDbfPath()));
dbfConfig.setClientConfig(
new String(
Files.readAllBytes(
@ -36,9 +38,48 @@ public class StartupListener {
)
)
);
log.info("CardsXmlMap: {}", CardsXmlParser.parse(serverConfig.getCardsXmlPath()));
if( serverConfig.getWorkDir() == null ) serverConfig.setWorkDir(new File(".").getCanonicalPath());
makeDir(FS.xmlDir);
makeDir(FS.pluginsDir);
makeDir(FS.dbDir);
// TODO: make a auto-mkdir using reflection of interface
if( !Path.of(serverConfig.getWorkDir() + File.separator + FS.dbfDir).toFile().exists() ) initDbf();
if( !Path.of(serverConfig.getWorkDir() + File.separator + FS.dataDir).toFile().exists() ) initXml();
dbfConfig.setCards(DBFParser.parseCards(serverConfig.getWorkDir() + File.separator + FS.dbfDir));
} catch (Exception e) {
log.error(e.getMessage());
}
}
private void initDbf() throws Exception {
makeDir(FS.dbfDir);
Files.walk(Path.of(serverConfig.getDbfPath()))
.map(Path::toFile)
.filter(f -> f.isFile() && f.getName().endsWith("xml"))
.forEach(f -> {
try {
Files.copy(f.toPath(), Path.of(serverConfig.getWorkDir() + File.separator + FS.dbfDir + File.separator + f.getName()));
} catch (IOException e) {
log.error(e.getMessage());
}
});
}
private void initXml() throws IOException {
makeDir(FS.dataDir);
Map<String, String> m = CardsXmlParser.parse(serverConfig.getCardsXmlPath());
m.keySet().forEach(k -> {
try {
Path p = Path.of(serverConfig.getWorkDir()+File.separator+FS.dataDir+File.separator+k+".xml");
Files.createFile(p);
Files.write(p, m.get(k).getBytes(), StandardOpenOption.WRITE);
} catch (IOException e) {
log.error(e.getMessage());
}
});
}
private boolean makeDir(String dirName) {
return Path.of(serverConfig.getWorkDir() + File.separator + dirName).toFile().mkdirs();
}
}

View File

@ -0,0 +1,12 @@
package com.alterdekim.hearthhack.config;
public interface FS {
String xmlDir = "xml";
String pluginsDir = "plugins";
String dbDir = "db";
String dbfDir = xmlDir + "/dbf";
String dataDir = xmlDir + "/data";
String propertiesFilename = "server.properties";
String eulaFilename = "eula.txt";
String internalSettingsFilename = "flame.yml";
}

View File

@ -13,4 +13,5 @@ public class ServerConfig {
@Value("${hearthhack.game_port}") private Integer gamePort;
@Value("${hearthhack.host}") private String host;
@Value("${hearthhack.cards_xml_path}") private String cardsXmlPath;
@Value("${hearthhack.work_dir}") private String workDir;
}

View File

@ -1,6 +1,5 @@
package com.alterdekim.hearthhack.parser;
import com.alterdekim.hearthhack.util.Util;
import java.io.IOException;
import java.nio.file.Files;
@ -9,11 +8,12 @@ import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
public class CardsXmlParser {
private static final byte[] cardDef = Util.hexStringToByteArray("3C43617264446566733E");
private static final byte[] cardDef = "<CardDefs>".getBytes();
private static final byte[] cardDefEnd = Util.hexStringToByteArray("3C2F43617264446566733E");
private static final byte[] cardDefEnd = "</CardDefs>".getBytes();
public static Map<String, String> parse(String path) throws IOException {
Map<String, String> m = new HashMap<>();
@ -24,7 +24,7 @@ public class CardsXmlParser {
a = Arrays.copyOfRange(b, i-8, i-4);
int f = findFirst(b, i);
m.put(new String(a), new String(Arrays.copyOfRange(b, i, f)));
i += f;
i = f;
}
}
return m;