Skip to content

Commit

Permalink
Downloader and Installer implementations for itch.io games (tkash…
Browse files Browse the repository at this point in the history
  • Loading branch information
tkashkin committed Aug 28, 2019
1 parent 2ac609d commit 40adea8
Show file tree
Hide file tree
Showing 25 changed files with 634 additions and 346 deletions.
11 changes: 4 additions & 7 deletions data/com.github.tkashkin.gamehub.gschema.xml
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,10 @@
<default>'~/.config/itch'</default>
<summary>itch.io config/installation directory</summary>
</key>
<key name="itch-games" type="s">
<default>'~/Games/itch'</default>
<summary>itch.io games directory</summary>
</key>
</schema>

<!-- Paths / Collection -->
Expand Down Expand Up @@ -257,13 +261,6 @@
</key>
</schema>

<schema path="/com/github/tkashkin/gamehub/paths/collection/itch/" id="com.github.tkashkin.gamehub.paths.collection.itch">
<key name="game-dir" type="s">
<default>'$root/Itch/$game'</default>
<summary>itch.io collection: game directory</summary>
</key>
</schema>

<!-- Controller -->
<schema path="/com/github/tkashkin/gamehub/controller/" id="com.github.tkashkin.gamehub.controller">
<key name="enabled" type="b">
Expand Down
2 changes: 1 addition & 1 deletion src/data/Game.vala
Original file line number Diff line number Diff line change
Expand Up @@ -549,7 +549,7 @@ namespace GameHub.Data
case Game.State.INSTALLED: return C_("status", "Installed") + (game != null && game.version != null ? @": $(game.version)" : "");
case Game.State.INSTALLING: return C_("status", "Installing");
case Game.State.VERIFYING_INSTALLER_INTEGRITY: return C_("status", "Verifying installer integrity");
case Game.State.DOWNLOADING: return download != null ? download.status.description : C_("status", "Download started");
case Game.State.DOWNLOADING: return download != null && download.status != null && download.status.description != null ? download.status.description : C_("status", "Download started");
}
return C_("status", "Not installed");
}
Expand Down
6 changes: 3 additions & 3 deletions src/data/Runnable.vala
Original file line number Diff line number Diff line change
Expand Up @@ -580,7 +580,7 @@ namespace GameHub.Data
{
FSUtils.mkdir(part.local.get_parent().get_path());

var ds_id = Downloader.get_instance().download_started.connect(dl => {
var ds_id = Downloader.download_manager().file_download_started.connect(dl => {
if(dl.remote != part.remote) return;
if(game != null)
{
Expand All @@ -599,7 +599,7 @@ namespace GameHub.Data
}

var info = new Downloader.DownloadInfo(runnable.name, partDesc, game != null ? game.icon : null, null, null, game != null ? game.source.icon : null);
var file = yield Downloader.download(part.remote, part.local, info);
var file = yield Downloader.download_file(part.remote, part.local, info);
if(file != null && file.query_exists())
{
string? file_checksum = null;
Expand Down Expand Up @@ -643,7 +643,7 @@ namespace GameHub.Data
warning("Checksum mismatch in `%s`, skipping; expected: `%s`, actual: `%s`", file.get_basename(), part.checksum, file_checksum);
}
}
Downloader.get_instance().disconnect(ds_id);
Downloader.download_manager().disconnect(ds_id);

p++;
}
Expand Down
2 changes: 1 addition & 1 deletion src/data/compat/WineWrap.vala
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ namespace GameHub.Data.Compat

try
{
var wrapper = yield Downloader.download(wrapper_remote, wrapper_local);
var wrapper = yield Downloader.download_file(wrapper_remote, wrapper_local);

if(wrapper == null || !wrapper.query_exists()) return;

Expand Down
8 changes: 4 additions & 4 deletions src/data/sources/gog/GOGGame.vala
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ namespace GameHub.Data.Sources.GOG

public override void update_status()
{
if(status.state == Game.State.DOWNLOADING && status.download.status.state != Downloader.DownloadState.CANCELLED) return;
if(status.state == Game.State.DOWNLOADING && status.download.status.state != Downloader.Download.State.CANCELLED) return;

var state = Game.State.UNINSTALLED;

Expand Down Expand Up @@ -828,7 +828,7 @@ namespace GameHub.Data.Sources.GOG
FSUtils.mkdir(game.bonus_content_dir.get_path());

status = new BonusContent.Status(BonusContent.State.DOWNLOADING, null);
var ds_id = Downloader.get_instance().download_started.connect(dl => {
var ds_id = Downloader.download_manager().file_download_started.connect(dl => {
if(dl.remote != remote) return;
status = new BonusContent.Status(BonusContent.State.DOWNLOADING, dl);
dl.status_change.connect(s => {
Expand All @@ -840,11 +840,11 @@ namespace GameHub.Data.Sources.GOG

try
{
downloaded_file = yield Downloader.download(remote, local, dl_info, true, false);
downloaded_file = yield Downloader.download_file(remote, local, dl_info, true, false);
}
catch(Error e){}

Downloader.get_instance().disconnect(ds_id);
Downloader.download_manager().disconnect(ds_id);

save_filename();

Expand Down
2 changes: 1 addition & 1 deletion src/data/sources/humble/HumbleGame.vala
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ namespace GameHub.Data.Sources.Humble

public override void update_status()
{
if(status.state == Game.State.DOWNLOADING && status.download.status.state != Downloader.DownloadState.CANCELLED) return;
if(status.state == Game.State.DOWNLOADING && status.download.status.state != Downloader.Download.State.CANCELLED) return;

status = new Game.Status(executable != null && executable.query_exists() ? Game.State.INSTALLED : Game.State.UNINSTALLED, this);
if(status.state == Game.State.INSTALLED)
Expand Down
2 changes: 1 addition & 1 deletion src/data/sources/itch/ButlerClient.vala
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ namespace GameHub.Data.Sources.Itch
error = null;
while(!responses.has_key(message_id))
{
yield Utils.sleep_async(100);
yield Utils.sleep_async(50);
}
Response response;
responses.unset(message_id, out response);
Expand Down
66 changes: 46 additions & 20 deletions src/data/sources/itch/ButlerConnection.vala
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ using GameHub.Utils;

namespace GameHub.Data.Sources.Itch
{
public class ButlerConnection : Object, ForwardsNotifications
public class ButlerConnection: Object, ForwardsNotifications
{
private ButlerClient client;

public async bool connect(string address, string secret)
public async bool init(string address, string secret)
{
try
{
Expand Down Expand Up @@ -89,9 +89,7 @@ namespace GameHub.Data.Sources.Itch
.end_object();
}
}));

var caves = new HashMap<int, ArrayList<string>>();

var installed = new ArrayList<Json.Node>();

var arr = result.has_member("items") ? result.get_array_member("items") : null;
Expand Down Expand Up @@ -123,22 +121,50 @@ namespace GameHub.Data.Sources.Itch
return caves;
}

public async void install(int game_id, string path, string install_id)
public async ArrayList<Json.Object>? get_game_uploads(int game_id)
{
var install_plan_result = yield client.call("Install.Plan", Parser.json(j => j
.set_member_name("gameId").add_int_value(game_id)
var result = yield client.call("Game.FindUploads", Parser.json(j => j
.set_member_name("game").begin_object()
.set_member_name("id").add_int_value(game_id)
.end_object()
));
var upload_id = install_plan_result
.get_object_member("info")
.get_object_member("upload")
.get_int_member("id");
var uploads = new ArrayList<Json.Object>();
var arr = result.has_member("uploads") ? result.get_array_member("uploads") : null;
if(arr != null)
{
arr.foreach_element((array, index, node) => {
uploads.add(node.get_object());
});
}
return uploads;
}

var add_location_result = yield client.call("Install.Locations.Add", Parser.json(j => j
.set_member_name("path").add_string_value(path)
));
var install_location_id = add_location_result
.get_object_member("installLocation")
.get_string_member("id");
public async void install(int game_id, int upload_id, string install_id)
{
var install_dir = FSUtils.mkdir(FSUtils.Paths.Itch.Games);
var install_dir_is_added = false;
string? install_location_id = null;

var install_locations_list = (yield client.call("Install.Locations.List")).get_array_member("installLocations");

foreach(var location_node in install_locations_list.get_elements())
{
var location_obj = location_node.get_object();
if(location_obj != null && location_obj.get_string_member("path") == install_dir.get_path())
{
install_dir_is_added = true;
install_location_id = location_obj.get_string_member("id");
break;
}
}

if(!install_dir_is_added)
{
var add_install_location_result = yield client.call("Install.Locations.Add", Parser.json(j => j
.set_member_name("path").add_string_value(install_dir.get_path())
));
install_location_id = add_install_location_result.get_object_member("installLocation").get_string_member("id");
}

var install_queue_result = yield client.call("Install.Queue", Parser.json(j => j
.set_member_name("game").begin_object()
Expand All @@ -150,9 +176,8 @@ namespace GameHub.Data.Sources.Itch
.set_member_name("installLocationId").add_string_value(install_location_id)
.set_member_name("queueDownload").add_boolean_value(true)
));
// install_id = install_queue_result.get_string_member("id");
var staging_folder = install_queue_result.get_string_member("stagingFolder");

var staging_folder = install_queue_result.get_string_member("stagingFolder");
yield client.call("Install.Perform", Parser.json(j => j
.set_member_name("id").add_string_value(install_id)
.set_member_name("stagingFolder").add_string_value(staging_folder)
Expand All @@ -174,8 +199,9 @@ namespace GameHub.Data.Sources.Itch
));
}

public async void run(string cave_id, string prereqs_dir)
public async void run(string cave_id)
{
var prereqs_dir = FSUtils.expand(FSUtils.Paths.Itch.Games, ".prereqs");
yield client.call("Launch", Parser.json(j => j
.set_member_name("caveId").add_string_value(cave_id)
.set_member_name("prereqsDir").add_string_value(prereqs_dir)
Expand Down
9 changes: 4 additions & 5 deletions src/data/sources/itch/ButlerDaemon.vala
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ namespace GameHub.Data.Sources.Itch
}

var connection = new ButlerConnection();
yield connection.connect(address, secret);
yield connection.init(address, secret);
return connection;
}

Expand All @@ -58,10 +58,9 @@ namespace GameHub.Data.Sources.Itch
var butler_path = butler_executable.get_path();
var db_path = FSUtils.expand(FSUtils.Paths.Itch.Home, FSUtils.Paths.Itch.Database);

string[] cmd = {
butler_path, "daemon", "--json", "--transport", "tcp", "--keep-alive",
"--dbpath", db_path
};
var pid = ((int) Posix.getpid()).to_string();

string[] cmd = { butler_path, "daemon", "--json", "--transport", "tcp", "--keep-alive", "--dbpath", db_path, "--destiny-pid", pid };
int stdout_fd;

try
Expand Down
24 changes: 9 additions & 15 deletions src/data/sources/itch/Itch.vala
Original file line number Diff line number Diff line change
Expand Up @@ -211,14 +211,15 @@ namespace GameHub.Data.Sources.Itch
}
}

public async void install_game(ItchGame game)
public async ArrayList<Json.Object>? get_game_uploads(ItchGame game)
{
var connection = yield butler_daemon.create_connection();
var install_id = Uuid.string_random();
game.download = new ItchDownload(connection, install_id);
yield connection.install(game.int_id, make_game_dir(game), install_id);
game.download = null;
yield update_game_state(game);
return yield butler_connection.get_game_uploads(game.int_id);
}

public async void install_game(ItchGame.Installer installer)
{
yield ItchDownloader.get_instance().download(installer, butler_daemon);
yield update_game_state(installer.game);
}

public async void uninstall_game(ItchGame game)
Expand All @@ -234,14 +235,7 @@ namespace GameHub.Data.Sources.Itch

public async void run_game(ItchGame game)
{
yield butler_connection.run(game.get_cave(), make_game_dir(game));
}

private string make_game_dir(ItchGame game)
{
string install_dir = FSUtils.Paths.Collection.Itch.expand_game_dir(game.name);
FSUtils.mkdir(install_dir);
return install_dir;
yield butler_connection.run(game.get_cave());
}

private async void butler_connect()
Expand Down
Loading

0 comments on commit 40adea8

Please sign in to comment.