Add checkboxes to altar some things in the singleplayer's server config

This commit is contained in:
dginovker 2023-11-05 22:50:55 +09:00
parent cf863a9871
commit 749c0a08dd
5 changed files with 72 additions and 31 deletions

View file

@ -4,7 +4,6 @@ using System.IO.Compression;
using System.Net.Http;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using Saradomin.Model.Settings.Launcher;
using Saradomin.Utilities;
namespace Saradomin.Infrastructure.Services

View file

@ -188,7 +188,6 @@ namespace Saradomin.Utilities
|| RuntimeInformation.IsOSPlatform(OSPlatform.FreeBSD))
{
return Path.Combine(
// Get the XDG_DATA_HOME environment variable, or if it doesn't exist, use the default ~/.local/share
LocateUnixUserHome(),
"2009scape"
);

View file

@ -13,6 +13,7 @@ public static class SingleplayerManagement
"game/data/serverstore",
"game/worldprops"
};
private static readonly string[] FilesToBackup =
{
"game/data/eco/ge_resource.emp",
@ -39,6 +40,7 @@ public static class SingleplayerManagement
log($" Skipping backup of {dir} because it doesn't exist (Full path attempted: {sourcePath})");
continue;
}
string destPath = Path.Combine(newBackupDir, dir);
CopyDirectory(sourcePath, destPath);
}
@ -51,6 +53,7 @@ public static class SingleplayerManagement
log($" Skipping backup of {file} because it doesn't exist (Full path attempted: {sourceFilePath})");
continue;
}
string destFilePath = Path.Combine(newBackupDir, file);
string directoryForFile = Path.GetDirectoryName(destFilePath);
if (!Directory.Exists(directoryForFile))
@ -130,4 +133,24 @@ public static class SingleplayerManagement
log($" Backup from {mostRecentBackupDirName} has been successfully applied.");
}
}
private static Dictionary<string, string> _confCache;
public static T ParseConf<T>(string key, T defaultValue = default)
{
if (!File.Exists(CrossPlatform.GetSingleplayerHome() + "/game/worldprops/default.conf")) return defaultValue;
_confCache ??= File.ReadLines(CrossPlatform.GetSingleplayerHome() + "/game/worldprops/default.conf")
.Where(line => line.Contains('='))
.Select(line => line.Split('='))
.ToDictionary(parts => parts[0].Trim(), parts => parts[1].Trim());
return (T)Convert.ChangeType(_confCache[key], typeof(T));
}
public static void WriteConf(string key, object value)
{
var filePath = CrossPlatform.GetSingleplayerHome() + "/game/worldprops/default.conf";
var lines = File.ReadAllLines(filePath);
File.WriteAllLines(filePath, lines.Select(l => l.StartsWith(key + " =") ? $"{key} = {value}" : l));
_confCache[key] = value.ToString();
}
}

View file

@ -97,7 +97,7 @@
Header="singleplayer links">
<StackPanel Orientation="Vertical">
<Button
Margin="0,0,0,0"
Margin="0,0,0,4"
Width="140"
Height="25"
BorderBrush="{StaticResource DarkBorderBrush}"
@ -110,7 +110,7 @@
ZIndex="9999" />
<Button
Margin="0,0,0,0"
Margin="0,0,0,4"
Width="140"
Height="25"
BorderBrush="{StaticResource DarkBorderBrush}"
@ -130,7 +130,7 @@
Header="singleplayer launch">
<StackPanel Orientation="Vertical">
<Button
Margin="0,0,0,0"
Margin="0,0,0,4"
Width="140"
Height="25"
BorderBrush="{StaticResource DarkBorderBrush}"
@ -144,7 +144,7 @@
IsEnabled="{Binding CanDownload}" />
<Button
Margin="0,0,0,0"
Margin="0,0,0,4"
Width="140"
Height="25"
BorderBrush="{StaticResource DarkBorderBrush}"
@ -158,7 +158,7 @@
IsEnabled="{Binding CanLaunch}" />
<Button
Margin="0,0,0,0"
Margin="0,0,0,4"
Width="140"
Height="25"
BorderBrush="{StaticResource DarkBorderBrush}"
@ -171,7 +171,7 @@
ZIndex="9999"/>
<Button
Margin="0,0,0,0"
Margin="0,0,0,4"
Width="140"
Height="25"
BorderBrush="{StaticResource DarkBorderBrush}"
@ -202,20 +202,23 @@
/>
<CheckBox Content="Cheats"
IsEnabled="False"
IsChecked="{Binding Cheats, Mode=TwoWay}"
IsEnabled="{Binding CanLaunch}"
/>
<CheckBox Content="Fake Players"
IsChecked="True"
IsEnabled="False"
IsChecked="{Binding FakePlayers, Mode=TwoWay}"
IsEnabled="{Binding CanLaunch}"
/>
<CheckBox Content="GE Autostock"
IsEnabled="False"
<CheckBox Content="GE Auto Buy and Sell"
IsChecked="{Binding GEAutoBuySell, Mode=TwoWay}"
IsEnabled="{Binding CanLaunch}"
/>
<CheckBox Content="Debug"
IsEnabled="False"
IsChecked="{Binding Debug, Mode=TwoWay}"
IsEnabled="{Binding CanLaunch}"
/>
<Button
@ -228,7 +231,8 @@
Opacity="1"
FontSize="11"
Content="Reset Config"
IsEnabled="False"
Command="{Binding ResetToDefaults}"
IsEnabled="{Binding CanLaunch}"
ZIndex="9999"/>
</StackPanel>
</HeaderedContentControl>

View file

@ -6,11 +6,10 @@ using Avalonia.Controls;
using Avalonia.Media;
using Avalonia.Threading;
using Glitonea.Mvvm;
using Glitonea.Mvvm.Messaging;
using Saradomin.Infrastructure.Messaging;
using Saradomin.Infrastructure.Services;
using Saradomin.Model.Settings.Launcher;
using Saradomin.Utilities;
using static Saradomin.Utilities.SingleplayerManagement;
namespace Saradomin.ViewModel.Controls;
@ -32,7 +31,6 @@ public class SingleplayerViewModel : ViewModelBase
ISingleplayerUpdateService iSingleplayerUpdateService)
{
_settingsService = settingsService;
Message.Subscribe<MainViewLoadedMessage>(this, OnMainViewLoaded); // todo test delete
_singleplayerUpdateService = iSingleplayerUpdateService;
_singleplayerUpdateService.SingleplayerDownloadProgressChanged += OnSingleplayerDownloadProgressChanged;
@ -56,7 +54,7 @@ public class SingleplayerViewModel : ViewModelBase
if (finished)
{
SingleplayerDownloadText = "Update Singleplayer";
SingleplayerManagement.ApplyLatestBackup(PrintLog);
ApplyLatestBackup(PrintLog);
PrintLog($"");
PrintLog($"Singleplayer Download complete");
CanLaunch = true;
@ -72,17 +70,6 @@ public class SingleplayerViewModel : ViewModelBase
SingleplayerDownloadText = $"Downloading... {progress * 100:F2}%";
}
private void OnMainViewLoaded(MainViewLoadedMessage _)
{
Message.Subscribe<SettingsModifiedMessage>(this, OnSettingsModified);
}
private void OnSettingsModified(SettingsModifiedMessage _)
{
Console.WriteLine("Saving settings");
_settingsService.SaveAll();
}
public void DownloadSingleplayer()
{
CanLaunch = false;
@ -133,4 +120,33 @@ public class SingleplayerViewModel : ViewModelBase
{
CrossPlatform.LaunchURL("https://forum.2009scape.org/viewforum.php?f=8-support");
}
public bool Cheats
{
get => ParseConf<bool>("noauth_default_admin");
set => WriteConf("noauth_default_admin", value);
}
public bool FakePlayers
{
get => ParseConf<bool>("enable_bots", true);
set => WriteConf("enable_bots", value);
}
public bool GEAutoBuySell
{
get => ParseConf<bool>("i_want_to_cheat");
set => WriteConf("i_want_to_cheat", value);
}
public bool Debug
{
get => ParseConf<bool>("debug");
set => WriteConf("debug", value);
}
public void ResetToDefaults()
{
}
}