Skip to content

Commit

Permalink
WIP Part 11: Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Vysp3r committed Sep 22, 2024
1 parent 764da3e commit 9c70327
Show file tree
Hide file tree
Showing 7 changed files with 123 additions and 139 deletions.
1 change: 1 addition & 0 deletions src/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ sources = files(

'models/releases/basic.vala',
'models/releases/github-action.vala',
'models/releases/upgrade.vala',
'models/releases/steamtinkerlaunch.vala',

'utils/filesystem.vala',
Expand Down
50 changes: 50 additions & 0 deletions src/models/release.vala
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,55 @@ namespace ProtonPlus.Models {
break;
}
}

public async bool install () {
var busy_upgrading = state == State.BUSY_UPGRADING;

if (!busy_upgrading)
state = State.BUSY_INSTALLING;

// Attempt the installation.
var install_success = yield _start_install ();

if (install_success && !busy_upgrading)
send_message (_("The installation of %s is complete.").printf (title));
else {
// Attempt to clean up any leftover files and symlinks,
// but don't erase the user's STL configuration files.
yield remove (false); // Refreshes install state too.

if (!busy_upgrading)
send_message (_("An unexpected error occurred while installing %s.").printf (title));
}

refresh_state (); // Force UI state refresh.

return install_success;
}

protected abstract async bool _start_install ();

public async bool remove (Variant variant) {
var busy_upgrading_or_instaling = state == State.BUSY_UPGRADING || state == State.BUSY_INSTALLING;

if (!busy_upgrading_or_instaling)
state = State.BUSY_REMOVING;

// Attempt the removal.
var remove_success = yield _start_remove (variant);

if (remove_success && !busy_upgrading_or_instaling)
send_message (_("The removal of %s is complete.").printf (title));
else if (!busy_upgrading_or_instaling)
send_message (_("An unexpected error occurred while removing %s.").printf (title));

refresh_state (); // Force UI state refresh.

return remove_success;
}

protected abstract async bool _start_remove (Variant variant);

protected abstract void refresh_state ();
}
}
44 changes: 3 additions & 41 deletions src/models/releases/basic.vala
Original file line number Diff line number Diff line change
Expand Up @@ -28,33 +28,13 @@ namespace ProtonPlus.Models.Releases {
install_location = runner.group.launcher.directory + runner.group.directory + "/" + runner.get_directory_name (title);
}

protected void refresh_state () {
protected override void refresh_state () {
canceled = false;

state = FileUtils.test (install_location, FileTest.IS_DIR) ? State.UP_TO_DATE : State.NOT_INSTALLED;
}

public async bool install () {
state = State.BUSY_INSTALLING;

var install_success = yield _start_install ();

if (canceled)
send_message (_("The installation of %s was canceled.").printf (title));
else if (install_success)
send_message (_("The installation of %s is complete.").printf (title));
else
send_message (_("An unexpected error occurred while installing %s.").printf (title));

if (canceled || !install_success)
yield remove ();

refresh_state ();

return true;
}

protected virtual async bool _start_install () {
protected override async bool _start_install () {
send_message (_("Downloading..."));

string path = runner.group.launcher.directory + runner.group.directory + "/" + title + ".tar.gz";
Expand Down Expand Up @@ -92,25 +72,7 @@ namespace ProtonPlus.Models.Releases {
return true;
}

public async bool remove () {
var busy_installing = state == State.BUSY_INSTALLING;

if (!busy_installing)
state = State.BUSY_REMOVING;

var remove_success = yield _start_remove ();

if (remove_success && !busy_installing)
send_message (_("The removal of %s is complete.").printf (title));
else if (!busy_installing)
send_message (_("An unexpected error occurred while removing %s.").printf (title));

refresh_state ();

return true;
}

protected virtual async bool _start_remove () {
protected override async bool _start_remove (Variant variant) {
send_message (_("Deleting..."));

var deleted = yield Utils.Filesystem.delete_directory (install_location);
Expand Down
145 changes: 49 additions & 96 deletions src/models/releases/steamtinkerlaunch.vala
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
namespace ProtonPlus.Models.Releases {
public class SteamTinkerLaunch : Release {
public class SteamTinkerLaunch : Upgrade {
string home_location { get; set; }
string compat_location { get; set; }
string parent_location { get; set; }
Expand Down Expand Up @@ -136,7 +136,30 @@ namespace ProtonPlus.Models.Releases {
}
}

void refresh_state () {
void write_installation_metadata (string meta_location) {
Utils.Filesystem.create_file (meta_location, @"$latest_date:$latest_hash");
}

public bool detect_external_locations () {
if (external_locations.length () > 0)
external_locations = new List<string> ();

var location = @"$home_location/SteamTinkerLaunch";
if (FileUtils.test (location, FileTest.IS_DIR))
external_locations.append (location);

location = Environment.get_home_dir () + "/stl";
if (!Utils.System.IS_STEAM_OS && FileUtils.test (location, FileTest.IS_DIR))
external_locations.append (location);

// Disabled for now, since we always erase base_location before installs.
// if (FileUtils.test (base_location, FileTest.IS_DIR) && !FileUtils.test (meta_location, FileTest.IS_REGULAR))
// external_locations.append (base_location);

return external_locations.length () > 0;
}

protected override void refresh_state () {
// Update the ProtonPlus UI state variables.
// NOTE: We treat a non-executable binary as a "broken installation".
var base_location_exists = FileUtils.test (base_location, FileTest.IS_DIR);
Expand Down Expand Up @@ -190,55 +213,7 @@ namespace ProtonPlus.Models.Releases {
state = !installed ? State.NOT_INSTALLED : updated ? State.UP_TO_DATE : State.UPDATE_AVAILABLE;
}

void write_installation_metadata (string meta_location) {
Utils.Filesystem.create_file (meta_location, @"$latest_date:$latest_hash");
}

public bool detect_external_locations () {
if (external_locations.length () > 0)
external_locations = new List<string> ();

var location = @"$home_location/SteamTinkerLaunch";
if (FileUtils.test (location, FileTest.IS_DIR))
external_locations.append (location);

location = Environment.get_home_dir () + "/stl";
if (!Utils.System.IS_STEAM_OS && FileUtils.test (location, FileTest.IS_DIR))
external_locations.append (location);

// Disabled for now, since we always erase base_location before installs.
// if (FileUtils.test (base_location, FileTest.IS_DIR) && !FileUtils.test (meta_location, FileTest.IS_REGULAR))
// external_locations.append (base_location);

return external_locations.length () > 0;
}

public async bool install () {
var busy_upgrading = state == State.BUSY_UPGRADING;

if (!busy_upgrading)
state = State.BUSY_INSTALLING;

// Attempt the installation.
var install_success = yield _start_install ();

if (install_success && !busy_upgrading)
send_message (_("The installation of %s is complete.").printf (title));
else {
// Attempt to clean up any leftover files and symlinks,
// but don't erase the user's STL configuration files.
yield remove (false); // Refreshes install state too.

if (!busy_upgrading)
send_message (_("An unexpected error occurred while installing %s.").printf (title));
}

refresh_state (); // Force UI state refresh.

return install_success;
}

async bool _start_install () {
protected async override bool _start_install () {
if (Utils.System.check_dependency ("steamtinkerlaunch"))
Utils.System.run_command ("steamtinkerlaunch compat del");

Expand Down Expand Up @@ -334,53 +309,17 @@ namespace ProtonPlus.Models.Releases {
return true;
}

public async bool upgrade () {
state = State.BUSY_UPGRADING;
// Variant (delete_config, user_request)
protected override async bool _start_remove (Variant variant) {
bool delete_config = false;
bool user_request = false;

var upgrade_success = yield _start_upgrade ();

if (upgrade_success)
send_message (_("The upgrade of %s is complete.").printf (title));
else
send_message (_("An unexpected error occurred while upgrading %s."));

return upgrade_success;
}
var iterator = variant.iterator ();
if (variant.n_children () >= 1)
iterator.next ("b", &delete_config);
if (variant.n_children () == 2)
iterator.next ("b", &user_request);

async bool _start_upgrade () {
var remove_success = yield remove (false);

if (!remove_success)
return false;

var install_success = yield install ();

if (!install_success)
return false;

return true;
}

public async bool remove (bool delete_config, bool user_request = false) {
var busy_upgrading_or_instaling = state == State.BUSY_UPGRADING || state == State.BUSY_INSTALLING;

if (!busy_upgrading_or_instaling)
state = State.BUSY_REMOVING;

// Attempt the removal.
var remove_success = yield _start_remove (delete_config, user_request);

if (remove_success && !busy_upgrading_or_instaling)
send_message (_("The removal of %s is complete.").printf (title));
else if (!busy_upgrading_or_instaling)
send_message (_("An unexpected error occurred while removing %s.").printf (title));

refresh_state (); // Force UI state refresh.

return remove_success;
}

async bool _start_remove (bool delete_config, bool user_request = false) {
exec_stl_if_exists (binary_location, "compat del");

// NOTE: We check specific types to avoid deleting unexpected data.
Expand Down Expand Up @@ -417,5 +356,19 @@ namespace ProtonPlus.Models.Releases {

return true;
}

protected override async bool _start_upgrade () {
var remove_success = yield remove (false);

if (!remove_success)
return false;

var install_success = yield install ();

if (!install_success)
return false;

return true;
}
}
}
18 changes: 18 additions & 0 deletions src/models/releases/upgrade.vala
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
namespace ProtonPlus.Models.Releases {
public abstract class Upgrade : Release {
public async bool upgrade () {
state = State.BUSY_UPGRADING;

var upgrade_success = yield _start_upgrade ();

if (upgrade_success)
send_message (_("The upgrade of %s is complete.").printf (title));
else
send_message (_("An unexpected error occurred while upgrading %s."));

return upgrade_success;
}

protected abstract async bool _start_upgrade ();
}
}
2 changes: 1 addition & 1 deletion src/widgets/release-rows/basic.vala
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ namespace ProtonPlus.Widgets.ReleaseRows {

remove_dialog.present ();

release.remove.begin ((obj, res) => {
release.remove.begin ("", (obj, res) => {
var success = release.remove.end (res);

remove_dialog.done (success);
Expand Down
2 changes: 1 addition & 1 deletion src/widgets/release-rows/steamtinkerlaunch.vala
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ namespace ProtonPlus.Widgets.ReleaseRows {

remove_dialog.present ();

release.remove.begin (remove_check.get_active (), true, (obj, res) => {
release.remove.begin (new Variant ("(bb)", remove_check.get_active (), true), (obj, res) => {
var success = release.remove.end (res);

remove_dialog.done (success);
Expand Down

0 comments on commit 9c70327

Please sign in to comment.