From 2025e371e6440347e8e77a09417a6e51bf20061c Mon Sep 17 00:00:00 2001 From: TheEntropyShard Date: Sat, 11 Feb 2023 16:48:15 +0300 Subject: [PATCH] Code improvements, now decoding works properly --- src/me/theentropyshard/jdarkroom/Decoder.java | 36 ++++++++++++------- .../jdarkroom/DecodingData.java | 5 +++ src/me/theentropyshard/jdarkroom/I18N.java | 13 ++----- .../theentropyshard/jdarkroom/JDarkroom.java | 11 ------ src/me/theentropyshard/jdarkroom/Main.java | 4 ++- .../jdarkroom/SaveFileReader.java | 7 ++-- src/me/theentropyshard/jdarkroom/Utils.java | 10 +----- src/me/theentropyshard/jdarkroom/View.java | 1 + 8 files changed, 38 insertions(+), 49 deletions(-) diff --git a/src/me/theentropyshard/jdarkroom/Decoder.java b/src/me/theentropyshard/jdarkroom/Decoder.java index 731cee4..7349c65 100644 --- a/src/me/theentropyshard/jdarkroom/Decoder.java +++ b/src/me/theentropyshard/jdarkroom/Decoder.java @@ -63,22 +63,29 @@ public enum Decoder { 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; - intCode |= (bytes[8] & (0x1E000000 >> 25)) << 25; - intCode |= (bytes[9] & (0x01000000 >> 19)) << 19; - intCode |= (bytes[9] & (0x00001F00 >> 8)) << 8; + intCode |= (bytes[8] & (0x1E000000 >> 25)) << 25; + intCode |= (bytes[9] & (0x01000000 >> 19)) << 19; + intCode |= (bytes[9] & (0x00001F00 >> 8)) << 8; intCode |= (bytes[10] & (0x001F0000 >> 14)) << 14; - intCode |= (bytes[10] & (0x0000000C >> 2)) << 2; - intCode |= (bytes[11] & (0x00000003 << 4)) >> 4; + intCode |= (bytes[10] & (0x0000000C >> 2)) << 2; + intCode |= (bytes[11] & (0x00000003 << 4)) >> 4; int l1 = (intCode >> 24) & 0xFF; int d1 = (intCode >> 16) & 0xFF; - int l2 = (intCode >> 8) & 0xFF; - int d2 = (intCode >> 0) & 0xFF; + int l2 = (intCode >> 8) & 0xFF; + int d2 = (intCode >> 0) & 0xFF; String[] letters1 = DecodingData.BYTES_TO_CODE.get(Integer.valueOf(l1).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 englishCode = letters1[0] + d1 + letters2[0] + d2; - // I could put this in PRSF (private static final), but it might be not initialized yet - 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); + return russianCode + "_" + 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]); } } diff --git a/src/me/theentropyshard/jdarkroom/DecodingData.java b/src/me/theentropyshard/jdarkroom/DecodingData.java index 58b7656..1c6eee0 100644 --- a/src/me/theentropyshard/jdarkroom/DecodingData.java +++ b/src/me/theentropyshard/jdarkroom/DecodingData.java @@ -29,6 +29,11 @@ public enum DecodingData { */ 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 */ diff --git a/src/me/theentropyshard/jdarkroom/I18N.java b/src/me/theentropyshard/jdarkroom/I18N.java index d05951f..932739e 100644 --- a/src/me/theentropyshard/jdarkroom/I18N.java +++ b/src/me/theentropyshard/jdarkroom/I18N.java @@ -45,20 +45,11 @@ public enum I18N { I18N.TRANSLATION.put("en.badFile", "Bad file given!"); I18N.TRANSLATION.put("ru.badFile", "Перемещен плохой файл!"); - I18N.TRANSLATION.put("ru.andText", "и"); - I18N.TRANSLATION.put("en.andText", "and"); - - I18N.TRANSLATION.put("ru.codeLabelRu", "Русский код"); - I18N.TRANSLATION.put("ru.codeLabelEn", "Английский код"); - - I18N.TRANSLATION.put("en.codeLabelRu", "Russian code"); - I18N.TRANSLATION.put("en.codeLabelEn", "English code"); + I18N.TRANSLATION.put("en.resultLabel", "Russian code: %s and English code: %s
Total time played: %s"); + I18N.TRANSLATION.put("ru.resultLabel", "Русский код: %s и Английский код: %s
Всего времени отыграно: %s"); } public static String getString(String key) { - /*if(key.indexOf(".") == 2) { - return I18N.TRANSLATION.get(key); - }*/ return I18N.TRANSLATION.get(I18N.LANGUAGE + "." + key); } } diff --git a/src/me/theentropyshard/jdarkroom/JDarkroom.java b/src/me/theentropyshard/jdarkroom/JDarkroom.java index 7d54060..6a2c68f 100644 --- a/src/me/theentropyshard/jdarkroom/JDarkroom.java +++ b/src/me/theentropyshard/jdarkroom/JDarkroom.java @@ -19,17 +19,6 @@ package me.theentropyshard.jdarkroom; public class JDarkroom { public JDarkroom() { - if(instance != null) { - throw new IllegalStateException("JDarkroom already running!"); - } - instance = this; - new View(); } - - private static JDarkroom instance; - - public static JDarkroom getInstance() { - return instance; - } } diff --git a/src/me/theentropyshard/jdarkroom/Main.java b/src/me/theentropyshard/jdarkroom/Main.java index de02383..e4fb0d3 100644 --- a/src/me/theentropyshard/jdarkroom/Main.java +++ b/src/me/theentropyshard/jdarkroom/Main.java @@ -24,8 +24,8 @@ import java.util.regex.Pattern; public class Main { public static void main(String[] args) { - Pattern localePattern = Pattern.compile("([a-z]{2})-([A-Z]{2})$"); if(args.length > 0) { + Pattern localePattern = Pattern.compile("([a-z]{2})-([A-Z]{2})$"); Matcher matcher = localePattern.matcher(args[0]); if(matcher.matches()) { try { @@ -34,6 +34,8 @@ public class Main { } catch (Exception se) { se.printStackTrace(); } + } else { + System.err.println("Incorrect locale supplied: " + args[0]); } } diff --git a/src/me/theentropyshard/jdarkroom/SaveFileReader.java b/src/me/theentropyshard/jdarkroom/SaveFileReader.java index c3c45e6..38dd9ee 100644 --- a/src/me/theentropyshard/jdarkroom/SaveFileReader.java +++ b/src/me/theentropyshard/jdarkroom/SaveFileReader.java @@ -31,6 +31,7 @@ public enum SaveFileReader { StringBuilder russianCode = new StringBuilder(); StringBuilder englishCode = new StringBuilder(); + String totalPlaytime = "00:00:00"; try(RandomAccessFile raf = new RandomAccessFile(file, "r")) { if(raf.length() <= 0) { return null; @@ -51,9 +52,7 @@ public enum SaveFileReader { return null; } - String russianLabel = I18N.getString("codeLabelRu"); - String englishLabel = I18N.getString("codeLabelEn"); - String andText = I18N.getString("andText"); - return String.format("%s: %s %s %s: %s", russianLabel, russianCode, andText, englishLabel, englishCode); + String resultLabel = I18N.getString("resultLabel"); + return String.format(resultLabel, russianCode, englishCode, totalPlaytime); } } diff --git a/src/me/theentropyshard/jdarkroom/Utils.java b/src/me/theentropyshard/jdarkroom/Utils.java index e4892d8..e8b72b4 100644 --- a/src/me/theentropyshard/jdarkroom/Utils.java +++ b/src/me/theentropyshard/jdarkroom/Utils.java @@ -39,10 +39,8 @@ public enum Utils { Utils.IMAGE_CACHE.put(path, bufferedImage); return bufferedImage; } catch (IOException e) { - e.printStackTrace(); + throw new RuntimeException(e); } - - return null; } /** @@ -70,10 +68,4 @@ public enum Utils { bytes[byteInd1] |= 1 << bi1; } } - - /*public static final class StrIntBiMap extends HashMap { - public Integer getValueByKey(String key) { - - } - }*/ } diff --git a/src/me/theentropyshard/jdarkroom/View.java b/src/me/theentropyshard/jdarkroom/View.java index 4e294e6..4d1e0dd 100644 --- a/src/me/theentropyshard/jdarkroom/View.java +++ b/src/me/theentropyshard/jdarkroom/View.java @@ -223,6 +223,7 @@ public final class View extends JPanel { cardLayout.show(this, "dropFilePanel"); JFrame frame = new JFrame("JDarkroom"); + frame.setResizable(false); frame.add(this, BorderLayout.CENTER); frame.pack(); frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);