Added a "Check for updates" option to the Help menu.

This commit is contained in:
skidau 2015-06-05 13:48:37 +00:00
parent 811fc9a872
commit 1dc399839c
8 changed files with 469 additions and 222 deletions

File diff suppressed because it is too large Load Diff

View File

@ -1,6 +1,15 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#ifndef _WIN32
#include <unistd.h>
#include <sys/stat.h>
#else // _WIN32
#include <io.h>
#include <direct.h>
#endif // _WIN32
#include <zlib.h>
#ifndef NO_PNG
@ -44,6 +53,16 @@ static int (ZEXPORT *utilGzReadFunc)(gzFile, voidp, unsigned int) = NULL;
static int (ZEXPORT *utilGzCloseFunc)(gzFile) = NULL;
static z_off_t (ZEXPORT *utilGzSeekFunc)(gzFile, z_off_t, int) = NULL;
bool FileExists(const char *filename)
{
#ifdef _WIN32
return (_access(filename, 0) != -1);
#else
struct stat buffer;
return (stat(filename, &buffer) == 0);
#endif
}
void utilReadScreenPixels(u8* dest, int w, int h)
{
u8* b = dest;
@ -615,6 +634,67 @@ u8 *utilLoad(const char *file,
return image;
}
void replaceAll(std::string& str, const std::string& from, const std::string& to) {
if (from.empty())
return;
size_t start_pos = 0;
while ((start_pos = str.find(from, start_pos)) != std::string::npos) {
str.replace(start_pos, from.length(), to);
start_pos += to.length(); // In case 'to' contains 'from', like replacing 'x' with 'yx'
}
}
void utilExtract(const char* filepath, const char* filename)
{
fex_t* fex;
std::string archive_name(filepath);
archive_name.append(filename);
fex_open(&fex, archive_name.c_str());
while (!fex_done(fex))
{
std::string extracted_filename(filepath);
extracted_filename.append(fex_name(fex));
#ifdef WIN32
replaceAll(extracted_filename, "/", "\\");
#endif
std::string new_dir(filepath);
new_dir.append(fex_name(fex));
#ifdef WIN32
replaceAll(new_dir, "/", "\\");
#endif
new_dir = new_dir.substr(0, new_dir.find_last_of("\\"));
if (!FileExists(new_dir.c_str()))
mkdir(new_dir.c_str()
#ifndef WIN32
,0777
#endif
);
if (FileExists(extracted_filename.c_str()))
{
std::string new_name(filepath);
new_name.append("old-");
new_name.append(fex_name(fex));
#ifdef WIN32
replaceAll(new_name, "/", "\\");
#endif
remove(new_name.c_str());
rename(extracted_filename.c_str(), new_name.c_str());
}
FILE *extracted_file = fopen(extracted_filename.c_str(), "wb");
const void* p;
fex_data(fex, &p);
fwrite(p, fex_size(fex), 1, extracted_file);
fclose(extracted_file);
fex_next(fex);
}
fex_close(fex);
}
void utilWriteInt(gzFile gzFile, int i)
{
utilGzWrite(gzFile, &i, sizeof(int));

View File

@ -14,6 +14,9 @@ typedef struct {
void *address;
int size;
} variable_desc;
bool FileExists(const char *filename);
void utilReadScreenPixels(u8* dest, int w, int h);
bool utilWritePNGFile(const char *, int, int, u8 *);
bool utilWriteBMPFile(const char *, int, int, u8 *);
@ -25,6 +28,7 @@ bool utilIsZipFile(const char *);
void utilStripDoubleExtension(const char *, char *);
IMAGE_TYPE utilFindType(const char *);
uint8_t *utilLoad(const char *, bool (*)(const char*), uint8_t *, int &);
void utilExtract(const char* filepath, const char* filename);
void utilPutDword(uint8_t *, uint32_t);
void utilPutWord(uint8_t *, uint16_t);

View File

@ -30,10 +30,8 @@ extern "C" {
#include "../Util.h"
#ifndef _WIN32
#include <unistd.h>
#define GETCWD getcwd
#else // _WIN32
#include <io.h>
#include <direct.h>
#define GETCWD _getcwd
#define snprintf sprintf
@ -615,16 +613,6 @@ void CloseConfig()
iniparser_freedict(preferences);
}
bool FileExists(const char *filename)
{
#ifdef _WIN32
return (_access(filename, 0) != -1);
#else
struct stat buffer;
return (stat(filename, &buffer) == 0);
#endif
}
char* FindConfigFile(char *name)
{
char buffer[4096];

View File

@ -12,6 +12,7 @@
#include <wx/regex.h>
#include <wx/numdlg.h>
#include <wx/progdlg.h>
#include <wx/url.h>
#ifndef NO_FFMPEG
extern "C" {
@ -2498,6 +2499,30 @@ EVT_HANDLER(UpdateRDB, "Update ROM database")
}
}
EVT_HANDLER(UpdateEmu, "Check for updates")
{
#ifndef __WXMSW__
int ret = wxMessageBox(_("Online updates are available on Windows only. Please browse this site for updates.\n\nhttps://sourceforge.net/projects/vbam/files/latest/download"),
_("Online Update"), wxOK | wxICON_INFORMATION);
return;
#endif
wxString update_url = CheckForUpdates(_T("sourceforge.net"), _T("/projects/vbam/files/latest/download"));
if (!update_url.IsEmpty())
{
int ret = wxMessageBox(_("A new update is available. To update, VisualBoyAdvance-M must be Run as administrator. Would you like to download and update VisualBoyAdvance-M?\n\nhttps://sourceforge.net/projects/vbam/files/latest/download"),
_("New Update Available"), wxYES_NO | wxICON_QUESTION);
if (ret == wxYES)
{
wxURL url(update_url);
UpdateFile(url.GetServer(), url.GetPath());
ret = wxMessageBox(_("The update has been downloaded and installed. Please restart VisualBoyAdvance-M."),
_("Update Downloaded"), wxOK | wxICON_INFORMATION);
}
}
}
// was About
EVT_HANDLER(wxID_ABOUT, "About...")
{
@ -2505,6 +2530,7 @@ EVT_HANDLER(wxID_ABOUT, "About...")
ai.SetName(wxT("VisualBoyAdvance-M"));
wxString version = wxT("");
#ifndef FINAL_BUILD
if (!version.IsEmpty())
version = version + wxT("-");

View File

@ -17,6 +17,8 @@
#include <wx/regex.h>
#include <wx/protocol/http.h>
#include <wx/zipstrm.h>
#include <wx/progdlg.h>
#include <wx/url.h>
// The built-in xrc file
#include "builtin-xrc.h"
@ -36,7 +38,7 @@ static void get_config_path(wxPathList &path, bool exists = true)
wxStandardPathsBase &stdp = wxStandardPaths::Get();
#define add_path(p) do { \
const wxString& s = stdp.p; \
if(!exists || wxDirExists(s)) \
if((!exists || wxDirExists(s)) && wxIsWritable(s)) \
path.Add(s); \
} while(0)
// NOTE: this does not support XDG (freedesktop.org) paths
@ -661,6 +663,110 @@ void MainFrame::DownloadFile(wxString host, wxString url)
get.Close();
}
void MainFrame::UpdateFile(wxString host, wxString url)
{
wxProgressDialog progress(_T("Downloading..."), _T("Please wait while the VisualBoyAdvance-M update is downloading."));
wxHTTP get;
get.SetHeader(_T("Content-type"), _T("text/html; charset=utf-8"));
get.SetTimeout(10);
while (!get.Connect(host))
wxSleep(5);
wxInputStream* httpStream = get.GetInputStream(url);
if (get.GetError() == wxPROTO_NOERR)
{
if (httpStream != NULL)
{
size_t size = httpStream->GetSize();
char* data = new char[size];
wxFileName name(wxStandardPaths::Get().GetPluginsDir(), url.AfterLast('/'), wxEmptyString);
if (httpStream)
{
size_t chunks = 100;
size_t chunkSize = httpStream->GetSize() / chunks;
char* fileContent = new char[chunkSize];
#if (wxMAJOR_VERSION >= 3)
progress.SetRange(chunks);
#endif
wxFFile file(name.GetFullPath(), _T("wb"));
for (size_t i = 0; i <= chunks; i++)
{
progress.Update(i);
httpStream->Read(fileContent, chunkSize);
file.Write(fileContent, httpStream->LastRead());
}
file.Flush();
wxDELETE(fileContent);
}
if (name.FileExists() && name.GetFullName().Lower().Contains(wxT(".7z")))
{
utilExtract(name.GetPathWithSep().mb_str(wxConvUTF8), name.GetFullName().mb_str(wxConvUTF8));
}
delete[] data;
delete httpStream;
}
}
get.Close();
}
wxString MainFrame::CheckForUpdates(wxString host, wxString url)
{
wxString update_url = wxT("");
wxHTTP get;
get.SetHeader(_T("Content-type"), _T("text/html; charset=utf-8"));
#ifdef __WXMSW__
get.SetHeader(wxT("User-Agent"), wxT("Mozilla/5.0 (Windows NT 6.1; WOW64; rv:31.0) Gecko/20100101 Firefox/31.0"));
#endif
get.SetTimeout(10);
while (!get.Connect(host))
wxSleep(5);
wxInputStream* httpStream = get.GetInputStream(url);
if (get.GetError() == wxPROTO_NOERR)
{
if (httpStream != NULL)
{
size_t size = httpStream->GetSize();
if (httpStream)
{
wxTextInputStream text(*httpStream, wxT("\x09"), wxConvUTF8);
while (httpStream->IsOk() && !httpStream->Eof() && update_url.IsEmpty())
{
wxString line = text.ReadLine();
if (line.Contains(wxT("direct-download")))
{
wxURL redirect_url(line.SubString(line.Find(wxT("http://")), line.Find(wxT("\" "))).BeforeLast('\"'));
update_url = CheckForUpdates(redirect_url.GetServer(), redirect_url.GetPath() + wxT("?") + redirect_url.GetQuery());
}
if (line.Contains(wxT(" redirected ")))
{
update_url = line.SubString(line.Find(wxT("http://")), line.Find(wxT(";"))).BeforeLast(';');
}
}
}
delete httpStream;
}
}
get.Close();
return update_url;
}
wxString MainFrame::GetGamePath(wxString path)
{
wxString game_path = path;

View File

@ -317,6 +317,8 @@ private:
wxDialog* LoadXRCropertySheetDialog(const char* name);
void DownloadFile(wxString host, wxString url);
void UpdateFile(wxString host, wxString url);
wxString CheckForUpdates(wxString host, wxString url);
#include "cmdhandlers.h"
};

View File

@ -624,6 +624,9 @@
<object class="wxMenuItem" name="UpdateRDB">
<label>Update ROM database</label>
</object>
<object class="wxMenuItem" name="UpdateEmu">
<label>Check for updates</label>
</object>
<object class="separator"/>
<object class="wxMenuItem" name="wxID_ABOUT"/>
</object>