Большой рефактор
This commit is contained in:
+79
-226
@@ -1,7 +1,7 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System.Windows.Forms;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Converters;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using Newtonsoft.Json.Serialization;
|
||||
using Pf2eModel;
|
||||
using Pf2eModel.Model;
|
||||
@@ -9,6 +9,7 @@ using Pf2eModel.Model.Base;
|
||||
using Pf2eModel.Model.Common.I18n;
|
||||
using Pf2eModel.Model.Enum;
|
||||
using Pf2eViewer.Converters;
|
||||
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
|
||||
|
||||
namespace Pf2eViewer;
|
||||
|
||||
@@ -16,15 +17,15 @@ public partial class MainViewForm : Form
|
||||
{
|
||||
public Pf2eDbContext Context = new();
|
||||
|
||||
public string JsonViewerHtml = string.Empty;
|
||||
public readonly string JsonViewerHtml;
|
||||
|
||||
private DarkModeCS dm = null;
|
||||
public readonly DatabaseViewer Viewer = new();
|
||||
|
||||
public MainViewForm()
|
||||
{
|
||||
InitializeComponent();
|
||||
|
||||
dm = new DarkModeCS(this) { ColorMode = DarkModeCS.DisplayMode.SystemDefault };
|
||||
_ = new DarkModeCS(this) { ColorMode = DarkModeCS.DisplayMode.SystemDefault };
|
||||
|
||||
Reset();
|
||||
|
||||
@@ -32,6 +33,10 @@ public partial class MainViewForm : Form
|
||||
|
||||
using StreamReader reader = new("Resources/json-viewer.html");
|
||||
JsonViewerHtml = reader.ReadToEnd();
|
||||
|
||||
Viewer.ViewUpdateStart += Viewer_ViewUpdateStart;
|
||||
Viewer.ViewUpdate += Viewer_ViewUpdate;
|
||||
Viewer.ViewSelect += Viewer_ViewSelect;
|
||||
}
|
||||
|
||||
public void Reset()
|
||||
@@ -43,53 +48,63 @@ public partial class MainViewForm : Form
|
||||
TypesListBox.SelectedIndex = 0;
|
||||
}
|
||||
|
||||
public const int PerPage = 30;
|
||||
|
||||
public int Count;
|
||||
|
||||
public int Page
|
||||
private void ToggleFreeze(bool state)
|
||||
{
|
||||
get => Convert.ToInt32(PageTextBox.Text);
|
||||
set
|
||||
{
|
||||
PageTextBox.Text = value.ToString();
|
||||
UpdateTreeView();
|
||||
}
|
||||
EntityGridView.Visible = state;
|
||||
JsonWebView.Visible = state;
|
||||
|
||||
FirstPageButton.Enabled = state;
|
||||
PrevPageButton.Enabled = state;
|
||||
NextPageButton.Enabled = state;
|
||||
LastPageButton.Enabled = state;
|
||||
|
||||
PageTextBox.Enabled = state;
|
||||
}
|
||||
|
||||
public int MaxPage => Convert.ToInt32(Math.Ceiling(1.0 * Count / PerPage));
|
||||
// Viewer events
|
||||
|
||||
private void UpdateTreeView()
|
||||
private void Viewer_ViewUpdateStart()
|
||||
{
|
||||
// EntityTreeView.Nodes.Clear();
|
||||
EntityType type = (EntityType)TypesListBox.SelectedItem!;
|
||||
|
||||
Count = Context.Entities.Count(e => e.Type == type);
|
||||
|
||||
if (Page < 1 || Page > MaxPage)
|
||||
{
|
||||
Page = Math.Min(MaxPage, Math.Max(1, Page));
|
||||
return;
|
||||
}
|
||||
|
||||
EntityGridView.DataSource = Context
|
||||
.Entities.Where(e => e.Type == (EntityType)TypesListBox.SelectedItem)
|
||||
.OrderBy(e => e.Id)
|
||||
.Select(e => new EntityGridRow(e))
|
||||
.Skip((Page - 1) * PerPage)
|
||||
.Take(PerPage)
|
||||
.ToList();
|
||||
|
||||
EntityGridView.Rows[0].Selected = true;
|
||||
|
||||
PaginationLabel.Text = $"Всего: {Count} объектов, {MaxPage} страниц";
|
||||
|
||||
FirstPageButton.Enabled = Page > 1;
|
||||
PrevPageButton.Enabled = Page > 1;
|
||||
NextPageButton.Enabled = Page < MaxPage;
|
||||
LastPageButton.Enabled = Page < MaxPage;
|
||||
ToggleFreeze(false);
|
||||
}
|
||||
|
||||
private void Viewer_ViewUpdate()
|
||||
{
|
||||
EntityGridView.DataSource = Viewer.Entities.Select(e => new EntityGridRow(e));
|
||||
|
||||
PaginationLabel.Text = $@"Всего: {Viewer.Count} объектов, {Viewer.MaxPage} страниц";
|
||||
|
||||
FirstPageButton.Enabled = Viewer.Page > 1;
|
||||
PrevPageButton.Enabled = Viewer.Page > 1;
|
||||
NextPageButton.Enabled = Viewer.Page < Viewer.MaxPage;
|
||||
LastPageButton.Enabled = Viewer.Page < Viewer.MaxPage;
|
||||
|
||||
ToggleFreeze(true);
|
||||
}
|
||||
|
||||
private void Viewer_ViewSelect(Entity? entity, IEntity? subEntity)
|
||||
{
|
||||
JsonSerializerSettings settings = new()
|
||||
{
|
||||
Converters =
|
||||
[
|
||||
new StringEnumConverter { NamingStrategy = new CamelCaseNamingStrategy() },
|
||||
new EntityConverter(),
|
||||
new LocaleStringConverter(),
|
||||
],
|
||||
ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
|
||||
};
|
||||
|
||||
string result = JsonConvert.SerializeObject(entity, settings);
|
||||
string subResult = JsonConvert.SerializeObject(subEntity, settings);
|
||||
|
||||
string json = result[..^1] + $",\"SubEntity\":{subResult}}}";
|
||||
|
||||
RenderJson(json);
|
||||
}
|
||||
|
||||
// Form events
|
||||
|
||||
private void TypesListBox_SelectedIndexChanged(object sender, EventArgs _)
|
||||
{
|
||||
if (TypesListBox.SelectedItem is null)
|
||||
@@ -97,27 +112,27 @@ public partial class MainViewForm : Form
|
||||
return;
|
||||
}
|
||||
|
||||
Page = 1;
|
||||
Viewer.Page = 1;
|
||||
}
|
||||
|
||||
private void FirstPageButton_Click(object sender, EventArgs e)
|
||||
{
|
||||
Page = 1;
|
||||
Viewer.Page = 1;
|
||||
}
|
||||
|
||||
private void PrevPageButton_Click(object sender, EventArgs e)
|
||||
{
|
||||
Page--;
|
||||
Viewer.Page--;
|
||||
}
|
||||
|
||||
private void NextPageButton_Click(object sender, EventArgs e)
|
||||
{
|
||||
Page++;
|
||||
Viewer.Page++;
|
||||
}
|
||||
|
||||
private void LastPageButton_Click(object sender, EventArgs e)
|
||||
{
|
||||
Page = MaxPage;
|
||||
Viewer.Page = Viewer.MaxPage;
|
||||
}
|
||||
|
||||
private void PageTextBox_KeyPress(object sender, KeyPressEventArgs e)
|
||||
@@ -129,7 +144,7 @@ public partial class MainViewForm : Form
|
||||
|
||||
if (e.KeyChar == (char)Keys.Return)
|
||||
{
|
||||
UpdateTreeView();
|
||||
Viewer.Page = Convert.ToInt32(PageTextBox.Text);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -148,37 +163,7 @@ public partial class MainViewForm : Form
|
||||
|
||||
EntityGridRow data = (EntityGridRow)row.DataBoundItem;
|
||||
|
||||
ViewEntity(data.Id);
|
||||
}
|
||||
|
||||
public void ViewEntity(int id)
|
||||
{
|
||||
// EntityTreeView.Nodes.Clear();
|
||||
|
||||
Entity? entity = Context
|
||||
.Entities.Where(e => e.Id == id && e.Type == (EntityType)TypesListBox.SelectedItem!)
|
||||
.Include(e => e.Sources)
|
||||
.ThenInclude(s => s.Source)
|
||||
.Include(e => e.Keywords)
|
||||
.Include(e => e.Externals)
|
||||
.ThenInclude(x => x.Target)
|
||||
.Include(e => e.Traits)
|
||||
.ThenInclude(t => t.Entity)
|
||||
.FirstOrDefault();
|
||||
|
||||
if (entity == null)
|
||||
{
|
||||
DescriptionTextBox.Text = "Not found";
|
||||
return;
|
||||
}
|
||||
|
||||
string json = SerializeEntity(entity);
|
||||
|
||||
// JsonToTreeview(EntityTreeView, json, "Entity");
|
||||
|
||||
RenderJson(json);
|
||||
|
||||
DescriptionTextBox.Text = entity.Description.Replace("\\r", "").Replace("\\n", "\\r\\n");
|
||||
Viewer.Select(data.GetEntity());
|
||||
}
|
||||
|
||||
public void RenderJson(string json)
|
||||
@@ -199,149 +184,17 @@ public partial class MainViewForm : Form
|
||||
)
|
||||
{
|
||||
JsonWebView.Visible = true;
|
||||
DataGridViewRow row = EntityGridView.SelectedRows[0];
|
||||
|
||||
EntityGridRow data = (EntityGridRow)row.DataBoundItem;
|
||||
|
||||
ViewEntity(data.Id);
|
||||
}
|
||||
|
||||
public string SerializeEntity(Entity entity)
|
||||
{
|
||||
JsonSerializerSettings settings = new()
|
||||
{
|
||||
Converters =
|
||||
[
|
||||
new StringEnumConverter { NamingStrategy = new CamelCaseNamingStrategy() },
|
||||
new EntityConverter(),
|
||||
new LocaleStringConverter(),
|
||||
],
|
||||
ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
|
||||
};
|
||||
|
||||
string result = JsonConvert.SerializeObject(entity, settings);
|
||||
|
||||
Type type = EntityOfTypeAttribute.Resolve(entity);
|
||||
string subResult = JsonConvert.SerializeObject(ResolveSubEntity(entity), settings);
|
||||
|
||||
return result[..^1] + $",\"SubEntity\":{subResult}}}";
|
||||
}
|
||||
|
||||
public IEntity? ResolveSubEntity(Entity entity)
|
||||
{
|
||||
switch (entity.Type)
|
||||
{
|
||||
case EntityType.Ancestry:
|
||||
return Context
|
||||
.Ancestries.Include(a => a.AncestryBoosts)
|
||||
.FirstOrDefault(e => e.Id == entity.Id);
|
||||
|
||||
case EntityType.Archetype:
|
||||
return Context
|
||||
.Archetypes.Include(a => a.DedicationFeat)
|
||||
.ThenInclude(f => f.Entity)
|
||||
.Include(a => a.Feats)
|
||||
.ThenInclude(f => f.Entity)
|
||||
.FirstOrDefault(a => a.Id == entity.Id);
|
||||
|
||||
case EntityType.Background:
|
||||
return Context
|
||||
.Backgrounds.Include(b => b.BackgroundBoosts)
|
||||
.Include(b => b.BackgroundSkills)
|
||||
.ThenInclude(t => t.Training)
|
||||
.Include(b => b.BackgroundItems)
|
||||
.FirstOrDefault(e => e.Id == entity.Id);
|
||||
|
||||
case EntityType.Class:
|
||||
return Context
|
||||
.Classes.Include(c => c.ClassKeyAbilities)
|
||||
.Include(c => c.ClassTrainings)
|
||||
.ThenInclude(t => t.Training)
|
||||
.Include(c => c.ClassProgressions)
|
||||
.FirstOrDefault(c => c.Id == entity.Id);
|
||||
|
||||
case EntityType.Feat:
|
||||
return Context
|
||||
.Feats.Include(f => f.Archetypes)
|
||||
.ThenInclude(a => a.Entity)
|
||||
.FirstOrDefault(f => f.Id == entity.Id);
|
||||
|
||||
default:
|
||||
Type type = EntityOfTypeAttribute.Resolve(entity);
|
||||
return (IEntity?)Context.Find(type, entity.Id);
|
||||
}
|
||||
}
|
||||
|
||||
public static void JsonToTreeview(TreeView treeView, string jsonString, string rootName)
|
||||
{
|
||||
object json = jsonString.StartsWith('[')
|
||||
? JArray.Parse(jsonString)
|
||||
: JObject.Parse(jsonString);
|
||||
|
||||
treeView.Nodes.Clear();
|
||||
foreach (TreeNode node in Element2TreeNode(json, rootName).Nodes)
|
||||
{
|
||||
treeView.Nodes.Add(node);
|
||||
}
|
||||
treeView.ExpandAll();
|
||||
}
|
||||
|
||||
private static TreeNode Element2TreeNode(object element, string text = "")
|
||||
{
|
||||
List<TreeNode> subNodes = [];
|
||||
|
||||
switch (element.GetType().Name)
|
||||
{
|
||||
case "JObject":
|
||||
JObject jObject = (JObject)element;
|
||||
foreach (KeyValuePair<string, JToken> token in jObject)
|
||||
{
|
||||
subNodes.Add(Element2TreeNode(token.Value, token.Key));
|
||||
}
|
||||
break;
|
||||
|
||||
case "JArray":
|
||||
JArray jArray = (JArray)element;
|
||||
int i = 0;
|
||||
subNodes.AddRange(jArray.Select(token => Element2TreeNode(token, $"[{i++}]")));
|
||||
|
||||
if (i == 0)
|
||||
{
|
||||
text += ": []";
|
||||
}
|
||||
break;
|
||||
|
||||
case "JValue":
|
||||
JValue jValue = (JValue)element;
|
||||
text += $": ({jValue.Type}) {jValue}";
|
||||
break;
|
||||
|
||||
default:
|
||||
throw new Exception(
|
||||
"clsJSON2Treeview can't interpret object:" + element.GetType().Name
|
||||
);
|
||||
}
|
||||
|
||||
TreeNode treeNode = new(text);
|
||||
|
||||
if (subNodes.Count > 0)
|
||||
{
|
||||
treeNode.Nodes.AddRange(
|
||||
subNodes.OrderBy(node => node.Nodes.Count > 0 ? 1 : 0).ToArray()
|
||||
);
|
||||
}
|
||||
|
||||
return treeNode;
|
||||
}
|
||||
|
||||
private class EntityGridRow(Entity e)
|
||||
{
|
||||
public int Id { get; set; } = e.Id;
|
||||
public string Name { get; set; } = e.Name[LocaleType.English];
|
||||
public EntityRarity Rarity { get; set; } = e.Rarity;
|
||||
|
||||
private readonly Entity _e = e;
|
||||
|
||||
public Entity GetEntity() => _e;
|
||||
Viewer_ViewSelect(Viewer.LastEntity, Viewer.LastSubEntity);
|
||||
}
|
||||
}
|
||||
|
||||
internal class EntityGridRow(Entity e)
|
||||
{
|
||||
public int Id { get; set; } = e.Id;
|
||||
public string Name { get; set; } = e.Name[LocaleType.English];
|
||||
public EntityRarity Rarity { get; set; } = e.Rarity;
|
||||
|
||||
private readonly Entity _e = e;
|
||||
|
||||
public Entity GetEntity() => _e;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user