Больше обработчиков
This commit is contained in:
@@ -1,14 +1,15 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using Pf2eModel;
|
||||
using Pf2eModel.Model;
|
||||
using Pf2eModel.Model.Base;
|
||||
using Pf2eModel.Model.Structure;
|
||||
|
||||
namespace Pf2eViewer;
|
||||
internal class EntityConventor : JsonConverter
|
||||
internal class EntityConverter : JsonConverter
|
||||
{
|
||||
private Pf2eDbContext Context = new();
|
||||
private readonly Pf2eDbContext _context = new();
|
||||
|
||||
public override bool CanConvert(Type objectType)
|
||||
{
|
||||
@@ -34,48 +35,31 @@ internal class EntityConventor : JsonConverter
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
obj.WriteTo(writer);
|
||||
}
|
||||
|
||||
JObject ProcessEntity(JObject obj, Entity e)
|
||||
private JObject ProcessEntity(JObject obj, Entity e)
|
||||
{
|
||||
List<int> traitIds = e.Traits.Select(t => t.Id).ToList();
|
||||
List<Trait> traits = Context.Traits.Where(t => traitIds.Contains(t.Id)).Include(t => t.Entity).ToList();
|
||||
List<Trait> traits = _context.Traits.Where(t => traitIds.Contains(t.Id)).Include(t => t.Entity).ToList();
|
||||
|
||||
|
||||
foreach (JToken item in (JArray) obj.SelectToken("Traits"))
|
||||
foreach (JToken item in (JArray) obj.SelectToken("Traits")!)
|
||||
{
|
||||
JObject o = (JObject) item;
|
||||
int id = o.SelectToken("Id").ToObject<int>();
|
||||
int id = o.SelectToken("Id")!.ToObject<int>();
|
||||
|
||||
o.Add("Name", traits.FirstOrDefault(t => t.Id == id)!.Entity.Name);
|
||||
}
|
||||
|
||||
obj.Remove("Description");
|
||||
|
||||
|
||||
|
||||
|
||||
return obj;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public override bool CanRead => false;
|
||||
|
||||
public override object? ReadJson(JsonReader reader, Type objectType, object? existingValue, JsonSerializer serializer)
|
||||
{
|
||||
throw new NotImplementedException("Unnecessary because CanRead is false. The type will skip the converter.");
|
||||
}
|
||||
|
||||
|
||||
private abstract class Conventer<T>(JsonWriter writer, T value, JsonSerializer serializer, Pf2eDbContext context)
|
||||
{
|
||||
public JsonWriter Writer { get; } = writer;
|
||||
public T Value { get; } = value;
|
||||
public JsonSerializer Serializer { get; } = serializer;
|
||||
public Pf2eDbContext Context { get; } = context;
|
||||
|
||||
public abstract JObject Process();
|
||||
}
|
||||
|
||||
}
|
||||
Generated
+10
-11
@@ -65,8 +65,8 @@ partial class MainViewForm
|
||||
//
|
||||
RootSplitContainer.Panel2.Controls.Add(ResultSplitContainer);
|
||||
RootSplitContainer.Panel2MinSize = 400;
|
||||
RootSplitContainer.Size = new Size(811, 688);
|
||||
RootSplitContainer.SplitterDistance = 324;
|
||||
RootSplitContainer.Size = new Size(1181, 688);
|
||||
RootSplitContainer.SplitterDistance = 200;
|
||||
RootSplitContainer.TabIndex = 0;
|
||||
//
|
||||
// InnerSplitContainer
|
||||
@@ -83,7 +83,7 @@ partial class MainViewForm
|
||||
// InnerSplitContainer.Panel2
|
||||
//
|
||||
InnerSplitContainer.Panel2.Controls.Add(EntityGridView);
|
||||
InnerSplitContainer.Size = new Size(324, 688);
|
||||
InnerSplitContainer.Size = new Size(200, 688);
|
||||
InnerSplitContainer.SplitterDistance = 274;
|
||||
InnerSplitContainer.TabIndex = 1;
|
||||
//
|
||||
@@ -94,7 +94,7 @@ partial class MainViewForm
|
||||
TypesListBox.ItemHeight = 15;
|
||||
TypesListBox.Location = new Point(0, 0);
|
||||
TypesListBox.Name = "TypesListBox";
|
||||
TypesListBox.Size = new Size(324, 274);
|
||||
TypesListBox.Size = new Size(200, 274);
|
||||
TypesListBox.TabIndex = 0;
|
||||
TypesListBox.SelectedIndexChanged += TypesListBox_SelectedIndexChanged;
|
||||
//
|
||||
@@ -112,7 +112,7 @@ partial class MainViewForm
|
||||
EntityGridView.RowHeadersVisible = false;
|
||||
EntityGridView.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.AutoSizeToDisplayedHeaders;
|
||||
EntityGridView.ShowEditingIcon = false;
|
||||
EntityGridView.Size = new Size(324, 410);
|
||||
EntityGridView.Size = new Size(200, 410);
|
||||
EntityGridView.TabIndex = 0;
|
||||
EntityGridView.RowStateChanged += EntityGridView_RowStateChanged;
|
||||
//
|
||||
@@ -121,7 +121,6 @@ partial class MainViewForm
|
||||
ResultSplitContainer.Dock = DockStyle.Fill;
|
||||
ResultSplitContainer.Location = new Point(0, 0);
|
||||
ResultSplitContainer.Name = "ResultSplitContainer";
|
||||
ResultSplitContainer.Orientation = Orientation.Horizontal;
|
||||
//
|
||||
// ResultSplitContainer.Panel1
|
||||
//
|
||||
@@ -130,8 +129,8 @@ partial class MainViewForm
|
||||
// ResultSplitContainer.Panel2
|
||||
//
|
||||
ResultSplitContainer.Panel2.Controls.Add(DescriptionTextBox);
|
||||
ResultSplitContainer.Size = new Size(483, 688);
|
||||
ResultSplitContainer.SplitterDistance = 269;
|
||||
ResultSplitContainer.Size = new Size(977, 688);
|
||||
ResultSplitContainer.SplitterDistance = 495;
|
||||
ResultSplitContainer.TabIndex = 1;
|
||||
//
|
||||
// EntityTreeView
|
||||
@@ -139,7 +138,7 @@ partial class MainViewForm
|
||||
EntityTreeView.Dock = DockStyle.Fill;
|
||||
EntityTreeView.Location = new Point(0, 0);
|
||||
EntityTreeView.Name = "EntityTreeView";
|
||||
EntityTreeView.Size = new Size(483, 269);
|
||||
EntityTreeView.Size = new Size(495, 688);
|
||||
EntityTreeView.TabIndex = 0;
|
||||
//
|
||||
// DescriptionTextBox
|
||||
@@ -150,14 +149,14 @@ partial class MainViewForm
|
||||
DescriptionTextBox.Name = "DescriptionTextBox";
|
||||
DescriptionTextBox.ReadOnly = true;
|
||||
DescriptionTextBox.ScrollBars = ScrollBars.Vertical;
|
||||
DescriptionTextBox.Size = new Size(483, 415);
|
||||
DescriptionTextBox.Size = new Size(478, 688);
|
||||
DescriptionTextBox.TabIndex = 0;
|
||||
//
|
||||
// MainViewForm
|
||||
//
|
||||
AutoScaleDimensions = new SizeF(7F, 15F);
|
||||
AutoScaleMode = AutoScaleMode.Font;
|
||||
ClientSize = new Size(811, 688);
|
||||
ClientSize = new Size(1181, 688);
|
||||
Controls.Add(RootSplitContainer);
|
||||
Name = "MainViewForm";
|
||||
Text = "DataBaseViewer";
|
||||
|
||||
+13
-20
@@ -29,7 +29,7 @@ public partial class MainViewForm : Form
|
||||
TypesListBox.SelectedIndex = 0;
|
||||
}
|
||||
|
||||
private void TypesListBox_SelectedIndexChanged(object sender, EventArgs e)
|
||||
private void TypesListBox_SelectedIndexChanged(object sender, EventArgs _)
|
||||
{
|
||||
if (TypesListBox.SelectedItem is null)
|
||||
{
|
||||
@@ -39,7 +39,10 @@ public partial class MainViewForm : Form
|
||||
EntityGridView.DataSource = Context.Entities
|
||||
.Where(e => e.Type == (EntityType) TypesListBox.SelectedItem)
|
||||
.OrderBy(e => e.Id)
|
||||
.Include(e => e.Sources).Include(e => e.Aliases).Include(e => e.Keywords).Include(e => e.Externals)
|
||||
.Include(e => e.Sources)
|
||||
.Include(e => e.Aliases)
|
||||
.Include(e => e.Keywords)
|
||||
.Include(e => e.Externals)
|
||||
.Include(e => e.Traits)
|
||||
.Select(e => new EntityGridRow(e))
|
||||
.ToList();
|
||||
@@ -49,7 +52,7 @@ public partial class MainViewForm : Form
|
||||
private void EntityGridView_RowStateChanged(object sender, DataGridViewRowStateChangedEventArgs e)
|
||||
{
|
||||
int rowsCount = EntityGridView.SelectedRows.Count;
|
||||
if (rowsCount == 0 || rowsCount > 1)
|
||||
if (rowsCount is 0 or > 1)
|
||||
{
|
||||
return;
|
||||
}
|
||||
@@ -62,23 +65,22 @@ public partial class MainViewForm : Form
|
||||
|
||||
JsonToTreeview(EntityTreeView, json, "Entity");
|
||||
|
||||
DescriptionTextBox.Text = data.GetEntity().Description;
|
||||
DescriptionTextBox.Text = data.GetEntity().Description.Replace("\\r", "").Replace("\\n", "\\r\\n");
|
||||
}
|
||||
|
||||
public string SerializeEntity(Entity entity)
|
||||
{
|
||||
JsonSerializerSettings settings = new JsonSerializerSettings()
|
||||
JsonSerializerSettings settings = new()
|
||||
{
|
||||
Converters = [new EntityConventor()]
|
||||
// ContractResolver = new CamelCasePropertyNamesContractResolver(),
|
||||
Converters = [new EntityConverter()]
|
||||
};
|
||||
|
||||
string result = JsonConvert.SerializeObject(entity, settings);
|
||||
|
||||
Type type = EntityOfTypeAttribute.Resolve(entity);
|
||||
string subReuslt = JsonConvert.SerializeObject(Context.Find(type, entity.Id), settings);
|
||||
string subResult = JsonConvert.SerializeObject(Context.Find(type, entity.Id), settings);
|
||||
|
||||
return result[..^1] + $",\"SubEntity\":{subReuslt}}}";
|
||||
return result[..^1] + $",\"SubEntity\":{subResult}}}";
|
||||
}
|
||||
|
||||
|
||||
@@ -114,10 +116,7 @@ public partial class MainViewForm : Form
|
||||
case "JArray":
|
||||
JArray jArray = (JArray) element;
|
||||
int i = 0;
|
||||
foreach (JToken oJtok in jArray)
|
||||
{
|
||||
subNodes.Add(Element2TreeNode(oJtok, string.Format("[{0}]", i++)));
|
||||
}
|
||||
subNodes.AddRange(jArray.Select(token => Element2TreeNode(token, $"[{i++}]")));
|
||||
|
||||
if (i == 0)
|
||||
{
|
||||
@@ -147,17 +146,11 @@ public partial class MainViewForm : Form
|
||||
return treeNode;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
private class EntityGridRow(Entity e)
|
||||
{
|
||||
public int Id { get; set; } = e.Id;
|
||||
public string Name { get; set; } = e.Name;
|
||||
//public string Slug { get; set; } = e.Slug;
|
||||
//public EntityRarity Rarity { get; set; } = e.Rarity;
|
||||
public EntityRarity Rarity { get; set; } = e.Rarity;
|
||||
|
||||
private readonly Entity _e = e;
|
||||
public Entity GetEntity() => _e;
|
||||
|
||||
Reference in New Issue
Block a user