Improvements to getscript
This commit is contained in:
parent
80e10460b8
commit
e9331794bd
@ -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
|
||||||
|
@ -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
|
||||||
|
109
src/luaconsole.c
109
src/luaconsole.c
@ -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(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"))
|
else
|
||||||
return 0;
|
{
|
||||||
|
goto fin;
|
||||||
char file[80];
|
}
|
||||||
char addr[300];
|
}
|
||||||
strcpy(addr,"http://chaos.powdertoy.co.uk/ScriptRepo.php?id=");
|
else
|
||||||
//strcpy(addr,"http://localhost:80/ScriptRepo.php?id=");
|
{
|
||||||
strcat(addr,id);
|
outputfile = fopen(filename, "w");
|
||||||
|
|
||||||
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){
|
if(!outputfile)
|
||||||
luaL_error(l, "Error retreiving file.");
|
{
|
||||||
return 0;
|
lastError = "Unable to write to file";
|
||||||
|
goto fin;
|
||||||
}
|
}
|
||||||
fputs(luadata,filetowriteto);
|
|
||||||
|
|
||||||
fclose(filetowriteto);
|
fputs(filedata, outputfile);
|
||||||
char tempstr[120];
|
fclose(outputfile);
|
||||||
strcpy(tempstr,"dofile(\"");
|
outputfile = NULL;
|
||||||
strcat(tempstr,file);
|
|
||||||
strcat(tempstr,"\")");
|
fin:
|
||||||
luacon_eval(tempstr);
|
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
|
||||||
|
Reference in New Issue
Block a user