소스 검색

Merge pull request #327 from christopherreed/compile-scripts

Compile Lua scripts to byte-code.
Ivan Safrin 12 년 전
부모
커밋
553c831c9f

+ 2 - 1
IDE/Contents/Include/ExportProjectWindow.h

@@ -47,5 +47,6 @@ class ExportProjectWindow : public UIWindow {
 		
 		UICheckBox *macCheckBox;
 		UICheckBox *winCheckBox;
-		UICheckBox *linCheckBox;		
+		UICheckBox *linCheckBox;
+		UICheckBox *compileCheckBox;
 };

+ 1 - 1
IDE/Contents/Include/PolycodeProjectManager.h

@@ -48,7 +48,7 @@ class PolycodeProjectManager : public EventDispatcher {
 	
 	PolycodeProject *getProjectByProjectFile(String projectFile);
 	
-	void exportProject(PolycodeProject *project, String exportPath, bool macOS, bool windows, bool linux_);
+	void exportProject(PolycodeProject *project, String exportPath, bool macOS, bool windows, bool linux_, bool compileScripts);
 	
 	int removeProject(PolycodeProject *project);
 	

+ 1 - 1
IDE/Contents/Include/PolycodeToolLauncher.h

@@ -52,7 +52,7 @@ class PolycodeToolLauncher {
 		
 		static void openExternalEditor(String app, String file, String inFolder);
 		static String generateTempPath(PolycodeProject *project);
-		static void buildProject(PolycodeProject *project, String destinationPath);
+		static void buildProject(PolycodeProject *project, String destinationPath, bool compileScripts);
 
 		static void runPolyapp(String polyappPath);
 };

+ 11 - 3
IDE/Contents/Source/ExportProjectWindow.cpp

@@ -25,7 +25,7 @@
 
 extern PolycodeFrame *globalFrame;
 
-ExportProjectWindow::ExportProjectWindow() : UIWindow(L"Publish Project", 400, 300) {
+ExportProjectWindow::ExportProjectWindow() : UIWindow(L"Publish Project", 400, 360) {
 
 	closeOnEscape = true;
 
@@ -62,17 +62,25 @@ ExportProjectWindow::ExportProjectWindow() : UIWindow(L"Publish Project", 400, 3
 	addChild(linCheckBox);
 	linCheckBox->setPosition(padding, 225);
 
+	label = new ScreenLabel("OPTIONS", 22, "section", Label::ANTIALIAS_FULL);
+	addChild(label);
+	label->color.a = 0.4;	
+	label->setPosition(padding, 260);
+
+	compileCheckBox = new UICheckBox("Compile Scripts (experimental)", false);
+	addChild(compileCheckBox);
+	compileCheckBox->setPosition(padding, 295);
 
 	cancelButton = new UIButton(L"Cancel", 100);
 	cancelButton->addEventListener(this, UIEvent::CLICK_EVENT);
 	addChild(cancelButton);
-	cancelButton->setPosition(400-75-padding-100-10, 300-15);
+	cancelButton->setPosition(400-75-padding-100-10, 360-15);
 		
 	
 	okButton = new UIButton(L"Publish", 100);
 	okButton->addEventListener(this, UIEvent::CLICK_EVENT);
 	addChild(okButton);
-	okButton->setPosition(400-75-padding, 300-15);
+	okButton->setPosition(400-75-padding, 360-15);
 	
 	projectLocationInput->setText(CoreServices::getInstance()->getCore()->getUserHomeDirectory()+"/Documents/Polycode");
 	

+ 2 - 2
IDE/Contents/Source/PolycodeIDEApp.cpp

@@ -405,7 +405,7 @@ void PolycodeIDEApp::doRunProject() {
 	frame->showConsole();
 
 	String outPath = PolycodeToolLauncher::generateTempPath(projectManager->getActiveProject()) + ".polyapp";
-	PolycodeToolLauncher::buildProject(projectManager->getActiveProject(), outPath);
+	PolycodeToolLauncher::buildProject(projectManager->getActiveProject(), outPath, false);
 	PolycodeToolLauncher::runPolyapp(outPath);
 }
 
@@ -899,7 +899,7 @@ void PolycodeIDEApp::handleEvent(Event *event) {
 
 	if(event->getDispatcher() == frame->exportProjectWindow) {
 		if(event->getEventType() == "UIEvent" && event->getEventCode() == UIEvent::OK_EVENT) {
-			projectManager->exportProject(projectManager->getActiveProject(), frame->exportProjectWindow->projectLocationInput->getText(), frame->exportProjectWindow->macCheckBox->isChecked(), frame->exportProjectWindow->winCheckBox->isChecked(), frame->exportProjectWindow->linCheckBox->isChecked());
+			projectManager->exportProject(projectManager->getActiveProject(), frame->exportProjectWindow->projectLocationInput->getText(), frame->exportProjectWindow->macCheckBox->isChecked(), frame->exportProjectWindow->winCheckBox->isChecked(), frame->exportProjectWindow->linCheckBox->isChecked(), frame->exportProjectWindow->compileCheckBox->isChecked());
 			frame->hideModal();			
 		}
 	}

+ 2 - 2
IDE/Contents/Source/PolycodeProjectManager.cpp

@@ -137,14 +137,14 @@ void PolycodeProjectManager::createNewProject(String templateFolder, String proj
 	openProject(projectLocation+"/"+projectName+"/"+projectName+".polyproject");	
 }
 
-void PolycodeProjectManager::exportProject(PolycodeProject *project, String exportPath, bool macOS, bool windows, bool linux_) {
+void PolycodeProjectManager::exportProject(PolycodeProject *project, String exportPath, bool macOS, bool windows, bool linux_, bool compileScripts) {
 
 	String polycodeBasePath = CoreServices::getInstance()->getCore()->getDefaultWorkingDirectory();
 
 	String publishPath = polycodeBasePath+"/Standalone/Publish";
 	
 	String polyappPath = PolycodeToolLauncher::generateTempPath(project) + ".polyapp";
-	PolycodeToolLauncher::buildProject(project, polyappPath);	
+	PolycodeToolLauncher::buildProject(project, polyappPath, compileScripts);	
 	
 	if(macOS) {
 		PolycodeConsole::print("Exporting Mac version to "+exportPath+"/Mac \n");

+ 3 - 3
IDE/Contents/Source/PolycodeToolLauncher.cpp

@@ -86,7 +86,7 @@ String PolycodeToolLauncher::generateTempPath(PolycodeProject *project) {
 #endif
 }
 
-void PolycodeToolLauncher::buildProject(PolycodeProject *project, String destinationPath) {
+void PolycodeToolLauncher::buildProject(PolycodeProject *project, String destinationPath, bool compileScripts) {
 
 	PolycodeConsole::print("Building project: "+project->getProjectName() + "\n");	
 
@@ -101,12 +101,12 @@ void PolycodeToolLauncher::buildProject(PolycodeProject *project, String destina
 #ifdef _WINDOWS
 	String targetFolder = projectBasePath;
 	String command = "\""+polycodeBasePath+"/Standalone/Bin/polybuild.exe\"";
-	String args =  "--config=\""+projectPath+"\" --out=\""+destinationPath+"\"";
+	String args =  "--config=\""+projectPath+"\" --out=\""+destinationPath+"\" --compileScripts=\""+((compileScripts)?"true":"false")+"\"";
 	String ret = CoreServices::getInstance()->getCore()->executeExternalCommand(command, args, targetFolder);
 #else
 	String command = polycodeBasePath+"/Standalone/Bin/polybuild";
 	String inFolder = projectBasePath; 
-	String args = "--config=\""+projectPath+"\" --out="+destinationPath;
+	String args = "--config=\""+projectPath+"\" --out=\""+destinationPath+"\" --compileScripts=\""+((compileScripts)?"true":"false")+"\"";
 	String ret = CoreServices::getInstance()->getCore()->executeExternalCommand(command, args, inFolder);
 //	PolycodeConsole::print(ret);
 #endif

+ 7 - 5
Tools/Contents/polybuild/CMakeLists.txt

@@ -20,11 +20,13 @@ SET(minizip_SRCS
 
 #IF(POLYCODE_BUILD_STATIC)
 ADD_EXECUTABLE(polybuild ${minizip_SRCS} Source/polybuild.cpp Include/polybuild.h)
-IF(APPLE)
-	TARGET_LINK_LIBRARIES(polybuild Polycore ${PHYSFS_LIBRARY} ${ZLIB_LIBRARIES} "-framework IOKit" "-framework Cocoa")
-ELSE()
-	TARGET_LINK_LIBRARIES(polybuild Polycore ${PHYSFS_LIBRARY} ${ZLIB_LIBRARIES})
-ENDIF(APPLE)
+IF(MSVC OR MINGW)
+    TARGET_LINK_LIBRARIES(polybuild Polycore ${LUA_LIBRARIES} ${PHYSFS_LIBRARY} ${ZLIB_LIBRARIES})
+ELSEIF(APPLE)
+    TARGET_LINK_LIBRARIES(polybuild Polycore ${LUA_LIBRARIES} ${PHYSFS_LIBRARY} ${ZLIB_LIBRARIES} "-framework IOKit" "-framework Cocoa")
+ELSE(MSVC OR MINGW)
+    TARGET_LINK_LIBRARIES(polybuild Polycore ${LUA_LIBRARIES} ${PHYSFS_LIBRARY} ${ZLIB_LIBRARIES} dl)
+ENDIF(MSVC OR MINGW)
 #ENDIF(POLYCODE_BUILD_STATIC)
 
 IF(POLYCODE_INSTALL_FRAMEWORK)

+ 6 - 0
Tools/Contents/polybuild/Include/polybuild.h

@@ -11,6 +11,12 @@
 #include <io.h>
 #endif
 
+extern "C" {
+#include "lua.h"
+#include "lualib.h"
+#include "lauxlib.h"
+}
+
 using namespace Polycode;
 
 class BuildArg {

+ 45 - 24
Tools/Contents/polybuild/Source/polybuild.cpp

@@ -88,32 +88,53 @@ uLong filetime(
   return ret;
 }
 
-void addFileToZip(zipFile z, String filePath, String pathInZip, bool silent) {
-			if(!silent)
-				printf("Packaging %s as %s\n", filePath.c_str(), pathInZip.c_str());
-
-                	zip_fileinfo zi;
-                	zi.tmz_date.tm_sec = zi.tmz_date.tm_min = zi.tmz_date.tm_hour =
-			zi.tmz_date.tm_mday = zi.tmz_date.tm_mon = zi.tmz_date.tm_year = 0;
-			 zi.dosDate = 0;
-              	 	 zi.internal_fa = 0;
-             		   zi.external_fa = 0;
-             		   filetime(filePath.c_str(),&zi.tmz_date,&zi.dosDate);
-	
-			zipOpenNewFileInZip(z, pathInZip.c_str(), &zi, NULL, 0, NULL, 0, NULL, Z_DEFLATED, 2);
-
-			FILE *f = fopen(filePath.c_str(), "rb");
-			fseek(f, 0, SEEK_END);
-			long fileSize = ftell(f);
-			fseek(f, 0, SEEK_SET);
-			char *buf = (char*) malloc(fileSize);
-			fread(buf, fileSize, 1, f);
-			zipWriteInFileInZip(z, buf, fileSize);
-			free(buf);
-			fclose(f);
+extern "C" int MyWriter(lua_State *L, const void *p, size_t sz, void *ud) {
+	int err = zipWriteInFileInZip(static_cast<zipFile>(ud), p, sz);
+	// Non 0 means an error and stops lua_dump from calling the writer again.
+	return (err != ZIP_OK) && (sz != 0) ? 1 : 0;
+}
 
-			zipCloseFileInZip(z);
+void addFileToZip(zipFile z, String filePath, String pathInZip, bool silent) {
+	if(!silent)
+		printf("Packaging %s as %s\n", filePath.c_str(), pathInZip.c_str());
+
+	zip_fileinfo zi;
+	zi.tmz_date.tm_sec = zi.tmz_date.tm_min = zi.tmz_date.tm_hour = 
+		zi.tmz_date.tm_mday = zi.tmz_date.tm_mon = zi.tmz_date.tm_year = 0;
+	zi.dosDate = 0;
+	zi.internal_fa = 0;
+	zi.external_fa = 0;
+	filetime(filePath.c_str(),&zi.tmz_date,&zi.dosDate);
+
+	zipOpenNewFileInZip(z, pathInZip.c_str(), &zi, NULL, 0, NULL, 0, NULL, Z_DEFLATED, 2);
+
+	// Are we dealing with a script file?
+	int pos = filePath.rfind(".lua");
+	bool isScript = (pos > -1 && pos == filePath.length() - 4) ? true : false;
+
+	if(isScript && getArg("--compileScripts") == "true") {
+		lua_State *L = lua_open();
+		int err = 1;
+		if(L && 0 == luaL_loadfile(L, filePath.c_str())) {
+			err = lua_dump(L, MyWriter, z);
+		}
+		if(L) lua_close(L);
+		if(err) {
+			printf("Error compiling script. Ignoring.\n");
+		}
+	} else {
+		FILE *f = fopen(filePath.c_str(), "rb");
+		fseek(f, 0, SEEK_END);
+		long fileSize = ftell(f);
+		fseek(f, 0, SEEK_SET);
+		char *buf = (char*) malloc(fileSize);
+		fread(buf, fileSize, 1, f);
+		zipWriteInFileInZip(z, buf, fileSize);
+		free(buf);
+		fclose(f);
+	}
 
+	zipCloseFileInZip(z);
 }
 
 void addFolderToZip(zipFile z, String folderPath, String parentFolder, bool silent) {