using System; using System.Collections.Generic; using System.Linq; using System.Security.Cryptography; using System.Text; using Artemis.Storage.Entities.Profile.Nodes; using SkiaSharp; namespace Artemis.Core.Services; internal class NodeService : INodeService { public IEnumerable AvailableNodes => NodeTypeStore.GetAll(); /// public List GetRegisteredTypes() { return NodeTypeStore.GetColors().Select(c => c.Type).Distinct().ToList(); } /// public TypeColorRegistration GetTypeColorRegistration(Type type) { TypeColorRegistration? match = NodeTypeStore.GetColor(type); if (match != null) return match; // Objects represent an input that can take any type, these are hardcoded white if (type == typeof(object)) return new TypeColorRegistration(type, new SKColor(255, 255, 255, 255), Constants.CorePluginFeature); // Come up with a random color based on the type name that should be the same each time MD5 md5Hasher = MD5.Create(); byte[] hashed = md5Hasher.ComputeHash(Encoding.UTF8.GetBytes(type.FullName!)); int hash = BitConverter.ToInt32(hashed, 0); SKColor baseColor = SKColor.FromHsl(hash % 255, 50 + hash % 50, 50); return new TypeColorRegistration(type, baseColor, Constants.CorePluginFeature); } public string ExportScript(NodeScript nodeScript) { nodeScript.Save(); return CoreJson.Serialize(nodeScript.Entity); } public void ImportScript(string json, NodeScript target) { NodeScriptEntity? entity = CoreJson.Deserialize(json); if (entity == null) throw new ArtemisCoreException("Failed to load node script"); target.LoadFromEntity(entity); } } /// /// A service that provides access to the node system /// public interface INodeService : IArtemisService { /// /// Gets all available nodes /// IEnumerable AvailableNodes { get; } /// /// Gets all currently available node pin types. /// /// A of containing the currently available node pin types. List GetRegisteredTypes(); /// /// Gets the best matching registration for the provided type /// TypeColorRegistration GetTypeColorRegistration(Type type); /// /// Exports the provided node script to JSON. /// /// The node script to export. /// The resulting JSON. string ExportScript(NodeScript nodeScript); /// /// Imports the provided JSON onto the provided node script, overwriting any existing contents. /// /// The JSON to import. /// The target node script whose contents to overwrite. void ImportScript(string json, NodeScript target); }