@return Map of file names to their raw data.
()
| 258 | * @return Map of file names to their raw data. |
| 259 | */ |
| 260 | public ListeningMap<String, byte[]> getFiles() { |
| 261 | synchronized(cachedFiles) { |
| 262 | try { |
| 263 | if (!cachedFiles.isBacked()) { |
| 264 | cachedFiles.setBacking(copyMap(loadFiles())); |
| 265 | // If this resource is not the primary resource, we are done |
| 266 | if (!isPrimary()) |
| 267 | return cachedFiles; |
| 268 | // Register listeners |
| 269 | cachedFiles.getPutListeners() |
| 270 | .add(InternalBiConsumer.internal((name, code) -> dirtyFiles.add(name))); |
| 271 | cachedFiles.getRemoveListeners().add(InternalConsumer.internal(dirtyFiles::remove)); |
| 272 | // Create initial save state |
| 273 | for (Map.Entry<String, byte[]> e : cachedFiles.entrySet()) { |
| 274 | addFileSave(e.getKey(), e.getValue()); |
| 275 | } |
| 276 | // Add listener to create initial save states for newly made files |
| 277 | cachedFiles.getPutListeners().add(InternalBiConsumer.internal((name, code) -> { |
| 278 | if (!cachedFiles.containsKey(name)) { |
| 279 | addFileSave(name, code); |
| 280 | } |
| 281 | })); |
| 282 | } |
| 283 | } catch(IOException ex) { |
| 284 | error(ex, "Failed to load files from resource \"{}\"", toString()); |
| 285 | cachedFiles.setBacking(Collections.emptyMap()); |
| 286 | } |
| 287 | } |
| 288 | return cachedFiles; |
| 289 | } |
| 290 | |
| 291 | /** |
| 292 | * Refresh this resource. |