Skip to content

Commit

Permalink
Added GPC defaults.
Browse files Browse the repository at this point in the history
  • Loading branch information
albertus82 committed May 30, 2021
1 parent 189f49a commit fe8b66b
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 2 deletions.
23 changes: 23 additions & 0 deletions src/main/java/it/albertus/cyclesmod/common/data/DataUtils.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package it.albertus.cyclesmod.common.data;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.Base64;
import java.util.zip.CRC32;
import java.util.zip.Checksum;
import java.util.zip.DataFormatException;
import java.util.zip.Deflater;
import java.util.zip.Inflater;

import lombok.AccessLevel;
Expand Down Expand Up @@ -39,4 +44,22 @@ private static long computeCrc32(@NonNull final byte[] bytes) {
return crc.getValue();
}

static String deflateToBase64(@NonNull final Path path) throws IOException {
final long inputSize = Files.size(path);
if (inputSize > 0x200000) { // 2 MiB
throw new IllegalArgumentException("Input file is too large");
}
final byte[] output = new byte[(int) inputSize];
final Deflater deflater = new Deflater(Deflater.BEST_COMPRESSION);
try {
deflater.setInput(Files.readAllBytes(path));
deflater.finish();
int compressedDataLength = deflater.deflate(output);
return Base64.getEncoder().encodeToString(Arrays.copyOf(output, compressedDataLength));
}
finally {
deflater.end();
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class DefaultBikes {

private static final long CRC32 = 0x28A33682L;
private static final long CRC32 = Integer.toUnsignedLong(0x28A33682);

private static final String DEFLATED_BASE64 = "eNpjY1ihv8GsiPEdEwQmAaGMKgMDwwaGmQytDMUMqQyRkpQAGTCQhwBFZWUVZKAIA5L8rAxwgO6mMiAEu2kBQwfQRYkMYQx+qpQALSjQ1QcBIxNzBDCCAn1dDWUZEX4uVribLAxBbuJh5mEGuakLCJVUgBKTGGoYMhkiGfwYXPRJBI7IwBsLCIAAJBE3R3NdZRGoqwArb09n";

Expand Down
35 changes: 35 additions & 0 deletions src/main/java/it/albertus/cyclesmod/common/data/DefaultCars.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package it.albertus.cyclesmod.common.data;

import java.util.zip.DataFormatException;

import it.albertus.cyclesmod.common.model.BikesInf;
import it.albertus.cyclesmod.common.resources.CommonMessages;
import it.albertus.cyclesmod.common.resources.Messages;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;

@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class DefaultCars {

private static final long CRC32 = Integer.toUnsignedLong(0x8B31837B);

private static final String DEFLATED_BASE64 = "eNpjZTDQeqCXwnCA+QBzGkcaBxMzEzObBAMDQxNDKoMfgy2DMQODBgwY2bhhA97YQCgURMXDQAoCgLgxMVGhAWClMHMYoICN4Y3OFYMKBgVmBWY+Nj62C0wXmHxkgRK9DHkMUQxeIFfB3aRhjgps4MARCKAme3j5+QUgAMxlKZmZeRVNPU0VFcVgUFCQl5OZEhWA1U0dOg/0poHddI3tGts7pncQN/Ux5DNEg91kRI6bgoJCQsIioqKiYoAAFkQFQLfk5WUCQXpKUjwsmDDcBAA7dXI/";

private static final Messages messages = CommonMessages.INSTANCE;

public static byte[] getByteArray() {
try {
return DataUtils.inflate(DEFLATED_BASE64, BikesInf.FILE_SIZE, CRC32);
}
catch (final IllegalArgumentException | DataFormatException e) {
throw new VerifyError(messages.get("common.error.original.file.corrupted", BikesInf.FILE_NAME), e);
}
catch (final InvalidSizeException e) {
throw new VerifyError(messages.get("common.error.original.file.corrupted.size", BikesInf.FILE_NAME, e.getExpected(), e.getActual()), e);
}
catch (final InvalidChecksumException e) {
throw new VerifyError(messages.get("common.error.original.file.corrupted.crc", BikesInf.FILE_NAME, String.format("%08X", e.getExpected()), String.format("%08X", e.getActual())), e);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class HiddenBike {

private static final long CRC32 = 0xEF30BAE6L;
private static final long CRC32 = Integer.toUnsignedLong(0xEF30BAE6);

private static final String DEFLATED_BASE64 = "eNpjY5DQ22DGr57GnMb8jukd0ynmU8zvpBkYGDoYihiiGDwYnBjsNFCBkY0bduCNDYRCQVQ8DKQgAIgbExMVGgBWamOkzM8KtBoAC/woBQ==";

Expand Down
35 changes: 35 additions & 0 deletions src/main/java/it/albertus/cyclesmod/common/data/HiddenCar.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package it.albertus.cyclesmod.common.data;

import java.util.zip.DataFormatException;

import it.albertus.cyclesmod.common.model.BikesInf;
import it.albertus.cyclesmod.common.resources.CommonMessages;
import it.albertus.cyclesmod.common.resources.Messages;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;

@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class HiddenCar {

private static final long CRC32 = Integer.toUnsignedLong(0x25C5E02D);

private static final String DEFLATED_BASE64 = "eNpjZVDR4TFIYTjAfIA5jSONQ4xZjJlNgoGBoYkhlcGPwZbBmIFBAwaMbNywAW9sIBQKouJhIAUBQNyYmKjQALBSmDkMUAAAoWokhw==";

private static final Messages messages = CommonMessages.INSTANCE;

public static byte[] getByteArray() {
try {
return DataUtils.inflate(DEFLATED_BASE64, BikesInf.FILE_SIZE / 3, CRC32);
}
catch (final IllegalArgumentException | DataFormatException e) {
throw new VerifyError(messages.get("common.error.hidden.cfg.corrupted"), e);
}
catch (final InvalidSizeException e) {
throw new VerifyError(messages.get("common.error.hidden.cfg.corrupted.size", e.getExpected(), e.getActual()), e);
}
catch (final InvalidChecksumException e) {
throw new VerifyError(messages.get("common.error.hidden.cfg.corrupted.crc", String.format("%08X", e.getExpected()), String.format("%08X", e.getActual())), e);
}
}

}

0 comments on commit fe8b66b

Please sign in to comment.