Skip to content

Commit 2e5afca

Browse files
committed
Add HashStore dump functions to facilitate debugging
1 parent b316697 commit 2e5afca

File tree

2 files changed

+38
-21
lines changed

2 files changed

+38
-21
lines changed

hash-utils/src/main/java/net/minecraftforge/util/hash/HashStore.java

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -232,17 +232,45 @@ public void save() {
232232
save(this.target);
233233
}
234234

235-
public void save(File file) {
236-
ArrayList<String> keys = new ArrayList<>(this.newHashes.keySet());
235+
/**
236+
* Returns a String representation of the old hashes, as if they were written to disc.
237+
* This is exposed for debugging purposes only
238+
*/
239+
public String dumpOld() {
240+
return dump(this.oldHashes, false);
241+
}
242+
243+
/**
244+
* Returns a String representation of the old hashes, as if they were written to disc.
245+
* This is exposed for debugging purposes only
246+
*/
247+
public String dump() {
248+
return dump(this.newHashes, false);
249+
}
250+
251+
private static String dump(HashMap<String, String> data, boolean newLine) {
252+
if (data.isEmpty())
253+
return "";
254+
255+
ArrayList<String> keys = new ArrayList<>(data.keySet());
237256
keys.sort(null);
238257
StringBuilder buf = new StringBuilder((keys.size() + 2) * 64); // rough estimate of size
239258

240-
for (String key : keys)
241-
buf.append(key).append('=').append(this.newHashes.get(key)).append('\n');
259+
for (String key : keys) {
260+
if (buf.length() > 0)
261+
buf.append('\n');
262+
buf.append(key).append('=').append(data.get(key));
263+
}
242264

265+
if (newLine)
266+
buf.append('\n');
267+
return buf.toString();
268+
}
269+
270+
public void save(File file) {
243271
try {
244272
Files.createDirectories(file.getParentFile().toPath());
245-
Files.write(file.toPath(), buf.toString().getBytes(StandardCharsets.UTF_8));
273+
Files.write(file.toPath(), dump(this.newHashes, true).getBytes(StandardCharsets.UTF_8));
246274
this.saved = true;
247275
} catch (IOException e) {
248276
sneak(e);

hash-utils/src/test/java/net/minecraftforge/util/hash/HashStoreTest.java

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,30 +22,19 @@ public class HashStoreTest {
2222
private static void isSame(HashStore cache, File file) throws IOException {
2323
if (cache.isSame())
2424
return;
25-
System.out.println("Original: " + read(file));
26-
cache.save();
27-
System.out.println("Modified: " + read(file));
25+
System.out.println("Original: " + cache.dumpOld());
26+
System.out.println("Modified: " + cache.dump());
2827
Assertions.assertTrue(cache.isSame());
2928
}
3029

3130
private static void isNotSame(HashStore cache, File file) throws IOException {
32-
if (!cache.isSame())
31+
if (cache.isSame())
3332
return;
34-
System.out.println("Original: " + read(file));
35-
cache.save();
36-
System.out.println("Modified: " + read(file));
33+
System.out.println("Original: " + cache.dumpOld());
34+
System.out.println("Modified: " + cache.dump());
3735
Assertions.assertFalse(cache.isSame());
3836
}
3937

40-
private static String read(File file) throws IOException {
41-
if (file.isDirectory())
42-
file = new File(file, ".cache");
43-
else
44-
file = new File(file.getAbsolutePath() + ".cache");
45-
46-
return String.join("\n", Files.readAllLines(file.toPath()));
47-
}
48-
4938
@Test
5039
public void invalidate() throws IOException {
5140
File file = File.createTempFile("hash-store-test", ".dat");

0 commit comments

Comments
 (0)