Files
Pf2eDataScrapper/Pf2eDataScrapper/Importers/Implementations/Pf2eFoundry/DataBuilder/AbstractProcessor.cs
T

173 lines
4.5 KiB
C#

using Newtonsoft.Json.Linq;
using Pf2eModel;
using Pf2eModel.Model;
using Pf2eModel.Model.Base;
using Pf2eModel.Model.Base.Source;
using Pf2eModel.Model.Base.Targets;
using Pf2eModel.Model.Enum;
using Pf2eModel.Model.Structure;
using System.Security.Cryptography;
using System.Text;
namespace Pf2eDataScrapper.Importers.Implementations.Pf2eFoundry.DataBuilder;
internal abstract class AbstractProcessor(ProcessorInput input)
{
protected DatabaseHelper Helper = DatabaseHelper.Instance;
public readonly Pf2eDbContext Context = DatabaseHelper.DbContext;
public readonly ProcessorInput Input = input;
public Entity Entity { get; set; } = new();
public abstract EntityType Type { get; }
public abstract IEntity Object { get; }
public virtual void Process()
{
Entity.Id = Helper.NextEntityId;
Entity.Type = Type;
Entity.Name = Input.GetRequired<string>("name");
Entity.Img = Input.GetRequired<string>("img");
Entity.Description = Input.GetRequired<string>("system.description.value");
Entity.IsTranslated = false;
Entity.IsChecked = false;
Entity.Slug = Helper.GenerateSlug(Entity.Name);
Entity.Rarity = Input.EnumOptional<EntityRarity>("system.traits.rarity", EntityRarity.Common);
Object.Id = Entity.Id;
Context.Entities.Add(Entity);
// Alias
EntityAlias alias = new()
{
Type = AliasType.English,
Alias = Entity.Name,
EntityId = Entity.Id,
Entity = Entity
};
Context.EntityAliases.Add(alias);
// Source
EntitySource source = new()
{
Entity = Entity,
Source = GetSource(Input.GetRequired<string>("system.publication.title")),
Suffix = "",
IsRemaster = Input.GetRequired<bool>("system.publication.remaster")
};
Context.EntitySources.Add(source);
// External
EntityExternal external = new()
{
Entity = Entity,
Target = GetTarget(),
Suffix = Input.GetRequired<string>("_id"),
Md5 = Md5(Input.Input.ToString())
};
Context.EntityExternals.Add(external);
// Traits
JToken? traitsJson = Input.Input.SelectToken("system.traits.value");
if (traitsJson?.Type != JTokenType.Array)
{
throw new Exception("system.traits.value is expected to be an array");
}
foreach (Trait trait in GetTraits(traitsJson.ToArray().Select(jToken => jToken.ToString())))
{
Entity.Traits.Add(trait);
}
}
public void Save()
{
Context.SaveChanges();
}
protected Source GetSource(string name)
{
Source? source = Context.Sources.FirstOrDefault(s => s.Name == name);
if (source != null)
{
return source;
}
source = new()
{
Name = name
};
Context.Sources.Add(source);
return source;
}
protected ExternalTarget GetTarget()
{
const string name = "pf2e - Foundry module";
ExternalTarget? target = Context.ExternalTargets.FirstOrDefault(s => s.Name == name);
if (target != null)
{
return target;
}
target = new()
{
Name = "pf2e - Foundry module",
Icon = "",
Prefix = "https://github.com/foundryvtt/pf2e",
};
Context.ExternalTargets.Add(target);
return target;
}
protected IEnumerable<Trait> GetTraits(IEnumerable<string> names)
{
List<Trait> result = Context.Traits.Where(t => names.Contains(t.Entity.Name)).ToList();
foreach (string name in names)
{
if (result.Any(t => t.Entity.Name == name))
{
continue;
}
/*
Trait trait = new()
{
Name = name,
Slug = name,
Type = TraitType.Regular,
Description = "",
};
Context.Traits.Add(trait);
result.Add(trait);
*/
}
return result;
}
public static string Md5(string input, bool isLowercase = false)
{
using MD5 md5 = MD5.Create();
byte[] byteHash = md5.ComputeHash(Encoding.UTF8.GetBytes(input));
string hash = BitConverter.ToString(byteHash).Replace("-", "");
return isLowercase ? hash.ToLower() : hash;
}
}