NoSoliciting/NoSoliciting.CursedWorkaround/CursedWorkaround.cs
Anna Clemens 1487863c19
fix: make plugin work on stock Dalamud
Use some horrible, cursed AppDomain shit to load dependencies that break on normal Dalamud in their own environment, then do classification there instead.
2020-12-23 03:52:19 -05:00

32 lines
1.2 KiB
C#

using System;
using System.IO;
using Microsoft.ML;
using NoSoliciting.Interface;
namespace NoSoliciting.CursedWorkaround {
[Serializable]
public class CursedWorkaround : MarshalByRefObject, IClassifier, IDisposable {
private MLContext Context { get; set; } = null!;
private ITransformer Model { get; set; } = null!;
private DataViewSchema Schema { get; set; } = null!;
private PredictionEngine<MessageData, MessagePrediction> PredictionEngine { get; set; } = null!;
public void Initialise(byte[] data) {
this.Context = new MLContext();
using var stream = new MemoryStream(data);
var model = this.Context.Model.Load(stream, out var schema);
this.Model = model;
this.Schema = schema;
this.PredictionEngine = this.Context.Model.CreatePredictionEngine<MessageData, MessagePrediction>(this.Model, this.Schema);
}
public string Classify(ushort channel, string message) {
return this.PredictionEngine.Predict(new MessageData(channel, message)).Category;
}
public void Dispose() {
this.PredictionEngine.Dispose();
}
}
}