Improvements to getscript

This commit is contained in:
Simon Robertshaw 2011-08-25 12:20:00 +01:00
parent 80e10460b8
commit e9331794bd
3 changed files with 73 additions and 45 deletions

View File

@ -16,9 +16,12 @@
#define IDENT_VERSION "G" //Change this if you're not Simon! It should be a single letter. #define IDENT_VERSION "G" //Change this if you're not Simon! It should be a single letter.
#define SERVER "powdertoy.co.uk" #define SERVER "powdertoy.co.uk"
#define SCRIPTSERVER "powdertoy.co.uk"
#define LOCAL_SAVE_DIR "Saves" #define LOCAL_SAVE_DIR "Saves"
#define LOCAL_LUA_DIR "Lua"
#define THUMB_CACHE_SIZE 256 #define THUMB_CACHE_SIZE 256
#ifndef M_PI #ifndef M_PI

View File

@ -73,5 +73,5 @@ int luatpt_heat(lua_State* l);
int luatpt_setfire(lua_State* l); int luatpt_setfire(lua_State* l);
int luatpt_setdebug(lua_State* l); int luatpt_setdebug(lua_State* l);
int luatpt_setfpscap(lua_State* l); int luatpt_setfpscap(lua_State* l);
int luatpt_getscriptid(lua_State* l); int luatpt_getscript(lua_State* l);
#endif #endif

View File

@ -1,12 +1,8 @@
#include <defines.h>
#ifdef LUACONSOLE #ifdef LUACONSOLE
#include <powder.h> #include <powder.h>
#include <console.h> #include <console.h>
#include <luaconsole.h> #include <luaconsole.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <string.h>
#include <sys/types.h>
lua_State *l; lua_State *l;
int step_functions[6] = {0, 0, 0, 0, 0, 0}; int step_functions[6] = {0, 0, 0, 0, 0, 0};
@ -65,7 +61,7 @@ void luacon_open(){
{"setfire", &luatpt_setfire}, {"setfire", &luatpt_setfire},
{"setdebug", &luatpt_setdebug}, {"setdebug", &luatpt_setdebug},
{"setfpscap",&luatpt_setfpscap}, {"setfpscap",&luatpt_setfpscap},
{"getscriptid",&luatpt_getscriptid}, {"getscript",&luatpt_getscript},
{NULL,NULL} {NULL,NULL}
}; };
@ -1074,52 +1070,81 @@ int fpscap = luaL_optint(l, 1, 0);
limitFPS = fpscap; limitFPS = fpscap;
return 0; 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; filedata = http_auth_get(fileuri, svf_user_id, NULL, svf_session_id, &ret, &len);
id = mystrdup(luaL_optstring(l, 1, "1")); if(len <= 0 || !filedata)
for (int i = 0; i < strlen(id);i++) {
if(id[i]>57||id[i]<48){ lastError = "Server did not return data.";
luaL_error(l, "Invalid ID format."); goto fin;
return 0;
} }
if(!confirm_ui(vid_buf,"Do you want to install script?",id,"Install")) if(ret != 200)
return 0; {
lastError = http_ret_text(ret);
char file[80]; goto fin;
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;
} }
if(len <=0||!luadata){ filename = malloc(strlen(fileauthor)+strlen(fileid)+strlen(PATH_SEP)+strlen(LOCAL_LUA_DIR)+6);
luaL_error(l, "Error retreiving file."); sprintf(filename, LOCAL_LUA_DIR PATH_SEP "%s_%s.lua", fileauthor, fileid);
return 0;
}
fputs(luadata,filetowriteto);
fclose(filetowriteto); #ifdef WIN32
char tempstr[120]; _mkdir(LOCAL_LUA_DIR);
strcpy(tempstr,"dofile(\""); #else
strcat(tempstr,file); mkdir(LOCAL_LUA_DIR, 0755);
strcat(tempstr,"\")"); #endif
luacon_eval(tempstr);
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");
}
else
{
goto fin;
}
}
else
{
outputfile = fopen(filename, "w");
}
if(!outputfile)
{
lastError = "Unable to write to file";
goto fin;
}
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; return 0;
} }
#endif #endif