Skip to content

Commit

Permalink
Archives: a bit more refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
ShadelessFox committed Mar 10, 2024
1 parent dcd6cbb commit 8d807aa
Show file tree
Hide file tree
Showing 10 changed files with 52 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -18,6 +19,9 @@ public interface Archive extends Closeable {
@NotNull
Path getPath();

@Nullable
ArchiveFile findFile(@NotNull String identifier);

@NotNull
ArchiveFile getFile(@NotNull String identifier);
}
Original file line number Diff line number Diff line change
@@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -79,19 +89,6 @@ public Packfile findFirst(long hash) {
.findAny().orElse(null);
}

@NotNull
public List<Packfile> findAll(@NotNull String path) {
return findAll(getPathHash(getNormalizedPath(path)));
}

@NotNull
public List<Packfile> 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()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -96,25 +96,23 @@ 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;
}

final Map<String, TextSchema> 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(
Expand Down Expand Up @@ -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<String, Map<String, TextSchema>> entry : file.files.entrySet()) {
for (Map.Entry<String, Map<String, TextSchema>> 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,14 +211,8 @@ public EntryPickerDialog(@NotNull String title, @NotNull Window window, @NotNull

final Optional<RTTICoreFile> 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);
}
Expand Down

0 comments on commit 8d807aa

Please sign in to comment.