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);

}
相关推荐
小歆8843 分钟前
100%全国产化时钟服务器、全国产化校时服务器、全国产化授时服务器
运维·服务器
方圆想当图灵4 分钟前
缓存之美:万文详解 Caffeine 实现原理(下)
java·redis·缓存
hgdlip11 分钟前
IP属地与视频定位位置不一致:现象解析与影响探讨
服务器·网络·tcp/ip
栗豆包19 分钟前
w175基于springboot的图书管理系统的设计与实现
java·spring boot·后端·spring·tomcat
小美的打工日记31 分钟前
ES6+新特性,var、let 和 const 的区别
前端·javascript·es6
0xfather36 分钟前
在Debian系统中安装Debian(Linux版PE装机)
linux·服务器·debian
helianying5539 分钟前
云原生架构下的AI智能编排:ScriptEcho赋能前端开发
前端·人工智能·云原生·架构
@PHARAOH1 小时前
HOW - 基于master的a分支和基于a的b分支合流问题
前端·git·github·分支管理
等一场春雨1 小时前
Java设计模式 十四 行为型模式 (Behavioral Patterns)
java·开发语言·设计模式
涔溪1 小时前
有哪些常见的 Vue 错误?
前端·javascript·vue.js