Skip to content

Commit 2025e37

Browse files
Code improvements, now decoding works properly
1 parent 8994bcf commit 2025e37

File tree

8 files changed

+38
-49
lines changed

8 files changed

+38
-49
lines changed

src/me/theentropyshard/jdarkroom/Decoder.java

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -63,22 +63,29 @@ public static String decodeInternetCode(String code) {
6363
bytes[byteInd] ^= (1 << bitInd);
6464
}
6565

66-
return Decoder.readInternetCode(bytes);
66+
return Decoder.getGameInfo(bytes);
6767
}
6868

69-
private static String readInternetCode(byte[] bytes) {
69+
public static String getGameInfo(byte[] bytes) {
70+
String resultLabel = I18N.getString("resultLabel");
71+
String[] internetCodes = Decoder.readInternetCode(bytes).split("_");
72+
String totalPlaytime = Decoder.readTotalPlaytime(bytes);
73+
return String.format(resultLabel, internetCodes[0], internetCodes[1], totalPlaytime);
74+
}
75+
76+
public static String readInternetCode(byte[] bytes) {
7077
int intCode = 0;
71-
intCode |= (bytes[8] & (0x1E000000 >> 25)) << 25;
72-
intCode |= (bytes[9] & (0x01000000 >> 19)) << 19;
73-
intCode |= (bytes[9] & (0x00001F00 >> 8)) << 8;
78+
intCode |= (bytes[8] & (0x1E000000 >> 25)) << 25;
79+
intCode |= (bytes[9] & (0x01000000 >> 19)) << 19;
80+
intCode |= (bytes[9] & (0x00001F00 >> 8)) << 8;
7481
intCode |= (bytes[10] & (0x001F0000 >> 14)) << 14;
75-
intCode |= (bytes[10] & (0x0000000C >> 2)) << 2;
76-
intCode |= (bytes[11] & (0x00000003 << 4)) >> 4;
82+
intCode |= (bytes[10] & (0x0000000C >> 2)) << 2;
83+
intCode |= (bytes[11] & (0x00000003 << 4)) >> 4;
7784

7885
int l1 = (intCode >> 24) & 0xFF;
7986
int d1 = (intCode >> 16) & 0xFF;
80-
int l2 = (intCode >> 8) & 0xFF;
81-
int d2 = (intCode >> 0) & 0xFF;
87+
int l2 = (intCode >> 8) & 0xFF;
88+
int d2 = (intCode >> 0) & 0xFF;
8289

8390
String[] letters1 = DecodingData.BYTES_TO_CODE.get(Integer.valueOf(l1).byteValue());
8491
String[] letters2 = DecodingData.BYTES_TO_CODE.get(Integer.valueOf(l2).byteValue());
@@ -89,9 +96,12 @@ private static String readInternetCode(byte[] bytes) {
8996
String russianCode = letters1[1] + d1 + letters2[1] + d2;
9097
String englishCode = letters1[0] + d1 + letters2[0] + d2;
9198

92-
// I could put this in PRSF (private static final), but it might be not initialized yet
93-
String russianLabel = I18N.getString("codeLabelRu");
94-
String englishLabel = I18N.getString("codeLabelEn");
95-
return String.format("%s: %s %s %s: %s", russianLabel, russianCode, I18N.getString("andText"), englishLabel, englishCode);
99+
return russianCode + "_" + englishCode;
100+
}
101+
102+
public static String readTotalPlaytime(byte[] bytes) {
103+
return (bytes[14] < 10 ? "0" + bytes[14] : "" + bytes[14]) + ":" +
104+
(bytes[13] < 10 ? "0" + bytes[13] : "" + bytes[13]) + ":" +
105+
(bytes[15] < 10 ? "0" + bytes[15] : "" + bytes[15]);
96106
}
97107
}

src/me/theentropyshard/jdarkroom/DecodingData.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ public enum DecodingData {
2929
*/
3030
public static final long CODE_OFFSET = 0x00002D58L;
3131

32+
/**
33+
* Offset, where total play time in seconds located in save
34+
*/
35+
public static final long TOTAL_PLAYTIME_OFFSET = 0x00000D4CL;
36+
3237
/**
3338
* Key for decoding Internet Code
3439
*/

src/me/theentropyshard/jdarkroom/I18N.java

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -45,20 +45,11 @@ public enum I18N {
4545
I18N.TRANSLATION.put("en.badFile", "Bad file given!");
4646
I18N.TRANSLATION.put("ru.badFile", "Перемещен плохой файл!");
4747

48-
I18N.TRANSLATION.put("ru.andText", "и");
49-
I18N.TRANSLATION.put("en.andText", "and");
50-
51-
I18N.TRANSLATION.put("ru.codeLabelRu", "Русский код");
52-
I18N.TRANSLATION.put("ru.codeLabelEn", "Английский код");
53-
54-
I18N.TRANSLATION.put("en.codeLabelRu", "Russian code");
55-
I18N.TRANSLATION.put("en.codeLabelEn", "English code");
48+
I18N.TRANSLATION.put("en.resultLabel", "<html>Russian code: %s and English code: %s<br>Total time played: %s</html>");
49+
I18N.TRANSLATION.put("ru.resultLabel", "<html>Русский код: %s и Английский код: %s<br>Всего времени отыграно: %s</html>");
5650
}
5751

5852
public static String getString(String key) {
59-
/*if(key.indexOf(".") == 2) {
60-
return I18N.TRANSLATION.get(key);
61-
}*/
6253
return I18N.TRANSLATION.get(I18N.LANGUAGE + "." + key);
6354
}
6455
}

src/me/theentropyshard/jdarkroom/JDarkroom.java

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,6 @@
1919

2020
public class JDarkroom {
2121
public JDarkroom() {
22-
if(instance != null) {
23-
throw new IllegalStateException("JDarkroom already running!");
24-
}
25-
instance = this;
26-
2722
new View();
2823
}
29-
30-
private static JDarkroom instance;
31-
32-
public static JDarkroom getInstance() {
33-
return instance;
34-
}
3524
}

src/me/theentropyshard/jdarkroom/Main.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424

2525
public class Main {
2626
public static void main(String[] args) {
27-
Pattern localePattern = Pattern.compile("([a-z]{2})-([A-Z]{2})$");
2827
if(args.length > 0) {
28+
Pattern localePattern = Pattern.compile("([a-z]{2})-([A-Z]{2})$");
2929
Matcher matcher = localePattern.matcher(args[0]);
3030
if(matcher.matches()) {
3131
try {
@@ -34,6 +34,8 @@ public static void main(String[] args) {
3434
} catch (Exception se) {
3535
se.printStackTrace();
3636
}
37+
} else {
38+
System.err.println("Incorrect locale supplied: " + args[0]);
3739
}
3840
}
3941

src/me/theentropyshard/jdarkroom/SaveFileReader.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public static String findInSave(File file) {
3131

3232
StringBuilder russianCode = new StringBuilder();
3333
StringBuilder englishCode = new StringBuilder();
34+
String totalPlaytime = "00:00:00";
3435
try(RandomAccessFile raf = new RandomAccessFile(file, "r")) {
3536
if(raf.length() <= 0) {
3637
return null;
@@ -51,9 +52,7 @@ public static String findInSave(File file) {
5152
return null;
5253
}
5354

54-
String russianLabel = I18N.getString("codeLabelRu");
55-
String englishLabel = I18N.getString("codeLabelEn");
56-
String andText = I18N.getString("andText");
57-
return String.format("%s: %s %s %s: %s", russianLabel, russianCode, andText, englishLabel, englishCode);
55+
String resultLabel = I18N.getString("resultLabel");
56+
return String.format(resultLabel, russianCode, englishCode, totalPlaytime);
5857
}
5958
}

src/me/theentropyshard/jdarkroom/Utils.java

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,8 @@ public static BufferedImage loadImage(String path) {
3939
Utils.IMAGE_CACHE.put(path, bufferedImage);
4040
return bufferedImage;
4141
} catch (IOException e) {
42-
e.printStackTrace();
42+
throw new RuntimeException(e);
4343
}
44-
45-
return null;
4644
}
4745

4846
/**
@@ -70,10 +68,4 @@ public static void swapBits(int byteInd1, int byteInd2, int bi1, int bi2, byte..
7068
bytes[byteInd1] |= 1 << bi1;
7169
}
7270
}
73-
74-
/*public static final class StrIntBiMap extends HashMap<String, Integer> {
75-
public Integer getValueByKey(String key) {
76-
77-
}
78-
}*/
7971
}

src/me/theentropyshard/jdarkroom/View.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,7 @@ protected void paintComponent(Graphics g) {
223223
cardLayout.show(this, "dropFilePanel");
224224

225225
JFrame frame = new JFrame("JDarkroom");
226+
frame.setResizable(false);
226227
frame.add(this, BorderLayout.CENTER);
227228
frame.pack();
228229
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

0 commit comments

Comments
 (0)