-
Notifications
You must be signed in to change notification settings - Fork 165
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
963 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
using Microsoft.Data.Sqlite; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Logging; | ||
using Serilog; | ||
using Starward.Features.Database; | ||
using System; | ||
using System.IO; | ||
using System.Net; | ||
using System.Net.Http; | ||
|
||
namespace Starward; | ||
|
||
public static class AppService | ||
{ | ||
|
||
|
||
|
||
private static IServiceProvider _serviceProvider; | ||
|
||
|
||
public static string LogFile { get; private set; } | ||
|
||
|
||
|
||
public static void ResetServiceProvider() | ||
{ | ||
AppSetting.ClearCache(); | ||
_serviceProvider = null!; | ||
} | ||
|
||
|
||
private static void BuildServiceProvider() | ||
{ | ||
if (_serviceProvider == null) | ||
{ | ||
var logFolder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), @"Starward\log"); | ||
Directory.CreateDirectory(logFolder); | ||
LogFile = Path.Combine(logFolder, $"Starward_{DateTime.Now:yyMMdd_HHmmss}.log"); | ||
Log.Logger = new LoggerConfiguration().WriteTo.File(path: LogFile, outputTemplate: "[{Timestamp:HH:mm:ss.fff}] [{Level:u4}] {SourceContext}{NewLine}{Message}{NewLine}{Exception}{NewLine}") | ||
.Enrich.FromLogContext() | ||
.CreateLogger(); | ||
Log.Information($"Welcome to Starward v{AppSetting.AppVersion}\r\nSystem: {Environment.OSVersion}\r\nCommand Line: {Environment.CommandLine}"); | ||
|
||
var sc = new ServiceCollection(); | ||
sc.AddLogging(c => c.AddSerilog(Log.Logger)); | ||
sc.AddTransient(_ => | ||
{ | ||
var client = new HttpClient(new SocketsHttpHandler { AutomaticDecompression = DecompressionMethods.All }) { DefaultRequestVersion = HttpVersion.Version20 }; | ||
client.DefaultRequestHeaders.Add("User-Agent", $"Starward/{AppSetting.AppVersion}"); | ||
return client; | ||
}); | ||
|
||
_serviceProvider = sc.BuildServiceProvider(); | ||
} | ||
} | ||
|
||
|
||
public static T GetService<T>() | ||
{ | ||
BuildServiceProvider(); | ||
return _serviceProvider.GetService<T>()!; | ||
} | ||
|
||
|
||
public static ILogger<T> GetLogger<T>() | ||
{ | ||
BuildServiceProvider(); | ||
return _serviceProvider.GetService<ILogger<T>>()!; | ||
} | ||
|
||
|
||
public static SqliteConnection CreateDatabaseConnection() | ||
{ | ||
return DatabaseService.CreateConnection(); | ||
} | ||
|
||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
using Dapper; | ||
using Starward.Core; | ||
using Starward.Core.GameRecord.Genshin.TravelersDiary; | ||
using Starward.Core.GameRecord.StarRail.TrailblazeCalendar; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Data; | ||
using System.Text.Json; | ||
|
||
namespace Starward.Features.Database; | ||
|
||
|
||
internal class DapperSqlMapper | ||
{ | ||
|
||
private static JsonSerializerOptions JsonSerializerOptions = new JsonSerializerOptions { Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping, PropertyNameCaseInsensitive = true }; | ||
|
||
|
||
public class DateTimeOffsetHandler : SqlMapper.TypeHandler<DateTimeOffset> | ||
{ | ||
public override DateTimeOffset Parse(object value) | ||
{ | ||
if (value is string str) | ||
{ | ||
return DateTimeOffset.Parse(str); | ||
} | ||
else | ||
{ | ||
return new DateTimeOffset(); | ||
} | ||
} | ||
|
||
public override void SetValue(IDbDataParameter parameter, DateTimeOffset value) | ||
{ | ||
parameter.Value = value.ToString(); | ||
} | ||
} | ||
|
||
|
||
|
||
public class TravelersDiaryPrimogemsMonthGroupStatsListHandler : SqlMapper.TypeHandler<List<TravelersDiaryPrimogemsMonthGroupStats>> | ||
{ | ||
public override List<TravelersDiaryPrimogemsMonthGroupStats> Parse(object value) | ||
{ | ||
if (value is string str) | ||
{ | ||
if (!string.IsNullOrWhiteSpace(str)) | ||
{ | ||
return JsonSerializer.Deserialize<List<TravelersDiaryPrimogemsMonthGroupStats>>(str, JsonSerializerOptions)!; | ||
} | ||
} | ||
return new(); | ||
} | ||
|
||
public override void SetValue(IDbDataParameter parameter, List<TravelersDiaryPrimogemsMonthGroupStats>? value) | ||
{ | ||
parameter.Value = JsonSerializer.Serialize(value, JsonSerializerOptions); | ||
} | ||
} | ||
|
||
|
||
public class TrailblazeCalendarMonthDataGroupByListHandler : SqlMapper.TypeHandler<List<TrailblazeCalendarMonthDataGroupBy>> | ||
{ | ||
public override List<TrailblazeCalendarMonthDataGroupBy> Parse(object value) | ||
{ | ||
if (value is string str) | ||
{ | ||
if (!string.IsNullOrWhiteSpace(str)) | ||
{ | ||
return JsonSerializer.Deserialize<List<TrailblazeCalendarMonthDataGroupBy>>(str, JsonSerializerOptions)!; | ||
} | ||
} | ||
return new(); | ||
} | ||
|
||
public override void SetValue(IDbDataParameter parameter, List<TrailblazeCalendarMonthDataGroupBy>? value) | ||
{ | ||
parameter.Value = JsonSerializer.Serialize(value, JsonSerializerOptions); | ||
} | ||
} | ||
|
||
|
||
public class StringListHandler : SqlMapper.TypeHandler<List<string>> | ||
{ | ||
public override List<string> Parse(object value) | ||
{ | ||
if (value is string str) | ||
{ | ||
if (!string.IsNullOrWhiteSpace(str)) | ||
{ | ||
return JsonSerializer.Deserialize<List<string>>(str)!; | ||
} | ||
} | ||
return new(); | ||
} | ||
|
||
public override void SetValue(IDbDataParameter parameter, List<string>? value) | ||
{ | ||
parameter.Value = JsonSerializer.Serialize(value, JsonSerializerOptions); | ||
} | ||
} | ||
|
||
|
||
public class GameBizHandler : SqlMapper.TypeHandler<GameBiz> | ||
{ | ||
public override GameBiz? Parse(object value) | ||
{ | ||
return new GameBiz(value as string); | ||
} | ||
|
||
public override void SetValue(IDbDataParameter parameter, GameBiz? value) | ||
{ | ||
parameter.Value = value?.ToString() ?? ""; | ||
} | ||
} | ||
|
||
|
||
} | ||
|
||
|
Oops, something went wrong.