Skip to content

Commit

Permalink
Minor cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
IhateTrains committed Apr 24, 2022
1 parent faf5eda commit 56d3b6a
Show file tree
Hide file tree
Showing 12 changed files with 42 additions and 58 deletions.
4 changes: 2 additions & 2 deletions Fronter.NET.Tests/Services/UpdateCheckerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public void LatestReleaseInfoIsDownloaded() {
Assert.Matches(versionRegex, info.Version);
Assert.False(string.IsNullOrWhiteSpace(info.Description));
Assert.NotNull(info.ZipUrl);
Assert.StartsWith($"https://github.com/ParadoxGameConverters/ImperatorToCK3/releases/download/{info.Version}/ImperatorToCK3", info.ZipUrl!);
Assert.EndsWith(".zip", info.ZipUrl!);
Assert.StartsWith($"https://github.com/ParadoxGameConverters/ImperatorToCK3/releases/download/{info.Version}/ImperatorToCK3", info.ZipUrl);
Assert.EndsWith(".zip", info.ZipUrl);
}
}
1 change: 1 addition & 0 deletions Fronter.NET/Extensions/DynamicLocExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public DynamicLocExtension(string path) {
Converter = new LocKeyToValueConverter();
}

// ReSharper disable once UnusedMember.Global
public MultiBinding ProvideValue() {
return this;
}
Expand Down
18 changes: 8 additions & 10 deletions Fronter.NET/Models/Configuration/Configuration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
using Fronter.Models.Configuration.Options;
using Fronter.ViewModels;
using log4net;
using Microsoft.CodeAnalysis;
using ReactiveUI;
using System;
using System.Collections.Generic;
using System.IO;
Expand Down Expand Up @@ -51,7 +49,7 @@ public Configuration() {
logger.Warn($"{fronterOptionsPath} not found!");
}
parser.ClearRegisteredRules();

InitializePaths();

RegisterPreloadKeys(parser);
Expand Down Expand Up @@ -157,23 +155,23 @@ public void InitializePaths() {
if (!OperatingSystem.IsWindows()) {
return;
}

string documentsDir = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);

foreach (var folder in RequiredFolders) {
string? initialValue = null;

if (!string.IsNullOrEmpty(folder.Value)) {
continue;
}

if (folder.SearchPathType == "windowsUsersFolder") {
initialValue = Path.Combine(documentsDir, folder.SearchPath);
} else if (folder.SearchPathType == "steamFolder") {
if (!int.TryParse(folder.SearchPathId, out int steamId)) {
continue;
}

var possiblePath = CommonFunctions.GetSteamInstallPath(steamId);
if (possiblePath is null) {
continue;
Expand All @@ -186,7 +184,7 @@ public void InitializePaths() {
} else if (folder.SearchPathType == "direct") {
initialValue = folder.SearchPath;
}

if (Directory.Exists(initialValue)) {
folder.Value = initialValue;
}
Expand Down Expand Up @@ -220,10 +218,10 @@ public void InitializePaths() {
}
}
}

public bool ExportConfiguration() {
SetSavingStatus("CONVERTSTATUSIN");

if (string.IsNullOrEmpty(ConverterFolder)) {
logger.Error("Converter folder is not set!");
SetSavingStatus("CONVERTSTATUSPOSTFAIL");
Expand Down
2 changes: 1 addition & 1 deletion Fronter.NET/Models/Configuration/RequiredFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public override string Value {
get => base.Value;
set {
if (!string.IsNullOrEmpty(value) && !File.Exists(value)) {
throw new DataValidationException($"File does not exist!");
throw new DataValidationException("File does not exist!");
}

base.Value = value;
Expand Down
2 changes: 1 addition & 1 deletion Fronter.NET/Models/Configuration/RequiredFolder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public override string Value {
get => base.Value;
set {
if (!string.IsNullOrEmpty(value) && !Directory.Exists(value)) {
throw new DataValidationException($"Directory does not exist!");
throw new DataValidationException("Directory does not exist!");
}

base.Value = value;
Expand Down
9 changes: 3 additions & 6 deletions Fronter.NET/Models/LogLine.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
using commonItems;
using Fronter.Services;
using log4net.Core;
using log4net.Core;
using ReactiveUI;
using System.ComponentModel;

namespace Fronter.Models;
namespace Fronter.Models;

public class LogLine : ReactiveObject {
public enum MessageSource {
Expand All @@ -15,7 +12,7 @@ public enum MessageSource {
public string Timestamp { get; set; } = string.Empty;
public Level? Level { get; set; }
public string LevelName => Level?.Name ?? string.Empty;

public MessageSource Source { get; set; } = MessageSource.Uninitialized;

private string message = string.Empty;
Expand Down
5 changes: 2 additions & 3 deletions Fronter.NET/Services/ConverterLauncher.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using commonItems;
using Fronter.LogAppenders;
using Fronter.Models.Configuration;
using log4net;
using log4net.Core;
Expand Down Expand Up @@ -57,11 +56,11 @@ public bool LaunchConverter() {

var timer = new Stopwatch();
timer.Start();

process.Start();
process.BeginOutputReadLine();
process.WaitForExit();

timer.Stop();

if (process.ExitCode == 0) {
Expand Down
9 changes: 4 additions & 5 deletions Fronter.NET/Services/MessageSlicer.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using commonItems;
using Fronter.Models;
using Fronter.Models;
using log4net;
using log4net.Core;
using System.Text.RegularExpressions;
Expand All @@ -11,7 +10,7 @@ public static class MessageSlicer {

public static LogLine SliceMessage(string message) {
var logMessage = new LogLine();

var posOpen = message.IndexOf('[');
var posClose = message.IndexOf(']');

Expand All @@ -24,13 +23,13 @@ public static LogLine SliceMessage(string message) {
if (dateTimeRegex.IsMatch(timestampPart)) {
logMessage.Timestamp = timestampPart;
}

var logLevelStr = message.Substring(posOpen + 1, posClose - posOpen - 1);
logMessage.Level = GetLogLevel(logLevelStr);
if (message.Length >= posClose + 2) {
logMessage.Message = message[(posClose + 2)..];
}

return logMessage;
}

Expand Down
6 changes: 2 additions & 4 deletions Fronter.NET/ValueConverters/LogLevelToColorNameConverter.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
using Avalonia;
using Avalonia.Data.Converters;
using Avalonia.Data.Converters;
using Avalonia.Media;
using commonItems;
using log4net.Core;
using System;

namespace Fronter.ValueConverters;
namespace Fronter.ValueConverters;

// based on https://stackoverflow.com/a/5551986/10249243
public class LogLevelToColorNameConverter : IValueConverter {
Expand Down
25 changes: 12 additions & 13 deletions Fronter.NET/ViewModels/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,23 @@
using commonItems;
using FluentAvalonia.Styling;
using Fronter.Extensions;
using Fronter.LogAppenders;
using Fronter.Models;
using Fronter.Models.Configuration;
using Fronter.Services;
using Fronter.Views;
using log4net;
using log4net.Core;
using MessageBox.Avalonia;
using MessageBox.Avalonia.DTO;
using MessageBox.Avalonia.Enums;
using MessageBox.Avalonia.Models;
using ReactiveUI;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Threading;
using Fronter.LogAppenders;
using log4net;
using log4net.Core;
using System.IO;

namespace Fronter.ViewModels;

Expand All @@ -32,7 +31,7 @@ public class MainWindowViewModel : ViewModelBase {
.ToDictionary(l => l, l => loc.TranslateLanguage(l));

public Configuration Config { get; }

public PathPickerViewModel PathPicker { get; }
public OptionsViewModel Options { get; }

Expand All @@ -57,11 +56,11 @@ public string CopyStatus {
get => copyStatus;
set => this.RaiseAndSetIfChanged(ref copyStatus, value);
}


public MainWindowViewModel() {
Config = new Configuration();

var appenders = LogManager.GetRepository().GetAppenders();
var gridAppender = appenders.First(a => a.Name == "grid");
if (gridAppender is not LogGridAppender logGridAppender) {
Expand Down Expand Up @@ -115,12 +114,12 @@ public void LaunchConverter() {
SaveStatus = "CONVERTSTATUSPRE";
ConvertStatus = "CONVERTSTATUSPRE";
CopyStatus = "CONVERTSTATUSPRE";

if (!VerifyMandatoryPaths()) {
return;
}
Config.ExportConfiguration();

var converterLauncher = new ConverterLauncher(Config);
bool success;
var converterThread = new Thread(() => {
Expand All @@ -145,8 +144,8 @@ public void LaunchConverter() {

public async void CheckForUpdates() {
if (Config.UpdateCheckerEnabled &&
Config.CheckForUpdatesOnStartup &&
UpdateChecker.IsUpdateAvailable("commit_id.txt", Config.PagesCommitIdUrl)) {
Config.CheckForUpdatesOnStartup &&
UpdateChecker.IsUpdateAvailable("commit_id.txt", Config.PagesCommitIdUrl)) {
var info = UpdateChecker.GetLatestReleaseInfo(Config.Name);

const string updateNow = "Update now";
Expand Down
18 changes: 6 additions & 12 deletions Fronter.NET/ViewModels/PathPickerViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.ApplicationLifetimes;
using Avalonia.Controls;
using commonItems;
using Fronter.Extensions;
using Fronter.Models.Configuration;
using Fronter.Views;
using ReactiveUI;
using System;
using System.Collections.ObjectModel;
using System.Reactive;
using System.Threading.Tasks;

namespace Fronter.ViewModels;
namespace Fronter.ViewModels;

/// <summary>
/// The PathPickerViewModel lets the user select paths to various stuff the converter needs to know where to find.
Expand All @@ -24,13 +18,13 @@ public PathPickerViewModel(Configuration config) {

public ObservableCollection<RequiredFolder> RequiredFolders { get; }
public ObservableCollection<RequiredFile> RequiredFiles { get; }

public async void OpenFolderDialog(RequiredFolder folder) {
var dlg = new OpenFolderDialog {
Title = TranslationSource.Instance[folder.DisplayName],
Directory = string.IsNullOrEmpty(folder.Value) ? null : folder.Value
};

var result = await dlg.ShowAsync(MainWindow.Instance);
if (result is not null) {
folder.Value = result;
Expand All @@ -46,9 +40,9 @@ public async void OpenFileDialog(RequiredFile file) {
dlg.Directory = CommonFunctions.GetPath(file.Value);
}
dlg.Filters.Add(new FileDialogFilter {
Extensions = {file.AllowedExtension.TrimStart('*', '.')}
Extensions = { file.AllowedExtension.TrimStart('*', '.') }
});

var result = await dlg.ShowAsync(MainWindow.Instance);
if (result is not null) {
file.Value = result[0];
Expand Down
1 change: 0 additions & 1 deletion Fronter.NET/Views/MainWindow.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:viewModels="clr-namespace:Fronter.ViewModels"
xmlns:views="clr-namespace:Fronter.Views"
xmlns:fronter="clr-namespace:Fronter"
xmlns:ns="clr-namespace:Fronter.Extensions"
xmlns:vc="clr-namespace:Fronter.ValueConverters"
xmlns:log4netCore="clr-namespace:log4net.Core;assembly=log4net"
Expand Down

0 comments on commit 56d3b6a

Please sign in to comment.