From e9331794bd463524c2161ebbc183c81d998431cb Mon Sep 17 00:00:00 2001 From: Simon Robertshaw Date: Thu, 25 Aug 2011 12:20:00 +0100 Subject: [PATCH] Improvements to getscript --- includes/defines.h | 3 ++ includes/luaconsole.h | 2 +- src/luaconsole.c | 113 ++++++++++++++++++++++++++---------------- 3 files changed, 73 insertions(+), 45 deletions(-) diff --git a/includes/defines.h b/includes/defines.h index ba3abd92d..eab94fde1 100644 --- a/includes/defines.h +++ b/includes/defines.h @@ -16,9 +16,12 @@ #define IDENT_VERSION "G" //Change this if you're not Simon! It should be a single letter. #define SERVER "powdertoy.co.uk" +#define SCRIPTSERVER "powdertoy.co.uk" #define LOCAL_SAVE_DIR "Saves" +#define LOCAL_LUA_DIR "Lua" + #define THUMB_CACHE_SIZE 256 #ifndef M_PI diff --git a/includes/luaconsole.h b/includes/luaconsole.h index 805a54054..b80dead51 100644 --- a/includes/luaconsole.h +++ b/includes/luaconsole.h @@ -73,5 +73,5 @@ int luatpt_heat(lua_State* l); int luatpt_setfire(lua_State* l); int luatpt_setdebug(lua_State* l); int luatpt_setfpscap(lua_State* l); -int luatpt_getscriptid(lua_State* l); +int luatpt_getscript(lua_State* l); #endif diff --git a/src/luaconsole.c b/src/luaconsole.c index f0f9dc41e..2071e4225 100644 --- a/src/luaconsole.c +++ b/src/luaconsole.c @@ -1,12 +1,8 @@ +#include #ifdef LUACONSOLE #include #include #include -#include -#include -#include -#include -#include lua_State *l; int step_functions[6] = {0, 0, 0, 0, 0, 0}; @@ -65,7 +61,7 @@ void luacon_open(){ {"setfire", &luatpt_setfire}, {"setdebug", &luatpt_setdebug}, {"setfpscap",&luatpt_setfpscap}, - {"getscriptid",&luatpt_getscriptid}, + {"getscript",&luatpt_getscript}, {NULL,NULL} }; @@ -1074,52 +1070,81 @@ int fpscap = luaL_optint(l, 1, 0); limitFPS = fpscap; return 0; } -int luatpt_getscriptid(lua_State* l) +int luatpt_getscript(lua_State* l) { + char *fileid = NULL, *filedata = NULL, *fileuri = NULL, *fileauthor = NULL, *filename = NULL, *lastError = NULL; + int len, ret; + FILE * outputfile; + fileauthor = mystrdup(luaL_optstring(l, 1, "")); + fileid = mystrdup(luaL_optstring(l, 2, "")); + if(!fileauthor || !fileid || strlen(fileauthor)<1 || strlen(fileid)<1) + goto fin; + if(!confirm_ui(vid_buf, "Do you want to install script?", fileid, "Install")) + goto fin; + + fileuri = malloc(strlen(SCRIPTSERVER)+strlen(fileauthor)+strlen(fileid)+44); + sprintf(fileuri, "http://" SCRIPTSERVER "/GetScript.api?Author=%s&Filename=%s", fileauthor, fileid); - - char * id; - - id = mystrdup(luaL_optstring(l, 1, "1")); - for (int i = 0; i < strlen(id);i++) - if(id[i]>57||id[i]<48){ - luaL_error(l, "Invalid ID format."); - return 0; + filedata = http_auth_get(fileuri, svf_user_id, NULL, svf_session_id, &ret, &len); + + if(len <= 0 || !filedata) + { + lastError = "Server did not return data."; + goto fin; + } + if(ret != 200) + { + lastError = http_ret_text(ret); + goto fin; + } + + filename = malloc(strlen(fileauthor)+strlen(fileid)+strlen(PATH_SEP)+strlen(LOCAL_LUA_DIR)+6); + sprintf(filename, LOCAL_LUA_DIR PATH_SEP "%s_%s.lua", fileauthor, fileid); + +#ifdef WIN32 + _mkdir(LOCAL_LUA_DIR); +#else + mkdir(LOCAL_LUA_DIR, 0755); +#endif + + outputfile = fopen(filename, "r"); + if(outputfile) + { + fclose(outputfile); + outputfile = NULL; + if(confirm_ui(vid_buf, "File already exists, overwrite?", filename, "Overwrite")) + { + outputfile = fopen(filename, "w"); } - if(!confirm_ui(vid_buf,"Do you want to install script?",id,"Install")) - return 0; - - char file[80]; - char addr[300]; - strcpy(addr,"http://chaos.powdertoy.co.uk/ScriptRepo.php?id="); - //strcpy(addr,"http://localhost:80/ScriptRepo.php?id="); - strcat(addr,id); - - strcpy(file,id); - strcat(file,".lua"); - FILE *filetowriteto; - filetowriteto=fopen(file, "w"); - int ret,len; - char* luadata = http_auth_get(addr, svf_user_id, NULL, svf_session_id, &ret, &len); - if(ret != 200){ - printf("%d",ret); - luaL_error(l, "Error connecting to server."); - return 0; + else + { + goto fin; + } + } + else + { + outputfile = fopen(filename, "w"); } - if(len <=0||!luadata){ - luaL_error(l, "Error retreiving file."); - return 0; + if(!outputfile) + { + lastError = "Unable to write to file"; + goto fin; } - fputs(luadata,filetowriteto); - fclose(filetowriteto); - char tempstr[120]; - strcpy(tempstr,"dofile(\""); - strcat(tempstr,file); - strcat(tempstr,"\")"); - luacon_eval(tempstr); + fputs(filedata, outputfile); + fclose(outputfile); + outputfile = NULL; + +fin: + if(fileid) free(fileid); + if(filedata) free(filedata); + if(fileuri) free(fileuri); + if(fileauthor) free(fileauthor); + if(filename) free(filename); + + if(lastError) return luaL_error(l, lastError); return 0; } #endif