109 lines
2.8 KiB
C#
109 lines
2.8 KiB
C#
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;
|
|
|
|
public readonly EntityView Viewer;
|
|
|
|
public MainViewForm()
|
|
{
|
|
InitializeComponent();
|
|
|
|
_ = new DarkModeCS(this) { ColorMode = DarkModeCS.DisplayMode.SystemDefault };
|
|
|
|
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}}}";
|
|
|
|
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);
|
|
}
|
|
|
|
private void JsonWebView_CoreWebView2InitializationCompleted(
|
|
object sender,
|
|
Microsoft.Web.WebView2.Core.CoreWebView2InitializationCompletedEventArgs e
|
|
)
|
|
{
|
|
JsonWebView.Visible = true;
|
|
DatabaseViewer_EntityChange(DatabaseViewer.CurrentEntity);
|
|
}
|
|
}
|