为方便mac下面打开多个2dx模拟器,对模拟器源码做了简单修改。模拟器配置的工程目录都是绝对路径,所以每个工程都需要重新执行脚本生成不同分辨率模拟器(基础包Simulator.app不用每次都生成)。
1. 修改2dx模拟器里面三个文件
-
COCOS_ROOT/tools/simulator/frameworks/runtime-src/proj.ios_mac/mac:
-
SimulatorApp.mm
修改parseCocosProjectConfig函数
- (void) parseCocosProjectConfig:(ProjectConfig*)config { ... ... config->setProjectDir(parser->getWorkDir()); config->setWritablePath(parser->getWritablePath()); ... ... }
-
-
COCOS_ROOT/tools/simulator/libsimulator/lib/runtime:
-
ConfigParser.h
ConfigParser类中增加函数和变量
class CC_LIBSIM_DLL ConfigParser { public: ... ... const std::string &getWritablePath(); const std::string &getWorkDir(); private: ... ... string _writablepath; string _workdir; }
-
ConfigParser.cpp
修改readConfig函数
void ConfigParser::readConfig(const string &filepath) { ... ... if (_docRootjson.HasMember("init_cfg")) { if(_docRootjson["init_cfg"].IsObject()) { ... ... if (objectInitView.HasMember("workdir") && objectInitView["workdir"].IsString()) { _workdir = objectInitView["workdir"].GetString(); } if (objectInitView.HasMember("writablepath") && objectInitView["writablepath"].IsString()) { _writablepath = objectInitView["writablepath"].GetString(); } ... ... } } ... ...
增加函数
const std::string &ConfigParser::getWorkDir() { return _workdir; } const std::string &ConfigParser::getWritablePath() { return _writablepath; }
-
2. 编译模拟器
运行cocos gen-simulator -o simulator2 -p mac
命令来编译模拟器
- cocos: 2dx的命令行工具(COCOS_ROOT/tools/cocos2d-console/bin),一般添加到环境变量
- -o OUT_DIR 指定输出目录
- -p {ios,mac,android,win32} 指定目标平台
3. 拷贝模拟器app到此文件,配置simulator.json文件
将生成的app文件放在任意目录下(最好每个工程建个simulator文件夹,方便管理),在相同目录创建配置文件simulator.json
,内容如下:
{
"sims": [
{"width": 960, "height": 640},
{"width": 1024, "height": 768},
{"width": 1280, "height": 720},
{"width": 800, "height": 480},
{"width": 800, "height": 1280}
],
"workdir": "工程目录",
"entry":"工程目录/入口文件"
}
- sims: 为数组,里面可以配置多个不同分辨率模拟器
- workdir:工程目录(绝对路径)
- entry:入口文件(绝对路径main.lua|main.js)
4. 运行python脚本
gen_simulator.py:
#coding=utf-8
import os
import json
import shutil
realpath = os.path.realpath(".")
def parseConfig():
f = open("simulator.json")
appConfig = json.loads(f.read())
f.close()
return appConfig
def genSimulators():
appConfig = parseConfig()
for i in range(len(appConfig["sims"])):
width = appConfig["sims"][i]["width"]
height = appConfig["sims"][i]["height"]
appname = "Sim_%d@%d.app" % (width, height)
try:
shutil.rmtree(appname)
except OSError, e:
pass
shutil.copytree("Simulator.app", appname)
f = open(appname + "/Contents/Resources/config.json")
configStr = f.read()
f.close()
config = json.loads(configStr)
config["init_cfg"]["workdir"] = appConfig["workdir"]
config["init_cfg"]["entry"] = appConfig["entry"]
config["init_cfg"]["width"] = width
config["init_cfg"]["height"] = height
config["init_cfg"]["name"] = "%d@%d" % (width, height)
config["init_cfg"]["writablepath"] = realpath + "/sim_%d_%d" % (width, height)
configStr = json.dumps(config, sort_keys=True, indent=4)
f = open(appname + "/Contents/Resources/config.json", "wb")
f.write(configStr)
f.close()
if __name__ == '__main__':
genSimulators()