好记性不如铅笔头

python && jython, 编程

jython学习笔记:自动生成cocos2dx文件

最近在学习cocos2dx,但是感觉使用IDE生成新的.cpp和.h文件比较麻烦,就用jython写了一个小工具。算是代替手动劳动了吧。

CONTENTS

备注:

1 作者的jython非常菜鸟,本工具是google无数才勉强搞定,还请各位大牛指正。

2 该工具非通用,需要文件夹路径的配合才可以,作者主要用它来生成自己需要的文件,没有考虑通用性。

jython代码:

def _renameClassName (_file_name, _old_name, _new_name):
	import re;
	
	fp=open(_file_name,'r');
	alllines=fp.readlines();
	fp.close();
	
	fp=open(_file_name,'w');
	for eachline in alllines:
		a=re.sub(_old_name, _new_name, eachline);
		fp.writelines(a);
	
	fp.close();

def renameClassInFile(_file_name, _new_name, NewMacro):
	_renameClassName(_file_name + ".h", "HelloLayer", _new_name);
	_renameClassName(_file_name + ".h", "__HELLO_LAYER_H__", NewMacro);
	print "Rename Success! File:" + _file_name + ".h";

	_renameClassName(_file_name + ".cpp", "HelloLayer", _new_name);
	print "Rename Success! File:" + _file_name + ".cpp";

def copyLayerFiles(_template_path, _new_path, _new_name):
	import shutil;
	shutil.copyfile(_template_path + "HelloLayer.h", _new_path + _new_name + ".h");
	print "Copy Success! File:" + _new_path + _new_name + ".h";

	shutil.copyfile(_template_path + "HelloLayer.cpp", _new_path + _new_name + ".cpp");	
	print "Copy Success! File:" + _new_path + _new_name + ".cpp";
	

def printFileContents(_file_name):
	fp=open(_file_name,'r');
	alllines=fp.readlines();
	for eachline in alllines:
		print eachline;
		
	fp.close();


# createLayers("C:\jython2.5.3\cocos2dxtemplates", "D:\cocos\"", "MyLayer", "__MY_LAYER_H__")
def createLayers(templatePath, newPath, newName, NewMacro):
	copyLayerFiles(templatePath, newPath, newName);
	renameClassInFile(newPath + newName, newName, NewMacro);
	
	#printFileContents(newPath + newName + ".h");
	#printFileContents(newPath + newName + ".cpp");
	
	print "All Done!";


if __name__ == "__main__":
	
	templatePath = raw_input("Please Input Template Path, Default .\\cocs2dxtemplates\\  :");
	if (len(templatePath) == 0):
		templatePath = ".\\cocos2dxtemplates\\";
	print "Template Path: " + templatePath;
	
	dstPath = raw_input("Please Input Dst Path, Default .\\  :");
	if (len(dstPath) == 0):
		dstPath = ".\\";
	print "Dst Path: " + dstPath;
	
	layerName = raw_input("Please Input Layer Name, Default MyLayer  :");
	if (len(layerName) == 0):
		layerName = "MyLayer";
	print "Layer Name: " + layerName;
	
	layerMacro = raw_input("Please Input Macro Name, Default __MY_LAYER_H__  :");
	if (len(layerMacro) == 0):
		layerMacro = "__MY_LAYER_H__";
	print "Layer Macro: " + layerMacro;
	
	createLayers(templatePath, dstPath, layerName, layerMacro);

 模板文件:

HelloLayer.h:

#ifndef __HELLO_LAYER_H__
#define __HELLO_LAYER_H__

#include "cocos2d.h"

class HelloLayer : public cocos2d::CCLayer
{
public:
	CREATE_FUNC(HelloLayer);
	virtual bool init();
};

#endif // __HELLO_LAYER_H__

 HelloLayer.cpp:

#include "HelloLayer.h"
USING_NS_CC;

bool HelloLayer::init()
{
	if ( !CCLayer::init() )
	{
		return false;
	}
	
	return true;
}

 

发表评论

12 − 7 =

此站点使用 Akismet 来减少垃圾评论。了解我们如何处理您的评论数据