Skip to content

Commit

Permalink
Merge pull request #566 from domialex/bugfix-userdata
Browse files Browse the repository at this point in the history
Use LocalApplicationData path instead of the executable's path
  • Loading branch information
domialex authored Apr 18, 2021
2 parents 84fa8c4 + 648a9c0 commit 06623fa
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 23 deletions.
3 changes: 1 addition & 2 deletions src/Sidekick.Application/Settings/SaveSettingsHandler.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.IO;
using System.Reflection;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
Expand Down Expand Up @@ -49,7 +48,7 @@ public async Task<Unit> Handle(SaveSettingsCommand request, CancellationToken ca

var json = JsonSerializer.Serialize(settings);
var defaults = JsonSerializer.Serialize(new SidekickSettings());
var filePath = Path.Combine(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location), FileName);
var filePath = SidekickPaths.GetDataFilePath(FileName);

using var fileStream = File.Create(filePath);
using var writer = new Utf8JsonWriter(fileStream, options: new JsonWriterOptions
Expand Down
16 changes: 16 additions & 0 deletions src/Sidekick.Extensions/SidekickPaths.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;
using System.IO;

namespace Sidekick.Extensions
{
public static class SidekickPaths
{
public static string GetDataFilePath(string fileName)
{
// Possible solution for cross platform support
// var folderPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);

return Path.Combine(Environment.ExpandEnvironmentVariables("%AppData%\\sidekick"), fileName);
}
}
}
8 changes: 5 additions & 3 deletions src/Sidekick.Logging/StartupExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
using System.IO;
using System.Reflection;
using System;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Sentry;
using Serilog;
using Sidekick.Domain.Settings;
using Sidekick.Extensions;

namespace Sidekick.Logging
{
Expand All @@ -14,13 +14,15 @@ public static class StartupExtensions
{
public static IServiceCollection AddSidekickLogging(this IServiceCollection services, IConfiguration configuration, IHostEnvironment environment)
{
var sidekickPath = Environment.ExpandEnvironmentVariables("%AppData%\\sidekick");

var logSink = new LogSink();

Log.Logger = new LoggerConfiguration()
.MinimumLevel.Debug()
.MinimumLevel.Override("Microsoft", Serilog.Events.LogEventLevel.Information)
.Enrich.FromLogContext()
.WriteTo.File(Path.Combine(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location), "Sidekick_log.log"),
.WriteTo.File(SidekickPaths.GetDataFilePath("Sidekick_log.log"),
rollingInterval: RollingInterval.Day,
retainedFileCountLimit: 1,
fileSizeLimitBytes: 5242880,
Expand Down
6 changes: 3 additions & 3 deletions src/Sidekick.Persistence/SidekickDesignTime.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
using System.IO;
using System.Reflection;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;
using Sidekick.Extensions;

namespace Sidekick.Persistence
{
internal class SidekickDesignTime : IDesignTimeDbContextFactory<SidekickContext>
{
public SidekickContext CreateDbContext(string[] args)
{
var builder = new DbContextOptionsBuilder<SidekickContext>();
var connectionString = "Filename=" + Path.Combine(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location), "data.db");
var connectionString = "Filename=" + SidekickPaths.GetDataFilePath("data.db");
builder.UseSqlite(connectionString);
return new SidekickContext(builder.Options);
}
Expand Down
5 changes: 2 additions & 3 deletions src/Sidekick.Persistence/StartupExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
using System.IO;
using System.Reflection;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Sidekick.Domain.Apis.PoeNinja;
using Sidekick.Domain.Cache;
using Sidekick.Domain.Game.Leagues;
using Sidekick.Domain.Views;
using Sidekick.Extensions;
using Sidekick.Persistence.Apis.PoeNinja;
using Sidekick.Persistence.Cache;
using Sidekick.Persistence.ItemCategories;
Expand All @@ -18,7 +17,7 @@ public static class StartupExtensions
{
public static IServiceCollection AddSidekickPersistence(this IServiceCollection services)
{
var connectionString = "Filename=" + Path.Combine(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location), "data.db");
var connectionString = "Filename=" + SidekickPaths.GetDataFilePath("data.db");

services.AddDbContextPool<SidekickContext>(options => options.UseSqlite(connectionString));

Expand Down
11 changes: 6 additions & 5 deletions src/Sidekick.Presentation.Blazor.Electron/Program.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
using System.IO;
using System.Reflection;
using ElectronNET.API;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Sidekick.Application.Settings;
using Sidekick.Extensions;

namespace Sidekick.Presentation.Blazor.Electron
{
Expand All @@ -15,17 +14,19 @@ public static void Main(string[] args)
CreateHostBuilder(args).Build().Run();
}

public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
public static IHostBuilder CreateHostBuilder(string[] args)
{
return Host.CreateDefaultBuilder(args)
.ConfigureAppConfiguration(config =>
{
config.AddJsonFile(Path.Combine(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location), SaveSettingsHandler.FileName), true, true);
config.AddJsonFile(SidekickPaths.GetDataFilePath(SaveSettingsHandler.FileName), true, true);
})
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder
.UseElectron(args)
.UseStartup<Startup>();
});
}
}
}
11 changes: 6 additions & 5 deletions src/Sidekick.Presentation.Blazor/Program.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
using System.IO;
using System.Reflection;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Sidekick.Application.Settings;
using Sidekick.Extensions;

namespace Sidekick.Presentation.Blazor
{
Expand All @@ -14,15 +13,17 @@ public static void Main(string[] args)
CreateHostBuilder(args).Build().Run();
}

public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
public static IHostBuilder CreateHostBuilder(string[] args)
{
return Host.CreateDefaultBuilder(args)
.ConfigureAppConfiguration(config =>
{
config.AddJsonFile(Path.Combine(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location), SaveSettingsHandler.FileName), true, true);
config.AddJsonFile(SidekickPaths.GetDataFilePath(SaveSettingsHandler.FileName), true, true);
})
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
}
}
}
8 changes: 8 additions & 0 deletions src/Sidekick.Presentation.Blazor/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Sidekick.Application;
using Sidekick.Application.Settings;
using Sidekick.Domain.Initialization.Commands;
using Sidekick.Domain.Platforms;
using Sidekick.Domain.Settings.Commands;
using Sidekick.Domain.Views;
using Sidekick.Infrastructure;
using Sidekick.Localization;
Expand Down Expand Up @@ -105,6 +107,12 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IService

Task.Run(async () =>
{
await mediator.Send(new SaveSettingsCommand(new SidekickSettings()
{
Language_Parser = "en",
Language_UI = "en",
LeagueId = "Ultimatum",
}, true));
await mediator.Send(new InitializeCommand(true));
});
}
Expand Down
4 changes: 2 additions & 2 deletions tests/Sidekick.Application.Tests/MediatorFixture.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System.IO;
using System.Reflection;
using System.Threading.Tasks;
using MediatR;
Expand All @@ -11,6 +10,7 @@
using Sidekick.Domain.Platforms;
using Sidekick.Domain.Settings.Commands;
using Sidekick.Domain.Views;
using Sidekick.Extensions;
using Sidekick.Infrastructure;
using Sidekick.Localization;
using Sidekick.Logging;
Expand All @@ -37,7 +37,7 @@ public async Task InitializeAsync()
var mockEnvironment = new Mock<IHostEnvironment>();

var configuration = new ConfigurationBuilder()
.AddJsonFile(Path.Combine(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location), SaveSettingsHandler.FileName), true, true)
.AddJsonFile(SidekickPaths.GetDataFilePath(SaveSettingsHandler.FileName), true, true)
.Build();

var services = new ServiceCollection()
Expand Down

0 comments on commit 06623fa

Please sign in to comment.