5
5
package net .minecraftforge .util .hash ;
6
6
7
7
import net .minecraftforge .util .logging .Log ;
8
+ import org .jetbrains .annotations .NotNullByDefault ;
9
+ import org .jetbrains .annotations .Nullable ;
8
10
9
11
import java .io .File ;
10
12
import java .io .IOException ;
14
16
import java .util .Collections ;
15
17
import java .util .HashMap ;
16
18
import java .util .Map ;
19
+ import java .util .Objects ;
17
20
18
21
import static net .minecraftforge .util .hash .HashUtils .sneak ;
19
22
23
+ @ NotNullByDefault
20
24
public class HashStore {
21
- private final HashFunction HASH = HashFunction .SHA1 ;
25
+ private static final HashFunction HASH = HashFunction .SHA1 ;
22
26
23
27
private final String root ;
24
28
private final Map <String , String > oldHashes = new HashMap <>();
25
29
private final Map <String , String > newHashes = new HashMap <>();
26
- private File target ;
30
+ private @ Nullable File target ;
27
31
28
32
public static HashStore fromFile (File path ) {
29
33
File parent = path .getAbsoluteFile ().getParentFile ();
@@ -37,11 +41,15 @@ public static HashStore fromDir(File path) {
37
41
}
38
42
39
43
public HashStore () {
40
- this . root = "" ;
44
+ this ( "" ) ;
41
45
}
42
46
43
47
public HashStore (File root ) {
44
- this .root = root .getAbsolutePath ();
48
+ this (root .getAbsolutePath ());
49
+ }
50
+
51
+ private HashStore (String root ) {
52
+ this .root = root ;
45
53
}
46
54
47
55
public boolean areSame (File ... files ) {
@@ -103,16 +111,20 @@ public boolean exists() {
103
111
}
104
112
105
113
public HashStore add (String key , String data ) {
106
- newHashes .put (key , HASH .hash (data ));
114
+ if (!data .isEmpty ())
115
+ newHashes .put (Objects .requireNonNull (key ), HASH .hash (data ));
107
116
return this ;
108
117
}
109
118
110
119
public HashStore add (String key , byte [] data ) {
111
- newHashes .put (key , HASH .hash (data ));
120
+ if (data .length > 0 )
121
+ this .newHashes .put (Objects .requireNonNull (key ), HASH .hash (data ));
112
122
return this ;
113
123
}
114
124
115
- public HashStore add (String key , File file ) {
125
+ public HashStore add (@ Nullable String key , File file ) {
126
+ if (!file .exists ()) return this ;
127
+
116
128
try {
117
129
if (key == null )
118
130
key = getPath (file );
@@ -121,10 +133,10 @@ public HashStore add(String key, File file) {
121
133
String prefix = getPath (file );
122
134
for (File f : HashUtils .listFiles (file )) {
123
135
String suffix = getPath (f ).substring (prefix .length ());
124
- newHashes .put (key + " - " + suffix , HASH .hash (f ));
136
+ this . newHashes .put (key + " - " + suffix , HASH .hash (f ));
125
137
}
126
138
} else {
127
- newHashes .put (key , HASH .hash (file ));
139
+ this . newHashes .put (key , HASH .hash (file ));
128
140
}
129
141
} catch (IOException e ) {
130
142
throw new RuntimeException (e );
@@ -150,22 +162,27 @@ public HashStore add(File file) {
150
162
}
151
163
152
164
public boolean isSame () {
153
- return oldHashes .equals (newHashes );
165
+ return this .oldHashes .equals (this .newHashes );
166
+ }
167
+
168
+ /** Clears the new hashes, does not clear the old hashes read from {@link #load(File)}. */
169
+ public void clear () {
170
+ this .newHashes .clear ();
154
171
}
155
172
156
173
public void save () {
157
- if (target == null )
174
+ if (this . target == null )
158
175
throw new RuntimeException ("HashStore.save() called without load(File) so we dont know where to save it! Use load(File) or save(File)" );
159
- save (target );
176
+ save (this . target );
160
177
}
161
178
162
179
public void save (File file ) {
163
180
StringBuilder buf = new StringBuilder ();
164
- ArrayList <String > keys = new ArrayList <String >( newHashes .keySet ());
181
+ ArrayList <String > keys = new ArrayList <>( this . newHashes .keySet ());
165
182
Collections .sort (keys );
166
183
167
184
for (String key : keys )
168
- buf .append (key ).append ('=' ).append (newHashes .get (key )).append ('\n' );
185
+ buf .append (key ).append ('=' ).append (this . newHashes .get (key )).append ('\n' );
169
186
170
187
try {
171
188
Files .write (file .toPath (), buf .toString ().getBytes (StandardCharsets .UTF_8 ));
@@ -177,8 +194,8 @@ public void save(File file) {
177
194
private String getPath (File file ) {
178
195
String path = file .getAbsolutePath ();
179
196
180
- if (path .startsWith (root ))
181
- path = path .substring (root .length ());
197
+ if (path .startsWith (this . root ))
198
+ path = path .substring (this . root .length ());
182
199
183
200
path = path .replace ('\\' , '/' );
184
201
0 commit comments