cocos2D插件转3D插件

cocos2D插件转3D插件

javascript 复制代码
'use strict';

/**

 * 3d插件api映射,兼容2d插件

 * */



let fs = require("fs");

let path = require("path");

let baseDir = '';

const prsPath = (Editor.Project && Editor.Project.path ? Editor.Project.path : Editor.remote.projectPath).replace(/\\/g,'/');



// 2D 映射到 3D编辑器的命令

let MAP_CMD = {

    'scene:enter-prefab-edit-mode'  :'scene:open-scene',

    'scene:open-by-uuid'            :'scene:open-scene',

    'assets:hint'                   : "twinkle", // 3d里高亮资源命令是 'twinkle'

    'hint'                          : "hint"           // 高亮选中的节点,未找到映射关系

}



// 模拟creator2d插件的API实现

let Editor2D =

{

    isWin32 : path.sep == '\\',

    appPath : Editor.App.path,

   

    error : console.error,

    log : console.log,

    warn : console.warn,

    info : console.info,

   

    _getUrlLast(url_path,head){

        let p_i = url_path.indexOf(head);

        if(p_i!=-1){

            return url_path.substr(head.length+2);

        }

    },



    url(url_path){

        let absPath = Editor._getUrlLast(url_path,'packages:');

        if(absPath){

            return path.join(baseDir,absPath)

        }



        absPath = Editor._getUrlLast(url_path,'db:');

        if(absPath ) {

            return path.join(prsPath,absPath)

        }

        return url_path;

    },



    require(url){

        url = Editor2D.url(url);

        return require(url);

    },



    Ipc : {



        sendToPanel: (head,cmd,...args)=>Editor2D.Ipc._send(head,cmd,...args),



        sendToAll:(cmd,...args)=>Editor2D.Ipc.sendToMain(cmd,...args),



        sendToMain:(cmd,...args)=>

        {

            cmd = MAP_CMD[cmd] || cmd;

            let temp = cmd.split(':')

            if(temp[1]){

                Editor2D.Ipc._send(temp[0],temp[1] || "",...args);

            }else{

                Editor.Message.send(cmd,...args);

            }

        },



        _send(head,cmd,...args)

        {

            let callback = arguments[arguments.length-1]

            Editor.Message.request(head,cmd,...args).then((v)=>{

                if(typeof callback == 'function'){

                    callback(null,v);

                    callback = null;

                }

            },()=>{

                if(typeof callback == 'function'){

                    callback('run _send error');

                    callback = null;

                }

            })

        }

    },



    Scene :{

        callSceneScript:(head,cmd,...args)=>{

            let info = {

                name: 'scene',

                method: cmd,

                args:args,

            }

            let callback = arguments[arguments.length-1]

            Editor2D.Ipc._send('scene','execute-scene-script',info,(callback instanceof Function) ? callback : null);

        },

    },

   

    assetdb:{

        assetBackupPath : path.join(prsPath,'temp','BackupAssets'),



        async urlToUuid(url){

            return await Editor.Message.request("asset-db",'query-uuid',url);

        },



        async uuidToUrl(uuid){

            return await Editor.Message.request("asset-db",'query-url',uuid);

        },



        async urlToFspath(uuidOrUrl){

            return await Editor.Message.request("asset-db",'query-path',uuidOrUrl);

        },



        async uuidToFspath(uuidOrUrl){

            return await Editor.Message.request("asset-db",'query-path',uuidOrUrl);

        },



        async fspathToUuid(fsPath){

            let url = "db://" + fsPath.replace(/\\/g,'/').replace(prsPath,'').substr(6);

            return await Editor.Message.request("asset-db",'query-uuid',url);

        },



        async existsByUuid(urlOrUUID){

            return await Editor.Message.request("asset-db",'query-asset-info',urlOrUUID);

        },

       

        async existsByUrl(urlOrUUID){

            return await Editor.Message.request("asset-db",'query-asset-info',urlOrUUID);

        },



        async assetInfoByUuid(urlOrUUID){

            return await Editor.Message.request("asset-db",'query-asset-info',urlOrUUID); // 注意3d返回字段与2d不太一样!

        },



        async assetInfoByUrl(urlOrUUID){

            return await Editor.Message.request("asset-db",'query-asset-info',urlOrUUID); // 注意3d返回字段与2d不太一样!

        },



        deepQuery(callback){

            if(!callback){

                return

            }

            Editor.Message.request("asset-db",'query-assets',{pattern:"db://**"}).then((list)=>{

                // 注意3d返回字段与2d不太一样!

                callback(null,list)

            },()=>{

                callback("run deepQuery error")

            });

        },



        queryInfoByUrl(...args){Editor2D.assetdb.queryInfoByUuid(...args)},

        queryInfoByUuid(urlOrUUID,callback){

            Editor.Message.request("asset-db",'query-asset-info',urlOrUUID).then((list)=>{

                // 注意3d返回字段与2d不太一样!

                callback(null,list)

            },()=>{

                callback("run queryInfoByUuid error")

            });

        },




        saveExists(url,text,callback){

            let promise = Editor.Message.request("asset-db",'save-asset',url,text).then(()=>{

                if(callback) callback()

            },()=>{

                if(callback) callback('save error')

            });

        },



        create(url,text,callback){

            let promise = Editor.Message.request("asset-db",'create-asset',url,text,{}).then((info)=>{

                if(callback) callback(null,info)

            },()=>{

                if(callback) callback('save error')

            });

        },



        delete(urls){

            for (let i = 0; i < urls.length; i++) {

                const url = urls[i];

                Editor.Message.request("asset-db",'delete-asset',url)

            }

        },

       

        move(source,target){

            Editor.Message.request("asset-db",'move-asset',source,target)

        },

    },



   

    Selection:{

        curGlobalActivate(){

            let type = Editor.Selection.getLastSelectedType();

            let ids = Editor.Selection.getSelected(type);

            return {type,id:ids[0]};

        },



        curSelection(type){

            return Editor.Selection.getSelected(type);

        },

    }

}



module.exports.analogApi = ()=>

{

    let packageRoot = __dirname.replace(/\\/g,'/')

    packageRoot = packageRoot.substr(0,packageRoot.lastIndexOf('simple-code/')-1)

    baseDir = packageRoot;



    // 插入api

    let copyFunc = (s_obj,t_obj)=>

    {

        for (const key in s_obj)

        {

            const v = s_obj[key];

            if(t_obj[key] == null){

                t_obj[key] = v;

            }else if(t_obj[key] instanceof Object){

                copyFunc(v,t_obj[key]);

            }

        }

    }



    copyFunc(Editor2D,Editor);

}
相关推荐
hackeroink1 小时前
【2024版】最新推荐好用的XSS漏洞扫描利用工具_xss扫描工具
前端·xss
迷雾漫步者3 小时前
Flutter组件————FloatingActionButton
前端·flutter·dart
向前看-3 小时前
验证码机制
前端·后端
xlsw_3 小时前
java全栈day20--Web后端实战(Mybatis基础2)
java·开发语言·mybatis
神仙别闹4 小时前
基于java的改良版超级玛丽小游戏
java
燃先生._.4 小时前
Day-03 Vue(生命周期、生命周期钩子八个函数、工程化开发和脚手架、组件化开发、根组件、局部注册和全局注册的步骤)
前端·javascript·vue.js
远游客07134 小时前
centos stream 8下载安装遇到的坑
linux·服务器·centos
黄油饼卷咖喱鸡就味增汤拌孜然羊肉炒饭4 小时前
SpringBoot如何实现缓存预热?
java·spring boot·spring·缓存·程序员
暮湫5 小时前
泛型(2)
java