Skip to content

Commit

Permalink
update tpt.getscript to use starcatcher.us
Browse files Browse the repository at this point in the history
  • Loading branch information
jacob1 committed Sep 18, 2015
1 parent e9043c9 commit f5774ad
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 59 deletions.
1 change: 0 additions & 1 deletion src/Config.h
Expand Up @@ -72,7 +72,6 @@
#define MTOS(str) MTOS_EXPAND(str)

#define SERVER "powdertoy.co.uk"
#define SCRIPTSERVER "powdertoy.co.uk"
#define STATICSERVER "static.powdertoy.co.uk"

#define LOCAL_SAVE_DIR "Saves"
Expand Down
90 changes: 34 additions & 56 deletions src/lua/LegacyLuaAPI.cpp
Expand Up @@ -1865,92 +1865,70 @@ int luatpt_setfpscap(lua_State* l)

int luatpt_getscript(lua_State* l)
{
char *filedata = NULL, *fileuri = NULL, *filename = NULL, *luacommand = NULL;
const char *lastError = NULL;
std::string fileauthor = "", fileid = "";
int len, ret,run_script;
FILE * outputfile;
int scriptID = luaL_checkinteger(l, 1);
const char *filename = luaL_checkstring(l, 2);
int runScript = luaL_optint(l, 3, 0);
int confirmPrompt = luaL_optint(l, 4, 1);

std::stringstream url;
url << "http://starcatcher.us/scripts/main.lua?get=" << scriptID;
if (confirmPrompt && !ConfirmPrompt::Blocking("Do you want to install script?", url.str(), "Install"))
return 0;

fileauthor = std::string(luaL_optstring(l, 1, ""));
fileid = std::string(luaL_optstring(l, 2, ""));
run_script = luaL_optint(l, 3, 0);
if(!fileauthor.length() || !fileid.length())
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";
goto fin;
free(scriptData);
return luaL_error(l, "Server did not return data");
}
if(!ConfirmPrompt::Blocking("Do you want to install script?", fileid, "Install"))
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)
if (ret != 200)
{
lastError = "Server did not return data.";
goto fin;
free(scriptData);
return luaL_error(l, http_ret_text(ret));
}
if(ret != 200)

if (!strcmp(scriptData, "Invalid script ID\r\n"))
{
lastError = http_ret_text(ret);
goto fin;
free(scriptData);
return luaL_error(l, "Invalid Script ID");
}

filename = new char[fileauthor.length()+fileid.length()+strlen(PATH_SEP)+strlen(LOCAL_LUA_DIR)+6];
sprintf(filename, LOCAL_LUA_DIR PATH_SEP "%s_%s.lua", fileauthor.c_str(), fileid.c_str());

Client::Ref().MakeDirectory(LOCAL_LUA_DIR);

outputfile = fopen(filename, "r");
if(outputfile)
FILE *outputfile = fopen(filename, "r");
if (outputfile)
{
fclose(outputfile);
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");
}
else
{
goto fin;
free(scriptData);
return 0;
}
}
else
{
outputfile = fopen(filename, "w");
}

if(!outputfile)
if (!outputfile)
{
lastError = "Unable to write to file";
goto fin;
free(scriptData);
return luaL_error(l, "Unable to write to file");
}


fputs(filedata, outputfile);
fputs(scriptData, outputfile);
fclose(outputfile);
outputfile = NULL;
if(run_script)
if (runScript)
{
luacommand = new char[strlen(filename)+20];
sprintf(luacommand,"dofile(\"%s\")",filename);
luaL_dostring (l, luacommand);
std::stringstream luaCommand;
luaCommand << "dofile('" << filename << "')";
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;
}

Expand Down
2 changes: 0 additions & 2 deletions src/lua/LuaScriptInterface.h
Expand Up @@ -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 :(

#define LOCAL_LUA_DIR "Lua"

#define LUACON_MDOWN 1
#define LUACON_MUP 2
#define LUACON_MPRESS 3
Expand Down

0 comments on commit f5774ad

Please sign in to comment.