Отчистка кода

This commit is contained in:
2025-04-15 16:47:40 +04:00
parent 4fec054ac6
commit 414ac03f83
47 changed files with 2192 additions and 2078 deletions
+267
View File
@@ -0,0 +1,267 @@
# Удалите строку ниже, если вы хотите наследовать параметры .editorconfig из каталогов, расположенных выше в иерархии
root = true
# Файлы C#
[*.cs]
#### Основные параметры EditorConfig ####
# Отступы и интервалы
indent_size = 4
indent_style = space
tab_width = 4
# Предпочтения для новых строк
end_of_line = crlf
insert_final_newline = false
#### Действия кода .NET ####
# Члены типа
dotnet_hide_advanced_members = false
dotnet_member_insertion_location = with_other_members_of_the_same_kind
dotnet_property_generation_behavior = prefer_throwing_properties
# Поиск символов
dotnet_search_reference_assemblies = true
#### Рекомендации по написанию кода .NET ####
# Упорядочение Using
dotnet_separate_import_directive_groups = false
dotnet_sort_system_directives_first = false
file_header_template = unset
# Предпочтения для this. и Me.
dotnet_style_qualification_for_event = false
dotnet_style_qualification_for_field = false
dotnet_style_qualification_for_method = false
dotnet_style_qualification_for_property = false
# Параметры использования ключевых слов языка и типов BCL
dotnet_style_predefined_type_for_locals_parameters_members = true
dotnet_style_predefined_type_for_member_access = true
# Предпочтения для скобок
dotnet_style_parentheses_in_arithmetic_binary_operators = always_for_clarity
dotnet_style_parentheses_in_other_binary_operators = always_for_clarity
dotnet_style_parentheses_in_other_operators = never_if_unnecessary
dotnet_style_parentheses_in_relational_binary_operators = always_for_clarity
# Предпочтения модификатора
dotnet_style_require_accessibility_modifiers = for_non_interface_members
# Выражения уровень предпочтения
dotnet_prefer_system_hash_code = true
dotnet_style_coalesce_expression = true
dotnet_style_collection_initializer = true
dotnet_style_explicit_tuple_names = true
dotnet_style_namespace_match_folder = true
dotnet_style_null_propagation = true
dotnet_style_object_initializer = true
dotnet_style_operator_placement_when_wrapping = beginning_of_line
dotnet_style_prefer_auto_properties = true
dotnet_style_prefer_collection_expression = when_types_loosely_match
dotnet_style_prefer_compound_assignment = true
dotnet_style_prefer_conditional_expression_over_assignment = true
dotnet_style_prefer_conditional_expression_over_return = true
dotnet_style_prefer_foreach_explicit_cast_in_source = when_strongly_typed
dotnet_style_prefer_inferred_anonymous_type_member_names = true
dotnet_style_prefer_inferred_tuple_names = true
dotnet_style_prefer_is_null_check_over_reference_equality_method = true
dotnet_style_prefer_simplified_boolean_expressions = true
dotnet_style_prefer_simplified_interpolation = true
# Предпочтения для полей
dotnet_style_readonly_field = true
# Настройки параметров
dotnet_code_quality_unused_parameters = all
# Параметры подавления
dotnet_remove_unnecessary_suppression_exclusions = none
# Предпочтения для новых строк
dotnet_style_allow_multiple_blank_lines_experimental = true
dotnet_style_allow_statement_immediately_after_block_experimental = true
#### Рекомендации по написанию кода C# ####
# Предпочтения var
csharp_style_var_elsewhere = false
csharp_style_var_for_built_in_types = false
csharp_style_var_when_type_is_apparent = false
# Члены, заданные выражениями
csharp_style_expression_bodied_accessors = true
csharp_style_expression_bodied_constructors = false
csharp_style_expression_bodied_indexers = true
csharp_style_expression_bodied_lambdas = true
csharp_style_expression_bodied_local_functions = false
csharp_style_expression_bodied_methods = false
csharp_style_expression_bodied_operators = false
csharp_style_expression_bodied_properties = true
# Настройки соответствия шаблонов
csharp_style_pattern_matching_over_as_with_null_check = true
csharp_style_pattern_matching_over_is_with_cast_check = true
csharp_style_prefer_extended_property_pattern = true
csharp_style_prefer_not_pattern = true
csharp_style_prefer_pattern_matching = true
csharp_style_prefer_switch_expression = true
# Настройки проверки на null
csharp_style_conditional_delegate_call = true
# Предпочтения модификатора
csharp_prefer_static_anonymous_function = true
csharp_prefer_static_local_function = true
csharp_preferred_modifier_order = public,private,protected,internal,file,static,extern,new,virtual,abstract,sealed,override,readonly,unsafe,required,volatile,async
csharp_style_prefer_readonly_struct = true
csharp_style_prefer_readonly_struct_member = true
# Предпочтения для блоков кода
csharp_prefer_braces = true
csharp_prefer_simple_using_statement = true
csharp_prefer_system_threading_lock = true
csharp_style_namespace_declarations = file_scoped
csharp_style_prefer_method_group_conversion = true
csharp_style_prefer_primary_constructors = true
csharp_style_prefer_top_level_statements = true
# Выражения уровень предпочтения
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_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_unbound_generic_type_in_nameof = 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
# предпочтения для директивы using
csharp_using_directive_placement = outside_namespace
# Предпочтения для новых строк
csharp_style_allow_blank_line_after_colon_in_constructor_initializer_experimental = true
csharp_style_allow_blank_line_after_token_in_arrow_expression_clause_experimental = true
csharp_style_allow_blank_line_after_token_in_conditional_expression_experimental = true
csharp_style_allow_blank_lines_between_consecutive_braces_experimental = true
csharp_style_allow_embedded_statements_on_same_line_experimental = true
#### Правила форматирования C# ####
# Предпочтения для новых строк
csharp_new_line_before_catch = true
csharp_new_line_before_else = true
csharp_new_line_before_finally = true
csharp_new_line_before_members_in_anonymous_types = true
csharp_new_line_before_members_in_object_initializers = true
csharp_new_line_before_open_brace = all
csharp_new_line_between_query_expression_clauses = true
# Параметры отступов
csharp_indent_block_contents = true
csharp_indent_braces = false
csharp_indent_case_contents = true
csharp_indent_case_contents_when_block = false
csharp_indent_labels = one_less_than_current
csharp_indent_switch_labels = true
# Предпочтения для интервалов
csharp_space_after_cast = true
csharp_space_after_colon_in_inheritance_clause = true
csharp_space_after_comma = true
csharp_space_after_dot = false
csharp_space_after_keywords_in_control_flow_statements = true
csharp_space_after_semicolon_in_for_statement = true
csharp_space_around_binary_operators = before_and_after
csharp_space_around_declaration_statements = false
csharp_space_before_colon_in_inheritance_clause = true
csharp_space_before_comma = false
csharp_space_before_dot = false
csharp_space_before_open_square_brackets = false
csharp_space_before_semicolon_in_for_statement = false
csharp_space_between_empty_square_brackets = false
csharp_space_between_method_call_empty_parameter_list_parentheses = false
csharp_space_between_method_call_name_and_opening_parenthesis = false
csharp_space_between_method_call_parameter_list_parentheses = false
csharp_space_between_method_declaration_empty_parameter_list_parentheses = false
csharp_space_between_method_declaration_name_and_open_parenthesis = false
csharp_space_between_method_declaration_parameter_list_parentheses = false
csharp_space_between_parentheses = false
csharp_space_between_square_brackets = false
# Параметры переноса
csharp_preserve_single_line_blocks = true
csharp_preserve_single_line_statements = false
#### Стили именования ####
# Правила именования
dotnet_naming_rule.interface_should_be_begins_with_i.severity = suggestion
dotnet_naming_rule.interface_should_be_begins_with_i.symbols = interface
dotnet_naming_rule.interface_should_be_begins_with_i.style = begins_with_i
dotnet_naming_rule.types_should_be_pascal_case.severity = suggestion
dotnet_naming_rule.types_should_be_pascal_case.symbols = types
dotnet_naming_rule.types_should_be_pascal_case.style = pascal_case
dotnet_naming_rule.non_field_members_should_be_pascal_case.severity = suggestion
dotnet_naming_rule.non_field_members_should_be_pascal_case.symbols = non_field_members
dotnet_naming_rule.non_field_members_should_be_pascal_case.style = pascal_case
# Спецификации символов
dotnet_naming_symbols.interface.applicable_kinds = interface
dotnet_naming_symbols.interface.applicable_accessibilities = public, internal, private, protected, protected_internal, private_protected
dotnet_naming_symbols.interface.required_modifiers =
dotnet_naming_symbols.types.applicable_kinds = class, struct, interface, enum
dotnet_naming_symbols.types.applicable_accessibilities = public, internal, private, protected, protected_internal, private_protected
dotnet_naming_symbols.types.required_modifiers =
dotnet_naming_symbols.non_field_members.applicable_kinds = property, event, method
dotnet_naming_symbols.non_field_members.applicable_accessibilities = public, internal, private, protected, protected_internal, private_protected
dotnet_naming_symbols.non_field_members.required_modifiers =
# Стили именования
dotnet_naming_style.pascal_case.required_prefix =
dotnet_naming_style.pascal_case.required_suffix =
dotnet_naming_style.pascal_case.word_separator =
dotnet_naming_style.pascal_case.capitalization = pascal_case
dotnet_naming_style.begins_with_i.required_prefix = I
dotnet_naming_style.begins_with_i.required_suffix =
dotnet_naming_style.begins_with_i.word_separator =
dotnet_naming_style.begins_with_i.capitalization = pascal_case
[*.{cs,vb}]
dotnet_style_coalesce_expression = true:suggestion
dotnet_style_null_propagation = true:suggestion
dotnet_style_prefer_is_null_check_over_reference_equality_method = true:suggestion
dotnet_style_prefer_auto_properties = true:silent
dotnet_style_object_initializer = true:suggestion
dotnet_style_collection_initializer = true:suggestion
dotnet_style_prefer_simplified_boolean_expressions = true:suggestion
dotnet_style_prefer_conditional_expression_over_assignment = true:silent
dotnet_style_prefer_conditional_expression_over_return = true:silent
dotnet_style_explicit_tuple_names = true:suggestion
dotnet_style_prefer_inferred_tuple_names = true:suggestion
dotnet_style_prefer_inferred_anonymous_type_member_names = true:suggestion
dotnet_style_prefer_compound_assignment = true:suggestion
dotnet_style_prefer_simplified_interpolation = true:suggestion
dotnet_style_prefer_collection_expression = when_types_loosely_match:suggestion
dotnet_style_namespace_match_folder = true:suggestion
dotnet_code_quality_unused_parameters = all:suggestion
dotnet_style_operator_placement_when_wrapping = beginning_of_line
tab_width = 4
indent_size = 4
+2 -3
View File
@@ -1,8 +1,8 @@
using dotenv.net;
using dotenv.net.Utilities;
namespace Pf2eDataScrapper
{
namespace Pf2eDataScrapper;
/// <summary>
/// Класс настроек.
/// </summary>
@@ -38,4 +38,3 @@ namespace Pf2eDataScrapper
public static string DatabaseConnection => EnvReader.GetStringValue("DATABASE_CONNECTION");
}
}
@@ -1,7 +1,7 @@
using Pf2eDataScrapper.Importers.Repositories;
namespace Pf2eDataScrapper.Importers
{
namespace Pf2eDataScrapper.Importers;
/// <summary>
/// Абстрактный класс импортёра
/// </summary>
@@ -39,4 +39,3 @@ namespace Pf2eDataScrapper.Importers
/// </summary>
public abstract void BuildSchemas();
}
}
@@ -1,8 +1,8 @@
using Pf2eDataScrapper.Importers.Repositories;
using Pf2eDataScrapper.Importers.Implementations.Aonprd.SchemaBuilder;
using Pf2eDataScrapper.Importers.Implementations.Aonprd.SchemaBuilder;
using Pf2eDataScrapper.Importers.Repositories;
namespace Pf2eDataScrapper.Importers.Implementations.Aonprd;
namespace Pf2eDataScrapper.Importers.Implementations.Aonprd
{
internal class AonprdImporter : AbstractImporter
{
public override string Name => "Archives of Nethys Pathfinder 2e";
@@ -26,7 +26,7 @@ namespace Pf2eDataScrapper.Importers.Implementations.Aonprd
public override void BuildSchemas()
{
var builder = new AonprdSchemaBuilder(Loader);
AonprdSchemaBuilder builder = new(Loader);
builder.BuildSchemas();
builder.Save(EnvSettings.OutputDir);
@@ -37,4 +37,3 @@ namespace Pf2eDataScrapper.Importers.Implementations.Aonprd
throw new NotImplementedException();
}
}
}
@@ -1,9 +1,8 @@
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Linq;
using Pf2eDataScrapper.Importers.Implementations.Aonprd.Compendium;
namespace Pf2eDataScrapper.Importers.Implementations.Aonprd
{
namespace Pf2eDataScrapper.Importers.Implementations.Aonprd;
internal class AonprdLoader(AonprdImporter importer)
{
public AonprdImporter Importer = importer;
@@ -14,15 +13,15 @@ namespace Pf2eDataScrapper.Importers.Implementations.Aonprd
public void Prepare()
{
using var indexFile = new StreamReader(Path.Combine(Importer.Dir, "index.json"));
var indexJson = JObject.Parse(indexFile.ReadToEnd());
using StreamReader indexFile = new(Path.Combine(Importer.Dir, "index.json"));
JObject indexJson = JObject.Parse(indexFile.ReadToEnd());
foreach (var kvp in indexJson)
foreach (KeyValuePair<string, JToken?> kvp in indexJson)
{
var file = kvp.Key.ToString();
var items = (JArray)kvp.Value!;
string file = kvp.Key.ToString();
JArray items = (JArray) kvp.Value!;
foreach (var item in items)
foreach (JToken item in items)
{
Compendium.AddReference(item.ToString(), file);
}
@@ -31,7 +30,7 @@ namespace Pf2eDataScrapper.Importers.Implementations.Aonprd
public IEnumerable<LoaderEntry> Entries()
{
return Entries(Compendium.Types.Keys.ToArray());
return Entries([.. Compendium.Types.Keys]);
}
public IEnumerable<LoaderEntry> Entries(string types)
@@ -41,26 +40,26 @@ namespace Pf2eDataScrapper.Importers.Implementations.Aonprd
public IEnumerable<LoaderEntry> Entries(string[] types)
{
foreach (var file in Compendium.Files(types))
foreach (string? file in Compendium.Files(types))
{
if (file is null)
{
continue;
}
using var fileReader = new StreamReader(Path.Combine(Importer.Dir, $"{file}.json"));
var array = JArray.Parse(fileReader.ReadToEnd());
using StreamReader fileReader = new(Path.Combine(Importer.Dir, $"{file}.json"));
JArray array = JArray.Parse(fileReader.ReadToEnd());
foreach (var item in array)
foreach (JToken item in array)
{
var id = item.SelectToken("id")?.ToString();
string? id = item.SelectToken("id")?.ToString();
if (id is null)
{
continue;
}
var entry = new LoaderEntry(id, file, (JObject)item);
LoaderEntry entry = new(id, file, (JObject) item);
if (types.Contains(entry.Type))
{
@@ -76,4 +75,3 @@ namespace Pf2eDataScrapper.Importers.Implementations.Aonprd
public JObject Json { get; } = itemJson;
}
}
}
@@ -2,8 +2,8 @@
using Newtonsoft.Json.Linq;
using Pf2eDataScrapper.Importers.Repositories;
namespace Pf2eDataScrapper.Importers.Implementations.Aonprd
{
namespace Pf2eDataScrapper.Importers.Implementations.Aonprd;
internal class AonprdRepository(AbstractImporter importer, string searchUrl) : AbstractImporterRepository(importer)
{
public readonly string SearchUrl = searchUrl;
@@ -17,9 +17,9 @@ namespace Pf2eDataScrapper.Importers.Implementations.Aonprd
url = SearchUrl + url;
}
using var client = new HttpClient();
using var stream = client.GetStreamAsync(url);
using var reader = new StreamReader(stream.Result);
using HttpClient client = new();
using Task<Stream> stream = client.GetStreamAsync(url);
using StreamReader reader = new(stream.Result);
return reader.ReadToEnd();
}
@@ -44,22 +44,22 @@ namespace Pf2eDataScrapper.Importers.Implementations.Aonprd
WriteFile("revision.txt", Revision.ToString());
Console.WriteLine("Загрузка индекса...");
var indexContent = Load($"/json-data/aon{Revision}-index.json");
string indexContent = Load($"/json-data/aon{Revision}-index.json");
WriteFile("index.json", indexContent);
var indexJson = JObject.Parse(indexContent);
JObject indexJson = JObject.Parse(indexContent);
Console.WriteLine("Загрузка индексных файлов...");
var bar = new ProgressBar(indexJson.Count);
var i = 0;
ProgressBar bar = new(indexJson.Count);
int i = 0;
foreach (var kvp in indexJson)
foreach (KeyValuePair<string, JToken?> kvp in indexJson)
{
var fileName = kvp.Key;
string fileName = kvp.Key;
bar.Refresh(i++, fileName);
var fileContent = Load($"/json-data/{fileName}.json");
string fileContent = Load($"/json-data/{fileName}.json");
WriteFile($"{fileName}.json", fileContent);
}
@@ -73,30 +73,29 @@ namespace Pf2eDataScrapper.Importers.Implementations.Aonprd
/// <returns>true - актуальная ревизия, false - устаревшая/отсутсвует</returns>
private bool CheckRevision()
{
var page = JObject.Parse(Load("/aon/_doc/feat-1"));
JObject page = JObject.Parse(Load("/aon/_doc/feat-1"));
Revision = Convert.ToInt32(page.SelectToken("_index")?.ToObject<string>()?.Replace("aon", "") ?? throw new Exception("Can't get revision"));
var revisionFile = Path.Combine(RepositoryDirPath, "revision.txt");
string revisionFile = Path.Combine(RepositoryDirPath, "revision.txt");
if (!File.Exists(revisionFile))
{
return false;
}
using var reader = new StreamReader(revisionFile);
var lastRevision = reader.ReadToEnd();
using StreamReader reader = new(revisionFile);
string lastRevision = reader.ReadToEnd();
return lastRevision == Revision.ToString();
}
private void WriteFile(string path, string content)
{
var filePath = Path.Combine(RepositoryDirPath, path);
string filePath = Path.Combine(RepositoryDirPath, path);
if (File.Exists(filePath))
{
File.Delete(filePath);
}
using var writer = new StreamWriter(filePath);
using StreamWriter writer = new(filePath);
writer.Write(content);
}
}
}
@@ -1,5 +1,5 @@
namespace Pf2eDataScrapper.Importers.Implementations.Aonprd.Compendium
{
namespace Pf2eDataScrapper.Importers.Implementations.Aonprd.Compendium;
internal class AonprdCompendium
{
public readonly Dictionary<string, AonprdType> Types = [];
@@ -8,7 +8,7 @@
{
AonprdReference reference = new(id, file);
if (Types.TryGetValue(reference.Type, out var type))
if (Types.TryGetValue(reference.Type, out AonprdType? type))
{
type.Add(reference);
}
@@ -25,4 +25,3 @@
.SelectMany(t => t.Files)
.Distinct();
}
}
@@ -1,6 +1,6 @@
namespace Pf2eDataScrapper.Importers.Implementations.Aonprd.Compendium
{
namespace Pf2eDataScrapper.Importers.Implementations.Aonprd.Compendium;
internal class AonprdReference(string id, string file)
{
public readonly string Id = id;
@@ -13,23 +13,11 @@ namespace Pf2eDataScrapper.Importers.Implementations.Aonprd.Compendium
{
static string? subType(string type, string cat)
{
if (type.StartsWith($"{cat}-") && type != cat)
{
if (type.EndsWith("-bonus"))
{
return $"{cat}-bonus";
}
else
{
return $"{cat}-sub";
}
}
return null;
return type.StartsWith($"{cat}-") && type != cat ? type.EndsWith("-bonus") ? $"{cat}-bonus" : $"{cat}-sub" : null;
}
var type = string.Join('-', id.Split('-')[0..^1]);
string type = string.Join('-', id.Split('-')[0..^1]);
return subType(type, "equipment") ?? subType(type, "weapon") ?? type;
}
}
}
@@ -1,5 +1,5 @@
namespace Pf2eDataScrapper.Importers.Implementations.Aonprd.Compendium
{
namespace Pf2eDataScrapper.Importers.Implementations.Aonprd.Compendium;
internal class AonprdType(string type)
{
public readonly string Type = type;
@@ -20,4 +20,3 @@
public int Count => Items.Count;
}
}
@@ -2,8 +2,8 @@
using Pf2eDataScrapper.Importers.Implementations.Aonprd.Compendium;
using Pf2eDataScrapper.SchemaBuilder;
namespace Pf2eDataScrapper.Importers.Implementations.Aonprd.SchemaBuilder
{
namespace Pf2eDataScrapper.Importers.Implementations.Aonprd.SchemaBuilder;
internal class AonprdSchemaBuilder(AonprdLoader loader)
{
public readonly AonprdLoader Loader = loader;
@@ -33,10 +33,10 @@ namespace Pf2eDataScrapper.Importers.Implementations.Aonprd.SchemaBuilder
{
Console.WriteLine("Building schemas...");
var bar = new ProgressBar(Loader.ItemCount);
var i = 0;
ProgressBar bar = new ProgressBar(Loader.ItemCount);
int i = 0;
foreach (var entry in Loader.Entries())
foreach (AonprdLoader.LoaderEntry entry in Loader.Entries())
{
bar.Refresh(i++, entry.Type);
GetSchema(entry).AddValue(entry.Json);
@@ -51,10 +51,10 @@ namespace Pf2eDataScrapper.Importers.Implementations.Aonprd.SchemaBuilder
targetDir = Path.Combine(targetDir, Loader.Importer.Slug);
var bar = new ProgressBar(Schemas.Count);
var i = 0;
ProgressBar bar = new(Schemas.Count);
int i = 0;
foreach (var kvp in Schemas)
foreach (KeyValuePair<string, SchemaNode> kvp in Schemas)
{
bar.Refresh(i++, kvp.Key);
SaveSchema(targetDir, kvp.Key, kvp.Value);
@@ -63,17 +63,17 @@ namespace Pf2eDataScrapper.Importers.Implementations.Aonprd.SchemaBuilder
protected void SaveSchema(string dir, string name, SchemaNode schema)
{
var invalids = Path.GetInvalidFileNameChars();
var fileName = string.Join("_", schema.Path.Split(invalids, StringSplitOptions.RemoveEmptyEntries)).TrimEnd('.');
var path = Path.Combine(dir, fileName + ".yaml");
char[] invalids = Path.GetInvalidFileNameChars();
string fileName = string.Join("_", schema.Path.Split(invalids, StringSplitOptions.RemoveEmptyEntries)).TrimEnd('.');
string path = Path.Combine(dir, fileName + ".yaml");
var dirPath = Path.GetDirectoryName(path);
string? dirPath = Path.GetDirectoryName(path);
if (dirPath is not null && !Directory.Exists(dirPath))
{
Directory.CreateDirectory(dirPath);
}
using var writer = new StreamWriter(path);
using StreamWriter writer = new(path);
schema.Beautify();
@@ -84,4 +84,3 @@ namespace Pf2eDataScrapper.Importers.Implementations.Aonprd.SchemaBuilder
}, schema.Types[0].GetYamlData()));
}
}
}
@@ -1,5 +1,5 @@
namespace Pf2eDataScrapper.Importers.Implementations.Pf2eFoundry.Compendium
{
namespace Pf2eDataScrapper.Importers.Implementations.Pf2eFoundry.Compendium;
/// <summary>
/// Тип документов компендиума.
/// </summary>
@@ -15,4 +15,3 @@
RollTable,
Scene
}
}
@@ -1,7 +1,7 @@
using Newtonsoft.Json.Linq;
namespace Pf2eDataScrapper.Importers.Implementations.Pf2eFoundry.Compendium
{
namespace Pf2eDataScrapper.Importers.Implementations.Pf2eFoundry.Compendium;
/// <summary>
/// Папка компендиума.
/// </summary>
@@ -43,7 +43,7 @@ namespace Pf2eDataScrapper.Importers.Implementations.Pf2eFoundry.Compendium
/// <param name="json"></param>
public CompendiumFolder(JObject json)
{
if (json.TryGetValue("_id", out var id))
if (json.TryGetValue("_id", out JToken? id))
{
// Вложенная папка через _folders.json
Id = id!.ToString();
@@ -85,4 +85,3 @@ namespace Pf2eDataScrapper.Importers.Implementations.Pf2eFoundry.Compendium
return $"CompendiumFolder([Id={Id}][Type={Type}][Name={Name}][Path={Path}][Parent={Parent?.Name ?? "null"}])";
}
}
}
@@ -1,5 +1,5 @@
namespace Pf2eDataScrapper.Importers.Implementations.Pf2eFoundry.Compendium
{
namespace Pf2eDataScrapper.Importers.Implementations.Pf2eFoundry.Compendium;
/// <summary>
/// Коллекция папок компендиума
/// </summary>
@@ -25,4 +25,3 @@
return [.. this.FindAll(x => x.Type != CompendiumDocumentType.Actor && x.Name == "Conditions")];
}
}
}
@@ -1,8 +1,8 @@
using Newtonsoft.Json.Linq;
using Pf2eDataScrapper.Importers.Implementations.Pf2eFoundry.Compendium;
namespace Pf2eDataScrapper.Importers.Implementations.Pf2eFoundry
{
namespace Pf2eDataScrapper.Importers.Implementations.Pf2eFoundry;
/// <summary>
/// Загрузчик данных компендиума
/// </summary>
@@ -36,25 +36,25 @@ namespace Pf2eDataScrapper.Importers.Implementations.Pf2eFoundry
/// <param name="parent"></param>
protected void ProcessPackFolders(JArray folders, CompendiumFolder? parent)
{
foreach (var token in folders)
foreach (JToken token in folders)
{
JObject obj = (JObject) token;
var id = $"folder-{_folderCount++}";
var name = obj["name"]!.ToString();
string id = $"folder-{_folderCount++}";
string name = obj["name"]!.ToString();
var folder = new CompendiumFolder(id, name)
CompendiumFolder folder = new(id, name)
{
Parent = parent
};
Collection.Add(folder);
if (obj.TryGetValue("packs", out var packs))
if (obj.TryGetValue("packs", out JToken? packs))
{
foreach (var jToken in packs!)
foreach (JToken jToken in packs!)
{
var packId = jToken.ToString();
var child = Collection.GetById(packId);
string packId = jToken.ToString();
CompendiumFolder? child = Collection.GetById(packId);
if (child is not null)
{
@@ -63,7 +63,7 @@ namespace Pf2eDataScrapper.Importers.Implementations.Pf2eFoundry
}
}
if (obj.TryGetValue("folders", out var innerFolders))
if (obj.TryGetValue("folders", out JToken? innerFolders))
{
ProcessPackFolders((JArray) innerFolders!, folder);
}
@@ -77,43 +77,43 @@ namespace Pf2eDataScrapper.Importers.Implementations.Pf2eFoundry
/// </summary>
public void Prepare()
{
using var systemFile = new StreamReader(Path.Combine(Importer.Dir, "static", "system.json"));
var systemData = JObject.Parse(systemFile.ReadToEnd());
using StreamReader systemFile = new(Path.Combine(Importer.Dir, "static", "system.json"));
JObject systemData = JObject.Parse(systemFile.ReadToEnd());
foreach (var pack in systemData["packs"]!)
foreach (JToken pack in systemData["packs"]!)
{
var folder = new CompendiumFolder((JObject)pack);
CompendiumFolder folder = new((JObject) pack);
Collection.Add(folder);
}
if (systemData.TryGetValue("packFolders", out var packFolders))
if (systemData.TryGetValue("packFolders", out JToken? packFolders))
{
ProcessPackFolders((JArray) packFolders, null);
}
// Поиск _folders.json
foreach (var parent in Collection.ToList())
foreach (CompendiumFolder? parent in Collection.ToList())
{
if (parent.Path is null)
{
continue;
}
var dirPath = Path.Combine(Importer.Dir, parent.Path);
string dirPath = Path.Combine(Importer.Dir, parent.Path);
ItemCount += Directory.GetFiles(dirPath, "*.json", SearchOption.AllDirectories)
.Length;
foreach (var foldersFilePath in Directory.GetFiles(dirPath, "_folders.json", SearchOption.AllDirectories))
foreach (string foldersFilePath in Directory.GetFiles(dirPath, "_folders.json", SearchOption.AllDirectories))
{
ItemCount--;
using var file = new StreamReader(foldersFilePath);
var data = JArray.Parse(file.ReadToEnd());
using StreamReader file = new(foldersFilePath);
JArray data = JArray.Parse(file.ReadToEnd());
foreach (var pack in data)
foreach (JToken pack in data)
{
var folder = new CompendiumFolder((JObject)pack) { Parent = parent };
CompendiumFolder folder = new((JObject) pack) { Parent = parent };
Collection.Add(folder);
}
}
@@ -131,24 +131,24 @@ namespace Pf2eDataScrapper.Importers.Implementations.Pf2eFoundry
public IEnumerable<LoaderEnrty> Enrties()
{
foreach (var folder in Collection.FindAll(f => f.Path is not null))
foreach (CompendiumFolder folder in Collection.FindAll(f => f.Path is not null))
{
var dirPath = Path.Combine(Importer.Dir, folder.Path!);
string dirPath = Path.Combine(Importer.Dir, folder.Path!);
foreach (var filePath in Directory.GetFiles(dirPath, "*.json", SearchOption.AllDirectories))
foreach (string filePath in Directory.GetFiles(dirPath, "*.json", SearchOption.AllDirectories))
{
var file = new FileInfo(filePath);
FileInfo file = new(filePath);
if (file.Name == "_folders.json")
{
continue;
}
using var reader = new StreamReader(file.FullName);
var itemJson = JObject.Parse(reader.ReadToEnd());
using StreamReader reader = new(file.FullName);
JObject itemJson = JObject.Parse(reader.ReadToEnd());
var itemFolder = folder;
if (itemJson.TryGetValue("folder", out var itemJsonFolder))
CompendiumFolder? itemFolder = folder;
if (itemJson.TryGetValue("folder", out JToken? itemJsonFolder))
{
itemFolder = Collection.GetById(itemJsonFolder.ToString());
}
@@ -167,4 +167,3 @@ namespace Pf2eDataScrapper.Importers.Implementations.Pf2eFoundry
?? throw new Exception($"Expected to have `type` field in file {file.FullName}");
}
}
}
@@ -1,11 +1,11 @@
using Newtonsoft.Json.Linq;
using NpgsqlTypes;
using Pf2eDataScrapper.Model;
using Pf2eDataScrapper.Model.Common;
using Pf2eDataScrapper.Model.Enum;
using System.ComponentModel;
namespace Pf2eDataScrapper.Importers.Implementations.Pf2eFoundry.DataBuilder
{
namespace Pf2eDataScrapper.Importers.Implementations.Pf2eFoundry.DataBuilder;
internal abstract class AbstractProcessor(JObject input)
{
public readonly Pf2eTestContext Context = new();
@@ -14,7 +14,7 @@ namespace Pf2eDataScrapper.Importers.Implementations.Pf2eFoundry.DataBuilder
protected DataBuilderHelper Helper = DataBuilderHelper.Instance;
public Entity Entity { get; set; } = new Entity();
public Entity Entity { get; set; } = new();
public abstract EntityType Type { get; }
@@ -28,7 +28,6 @@ namespace Pf2eDataScrapper.Importers.Implementations.Pf2eFoundry.DataBuilder
Entity.Name = GetRequired<string>("name");
Entity.Img = GetRequired<string>("img");
Entity.Description = GetRequired<string>("system.description.value");
Entity.DescriptionGm = GetOptional<string>("system.description.gm");
Entity.IsTranslated = false;
Entity.IsChecked = false;
@@ -49,21 +48,16 @@ namespace Pf2eDataScrapper.Importers.Implementations.Pf2eFoundry.DataBuilder
public T GetRequired<T>(string path)
{
var token = Input.SelectToken(path) ?? throw new NullReferenceException($"'{path}' is null");
JToken token = Input.SelectToken(path) ?? throw new NullReferenceException($"'{path}' is null");
return token.ToObject<T>() ?? throw new NullReferenceException($"'{path}' is null");
}
public T? GetOptional<T>(string path)
{
var token = Input.SelectToken(path) ?? null;
JToken? token = Input.SelectToken(path) ?? null;
if (token is null)
{
return default!;
}
return token.ToObject<T>() ?? default;
return token is null ? default! : token.ToObject<T>() ?? default;
}
public T EnumRequired<T>(string path) where T : Enum
@@ -80,7 +74,7 @@ namespace Pf2eDataScrapper.Importers.Implementations.Pf2eFoundry.DataBuilder
return default;
}
foreach (var field in typeof(T).GetFields())
foreach (System.Reflection.FieldInfo field in typeof(T).GetFields())
{
if (Attribute.GetCustomAttribute(field, typeof(PgNameAttribute)) is PgNameAttribute attribute)
{
@@ -101,4 +95,3 @@ namespace Pf2eDataScrapper.Importers.Implementations.Pf2eFoundry.DataBuilder
return default;
}
}
}
@@ -1,7 +1,7 @@
using Slugify;
namespace Pf2eDataScrapper.Importers.Implementations.Pf2eFoundry.DataBuilder
{
namespace Pf2eDataScrapper.Importers.Implementations.Pf2eFoundry.DataBuilder;
internal class DataBuilderHelper
{
public Pf2eDatabase Database = Pf2eDatabase.Instance;
@@ -23,6 +23,5 @@ namespace Pf2eDataScrapper.Importers.Implementations.Pf2eFoundry.DataBuilder
public readonly static DataBuilderHelper Instance = new();
}
public static readonly DataBuilderHelper Instance = new();
}
@@ -3,8 +3,8 @@ using Newtonsoft.Json.Linq;
using Pf2eDataScrapper.Importers.Implementations.Pf2eFoundry.Compendium;
using System.Reflection;
namespace Pf2eDataScrapper.Importers.Implementations.Pf2eFoundry.DataBuilder
{
namespace Pf2eDataScrapper.Importers.Implementations.Pf2eFoundry.DataBuilder;
internal class PF2eFoundryDataBuilder
{
public Pf2eDatabase Database = Pf2eDatabase.Instance;
@@ -29,25 +29,20 @@ namespace Pf2eDataScrapper.Importers.Implementations.Pf2eFoundry.DataBuilder
public void PrepareProcessors()
{
foreach (var type in Assembly.GetExecutingAssembly().GetTypes())
foreach (Type type in Assembly.GetExecutingAssembly().GetTypes())
{
if (type.GetCustomAttributes(typeof(Pf2eTypeProcessor), true).Length <= 0)
{
continue;
}
var attribute = type.GetCustomAttribute<Pf2eTypeProcessor>();
Processors.Add(attribute!.Type, type.GetConstructor(new[] { typeof(JObject) })!);
Pf2eTypeProcessor? attribute = type.GetCustomAttribute<Pf2eTypeProcessor>();
Processors.Add(attribute!.Type, type.GetConstructor([typeof(JObject)])!);
}
}
public AbstractProcessor? GetProcessor(string type, JObject input)
{
if (Processors.TryGetValue(type, out var constructor))
{
return (AbstractProcessor)constructor.Invoke([input]);
}
return null;
return Processors.TryGetValue(type, out ConstructorInfo? constructor) ? (AbstractProcessor) constructor.Invoke([input]) : null;
}
public void TruncateTables()
@@ -63,14 +58,14 @@ namespace Pf2eDataScrapper.Importers.Implementations.Pf2eFoundry.DataBuilder
{
Console.WriteLine("Building data...");
var bar = new ProgressBar(Loader.ItemCount);
var i = 0;
ProgressBar bar = new(Loader.ItemCount);
int i = 0;
foreach (var entry in Loader.Enrties())
foreach (CompendiumLoader.LoaderEnrty entry in Loader.Enrties())
{
bar.Refresh(i++, $"{entry.Folder.Name}/{entry.File.Name}");
var processor = GetProcessor($"{entry.Folder.Type}.{entry.Type}", entry.Json);
AbstractProcessor? processor = GetProcessor($"{entry.Folder.Type}.{entry.Type}", entry.Json);
if (processor is null)
{
continue;
@@ -83,4 +78,3 @@ namespace Pf2eDataScrapper.Importers.Implementations.Pf2eFoundry.DataBuilder
bar.Refresh(Loader.ItemCount, "Done!");
}
}
}
@@ -1,5 +1,5 @@
namespace Pf2eDataScrapper.Importers.Implementations.Pf2eFoundry.DataBuilder
{
namespace Pf2eDataScrapper.Importers.Implementations.Pf2eFoundry.DataBuilder;
[AttributeUsage(AttributeTargets.Class)]
internal class Pf2eTypeProcessor(string type, int priority = 0) : Attribute
{
@@ -7,4 +7,3 @@
public readonly int Priority = priority;
}
}
@@ -2,8 +2,8 @@
using Pf2eDataScrapper.Model;
using Pf2eDataScrapper.Model.Enum;
namespace Pf2eDataScrapper.Importers.Implementations.Pf2eFoundry.DataBuilder.Processors
{
namespace Pf2eDataScrapper.Importers.Implementations.Pf2eFoundry.DataBuilder.Processors;
[Pf2eTypeProcessor("Item.condition")]
internal class ConditionProcessor(JObject input) : AbstractProcessor(input)
{
@@ -30,4 +30,3 @@ namespace Pf2eDataScrapper.Importers.Implementations.Pf2eFoundry.DataBuilder.Pro
Context.Conditions.Add(Object);
}
}
}
@@ -2,8 +2,8 @@
using Pf2eDataScrapper.Importers.Implementations.Pf2eFoundry.SchemaBuilder;
using Pf2eDataScrapper.Importers.Repositories;
namespace Pf2eDataScrapper.Importers.Implementations.Pf2eFoundry
{
namespace Pf2eDataScrapper.Importers.Implementations.Pf2eFoundry;
/// <summary>
/// Импортёр для модуля Pf2e FoundryVTT.
/// </summary>
@@ -47,7 +47,7 @@ namespace Pf2eDataScrapper.Importers.Implementations.Pf2eFoundry
/// </summary>
public override void Import()
{
var builder = new PF2eFoundryDataBuilder(Loader);
PF2eFoundryDataBuilder builder = new(Loader);
builder.TruncateTables();
builder.Build();
@@ -58,10 +58,9 @@ namespace Pf2eDataScrapper.Importers.Implementations.Pf2eFoundry
/// </summary>
public override void BuildSchemas()
{
var builder = new CompendiumSchemaBuilder(Loader);
CompendiumSchemaBuilder builder = new(Loader);
builder.BuildSchemas();
builder.Save(EnvSettings.OutputDir);
}
}
}
@@ -2,8 +2,8 @@
using Pf2eDataScrapper.Importers.Implementations.Pf2eFoundry.Compendium;
using Pf2eDataScrapper.SchemaBuilder;
namespace Pf2eDataScrapper.Importers.Implementations.Pf2eFoundry.SchemaBuilder
{
namespace Pf2eDataScrapper.Importers.Implementations.Pf2eFoundry.SchemaBuilder;
/// <summary>
/// Класс построения схемы компендиумов.
/// </summary>
@@ -75,16 +75,11 @@ namespace Pf2eDataScrapper.Importers.Implementations.Pf2eFoundry.SchemaBuilder
/// <returns></returns>
protected List<SchemaNode> GetItemSchemas(CompendiumFolder folder, string? itemType)
{
var key = folder.Type.ToString() + (itemType is not null ? $".{itemType}" : "");
string key = folder.Type.ToString() + (itemType is not null ? $".{itemType}" : "");
if (_equipmentTypes.Contains(itemType))
{
return [GetPackSchema(folder), GetTypeSchema(key), GetTypeSchema("Equipment")];
}
else
{
return [GetPackSchema(folder), GetTypeSchema(key)];
}
return _equipmentTypes.Contains(itemType)
? [GetPackSchema(folder), GetTypeSchema(key), GetTypeSchema("Equipment")]
: [GetPackSchema(folder), GetTypeSchema(key)];
}
/// <summary>
@@ -94,16 +89,16 @@ namespace Pf2eDataScrapper.Importers.Implementations.Pf2eFoundry.SchemaBuilder
{
Console.WriteLine("Building schemas...");
var bar = new ProgressBar(Loader.ItemCount);
var i = 0;
ProgressBar bar = new(Loader.ItemCount);
int i = 0;
foreach (var entry in Loader.Enrties())
foreach (CompendiumLoader.LoaderEnrty entry in Loader.Enrties())
{
bar.Refresh(i++, entry.Folder.Name);
var schemas = GetItemSchemas(entry.Folder, entry.Type);
List<SchemaNode> schemas = GetItemSchemas(entry.Folder, entry.Type);
foreach (var schema in schemas)
foreach (SchemaNode schema in schemas)
{
schema.AddValue(entry.Json);
}
@@ -120,8 +115,8 @@ namespace Pf2eDataScrapper.Importers.Implementations.Pf2eFoundry.SchemaBuilder
{
Console.WriteLine("Dumping schemas...");
var bar = new ProgressBar(PackSchemas.Count + TypeSchemas.Count);
var i = 0;
ProgressBar bar = new(PackSchemas.Count + TypeSchemas.Count);
int i = 0;
targetDir = Path.Combine(targetDir, Loader.Importer.Slug);
@@ -132,19 +127,19 @@ namespace Pf2eDataScrapper.Importers.Implementations.Pf2eFoundry.SchemaBuilder
Array.ForEach(Directory.GetFiles(@targetDir), File.Delete);
// Packs
var packsDir = Path.Combine(targetDir, "packs");
string packsDir = Path.Combine(targetDir, "packs");
Directory.CreateDirectory(packsDir);
foreach (var pair in PackSchemas)
foreach (KeyValuePair<CompendiumFolder, SchemaNode> pair in PackSchemas)
{
bar.Refresh(i++, pair.Key.Name);
SaveSchema(packsDir, pair.Key.Name, pair.Value);
}
// Types
var typesDir = Path.Combine(targetDir, "types");
string typesDir = Path.Combine(targetDir, "types");
Directory.CreateDirectory(typesDir);
foreach (var pair in TypeSchemas)
foreach (KeyValuePair<string, SchemaNode> pair in TypeSchemas)
{
bar.Refresh(i++, pair.Key);
SaveSchema(typesDir, pair.Key, pair.Value);
@@ -161,19 +156,19 @@ namespace Pf2eDataScrapper.Importers.Implementations.Pf2eFoundry.SchemaBuilder
/// <param name="schema">Сохраняемая схема.</param>
protected void SaveSchema(string dir, string name, SchemaNode schema)
{
var invalids = Path.GetInvalidFileNameChars();
var fileName = string.Join("_", schema.Path.Split(invalids, StringSplitOptions.RemoveEmptyEntries)).TrimEnd('.');
char[] invalids = Path.GetInvalidFileNameChars();
string fileName = string.Join("_", schema.Path.Split(invalids, StringSplitOptions.RemoveEmptyEntries)).TrimEnd('.');
var path = Path.Combine(dir, fileName + ".yaml");
string path = Path.Combine(dir, fileName + ".yaml");
var dirPath = Path.GetDirectoryName(path);
string? dirPath = Path.GetDirectoryName(path);
if (dirPath is not null && !Directory.Exists(dirPath))
{
Directory.CreateDirectory(dirPath);
}
using var writer = new StreamWriter(path);
using StreamWriter writer = new(path);
schema.Beautify();
@@ -189,4 +184,3 @@ namespace Pf2eDataScrapper.Importers.Implementations.Pf2eFoundry.SchemaBuilder
//writer.Write(schema.ToString());
}
}
}
@@ -2,8 +2,8 @@
using Pf2eDataScrapper.SchemaBuilder;
using Pf2eDataScrapper.SchemaBuilder.Types;
namespace Pf2eDataScrapper.Importers.Implementations.Pf2eFoundry.SchemaBuilder
{
namespace Pf2eDataScrapper.Importers.Implementations.Pf2eFoundry.SchemaBuilder;
internal class CompendiumSchemaTypeResolver : TypeResolver
{
public class FoundryRulesSchemaType(SchemaNode parent) : IgnoreSchemaType(parent)
@@ -46,17 +46,8 @@ namespace Pf2eDataScrapper.Importers.Implementations.Pf2eFoundry.SchemaBuilder
/// <returns>Новый тип для схемы данных. `null`, что бы использовать значение по умолчанию</returns>
protected override AbstractSchemaType? CustomResolve(SchemaNode node, JToken? value)
{
if (CheckPath(node, DictionaryPaths))
{
return new DictionarySchemaType(node);
}
if (node.Path.EndsWith(".rules"))
{
return new FoundryRulesSchemaType(node);
}
return null;
}
return CheckPath(node, DictionaryPaths)
? new DictionarySchemaType(node)
: node.Path.EndsWith(".rules") ? new FoundryRulesSchemaType(node) : (AbstractSchemaType?) null;
}
}
@@ -1,5 +1,5 @@
namespace Pf2eDataScrapper.Importers.Repositories
{
namespace Pf2eDataScrapper.Importers.Repositories;
internal abstract class AbstractImporterRepository(AbstractImporter importer)
{
public readonly AbstractImporter Importer = importer;
@@ -10,4 +10,3 @@
public abstract void Update();
}
}
@@ -1,8 +1,7 @@
using System.Data.Common;
using System.Diagnostics;
using System.Diagnostics;
namespace Pf2eDataScrapper.Importers.Repositories;
namespace Pf2eDataScrapper.Importers.Repositories
{
/// <summary>
/// Загрузчик данных из git-репозиториев
/// </summary>
@@ -16,12 +15,12 @@ namespace Pf2eDataScrapper.Importers.Repositories
/// </summary>
public override void Update()
{
var gitPath = Path.Combine(RepositoryDirPath, importer.Slug);
string gitPath = Path.Combine(RepositoryDirPath, importer.Slug);
if (Directory.Exists(gitPath))
{
// git pull
var process = new Process
Process process = new()
{
StartInfo = new ProcessStartInfo()
{
@@ -35,7 +34,7 @@ namespace Pf2eDataScrapper.Importers.Repositories
else
{
// git clone
var process = new Process
Process process = new()
{
StartInfo = new ProcessStartInfo()
{
@@ -49,4 +48,3 @@ namespace Pf2eDataScrapper.Importers.Repositories
}
}
}
}
-1
View File
@@ -1,5 +1,4 @@
using Pf2eDataScrapper.Model.Common;
using Pf2eDataScrapper.Model.Enum;
namespace Pf2eDataScrapper.Model;
-1
View File
@@ -1,5 +1,4 @@
using Pf2eDataScrapper.Model.Common;
using Pf2eDataScrapper.Model.Enum;
namespace Pf2eDataScrapper.Model;
-1
View File
@@ -1,5 +1,4 @@
using Pf2eDataScrapper.Model.Common;
using Pf2eDataScrapper.Model.Enum;
namespace Pf2eDataScrapper.Model;
@@ -1,5 +1,4 @@
using Pf2eDataScrapper.Model.Common;
using Pf2eDataScrapper.Model.Enum;
namespace Pf2eDataScrapper.Model;
-1
View File
@@ -1,5 +1,4 @@
using Pf2eDataScrapper.Model.Common;
using Pf2eDataScrapper.Model.Enum;
namespace Pf2eDataScrapper.Model;
-1
View File
@@ -1,5 +1,4 @@
using Pf2eDataScrapper.Model.Common;
using Pf2eDataScrapper.Model.Enum;
namespace Pf2eDataScrapper.Model;
+2 -3
View File
@@ -1,7 +1,7 @@
using NpgsqlTypes;
namespace Pf2eDataScrapper.Model.Enum
{
namespace Pf2eDataScrapper.Model.Enum;
public enum EntityRarity
{
[PgName("common")]
@@ -16,4 +16,3 @@ namespace Pf2eDataScrapper.Model.Enum
[PgName("unique")]
Unique
}
}
+2 -3
View File
@@ -1,7 +1,7 @@
using NpgsqlTypes;
namespace Pf2eDataScrapper.Model.Enum
{
namespace Pf2eDataScrapper.Model.Enum;
public enum TraitType
{
[PgName("regular")]
@@ -10,4 +10,3 @@ namespace Pf2eDataScrapper.Model.Enum
[PgName("tradition")]
Tradition
}
}
-1
View File
@@ -1,5 +1,4 @@
using Pf2eDataScrapper.Model.Common;
using Pf2eDataScrapper.Model.Enum;
namespace Pf2eDataScrapper.Model;
-1
View File
@@ -1,5 +1,4 @@
using Pf2eDataScrapper.Model.Common;
using Pf2eDataScrapper.Model.Enum;
namespace Pf2eDataScrapper.Model;
+2 -3
View File
@@ -1,7 +1,7 @@
using Pf2eDataScrapper.Model.Common;
namespace Pf2eDataScrapper.Model
{
namespace Pf2eDataScrapper.Model;
internal interface IEntity
{
public int Id { get; set; }
@@ -9,4 +9,3 @@ namespace Pf2eDataScrapper.Model
public Entity Entity { get; set; }
}
}
-1
View File
@@ -1,5 +1,4 @@
using Pf2eDataScrapper.Model.Common;
using Pf2eDataScrapper.Model.Enum;
namespace Pf2eDataScrapper.Model;
+3 -4
View File
@@ -1,8 +1,8 @@
using Npgsql;
using System.Text.RegularExpressions;
namespace Pf2eDataScrapper
{
namespace Pf2eDataScrapper;
public partial class Pf2eDatabase
{
public NpgsqlConnection Connection;
@@ -51,7 +51,7 @@ namespace Pf2eDataScrapper
public void ExecuteCommand(string sql)
{
var command = GetCommand(sql);
NpgsqlCommand command = GetCommand(sql);
command.ExecuteNonQuery();
}
@@ -120,4 +120,3 @@ namespace Pf2eDataScrapper
[GeneratedRegex(@"(?:^|;)User=(.*?)(?:$|;)")]
private static partial Regex UsernamePattern();
}
}
+4 -49
View File
@@ -1,9 +1,7 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using Pf2eDataScrapper.Model;
using Pf2eDataScrapper.Model.Common;
using Pf2eDataScrapper.Model.Enum;
using System.ComponentModel;
using System.Reflection;
namespace Pf2eDataScrapper;
@@ -103,8 +101,10 @@ public partial class Pf2eTestContext : DbContext
public virtual DbSet<Trait> Traits { get; set; }
#endregion
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
EnvSettings.Load();
optionsBuilder.UseNpgsql(EnvSettings.DatabaseConnection,
builder =>
{
@@ -115,50 +115,6 @@ public partial class Pf2eTestContext : DbContext
);
}
protected string FromEnum<T>(T enumValue) where T : Enum
{
var enumFieldName = enumValue.ToString();
var field = typeof(T).GetField(enumFieldName);
if (field is null)
{
return enumFieldName;
}
var attribute = field.GetCustomAttribute<DescriptionAttribute>();
return attribute is null ? enumFieldName : attribute.Description;
}
protected T ToEnum<T>(string enumValue) where T : Enum
{
var enumType = typeof(T);
foreach (var field in enumType.GetFields())
{
var attribute = field.GetCustomAttribute<DescriptionAttribute>();
if (attribute is null)
{
if (field.Name == enumValue)
{
return (T)field.GetValue(null)!;
}
}
else
{
if (attribute.Description == enumValue)
{
return (T)field.GetValue(null)!;
}
}
}
throw new ArgumentException($"No matching value found for {enumValue} in {enumType.Name}");
}
protected ValueConverter<T, string> GetEnumConverter<T>() where T : Enum
{
return new ValueConverter<T, string>(
v => FromEnum(v),
v => ToEnum<T>(v));
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.UseIdentityByDefaultColumns();
@@ -198,8 +154,7 @@ public partial class Pf2eTestContext : DbContext
entity.Property(e => e.Id).HasColumnName("id");
entity.Property(e => e.Description).HasColumnName("description");
entity.Property(e => e.DescriptionGm).HasColumnName("description_gm");
entity.Property(e => e.Type)
.HasColumnName("type");
entity.Property(e => e.Type).HasColumnName("type");
entity.Property(e => e.Img)
.HasMaxLength(150)
.HasColumnName("img");
+1 -2
View File
@@ -1,10 +1,9 @@
using Pf2eDataScrapper;
using Pf2eDataScrapper.Importers.Implementations.Aonprd;
using Pf2eDataScrapper.Importers.Implementations.Pf2eFoundry;
EnvSettings.Load();
var importer = new AonprdImporter();
AonprdImporter importer = new AonprdImporter();
importer.Prepare();
importer.BuildSchemas();
+15 -16
View File
@@ -1,8 +1,8 @@
using Newtonsoft.Json.Linq;
using Pf2eDataScrapper.SchemaBuilder.Types;
namespace Pf2eDataScrapper.SchemaBuilder
{
namespace Pf2eDataScrapper.SchemaBuilder;
/// <summary>
/// Элемент схемы данных.
/// </summary>
@@ -93,7 +93,7 @@ namespace Pf2eDataScrapper.SchemaBuilder
Values++;
foreach (var type in Types)
foreach (AbstractSchemaType type in Types)
{
if (type.IsCompatibleValue(value))
{
@@ -102,7 +102,7 @@ namespace Pf2eDataScrapper.SchemaBuilder
}
}
var newType = Resolver.Resolve(this, value);
AbstractSchemaType newType = Resolver.Resolve(this, value);
if (newType.IsCompatibleValue(value))
{
@@ -111,7 +111,7 @@ namespace Pf2eDataScrapper.SchemaBuilder
}
else
{
var fallbackType = Resolver.Resolve(this, value, true);
AbstractSchemaType fallbackType = Resolver.Resolve(this, value, true);
Types.Add(fallbackType);
fallbackType.AddValue(value);
}
@@ -128,7 +128,7 @@ namespace Pf2eDataScrapper.SchemaBuilder
return Types[0].GetTypeName();
}
var typeNames = Types.Select(t => t.GetTypeName()).ToList();
List<string> typeNames = [.. Types.Select(t => t.GetTypeName())];
return string.Join(" | ", typeNames);
}
@@ -139,7 +139,7 @@ namespace Pf2eDataScrapper.SchemaBuilder
/// <returns>Строковое представление элемента.</returns>
public override string ToString()
{
var result = new Dictionary<string, string>()
Dictionary<string, string> result = new()
{
["type"] = GetTypeName(),
["path"] = Path,
@@ -159,7 +159,7 @@ namespace Pf2eDataScrapper.SchemaBuilder
List<string> types = [];
foreach (var type in Types)
foreach (AbstractSchemaType type in Types)
{
types.Add(Indent(type?.ToString() ?? ""));
}
@@ -172,7 +172,7 @@ namespace Pf2eDataScrapper.SchemaBuilder
/// </summary>
public void Beautify()
{
foreach (var type in Types)
foreach (AbstractSchemaType type in Types)
{
type.Beautify();
}
@@ -207,11 +207,11 @@ namespace Pf2eDataScrapper.SchemaBuilder
/// <returns>Строка форматированных данных</returns>
public static string FormatYaml(params List<Dictionary<string, string>> input)
{
var result = "";
string result = "";
foreach (var kvp in MergeDictionaries(input))
foreach (KeyValuePair<string, string> kvp in MergeDictionaries(input))
{
var val = kvp.Value.Contains("\r\n") ? $"\r\n{Tab}{Indent(kvp.Value)}" : kvp.Value;
string val = kvp.Value.Contains("\r\n") ? $"\r\n{Tab}{Indent(kvp.Value)}" : kvp.Value;
result += $"{kvp.Key}: {val}\r\n";
}
@@ -221,11 +221,11 @@ namespace Pf2eDataScrapper.SchemaBuilder
public static Dictionary<string, string> MergeDictionaries(params List<Dictionary<string, string>> input)
{
var result = new Dictionary<string, string>();
Dictionary<string, string> result = [];
foreach (var item in input)
foreach (Dictionary<string, string> item in input)
{
foreach (var kvp in item)
foreach (KeyValuePair<string, string> kvp in item)
{
if (result.ContainsKey(kvp.Key))
{
@@ -241,4 +241,3 @@ namespace Pf2eDataScrapper.SchemaBuilder
return result;
}
}
}
+11 -25
View File
@@ -1,8 +1,8 @@
using Newtonsoft.Json.Linq;
using Pf2eDataScrapper.SchemaBuilder.Types;
namespace Pf2eDataScrapper.SchemaBuilder
{
namespace Pf2eDataScrapper.SchemaBuilder;
/// <summary>
/// Класс для определения типа поля в схеме данных.
/// </summary>
@@ -21,15 +21,10 @@ namespace Pf2eDataScrapper.SchemaBuilder
return path == node.Path;
}
var pathParts = path.Split('.');
var nodeParts = node.Path.Split('.');
string[] pathParts = path.Split('.');
string[] nodeParts = node.Path.Split('.');
if (pathParts.Length > nodeParts.Length)
{
return false;
}
return !pathParts.Where((t, i) => t != "*" && t != nodeParts[i]).Any();
return pathParts.Length > nodeParts.Length ? false : !pathParts.Where((t, i) => t != "*" && t != nodeParts[i]).Any();
}
/// <summary>
@@ -50,12 +45,7 @@ namespace Pf2eDataScrapper.SchemaBuilder
/// <returns>Типа значения.</returns>
public JTokenType TokenType(JToken? value)
{
if (value is null || value.Type is JTokenType.Null or JTokenType.Undefined or JTokenType.None)
{
return JTokenType.Null;
}
return value.Type;
return value is null || value.Type is JTokenType.Null or JTokenType.Undefined or JTokenType.None ? JTokenType.Null : value.Type;
}
/// <summary>
@@ -84,7 +74,7 @@ namespace Pf2eDataScrapper.SchemaBuilder
{
if (!disableResolve)
{
var customType = this.CustomResolve(node, value);
AbstractSchemaType? customType = this.CustomResolve(node, value);
if (customType is not null)
{
@@ -92,14 +82,11 @@ namespace Pf2eDataScrapper.SchemaBuilder
}
}
var type = TokenType(value);
JTokenType type = TokenType(value);
if (type == JTokenType.Null)
{
throw new ArgumentNullException(nameof(value));
}
return type switch
return type == JTokenType.Null
? throw new ArgumentNullException(nameof(value))
: type switch
{
JTokenType.Array => new ArraySchemaType(node),
JTokenType.Object => new ObjectSchemaType(node),
@@ -122,4 +109,3 @@ namespace Pf2eDataScrapper.SchemaBuilder
return this.Resolve(node, value, false);
}
}
}
@@ -1,7 +1,7 @@
using Newtonsoft.Json.Linq;
namespace Pf2eDataScrapper.SchemaBuilder.Types
{
namespace Pf2eDataScrapper.SchemaBuilder.Types;
/// <summary>
/// Абстрактный тип представоления данных в схеме.
/// </summary>
@@ -62,4 +62,3 @@ namespace Pf2eDataScrapper.SchemaBuilder.Types
return SchemaNode.FormatYaml(GetYamlData());
}
}
}
@@ -1,7 +1,7 @@
using Newtonsoft.Json.Linq;
namespace Pf2eDataScrapper.SchemaBuilder.Types
{
namespace Pf2eDataScrapper.SchemaBuilder.Types;
/// <summary>
/// Тип представоления массива в схеме.
/// </summary>
@@ -44,12 +44,12 @@ namespace Pf2eDataScrapper.SchemaBuilder.Types
throw new Exception("Value is not an array, did you forget to call IsCompatibleValue?");
}
var array = (JArray)value;
JArray array = (JArray) value;
MinCount = Math.Min(MinCount, array.Count);
MaxCount = Math.Max(MaxCount, array.Count);
foreach (var item in array)
foreach (JToken item in array)
{
ElementSchema.AddValue(item);
}
@@ -86,4 +86,3 @@ namespace Pf2eDataScrapper.SchemaBuilder.Types
};
}
}
}
@@ -1,7 +1,7 @@
using Newtonsoft.Json.Linq;
namespace Pf2eDataScrapper.SchemaBuilder.Types
{
namespace Pf2eDataScrapper.SchemaBuilder.Types;
/// <summary>
/// Тип представоления словаря в схеме.
/// </summary>
@@ -34,9 +34,9 @@ namespace Pf2eDataScrapper.SchemaBuilder.Types
/// <param name="value">Новое значение.</param>
public override void AddValue(JToken value)
{
var obj = (JObject)value;
JObject obj = (JObject) value;
foreach (var pair in obj)
foreach (KeyValuePair<string, JToken?> pair in obj)
{
KeyNode.AddValue(pair.Key);
ItemNode.AddValue(pair.Value);
@@ -74,4 +74,3 @@ namespace Pf2eDataScrapper.SchemaBuilder.Types
};
}
}
}
@@ -1,7 +1,7 @@
using Newtonsoft.Json.Linq;
namespace Pf2eDataScrapper.SchemaBuilder.Types
{
namespace Pf2eDataScrapper.SchemaBuilder.Types;
/// <summary>
/// Тип, указывающий, что данные игнорируются.
/// </summary>
@@ -52,4 +52,3 @@ namespace Pf2eDataScrapper.SchemaBuilder.Types
return [];
}
}
}
@@ -1,7 +1,7 @@
using Newtonsoft.Json.Linq;
namespace Pf2eDataScrapper.SchemaBuilder.Types
{
namespace Pf2eDataScrapper.SchemaBuilder.Types;
/// <summary>
/// Тип представоления объектов в схеме.
/// </summary>
@@ -29,9 +29,9 @@ namespace Pf2eDataScrapper.SchemaBuilder.Types
/// <param name="value">Новое значение.</param>
public override void AddValue(JToken value)
{
var obj = (JObject)value;
JObject obj = (JObject) value;
foreach (var field in Fields)
foreach (SchemaNode field in Fields)
{
if (!obj.ContainsKey(field.Name))
{
@@ -39,9 +39,9 @@ namespace Pf2eDataScrapper.SchemaBuilder.Types
}
}
foreach (var pair in obj)
foreach (KeyValuePair<string, JToken?> pair in obj)
{
var field = Fields.FirstOrDefault(f => f.Name == pair.Key);
SchemaNode? field = Fields.FirstOrDefault(f => f.Name == pair.Key);
if (field is null)
{
@@ -60,15 +60,10 @@ namespace Pf2eDataScrapper.SchemaBuilder.Types
{
Fields.Sort((a, b) =>
{
if (a.IsNullable && !b.IsNullable)
{
return 0;
}
return string.Compare(a.Name, b.Name, StringComparison.Ordinal);
return a.IsNullable && !b.IsNullable ? 0 : string.Compare(a.Name, b.Name, StringComparison.Ordinal);
});
foreach (var field in Fields)
foreach (SchemaNode field in Fields)
{
field.Beautify();
}
@@ -89,19 +84,12 @@ namespace Pf2eDataScrapper.SchemaBuilder.Types
/// <returns>Словарь данных</returns>
public Dictionary<string, string> GetSchemaYaml()
{
var result = new Dictionary<string, string>();
Dictionary<string, string> result = new();
foreach (var field in Fields)
foreach (SchemaNode field in Fields)
{
var s = field.Name;
if (field.IsNullable)
{
s = $"[{s}]";
}
else
{
s = $"'{s}'";
}
string s = field.Name;
s = field.IsNullable ? $"[{s}]" : $"'{s}'";
result.Add(s, field.ToString());
}
@@ -120,5 +108,3 @@ namespace Pf2eDataScrapper.SchemaBuilder.Types
};
}
}
}
@@ -1,7 +1,7 @@
using Newtonsoft.Json.Linq;
namespace Pf2eDataScrapper.SchemaBuilder.Types
{
namespace Pf2eDataScrapper.SchemaBuilder.Types;
/// <summary>
/// Тип представоления простых значений в схеме.
/// </summary>
@@ -47,11 +47,11 @@ namespace Pf2eDataScrapper.SchemaBuilder.Types
{
if (Type == JTokenType.String)
{
var newValues = new Dictionary<JToken, int>();
Dictionary<JToken, int> newValues = new();
foreach (var pair in Values)
foreach (KeyValuePair<JToken, int> pair in Values)
{
var newValue = "\"" + pair.Key.ToString().Trim().Replace("\"", "\\\"") + "\"";
string newValue = "\"" + pair.Key.ToString().Trim().Replace("\"", "\\\"") + "\"";
newValues.TryAdd(newValue, 0);
newValues[newValue] += pair.Value;
}
@@ -77,14 +77,14 @@ namespace Pf2eDataScrapper.SchemaBuilder.Types
/// <returns>Словарь данных</returns>
public override Dictionary<string, string> GetYamlData()
{
var values = new List<string>();
var count = Values.Count;
List<string> values = [];
int count = Values.Count;
var prefix = (count == 1) ? "" : "- ";
string prefix = (count == 1) ? "" : "- ";
if (count is <= SchemaNode.ElementLimit and > 0 && (Type != JTokenType.String || !Values.Any(s => s.ToString().Contains('\n'))))
{
foreach (var kvp in Values)
foreach (KeyValuePair<JToken, int> kvp in Values)
{
values.Add($"{prefix}{kvp.Key} (x{kvp.Value})");
}
@@ -99,4 +99,3 @@ namespace Pf2eDataScrapper.SchemaBuilder.Types
};
}
}
}