index.js 3.61 KB
Newer Older
liujiangnan's avatar
liujiangnan committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
const PanelManager = require('./panel-manager');
const ConfigManager = require('../common/config-manager');
const EditorMainKit = require('../eazax/editor-main-kit');
const { checkUpdate, print, translate } = require('../eazax/editor-main-util');
const { openRepository } = require('../eazax/package-util');
const EditorAPI = require('./editor-api');
const Parser = require('./parser');
const Finder = require('./finder');
const Printer = require('./printer');

/**
 * 生命周期:加载
 */
function load() {
    // 监听事件
    EditorMainKit.register();
}

/**
 * 生命周期:卸载
 */
function unload() {
    // 取消事件监听
    EditorMainKit.unregister();
}

/**
 * 查找当前选中资源
 */
async function findCurrentSelection() {
    // 过滤选中的资源 uuid
    const uuids = EditorAPI.getCurrentSelectedAssets();
    for (let i = 0; i < uuids.length; i++) {
        const assetInfo = EditorAPI.assetInfoByUuid(uuids[i]);
        if (assetInfo.type === 'folder') {
            uuids.splice(i--);
        }
    }
    // 未选择资源
    if (uuids.length === 0) {
        print('log', translate('please-select-assets'));
        return;
    }
    // 遍历查找
    for (let i = 0; i < uuids.length; i++) {
        const uuid = uuids[i],
            assetInfo = EditorAPI.assetInfoByUuid(uuid),
            shortUrl = assetInfo.url.replace('db://', '');
        // 查找引用
        print('log', '🔍', `${translate('find-asset-refs')} ${shortUrl}`);
        const refs = await Finder.findByUuid(uuid);
        if (refs.length === 0) {
            print('log', '📂', `${translate('no-refs')} ${shortUrl}`);
            continue;
        }
        // 打印结果
        Printer.printResult({
            type: assetInfo.type,
            uuid: uuid,
            url: assetInfo.url,
            path: assetInfo.path,
            refs: refs,
        });
    }
}

function getSelection() {

}

/**
 * 资源变化回调
 * @param {{ type: string, uuid: string }} info 
 */
function onAssetChanged(info) {
    const { type, uuid } = info;
    // 场景和预制体
    if (type === 'scene' || type === 'prefab') {
        const { url, path } = EditorAPI.assetInfoByUuid(uuid);
        // 排除内置资源
        if (url.indexOf('db://internal') !== -1) {
            return;
        }
        // 更新节点树
        Parser.updateCache(path);
    }
}

module.exports = {

    /**
     * 扩展消息
     */
    messages: {

        /**
         * 查找当前选中资源
         * @param {*} event 
         */
        'find-current-selection'(event) {
            findCurrentSelection();
        },

        /**
         * 打开设置面板
         * @param {*} event 
         */
        'open-settings-panel'(event) {
            PanelManager.openSettingsPanel();
        },

        /**
         * 检查更新
         * @param {*} event 
         */
        'menu-check-update'(event) {
            checkUpdate(true);
        },

        /**
         * 版本
         * @param {*} event 
         */
        'menu-version'(event) {
            openRepository();
        },

        /**
         * 场景面板加载完成后
         * @param {*} event 
         */
        'scene:ready'(event) {
            // 自动检查更新
            const config = ConfigManager.get();
            if (config.autoCheckUpdate) {
                checkUpdate(false);
            }
        },

        /**
         * 资源变化
         * @param {*} event 
         * @param {{ type: string, uuid: string }} info 
         */
        'asset-db:asset-changed'(event, info) {
            onAssetChanged(info);
        },

    },

    load,

    unload,

};