DFLayoutMultiImageProperties.cs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. using Newtonsoft.Json.Linq;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. namespace InABox.Core
  6. {
  7. public class DFLayoutMultiImageProperties : DFLayoutFieldProperties<List<byte[]>>
  8. {
  9. [CheckBoxEditor]
  10. public bool DisableLibrary { get; set; } = false;
  11. public override string FormatValue(object? value)
  12. {
  13. return value != null ? "Yes" : "";
  14. }
  15. public override object? ParseValue(object? value)
  16. {
  17. List<string>? values = null;
  18. if (value is List<byte[]> list)
  19. return list;
  20. else if(value is JArray jArray)
  21. values = jArray.Select(x => x.ToString()).ToList();
  22. else if(value is string str)
  23. {
  24. if (Guid.TryParse(str, out var id))
  25. {
  26. return new List<byte[]>();
  27. }
  28. values = Serialization.Deserialize<List<string>>(str);
  29. }
  30. if(values != null)
  31. {
  32. try
  33. {
  34. var convertedValues = new List<byte[]>();
  35. foreach (var v in values) convertedValues.Add(Convert.FromBase64String(v));
  36. return convertedValues;
  37. }
  38. catch (Exception)
  39. {
  40. return null;
  41. }
  42. }
  43. return null;
  44. }
  45. protected override void LoadProperties()
  46. {
  47. base.LoadProperties();
  48. DisableLibrary = GetProperty("DisableLibrary", false);
  49. }
  50. protected override void SaveProperties()
  51. {
  52. base.SaveProperties();
  53. SetProperty("DisableLibrary", DisableLibrary);
  54. }
  55. }
  56. }