Skip to content

Commit

Permalink
Adding string store to background path
Browse files Browse the repository at this point in the history
  • Loading branch information
neon-nyan committed Aug 25, 2024
1 parent 2b3b850 commit 18693e4
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ internal class CollapseMiscSetting : IGameSettingsValue<CollapseMiscSetting>

private bool _UseCustomArguments = true;

private bool _UseCustomRegionBG = true;
private bool _UseCustomRegionBG = false;

private static bool _IsDeserializing;
#endregion
Expand Down Expand Up @@ -103,6 +103,14 @@ public bool UseCustomRegionBG
if (!_IsDeserializing) Save();
}
}

#nullable enable
/// <summary>
/// The path of the custom BG for each region
/// </summary>
public string? CustomRegionBGPath { get; set; }
#nullable restore

Check warning on line 112 in CollapseLauncher/Classes/GameManagement/GameSettings/Universal/RegistryClass/CollapseMiscSetting.cs

View workflow job for this annotation

GitHub Actions / Qodana for .NET

Unused nullable directive

Unused nullable directive

Check warning

Code scanning / QDNET

Unused nullable directive Warning

Unused nullable directive

#endregion

#region Methods
Expand Down
31 changes: 28 additions & 3 deletions CollapseLauncher/XAMLs/MainApp/MainPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using CollapseLauncher.Helper.Update;
using CollapseLauncher.Interfaces;
using CollapseLauncher.Pages;
using CollapseLauncher.Statics;
using CommunityToolkit.WinUI;
using CommunityToolkit.WinUI.Animations;
using Hi3Helper;
Expand Down Expand Up @@ -486,9 +487,12 @@ private void CustomBackgroundChanger_Event(object sender, BackgroundImgProperty

internal async void ChangeBackgroundImageAsRegionAsync(bool ShowLoadingMsg = false)
{
GamePresetProperty currentGameProperty = GetCurrentGameProperty();
bool isUseCustomPerRegionBg = ((IGameSettingsUniversal)currentGameProperty?._GameSettings)?.SettingsCollapseMisc?.UseCustomRegionBG ?? false;

IsCustomBG = GetAppConfigValue("UseCustomBG").ToBool();
bool isAPIBackgroundAvailable = !string.IsNullOrEmpty(LauncherMetadataHelper.CurrentMetadataConfig?.GameLauncherApi?.GameBackgroundImg);
if (IsCustomBG)
if (IsCustomBG && !isUseCustomPerRegionBg)
{
string BGPath = GetAppConfigValue("CustomBGPath").ToString();
if (LauncherMetadataHelper.CurrentMetadataConfig?.GameLauncherApi != null)
Expand All @@ -497,6 +501,25 @@ internal async void ChangeBackgroundImageAsRegionAsync(bool ShowLoadingMsg = fal
string.IsNullOrEmpty(BGPath) ? AppDefaultBG : BGPath;
}
}
else if (isUseCustomPerRegionBg && IsCustomBG)
{
string regionBgPath = ((IGameSettingsUniversal)currentGameProperty?._GameSettings)?.SettingsCollapseMisc?.CustomRegionBGPath;

Check warning on line 506 in CollapseLauncher/XAMLs/MainApp/MainPage.xaml.cs

View workflow job for this annotation

GitHub Actions / Qodana for .NET

Conditional access qualifier expression is known to be null or not null

Conditional access qualifier expression is known to be not null

Check warning

Code scanning / QDNET

Conditional access qualifier expression is known to be null or not null Warning

Conditional access qualifier expression is known to be not null

if (!string.IsNullOrEmpty(regionBgPath)
&& File.Exists(regionBgPath))
{
LauncherMetadataHelper.CurrentMetadataConfig.GameLauncherApi.GameBackgroundImgLocal = regionBgPath;

Check warning on line 511 in CollapseLauncher/XAMLs/MainApp/MainPage.xaml.cs

View workflow job for this annotation

GitHub Actions / Qodana for .NET

Possible 'System.NullReferenceException'

Possible 'System.NullReferenceException'

Check warning

Code scanning / QDNET

Possible 'System.NullReferenceException' Warning

Possible 'System.NullReferenceException'
}
else if (isAPIBackgroundAvailable)
{
await DownloadBackgroundImage(default);
if (((IGameSettingsUniversal)currentGameProperty?._GameSettings)?.SettingsCollapseMisc != null)

Check warning on line 516 in CollapseLauncher/XAMLs/MainApp/MainPage.xaml.cs

View workflow job for this annotation

GitHub Actions / Qodana for .NET

Conditional access qualifier expression is known to be null or not null

Conditional access qualifier expression is known to be not null

Check warning

Code scanning / QDNET

Conditional access qualifier expression is known to be null or not null Warning

Conditional access qualifier expression is known to be not null
{
((IGameSettingsUniversal)currentGameProperty._GameSettings).SettingsCollapseMisc.CustomRegionBGPath =
LauncherMetadataHelper.CurrentMetadataConfig.GameLauncherApi.GameBackgroundImg;
}
}
}
else if (isAPIBackgroundAvailable)
{
try
Expand All @@ -515,15 +538,17 @@ internal async void ChangeBackgroundImageAsRegionAsync(bool ShowLoadingMsg = fal
if (!isAPIBackgroundAvailable && !IsCustomBG && LauncherMetadataHelper.CurrentMetadataConfig?.GameLauncherApi != null)
LauncherMetadataHelper.CurrentMetadataConfig.GameLauncherApi.GameBackgroundImgLocal = AppDefaultBG;

if ((!IsCustomBG || IsFirstStartup) && LauncherMetadataHelper.CurrentMetadataConfig?.GameLauncherApi != null)
// If the custom per region is enabled, then execute below
if (((IsCustomBG || IsFirstStartup) && LauncherMetadataHelper.CurrentMetadataConfig?.GameLauncherApi != null)
|| isUseCustomPerRegionBg)
{
BackgroundImgChanger.ChangeBackground(LauncherMetadataHelper.CurrentMetadataConfig.GameLauncherApi.GameBackgroundImgLocal,

Check warning on line 545 in CollapseLauncher/XAMLs/MainApp/MainPage.xaml.cs

View workflow job for this annotation

GitHub Actions / Qodana for .NET

Possible 'System.NullReferenceException'

Possible 'System.NullReferenceException'
() =>
{
IsFirstStartup = false;
ColorPaletteUtility.ReloadPageTheme(this, CurrentAppTheme);
},
IsCustomBG);
IsCustomBG && !isUseCustomPerRegionBg);
}
}
#endregion
Expand Down
6 changes: 2 additions & 4 deletions CollapseLauncher/XAMLs/MainApp/Pages/HomePage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -1920,17 +1920,15 @@
Margin="0,8,0,0"
Style="{ThemeResource BodyStrongTextBlockStyle}"
Text="{x:Bind helper:Locale.Lang._HomePage.GameSettings_Panel3CustomBGRegionSectionTitle}" />
<ToggleSwitch Margin="0,0,0,8"
x:Name="UseCustomBGParamsSwitch"
<ToggleSwitch x:Name="UseCustomBGParamsSwitch"
IsOn="{x:Bind UseCustomBGRegion, Mode=TwoWay}"
OffContent="{x:Bind helper:Locale.Lang._Misc.Disabled}"
OnContent="{x:Bind helper:Locale.Lang._Misc.Enabled}" />
<Button x:Name="ChangeGameBGButton"
Margin="0,8"
Margin="0,8,0,8"
HorizontalAlignment="Stretch"
HorizontalContentAlignment="Left"
Click="ChangeGameBGButton_Click"
IsEnabled="{x:Bind UseCustomBGParamsSwitch.IsOn, Mode=OneWay}"
CornerRadius="14">
<Button.Content>
<Grid Margin="4,0">
Expand Down
45 changes: 29 additions & 16 deletions CollapseLauncher/XAMLs/MainApp/Pages/HomePage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1917,24 +1917,36 @@ public bool UseCustomArgs

public bool UseCustomBGRegion
{
get => ((IGameSettingsUniversal)CurrentGameProperty._GameSettings).SettingsCollapseMisc.UseCustomRegionBG;
get
{
bool value = ((IGameSettingsUniversal)CurrentGameProperty?._GameSettings)?.SettingsCollapseMisc?.UseCustomRegionBG ?? false;
UseCustomBGParamsSwitch.IsEnabled = GetAppConfigValue("UseCustomBG").ToBool();
ChangeGameBGButton.IsEnabled = UseCustomBGParamsSwitch.IsEnabled && value;
return value;
}
set
{
if (value)
ChangeGameBGButton.IsEnabled = value;

string regionBgPath = ((IGameSettingsUniversal)CurrentGameProperty._GameSettings).SettingsCollapseMisc.CustomRegionBGPath;
if (string.IsNullOrEmpty(regionBgPath)
|| !File.Exists(regionBgPath))
{
ChangeGameBGButton.IsEnabled = true;
SetAndSaveConfigValue("UseCustomBG", new IniValue(true));
regionBgPath = GetAppConfigValue("CustomBGPath").ToString();
((IGameSettingsUniversal)CurrentGameProperty._GameSettings)
.SettingsCollapseMisc
.CustomRegionBGPath = regionBgPath;
CurrentGameProperty._GameSettings.SaveSettings();
}
else

if (!value)
{
ChangeGameBGButton.IsEnabled = false;
SetAndSaveConfigValue("UseCustomBG", new IniValue(false));
regionBgPath = GetAppConfigValue("CustomBGPath").ToString();

Check warning on line 1944 in CollapseLauncher/XAMLs/MainApp/Pages/HomePage.xaml.cs

View workflow job for this annotation

GitHub Actions / Qodana for .NET

Assignment is not used

Value assigned is not used in any execution path

Check warning

Code scanning / QDNET

Assignment is not used Warning

Value assigned is not used in any execution path
}

string CustomBGParam = value ? "CustomBGPath" : "CurrentBackground";
((IGameSettingsUniversal)CurrentGameProperty._GameSettings).SettingsCollapseMisc.UseCustomRegionBG = value;
LauncherMetadataHelper.CurrentMetadataConfig.GameLauncherApi.GameBackgroundImgLocal = GetAppConfigValue(CustomBGParam).ToString();
BackgroundImgChanger.ChangeBackground(LauncherMetadataHelper.CurrentMetadataConfig.GameLauncherApi.GameBackgroundImgLocal, null, true, true);
CurrentGameProperty._GameSettings.SaveSettings();
m_mainPage?.ChangeBackgroundImageAsRegionAsync();
}
}
#endregion
Expand Down Expand Up @@ -2160,12 +2172,13 @@ private async void ChangeGameBGButton_Click(object sender, RoutedEventArgs e)
if (croppedImage == null) return;
SetAlternativeFileStream(croppedImage);
}

LauncherMetadataHelper.CurrentMetadataConfig.GameLauncherApi.GameBackgroundImgLocal = file;
SetAndSaveConfigValue("CustomBGPath", file);
LogWriteLine("Wrote " + GetAppConfigValue("UseCustomBG").ToBool() + "to config", LogType.Debug, false);
BackgroundImgChanger.ChangeBackground(LauncherMetadataHelper.CurrentMetadataConfig.GameLauncherApi.GameBackgroundImgLocal, null, true, true);


if (((IGameSettingsUniversal)CurrentGameProperty?._GameSettings)?.SettingsCollapseMisc != null)
{
((IGameSettingsUniversal)CurrentGameProperty._GameSettings).SettingsCollapseMisc.CustomRegionBGPath = file;
CurrentGameProperty._GameSettings.SaveSettings();
}
m_mainPage?.ChangeBackgroundImageAsRegionAsync();
}
}

Expand Down
51 changes: 35 additions & 16 deletions CollapseLauncher/XAMLs/MainApp/Pages/SettingsPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
using CollapseLauncher.Helper.Image;
using CollapseLauncher.Helper.Metadata;
using CollapseLauncher.Helper.Update;
using CollapseLauncher.Interfaces;
using CollapseLauncher.Pages.OOBE;
using CollapseLauncher.Statics;
using CommunityToolkit.WinUI;
using Hi3Helper;
using Hi3Helper.Data;
Expand Down Expand Up @@ -512,15 +514,23 @@ private bool IsBGCustom
set
{
SetAndSaveConfigValue("UseCustomBG", new IniValue(value));
GamePresetProperty currentGameProperty = GamePropertyVault.GetCurrentGameProperty();
bool isUseRegionCustomBG = ((IGameSettingsUniversal)currentGameProperty?._GameSettings)?.SettingsCollapseMisc?.UseCustomRegionBG ?? false;
if (!value)
{
BGPathDisplay.Text = Lang._Misc.NotSelected;
LauncherMetadataHelper.CurrentMetadataConfig.GameLauncherApi.GameBackgroundImgLocal = GetAppConfigValue("CurrentBackground").ToString();
m_mainPage?.ChangeBackgroundImageAsRegionAsync();
AppBGCustomizer.Visibility = Visibility.Collapsed;
AppBGCustomizerNote.Visibility = Visibility.Collapsed;
CustomBGImageSettings.Visibility = IsWaifu2XUsable ? Visibility.Visible : Visibility.Collapsed;
CustomBGVideoSettings.Visibility = Visibility.Collapsed;

ToggleCustomBgButtons();
}
else if (isUseRegionCustomBG && value)

Check warning on line 527 in CollapseLauncher/XAMLs/MainApp/Pages/SettingsPage.xaml.cs

View workflow job for this annotation

GitHub Actions / Qodana for .NET

Expression is always 'true' or always 'false'

Expression is always true

Check warning

Code scanning / QDNET

Expression is always 'true' or always 'false' Warning

Expression is always true
{
string currentRegionCustomBg = ((IGameSettingsUniversal)currentGameProperty._GameSettings).SettingsCollapseMisc.CustomRegionBGPath;
LauncherMetadataHelper.CurrentMetadataConfig.GameLauncherApi.GameBackgroundImgLocal = currentRegionCustomBg;
m_mainPage?.ChangeBackgroundImageAsRegionAsync();

ToggleCustomBgButtons();
}
else
{
Expand All @@ -544,20 +554,29 @@ private bool IsBGCustom
BackgroundImgChanger.ChangeBackground(LauncherMetadataHelper.CurrentMetadataConfig.GameLauncherApi.GameBackgroundImgLocal, null, true, true);
AppBGCustomizer.Visibility = Visibility.Visible;
AppBGCustomizerNote.Visibility = Visibility.Visible;
var currentMediaType = BackgroundMediaUtility.GetMediaType(LauncherMetadataHelper.CurrentMetadataConfig.GameLauncherApi.GameBackgroundImgLocal);
if (currentMediaType == MediaType.Media)
{
CustomBGImageSettings.Visibility = Visibility.Collapsed;
CustomBGVideoSettings.Visibility = Visibility.Visible;
}
else
{
CustomBGImageSettings.Visibility = IsWaifu2XUsable ? Visibility.Visible : Visibility.Collapsed;
CustomBGVideoSettings.Visibility = Visibility.Collapsed;
}
}

var currentMediaType = BackgroundMediaUtility.GetMediaType(LauncherMetadataHelper.CurrentMetadataConfig.GameLauncherApi.GameBackgroundImgLocal);
if (currentMediaType == MediaType.Media)
{
CustomBGImageSettings.Visibility = Visibility.Collapsed;
CustomBGVideoSettings.Visibility = Visibility.Visible;
}
else
{
CustomBGImageSettings.Visibility = IsWaifu2XUsable ? Visibility.Visible : Visibility.Collapsed;
CustomBGVideoSettings.Visibility = Visibility.Collapsed;
}
BGSelector.IsEnabled = value;

return;
void ToggleCustomBgButtons()
{
AppBGCustomizer.Visibility = Visibility.Collapsed;
AppBGCustomizerNote.Visibility = Visibility.Collapsed;
CustomBGImageSettings.Visibility = IsWaifu2XUsable ? Visibility.Visible : Visibility.Collapsed;
CustomBGVideoSettings.Visibility = Visibility.Collapsed;
}
}
}

Expand Down

0 comments on commit 18693e4

Please sign in to comment.