11package gg .essential .ad ;
22
33import gg .essential .ad .data .AdData ;
4+ import gg .essential .ad .data .ModalData ;
45import gg .essential .ad .loader .EssentialAdLoader ;
56import org .apache .commons .io .IOUtils ;
67
78import java .io .BufferedReader ;
9+ import java .io .IOException ;
810import java .io .InputStream ;
911import java .net .HttpURLConnection ;
1012import java .net .URI ;
1113import java .nio .charset .StandardCharsets ;
1214import java .nio .file .Files ;
1315import java .nio .file .Path ;
16+ import java .util .Base64 ;
1417import java .util .concurrent .CompletableFuture ;
1518
1619public class EssentialAPI {
@@ -23,15 +26,15 @@ public class EssentialAPI {
2326
2427 private static final String USER_AGENT = "EssentialAd/" + EssentialAdLoader .OUR_VERSION + " (" + EssentialAdLoader .OUR_PKG + ")" ;
2528
26- private static final Path OVERRIDE_FILE = AdConfig .CONFIG_FOLDER .resolve ("data.override.json" );
29+ private static final Path API_OVERRIDE_FILE = AdConfig .CONFIG_FOLDER .resolve ("data.override.json" );
30+ private static final Path MODAL_OVERRIDE_FILE = AdConfig .CONFIG_FOLDER .resolve ("mod-partner-modal-metadata.json" );
2731
2832 public static CompletableFuture <AdData > fetchAdData () {
2933 CompletableFuture <AdData > future = new CompletableFuture <>();
3034 CompletableFuture .runAsync (() -> {
3135 try {
32- // todo: add an override file just for the modal, and that behaves like that configuration repo (images as separate files)
33- if (Files .exists (OVERRIDE_FILE )) {
34- try (BufferedReader reader = Files .newBufferedReader (OVERRIDE_FILE )) {
36+ if (Files .exists (API_OVERRIDE_FILE )) {
37+ try (BufferedReader reader = Files .newBufferedReader (API_OVERRIDE_FILE )) {
3538 AdData data = EssentialAd .GSON .fromJson (reader , AdData .class );
3639 future .complete (data );
3740 return ;
@@ -51,6 +54,26 @@ public static CompletableFuture<AdData> fetchAdData() {
5154 }
5255
5356 AdData data = EssentialAd .GSON .fromJson (response , AdData .class );
57+
58+ if (Files .exists (MODAL_OVERRIDE_FILE )) {
59+ try (BufferedReader reader = Files .newBufferedReader (MODAL_OVERRIDE_FILE )) {
60+ ModalData modalData = EssentialAd .GSON .fromJson (reader , ModalData .class );
61+ // Replace image paths with base64 representation
62+ for (ModalData .Feature feature : modalData .getFeatures ()) {
63+ Path iconPath = AdConfig .CONFIG_FOLDER .resolve (feature .getIcon ());
64+ try {
65+ byte [] bytes = Files .readAllBytes (iconPath );
66+ String base64 = Base64 .getEncoder ().encodeToString (bytes );
67+ feature .setIcon (base64 );
68+ } catch (IOException e ) {
69+ EssentialAd .LOGGER .error ("Failed to load icon {}" , iconPath , e );
70+ }
71+ }
72+
73+ data = new AdData (modalData , data .getPartneredMods ());
74+ }
75+ }
76+
5477 future .complete (data );
5578 } catch (Exception e ) {
5679 EssentialAd .LOGGER .error ("Failed to fetch modal data" , e );
0 commit comments