Humble Bundle support
[GameHub.git] / src / data / sources / gog / GOGGame.vala
bloba524d08b7b5a2bc504e965c2f1e7118ccd4f567a
1 using Gtk;
2 using Gee;
3 using GameHub.Utils;
5 namespace GameHub.Data.Sources.GOG
7 public class GOGGame: Game
9 private bool _is_for_linux;
11 public File executable { get; private set; }
13 private string installation_dir_name
15 owned get
17 return name.escape().replace(" ", "_").replace(":", "");
21 public GOGGame(GOG src, Json.Object json)
23 source = src;
24 id = json.get_int_member("id").to_string();
25 name = json.get_string_member("title");
26 image = "https:" + json.get_string_member("image") + "_392.jpg";
27 icon = image;
28 _is_for_linux = json.get_object_member("worksOn").get_boolean_member("Linux");
30 if(!_is_for_linux)
32 GamesDB.get_instance().add_unsupported_game(source, id);
33 return;
36 executable = FSUtils.file(FSUtils.Paths.GOG.Games, installation_dir_name + "/start.sh");
39 public GOGGame.from_db(GOG src, Sqlite.Statement stmt)
41 source = src;
42 id = stmt.column_text(1);
43 name = stmt.column_text(2);
44 icon = stmt.column_text(3);
45 image = stmt.column_text(4);
46 custom_info = stmt.column_text(5);
47 _is_for_linux = true;
48 executable = FSUtils.file(FSUtils.Paths.GOG.Games, installation_dir_name + "/start.sh");
51 public override async bool is_for_linux()
53 return _is_for_linux;
56 public override bool is_installed()
58 return executable.query_exists();
61 public override async void install(DownloadProgress progress = (d, t) => {})
63 var url = @"https://api.gog.com/products/$(id)?expand=downloads";
64 var root = (yield Parser.parse_remote_json_file_async(url, "GET", ((GOG) source).user_token)).get_object();
66 icon = "https:" + root.get_object_member("images").get_string_member("icon");
68 var installers_json = root.get_object_member("downloads").get_array_member("installers");
70 var installers = new ArrayList<Game.Installer>();
72 foreach(var installer_json in installers_json.get_elements())
74 var installer = new Installer(installer_json.get_object());
75 if(installer.os == "linux") installers.add(installer);
78 var wnd = new GameHub.UI.Dialogs.GameInstallDialog(this, installers);
80 wnd.canceled.connect(() => Idle.add(install.callback));
82 wnd.install.connect(installer => {
83 root = Parser.parse_remote_json_file(installer.file, "GET", ((GOG) source).user_token).get_object();
84 var link = root.get_string_member("downlink");
85 var local = FSUtils.expand(FSUtils.Paths.GOG.Installers, "gog_" + id + "_" + installer.id + ".sh");
87 FSUtils.mkdir(FSUtils.Paths.GOG.Games);
88 FSUtils.mkdir(FSUtils.Paths.GOG.Installers);
90 Downloader.get_instance().download.begin(File.new_for_uri(link), { local }, progress, null, (obj, res) => {
91 try
93 var file = Downloader.get_instance().download.end(res).get_path();
94 var install_dir = FSUtils.expand(FSUtils.Paths.GOG.Games, installation_dir_name);
95 Utils.run(@"chmod +x \"$(file)\"");
96 Utils.run_async.begin(@"$(file) -- --i-agree-to-all-licenses --noreadme --nooptions --noprompt --destination $(install_dir)", (obj, res) => {
97 Utils.run_async.end(res);
98 Idle.add(install.callback);
99 });
101 catch(Error e)
103 warning(e.message);
108 wnd.show_all();
109 wnd.present();
111 yield;
114 public override async void run()
116 if(is_installed())
118 var path = executable.get_path();
119 yield Utils.run_async(@"$(path)");
123 public class Installer: Game.Installer
125 public string lang;
126 public string lang_full;
128 public override string name { get { return lang_full; } }
130 public Installer(Json.Object json)
132 id = json.get_string_member("id");
133 os = json.get_string_member("os");
134 lang = json.get_string_member("language");
135 lang_full = json.get_string_member("language_full");
136 file = json.get_array_member("files").get_object_element(0).get_string_member("downlink");