Deprecated Acquire/ReleaseLock from IGameConfigManager, use thread pump in updater.
This commit is contained in:
@@ -43,7 +43,7 @@
|
||||
|
||||
using namespace SourceMod;
|
||||
|
||||
UpdateReader::UpdateReader()
|
||||
UpdateReader::UpdateReader() : partFirst(NULL), partLast(NULL)
|
||||
{
|
||||
}
|
||||
|
||||
@@ -174,6 +174,21 @@ SMCResult UpdateReader::ReadSMC_LeavingSection(const SMCStates *states)
|
||||
return SMCResult_Continue;
|
||||
}
|
||||
|
||||
void UpdateReader::LinkPart(UpdatePart *part)
|
||||
{
|
||||
part->next = NULL;
|
||||
if (partFirst == NULL)
|
||||
{
|
||||
partFirst = part;
|
||||
partLast = part;
|
||||
}
|
||||
else
|
||||
{
|
||||
partLast->next = part;
|
||||
partLast = part;
|
||||
}
|
||||
}
|
||||
|
||||
void UpdateReader::HandleFile()
|
||||
{
|
||||
MD5 md5;
|
||||
@@ -192,6 +207,12 @@ void UpdateReader::HandleFile()
|
||||
md5.finalize();
|
||||
md5.hex_digest(real_checksum);
|
||||
|
||||
if (mdl.GetSize() == 0)
|
||||
{
|
||||
AddUpdateError("Zero-length file returned for \"%s\"", curfile.c_str());
|
||||
return;
|
||||
}
|
||||
|
||||
if (strcasecmp(checksum, real_checksum) != 0)
|
||||
{
|
||||
AddUpdateError("Checksums for file \"%s\" do not match:", curfile.c_str());
|
||||
@@ -199,45 +220,20 @@ void UpdateReader::HandleFile()
|
||||
return;
|
||||
}
|
||||
|
||||
char path[PLATFORM_MAX_PATH];
|
||||
smutils->BuildPath(Path_SM, path, sizeof(path), "gamedata/%s", curfile.c_str());
|
||||
|
||||
gameconfs->AcquireLock();
|
||||
|
||||
FILE *fp = fopen(path, "wt");
|
||||
if (fp == NULL)
|
||||
{
|
||||
gameconfs->ReleaseLock();
|
||||
AddUpdateError("Could not open %s for writing", path);
|
||||
return;
|
||||
}
|
||||
|
||||
fwrite(mdl.GetBuffer(), 1, mdl.GetSize(), fp);
|
||||
fclose(fp);
|
||||
|
||||
gameconfs->ReleaseLock();
|
||||
|
||||
AddUpdateMessage("Successfully updated gamedata file \"%s\"", curfile.c_str());
|
||||
UpdatePart *part = new UpdatePart;
|
||||
part->data = (char*)malloc(mdl.GetSize());
|
||||
part->file = strdup(curfile.c_str());
|
||||
part->length = mdl.GetSize();
|
||||
LinkPart(part);
|
||||
}
|
||||
|
||||
void UpdateReader::HandleFolder(const char *folder)
|
||||
{
|
||||
char path[PLATFORM_MAX_PATH];
|
||||
|
||||
smutils->BuildPath(Path_SM, path, sizeof(path), "gamedata/%s", folder);
|
||||
if (libsys->IsPathDirectory(path))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (!libsys->CreateFolder(path))
|
||||
{
|
||||
AddUpdateError("Could not create folder: %s", path);
|
||||
}
|
||||
else
|
||||
{
|
||||
AddUpdateMessage("Created folder \"%s\" from updater", folder);
|
||||
}
|
||||
UpdatePart *part = new UpdatePart;
|
||||
part->data = NULL;
|
||||
part->length = 0;
|
||||
part->file = strdup(folder);
|
||||
LinkPart(part);
|
||||
}
|
||||
|
||||
static bool md5_file(const char *file, char checksum[33])
|
||||
@@ -381,3 +377,14 @@ cleanup:
|
||||
delete xfer;
|
||||
delete form;
|
||||
}
|
||||
|
||||
UpdatePart *UpdateReader::DetachParts()
|
||||
{
|
||||
UpdatePart *first;
|
||||
|
||||
first = partFirst;
|
||||
partFirst = NULL;
|
||||
partLast = NULL;
|
||||
|
||||
return first;
|
||||
}
|
||||
|
||||
@@ -37,6 +37,14 @@
|
||||
#include <sh_string.h>
|
||||
#include "MemoryDownloader.h"
|
||||
|
||||
struct UpdatePart
|
||||
{
|
||||
UpdatePart* next;
|
||||
char *file;
|
||||
char *data;
|
||||
size_t length;
|
||||
};
|
||||
|
||||
namespace SourceMod
|
||||
{
|
||||
class UpdateReader :
|
||||
@@ -52,9 +60,11 @@ namespace SourceMod
|
||||
SMCResult ReadSMC_LeavingSection(const SMCStates *states);
|
||||
public:
|
||||
void PerformUpdate();
|
||||
UpdatePart *DetachParts();
|
||||
private:
|
||||
void HandleFile();
|
||||
void HandleFolder(const char *folder);
|
||||
void LinkPart(UpdatePart *part);
|
||||
private:
|
||||
IWebTransfer *xfer;
|
||||
MemoryDownloader mdl;
|
||||
@@ -63,6 +73,8 @@ namespace SourceMod
|
||||
SourceHook::String curfile;
|
||||
SourceHook::String url;
|
||||
char checksum[33];
|
||||
UpdatePart *partFirst;
|
||||
UpdatePart *partLast;
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -45,7 +45,6 @@ SMEXT_LINK(&g_Updater);
|
||||
|
||||
IWebternet *webternet;
|
||||
static List<String *> update_errors;
|
||||
static List<String *> update_messages;
|
||||
static IThreadHandle *update_thread;
|
||||
|
||||
bool SmUpdater::SDK_OnLoad(char *error, size_t maxlength, bool late)
|
||||
@@ -85,10 +84,6 @@ void SmUpdater::SDK_OnUnload()
|
||||
{
|
||||
iter = update_errors.erase(iter);
|
||||
}
|
||||
while (iter != update_messages.end())
|
||||
{
|
||||
iter = update_messages.erase(iter);
|
||||
}
|
||||
}
|
||||
|
||||
bool SmUpdater::QueryInterfaceDrop(SourceMod::SMInterface *pInterface)
|
||||
@@ -112,11 +107,54 @@ void SmUpdater::NotifyInterfaceDrop(SMInterface *pInterface)
|
||||
}
|
||||
}
|
||||
|
||||
static void LogAllMessages(void *data)
|
||||
static void PumpUpdate(void *data)
|
||||
{
|
||||
String *str;
|
||||
List<String *>::iterator iter;
|
||||
|
||||
char path[PLATFORM_MAX_PATH];
|
||||
UpdatePart *temp;
|
||||
UpdatePart *part = (UpdatePart*)data;
|
||||
while (part != NULL)
|
||||
{
|
||||
if (part->data == NULL)
|
||||
{
|
||||
smutils->BuildPath(Path_SM, path, sizeof(path), "gamedata/%s", part->file);
|
||||
if (libsys->IsPathDirectory(path))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
if (!libsys->CreateFolder(path))
|
||||
{
|
||||
AddUpdateError("Could not create folder: %s", path);
|
||||
}
|
||||
else
|
||||
{
|
||||
smutils->LogMessage(myself, "Created folder \"%s\" from updater", path);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
smutils->BuildPath(Path_SM, path, sizeof(path), "gamedata/%s", part->file);
|
||||
FILE *fp = fopen(path, "wt");
|
||||
if (fp == NULL)
|
||||
{
|
||||
AddUpdateError("Could not open %s for writing", path);
|
||||
return;
|
||||
}
|
||||
fwrite(part->data, 1, part->length, fp);
|
||||
fclose(fp);
|
||||
smutils->LogMessage(myself,
|
||||
"Successfully updated gamedata file \"%s\"",
|
||||
part->file);
|
||||
}
|
||||
temp = part->next;
|
||||
free(part->data);
|
||||
free(part->file);
|
||||
delete part;
|
||||
part = temp;
|
||||
}
|
||||
|
||||
if (update_errors.size())
|
||||
{
|
||||
smutils->LogError(myself, "--- BEGIN ERRORS FROM AUTOMATIC UPDATER ---");
|
||||
@@ -131,14 +169,6 @@ static void LogAllMessages(void *data)
|
||||
|
||||
smutils->LogError(myself, "--- END ERRORS FROM AUTOMATIC UPDATER ---");
|
||||
}
|
||||
|
||||
for (iter = update_messages.begin();
|
||||
iter != update_messages.end();
|
||||
iter++)
|
||||
{
|
||||
str = (*iter);
|
||||
smutils->LogMessage(myself, "%s", str->c_str());
|
||||
}
|
||||
}
|
||||
|
||||
void SmUpdater::RunThread(IThreadHandle *pHandle)
|
||||
@@ -147,28 +177,13 @@ void SmUpdater::RunThread(IThreadHandle *pHandle)
|
||||
|
||||
ur.PerformUpdate();
|
||||
|
||||
if (update_errors.size() || update_messages.size())
|
||||
{
|
||||
smutils->AddFrameAction(LogAllMessages, NULL);
|
||||
}
|
||||
smutils->AddFrameAction(PumpUpdate, ur.DetachParts());
|
||||
}
|
||||
|
||||
void SmUpdater::OnTerminate(IThreadHandle *pHandle, bool cancel)
|
||||
{
|
||||
}
|
||||
|
||||
void AddUpdateMessage(const char *fmt, ...)
|
||||
{
|
||||
va_list ap;
|
||||
char buffer[2048];
|
||||
|
||||
va_start(ap, fmt);
|
||||
smutils->FormatArgs(buffer, sizeof(buffer), fmt, ap);
|
||||
va_end(ap);
|
||||
|
||||
update_messages.push_back(new String(buffer));
|
||||
}
|
||||
|
||||
void AddUpdateError(const char *fmt, ...)
|
||||
{
|
||||
va_list ap;
|
||||
|
||||
Reference in New Issue
Block a user