168 lines
4.6 KiB
C#
168 lines
4.6 KiB
C#
using Microsoft.EntityFrameworkCore;
|
|
using Newtonsoft.Json;
|
|
using Newtonsoft.Json.Linq;
|
|
using Newtonsoft.Json.Serialization;
|
|
using Pf2eModel;
|
|
using Pf2eModel.Model;
|
|
using Pf2eModel.Model.Base;
|
|
using Pf2eModel.Model.Enum;
|
|
using static LinqToDB.Common.Configuration;
|
|
|
|
namespace Pf2eViewer;
|
|
|
|
public partial class MainViewForm : Form
|
|
{
|
|
public Pf2eDbContext Context = new();
|
|
|
|
public MainViewForm()
|
|
{
|
|
InitializeComponent();
|
|
Reset();
|
|
|
|
EntityGridView.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
|
|
}
|
|
|
|
public void Reset()
|
|
{
|
|
Context.Dispose();
|
|
Context = new();
|
|
|
|
TypesListBox.DataSource = Context.Entities.Select(e => e.Type).Distinct().ToList();
|
|
TypesListBox.SelectedIndex = 0;
|
|
}
|
|
|
|
private void TypesListBox_SelectedIndexChanged(object sender, EventArgs e)
|
|
{
|
|
if (TypesListBox.SelectedItem is null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
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.Traits)
|
|
.Select(e => new EntityGridRow(e))
|
|
.ToList();
|
|
EntityGridView.Rows[0].Selected = true;
|
|
}
|
|
|
|
private void EntityGridView_RowStateChanged(object sender, DataGridViewRowStateChangedEventArgs e)
|
|
{
|
|
var rowsCount = EntityGridView.SelectedRows.Count;
|
|
if (rowsCount == 0 || rowsCount > 1){
|
|
return;
|
|
}
|
|
|
|
var row = EntityGridView.SelectedRows[0];
|
|
|
|
var data = (EntityGridRow) row.DataBoundItem;
|
|
|
|
var json = SerializeEntity(data.GetEntity());
|
|
|
|
JsonToTreeview(EntityTreeView, json, "Entity");
|
|
|
|
DescriptionTextBox.Text = data.GetEntity().Description;
|
|
}
|
|
|
|
public string SerializeEntity(Entity entity)
|
|
{
|
|
var settings = new JsonSerializerSettings()
|
|
{
|
|
Converters = [ new EntityConventor() ]
|
|
// ContractResolver = new CamelCasePropertyNamesContractResolver(),
|
|
};
|
|
|
|
string result = JsonConvert.SerializeObject(entity, settings);
|
|
|
|
Type type = EntityOfTypeAttribute.Resolve(entity);
|
|
string subReuslt = JsonConvert.SerializeObject(Context.Find(type, entity.Id), settings);
|
|
|
|
return result[..^1] + $",\"SubEntity\":{subReuslt}}}";
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
}
|
|
}
|
|
|
|
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;
|
|
foreach (JToken oJtok in jArray)
|
|
{
|
|
subNodes.Add(Element2TreeNode(oJtok, string.Format("[{0}]", 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;
|
|
//public string Slug { get; set; } = e.Slug;
|
|
//public EntityRarity Rarity { get; set; } = e.Rarity;
|
|
|
|
private readonly Entity _e = e;
|
|
public Entity GetEntity() => _e;
|
|
|
|
}
|
|
}
|