update tpt.getscript to use starcatcher.us

This commit is contained in:
jacob1 2015-09-18 13:00:01 -04:00
parent e9043c93aa
commit f5774ad9b9
3 changed files with 35 additions and 60 deletions

View File

@ -72,7 +72,6 @@
#define MTOS(str) MTOS_EXPAND(str) #define MTOS(str) MTOS_EXPAND(str)
#define SERVER "powdertoy.co.uk" #define SERVER "powdertoy.co.uk"
#define SCRIPTSERVER "powdertoy.co.uk"
#define STATICSERVER "static.powdertoy.co.uk" #define STATICSERVER "static.powdertoy.co.uk"
#define LOCAL_SAVE_DIR "Saves" #define LOCAL_SAVE_DIR "Saves"

View File

@ -1865,92 +1865,70 @@ int luatpt_setfpscap(lua_State* l)
int luatpt_getscript(lua_State* l) int luatpt_getscript(lua_State* l)
{ {
char *filedata = NULL, *fileuri = NULL, *filename = NULL, *luacommand = NULL; int scriptID = luaL_checkinteger(l, 1);
const char *lastError = NULL; const char *filename = luaL_checkstring(l, 2);
std::string fileauthor = "", fileid = ""; int runScript = luaL_optint(l, 3, 0);
int len, ret,run_script; int confirmPrompt = luaL_optint(l, 4, 1);
FILE * outputfile;
fileauthor = std::string(luaL_optstring(l, 1, "")); std::stringstream url;
fileid = std::string(luaL_optstring(l, 2, "")); url << "http://starcatcher.us/scripts/main.lua?get=" << scriptID;
run_script = luaL_optint(l, 3, 0); if (confirmPrompt && !ConfirmPrompt::Blocking("Do you want to install script?", url.str(), "Install"))
if(!fileauthor.length() || !fileid.length()) return 0;
int ret, len;
char *scriptData = http_simple_get(url.str().c_str(), &ret, &len);
if (len <= 0 || !filename)
{ {
lastError = "Script Author or ID not given"; free(scriptData);
goto fin; return luaL_error(l, "Server did not return data");
} }
if(!ConfirmPrompt::Blocking("Do you want to install script?", fileid, "Install")) if (ret != 200)
goto fin;
fileuri = new char[strlen(SCRIPTSERVER)+fileauthor.length()+fileid.length()+44];
sprintf(fileuri, "http://" SCRIPTSERVER "/GetScript.api?Author=%s&Filename=%s", fileauthor.c_str(), fileid.c_str());
//filedata = http_auth_get(fileuri, svf_user_id, NULL, svf_session_id, &ret, &len);
filedata = http_auth_get(fileuri, NULL, NULL, NULL, &ret, &len);
if(len <= 0 || !filedata)
{ {
lastError = "Server did not return data."; free(scriptData);
goto fin; return luaL_error(l, http_ret_text(ret));
}
if(ret != 200)
{
lastError = http_ret_text(ret);
goto fin;
} }
filename = new char[fileauthor.length()+fileid.length()+strlen(PATH_SEP)+strlen(LOCAL_LUA_DIR)+6]; if (!strcmp(scriptData, "Invalid script ID\r\n"))
sprintf(filename, LOCAL_LUA_DIR PATH_SEP "%s_%s.lua", fileauthor.c_str(), fileid.c_str()); {
free(scriptData);
return luaL_error(l, "Invalid Script ID");
}
Client::Ref().MakeDirectory(LOCAL_LUA_DIR); FILE *outputfile = fopen(filename, "r");
if (outputfile)
outputfile = fopen(filename, "r");
if(outputfile)
{ {
fclose(outputfile); fclose(outputfile);
outputfile = NULL; outputfile = NULL;
if(ConfirmPrompt::Blocking("File already exists, overwrite?", filename, "Overwrite")) if (!confirmPrompt || ConfirmPrompt::Blocking("File already exists, overwrite?", filename, "Overwrite"))
{ {
outputfile = fopen(filename, "w"); outputfile = fopen(filename, "w");
} }
else else
{ {
goto fin; free(scriptData);
return 0;
} }
} }
else else
{ {
outputfile = fopen(filename, "w"); outputfile = fopen(filename, "w");
} }
if (!outputfile)
if(!outputfile)
{ {
lastError = "Unable to write to file"; free(scriptData);
goto fin; return luaL_error(l, "Unable to write to file");
} }
fputs(scriptData, outputfile);
fputs(filedata, outputfile);
fclose(outputfile); fclose(outputfile);
outputfile = NULL; outputfile = NULL;
if(run_script) if (runScript)
{ {
luacommand = new char[strlen(filename)+20]; std::stringstream luaCommand;
sprintf(luacommand,"dofile(\"%s\")",filename); luaCommand << "dofile('" << filename << "')";
luaL_dostring (l, luacommand); luaL_dostring(l, luaCommand.str().c_str());
} }
fin:
free(filedata);
delete[] fileuri;
delete[] filename;
delete[] luacommand;
luacommand = NULL;
if(lastError)
{
return luaL_error(l, lastError);
}
return 0; return 0;
} }

View File

@ -15,8 +15,6 @@ class Tool;
//Because lua only has bindings for C, we're going to have to go outside "outside" the LuaScriptInterface, this means we can only have one instance :( //Because lua only has bindings for C, we're going to have to go outside "outside" the LuaScriptInterface, this means we can only have one instance :(
#define LOCAL_LUA_DIR "Lua"
#define LUACON_MDOWN 1 #define LUACON_MDOWN 1
#define LUACON_MUP 2 #define LUACON_MUP 2
#define LUACON_MPRESS 3 #define LUACON_MPRESS 3