Skip to content

Commit

Permalink
Model AppCacheFormat explicitly
Browse files Browse the repository at this point in the history
This will future-proof it better.
  • Loading branch information
selesse committed Jul 13, 2024
1 parent b41f9a9 commit b7bbc1b
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,13 @@ public AppCacheBufferedReader(Path path) {
public AppCache call() throws Exception {
try (BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(path.toFile()))) {
String firstFourBytes = readFourBytes(bufferedInputStream);
if (!(firstFourBytes.equals("27 44 56 7")
|| firstFourBytes.equals("28 44 56 7")
|| firstFourBytes.equals("29 44 56 7"))) {
throw new IllegalStateException("Unknown app cache format: " + firstFourBytes);
}
boolean parseSha1Binary = firstFourBytes.equals("28 44 56 7") || firstFourBytes.equals("29 44 56 7");
AppCacheFormat appCacheFormat = AppCacheFormat.fromFirstFourBytes(firstFourBytes);
boolean parseSha1Binary = appCacheFormat.isAtLeast(AppCacheFormat.TWENTY_EIGHT);
String nextByte = readFourBytes(bufferedInputStream);
assert nextByte.equals("1 0 0 0");
AppCache appCache = new AppCache();
StringCache stringCache = null;
if (firstFourBytes.equals("29 44 56 7")) {
if (appCacheFormat.isAtLeast(AppCacheFormat.TWENTY_NINE)) {
long offsetToStringTable = parse64Int(bufferedInputStream);
stringCache = new StringCacheReader(path, offsetToStringTable).read();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.selesse.steam.appcache;

import java.util.Arrays;

public enum AppCacheFormat {
TWENTY_SEVEN("27"),
TWENTY_EIGHT("28"),
TWENTY_NINE("29"),
;

private final String number;

AppCacheFormat(String number) {
this.number = number;
}

public static AppCacheFormat fromFirstFourBytes(String bytes) {
if (!bytes.endsWith("44 56 7")) {
throw new IllegalStateException("First four bytes didn't end in '44 56 7'");
}
String magicNumber = bytes.split(" ")[0];
return fromString(magicNumber);
}

private static AppCacheFormat fromString(String value) {
return Arrays.stream(values())
.filter(x -> x.number.equals(value))
.findFirst()
.orElseThrow();
}

public boolean isAtLeast(AppCacheFormat appCacheFormat) {
return this.ordinal() >= appCacheFormat.ordinal();
}
}

0 comments on commit b7bbc1b

Please sign in to comment.