Skip to content

Commit

Permalink
BROKEN: Moving installed-list over to Spread. WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
korslund committed Mar 28, 2013
1 parent fc81110 commit 83c9a34
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 85 deletions.
8 changes: 2 additions & 6 deletions app_wx/importer_backend.cpp
Expand Up @@ -167,8 +167,8 @@ static void file2GameList(vector<string> &games, const string &dir,
void Import::getGameList(vector<string> &games, const string &from,
Misc::Logger &log)
{
/* TODO: This does NOT currently work with games installed in
non-standard locations.
/* Note: This does NOT work with games installed in non-standard
locations.
*/
file2GameList(games, from, "installed.json", log);
file2GameList(games, from, "tiglib_installed.conf", log);
Expand Down Expand Up @@ -241,10 +241,6 @@ JobInfoPtr Import::importGame(const string &game,
string fromDir2 = (bf::path(from)/"games"/game).string();
string fromDir3 = (bf::path(from)/"gamedata"/game).string();

// TODO: When importing from new repositories, we should actually
// check the value in the tiglib_installed.conf file first as our
// source path.

string toDir = (bf::path(to)/"gamedata"/game).string();
JobInfoPtr info;

Expand Down
31 changes: 11 additions & 20 deletions tiglib/gamedata.cpp
Expand Up @@ -34,38 +34,29 @@ void GameData::createLiveData(Repo *repo)
int64_t maxTime = 0;
for(it = list.begin(); it != list.end(); it++)
{
const std::string &idname = it->first;
const TigData::TigEntry *ent = it->second;
LiveInfo *inf = new LiveInfo(ent, repo);
out[index++] = inf;
lookup[it->first] = inf;
lookup[idname] = inf;

if(ent->addTime > maxTime)
maxTime = ent->addTime;

inf->instSize = repo->getGameSize(ent->urlname);
}

// Store new maxtime
repo->setLastTime(maxTime);

// Apply install status
std::vector<std::string> games = repo->getGameList();
// Apply current status information
Repo::StatusList games;
repo->getStatusList(games);
for(int i=0; i<games.size(); i++)
{
const std::string &id = games[i];
if(repo->getGameDir(id) != "")
{
LiveInfo *l = get(id);
if(l) l->markAsInstalled();
}
const Repo::GameStatus &e = games[i];
LiveInfo *l = get(e.id);
if(!l) continue;
l->markAsInstalled(e.curVer, e.newVer, e.isUpdated);
}

// Signal child lists that data has changed
//allList.done();

/* Update: this was a bad idea. Because systems further down stream
may rely on the LiveInfo::extra member to be set, which of course
isn't the case at this point.
So do NOT signal child lists that we have changed. Leave that to
the caller.
*/
}
12 changes: 6 additions & 6 deletions tiglib/liveinfo.cpp
Expand Up @@ -9,7 +9,7 @@ namespace bs = boost::filesystem;
using namespace TigLib;

LiveInfo::LiveInfo(const TigData::TigEntry *e, Repo *_repo)
: ent(e), extra(NULL), repo(_repo), myRate(-2)
: ent(e), extra(NULL), instSize(0), repo(_repo), myRate(-2)
{
// Mark newly added games as 'new'.
sNew = ent->addTime > repo->getLastTime();
Expand Down Expand Up @@ -55,7 +55,7 @@ void LiveInfo::markAsInstalled()
std::string LiveInfo::getInstallDir() const
{
assert(isInstalled());
return repo->getGameDir(ent->idname);
return repo->getGameDir(ent->urlname);
}

int LiveInfo::getMyRating()
Expand Down Expand Up @@ -89,7 +89,7 @@ Spread::JobInfoPtr LiveInfo::install(bool async)
std::string instDir = repo->getDefGameDir(ent->idname);

// Ask the repository to start the install process
installJob = repo->startInstall(ent->idname, ent->urlname, instDir, async);
installJob = repo->startInstall(ent->urlname, instDir, async);
}

return installJob;
Expand All @@ -106,9 +106,9 @@ Spread::JobInfoPtr LiveInfo::install(bool async)
Spread::JobInfoPtr LiveInfo::update(bool async)
{
assert(isInstalled());
std::string instDir = repo->getGameDir(ent->idname);
std::string instDir = repo->getGameDir(ent->urlname);
assert(instDir != "");
return repo->startInstall(ent->idname, ent->urlname, instDir, async);
return repo->startInstall(ent->urlname, instDir, async);
}

Spread::JobInfoPtr LiveInfo::uninstall(bool async)
Expand All @@ -124,7 +124,7 @@ Spread::JobInfoPtr LiveInfo::uninstall(bool async)
installJob->reset();

// Tell the repository to uninstall this game
return repo->startUninstall(ent->idname, async);
return repo->startUninstall(ent->urlname, async);
}

void LiveInfo::launch() const
Expand Down
14 changes: 11 additions & 3 deletions tiglib/liveinfo.hpp
Expand Up @@ -25,6 +25,9 @@ namespace TigLib
// user-data.
void *extra;

// Total file size after install
uint64_t instSize;

bool isInstalled() const;
bool isUninstalled() const;
bool isWorking() const;
Expand Down Expand Up @@ -104,9 +107,14 @@ namespace TigLib
*/
void launch() const;

// Mark this game as installed. Called on installed games at
// startup.
void markAsInstalled();
/* Mark this game as installed. Called on installed games at
startup.
If isUpdated (and optionally newVer) is set, then there is a
new version available for this game.
*/
void markAsInstalled(const std::string &curVer, const std::string &newVer,
bool isUpdated);

// Set to true if this game was added since our last repo load.
// Allows clients to notify users about newly added games.
Expand Down
110 changes: 68 additions & 42 deletions tiglib/repo.cpp
Expand Up @@ -16,7 +16,7 @@ using namespace Spread;

namespace bf = boost::filesystem;

//#define PRINT_DEBUG
#define PRINT_DEBUG
#ifdef PRINT_DEBUG
#include <iostream>
#define PRINT(a) std::cout << a << "\n"
Expand Down Expand Up @@ -146,7 +146,6 @@ bool Repo::initRepo(bool forceLock)

// Open config files
conf.load(getPath("tiglib.conf"));
inst.load(getPath("tiglib_installed.conf"));
news.load(getPath("tiglib_news.conf"));
rates.load(getPath("tiglib_rates.conf"));

Expand All @@ -156,6 +155,34 @@ bool Repo::initRepo(bool forceLock)
return true;
}

void Repo::convertOldInstallConf() const
{
// Convert old install list, if present
std::string instFile = getPath("tiglib_installed.conf");
std::string oldF = instFile+".old", newF = instFile+".new";
if(!bf::exists(instFile) && !bf::exists(oldF) && !bf::exists(newF))
return;

PRINT("Converting old install list conf");
blah; // More output here

Misc::JConfig inst(instFile);
std::vector<std::string> names = inst.getNames();
for(int i=0; i<names.size(); i++)
{
const std::string &id = names[i];
std::string path = inst.get(id);
if(path != "" && id.size() > 11 && id.substr(0,11) == "tiggit.net/")
{
std::string urlname = id.substr(11);
ptr->spread.install("tiggit.net", urlname, path, NULL, false);
}
}
bf::remove(instFile);
bf::remove(oldF);
bf::remove(newF);
}

void Repo::setLastTime(int64_t val)
{
// Store as binary data, since 64 bit int support in general is
Expand Down Expand Up @@ -268,26 +295,19 @@ JobInfoPtr Repo::fetchFiles(bool includeShots, bool async)
&ptr->newData), async);
}

std::string Repo::getGameDir(const std::string &idname)
std::string Repo::getGameDir(const std::string &urlname) const
{
if(inst.has(idname))
{
std::string val = inst.get(idname);

// Check for old int values
std::string newVal = val;
if(val == "0") newVal = "";
else if(val == "2") newVal = getDefGameDir(idname);

// Update the stored value if it was altered
if(newVal != val)
inst.set(idname, newVal);

return newVal;
}
// TODO: NOT urlname!
const SpreadLib::PackStatus *is = ptr->spread.getPackStatus("tiggit.net", urlname);
if(is) return is->path;
return "";
}

uint64_t Repo::getGameSize(const std::string &urlname) const
{
return ptr->spread.getPackInfo("tiggit.net", urlname).installSize;
}

void Repo::loadStats()
{
// Always ignore errors, stats aren't critically important
Expand All @@ -310,6 +330,7 @@ void Repo::loadData()
ptr->data.data.clear();

ptr->data.data.addChannel("tiggit.net", tigFile);
convertOldInstallConf();
loadStats();
ptr->data.createLiveData(this);
}
Expand All @@ -324,15 +345,15 @@ struct InstallJob : Job
{
std::string sendOnDone;
JobInfoPtr client;
std::string idname, where;
Misc::JConfig *inst;

void doJob()
{
if(waitClient(client)) return;

// Set config status
inst->set(idname, where);
client->wait(info);
if(checkStatus() || !client->isSuccess())
{
setError("Install failed");
return;
}

// Notify the server that the game was downloaded
Fetch::fetchString(sendOnDone, true);
Expand All @@ -341,21 +362,35 @@ struct InstallJob : Job
}
};

void Repo::getStatusList(StatusList &list) const
{
const SpreadLib::StatusList &inp = spread.getStatusList("tiggit.net");
list.resize(inp.size());
for(int i=0; i<list.size(); i++)
{
GameStatus &s = list[i];
const SpreadLib::PackStatus &is = inp[i];
assert(is.pack->channel == "tiggit.net");
s.id = "tiggit.net/" + is.pack->package;
s.curVer = is.curVer;
s.newVer = is.newVer;
s.path = is.path;
s.isUpdated = is.isUpdated;
}
}

// Start installing or upgrading a game
JobInfoPtr Repo::startInstall(const std::string &idname, const std::string &urlname,
std::string where, bool async)
JobInfoPtr Repo::startInstall(const std::string &urlname, std::string where, bool async)
{
// Ignore offline mode for game installs, since they are user
// initiated events.
where = bf::absolute(where).string();
JobInfoPtr client = ptr->spread.install("tiggit.net", idname, where);
JobInfoPtr client = ptr->spread.install("tiggit.net", urlname, where, NULL, async);
InstallJob *job = new InstallJob;
job->client = client;
job->sendOnDone = ServerAPI::dlCountURL(urlname);
job->where = where;
job->idname = idname;
job->inst = &inst;
return Thread::run(job,async);
Thread::run(job, async);
return client;
}

struct RemoveJob : Job
Expand All @@ -378,16 +413,7 @@ JobInfoPtr Repo::killPath(const std::string &dir, bool async)
return Thread::run(new RemoveJob(dir), async);
}

// Start uninstalling a game
JobInfoPtr Repo::startUninstall(const std::string &idname, bool async)
JobInfoPtr Repo::startUninstall(const std::string &urlname, bool async)
{
// Get the game's install dir
std::string dir = getGameDir(idname);
if(dir == "") return JobInfoPtr();

// Mark the game as uninstalled immediately
inst.set(idname, "");

// Kill the installation directory
return killPath(dir, async);
return ptr->spread.uninstall("tiggit.net", urlname, async);
}
33 changes: 25 additions & 8 deletions tiglib/repo.hpp
Expand Up @@ -19,12 +19,21 @@ namespace TigLib

std::string dir;
std::string tigFile, statsFile, newsFile, shotDir, spreadDir;
Misc::JConfig conf, inst;
Misc::JConfig conf;
int64_t lastTime;

void setDirs();

public:

struct GameStatus
{
std::string id, curVer, newVer;
bool isUpdated;
};

typedef std::vector<GameStatus> StatusList;

Repo(bool runOffline=false)
: offline(runOffline) {}

Expand Down Expand Up @@ -161,24 +170,26 @@ namespace TigLib
std::string fetchPath(const std::string &url,
const std::string &fname);

// Returns idnames of all games that are or have been installed.
// Use getGameDir() to check actual install status of the game.
std::vector<std::string> getGameList() { return inst.getNames(); }

// Get actuall install dir for a game. Returns "" if the game is
// not registered as installed.
std::string getGameDir(const std::string &idname);
std::string getGameDir(const std::string &urlname) const;

// Get the complete install size of a given package
uint64_t getGameSize(const std::string &urlname) const;

// Get default install dir for a game
std::string getDefGameDir(const std::string &idname) const
{ return getPath("gamedata/" + idname); }

// Get the status of all installed games, based on latest
// up-to-date information
void getStatusList(StatusList &list) const;

// Get screenshot path for a game.
std::string getScreenshot(const std::string &idname) const;

// Start installing a game
Spread::JobInfoPtr startInstall(const std::string &idname,
const std::string &urlname,
Spread::JobInfoPtr startInstall(const std::string &urlname,
std::string where,
bool async=true);

Expand Down Expand Up @@ -212,6 +223,12 @@ namespace TigLib
// Set new lastTime. Does NOT change the current lastTime field,
// but instead stores the value in conf for our next run.
void setLastTime(int64_t val);

private:
/* Load the legacy file tiglib_installed.conf, if it exists, and
convert it into the new spread system. Called from loadData().
*/
void convertOldInstallConf() const;
};
}
#endif

0 comments on commit 83c9a34

Please sign in to comment.