Skip to content

Commit

Permalink
Changed game config loading to use default file for WEB platform if a…
Browse files Browse the repository at this point in the history
…n existing preferences setting is not available. And changed GWT game loader to support ZIP files hosted on server.
  • Loading branch information
lanceewing committed Feb 25, 2024
1 parent 6ed48d5 commit 77d4790
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 28 deletions.
26 changes: 19 additions & 7 deletions core/src/main/java/com/agifans/agile/HomeScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,7 @@ public HomeScreen(Agile agile, DialogHandler dialogHandler) {
this.agile = agile;
this.dialogHandler = dialogHandler;

// Load the app meta data.
Json json = getJson();
//String appConfigJson = Gdx.files.internal("data/games.json").readString();
String appConfigJson =
json.prettyPrint(agile.getPreferences().getString(HOME_SCREEN_APP_LIST_PREF_NAME, DEFAULT_APP_CONFIG_JSON));
agile.getPreferences().putString(HOME_SCREEN_APP_LIST_PREF_NAME, appConfigJson);
AppConfig appConfig = json.fromJson(AppConfig.class, appConfigJson);
AppConfig appConfig = loadAppConfig();
appConfigMap = new TreeMap<String, AppConfigItem>();
for (AppConfigItem appConfigItem : appConfig.getApps()) {
appConfigMap.put(appConfigItem.getName(), appConfigItem);
Expand Down Expand Up @@ -146,6 +140,24 @@ public HomeScreen(Agile agile, DialogHandler dialogHandler) {
landscapeInputProcessor.addProcessor(this);
}

private AppConfig loadAppConfig() {
Json json = getJson();
String appConfigJson = null;
if (agile.getPreferences().contains(HOME_SCREEN_APP_LIST_PREF_NAME)) {
appConfigJson = json.prettyPrint(agile.getPreferences().getString(HOME_SCREEN_APP_LIST_PREF_NAME));
} else {
if (Gdx.app.getType().equals(ApplicationType.WebGL)) {
// First time use for web version, so load preconfigured file.
appConfigJson = Gdx.files.internal("data/games.json").readString();
} else {
// Desktop currently empty to begin with.
appConfigJson = DEFAULT_APP_CONFIG_JSON;
}
}
agile.getPreferences().putString(HOME_SCREEN_APP_LIST_PREF_NAME, appConfigJson);
return json.fromJson(AppConfig.class, appConfigJson);
}

private Texture createWhitePixelTexture() {
Pixmap pixmap = new Pixmap(1, 1, Pixmap.Format.RGBA8888);
pixmap.setColor(1,1,1,1);
Expand Down
46 changes: 25 additions & 21 deletions html/src/main/java/com/agifans/agile/gwt/GwtGameLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,30 +33,34 @@ public GwtGameLoader(PixelData pixelData) {

@Override
public void fetchGameFiles(String gameUri, Consumer<Map<String, byte[]>> gameFilesConsumer) {
//Map<String, byte[]> gameFileMap = new HashMap<>();

opfsGameFiles.readGameFilesData(gameUri, new GwtOpenFileResultsHandler() {
@Override
public void onFileResultsReady(GwtOpenFileResult[] openFileResultArray) {
if (openFileResultArray.length == 1) {
GwtOpenFileResult openFileResult = openFileResultArray[0];
Map<String, byte[]> gameFileMap = gameFileMapEncoder.decodeGameFileMap(openFileResult.getFileData());
gameFilesConsumer.accept(gameFileMap);
if (gameUri.toLowerCase().endsWith(".zip")) {
// Must be a ZIP URL for one of the embedded fan made games.
Map<String, byte[]> gameFileMap = new HashMap<>();
JSZip jsZip = JSZip.loadFile(gameUri);
JsArrayString files = jsZip.getFiles();

for (int i=0; i < files.length(); i++) {
String fileName = files.get(i);
if (isGameFile(fileName)) {
JSFile gameFile = jsZip.getFile(fileName);
gameFileMap.put(fileName.toLowerCase(), gameFile.asUint8Array().toByteArray());
}
}
});

gameFilesConsumer.accept(gameFileMap);

/* TODO: Add back in when ZIP files are supported again.
JSZip jsZip = JSZip.loadFile(gameUri);
JsArrayString files = jsZip.getFiles();
for (int i=0; i < files.length(); i++) {
String fileName = files.get(i);
if (isGameFile(fileName)) {
JSFile gameFile = jsZip.getFile(fileName);
gameFileMap.put(fileName.toLowerCase(), gameFile.asUint8Array().toByteArray());
}
} else {
// Otherwise, if it isn't a ZIP URL, then it must be in the OPFS.
opfsGameFiles.readGameFilesData(gameUri, new GwtOpenFileResultsHandler() {
@Override
public void onFileResultsReady(GwtOpenFileResult[] openFileResultArray) {
if (openFileResultArray.length == 1) {
GwtOpenFileResult openFileResult = openFileResultArray[0];
Map<String, byte[]> gameFileMap = gameFileMapEncoder.decodeGameFileMap(openFileResult.getFileData());
gameFilesConsumer.accept(gameFileMap);
}
}
});
}
*/
}
}

0 comments on commit 77d4790

Please sign in to comment.