Files
Torch/Torch.Server/Views/ConfigControl.xaml.cs
z__ 1c92f69bd4 updated NLog to v5
fixed most of issues with world creating/loading
fixed log window lags
fixed some compiler warnings
fixed empty log files creating
fixed logging performance
added better logging of load process
commands autocomplete in torch GUI
chat in torch GUI now has entered history (like console, use up/down arrows)
abstraction of instance manager
session name now correctly displaying in torchSessionManager messages
TorchSession now has loaded world property (as torch now has more control about world load process)
now only dedicated config locks after session start
2022-02-02 14:28:53 +07:00

167 lines
5.9 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;
using System.Windows.Threading;
using Sandbox;
using Torch.API;
using Torch.API.Managers;
using Torch.Server.Annotations;
using Torch.Server.Managers;
using Torch.Server.ViewModels;
using Torch.Views;
using VRage.Game.ModAPI;
using VRage.Serialization;
namespace Torch.Server.Views
{
/// <summary>
/// Interaction logic for ConfigControl.xaml
/// </summary>
public partial class ConfigControl : UserControl, INotifyPropertyChanged
{
private InstanceManager _instanceManager;
private bool _configValid;
public bool ConfigValid { get => _configValid; private set { _configValid = value; OnPropertyChanged(); } }
private List<BindingExpression> _bindingExpressions = new List<BindingExpression>();
public ConfigControl()
{
#pragma warning disable CS0618
var instance = TorchBase.Instance;
#pragma warning restore CS0618
instance.GameStateChanged += InstanceOnGameStateChanged;
_instanceManager = instance.Managers.GetManager<InstanceManager>();
_instanceManager.InstanceLoaded += _instanceManager_InstanceLoaded;
DataContext = _instanceManager.DedicatedConfig;
InitializeComponent();
TorchSettings.DataContext = (TorchConfig)instance.Config;
// Gets called once all children are loaded
Dispatcher.BeginInvoke(DispatcherPriority.Loaded, new Action(ApplyStyles));
}
private void InstanceOnGameStateChanged(MySandboxGame game, TorchGameState newState)
{
if (newState > TorchGameState.Creating)
Dispatcher.InvokeAsync(() => DediConfigScrollViewer.IsEnabled = false);
}
private void CheckValid()
{
ConfigValid = !_bindingExpressions.Any(x => x.HasError);
}
private void ApplyStyles()
{
foreach (var textbox in GetAllChildren<TextBox>(this))
{
textbox.Style = (Style)Resources["ValidatedTextBox"];
var binding = textbox.GetBindingExpression(TextBox.TextProperty);
if (binding == null)
continue;
_bindingExpressions.Add(binding);
textbox.TextChanged += (sender, args) =>
{
binding.UpdateSource();
CheckValid();
};
textbox.LostKeyboardFocus += (sender, args) =>
{
if (binding.HasError)
binding.UpdateTarget();
CheckValid();
};
CheckValid();
}
}
private IEnumerable<T> GetAllChildren<T>(DependencyObject control) where T : DependencyObject
{
var children = LogicalTreeHelper.GetChildren(control).OfType<DependencyObject>();
foreach (var child in children)
{
if (child is T t)
yield return t;
foreach (var grandChild in GetAllChildren<T>(child))
yield return grandChild;
}
}
private void _instanceManager_InstanceLoaded(ConfigDedicatedViewModel obj)
{
Dispatcher.Invoke(() => DataContext = obj);
}
private void Save_OnClick(object sender, RoutedEventArgs e)
{
_instanceManager.SaveConfig();
((ITorchConfig)TorchSettings.DataContext).Save();
}
private void ImportConfig_OnClick(object sender, RoutedEventArgs e)
{
_instanceManager.ImportSelectedWorldConfig();
}
private void Selector_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
{
//The control doesn't update the binding before firing the event.
if (e.AddedItems.Count > 0)
{
var result = MessageBoxResult.Yes; //MessageBox.Show("Do you want to import the session settings from the selected world?", "Import Config", MessageBoxButton.YesNo);
var world = (WorldViewModel)e.AddedItems[0]!;
_instanceManager.SelectWorld(world.WorldPath, result != MessageBoxResult.Yes);
}
}
public event PropertyChangedEventHandler PropertyChanged;
[NotifyPropertyChangedInvocator]
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
private void NewWorld_OnClick(object sender, RoutedEventArgs e)
{
var c = new WorldGeneratorDialog(_instanceManager);
c.Show();
}
private void RoleEdit_Onlick(object sender, RoutedEventArgs e)
{
//var w = new RoleEditor(_instanceManager.DedicatedConfig.SelectedWorld);
//w.Show();
var d = new RoleEditor();
var w = _instanceManager.DedicatedConfig.SelectedWorld;
if(w.Checkpoint.PromotedUsers == null) {
w.Checkpoint.PromotedUsers = new VRage.Serialization.SerializableDictionary<ulong, MyPromoteLevel>();
}
if (w == null)
{
MessageBox.Show("A world is not selected.");
return;
}
if (w.Checkpoint.PromotedUsers == null)
w.Checkpoint.PromotedUsers = new SerializableDictionary<ulong, MyPromoteLevel>();
d.Edit(w.Checkpoint.PromotedUsers.Dictionary);
_instanceManager.DedicatedConfig.Administrators = w.Checkpoint.PromotedUsers.Dictionary.Where(k => k.Value >= MyPromoteLevel.Admin).Select(k => k.Key.ToString()).ToList();
}
}
}