103 lines
2.6 KiB
C#
103 lines
2.6 KiB
C#
using System.Diagnostics.CodeAnalysis;
|
|
using Newtonsoft.Json;
|
|
using Newtonsoft.Json.Converters;
|
|
using Newtonsoft.Json.Serialization;
|
|
using Pf2eModel;
|
|
using Pf2eModel.Model;
|
|
using Pf2eModel.Model.Base;
|
|
using Pf2eModel.Model.Common.I18n;
|
|
using Pf2eModel.Model.Enum;
|
|
using Pf2eViewer.Converters;
|
|
|
|
namespace Pf2eViewer;
|
|
|
|
public partial class MainViewForm : Form
|
|
{
|
|
public Pf2eDbContext Context = new();
|
|
|
|
public readonly string JsonViewerHtml;
|
|
|
|
[AllowNull]
|
|
public readonly EntityView Viewer = null;
|
|
|
|
public MainViewForm()
|
|
{
|
|
InitializeComponent();
|
|
|
|
using StreamReader reader = new("Resources/json-viewer.html");
|
|
JsonViewerHtml = reader.ReadToEnd();
|
|
|
|
DatabaseViewer.EntityChange += DatabaseViewer_EntityChange;
|
|
|
|
Reset();
|
|
}
|
|
|
|
public void Reset()
|
|
{
|
|
Context.Dispose();
|
|
Context = new();
|
|
|
|
TypesListBox.DataSource = Context
|
|
.Entities.Select(e => e.Type)
|
|
.Distinct()
|
|
.OrderBy(t => t.ToString())
|
|
.ToList();
|
|
TypesListBox.SelectedIndex = 0;
|
|
}
|
|
|
|
private void ToggleFreeze(bool state)
|
|
{
|
|
// EntityGridView.Visible = state;
|
|
// JsonWebView.Visible = state;
|
|
}
|
|
|
|
private void DatabaseViewer_EntityChange(EntityView newEntity)
|
|
{
|
|
JsonSerializerSettings settings = new()
|
|
{
|
|
Converters =
|
|
[
|
|
new StringEnumConverter { NamingStrategy = new CamelCaseNamingStrategy() },
|
|
new EntityConverter(),
|
|
new LocaleStringConverter(),
|
|
],
|
|
ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
|
|
};
|
|
|
|
string result = JsonConvert.SerializeObject(newEntity.Entity, settings);
|
|
string subResult = JsonConvert.SerializeObject(newEntity.SubEntity, settings);
|
|
|
|
string json = result[..^1] + $",\"SubEntity\":{subResult}}}";
|
|
|
|
EntityTreeViewerControl.EntityView = newEntity;
|
|
// RenderJson(json);
|
|
|
|
DescriptionTextBox.Text = newEntity.Entity!.Description;
|
|
}
|
|
|
|
private void TypesListBox_SelectedIndexChanged(object sender, EventArgs _)
|
|
{
|
|
if (TypesListBox.SelectedItem is null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
DatabaseViewer.Type = (EntityType)TypesListBox.SelectedItem;
|
|
// Viewer.Page = 1;
|
|
}
|
|
|
|
public void RenderJson(string json)
|
|
{
|
|
/*
|
|
if (JsonWebView.CoreWebView2 == null)
|
|
{
|
|
JsonWebView.EnsureCoreWebView2Async();
|
|
return;
|
|
}
|
|
|
|
string html = JsonViewerHtml.Replace("{JSON}", json);
|
|
JsonWebView.CoreWebView2.NavigateToString(html);
|
|
*/
|
|
}
|
|
}
|