Code improvements, now decoding works properly

This commit is contained in:
Yuri Torlopov 2023-02-11 16:48:15 +03:00
parent 8994bcf209
commit 2025e371e6
8 changed files with 38 additions and 49 deletions

View file

@ -63,22 +63,29 @@ public enum Decoder {
bytes[byteInd] ^= (1 << bitInd); bytes[byteInd] ^= (1 << bitInd);
} }
return Decoder.readInternetCode(bytes); return Decoder.getGameInfo(bytes);
} }
private static String readInternetCode(byte[] bytes) { public static String getGameInfo(byte[] bytes) {
String resultLabel = I18N.getString("resultLabel");
String[] internetCodes = Decoder.readInternetCode(bytes).split("_");
String totalPlaytime = Decoder.readTotalPlaytime(bytes);
return String.format(resultLabel, internetCodes[0], internetCodes[1], totalPlaytime);
}
public static String readInternetCode(byte[] bytes) {
int intCode = 0; int intCode = 0;
intCode |= (bytes[8] & (0x1E000000 >> 25)) << 25; intCode |= (bytes[8] & (0x1E000000 >> 25)) << 25;
intCode |= (bytes[9] & (0x01000000 >> 19)) << 19; intCode |= (bytes[9] & (0x01000000 >> 19)) << 19;
intCode |= (bytes[9] & (0x00001F00 >> 8)) << 8; intCode |= (bytes[9] & (0x00001F00 >> 8)) << 8;
intCode |= (bytes[10] & (0x001F0000 >> 14)) << 14; intCode |= (bytes[10] & (0x001F0000 >> 14)) << 14;
intCode |= (bytes[10] & (0x0000000C >> 2)) << 2; intCode |= (bytes[10] & (0x0000000C >> 2)) << 2;
intCode |= (bytes[11] & (0x00000003 << 4)) >> 4; intCode |= (bytes[11] & (0x00000003 << 4)) >> 4;
int l1 = (intCode >> 24) & 0xFF; int l1 = (intCode >> 24) & 0xFF;
int d1 = (intCode >> 16) & 0xFF; int d1 = (intCode >> 16) & 0xFF;
int l2 = (intCode >> 8) & 0xFF; int l2 = (intCode >> 8) & 0xFF;
int d2 = (intCode >> 0) & 0xFF; int d2 = (intCode >> 0) & 0xFF;
String[] letters1 = DecodingData.BYTES_TO_CODE.get(Integer.valueOf(l1).byteValue()); String[] letters1 = DecodingData.BYTES_TO_CODE.get(Integer.valueOf(l1).byteValue());
String[] letters2 = DecodingData.BYTES_TO_CODE.get(Integer.valueOf(l2).byteValue()); String[] letters2 = DecodingData.BYTES_TO_CODE.get(Integer.valueOf(l2).byteValue());
@ -89,9 +96,12 @@ public enum Decoder {
String russianCode = letters1[1] + d1 + letters2[1] + d2; String russianCode = letters1[1] + d1 + letters2[1] + d2;
String englishCode = letters1[0] + d1 + letters2[0] + d2; String englishCode = letters1[0] + d1 + letters2[0] + d2;
// I could put this in PRSF (private static final), but it might be not initialized yet return russianCode + "_" + englishCode;
String russianLabel = I18N.getString("codeLabelRu"); }
String englishLabel = I18N.getString("codeLabelEn");
return String.format("%s: %s %s %s: %s", russianLabel, russianCode, I18N.getString("andText"), englishLabel, englishCode); public static String readTotalPlaytime(byte[] bytes) {
return (bytes[14] < 10 ? "0" + bytes[14] : "" + bytes[14]) + ":" +
(bytes[13] < 10 ? "0" + bytes[13] : "" + bytes[13]) + ":" +
(bytes[15] < 10 ? "0" + bytes[15] : "" + bytes[15]);
} }
} }

View file

@ -29,6 +29,11 @@ public enum DecodingData {
*/ */
public static final long CODE_OFFSET = 0x00002D58L; public static final long CODE_OFFSET = 0x00002D58L;
/**
* Offset, where total play time in seconds located in save
*/
public static final long TOTAL_PLAYTIME_OFFSET = 0x00000D4CL;
/** /**
* Key for decoding Internet Code * Key for decoding Internet Code
*/ */

View file

@ -45,20 +45,11 @@ public enum I18N {
I18N.TRANSLATION.put("en.badFile", "Bad file given!"); I18N.TRANSLATION.put("en.badFile", "Bad file given!");
I18N.TRANSLATION.put("ru.badFile", "Перемещен плохой файл!"); I18N.TRANSLATION.put("ru.badFile", "Перемещен плохой файл!");
I18N.TRANSLATION.put("ru.andText", "и"); I18N.TRANSLATION.put("en.resultLabel", "<html>Russian code: %s and English code: %s<br>Total time played: %s</html>");
I18N.TRANSLATION.put("en.andText", "and"); I18N.TRANSLATION.put("ru.resultLabel", "<html>Русский код: %s и Английский код: %s<br>Всего времени отыграно: %s</html>");
I18N.TRANSLATION.put("ru.codeLabelRu", "Русский код");
I18N.TRANSLATION.put("ru.codeLabelEn", "Английский код");
I18N.TRANSLATION.put("en.codeLabelRu", "Russian code");
I18N.TRANSLATION.put("en.codeLabelEn", "English code");
} }
public static String getString(String key) { public static String getString(String key) {
/*if(key.indexOf(".") == 2) {
return I18N.TRANSLATION.get(key);
}*/
return I18N.TRANSLATION.get(I18N.LANGUAGE + "." + key); return I18N.TRANSLATION.get(I18N.LANGUAGE + "." + key);
} }
} }

View file

@ -19,17 +19,6 @@ package me.theentropyshard.jdarkroom;
public class JDarkroom { public class JDarkroom {
public JDarkroom() { public JDarkroom() {
if(instance != null) {
throw new IllegalStateException("JDarkroom already running!");
}
instance = this;
new View(); new View();
} }
private static JDarkroom instance;
public static JDarkroom getInstance() {
return instance;
}
} }

View file

@ -24,8 +24,8 @@ import java.util.regex.Pattern;
public class Main { public class Main {
public static void main(String[] args) { public static void main(String[] args) {
Pattern localePattern = Pattern.compile("([a-z]{2})-([A-Z]{2})$");
if(args.length > 0) { if(args.length > 0) {
Pattern localePattern = Pattern.compile("([a-z]{2})-([A-Z]{2})$");
Matcher matcher = localePattern.matcher(args[0]); Matcher matcher = localePattern.matcher(args[0]);
if(matcher.matches()) { if(matcher.matches()) {
try { try {
@ -34,6 +34,8 @@ public class Main {
} catch (Exception se) { } catch (Exception se) {
se.printStackTrace(); se.printStackTrace();
} }
} else {
System.err.println("Incorrect locale supplied: " + args[0]);
} }
} }

View file

@ -31,6 +31,7 @@ public enum SaveFileReader {
StringBuilder russianCode = new StringBuilder(); StringBuilder russianCode = new StringBuilder();
StringBuilder englishCode = new StringBuilder(); StringBuilder englishCode = new StringBuilder();
String totalPlaytime = "00:00:00";
try(RandomAccessFile raf = new RandomAccessFile(file, "r")) { try(RandomAccessFile raf = new RandomAccessFile(file, "r")) {
if(raf.length() <= 0) { if(raf.length() <= 0) {
return null; return null;
@ -51,9 +52,7 @@ public enum SaveFileReader {
return null; return null;
} }
String russianLabel = I18N.getString("codeLabelRu"); String resultLabel = I18N.getString("resultLabel");
String englishLabel = I18N.getString("codeLabelEn"); return String.format(resultLabel, russianCode, englishCode, totalPlaytime);
String andText = I18N.getString("andText");
return String.format("%s: %s %s %s: %s", russianLabel, russianCode, andText, englishLabel, englishCode);
} }
} }

View file

@ -39,10 +39,8 @@ public enum Utils {
Utils.IMAGE_CACHE.put(path, bufferedImage); Utils.IMAGE_CACHE.put(path, bufferedImage);
return bufferedImage; return bufferedImage;
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); throw new RuntimeException(e);
} }
return null;
} }
/** /**
@ -70,10 +68,4 @@ public enum Utils {
bytes[byteInd1] |= 1 << bi1; bytes[byteInd1] |= 1 << bi1;
} }
} }
/*public static final class StrIntBiMap extends HashMap<String, Integer> {
public Integer getValueByKey(String key) {
}
}*/
} }

View file

@ -223,6 +223,7 @@ public final class View extends JPanel {
cardLayout.show(this, "dropFilePanel"); cardLayout.show(this, "dropFilePanel");
JFrame frame = new JFrame("JDarkroom"); JFrame frame = new JFrame("JDarkroom");
frame.setResizable(false);
frame.add(this, BorderLayout.CENTER); frame.add(this, BorderLayout.CENTER);
frame.pack(); frame.pack();
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);