Skip to content

Commit

Permalink
Put IDs in plain text with a \0 separator, and fix up some issues wit…
Browse files Browse the repository at this point in the history
…h not deleting files.
  • Loading branch information
ipsi committed Sep 12, 2022
1 parent f559779 commit 5a7d70d
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 28 deletions.
2 changes: 1 addition & 1 deletion src/main/java/name/ipsi/project/fwbp/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ static void writeIds() throws IOException {
var outputFile = Path.of("src", "main", "resources", "id-list.txt");
if (Files.exists(outputFile)) {
var fileContents = ids.entrySet().stream()
.map(e -> String.format("%s,%s", e.getKey(), e.getValue()))
.map(e -> String.format("%s\0%s", e.getKey(), e.getValue()))
.collect(Collectors.joining("\n"));
Files.writeString(outputFile, fileContents, StandardCharsets.UTF_8, StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.WRITE);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -625,7 +625,7 @@ private void processRites() {
}
}

entries.add(new Rite(rite.name(), rite.type(), rite.level(), rite.type().rollData(), description, system));
entries.add(new Rite(fixText(rite.name()), rite.type(), rite.level(), rite.type().rollData(), description, system));
}
}

Expand Down Expand Up @@ -657,7 +657,7 @@ private void processFetishes() {
}

entries.add(new Fetish(
fetish.name(),
fixText(fetish.name()),
fetish.level(),
fetish.gnosis(),
description
Expand All @@ -678,14 +678,21 @@ private void processTalens() {
}

entries.add(new Talen(
talen.name(),
fixText(talen.name()),
talen.gnosis(),
description
));
}
}

private record Data(String name, int cost, int max, boolean merit) {}
private record Data(String name, int cost, int max, boolean merit) {
private Data(String name, int cost, int max, boolean merit) {
this.name = fixText(name);
this.cost = cost;
this.max = max;
this.merit = merit;
}
}

private void processMeritsAndFlaws() {
List<String> sectionHeaders = new ArrayList<>();
Expand Down
32 changes: 11 additions & 21 deletions src/main/java/name/ipsi/project/fwbp/foundry/core/FoundryUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.*;
import java.util.Collections;
import java.util.Map;
import java.util.Random;
import java.util.TreeMap;
import java.util.stream.Collectors;

public class FoundryUtils {
Expand All @@ -26,9 +27,9 @@ public class FoundryUtils {
}
try (var reader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8))) {
for (var line = reader.readLine(); line != null; line = reader.readLine()) {
var parts = line.split(",");
var parts = line.split("\0");
if (parts.length != 2) {
throw new RuntimeException("Expected ID file line [" + line + "] to match format <id>,<hash>");
throw new RuntimeException("Expected ID file line [" + line + "] to match format <key>\0<id>");
}
ids.put(parts[0], parts[1]);
}
Expand All @@ -38,31 +39,20 @@ public class FoundryUtils {
}

public static String generateId(String group, String name) {
var toHash = String.format("%s:%s", group, name);
var hash = generateHash(toHash);
if (ids.containsKey(hash)) {
return ids.get(hash);
var key = String.format("%s:%s", group, name);
if (ids.containsKey(key)) {
return ids.get(key);
} else {
log.warn("No existing ID found for [{}] - generating new", toHash);
log.warn("No existing ID found for [{}] - generating new", key);
var r = new Random();
var id = r.ints(16, 0, 36)
.mapToObj(i -> Integer.toString(i, 36))
.collect(Collectors.joining(""));
ids.put(hash, id);
ids.put(key, id);
return id;
}
}

private static String generateHash(String toHash) {
try {
var hasher = MessageDigest.getInstance("SHA512");
var hash = hasher.digest(toHash.getBytes(StandardCharsets.UTF_8));
return Base64.getEncoder().encodeToString(hash);
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
}

public static Map<String, String> getIds() {
return Collections.unmodifiableMap(ids);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,10 @@ private ModulePack createPack(String label, String packName, DocumentTypes type,
var packFile = outputPath.resolve(path);
log.trace("Creating pack {}, {}, {} at {}", label, packName, type, packFile);

Files.createFile(packFile);
if (!Files.exists(packFile)) {
Files.createFile(packFile);
}

foundryDocuments.forEach(doc -> {
try {
log.trace("Writing document to pack file");
Expand Down
Binary file modified src/main/resources/id-list.txt
Binary file not shown.
2 changes: 1 addition & 1 deletion src/test/java/name/ipsi/project/fwbp/MainTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IO
expectedFiles.put(Path.of("modules", "wod-werewolf-20-core", "module.json"), "96947f364f029a8b6eb0629bdb8d167829254ae3215775ccb34b2f2613ff56ca8dc3786193a970c41c01787d2746415ba59a572803285a5d3d53556b4fefb39a");
expectedFiles.put(Path.of("modules", "wod-werewolf-20-core", "w20.css"), "2debd674fb547d898a6cfc8addefa1e22cacde66938df545de2937c23715b39ecc4185818845ad2318c98dba0514e476d23377d80ba9fd450be4e0addf550cad");
expectedFiles.put(Path.of("modules", "wod-werewolf-20-core", "w20.js"), "01d4f461c7e84a173e206815cd9e48ceef1606c954c189f85e0d293657ac9393beb42faab59711f374733ab3b6c5bca6cebf63aed5d767c574e8e6e0d325d363");
expectedFiles.put(Path.of("modules", "wod-werewolf-20-core", "packs", "w20.db"), "432758b98d4b1327031629c374b421c73ef643c12137315a5759454192bdd8f09d4b1d50be7cb5b3863c67e958edb85a2d45b99ac6beaf57b4b97ec905ecdcfb");
expectedFiles.put(Path.of("modules", "wod-werewolf-20-core", "packs", "w20.db"), "bac277ffd66f179cf422320c84ce686a25b5960ff5771f0499f58854979ba77fee22cc73bde97a11a43df53de641f6cdf5a7485989eb5c9b03aa12fdccc05414");
expectedFiles.put(Path.of("modules", "wod-werewolf-20-core", "images", "adventure-cover.jpeg"), "17d71cdd62f4c930b803fbcf1fab02a3d0244eb7fa1d4359ab088381b1288f1bb191117ec60d3075528732ff9ce4888545e6b66b0d681b00b68b79a214daee1c");
expectedFiles.put(Path.of("modules", "wod-werewolf-20-core", "images", "black-furies-splash.jpeg"), "0c2f71fba6238336509d84672af71bee9344c477e0696ddd2f4dcf7d53193a1df4726f59f78135d653411f9e44dddbb08f54c65cb552a3ae3ce358dc1e2a4dea");
expectedFiles.put(Path.of("modules", "wod-werewolf-20-core", "images", "black-spiral-dancers-splash.jpeg"), "10c193572ee8f878ce800f3e4bd6db12e3971714f9f69bda1ca15ee8edb64b9540f6638aacefdafdeef07d7027dc9c6f44d0423ca9c75caa492bcc752feaa372");
Expand Down

0 comments on commit 5a7d70d

Please sign in to comment.