Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 28 additions & 3 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,17 @@ indent_style = space
[*.xml]
indent_size = 2

# Xml project files
[*.{csproj,fsproj,vbproj,proj,slnx}]
indent_size = 2

# Xml config files
[*.{props,targets,config,nuspec}]
indent_size = 2

[*.json]
indent_size = 2

# C# files
[*.cs]

Expand All @@ -18,7 +29,6 @@ indent_size = 4
tab_width = 4

# New line preferences
end_of_line = crlf
insert_final_newline = false

#### .NET Coding Conventions ####
Expand Down Expand Up @@ -52,13 +62,16 @@ dotnet_style_require_accessibility_modifiers = for_non_interface_members:silent
dotnet_style_coalesce_expression = true:suggestion
dotnet_style_collection_initializer = true:suggestion
dotnet_style_explicit_tuple_names = true:suggestion
dotnet_style_namespace_match_folder = true:suggestion
dotnet_style_null_propagation = true:suggestion
dotnet_style_object_initializer = true:suggestion
dotnet_style_operator_placement_when_wrapping = beginning_of_line
dotnet_style_prefer_auto_properties = true:suggestion
dotnet_style_prefer_collection_expression = when_types_loosely_match:suggestion
dotnet_style_prefer_compound_assignment = true:suggestion
dotnet_style_prefer_conditional_expression_over_assignment = true:suggestion
dotnet_style_prefer_conditional_expression_over_return = true:suggestion
dotnet_style_prefer_foreach_explicit_cast_in_source = when_strongly_typed:suggestion
dotnet_style_prefer_inferred_anonymous_type_member_names = true:suggestion
dotnet_style_prefer_inferred_tuple_names = true:suggestion
dotnet_style_prefer_is_null_check_over_reference_equality_method = true:suggestion
Expand Down Expand Up @@ -95,6 +108,7 @@ csharp_style_expression_bodied_properties = true:silent
# Pattern matching preferences
csharp_style_pattern_matching_over_as_with_null_check = true:suggestion
csharp_style_pattern_matching_over_is_with_cast_check = true:suggestion
csharp_style_prefer_extended_property_pattern = true:suggestion
csharp_style_prefer_not_pattern = true:suggestion
csharp_style_prefer_pattern_matching = true:silent
csharp_style_prefer_switch_expression = true:suggestion
Expand All @@ -103,20 +117,31 @@ csharp_style_prefer_switch_expression = true:suggestion
csharp_style_conditional_delegate_call = true:suggestion

# Modifier preferences
csharp_prefer_static_anonymous_function = true:suggestion
csharp_prefer_static_local_function = true:warning
csharp_preferred_modifier_order = public,private,protected,internal,static,extern,new,virtual,abstract,sealed,override,readonly,unsafe,volatile,async:silent
csharp_preferred_modifier_order = public,private,protected,internal,file,const,static,extern,new,virtual,abstract,sealed,override,readonly,unsafe,required,volatile,async:suggestion
csharp_style_prefer_readonly_struct = true:suggestion
csharp_style_prefer_readonly_struct_member = true:suggestion

# Code-block preferences
csharp_prefer_braces = true:silent
csharp_prefer_simple_using_statement = true:suggestion
csharp_style_namespace_declarations = file_scoped:suggestion
csharp_style_prefer_method_group_conversion = true:silent
csharp_style_prefer_primary_constructors = true:suggestion
csharp_style_prefer_top_level_statements = true:silent

# Expression-level preferences
csharp_prefer_simple_default_expression = true:suggestion
csharp_style_deconstructed_variable_declaration = true:suggestion
csharp_style_implicit_object_creation_when_type_is_apparent = true:suggestion
csharp_style_inlined_variable_declaration = true:suggestion
csharp_style_pattern_local_over_anonymous_function = true:suggestion
csharp_style_prefer_index_operator = true:suggestion
csharp_style_prefer_local_over_anonymous_function = true:suggestion
csharp_style_prefer_null_check_over_type_check = true:suggestion
csharp_style_prefer_range_operator = true:suggestion
csharp_style_prefer_tuple_swap = true:suggestion
csharp_style_prefer_utf8_string_literals = true:suggestion
csharp_style_throw_expression = true:suggestion
csharp_style_unused_value_assignment_preference = discard_variable:suggestion
csharp_style_unused_value_expression_statement_preference = discard_variable:silent
Expand Down
6 changes: 4 additions & 2 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
<AvaloniaVersion>11.3.12</AvaloniaVersion>
</PropertyGroup>
<ItemGroup>
<PackageVersion Include="AvalonEdit" Version="6.3.1.120" />
<PackageVersion Include="Avalonia" Version="$(AvaloniaVersion)" />
<PackageVersion Include="Avalonia.AvaloniaEdit" Version="11.4.1" />
<PackageVersion Include="Avalonia.Controls.DataGrid" Version="$(AvaloniaVersion)" />
<PackageVersion Include="Avalonia.Desktop" Version="$(AvaloniaVersion)" />
<PackageVersion Include="Avalonia.Themes.Fluent" Version="$(AvaloniaVersion)" />
Expand All @@ -19,10 +19,12 @@
<PackageVersion Include="ConsoleAppFramework" Version="5.2.4" />
<PackageVersion Include="K4os.Compression.LZ4" Version="1.3.8" />
<PackageVersion Include="Microsoft.Extensions.DependencyInjection" Version="$(BclVersion)" />
<PackageVersion Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="$(BclVersion)" />
<PackageVersion Include="Newtonsoft.Json" Version="13.0.4" />
<PackageVersion Include="ObjectListView.Official" Version="2.9.1" />
<PackageVersion Include="ObservableCollections" Version="3.3.4" />
<PackageVersion Include="Ookii.Dialogs.Wpf" Version="5.0.1" />
<PackageVersion Include="Serilog.Extensions.Logging" Version="10.0.0" />
<PackageVersion Include="Serilog.Sinks.File" Version="7.0.0" />
<PackageVersion Include="SharpGLTF.Toolkit" Version="1.0.0-alpha0023" />
<PackageVersion Include="SixLabors.ImageSharp" Version="2.1.13" />
<PackageVersion Include="System.IO.Packaging" Version="$(BclVersion)" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@ public static void Register(Visual recipient)
{
FileDialog.Messenger.Register<Visual, FileOpenMessage>(recipient, FileOpenHandler);
FileDialog.Messenger.Register<Visual, FileSaveMessage>(recipient, FileSaveHandler);
FileDialog.Messenger.Register<Visual, FolderOpenMessage>(recipient, FolderOpenHandler);
}

public static void Unregister(Visual recipient)
{
FileDialog.Messenger.Unregister<FileOpenMessage>(recipient);
FileDialog.Messenger.Unregister<FileSaveMessage>(recipient);
FileDialog.Messenger.Unregister<FolderOpenMessage>(recipient);
}

private static void FileOpenHandler(Visual recipient, FileOpenMessage message)
Expand Down Expand Up @@ -82,4 +84,34 @@ private static void FileSaveHandler(Visual recipient, FileSaveMessage message)
var storageFiles = await topLevel.StorageProvider.SaveFilePickerAsync(options);
return storageFiles?.Path.LocalPath;
}

private static void FolderOpenHandler(Visual recipient, FolderOpenMessage message)
{
message.Reply(Handle(recipient, message));
return;

static async Task<IReadOnlyList<string>> Handle(Visual recipient, FolderOpenMessage message)
{
// Get a reference to our TopLevel (in our case the parent Window)
var topLevel = TopLevel.GetTopLevel(recipient);
if (topLevel is null)
{
return [];
}

var openOptions = message.Options;
var options = new FolderPickerOpenOptions
{
Title = openOptions.Title,
AllowMultiple = openOptions.AllowMultiple,
SuggestedFileName = openOptions.FileName,
SuggestedStartLocation = openOptions.InitialDirectory is null
? null
: await topLevel.StorageProvider.TryGetFolderFromPathAsync(openOptions.InitialDirectory),
};

var storageFiles = await topLevel.StorageProvider.OpenFolderPickerAsync(options);
return storageFiles.Select(x => x.Path.LocalPath).ToArray();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ namespace EgoEngineLibrary.Frontend.Dialogs.File;

public static class FilePickerTypes
{
private static readonly FilePickerFileType Pssg = new("PSSG files")
private static readonly FilePickerFileType Bin = new("Bin files")
{
Patterns = ["*.pssg"],
Patterns = ["*.bin"],
MimeTypes = ["application/octet-stream"],
AppleUniformTypeIdentifiers = ["public.data"]
};
Expand All @@ -18,16 +18,30 @@ public static class FilePickerTypes
AppleUniformTypeIdentifiers = ["public.image"]
};

private static readonly FilePickerFileType Erp = new("ERP files")
{
Patterns = ["*.erp"],
MimeTypes = ["application/octet-stream"],
AppleUniformTypeIdentifiers = ["public.data"]
};

private static readonly FilePickerFileType Gltf = new("Gltf files")
{
Patterns = ["*.glb", "*.gltf"],
MimeTypes = ["model/gltf-binary", "model/gltf+json"],
AppleUniformTypeIdentifiers = ["public.3d-content"]
};

private static readonly FilePickerFileType Bin = new("Bin files")
private static readonly FilePickerFileType Mipmaps = new("Mipmaps files")
{
Patterns = ["*.bin"],
Patterns = ["*.mipmaps"],
MimeTypes = ["application/octet-stream"],
AppleUniformTypeIdentifiers = ["public.data"]
};

private static readonly FilePickerFileType Pssg = new("PSSG files")
{
Patterns = ["*.pssg"],
MimeTypes = ["application/octet-stream"],
AppleUniformTypeIdentifiers = ["public.data"]
};
Expand All @@ -39,7 +53,10 @@ public static FilePickerFileType ToFilePickerType(this FilePickerType type)
FilePickerType.All => FilePickerFileTypes.All,
FilePickerType.Bin => Bin,
FilePickerType.Dds => Dds,
FilePickerType.Erp => Erp,
FilePickerType.Gltf => Gltf,
FilePickerType.Json => FilePickerFileTypes.Json,
FilePickerType.Mipmaps => Mipmaps,
FilePickerType.Pssg => Pssg,
FilePickerType.Xml => FilePickerFileTypes.Xml,
_ => throw new ArgumentOutOfRangeException(nameof(type), type, null)
Expand Down
74 changes: 74 additions & 0 deletions src/EgoEngineLibrary.Frontend/Configuration/Config.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
using System.Collections.Concurrent;

namespace EgoEngineLibrary.Frontend.Configuration;

public static class Config
{
private static readonly ConcurrentDictionary<Type, ConfigInfo> ConfigInfos = new();

public static void Add<T>(IConfigProvider provider)
{
if (!ConfigInfos.TryAdd(typeof(T), new ConfigInfo { Provider = provider }))
{
throw new InvalidOperationException($"The configuration provider for type {typeof(T)} already exists.");
}
}

public static void LoadAll()
{
foreach (var configType in ConfigInfos.Keys)
{
_ = Load(configType);
}
}

public static T Load<T>(bool reload = false)
{
return (T)Load(typeof(T), reload);
}

public static object Load(Type configType, bool reload = false)
{
if (!ConfigInfos.TryGetValue(configType, out var configInfo))
{
throw new InvalidOperationException($"The configuration provider for type {configType} does not exist.");
}

lock (configInfo.Lock)
{
if (!reload && configInfo.Value is not null)
{
return configInfo.Value;
}

var value = configInfo.Provider.Load();
configInfo.Value = value;
return value;
}
}

public static void Save<T>()
{
if (!ConfigInfos.TryGetValue(typeof(T), out var configInfo))
{
throw new InvalidOperationException($"The configuration provider for type {typeof(T)} does not exist.");
}

lock (configInfo.Lock)
{
if (configInfo.Value is not null)
{
configInfo.Provider.Save(configInfo.Value);
}
}
}

private class ConfigInfo
{
public object? Value { get; set; }

public required IConfigProvider Provider { get; init; }

public Lock Lock { get; } = new();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace EgoEngineLibrary.Frontend.Configuration;

public interface IConfigProvider
{
public object Load();

public void Save(object config);
}
9 changes: 9 additions & 0 deletions src/EgoEngineLibrary.Frontend/Configuration/IOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System.Diagnostics.CodeAnalysis;

namespace EgoEngineLibrary.Frontend.Configuration;

public interface IOptions<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] out TOptions>
where TOptions : class
{
TOptions Value { get; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace EgoEngineLibrary.Frontend.Configuration;

public interface IWriteableOptions<out T> : IOptions<T>
where T : class
{
void Save();
}
37 changes: 37 additions & 0 deletions src/EgoEngineLibrary.Frontend/Configuration/JsonConfigProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System.Text.Json;
using System.Text.Json.Serialization.Metadata;

namespace EgoEngineLibrary.Frontend.Configuration;

public class JsonConfigProvider : IConfigProvider
{
private readonly string _filePath;
private readonly JsonTypeInfo _options;

public JsonConfigProvider(string filePath, JsonTypeInfo options)
{
_filePath = filePath;
_options = options;
}

public object Load()
{
using Stream stream = File.Exists(_filePath)
? File.Open(_filePath, FileMode.Open, FileAccess.Read, FileShare.Read)
: new MemoryStream("{}"u8.ToArray());

var res = JsonSerializer.Deserialize(stream, _options);
if (res is null)
{
throw new JsonException("Object is null");
}

return res;
}

public void Save(object config)
{
using var fs = File.Open(_filePath, FileMode.Create, FileAccess.Write, FileShare.Read);
JsonSerializer.Serialize(fs, config, _options);
}
}
11 changes: 11 additions & 0 deletions src/EgoEngineLibrary.Frontend/Configuration/NullOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace EgoEngineLibrary.Frontend.Configuration;

public class NullOptions<T>(T config) : IWriteableOptions<T>
where T : class
{
public T Value { get; } = config;

public void Save()
{
}
}
12 changes: 12 additions & 0 deletions src/EgoEngineLibrary.Frontend/Configuration/WriteableOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace EgoEngineLibrary.Frontend.Configuration;

public class WriteableOptions<T> : IWriteableOptions<T>
where T : class
{
public T Value => Config.Load<T>();

public void Save()
{
Config.Save<T>();
}
}
Loading