Guest User

Untitled

a guest
Nov 21st, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.69 KB | None | 0 0
  1. // only included in case some of the ctx weirdness needs explanation
  2.  
  3. using System;
  4. using System.Collections.ObjectModel;
  5. using System.IO;
  6. using System.Linq;
  7.  
  8. namespace SprueKit.Data
  9. {
  10. // When we have a broken path have to have a means to fix it
  11. public class SerializationBrokenPath
  12. {
  13. // target to fix
  14. public object TargetObject { get; set; }
  15. // unused, was going to be for differentiating parameterized Uris
  16. public string LinkType { get; set; }
  17. // the string name of the property has found in GetProperty().Name NOT as returned by the property helpers
  18. public string TargetProperty { get; set; }
  19. // The string form of the path that we tried to find it from
  20. public string BrokenPath { get; set; }
  21. // The file extension table, in dialog format "My File (*.txt)|*.txt"
  22. public string ExtensionMask { get; set; }
  23.  
  24. public SerializationBrokenPath(object target, string property, string path, string type, string mask)
  25. {
  26. TargetObject = target;
  27. TargetProperty = property;
  28. BrokenPath = path;
  29. LinkType = type;
  30. ExtensionMask = mask;
  31. }
  32.  
  33. /// <summary>
  34. /// Shows a file dialog to select a replacement file
  35. /// </summary>
  36. public bool Fix(string path)
  37. {
  38. if (System.IO.File.Exists(path))
  39. {
  40. if (ExtensionMask == null || ExtensionMask.Contains(System.IO.Path.GetExtension(path)))
  41. {
  42. var property = TargetObject.GetType().GetProperty(TargetProperty);
  43. if (property != null)
  44. {
  45. // Did this stuff ever work?
  46. //object convertedValue = Convert.ChangeType(path, property.PropertyType);
  47. //if (convertedValue != null)
  48. {
  49. property.SetValue(TargetObject, new Uri(path));// convertedValue));
  50. return true;
  51. }
  52. }
  53. }
  54. }
  55. return false;
  56. }
  57. }
  58.  
  59. // Manages the mapping of relative paths
  60. // When saving absolute paths need to be converted into relative ones
  61. // When loading relative paths have to be converted into absolute ones
  62. public class SerializationContext
  63. {
  64. public Uri MapRelativeTo { get; set; }
  65.  
  66. public Uri GetRelativePath(Uri uri)
  67. {
  68. if (MapRelativeTo == null)
  69. return uri;
  70. if (uri != null)
  71. {
  72. if (uri.IsFile)
  73. {
  74. string path = uri.AbsolutePath;
  75. var dirInfo = new DirectoryInfo(System.IO.Path.GetDirectoryName(path));
  76. if (dirInfo.Exists && System.IO.File.Exists(path))
  77. {
  78. if (Settings.GeneralSettings.CorePaths.Contains(dirInfo.Name))
  79. return new Uri(string.Format("{0}://{1}", dirInfo.Name, System.IO.Path.GetFileName(path)));
  80. }
  81.  
  82. Uri settingsFolder = new IOCDependency<Settings.GeneralSettings>().Object.CheckUri_Save(uri);
  83. if (settingsFolder != null)
  84. return settingsFolder;
  85. }
  86. return MapRelativeTo.MakeRelativeUri(uri);
  87. }
  88. return null;
  89. }
  90.  
  91. public string GetRelativePathString(Uri uri)
  92. {
  93. if (uri != null)
  94. {
  95. string retString = GetRelativePath(uri).ToString();
  96. return StripTrailingSlash(retString);
  97. }
  98. return "";
  99. }
  100.  
  101. static string StripTrailingSlash(string inStr)
  102. {
  103. if (inStr.EndsWith("/"))
  104. return inStr.Substring(0, inStr.Length - 1);
  105. return inStr;
  106. }
  107.  
  108. public Uri GetAbsolutePath(Uri relativeUri, object owner, string property, string type, string mask)
  109. {
  110. if (MapRelativeTo == null)
  111. return relativeUri;
  112. if (relativeUri != null)
  113. {
  114. Uri ret = relativeUri;
  115. if (!relativeUri.IsAbsoluteUri)
  116. ret = new Uri(MapRelativeTo, relativeUri);
  117.  
  118. foreach (var corePath in Settings.GeneralSettings.CorePaths)
  119. {
  120. if (corePath.ToLowerInvariant().Equals(relativeUri.Scheme))
  121. {
  122. string trimmed = relativeUri.ToString().Replace(string.Format("{0}://", relativeUri.Scheme), "");
  123. if (!string.IsNullOrEmpty(trimmed))
  124. ret = new Uri(string.Format("{0}/{1}", App.ProgramPath(corePath), StripTrailingSlash(trimmed)));
  125. }
  126. }
  127.  
  128. Uri settingsFolder = new IOCDependency<Settings.GeneralSettings>().Object.CheckUri_Read(relativeUri);
  129. if (settingsFolder != null)
  130. ret = settingsFolder;
  131.  
  132. if (!System.IO.File.Exists(ret.AbsolutePath))
  133. {
  134. BrokenPaths.Add(new SerializationBrokenPath(owner, property, Uri.UnescapeDataString(ret.AbsolutePath), type, mask));
  135. return null;
  136. }
  137. return ret;
  138. }
  139. return null;
  140. }
  141.  
  142. public SerializationContext(Uri relativeFolder)
  143. {
  144. MapRelativeTo = relativeFolder;
  145. }
  146.  
  147. // Must check this after deserialization to decide whether to show GUI for fixing bad paths
  148. public ObservableCollection<SerializationBrokenPath> BrokenPaths { get; private set; } = new ObservableCollection<SerializationBrokenPath>();
  149. }
  150. }
Add Comment
Please, Sign In to add comment