【2014-01-26】jython学习笔记:自动生成cocos2dx文件
[历史归档]本文原发布于 cstriker1407.info 个人博客内容为历史存档仅供参考。发布时间2014-01-26 标题jython学习笔记自动生成cocos2dx文件分类编程 / python jython 标签python jython·cocos2dxjython学习笔记自动生成cocos2dx文件备注jython代码模板文件最近在学习cocos2dx但是感觉使用IDE生成新的.cpp和.h文件比较麻烦就用jython写了一个小工具。算是代替手动劳动了吧。备注1 作者的jython非常菜鸟本工具是google无数才勉强搞定还请各位大牛指正。2 该工具非通用需要文件夹路径的配合才可以作者主要用它来生成自己需要的文件没有考虑通用性。jython代码def_renameClassName(_file_name,_old_name,_new_name):importre;fpopen(_file_name,r);alllinesfp.readlines();fp.close();fpopen(_file_name,w);foreachlineinalllines:are.sub(_old_name,_new_name,eachline);fp.writelines(a);fp.close();defrenameClassInFile(_file_name,_new_name,NewMacro):_renameClassName(_file_name.h,HelloLayer,_new_name);_renameClassName(_file_name.h,__HELLO_LAYER_H__,NewMacro);printRename Success! File:_file_name.h;_renameClassName(_file_name.cpp,HelloLayer,_new_name);printRename Success! File:_file_name.cpp;defcopyLayerFiles(_template_path,_new_path,_new_name):importshutil;shutil.copyfile(_template_pathHelloLayer.h,_new_path_new_name.h);printCopy Success! File:_new_path_new_name.h;shutil.copyfile(_template_pathHelloLayer.cpp,_new_path_new_name.cpp);printCopy Success! File:_new_path_new_name.cpp;defprintFileContents(_file_name):fpopen(_file_name,r);alllinesfp.readlines();foreachlineinalllines:printeachline;fp.close();# createLayers(C:\jython2.5.3\cocos2dxtemplates, D:\cocos\, MyLayer, __MY_LAYER_H__)defcreateLayers(templatePath,newPath,newName,NewMacro):copyLayerFiles(templatePath,newPath,newName);renameClassInFile(newPathnewName,newName,NewMacro);#printFileContents(newPath newName .h);#printFileContents(newPath newName .cpp);printAll Done!;if__name____main__:templatePathraw_input(Please Input Template Path, Default .\\cocs2dxtemplates\\ :);if(len(templatePath)0):templatePath.\\cocos2dxtemplates\\;printTemplate Path: templatePath;dstPathraw_input(Please Input Dst Path, Default .\\ :);if(len(dstPath)0):dstPath.\\;printDst Path: dstPath;layerNameraw_input(Please Input Layer Name, Default MyLayer :);if(len(layerName)0):layerNameMyLayer;printLayer Name: layerName;layerMacroraw_input(Please Input Macro Name, Default __MY_LAYER_H__ :);if(len(layerMacro)0):layerMacro__MY_LAYER_H__;printLayer Macro: layerMacro;createLayers(templatePath,dstPath,layerName,layerMacro);模板文件HelloLayer.h#ifndef__HELLO_LAYER_H__#define__HELLO_LAYER_H__#includecocos2d.hclassHelloLayer:publiccocos2d::CCLayer{public:CREATE_FUNC(HelloLayer);virtualboolinit();};#endif// __HELLO_LAYER_H__HelloLayer.cpp:#includeHelloLayer.hUSING_NS_CC;boolHelloLayer::init(){if(!CCLayer::init()){returnfalse;}returntrue;}