diff --git a/modules/decima-model/src/main/java/com/shade/decima/model/app/Project.java b/modules/decima-model/src/main/java/com/shade/decima/model/app/Project.java index 6f72bd1ca..d5242731e 100644 --- a/modules/decima-model/src/main/java/com/shade/decima/model/app/Project.java +++ b/modules/decima-model/src/main/java/com/shade/decima/model/app/Project.java @@ -47,6 +47,7 @@ public class Project implements Closeable { mountDefaults(); } + // TODO: Should be specific to the archive manager, hence should be moved to the concrete implementation private void mountDefaults() throws IOException { final PackfileProvider packfileProvider = switch (container.getType()) { case DS, DSDC -> new DSPackfileProvider(); diff --git a/modules/decima-model/src/main/java/com/shade/decima/model/archive/Archive.java b/modules/decima-model/src/main/java/com/shade/decima/model/archive/Archive.java index 5f58bf459..9e0bf2a0f 100644 --- a/modules/decima-model/src/main/java/com/shade/decima/model/archive/Archive.java +++ b/modules/decima-model/src/main/java/com/shade/decima/model/archive/Archive.java @@ -1,6 +1,7 @@ package com.shade.decima.model.archive; import com.shade.util.NotNull; +import com.shade.util.Nullable; import java.io.Closeable; import java.nio.file.Path; @@ -18,6 +19,9 @@ public interface Archive extends Closeable { @NotNull Path getPath(); + @Nullable + ArchiveFile findFile(@NotNull String identifier); + @NotNull ArchiveFile getFile(@NotNull String identifier); } diff --git a/modules/decima-model/src/main/java/com/shade/decima/model/archive/ArchiveManager.java b/modules/decima-model/src/main/java/com/shade/decima/model/archive/ArchiveManager.java index 9068c4d9c..0b15716ba 100644 --- a/modules/decima-model/src/main/java/com/shade/decima/model/archive/ArchiveManager.java +++ b/modules/decima-model/src/main/java/com/shade/decima/model/archive/ArchiveManager.java @@ -1,11 +1,15 @@ package com.shade.decima.model.archive; import com.shade.util.NotNull; +import com.shade.util.Nullable; import java.io.Closeable; import java.util.Collection; public interface ArchiveManager extends Closeable { + @Nullable + ArchiveFile findFile(@NotNull String identifier); + @NotNull ArchiveFile getFile(@NotNull String identifier); diff --git a/modules/decima-model/src/main/java/com/shade/decima/model/packfile/Packfile.java b/modules/decima-model/src/main/java/com/shade/decima/model/packfile/Packfile.java index 32a761960..3894c0355 100644 --- a/modules/decima-model/src/main/java/com/shade/decima/model/packfile/Packfile.java +++ b/modules/decima-model/src/main/java/com/shade/decima/model/packfile/Packfile.java @@ -273,6 +273,16 @@ public String getName() { return info.name(); } + @Nullable + @Override + public ArchiveFile findFile(@NotNull String identifier) { + final FileEntry entry = getFileEntry(identifier); + if (entry == null) { + return null; + } + return new PackfileFile(this, entry); + } + @NotNull @Override public ArchiveFile getFile(@NotNull String identifier) { diff --git a/modules/decima-model/src/main/java/com/shade/decima/model/packfile/PackfileManager.java b/modules/decima-model/src/main/java/com/shade/decima/model/packfile/PackfileManager.java index 8002debc0..ad3e3b0b2 100644 --- a/modules/decima-model/src/main/java/com/shade/decima/model/packfile/PackfileManager.java +++ b/modules/decima-model/src/main/java/com/shade/decima/model/packfile/PackfileManager.java @@ -50,6 +50,16 @@ public Packfile openPackfile(@NotNull Path path) throws IOException { return new Packfile(this, oodle, new PackfileInfo(path, IOUtils.getBasename(path), null)); } + @Nullable + @Override + public ArchiveFile findFile(@NotNull String identifier) { + final Packfile archive = findFirst(identifier); + if (archive == null) { + return null; + } + return archive.getFile(identifier); + } + @NotNull @Override public ArchiveFile getFile(@NotNull String identifier) { @@ -79,19 +89,6 @@ public Packfile findFirst(long hash) { .findAny().orElse(null); } - @NotNull - public List findAll(@NotNull String path) { - return findAll(getPathHash(getNormalizedPath(path))); - } - - @NotNull - public List findAll(long hash) { - // Process in descending order, so patch packfile will be first (as it has the highest priority), if present - return packfiles.descendingSet().stream() - .filter(x -> x.getFileEntry(hash) != null) - .toList(); - } - public boolean hasChanges() { for (Packfile packfile : packfiles) { if (packfile.hasChanges()) { diff --git a/modules/decima-model/src/main/java/com/shade/decima/model/rtti/objects/RTTIReference.java b/modules/decima-model/src/main/java/com/shade/decima/model/rtti/objects/RTTIReference.java index c9e8adeb1..4bfc40ae2 100644 --- a/modules/decima-model/src/main/java/com/shade/decima/model/rtti/objects/RTTIReference.java +++ b/modules/decima-model/src/main/java/com/shade/decima/model/rtti/objects/RTTIReference.java @@ -2,7 +2,6 @@ import com.shade.decima.model.app.Project; import com.shade.decima.model.archive.ArchiveFile; -import com.shade.decima.model.packfile.Packfile; import com.shade.decima.model.rtti.RTTICoreFile; import com.shade.decima.model.rtti.RTTIUtils; import com.shade.util.NotNull; @@ -31,13 +30,7 @@ record External(@NotNull Kind kind, @NotNull RTTIObject uuid, @NotNull String pa @NotNull @Override public FollowResult follow(@NotNull Project project, @NotNull RTTICoreFile current) throws IOException { - final Packfile packfile = project.getPackfileManager().findFirst(path); - - if (packfile == null) { - throw new IOException("Couldn't find referenced file: " + path); - } - - final ArchiveFile file = packfile.getFile(path); + final ArchiveFile file = project.getPackfileManager().getFile(path); final RTTICoreFile core = project.getCoreFileReader().read(file, true); return Internal.follow(core, uuid); } diff --git a/modules/decima-model/src/main/java/com/shade/decima/model/rtti/types/ds/DSDataSource.java b/modules/decima-model/src/main/java/com/shade/decima/model/rtti/types/ds/DSDataSource.java index b4b90e639..f68333c4d 100644 --- a/modules/decima-model/src/main/java/com/shade/decima/model/rtti/types/ds/DSDataSource.java +++ b/modules/decima-model/src/main/java/com/shade/decima/model/rtti/types/ds/DSDataSource.java @@ -1,6 +1,6 @@ package com.shade.decima.model.rtti.types.ds; -import com.shade.decima.model.packfile.Packfile; +import com.shade.decima.model.archive.ArchiveFile; import com.shade.decima.model.packfile.PackfileManager; import com.shade.decima.model.rtti.RTTIClass; import com.shade.decima.model.rtti.Type; @@ -65,14 +65,8 @@ public byte[] getData(@NotNull PackfileManager manager) throws IOException { @NotNull @Override public byte[] getData(@NotNull PackfileManager manager, int offset, int length) throws IOException { - final String path = "%s.core.stream".formatted(location); - final Packfile packfile = manager.findFirst(path); - - if (packfile == null) { - throw new IOException("Can't find packfile that contains " + path); - } - - try (InputStream is = packfile.newInputStream(path)) { + final ArchiveFile file = manager.getFile("%s.core.stream".formatted(location)); + try (InputStream is = file.newInputStream()) { if (offset > 0) { is.skipNBytes(offset); } diff --git a/modules/decima-model/src/main/java/com/shade/decima/model/rtti/types/hzd/HZDDataSource.java b/modules/decima-model/src/main/java/com/shade/decima/model/rtti/types/hzd/HZDDataSource.java index d80f9c703..6f4000826 100644 --- a/modules/decima-model/src/main/java/com/shade/decima/model/rtti/types/hzd/HZDDataSource.java +++ b/modules/decima-model/src/main/java/com/shade/decima/model/rtti/types/hzd/HZDDataSource.java @@ -1,6 +1,6 @@ package com.shade.decima.model.rtti.types.hzd; -import com.shade.decima.model.packfile.Packfile; +import com.shade.decima.model.archive.ArchiveFile; import com.shade.decima.model.packfile.PackfileManager; import com.shade.decima.model.rtti.Type; import com.shade.decima.model.rtti.objects.RTTIObject; @@ -61,14 +61,8 @@ public byte[] getData(@NotNull PackfileManager manager, int offset, int length) } // TODO: Should prefix removal be handled by the manager itself? - final String path = location.substring(6); - final Packfile packfile = manager.findFirst(path); - - if (packfile == null) { - throw new IOException("Can't find packfile that contains " + path); - } - - try (InputStream is = packfile.newInputStream(path)) { + final ArchiveFile file = manager.getFile(location.substring(6)); + try (InputStream is = file.newInputStream()) { if (offset > 0) { is.skipNBytes(offset); } diff --git a/modules/decima-ui/src/main/java/com/shade/decima/cli/commands/Localization.java b/modules/decima-ui/src/main/java/com/shade/decima/cli/commands/Localization.java index 4eca172a0..85769f8bb 100644 --- a/modules/decima-ui/src/main/java/com/shade/decima/cli/commands/Localization.java +++ b/modules/decima-ui/src/main/java/com/shade/decima/cli/commands/Localization.java @@ -6,7 +6,7 @@ import com.google.gson.stream.JsonReader; import com.google.gson.stream.JsonWriter; import com.shade.decima.model.app.Project; -import com.shade.decima.model.packfile.Packfile; +import com.shade.decima.model.archive.ArchiveFile; import com.shade.decima.model.packfile.PackfileManager; import com.shade.decima.model.rtti.RTTICoreFile; import com.shade.decima.model.rtti.RTTIEnum; @@ -96,17 +96,15 @@ private FileSchema toSchema( final String path = paths[i]; log.info("[{}/{}] Exporting {}", i + 1, paths.length, path); - final Packfile packfile = packfileManager.findFirst(path); - - if (packfile == null) { - log.warn("Can't find packfile for localization file '{}'", path); + final ArchiveFile file = packfileManager.findFile(path); + if (file == null) { + log.warn("Can't find localization file '{}'", path); continue; } - final RTTICoreFile file; - + final RTTICoreFile core; try { - file = project.getCoreFileReader().read(packfile.getFile(path), false); + core = project.getCoreFileReader().read(file, false); } catch (Exception e) { log.warn("Unable to read '{}': {}", path, e.getMessage()); continue; @@ -114,7 +112,7 @@ private FileSchema toSchema( final Map texts = new LinkedHashMap<>(); - file.visitAllObjects("LocalizedTextResource", object -> { + core.visitAllObjects("LocalizedTextResource", object -> { final HwLocalizedText text = object.obj("Data").cast(); final String uuid = RTTIUtils.uuidToString(object.uuid()); final TextSchema schema = new TextSchema( @@ -176,30 +174,28 @@ public Void call() throws Exception { log.info("Importing localization data from {}", input); try (Reader reader = Files.newBufferedReader(input, StandardCharsets.UTF_8)) { - final FileSchema file = gson.fromJson(reader, FileSchema.class); + final FileSchema schema = gson.fromJson(reader, FileSchema.class); final RTTIEnum languages = typeRegistry.find("ELanguage"); - final RTTIEnum.Constant sourceLanguage = languages.valueOf(file.source); - final RTTIEnum.Constant targetLanguage = languages.valueOf(file.target); + final RTTIEnum.Constant sourceLanguage = languages.valueOf(schema.source); + final RTTIEnum.Constant targetLanguage = languages.valueOf(schema.target); log.info("Source language: {}", sourceLanguage); log.info("Target language: {}", targetLanguage); - for (Map.Entry> entry : file.files.entrySet()) { + for (Map.Entry> entry : schema.files.entrySet()) { final String path = entry.getKey(); - final Packfile packfile = packfileManager.findFirst(path); - log.info("Reading {}", path); - if (packfile == null) { - log.warn("Can't find packfile for localization file '{}'", path); + final ArchiveFile file = packfileManager.findFile(path); + if (file == null) { + log.warn("Can't find localization file '{}'", path); continue; } final RTTICoreFile core; - try { - core = project.getCoreFileReader().read(packfile.getFile(path), false); + core = project.getCoreFileReader().read(file, false); } catch (Exception e) { log.warn("Unable to read '{}': {}", path, e.getMessage()); continue; diff --git a/modules/decima-ui/src/main/java/com/shade/decima/ui/data/editors/ReferenceValueEditor.java b/modules/decima-ui/src/main/java/com/shade/decima/ui/data/editors/ReferenceValueEditor.java index d2b91800d..1740cf9a1 100644 --- a/modules/decima-ui/src/main/java/com/shade/decima/ui/data/editors/ReferenceValueEditor.java +++ b/modules/decima-ui/src/main/java/com/shade/decima/ui/data/editors/ReferenceValueEditor.java @@ -211,14 +211,8 @@ public EntryPickerDialog(@NotNull String title, @NotNull Window window, @NotNull final Optional result = ProgressDialog.showProgressDialog(window, "Enumerate entries", monitor -> { try (ProgressMonitor.IndeterminateTask ignored = monitor.begin("Read core file")) { - final Packfile packfile = project.getPackfileManager().findFirst(path); - - if (packfile == null) { - throw new IllegalStateException("Can't find packfile containing the target file"); - } - try { - return project.getCoreFileReader().read(packfile.getFile(path), true); + return project.getCoreFileReader().read(project.getPackfileManager().getFile(path), true); } catch (IOException e) { throw new UncheckedIOException(e); }