Skip to content

Commit

Permalink
Add option to limit max allowed local filesize (file gets deleted if …
Browse files Browse the repository at this point in the history
…too big) and add info/warning to Local Requests option
  • Loading branch information
Sekoree committed Mar 7, 2022
1 parent b18b6e8 commit 61c2869
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 26 deletions.
22 changes: 20 additions & 2 deletions Audiosurf2_Tools/Controls/SettingsControl.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,20 @@
IsEnabled="{Binding TwitchQueueCutOffTimeEnabled}"
SelectedTime="{Binding TwitchQueueCutOffTimeTime}" />

<ToggleSwitch Content="Enable Local Requests"
IsChecked="{Binding TwitchEnableLocalRequests}" />
<StackPanel Orientation="Horizontal"
Spacing="5">
<ToggleSwitch Content="Enable Local Requests"
IsChecked="{Binding TwitchEnableLocalRequests}" />
<TextBlock Foreground="#AA2222"
VerticalAlignment="Bottom"
Margin="-60,0,0,10"
Text="Experimental"/>
<Button VerticalAlignment="Bottom"
Content="Info"
Command="{Binding OpenHelpText, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}}"
Margin="0,0,0,5"
FontSize="10"/>
</StackPanel>
<Border Margin="0,5,0,15"
IsEnabled="{Binding TwitchEnableLocalRequests}"
BorderBrush="#666666"
Expand All @@ -88,6 +100,12 @@
CommandParameter="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}}" />
</Grid>
</Border>
<TextBlock Text="Max Local Request Size (MB)"/>
<NumericUpDown Margin="0,5,0,15"
IsEnabled="{Binding TwitchEnableLocalRequests}"
HorizontalAlignment="Left"
Width="130"
Value="{Binding TwitchLocalRequestMaxSizeMB}"/>

<TextBlock Text="Max Recent Request Count:"
TextWrapping="Wrap" />
Expand Down
30 changes: 30 additions & 0 deletions Audiosurf2_Tools/Controls/SettingsControl.axaml.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using Avalonia;
using Avalonia.Controls;
using Avalonia.Layout;
using Avalonia.Markup.Xaml;
using Avalonia.Media;

namespace Audiosurf2_Tools.Controls;

Expand All @@ -15,4 +17,32 @@ private void InitializeComponent()
{
AvaloniaXamlLoader.Load(this);
}

public void OpenHelpText()
{
var wnd = new Window()
{
SizeToContent = SizeToContent.Height,
Width = 500,
SystemDecorations = SystemDecorations.None,
Content = new TextBlock()
{
Text = "This is a very experimental feature and you need a specific setup for this!\n\n" +
"The location you set here needs to have direct access to the location your viewers " +
"upload files to/request files from with the command: \n\"!sr fileName.extension\"\n\n" +
"The recommended way would work via a mounted Network Drive, \n" +
"a Nextcloud or Seafile mounted via WebDAV would work well.\n" +
"Using sync clients like Dropbox might need 2 attempts to request a song.",
Margin = new Thickness(15),
MaxWidth = 480,
TextWrapping = TextWrapping.Wrap,
VerticalAlignment = VerticalAlignment.Center,
HorizontalAlignment = HorizontalAlignment.Center
},
Position = this.PointToScreen(this.Bounds.Center)
};
wnd.Tapped += (sender, args) => ((Window) sender!).Close();
wnd.LostFocus += (sender, args) => ((Window) sender!).Close();
wnd.Show();
}
}
1 change: 1 addition & 0 deletions Audiosurf2_Tools/Entities/AppSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ public class AppSettings
public DateTimeOffset TwitchQueueCutOffTime { get; set; } = DateTime.Now;
public bool TwitchEnableLocalRequests { get; set; } = false;
public string TwitchLocalRequestPath { get; set; } = "";
public int TwitchLocalRequestMaxSizeMB { get; set; } = 50;
}
31 changes: 14 additions & 17 deletions Audiosurf2_Tools/ViewModels/InstallerViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public async Task AutoFindAsync()
{
GameLocation = await ToolUtils.GetGameDirectoryAsync();
}

public async Task BrowserLocation(Window parent)
{
var openFol = new OpenFolderDialog()
Expand Down Expand Up @@ -82,12 +82,7 @@ public async Task FindGameAndPatchVersionAsync(string x)
if (as2Ver.FileVersion?.StartsWith("2017.4.40") == true)
{
UnityVersion = as2Ver.FileVersion;
BetaChannel = "bleedingedge";
}
else if (as2Ver.FileVersion?.StartsWith("2017") == true)
{
UnityVersion = as2Ver.FileVersion;
BetaChannel = "none";
BetaChannel = "latest/bleedingedge";
}
else if (as2Ver.FileVersion?.StartsWith("5.5") == true)
{
Expand All @@ -105,16 +100,18 @@ public async Task FindGameAndPatchVersionAsync(string x)
IsPatchInstalled = true;
var version = await File.ReadAllTextAsync(Path.Combine(GameLocation,
"Audiosurf2_Data\\patchupdater\\installedversion.txt"));
if (version.Contains('.'))
{
PatchVersion = version;
PatchChannel = "latest";
}
else
{
PatchVersion = version;
PatchChannel = "beta patch";
}

PatchVersion = version;
PatchChannel = "beta patch";
}
else if (Directory.Exists(Path.Combine(GameLocation, "Audiosurf2_Data\\Updater")))
{
IsPatchInstalled = true;
var version = await File.ReadAllTextAsync(Path.Combine(GameLocation,
"Audiosurf2_Data\\Updater\\version.txt"));

PatchVersion = version;
PatchChannel = "latest patch";
}
}

Expand Down
1 change: 1 addition & 0 deletions Audiosurf2_Tools/ViewModels/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ public async Task LoadSettingsVMAsync()
SettingsViewModel.TwitchQueueCutOffTimeTime = cfg.TwitchQueueCutOffTime.TimeOfDay;
SettingsViewModel.TwitchEnableLocalRequests = cfg.TwitchEnableLocalRequests;
SettingsViewModel.TwitchLocalRequestPath = cfg.TwitchLocalRequestPath;
SettingsViewModel.TwitchLocalRequestMaxSizeMB = cfg.TwitchLocalRequestMaxSizeMB;
});
}

Expand Down
2 changes: 2 additions & 0 deletions Audiosurf2_Tools/ViewModels/SettingsViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public class SettingsViewModel : ViewModelBase

[Reactive] public bool TwitchEnableLocalRequests { get; set; } = false;
[Reactive] public string TwitchLocalRequestPath { get; set; } = "";
[Reactive] public int TwitchLocalRequestMaxSizeMB { get; set; } = 50;


public async Task SaveSettingsAsync()
Expand All @@ -47,6 +48,7 @@ public async Task SaveSettingsAsync()
cfg.TwitchQueueCutOffTime = TwitchQueueCutOffTimeDate + TwitchQueueCutOffTimeTime;
cfg.TwitchEnableLocalRequests = TwitchEnableLocalRequests;
cfg.TwitchLocalRequestPath = TwitchLocalRequestPath;
cfg.TwitchLocalRequestMaxSizeMB = TwitchLocalRequestMaxSizeMB;
var text = JsonSerializer.Serialize(cfg);
var appdata = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
await File.WriteAllTextAsync(Path.Combine(appdata, "AS2Tools\\Settings.json"), text);
Expand Down
40 changes: 33 additions & 7 deletions Audiosurf2_Tools/ViewModels/TwitchBotViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -169,17 +169,43 @@ private void Client_OnCommandReceived(object? sender, OnMessageReceivedArgs e)

else if (cfg.TwitchEnableLocalRequests)
{
if (!File.Exists(Path.Combine(cfg.TwitchLocalRequestPath, e.ChatMessage.Message.Substring(length))))
_ = Task.Run(() =>
LocalRequestHelperAsync(
Path.Combine(cfg.TwitchLocalRequestPath, e.ChatMessage.Message.Substring(length)),
e.ChatMessage.Username));
}
}

public async Task LocalRequestHelperAsync(string path, string username)
{
var cfg = Globals.TryGetGlobal<AppSettings>("Settings");
if (!File.Exists(path))
{
await Task.Delay(5000);
if (!File.Exists(path))
{
_twitchClient.SendMessage(TwitchBotSetupViewModel.ChatChannelResult,
$"@{e.ChatMessage.Username} File not found, did you type the name wrong? (you also need to type the extension like .mp3 or .flac)");
Dispatcher.UIThread.Post(() =>
{
_twitchClient.SendMessage(TwitchBotSetupViewModel.ChatChannelResult,
$"@{username} File not found, did you type the name wrong? (you also need to type the extension like .mp3 or .flac)");
});
return;
}
_ = Task.Run(() =>
HandleSongRequestAsync(
Path.Combine(cfg.TwitchLocalRequestPath, e.ChatMessage.Message.Substring(length)),
e.ChatMessage.Username, false));
}

var fileInfo = new FileInfo(path);
if ((fileInfo.Length / 2048) > cfg!.TwitchLocalRequestMaxSizeMB)
{
Dispatcher.UIThread.Post(() =>
{
_twitchClient.SendMessage(TwitchBotSetupViewModel.ChatChannelResult,
$"@{username} File is too big, the max size allowed is {cfg.TwitchLocalRequestMaxSizeMB}MB");
fileInfo.Delete();
});
return;
}

await HandleSongRequestAsync(path, username, false);
}

public bool InitialCanRequestChecks(string username)
Expand Down

0 comments on commit 61c2869

Please sign in to comment.