Initial commit: Better-Seewo EN5 plugin v1.0.1
This commit is contained in:
111
Better-EN5/Services/InkService.cs
Normal file
111
Better-EN5/Services/InkService.cs
Normal file
@@ -0,0 +1,111 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace BetterEN5.Services
|
||||
{
|
||||
public class InkService : IInkService
|
||||
{
|
||||
public async Task ExportInkAsync(string filePath)
|
||||
{
|
||||
await Task.Run(() =>
|
||||
{
|
||||
dynamic? board = GetENProperty("CurrentBoardApi");
|
||||
if (board == null) return;
|
||||
|
||||
dynamic? slides = board.Slides;
|
||||
if (slides == null || slides.Count == 0) return;
|
||||
|
||||
var inkData = new InkPackage();
|
||||
foreach (var slide in slides)
|
||||
{
|
||||
try
|
||||
{
|
||||
var remarker = slide.GetType().GetMethod("GetRemarkProvider")?.Invoke(slide, null);
|
||||
if (remarker != null)
|
||||
{
|
||||
var remarks = remarker.GetType().GetMethod("GetAllRemarks")?.Invoke(remarker, null);
|
||||
if (remarks != null)
|
||||
{
|
||||
inkData.Slides.Add(new SlideInkData
|
||||
{
|
||||
SlideId = slide.Id,
|
||||
Remarks = remarks
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
catch { }
|
||||
}
|
||||
|
||||
var dir = Path.GetDirectoryName(filePath);
|
||||
if (!string.IsNullOrEmpty(dir) && !Directory.Exists(dir))
|
||||
Directory.CreateDirectory(dir);
|
||||
|
||||
var json = System.Text.Json.JsonSerializer.Serialize(inkData);
|
||||
File.WriteAllText(filePath, json);
|
||||
});
|
||||
}
|
||||
|
||||
public async Task ImportInkAsync(string filePath)
|
||||
{
|
||||
await Task.Run(() =>
|
||||
{
|
||||
if (!File.Exists(filePath)) return;
|
||||
|
||||
var json = File.ReadAllText(filePath);
|
||||
var inkData = System.Text.Json.JsonSerializer.Deserialize<InkPackage>(json);
|
||||
if (inkData == null) return;
|
||||
|
||||
dynamic? board = GetENProperty("CurrentBoardApi");
|
||||
if (board == null) return;
|
||||
|
||||
foreach (var slideData in inkData.Slides)
|
||||
{
|
||||
try
|
||||
{
|
||||
var slide = board.GetType().GetMethod("FindSlideById")?.Invoke(board, new[] { slideData.SlideId });
|
||||
if (slide == null) continue;
|
||||
|
||||
var remarker = slide.GetType().GetMethod("GetRemarkProvider")?.Invoke(slide, null);
|
||||
if (remarker != null)
|
||||
{
|
||||
remarker.GetType().GetMethod("LoadRemarks")?.Invoke(remarker, new[] { slideData.Remarks });
|
||||
}
|
||||
}
|
||||
catch { }
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private static dynamic? GetENProperty(string propertyName)
|
||||
{
|
||||
try
|
||||
{
|
||||
var enType = Type.GetType("Cvte.EasiNote.EN, Cvte.EasiUI", false);
|
||||
if (enType == null)
|
||||
{
|
||||
var asm = typeof(Cvte.EasiNote.UIItem).Assembly;
|
||||
enType = asm.GetType("Cvte.EasiNote.EN");
|
||||
}
|
||||
return enType?.GetProperty(propertyName)?.GetValue(null);
|
||||
}
|
||||
catch
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public class InkPackage
|
||||
{
|
||||
public List<SlideInkData> Slides { get; set; } = new();
|
||||
}
|
||||
|
||||
public class SlideInkData
|
||||
{
|
||||
public string SlideId { get; set; } = string.Empty;
|
||||
public object Remarks { get; set; } = new();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user