Немного работы

This commit is contained in:
2025-04-26 10:53:50 +04:00
parent 311a14a39d
commit 9d0683a23f
15 changed files with 2060 additions and 27 deletions
@@ -1,12 +1,14 @@
using System.ComponentModel;
using System.Collections.Generic;
using System.ComponentModel;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Pf2eModel;
using Pf2eModel.Model;
using Pf2eModel.Model.Base;
using Pf2eModel.Model.Common.I18n;
using Pf2eModel.Model.Structure;
namespace Pf2eViewer;
namespace Pf2eViewer.Converters;
internal class EntityConverter : JsonConverter
{
@@ -38,12 +40,12 @@ internal class EntityConverter : JsonConverter
public override void WriteJson(JsonWriter writer, object? value, JsonSerializer serializer)
{
Dictionary<string, object?> dictionary = DictionarySerialization(value!);
/*
if (value.GetType() != typeof(Entity))
{
dictionary.Remove("Entity");
}
*/
switch (value)
{
case Entity entity:
@@ -55,6 +57,9 @@ internal class EntityConverter : JsonConverter
break;
}
serializer.Serialize(writer, dictionary);
/*
writer.WriteStartObject();
foreach (KeyValuePair<string, object> keyValuePair in dictionary)
@@ -65,6 +70,7 @@ internal class EntityConverter : JsonConverter
}
writer.WriteEndObject();
*/
}
private void ProcessTrait(Dictionary<string, object?> dictionary, Trait trait)
@@ -83,6 +89,11 @@ internal class EntityConverter : JsonConverter
dictionary.Remove("Description");
}
private void ProcessSubclass(Dictionary<string, object?> dictionary, Subclass entity)
{
dictionary.Remove("Group");
}
public override bool CanRead => false;
public override object? ReadJson(
@@ -0,0 +1,41 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Pf2eModel.Model.Common.I18n;
namespace Pf2eViewer.Converters;
public class LocaleStringConverter : JsonConverter
{
public override bool CanConvert(Type objectType)
{
return objectType == typeof(LocaleString);
}
public override void WriteJson(JsonWriter writer, object? value, JsonSerializer serializer)
{
JObject result = new();
LocaleString str = (LocaleString)value!;
foreach (LocaleItem item in str)
{
result.Add(item.Locale.GetTypeString(), item.Value);
}
result.WriteTo(writer);
}
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."
);
}
}
File diff suppressed because it is too large Load Diff
+17 -9
View File
@@ -40,7 +40,7 @@ partial class MainViewForm
PrevPageButton = new Button();
FirstPageButton = new Button();
ResultSplitContainer = new SplitContainer();
EntityTreeView = new TreeView();
JsonWebView = new Microsoft.Web.WebView2.WinForms.WebView2();
DescriptionTextBox = new TextBox();
((System.ComponentModel.ISupportInitialize) RootSplitContainer).BeginInit();
RootSplitContainer.Panel1.SuspendLayout();
@@ -56,6 +56,7 @@ partial class MainViewForm
ResultSplitContainer.Panel1.SuspendLayout();
ResultSplitContainer.Panel2.SuspendLayout();
ResultSplitContainer.SuspendLayout();
((System.ComponentModel.ISupportInitialize) JsonWebView).BeginInit();
SuspendLayout();
//
// RootSplitContainer
@@ -212,7 +213,7 @@ partial class MainViewForm
//
// ResultSplitContainer.Panel1
//
ResultSplitContainer.Panel1.Controls.Add(EntityTreeView);
ResultSplitContainer.Panel1.Controls.Add(JsonWebView);
//
// ResultSplitContainer.Panel2
//
@@ -221,13 +222,19 @@ partial class MainViewForm
ResultSplitContainer.SplitterDistance = 467;
ResultSplitContainer.TabIndex = 1;
//
// EntityTreeView
// JsonWebView
//
EntityTreeView.Dock = DockStyle.Fill;
EntityTreeView.Location = new Point(0, 0);
EntityTreeView.Name = "EntityTreeView";
EntityTreeView.Size = new Size(467, 688);
EntityTreeView.TabIndex = 0;
JsonWebView.AllowExternalDrop = true;
JsonWebView.CreationProperties = null;
JsonWebView.DefaultBackgroundColor = Color.White;
JsonWebView.Dock = DockStyle.Fill;
JsonWebView.Location = new Point(0, 0);
JsonWebView.Name = "JsonWebView";
JsonWebView.Size = new Size(467, 688);
JsonWebView.TabIndex = 0;
JsonWebView.Visible = false;
JsonWebView.ZoomFactor = 1D;
JsonWebView.CoreWebView2InitializationCompleted += JsonWebView_CoreWebView2InitializationCompleted;
//
// DescriptionTextBox
//
@@ -264,6 +271,7 @@ partial class MainViewForm
ResultSplitContainer.Panel2.PerformLayout();
((System.ComponentModel.ISupportInitialize) ResultSplitContainer).EndInit();
ResultSplitContainer.ResumeLayout(false);
((System.ComponentModel.ISupportInitialize) JsonWebView).EndInit();
ResumeLayout(false);
}
@@ -273,7 +281,6 @@ partial class MainViewForm
private SplitContainer InnerSplitContainer;
private ListBox TypesListBox;
private DataGridView EntityGridView;
private TreeView EntityTreeView;
private SplitContainer ResultSplitContainer;
private TextBox DescriptionTextBox;
private Panel PaginationPagel;
@@ -283,4 +290,5 @@ partial class MainViewForm
private Button FirstPageButton;
private TextBox PageTextBox;
private TextBox PaginationLabel;
private Microsoft.Web.WebView2.WinForms.WebView2 JsonWebView;
}
+43 -4
View File
@@ -8,6 +8,7 @@ using Pf2eModel.Model;
using Pf2eModel.Model.Base;
using Pf2eModel.Model.Common.I18n;
using Pf2eModel.Model.Enum;
using Pf2eViewer.Converters;
namespace Pf2eViewer;
@@ -15,12 +16,22 @@ public partial class MainViewForm : Form
{
public Pf2eDbContext Context = new();
public string JsonViewerHtml = string.Empty;
private DarkModeCS dm = null;
public MainViewForm()
{
InitializeComponent();
dm = new DarkModeCS(this) { ColorMode = DarkModeCS.DisplayMode.SystemDefault };
Reset();
EntityGridView.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
using StreamReader reader = new("Resources/json-viewer.html");
JsonViewerHtml = reader.ReadToEnd();
}
public void Reset()
@@ -50,7 +61,7 @@ public partial class MainViewForm : Form
private void UpdateTreeView()
{
EntityTreeView.Nodes.Clear();
// EntityTreeView.Nodes.Clear();
EntityType type = (EntityType)TypesListBox.SelectedItem!;
Count = Context.Entities.Count(e => e.Type == type);
@@ -142,7 +153,7 @@ public partial class MainViewForm : Form
public void ViewEntity(int id)
{
EntityTreeView.Nodes.Clear();
// EntityTreeView.Nodes.Clear();
Entity? entity = Context
.Entities.Where(e => e.Id == id && e.Type == (EntityType)TypesListBox.SelectedItem!)
@@ -163,19 +174,47 @@ public partial class MainViewForm : Form
string json = SerializeEntity(entity);
JsonToTreeview(EntityTreeView, json, "Entity");
// JsonToTreeview(EntityTreeView, json, "Entity");
RenderJson(json);
DescriptionTextBox.Text = entity.Description.Replace("\\r", "").Replace("\\n", "\\r\\n");
}
public void RenderJson(string json)
{
if (JsonWebView.CoreWebView2 == null)
{
JsonWebView.EnsureCoreWebView2Async();
return;
}
string html = JsonViewerHtml.Replace("{JSON}", json);
JsonWebView.CoreWebView2.NavigateToString(html);
}
private void JsonWebView_CoreWebView2InitializationCompleted(
object sender,
Microsoft.Web.WebView2.Core.CoreWebView2InitializationCompletedEventArgs e
)
{
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 EntityConverter(),
new StringEnumConverter { NamingStrategy = new CamelCaseNamingStrategy() },
new EntityConverter(),
new LocaleStringConverter(),
],
ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
};
+6
View File
@@ -7,6 +7,7 @@
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Web.WebView2" Version="1.0.3179.45" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
</ItemGroup>
<ItemGroup>
@@ -15,4 +16,9 @@
<ItemGroup>
<Folder Include="Properties\" />
</ItemGroup>
<ItemGroup>
<None Update="Resources\json-viewer.html">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>
+24
View File
@@ -0,0 +1,24 @@
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>JSON Viewer</title>
<script src="https://pfau-software.de/json-viewer/dist/iife/index.js"></script>
<style>
body {
margin: 0;
background: black;
}
</style>
</head>
<body>
<andypf-json-viewer indent="4"
expanded="true"
theme="classic-dark"
expand-icon-type="square"
show-data-types="false"
show-toolbar="false"
show-copy="false"
show-size="true">
{JSON}
</andypf-json-viewer>
</body>
</html>