Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 44 additions & 36 deletions src/main/java/dev/koifysh/archipelago/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -218,46 +218,53 @@ protected void loadDataPackage() {
synchronized (Client.class){
File directoryPath = dataPackageLocation.toFile();

if(!directoryPath.exists())
{
boolean success = directoryPath.mkdirs();
if(success){
LOGGER.info("DataPackage directory didn't exist. Starting from a new one.");
} else{
LOGGER.severe("Failed to make directories for datapackage cache.");
}
return;
}

//ensure the path to the cache exists
if(directoryPath.exists() && directoryPath.isDirectory()){
//loop through all Archipelago cache folders to find valid data package files
Map<String,File> localGamesList = new HashMap<String,File>();
if(!directoryPath.isDirectory()) {
return;
}
//loop through all Archipelago cache folders to find valid data package files
Map<String,File> localGamesList = new HashMap<String,File>();

for(File gameDir : directoryPath.listFiles()){
if(gameDir.isDirectory()){
localGamesList.put(gameDir.getName(), gameDir);
}
for(File gameDir : directoryPath.listFiles()){
if(gameDir.isDirectory()){
localGamesList.put(gameDir.getName(), gameDir);
}
}

if(localGamesList.isEmpty()){
//cache doesn't exist. Create the filepath
boolean success = directoryPath.mkdirs();
if(success){
LOGGER.info("DataPackage directory didn't exist. Starting from a new one.");
} else{
LOGGER.severe("Failed to make directories for datapackage cache.");
}
return;
if(localGamesList.isEmpty()){
LOGGER.info("Datapackage is empty");
return;
}

for(String gameName : games) {
String safeName = Utils.getFileSafeName(gameName);
File dir = localGamesList.get(safeName);

if(null == dir){
continue;
}

for(String gameName : games) {
File dir = localGamesList.get(gameName);

if(null == dir){
continue;
}

//check all checksums
for(File version : dir.listFiles()){
String versionStr = versions.get(gameName);
if(versionStr != null && versionStr.equals(version.getName())) {
try(FileReader reader = new FileReader(version)){
Game game = gson.fromJson(reader, Game.class);
dataPackage.update(gameName, game);
LOGGER.info("Read datapackage for Game: ".concat(gameName).concat(" Checksum: ").concat(version.getName()));
} catch (IOException e){
LOGGER.info("Failed to read a datapackage. Starting with a new one.");
}
//check all checksums
for(File version : dir.listFiles()){
String versionStr = versions.get(gameName);
if(versionStr != null && versionStr.equals(version.getName())) {
try(FileReader reader = new FileReader(version)){
Game game = gson.fromJson(reader, Game.class);
dataPackage.update(gameName, game);
LOGGER.info("Read datapackage for Game: ".concat(gameName).concat(" Checksum: ").concat(version.getName()));
} catch (IOException e){
LOGGER.info("Failed to read a datapackage. Starting with a new one.");
}
}
}
Expand All @@ -269,7 +276,8 @@ public void saveDataPackage() {
synchronized (Client.class){
//Loop through games to ensure we have folders for each of them in the cache
for(String gameName : games){
File gameFolder = dataPackageLocation.resolve(gameName).toFile();
String safeName = Utils.getFileSafeName(gameName);
File gameFolder = dataPackageLocation.resolve(safeName).toFile();
if(!gameFolder.exists()){
//game folder not found. Make it
gameFolder.mkdirs();
Expand All @@ -282,7 +290,7 @@ public void saveDataPackage() {
}

//if key is for this game
File filePath = dataPackageLocation.resolve(gameName).resolve(gameVersion).toFile();
File filePath = dataPackageLocation.resolve(safeName).resolve(gameVersion).toFile();

try (Writer writer = new FileWriter(filePath)){
//if game is in list of games, save it
Expand Down
35 changes: 35 additions & 0 deletions src/main/java/dev/koifysh/archipelago/Utils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package dev.koifysh.archipelago;

public class Utils {

public static String getFileSafeName(String text)
{
if(text == null)
{
return null;
}
StringBuilder sb = new StringBuilder();

for(int i = 0; i < text.length(); i++)
{
char c = text.charAt(i);
switch(c)
{
case '<':
case '>':
case ':':
case '"':
case '/':
case '\\':
case '|':
case '?':
case '*':
continue;
default:
sb.append(c);

}
}
return sb.toString();
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package dev.koifysh.archipelago.network;

public enum ConnectionResult {
Success,InvalidSlot, SlotAlreadyTaken, IncompatibleVersion, InvalidPassword
Success,InvalidSlot, SlotAlreadyTaken, IncompatibleVersion, InvalidPassword,InvalidGame
}
20 changes: 20 additions & 0 deletions src/test/java/dev/koifysh/archipelago/UtilsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package dev.koifysh.archipelago;

import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;

public class UtilsTest {

@Test
public void textIsSafe()
{
assertEquals("Slay the Spire", Utils.getFileSafeName("Slay the Spire"));
}

@Test
public void textIsBad()
{

assertEquals("Slay the Spire", Utils.getFileSafeName("Slay <>\"/?\\|:*the Spire"));
}
}