Skip to content

Commit 7eea3bc

Browse files
committed
🐛 Fix Pf2e image paths
1 parent f1fe6fe commit 7eea3bc

File tree

9 files changed

+45
-23
lines changed

9 files changed

+45
-23
lines changed

src/main/java/dev/ebullient/convert/RpgDataConvertCli.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@ public Integer call() {
200200
boolean isTools = tui.readToolsDir(input, index::importTree);
201201
if (isTools) { // we found the tools directory
202202
toolsPath = input;
203+
TtrpgConfig.setToolsPath(toolsPath);
203204
} else {
204205
// this is some other directory full of json
205206
allOk &= tui.readDirectory("", input, index::importTree);

src/main/java/dev/ebullient/convert/config/TtrpgConfig.java

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,23 @@
2525

2626
public class TtrpgConfig {
2727

28+
public static final String DEFAULT_IMG_ROOT = "imgRoot";
29+
2830
static final Map<Datasource, DatasourceConfig> globalConfig = new HashMap<>();
2931
static final Map<Datasource, CompendiumConfig> userConfig = new HashMap<>();
3032
static final Set<String> missingSourceName = new HashSet<>();
3133

3234
private static Datasource datasource = Datasource.tools5e;
3335
private static Tui tui;
3436
private static ImageRoot internalImageRoot;
37+
private static Path toolsPath;
3538

3639
public static void init(Tui tui, Datasource datasource) {
3740
userConfig.clear();
38-
internalImageRoot = null;
3941
TtrpgConfig.tui = tui;
4042
TtrpgConfig.datasource = datasource;
43+
TtrpgConfig.internalImageRoot = null;
44+
TtrpgConfig.toolsPath = null;
4145
readSystemConfig();
4246
}
4347

@@ -53,6 +57,10 @@ public static String getConstant(String key) {
5357
return activeDSConfig().constants.get(key);
5458
}
5559

60+
public static void setToolsPath(Path toolsPath) {
61+
TtrpgConfig.toolsPath = toolsPath;
62+
}
63+
5664
private static DatasourceConfig activeDSConfig() {
5765
return globalConfig.computeIfAbsent(datasource, (k) -> new DatasourceConfig());
5866
}
@@ -111,6 +119,7 @@ private ImageRoot(String cfgRoot, ImageOptions options) {
111119
} else {
112120
if (cfgRoot.startsWith("http") || cfgRoot.startsWith("file:")) {
113121
this.internalImageRoot = endWithSlash(cfgRoot);
122+
this.copyInternal = options.copyInternal();
114123
} else {
115124
Path imgPath = Path.of("").resolve(cfgRoot).normalize().toAbsolutePath();
116125
if (!imgPath.toFile().exists()) {
@@ -120,8 +129,8 @@ private ImageRoot(String cfgRoot, ImageOptions options) {
120129
return;
121130
}
122131
this.internalImageRoot = endWithSlash(imgPath.toString());
132+
this.copyInternal = true;
123133
}
124-
this.copyInternal = options.copyInternal();
125134
Tui.instance().printlnf("[ 🖼️ OK] Using %s as the source for remote images (copyInternal=%s)",
126135
this.internalImageRoot, this.copyInternal);
127136
}
@@ -139,13 +148,6 @@ public boolean copyExternalToVault() {
139148
return copyExternal;
140149
}
141150

142-
private String endWithSlash(String path) {
143-
if (path == null) {
144-
return "";
145-
}
146-
return path.endsWith("/") ? path : path + "/";
147-
}
148-
149151
public String getFallbackPath(String key) {
150152
return fallbackPaths.getOrDefault(key, key);
151153
}
@@ -157,13 +159,27 @@ public static ImageRoot internalImageRoot() {
157159
ImageOptions options = getConfig().imageOptions();
158160
String cfg = options.internalRoot;
159161
if (cfg == null) {
160-
cfg = activeDSConfig().constants.get("5etools-img");
162+
String imgRoot = activeDSConfig().constants.get(DEFAULT_IMG_ROOT);
163+
if (imgRoot == null && toolsPath != null && datasource == Datasource.toolsPf2e) {
164+
cfg = toolsPath.resolve("..").normalize().toString();
165+
} else if (imgRoot == null) {
166+
cfg = "";
167+
} else {
168+
cfg = imgRoot;
169+
}
161170
}
162171
internalImageRoot = root = new ImageRoot(cfg, options);
163172
}
164173
return root;
165174
}
166175

176+
private static String endWithSlash(String path) {
177+
if (path == null) {
178+
return "";
179+
}
180+
return path.endsWith("/") ? path : path + "/";
181+
}
182+
167183
public static JsonNode readIndex(String key) {
168184
String file = activeDSConfig().indexes.get(key);
169185
Optional<Path> root = file == null ? Optional.empty() : tui.resolvePath(Path.of(file));

src/main/java/dev/ebullient/convert/qute/ImageRef.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,8 @@ public ImageRef build() {
233233
if (sourceUrl.startsWith("http") || sourceUrl.startsWith("file")) {
234234
sourceUrl = sourceUrl.replaceAll("^(https?):/+", "$1://");
235235
copyToVault = imageRoot.copyExternalToVault();
236+
} else if (sourceUrl.startsWith("stream/")) {
237+
copyToVault = true;
236238
} else if (!sourceUrl.startsWith("file:/")) {
237239
sourceUrl = imageRoot.getRootPath() + sourceUrl;
238240
copyToVault = imageRoot.copyInternalToVault();

src/main/java/dev/ebullient/convert/tools/dnd5e/JsonTextReplacement.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ default String _replaceTokenText(String input, boolean nested) {
250250
}
251251

252252
String[] parts = match.group(1).split("\\|");
253-
String imgRepo = TtrpgConfig.getConstant("5etools-img");
253+
String imgRepo = TtrpgConfig.getConstant(TtrpgConfig.DEFAULT_IMG_ROOT);
254254
String url = ImageRef.Builder.fixUrl(imgRepo + (imgRepo.endsWith("/") ? "" : "/") + parts[1]);
255255

256256
return String.format("[%s](%s)", parts[0], url);

src/main/resources/convertData.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@
330330
]
331331
},
332332
"constants": {
333-
"5etools-img": "https://raw.githubusercontent.com/5etools-mirror-2/5etools-img/main/"
333+
"imgRoot": "https://raw.githubusercontent.com/5etools-mirror-2/5etools-img/main/"
334334
},
335335
"markerFiles": [
336336
"bestiary/bestiary-mm.json",

src/test/java/dev/ebullient/convert/Pf2eDataConvertTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public static void setupDir(String root) {
3939

4040
@Test
4141
void testLiveData_Pf2eAllSources(QuarkusMainLauncher launcher) {
42-
if (TestUtils.TOOLS_PATH_PF2E.toFile().exists()) {
42+
if (TestUtils.PATH_PF2E_TOOLS_DATA.toFile().exists()) {
4343
// All, I mean it. Really for real.. ALL.
4444
final Path allIndex = testOutput.resolve("all-index");
4545
TestUtils.deleteDir(allIndex);
@@ -48,13 +48,13 @@ void testLiveData_Pf2eAllSources(QuarkusMainLauncher launcher) {
4848
"-s", "ALL",
4949
"-o", allIndex.toString(),
5050
"-g", "pf2e",
51-
TestUtils.TOOLS_PATH_PF2E.toString()));
51+
TestUtils.PATH_PF2E_TOOLS_DATA.toString()));
5252

53-
args.addAll(TestUtils.getFilesFrom(TestUtils.TOOLS_PATH_PF2E.resolve("adventure"))
53+
args.addAll(TestUtils.getFilesFrom(TestUtils.PATH_PF2E_TOOLS_DATA.resolve("adventure"))
5454
.stream()
5555
.filter(x -> !x.endsWith("-id.json"))
5656
.toList());
57-
args.addAll(TestUtils.getFilesFrom(TestUtils.TOOLS_PATH_PF2E.resolve("book")));
57+
args.addAll(TestUtils.getFilesFrom(TestUtils.PATH_PF2E_TOOLS_DATA.resolve("book")));
5858

5959
LaunchResult result = launcher.launch(args.toArray(new String[0]));
6060
assertThat(result.exitCode())

src/test/java/dev/ebullient/convert/TestUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public class TestUtils {
4141

4242
public final static Path PATH_5E_HOMEBREW = PROJECT_PATH.resolve("sources/5e-homebrew");
4343
public final static Path PATH_5E_UA = PROJECT_PATH.resolve("sources/5e-unearthed-arcana");
44-
public final static Path TOOLS_PATH_PF2E = PROJECT_PATH.resolve("sources/Pf2eTools/data");
44+
public final static Path PATH_PF2E_TOOLS_DATA = PROJECT_PATH.resolve("sources/Pf2eTools/data");
4545

4646
static String GENERATED_DOCS = PROJECT_PATH.resolve("docs/templates").normalize().toAbsolutePath().toString();
4747

src/test/java/dev/ebullient/convert/tools/dnd5e/CommonDataTests.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ public CommonDataTests(TestInput variant, Path toolsData) throws Exception {
6060
tui.setTemplates(templates);
6161

6262
TtrpgConfig.init(tui, Datasource.tools5e);
63+
TtrpgConfig.setToolsPath(TestUtils.PATH_5E_TOOLS_DATA);
6364

6465
configurator = new Configurator(tui);
6566
if (imgPresent) {

src/test/java/dev/ebullient/convert/tools/pf2e/CommonDataTests.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,14 @@ public CommonDataTests(TestInput variant) throws Exception {
5252
outputPath.toFile().mkdirs();
5353

5454
TtrpgConfig.init(tui, Datasource.toolsPf2e);
55+
TtrpgConfig.setToolsPath(TestUtils.PATH_PF2E_TOOLS_DATA);
56+
5557
configurator = new Configurator(tui);
5658

5759
index = new Pf2eIndex(TtrpgConfig.getConfig());
5860
templates.setCustomTemplates(TtrpgConfig.getConfig());
5961

60-
if (TestUtils.TOOLS_PATH_PF2E.toFile().exists()) {
62+
if (TestUtils.PATH_PF2E_TOOLS_DATA.toFile().exists()) {
6163
switch (variant) {
6264
case all:
6365
configurator.addSources(List.of("*"));
@@ -72,9 +74,9 @@ public CommonDataTests(TestInput variant) throws Exception {
7274

7375
for (String x : List.of("books.json",
7476
"book/book-crb.json", "book/book-gmg.json")) {
75-
tui.readFile(TestUtils.TOOLS_PATH_PF2E.resolve(x), TtrpgConfig.getFixes(x), index::importTree);
77+
tui.readFile(TestUtils.PATH_PF2E_TOOLS_DATA.resolve(x), TtrpgConfig.getFixes(x), index::importTree);
7678
}
77-
tui.readToolsDir(TestUtils.TOOLS_PATH_PF2E, index::importTree);
79+
tui.readToolsDir(TestUtils.PATH_PF2E_TOOLS_DATA, index::importTree);
7880
index.prepare();
7981
}
8082
}
@@ -88,7 +90,7 @@ public void cleanup() {
8890
}
8991

9092
public void testDataIndex_pf2e() throws Exception {
91-
if (TestUtils.TOOLS_PATH_PF2E.toFile().exists()) {
93+
if (TestUtils.PATH_PF2E_TOOLS_DATA.toFile().exists()) {
9294
Path full = outputPath.resolve("allIndex.json");
9395
index.writeFullIndex(full);
9496

@@ -120,7 +122,7 @@ public void testNotes_p2fe() throws Exception {
120122
Path rulesDir = outputPath.resolve(index.rulesFilePath());
121123
Path compendiumDir = outputPath.resolve(index.compendiumFilePath());
122124

123-
if (TestUtils.TOOLS_PATH_PF2E.toFile().exists()) {
125+
if (TestUtils.PATH_PF2E_TOOLS_DATA.toFile().exists()) {
124126
MarkdownWriter writer = new MarkdownWriter(outputPath, templates, tui);
125127
index.markdownConverter(writer)
126128
.writeNotesAndTables()
@@ -146,7 +148,7 @@ Map<Pf2eIndexType, Path> generateNotesForType(List<Pf2eIndexType> types) {
146148
paths.add(p);
147149
});
148150

149-
if (TestUtils.TOOLS_PATH_PF2E.toFile().exists()) {
151+
if (TestUtils.PATH_PF2E_TOOLS_DATA.toFile().exists()) {
150152
paths.forEach(p -> TestUtils.deleteDir(p));
151153

152154
MarkdownWriter writer = new MarkdownWriter(outputPath, templates, tui);

0 commit comments

Comments
 (0)