<commit_before>/*
 ** 2014 September 28
 **
 ** The author disclaims copyright to this source code.  In place of
 ** a legal notice, here is a blessing:
 **    May you do good and not evil.
 **    May you find forgiveness for yourself and forgive others.
 **    May you share freely, never taking more than you give.
 */
package info.ata4.unity.gui.control;

import info.ata4.io.buffer.ByteBufferUtils;
import info.ata4.unity.assetbundle.AssetBundleUtils;
import info.ata4.unity.assetbundle.BufferedEntry;
import info.ata4.unity.gui.util.DialogUtils;
import info.ata4.unity.gui.util.progress.ProgressTask;
import info.ata4.unity.rtti.ObjectData;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.file.Files;
import java.nio.file.Path;
import javax.swing.JFileChooser;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreePath;

/**
 *
 * @author <NAME> <barracuda415 at yahoo.de>
 */
public class AssetFileTreePopup extends JPopupMenu {

    public AssetFileTreePopup(int selRow, TreePath selPath) {
        Object lastComponent = selPath.getLastPathComponent();
        if (!(lastComponent instanceof DefaultMutableTreeNode)) {
            return;
        }
        
        DefaultMutableTreeNode treeNode = (DefaultMutableTreeNode) lastComponent;
        
        Object userObject = treeNode.getUserObject();
        if (userObject instanceof ObjectData) {
            final ObjectData objectData = (ObjectData) userObject;
            JMenuItem item = new JMenuItem("Extract raw object data");
            item.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent evt) {
                    extractObjectData(objectData);
                }
            });
            add(item);
        } else if (userObject instanceof Path) {
            final Path file = (Path) userObject;
            if (AssetBundleUtils.isAssetBundle(file)) {
                JMenuItem item = new JMenuItem("Extract asset bundle");
                item.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent evt) {
                        extractAssetBundle(file);
                    }
                });
                add(item);
            }
        } else if (userObject instanceof BufferedEntry) {
            final BufferedEntry entry = (BufferedEntry) userObject;
            JMenuItem item = new JMenuItem("Extract file");
            item.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent evt) {
                    extractAssetBundleEntry(entry);
                }
            });
            add(item);
        } 
    }
    
    private void extractObjectData(ObjectData objectData) {
        JFileChooser fileChooser = new JFileChooser();
        fileChooser.setSelectedFile(new File(String.format("%s.bin", objectData.getName())));
        
        int result = fileChooser.showSaveDialog(this);
        if (result != JFileChooser.APPROVE_OPTION) {
            return;
        }
        
        Path file = fileChooser.getSelectedFile().toPath();
        ByteBuffer bb = objectData.getBuffer();
        bb.rewind();
        
        try {<commit_msg>Added menu to extract array buffers<commit_after>/*
 ** 2014 September 28
 **
 ** The author disclaims copyright to this source code.  In place of
 ** a legal notice, here is a blessing:
 **    May you do good and not evil.
 **    May you find forgiveness for yourself and forgive others.
 **    May you share freely, never taking more than you give.
 */
package info.ata4.unity.gui.control;

import info.ata4.io.buffer.ByteBufferUtils;
import info.ata4.unity.assetbundle.AssetBundleUtils;
import info.ata4.unity.assetbundle.BufferedEntry;
import info.ata4.unity.gui.util.DialogUtils;
import info.ata4.unity.gui.util.progress.ProgressTask;
import info.ata4.unity.rtti.FieldNode;
import info.ata4.unity.rtti.ObjectData;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.file.Files;
import java.nio.file.Path;
import javax.swing.JFileChooser;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreePath;

/**
 *
 * @author <NAME> <barracuda415 at yahoo.de>
 */
public class AssetFileTreePopup extends JPopupMenu {

    public AssetFileTreePopup(int selRow, TreePath selPath) {
        Object lastComponent = selPath.getLastPathComponent();
        if (!(lastComponent instanceof DefaultMutableTreeNode)) {
            return;
        }
        
        DefaultMutableTreeNode treeNode = (DefaultMutableTreeNode) lastComponent;
        
        Object userObject = treeNode.getUserObject();
        if (userObject instanceof ObjectData) {
            final ObjectData objectData = (ObjectData) userObject;
            JMenuItem item = new JMenuItem("Extract raw object data");
            item.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent evt) {
                    extractByteBuffer(objectData.getBuffer(), objectData.getName());
                }
            });
            add(item);
        } else if (userObject instanceof Path) {
            final Path file = (Path) userObject;
            if (AssetBundleUtils.isAssetBundle(file)) {
                JMenuItem item = new JMenuItem("Extract asset bundle");
                item.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent evt) {
                        extractAssetBundle(file);
                    }
                });
                add(item);
            }
        } else if (userObject instanceof BufferedEntry) {
            final BufferedEntry entry = (BufferedEntry) userObject;
            JMenuItem item = new JMenuItem("Extract file");
            item.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent evt) {
                    extractAssetBundleEntry(entry);
                }
            });
            add(item);
        } else if (userObject instanceof FieldNode) {
            final FieldNode fieldNode = (FieldNode) userObject;
            Object fieldValue = fieldNode.getValue();
            if (fieldValue instanceof ByteBuffer) {
                final ByteBuffer bb = (ByteBuffer) fieldValue;
                JMenuItem item = new JMenuItem("Extract array data");
                item.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent evt) {
                        extractByteBuffer(bb, fieldNode.getType().getFieldName());
                    }
                });
                add(item);
            }
        }
    }
    
    private void extractByteBuffer(ByteBuffer bb, String name) {
        JFileChooser fileChooser = new JFileChooser();
        fileChooser.setSelectedFile(new File(String.format("%s.bin", name)));
        
        int result = fileChooser.showSaveDialog(this);
        if (result != JFileChooser.APPROVE_OPTION) {
            return;
        }
        
        Path file = fileChooser.getSelectedFile().toPath();
        bb.rewind();
        
        try {
{'fraction_non_alphanumeric': 0.0888427068450448, 'fraction_numerical': 0.0042862709442784775, 'mean_word_length': 2.221757322175732, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7146971', 'n_tokens_mistral': 2030, 'n_tokens_neox': 1957, 'n_words': 485}