fix: Epic games with a JSON manifest download (#578)

This commit is contained in:
Gustash authored and GitHub committed 2026-10-11 17:37:47 -04:00
1 parent 55c51f843e
commit 8f5ccbf23f
3 files changed
+148 -26

No files matched your search

@@ -548,10 +548,10 @@ public final class EpicDownloadManager {
try {
JSONObject root = new JSONObject(new String(bytes, StandardCharsets.UTF_8));
int manifestVersion = 0;
try { manifestVersion = Integer.parseInt(root.optString("ManifestFileVersion", "0")); } catch (NumberFormatException ignored) {}
try { manifestVersion = (int) blobToNum(root.optString("ManifestFileVersion", "0")); } catch (NumberFormatException ignored) {}
Manifest m = new Manifest();
m.chunkDir = manifestVersion >= 15 ? "ChunksV4" : manifestVersion >= 6 ? "ChunksV3" : manifestVersion >= 3 ? "ChunksV2" : "ChunksV4";
m.launchExe = root.optString("LaunchExe", "").replace('\\', '/');
m.launchExe = root.optString("LaunchExeString", root.optString("LaunchExe", "")).replace('\\', '/');
m.launchCommand = root.optString("LaunchCommand", "");
m.buildVersion = root.optString("BuildVersionString", "");
m.prereqName = root.optString("PrereqName", "");
@@ -568,10 +568,9 @@ public final class EpicDownloadManager {
if (guidHex.length() < 32) continue;
ChunkInfo c = new ChunkInfo();
for (int k = 0; k < 4; k++) c.guid[k] = (int) Long.parseLong(guidHex.substring(k * 8, k * 8 + 8), 16);
String hashHex = chunkHashList.getString(guidHex);
if (hashHex != null && hashHex.length() >= 16) { try { c.hash = Long.parseUnsignedLong(hashHex.substring(0, 16), 16); } catch (Exception ignored) {} }
if (dataGroupList != null) { try { c.groupNum = Integer.parseInt(dataGroupList.optString(guidHex, "0")); } catch (NumberFormatException ignored) {} }
if (chunkFilesizeList != null) { try { c.fileSize = Long.parseLong(chunkFilesizeList.optString(guidHex, "0"), 16); } catch (NumberFormatException ignored) {} }
try { c.hash = blobToNum(chunkHashList.getString(guidHex)); } catch (NumberFormatException ignored) {}
if (dataGroupList != null) { try { c.groupNum = (int) blobToNum(dataGroupList.optString(guidHex, "0")); } catch (NumberFormatException ignored) {} }
if (chunkFilesizeList != null) { try { c.fileSize = blobToNum(chunkFilesizeList.optString(guidHex, "0")); } catch (NumberFormatException ignored) {} }
chunkMap.put(guidHex, c);
}
JSONArray fileList = root.optJSONArray("FileManifestList");
@@ -589,8 +588,8 @@ public final class EpicDownloadManager {
ChunkPart part = new ChunkPart();
String g = po.optString("Guid", "");
if (g.length() >= 32) for (int k = 0; k < 4; k++) part.guid[k] = (int) Long.parseLong(g.substring(k * 8, k * 8 + 8), 16);
try { part.offset = Integer.parseInt(po.optString("Offset", "0")); } catch (NumberFormatException ignored) {}
try { part.size = Integer.parseInt(po.optString("Size", "0")); } catch (NumberFormatException ignored) {}
try { part.offset = (int) blobToNum(po.optString("Offset", "0")); } catch (NumberFormatException ignored) {}
try { part.size = (int) blobToNum(po.optString("Size", "0")); } catch (NumberFormatException ignored) {}
fi.parts.add(part);
}
files.add(fi);
@@ -604,6 +603,24 @@ public final class EpicDownloadManager {
}
}
/**
* A JSON manifest writes every number as a "blob": each byte as three decimal digits, least
* significant first ("013000000000" = 13), as Legendary's blob_to_num reads it. Bits past 64
* are dropped; a group that is not digits throws.
*/
static long blobToNum(String blob) {
long num = 0;
for (int i = 0, shift = 0; i < blob.length(); i += 3, shift += 8) {
String group = blob.substring(i, Math.min(i + 3, blob.length()));
for (int k = 0; k < group.length(); k++) {
char ch = group.charAt(k);
if (ch < '0' || ch > '9') throw new NumberFormatException("not a blob: " + blob);
}
if (shift < 64) num += Long.parseLong(group) << shift;
}
return num;
}
public static String readFString(ByteBuffer buf) {
int length = buf.getInt();
if (length == 0) return "";
+68 -18
View File
@@ -4,7 +4,7 @@
//! Every quirk of the Java reader is kept on purpose (the Java manager stays the reference and
//! the two engines must derive the same plan from the same bytes): section skips by declared
//! size, `readFString` UTF-16LE/ASCII decoding, duplicate-GUID handling (last value, first
//! position), the JSON `Long.parseLong(.., 16)` / `parseInt` fallbacks, and "any exception →
//! position), the JSON blob decoding (`blobToNum`) and its fallbacks, and "any exception →
//! parse failed".
use std::collections::HashMap;
@@ -445,6 +445,25 @@ fn json_to_string(v: &serde_json::Value) -> String {
}
}
/// `EpicDownloadManager.blobToNum`: a JSON manifest writes every number as a "blob", each byte
/// as three decimal digits, least significant first ("013000000000" = 13). Bits past 64 are
/// dropped; `None` when a group is not a number (Java: `NumberFormatException`).
pub fn blob_to_num(s: &str) -> Option<u64> {
let bytes = s.as_bytes();
let mut num: u64 = 0;
for (i, group) in bytes.chunks(3).enumerate() {
if !group.iter().all(u8::is_ascii_digit) {
return None;
}
let byte: u64 = std::str::from_utf8(group).ok()?.parse().ok()?;
let shift = i * 8;
if shift < 64 {
num = num.wrapping_add(byte << shift);
}
}
Some(num)
}
/// `obj.optString(key, fallback)`.
fn opt_string(obj: &serde_json::Map<String, serde_json::Value>, key: &str, fallback: &str) -> String {
match obj.get(key) {
@@ -483,9 +502,8 @@ pub fn parse_json_manifest(bytes: &[u8]) -> Result<Manifest, String> {
.as_object()
.ok_or_else(|| "JSON manifest: root is not an object".to_string())?;
let manifest_version: i32 = opt_string(root, "ManifestFileVersion", "0")
.parse::<i32>()
.unwrap_or(0);
let manifest_version: i32 = blob_to_num(&opt_string(root, "ManifestFileVersion", "0"))
.map_or(0, |v| v as i32);
let chunk_dir = chunk_dir_for_json_version(manifest_version);
let chunk_hash_list = root
@@ -502,19 +520,14 @@ pub fn parse_json_manifest(bytes: &[u8]) -> Result<Manifest, String> {
if guid_hex.len() < 32 || !guid_hex.is_char_boundary(32) {
continue;
}
let hash_hex = json_to_string(hash_value);
let guid = parse_guid_from_hex(guid_hex)?;
let hash = if hash_hex.len() >= 16 && hash_hex.is_char_boundary(16) {
u64::from_str_radix(&hash_hex[0..16], 16).unwrap_or(0)
} else {
0
};
let hash = blob_to_num(&json_to_string(hash_value)).unwrap_or(0);
let group_num = match data_group_list {
Some(dgl) => opt_string(dgl, guid_hex, "0").parse::<i32>().unwrap_or(0),
Some(dgl) => blob_to_num(&opt_string(dgl, guid_hex, "0")).map_or(0, |v| v as i32),
None => 0,
};
let file_size = match chunk_filesize_list {
Some(cfl) => i64::from_str_radix(&opt_string(cfl, guid_hex, "0"), 16).unwrap_or(0),
Some(cfl) => blob_to_num(&opt_string(cfl, guid_hex, "0")).map_or(0, |v| v as i64),
None => 0,
};
let c = ChunkInfo {
@@ -570,8 +583,8 @@ pub fn parse_json_manifest(bytes: &[u8]) -> Result<Manifest, String> {
if part_guid.len() >= 32 && part_guid.is_char_boundary(32) {
part.guid = parse_guid_from_hex(&part_guid)?;
}
part.offset = opt_string(part_obj, "Offset", "0").parse::<i32>().unwrap_or(0);
part.size = opt_string(part_obj, "Size", "0").parse::<i32>().unwrap_or(0);
part.offset = blob_to_num(&opt_string(part_obj, "Offset", "0")).map_or(0, |v| v as i32);
part.size = blob_to_num(&opt_string(part_obj, "Size", "0")).map_or(0, |v| v as i32);
fi.parts.push(part);
}
}
@@ -928,17 +941,29 @@ mod tests {
assert_eq!(c.fstring().unwrap(), "a\u{FFFD}b");
}
#[test]
fn blob_numbers_are_decimal_bytes_least_significant_first() {
assert_eq!(blob_to_num("001000000000"), Some(1));
assert_eq!(blob_to_num("000001000000"), Some(0x100));
assert_eq!(blob_to_num("001002003004005006007008"), Some(0x0807_0605_0403_0201));
assert_eq!(blob_to_num("255255255255255255255255"), Some(u64::MAX));
assert_eq!(blob_to_num("7"), Some(7));
assert_eq!(blob_to_num(""), Some(0));
assert_eq!(blob_to_num("0x1"), None);
assert_eq!(blob_to_num("-05"), None);
}
#[test]
fn json_manifest_parses_like_java() {
let json = r#"{
"ManifestFileVersion": "013",
"ChunkHashList": {
"0000000100000002000000030000000ff": "0123456789ABCDEF00",
"0000000100000002000000030000000ff": "001002003004005006007008",
"short": "00",
"DEADBEEFFFFFFFFF0000000080000000": "zz"
},
"DataGroupList": { "0000000100000002000000030000000ff": "7", "DEADBEEFFFFFFFFF0000000080000000": "x" },
"ChunkFilesizeList": { "0000000100000002000000030000000ff": "FF" },
"ChunkFilesizeList": { "0000000100000002000000030000000ff": "255" },
"FileManifestList": [
{ "Filename": "a.bin", "InstallTags": ["German", ""],
"FileChunkParts": [ { "Guid": "0000000100000002000000030000000ff", "Offset": "12", "Size": "34" },
@@ -951,7 +976,7 @@ mod tests {
assert_eq!(m.unique_chunks.len(), 2, "keys shorter than 32 chars are skipped");
let c0 = &m.unique_chunks[0];
assert_eq!(c0.guid, [1, 2, 3, 0x0000_000F]);
assert_eq!(c0.hash, 0x0123_4567_89AB_CDEF);
assert_eq!(c0.hash, 0x0807_0605_0403_0201);
assert_eq!(c0.group_num, 7);
assert_eq!(c0.file_size, 0xFF);
assert_eq!(c0.window_size, 0);
@@ -968,11 +993,36 @@ mod tests {
assert_eq!(m.files[0].parts[0].size, 34);
assert_eq!(m.files[0].parts[1].guid, [0; 4]);
assert_eq!(m.files[0].parts[1].offset, 0);
assert_eq!(m.files[0].parts[1].size, -5);
assert_eq!(m.files[0].parts[1].size, 0);
assert!(m.files[0].sha1.is_none());
assert!(m.files[1].parts.is_empty());
}
#[test]
fn json_manifest_uses_blobs() {
let json = r#"{
"ManifestFileVersion": "006000000000",
"ChunkHashList": { "0000000100000002000000030000000F": "001002003004005006007008" },
"DataGroupList": { "0000000100000002000000030000000F": "007" },
"ChunkFilesizeList": { "0000000100000002000000030000000F": "001002000000000000000000" },
"FileManifestList": [
{ "Filename": "a.bin",
"FileChunkParts": [ { "Guid": "0000000100000002000000030000000F", "Offset": "001000000000", "Size": "000000016000" } ] }
]
}"#;
let m = parse_manifest(json.as_bytes()).expect("json parse");
assert_eq!(m.chunk_dir, "ChunksV3");
let c = &m.unique_chunks[0];
assert_eq!(c.file_size, 0x0201);
assert_eq!(
c.path(&m.chunk_dir),
"ChunksV3/07/0807060504030201_0000000100000002000000030000000F.chunk"
);
let part = &m.files[0].parts[0];
assert_eq!(part.offset, 1);
assert_eq!(part.size, 1_048_576);
}
#[test]
fn json_manifest_failures_match_java_nulls() {
assert!(parse_manifest(br#"{"FileManifestList": []}"#).is_err(), "no ChunkHashList");
@@ -0,0 +1,55 @@
package com.droiddeck.launcher.stores.epic
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNotNull
import org.junit.Assert.assertThrows
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import org.robolectric.annotation.Config
/** The JSON manifest's numbers are blobs; the same values as the Rust engine's manifest tests. */
@RunWith(RobolectricTestRunner::class)
@Config(sdk = [33])
class EpicJsonManifestTest {
@Test
fun blobNumbersAreDecimalBytesLeastSignificantFirst() {
assertEquals(1L, EpicDownloadManager.blobToNum("001000000000"))
assertEquals(0x100L, EpicDownloadManager.blobToNum("000001000000"))
assertEquals(0x0807_0605_0403_0201L, EpicDownloadManager.blobToNum("001002003004005006007008"))
assertEquals(-1L, EpicDownloadManager.blobToNum("255255255255255255255255"))
assertEquals(7L, EpicDownloadManager.blobToNum("7"))
assertEquals(0L, EpicDownloadManager.blobToNum(""))
}
@Test
fun aBlobIsDigitsOnly() {
assertThrows(NumberFormatException::class.java) { EpicDownloadManager.blobToNum("0x1") }
assertThrows(NumberFormatException::class.java) { EpicDownloadManager.blobToNum("-05") }
}
@Test
fun jsonManifestUsesBlobs() {
val json = """{
"ManifestFileVersion": "006000000000",
"LaunchExeString": "Game\\Binaries\\Game.exe",
"ChunkHashList": { "0000000100000002000000030000000F": "001002003004005006007008" },
"DataGroupList": { "0000000100000002000000030000000F": "007" },
"ChunkFilesizeList": { "0000000100000002000000030000000F": "001002000000000000000000" },
"FileManifestList": [
{ "Filename": "a.bin",
"FileChunkParts": [ { "Guid": "0000000100000002000000030000000F", "Offset": "001000000000", "Size": "000000016000" } ] }
]
}"""
val m = EpicDownloadManager.parseManifest(json.toByteArray())
assertNotNull(m)
assertEquals("ChunksV3", m.chunkDir)
assertEquals("Game/Binaries/Game.exe", m.launchExe)
val c = m.uniqueChunks.single()
assertEquals(0x0201L, c.fileSize)
assertEquals("ChunksV3/07/0807060504030201_0000000100000002000000030000000F.chunk", c.getPath(m.chunkDir))
val part = m.files.single().parts.single()
assertEquals(1, part.offset)
assertEquals(1_048_576, part.size)
}
}