Skip to content

Commit

Permalink
Finished repo movement function. Also attempted to fix .bat execution…
Browse files Browse the repository at this point in the history
…. Windows testing remains.
  • Loading branch information
korslund committed Dec 18, 2012
1 parent aa944dc commit 9c3f62b
Show file tree
Hide file tree
Showing 13 changed files with 184 additions and 68 deletions.
4 changes: 2 additions & 2 deletions app_wx/appupdate.cpp
Expand Up @@ -227,8 +227,8 @@ Spread::JobInfoPtr AppUpdater::startJob()

bool AppUpdater::launchCorrectExe()
{
// This is meand to be run at program startup, so we assume no
// values have been set yet
// This is ment to be run at program startup, so we assume no values
// have been set yet
assert(!hasNewUpdate);
assert(!current);
assert(newExePath == "");
Expand Down
69 changes: 42 additions & 27 deletions app_wx/gamedata.cpp
Expand Up @@ -4,7 +4,11 @@
#include "notifier.hpp"
#include "misc/dirfinder.hpp"
#include "importer_gui.hpp"
#include <boost/filesystem.hpp>
#include <spread/misc/readjson.hpp>
#include "launcher/run.hpp"

namespace bf = boost::filesystem;
using namespace TigData;
using namespace wxTigApp;
using namespace TigLib;
Expand Down Expand Up @@ -182,10 +186,45 @@ bool wxTigApp::GameData::moveRepo(const std::string &newPath)

try
{
// Import main data (games and screenshots). The last 'false'
// parameter means 'do not delete source files'.
if(!ImportGui::importRepoGui(repo.getPath(), newPath, &repo.getSpread(), false))
Spread::SpreadLib *spread = &repo.getSpread();

// Import main data (games, screenshots and config files.) The
// last 'false' parameter means 'do not delete source files'.
if(!ImportGui::importRepoGui(repo.getPath(), newPath, spread, false))
return true;

bf::path oldP = repo.getPath(), newP = newPath;

// Next, copy executables and spread files.
if(!ImportGui::copyFilesGui((oldP/"run").string(), (newP/"run").string(),
spread, "Copying executables"))
return true;
if(!ImportGui::copyFilesGui((oldP/"spread/channels").string(),
(newP/"spread/channels").string(),
spread, "Copying Tiggit data"))
return true;

bf::copy_file(oldP/"spread/cache.conf", newP/"spread/cache.conf");

/* Create a cleanup file in the new repo. This will ask the user
if they want to delete the old repository after we've
restarted.
*/
ReadJson::writeJson((newP/"cleanup.json").string(), oldP.string(), true);

// Finally, switch the globally stored path string over to the
// new location. This makes this the "official" repository from
// now on.
repo.setStoredPath(newPath);

// Notify the user that we are restarting from the new location
Boxes::say("Tiggit will now restart for changes to take effect");

// Launch the new exe
newP = newP / "run" / "1";
try { Launcher::run((newP/"tiggit.exe").string(), newP.string()); }
catch(std::exception &e) { Boxes::error(e.what()); }
frame->Close();
}
catch(std::exception &e)
{
Expand All @@ -196,30 +235,6 @@ bool wxTigApp::GameData::moveRepo(const std::string &newPath)
Boxes::error("An unknown error occured");
}

// Test this process on its own. Use diffing and check that
// EVERYTHING is copied to the new location. Don't switch anything
// over yet. We also have to make sure we catch, report and abort on
// ANY error (a catch-all try block should work). An abort still
// returns 'true', so it doesn't loop the dialog box.

// The only files missing will be the log files, all .old files, and
// cache.conf. EVERYTHING else, including run/*, should be
// present. Actually we have to make sure we copy the 'current' file
// correctly though. We also create a cleanup.json file in the new
// repo.

// The final action is to write the cache file, set the global dir
// pointer, then restart. Say: "Tiggit will now restart for changes
// to take effect. [Ok]"

// Reuse whatever launch code we're already using to restart, but
// don't use appupdater, since that's too tied to the repo. We're
// not switching repo dirs internally, we're just setting it up for
// ANOTHER process to work off the new repo! THAT is clean
// transactional thinking!

// Finally consider moving this entire thing to a separate file as
// well. We're already using the importer for much of the work.
return true;
}

Expand Down
77 changes: 47 additions & 30 deletions app_wx/importer_backend.cpp
Expand Up @@ -47,9 +47,9 @@ struct Copy
if(info) info->setProgress(cur, tot);
}

void copyFiles(const string &from, const string &to, bool addPng=false)
void doCopyFiles(const string &from, const string &to, bool addPng=false)
{
log("copyFiles FROM=" + from + " TO=" + to);
log("doCopyFiles FROM=" + from + " TO=" + to);

assert(spread);
assert(!info || info->isBusy());
Expand Down Expand Up @@ -127,17 +127,6 @@ struct Copy
}
};

void Import::copyFiles(const string &from, const string &to, bool addPng,
SpreadLib *spread, JobInfoPtr info,
Misc::Logger &logger)
{
Copy cpy;
cpy.spread = spread;
cpy.info = info;
cpy.logger = &logger;
cpy.copyFiles(from, to, addPng);
}

static void file2GameList(vector<string> &games, const string &dir,
const std::string &name, Misc::Logger &log)
{
Expand Down Expand Up @@ -192,16 +181,26 @@ struct CopyJob : Spread::Job
SpreadLib *spread;
bool addPng;

CopyJob() : log(NULL), addPng(false) {}

void doJob()
{
setBusy("Copying files");
Import::copyFiles(fromDir, toDir, addPng, spread, info, *log);
assert(spread);

Copy cpy;
cpy.spread = spread;
cpy.info = info;
cpy.logger = log;
cpy.doCopyFiles(fromDir, toDir, addPng);

if(checkStatus()) return;

// Success. Write the entry to the output config file, if any.
if(outConf != "" && idname != "")
{
(*log)("Updating " + outConf + " with " + idname + "=" + toDir);
if(log)
(*log)("Updating " + outConf + " with " + idname + "=" + toDir);
JConfig conf(outConf);
conf.set(idname, toDir);
}
Expand All @@ -210,6 +209,22 @@ struct CopyJob : Spread::Job
}
};

JobInfoPtr Import::copyFiles(const string &from, const string &to,
SpreadLib *spread, Misc::Logger *logger)
{
if(logger) (*logger)("Copying " + from + " to " + to);

if(bf::equivalent(from, to))
return JobInfoPtr();

CopyJob *job = new CopyJob;
job->fromDir = from;
job->toDir = to;
job->log = logger;
job->spread = spread;
return Thread::run(job);
}

JobInfoPtr Import::importGame(const string &game,
const string &from, const string &to,
SpreadLib *spread, Misc::Logger &log, bool async)
Expand Down Expand Up @@ -417,22 +432,22 @@ void Import::cleanup(const string &from, const vector<string> &games,
}
}

static void mergeConf(const std::string &fromDir,
static void copyFile(const std::string &fromDir,
const std::string &toDir,
const std::string &name)
{
string infile = (bf::path(fromDir)/name).string();
string outfile = (bf::path(toDir)/name).string();
try
{
bf::path from = fromDir;
bf::path to = toDir;

JConfig input(infile, true);
JConfig output(outfile);
from /= name;
to /= name;

vector<string> names = input.getNames();
for(int i=0; i<names.size(); i++)
{
const string &name = names[i];
if(!output.has(name)) output.set(name, input.get(name));
if(exists(from) && !exists(to))
copy_file(from, to);
}
catch(...) {}
}

void Import::importConfig(const string &from, const string &to,
Expand All @@ -451,11 +466,13 @@ void Import::importConfig(const string &from, const string &to,

using namespace Misc;

// Convert new config files first
mergeConf(from, to, "tiglib.conf");
mergeConf(from, to, "tiglib_news.conf");
mergeConf(from, to, "tiglib_rates.conf");
mergeConf(from, to, "wxtiggit.conf");
// Copy over new files first, if present
copyFile(from, to, "tiglib.conf");
copyFile(from, to, "tiglib_news.conf");
copyFile(from, to, "tiglib_rates.conf");
copyFile(from, to, "wxtiggit.conf");
copyFile(from, to, "stats.json");
copyFile(from, to, "news.json");

// Convert main config file
try
Expand Down
5 changes: 3 additions & 2 deletions app_wx/importer_backend.hpp
Expand Up @@ -24,8 +24,9 @@ namespace Import
void cleanup(const std::string &from, const std::vector<std::string> &games,
Misc::Logger &logger);

void copyFiles(const std::string &from, const std::string &to, bool addPng,
Spread::SpreadLib *spread, Spread::JobInfoPtr info, Misc::Logger &logger);
Spread::JobInfoPtr copyFiles(const std::string &from, const std::string &to,
Spread::SpreadLib *spread,
Misc::Logger *logger = NULL);
}

#endif
22 changes: 22 additions & 0 deletions app_wx/importer_gui.cpp
Expand Up @@ -43,6 +43,28 @@ void ImportGui::doUserCleanup(const std::string &repoDir)
catch(...) {}
}

bool ImportGui::copyFilesGui(const std::string &from, const std::string &to,
Spread::SpreadLib *spread, const std::string &text)
{
assert(spread);
if(bf::equivalent(from, to))
return false;

JobInfoPtr info = Import::copyFiles(from, to, spread);
if(info)
{
wxTigApp::JobProgress prog(info);
if(!prog.start(text))
{
if(info->isError())
Boxes::error("Copy failure: " + info->getMessage());
return false;
}
return true;
}
return false;
}

bool ImportGui::importRepoGui(const string &from, const string &to, SpreadLib *spread,
bool doCleanup)
{
Expand Down
3 changes: 3 additions & 0 deletions app_wx/importer_gui.hpp
Expand Up @@ -8,6 +8,9 @@ namespace ImportGui
bool importRepoGui(const std::string &from, const std::string &to,
Spread::SpreadLib *spread, bool doCleanup);

bool copyFilesGui(const std::string &from, const std::string &to,
Spread::SpreadLib *spread, const std::string &text);

void doUserCleanup(const std::string &repoDir);
}

Expand Down
16 changes: 13 additions & 3 deletions app_wx/notifier.cpp
Expand Up @@ -50,22 +50,32 @@ void StatusNotifier::cleanup()
// Disable the loop
data = NULL;

// True if there was anything to abort
bool abort = false;

// Abort the update job, if any
if(updateJob && !updateJob->isFinished())
updateJob->abort();
{
updateJob->abort();
abort = true;
}

// Abort any other job
WatchList::iterator it;
for(it = watchList.begin(); it != watchList.end(); it++)
{
JobInfoPtr job = it->second;
if(job && !job->isFinished())
job->abort();
{
job->abort();
abort = true;
}
}

// Give jobs some time to finish. This is run after the main window
// closes, so a delay before exiting won't disturb the user.
wxSleep(1);
if(abort)
wxSleep(1);
}

void StatusNotifier::reassignJobs()
Expand Down
2 changes: 1 addition & 1 deletion app_wx/version.hpp
@@ -1,6 +1,6 @@
#ifndef __TIGGIT_VERSION_HPP_
#define __TIGGIT_VERSION_HPP_

#define TIGGIT_VERSION "0.87 beta"
#define TIGGIT_VERSION "0.88 beta"

#endif
5 changes: 5 additions & 0 deletions app_wx/wxtiggit_main.cpp
Expand Up @@ -286,6 +286,11 @@ struct TigApp : wxApp
// Check for and act on cleanup instructions.
ImportGui::doUserCleanup(rep.getPath());

// TODO: Reaffirm the stored path. This might be needed on
// Linux, where the path is stored in a file. In some cases
// the user cleanup above can delete that file.
//rep.setStoredPath(rep.getPath());

return true;
}
catch(std::exception &e)
Expand Down
25 changes: 25 additions & 0 deletions launcher/run_windows.cpp
Expand Up @@ -25,8 +25,33 @@ static std::string getWinError(int errCode, const std::string &prepend = "")
return res;
}

static bool icmp(char a, char b)
{
if(a >= 'A' && a <= 'Z') a += 'a'-'A';
if(b >= 'A' && b <= 'Z') b += 'a'-'A';
return a == b;
}

static bool iends(const std::string &str, const std::string &ending)
{
if(str.size() < ending.size()) return false;

std::string tmp = str.substr(str.size()-ending.size());
assert(tmp.size() == ending.size());
for(int i=0; i<tmp.size(); i++)
if(!icmp(tmp[i], ending[i])) return false;
return true;
}

void Launcher::win32_run(const std::string &command, const std::string &workdir)
{
// Check if the command is a .bat file
if(iends(command, ".bat"))
{
ShellExecute(NULL, NULL, (char*)command.c_str(), NULL, (workdir==""?NULL:workdir.c_str()), SW_SHOWNORMAL);
return;
}

STARTUPINFO si;
PROCESS_INFORMATION pi;

Expand Down
5 changes: 5 additions & 0 deletions tiglib/repo.cpp
Expand Up @@ -83,6 +83,11 @@ bool Repo::findRepo(const std::string &where)
return true;
}

bool Repo::setStoredPath(const std::string &newPath)
{
return TigLibInt::setStoredPath(newPath);
}

void Repo::setDirs()
{
assert(dir != "");
Expand Down

0 comments on commit 9c3f62b

Please sign in to comment.