-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
132 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
122 changes: 122 additions & 0 deletions
122
src/main/java/ovh/maddie480/everest/updatechecker/ModAliasLister.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
package ovh.maddie480.everest.updatechecker; | ||
|
||
import org.apache.commons.io.FileUtils; | ||
import org.apache.commons.io.IOUtils; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.io.*; | ||
import java.nio.file.Files; | ||
import java.nio.file.Paths; | ||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.zip.ZipEntry; | ||
import java.util.zip.ZipFile; | ||
|
||
import static ovh.maddie480.everest.updatechecker.DatabaseUpdater.checkZipSignature; | ||
|
||
public class ModAliasLister { | ||
private static final Logger log = LoggerFactory.getLogger(ModAliasLister.class); | ||
|
||
static void updateModAliasList() throws IOException { | ||
Map<String, Map<String, Object>> oldAliasList; | ||
try (InputStream is = Files.newInputStream(Paths.get("uploads/modaliases.yaml"))) { | ||
oldAliasList = YamlUtil.load(is); | ||
} | ||
|
||
Map<String, Map<String, Object>> everestUpdate; | ||
try (InputStream is = Files.newInputStream(Paths.get("uploads/everestupdate.yaml"))) { | ||
everestUpdate = YamlUtil.load(is); | ||
} | ||
|
||
Map<String, Map<String, Object>> newAliasList = new HashMap<>(); | ||
|
||
// go across every entry in everest_update.yaml. | ||
for (Map.Entry<String, Map<String, Object>> mod : everestUpdate.entrySet()) { | ||
String name = mod.getKey(); | ||
String url = (String) mod.getValue().get(Main.serverConfig.mainServerIsMirror ? "MirrorURL" : "URL"); | ||
|
||
if (oldAliasList.containsKey(name) && oldAliasList.get(name).get("URL").toString().equals(url)) { | ||
// we already have that mod! | ||
newAliasList.put(name, oldAliasList.get(name)); | ||
} else { | ||
// download file | ||
ConnectionUtils.runWithRetry(() -> { | ||
try (OutputStream os = new BufferedOutputStream(Files.newOutputStream(Paths.get("mod-aliaslist.zip")))) { | ||
IOUtils.copy(new BufferedInputStream(ConnectionUtils.openStreamWithTimeout(url)), os); | ||
return null; // to fulfill this stupid method signature | ||
} | ||
}); | ||
|
||
// check that its size makes sense | ||
long actualSize = new File("mod-aliaslist.zip").length(); | ||
if (((int) mod.getValue().get("Size")) != actualSize) { | ||
FileUtils.forceDelete(new File("mod-aliaslist.zip")); | ||
throw new IOException("The announced file size (" + mod.getValue().get("Size") + ") does not match what we got (" + actualSize + ")" + | ||
" for file " + url); | ||
} | ||
|
||
// read its everest.yaml and collect the values of AlsoKnownAs | ||
List<String> aliasList = Collections.emptyList(); | ||
|
||
try (ZipFile zipFile = ZipFileWithAutoEncoding.open("mod-aliaslist.zip")) { | ||
checkZipSignature(new File("mod-aliaslist.zip").toPath()); | ||
|
||
ZipEntry everestYaml = zipFile.getEntry("everest.yaml"); | ||
if (everestYaml == null) { | ||
everestYaml = zipFile.getEntry("everest.yml"); | ||
} | ||
|
||
List<Map<String, Object>> everestYamlContents; | ||
try (InputStream is = zipFile.getInputStream(everestYaml)) { | ||
everestYamlContents = YamlUtil.loadNoFloats(is); | ||
} | ||
|
||
Map<String, Object> matchingYamlEntry = null; | ||
|
||
for (Map<String, Object> yamlEntry : everestYamlContents) { | ||
if (name.equals(yamlEntry.get("Name"))) { | ||
matchingYamlEntry = yamlEntry; | ||
break; | ||
} | ||
} | ||
|
||
if (matchingYamlEntry == null) { | ||
throw new IOException("Could not find matching entry that's supposed to be there since it's in everestupdate.yaml!"); | ||
} | ||
|
||
// instanceof List<String> isn't a thing, but we can check if all elements are Strings instead! | ||
if (matchingYamlEntry.get("AlsoKnownAs") instanceof List aliases | ||
&& aliases.stream().allMatch(e -> e instanceof String)) { | ||
|
||
aliasList = (List<String>) aliases; | ||
} | ||
|
||
log.info("Found aliases for {}: {}.", name, aliasList); | ||
final List<String> aliases = aliasList; | ||
EventListener.handle(listener -> listener.scannedModAliases(name, aliases)); | ||
} catch (Exception e) { | ||
// if a file cannot be read as a zip, no need to worry about it. | ||
// we will just write an empty array. | ||
log.warn("Could not read aliases from {}", name, e); | ||
EventListener.handle(listener -> listener.modAliasListScanException(name, e)); | ||
} | ||
|
||
// save the entry we just got. | ||
Map<String, Object> aliasEntry = new HashMap<>(); | ||
aliasEntry.put("URL", url); | ||
aliasEntry.put("AlsoKnownAs", aliasList); | ||
newAliasList.put(name, aliasEntry); | ||
|
||
FileUtils.forceDelete(new File("mod-aliaslist.zip")); | ||
} | ||
} | ||
|
||
// write it out! | ||
try (OutputStream os = new FileOutputStream("uploads/modaliases.yaml")) { | ||
YamlUtil.dump(newAliasList, os); | ||
} | ||
} | ||
} |