Skip to content

Commit

Permalink
TweakSet.get_options_or_copy_global: actually copy global options by …
Browse files Browse the repository at this point in the history
…serializing and deserializing them (#355)
  • Loading branch information
tkashkin committed Aug 15, 2020
1 parent d2ec729 commit 9458fb8
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 18 deletions.
9 changes: 6 additions & 3 deletions src/data/runnables/traits/game/SupportsTweaks.vala
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,17 @@ namespace GameHub.Data.Runnables.Traits.Game
protected void init_tweaks()
{
tweaks = new TweakSet.from_json(false, null);
tweaks.changed.connect(() => {
save();
});
_tweaks_init();
}

protected void dbinit_tweaks(Sqlite.Statement s)
{
tweaks = new TweakSet.from_json(false, Parser.parse_json(Tables.Games.TWEAKS.get(s)));
_tweaks_init();
}

private void _tweaks_init()
{
tweaks.changed.connect(() => {
save();
});
Expand Down
5 changes: 3 additions & 2 deletions src/data/tweaks/Tweak.vala
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ namespace GameHub.Data.Tweaks
public Requirements? requirements { get; protected set; default = null; }

public ArrayList<Option>? options { get; protected set; default = null; }
public bool has_options { get { return options != null && options.size > 0; } }

public HashMap<string, string?>? env { get; protected set; default = null; }
public string? command { get; protected set; default = null; }
Expand Down Expand Up @@ -154,7 +155,7 @@ namespace GameHub.Data.Tweaks

public Option? get_option(string id)
{
if(options == null || options.size == 0) return null;
if(!has_options) return null;
foreach(var option in options)
{
if(option.id == id) return option;
Expand Down Expand Up @@ -299,7 +300,7 @@ namespace GameHub.Data.Tweaks
if(reqs != null) obj.set_member("requires", reqs);
}

if(options != null && options.size > 0)
if(has_options)
{
var options_obj = new Json.Object();
foreach(var option in options)
Expand Down
14 changes: 10 additions & 4 deletions src/data/tweaks/TweakOptions.vala
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ namespace GameHub.Data.Tweaks
public Tweak tweak { get; set; }
public State state { get; set; default = State.GLOBAL; }
public ArrayList<OptionProperties>? properties { get; protected set; default = null; }
public bool has_properties { get { return properties != null && properties.size > 0; } }

public TweakOptions(Tweak tweak, State state = State.GLOBAL, ArrayList<OptionProperties>? properties = null)
{
Expand Down Expand Up @@ -61,9 +62,14 @@ namespace GameHub.Data.Tweaks
Object(tweak: tweak, state: state, properties: properties);
}

public TweakOptions copy()
{
return new TweakOptions.from_json(tweak, to_json());
}

public OptionProperties? get_properties_for_id(string option_id)
{
if(properties == null || properties.size == 0) return null;
if(!has_properties) return null;
foreach(var opt_props in properties)
{
if(opt_props.option.id == option_id) return opt_props;
Expand Down Expand Up @@ -111,14 +117,14 @@ namespace GameHub.Data.Tweaks
public string expand(string value)
{
var result = value;
if(properties != null && properties.size > 0)
if(has_properties)
{
foreach(var option_props in properties)
{
result = result.replace("${option:%s}".printf(option_props.option.id), option_props.value);
}
}
if(tweak.options != null && tweak.options.size > 0)
if(tweak.has_options)
{
foreach(var option in tweak.options)
{
Expand All @@ -135,7 +141,7 @@ namespace GameHub.Data.Tweaks

obj.set_string_member("state", state.to_string());

if(properties != null && properties.size > 0)
if(has_properties)
{
var options = new Json.Object();
foreach(var opt_props in properties)
Expand Down
13 changes: 6 additions & 7 deletions src/data/tweaks/TweakSet.vala
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@ namespace GameHub.Data.Tweaks
public TweakOptions get_options_or_copy_global(Tweak tweak)
{
var local = get_options_for_tweak(tweak);
if(local != null && local.properties.size > 0) return local;
var global = get_or_create_global_options(tweak);
if(local != null && (!tweak.has_options || local.has_properties)) return local;
var global = get_or_create_global_options(tweak).copy();
set_options_for_tweak(tweak, global);
return global;
}
Expand All @@ -121,7 +121,7 @@ namespace GameHub.Data.Tweaks
return get_or_create_options(tweak);
}
var local = get_options_for_tweak(tweak);
if(local != null && local.properties.size > 0) return local;
if(local != null && (!tweak.has_options || local.has_properties)) return local;
return get_or_create_global_options(tweak);
}

Expand All @@ -132,12 +132,11 @@ namespace GameHub.Data.Tweaks
{
return opts != null && opts.state == TweakOptions.State.ENABLED;
}
if(opts != null && opts.state != TweakOptions.State.GLOBAL)
if(opts == null || opts.state == TweakOptions.State.GLOBAL)
{
return opts.state == TweakOptions.State.ENABLED;
return GameHub.Settings.Tweaks.global_tweakset.is_enabled(tweak_id);
}
var global_opts = GameHub.Settings.Tweaks.global_tweakset.get_options_for_id(tweak_id);
return global_opts != null && global_opts.state == TweakOptions.State.ENABLED;
return opts.state == TweakOptions.State.ENABLED;
}

public void reset()
Expand Down
2 changes: 1 addition & 1 deletion src/ui/widgets/tweaks/TweakOptionsPopover.vala
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ namespace GameHub.UI.Widgets.Tweaks
{
get_style_context().add_class("tweak-options-popover");

if(tweak.options == null || tweak.options.size == 0)
if(!tweak.has_options)
{
var options_warning = new AlertView(_("%s: no configurable options").printf(tweak.name), _("This tweak does not have any configurable options"), "dialog-warning-symbolic");
options_warning.get_style_context().remove_class(Gtk.STYLE_CLASS_VIEW);
Expand Down
2 changes: 1 addition & 1 deletion src/ui/widgets/tweaks/TweakRow.vala
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ namespace GameHub.UI.Widgets.Tweaks

Button? options = null;
TweakOptionsPopover? options_popover = null;
if(tweak.options != null && tweak.options.size > 0)
if(tweak.has_options)
{
options = new Button.from_icon_name("gh-settings-cog-symbolic", IconSize.BUTTON);
options.get_style_context().add_class(Gtk.STYLE_CLASS_FLAT);
Expand Down

0 comments on commit 9458fb8

Please sign in to comment.