mirror of
https://github.com/The-Powder-Toy/The-Powder-Toy.git
synced 2025-08-01 06:00:15 +02:00
make Platform::ExecutableName return std::string (fixes minor memory leak)
update code is untested
This commit is contained in:
@@ -20,8 +20,9 @@
|
|||||||
namespace Platform
|
namespace Platform
|
||||||
{
|
{
|
||||||
|
|
||||||
char *ExecutableName(void)
|
std::string ExecutableName()
|
||||||
{
|
{
|
||||||
|
std::string ret;
|
||||||
#if defined(WIN)
|
#if defined(WIN)
|
||||||
char *name = (char *)malloc(64);
|
char *name = (char *)malloc(64);
|
||||||
DWORD max = 64, res;
|
DWORD max = 64, res;
|
||||||
@@ -41,7 +42,7 @@ char *ExecutableName(void)
|
|||||||
{
|
{
|
||||||
free(fn);
|
free(fn);
|
||||||
free(name);
|
free(name);
|
||||||
return NULL;
|
return "";
|
||||||
}
|
}
|
||||||
res = 1;
|
res = 1;
|
||||||
#else
|
#else
|
||||||
@@ -63,22 +64,23 @@ char *ExecutableName(void)
|
|||||||
if (res <= 0)
|
if (res <= 0)
|
||||||
{
|
{
|
||||||
free(name);
|
free(name);
|
||||||
return NULL;
|
return "";
|
||||||
}
|
}
|
||||||
return name;
|
ret = name;
|
||||||
|
free(name);
|
||||||
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
void DoRestart()
|
void DoRestart()
|
||||||
{
|
{
|
||||||
char *exename = ExecutableName();
|
std::string exename = ExecutableName();
|
||||||
if (exename)
|
if (exename.length())
|
||||||
{
|
{
|
||||||
#ifdef WIN
|
#ifdef WIN
|
||||||
ShellExecute(NULL, "open", exename, NULL, NULL, SW_SHOWNORMAL);
|
ShellExecute(NULL, "open", exename, NULL, NULL, SW_SHOWNORMAL);
|
||||||
#elif defined(LIN) || defined(MACOSX)
|
#elif defined(LIN) || defined(MACOSX)
|
||||||
execl(exename, "powder", NULL);
|
execl(exename.c_str(), "powder", NULL);
|
||||||
#endif
|
#endif
|
||||||
free(exename);
|
|
||||||
}
|
}
|
||||||
exit(-1);
|
exit(-1);
|
||||||
}
|
}
|
||||||
|
@@ -5,7 +5,7 @@
|
|||||||
|
|
||||||
namespace Platform
|
namespace Platform
|
||||||
{
|
{
|
||||||
char * ExecutableName();
|
std::string ExecutableName();
|
||||||
void DoRestart();
|
void DoRestart();
|
||||||
|
|
||||||
void OpenURI(std::string uri);
|
void OpenURI(std::string uri);
|
||||||
|
@@ -25,120 +25,106 @@
|
|||||||
|
|
||||||
int update_start(char *data, unsigned int len)
|
int update_start(char *data, unsigned int len)
|
||||||
{
|
{
|
||||||
char *self = Platform::ExecutableName(), *temp;
|
std::string exeName = Platform::ExecutableName(), updName;
|
||||||
#ifdef WIN
|
|
||||||
char *p;
|
|
||||||
#endif
|
|
||||||
FILE *f;
|
FILE *f;
|
||||||
int res = 1;
|
|
||||||
|
|
||||||
if (!self)
|
if (!exeName.length())
|
||||||
return 1;
|
return 1;
|
||||||
|
|
||||||
#ifdef WIN
|
#ifdef WIN
|
||||||
temp = (char*)malloc(strlen(self)+12);
|
updName = exeName;
|
||||||
strcpy(temp, self);
|
std::string extension = exeName.substr(exeName.length() - 4);
|
||||||
p = temp + strlen(temp) - 4;
|
if (extension == ".exe")
|
||||||
if (_stricmp(p, ".exe"))
|
updName = exeName.substr(0, exeName.length() - 4);
|
||||||
p += 4;
|
updName = updName + "_upd.exe";
|
||||||
strcpy(p, "_upd.exe");
|
|
||||||
|
|
||||||
if (!MoveFile(self, temp))
|
if (!MoveFile(exeName.c_str(), updName.c_str()))
|
||||||
goto fail;
|
goto fail;
|
||||||
|
|
||||||
f = fopen(self, "wb");
|
f = fopen(exeName.c_str(), "wb");
|
||||||
if (!f)
|
if (!f)
|
||||||
goto fail;
|
goto fail;
|
||||||
if (fwrite(data, 1, len, f) != len)
|
if (fwrite(data, 1, len, f) != len)
|
||||||
{
|
{
|
||||||
fclose(f);
|
fclose(f);
|
||||||
DeleteFile(self);
|
DeleteFile(exeName.c_str());
|
||||||
goto fail;
|
goto fail;
|
||||||
}
|
}
|
||||||
fclose(f);
|
fclose(f);
|
||||||
|
|
||||||
if ((uintptr_t)ShellExecute(NULL, "open", self, NULL, NULL, SW_SHOWNORMAL) <= 32)
|
if ((uintptr_t)ShellExecute(NULL, "open", exeName.c_str(), NULL, NULL, SW_SHOWNORMAL) <= 32)
|
||||||
{
|
{
|
||||||
DeleteFile(self);
|
DeleteFile(exeName.c_str());
|
||||||
goto fail;
|
goto fail;
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
#else
|
#else
|
||||||
temp = (char*)malloc(strlen(self)+8);
|
updName = exeName + "-update";
|
||||||
strcpy(temp, self);
|
|
||||||
strcat(temp, "-update");
|
|
||||||
|
|
||||||
f = fopen(temp, "w");
|
f = fopen(updName.c_str(), "w");
|
||||||
if (!f)
|
if (!f)
|
||||||
goto fail;
|
return 1;
|
||||||
if (fwrite(data, 1, len, f) != len)
|
if (fwrite(data, 1, len, f) != len)
|
||||||
{
|
{
|
||||||
fclose(f);
|
fclose(f);
|
||||||
unlink(temp);
|
unlink(updName.c_str());
|
||||||
goto fail;
|
return 1;
|
||||||
}
|
}
|
||||||
fclose(f);
|
fclose(f);
|
||||||
|
|
||||||
if (chmod(temp, 0755))
|
if (chmod(updName.c_str(), 0755))
|
||||||
{
|
{
|
||||||
unlink(temp);
|
unlink(updName.c_str());
|
||||||
goto fail;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (rename(temp, self))
|
if (rename(updName.c_str(), exeName.c_str()))
|
||||||
{
|
{
|
||||||
unlink(temp);
|
unlink(updName.c_str());
|
||||||
goto fail;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
execl(self, "powder-update", NULL);
|
execl(exeName.c_str(), "powder-update", NULL);
|
||||||
|
return 0;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
fail:
|
|
||||||
free(temp);
|
|
||||||
free(self);
|
|
||||||
return res;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int update_finish(void)
|
int update_finish(void)
|
||||||
{
|
{
|
||||||
#ifdef WIN
|
#ifdef WIN
|
||||||
char *temp, *self = Platform::ExecutableName(), *p;
|
std::string exeName = Platform::ExecutableName(), updName;
|
||||||
int timeout = 5, err;
|
int timeout = 5, err;
|
||||||
|
|
||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
printf("Update: Current EXE name: %s\n", self);
|
printf("Update: Current EXE name: %s\n", exeName.c_str());
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
temp = (char*)malloc(strlen(self)+12);
|
updName = exeName;
|
||||||
strcpy(temp, self);
|
std::string extension = exeName.substr(exeName.length() - 4);
|
||||||
p = temp + strlen(temp) - 4;
|
if (extension == ".exe")
|
||||||
if (_stricmp(p, ".exe"))
|
updName = exeName.substr(0, exeName.length() - 4);
|
||||||
p += 4;
|
updName = updName + "_upd.exe";
|
||||||
strcpy(p, "_upd.exe");
|
|
||||||
|
|
||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
printf("Update: Temp EXE name: %s\n", temp);
|
printf("Update: Temp EXE name: %s\n", updName.c_str());
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
while (!DeleteFile(temp))
|
while (!DeleteFile(updName.c_str()))
|
||||||
{
|
{
|
||||||
err = GetLastError();
|
err = GetLastError();
|
||||||
if (err == ERROR_FILE_NOT_FOUND)
|
if (err == ERROR_FILE_NOT_FOUND)
|
||||||
{
|
{
|
||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
printf("Update: Temp file deleted\n");
|
printf("Update: Temp file not deleted\n");
|
||||||
#endif
|
#endif
|
||||||
free(temp);
|
|
||||||
// Old versions of powder toy name their update files with _update.exe, delete that upgrade file here
|
// Old versions of powder toy name their update files with _update.exe, delete that upgrade file here
|
||||||
temp = (char*)malloc(strlen(self)+12);
|
updName = exeName;
|
||||||
strcpy(temp, self);
|
std::string extension = exeName.substr(exeName.length() - 4);
|
||||||
p = temp + strlen(temp) - 4;
|
if (extension == ".exe")
|
||||||
if (_stricmp(p, ".exe"))
|
updName = exeName.substr(0, exeName.length() - 4);
|
||||||
p += 4;
|
updName = updName + "_update.exe";
|
||||||
strcpy(p, "_update.exe");
|
DeleteFile(updName.c_str());
|
||||||
DeleteFile(temp);
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
Sleep(500);
|
Sleep(500);
|
||||||
@@ -148,11 +134,9 @@ int update_finish(void)
|
|||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
printf("Update: Delete timeout\n");
|
printf("Update: Delete timeout\n");
|
||||||
#endif
|
#endif
|
||||||
free(temp);
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
free(temp);
|
|
||||||
#endif
|
#endif
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@@ -3379,9 +3379,9 @@ int LuaScriptInterface::platform_releaseType(lua_State * l)
|
|||||||
|
|
||||||
int LuaScriptInterface::platform_exeName(lua_State * l)
|
int LuaScriptInterface::platform_exeName(lua_State * l)
|
||||||
{
|
{
|
||||||
char *name = Platform::ExecutableName();
|
std::string name = Platform::ExecutableName();
|
||||||
if (name)
|
if (name.length())
|
||||||
lua_pushstring(l, name);
|
lua_pushstring(l, name.c_str());
|
||||||
else
|
else
|
||||||
luaL_error(l, "Error, could not get executable name");
|
luaL_error(l, "Error, could not get executable name");
|
||||||
return 1;
|
return 1;
|
||||||
|
Reference in New Issue
Block a user