Skip to content

Commit

Permalink
RTTI: Delegate core object lookup to the reference itself
Browse files Browse the repository at this point in the history
  • Loading branch information
ShadelessFox committed Mar 2, 2024
1 parent f43e078 commit 852d3df
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -92,18 +92,6 @@ public byte[] write(@NotNull RTTICoreFile file) {
}
}

@Nullable
@Override
public RTTIObject findObject(@NotNull Predicate<RTTIObject> predicate) {
for (RTTIObject object : objects) {
if (predicate.test(object)) {
return object;
}
}

return null;
}

@Override
public <T> void visitAllObjects(@NotNull Class<T> type, @NotNull Consumer<T> consumer) {
visitAllObjects(type::isInstance, consumer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,14 @@

import com.shade.decima.model.rtti.objects.RTTIObject;
import com.shade.util.NotNull;
import com.shade.util.Nullable;

import java.util.List;
import java.util.function.Consumer;
import java.util.function.Predicate;

public interface RTTICoreFile {
@NotNull
List<RTTIObject> objects();

@Nullable
RTTIObject findObject(@NotNull Predicate<RTTIObject> predicate);

void visitAllObjects(@NotNull String type, @NotNull Consumer<RTTIObject> consumer);

<T> void visitAllObjects(@NotNull Class<T> type, @NotNull Consumer<T> consumer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,27 +39,31 @@ public FollowResult follow(@NotNull Project project, @NotNull RTTICoreFile curre

final ArchiveFile file = packfile.getFile(path);
final RTTICoreFile core = project.getCoreFileReader().read(file, true);
final RTTIObject object = core.findObject(obj -> obj.uuid().equals(uuid));

if (object == null) {
throw new IOException("Couldn't find referenced entry: " + RTTIUtils.uuidToString(uuid));
}

return new FollowResult(core, object);
return Internal.follow(core, uuid);
}
}

record Internal(@NotNull Kind kind, @NotNull RTTIObject uuid) implements RTTIReference {
@NotNull
@Override
public FollowResult follow(@NotNull Project project, @NotNull RTTICoreFile current) throws IOException {
final RTTIObject object = current.findObject(obj -> obj.uuid().equals(uuid));
return follow(current);
}

if (object == null) {
throw new IOException("Couldn't find referenced entry: " + RTTIUtils.uuidToString(uuid));
@NotNull
public FollowResult follow(@NotNull RTTICoreFile current) throws IOException {
return follow(current, uuid);
}

@NotNull
private static FollowResult follow(@NotNull RTTICoreFile current, @NotNull RTTIObject uuid) throws IOException {
for (RTTIObject object : current.objects()) {
if (object.uuid().equals(uuid)) {
return new FollowResult(current, object);
}
}

return new FollowResult(current, object);
throw new IOException("Couldn't find referenced object: " + RTTIUtils.uuidToString(uuid));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.shade.decima.model.rtti.RTTIClass;
import com.shade.decima.model.rtti.RTTICoreFile;
import com.shade.decima.model.rtti.RTTIUtils;
import com.shade.decima.model.rtti.objects.RTTIObject;
import com.shade.decima.model.rtti.objects.RTTIReference;
import com.shade.decima.model.rtti.path.RTTIPath;
Expand All @@ -24,6 +25,8 @@
import com.shade.platform.ui.menus.MenuItemRegistration;
import com.shade.util.NotNull;
import com.shade.util.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.swing.*;
import java.awt.*;
Expand All @@ -35,6 +38,8 @@

@MenuItemRegistration(parent = CTX_MENU_CORE_EDITOR_ID, name = "Show &Graph", keystroke = "ctrl alt U", group = CTX_MENU_CORE_EDITOR_GROUP_GENERAL, order = 3000)
public class ShowGraphItem extends MenuItem {
private static final Logger log = LoggerFactory.getLogger(ShowGraphItem.class);

@Override
public void perform(@NotNull MenuItemContext ctx) {
final CoreEditor editor = (CoreEditor) ctx.getData(PlatformDataKeys.EDITOR_KEY);
Expand Down Expand Up @@ -151,10 +156,12 @@ private static void buildGraph(@NotNull RTTICoreFile file, @NotNull RTTIObject s
buildGraph(file, source, element, graph);
}
} else if (object instanceof RTTIReference.Internal ref) {
final RTTIObject target = file.findObject(obj -> obj.uuid().equals(ref.uuid()));
if (target != null) {
try {
final RTTIObject target = ref.follow(file).object();
graph.addVertex(target);
graph.addEdge(source, target);
} catch (Exception e) {
log.warn("Can't find referenced local object: {}", RTTIUtils.uuidToString(ref.uuid()), e);
}
}
}
Expand Down

0 comments on commit 852d3df

Please sign in to comment.