/* Function: ResolveImageLink * * Calculates a new target for the passed image link ID. * * Requirements: * * - Requires the accessor to have at least a read/possible write lock. If the link target changes it will be upgraded to * read/write automatically. * */ protected void ResolveImageLink(int imageLinkID, Accessor accessor) { ImageLink imageLink = accessor.GetImageLinkByID(imageLinkID, Accessor.GetImageLinkFlags.DontLookupClasses); int bestMatchFileID = 0; int bestMatchScore = 0; // First try the path relative to the source file. Path pathRelativeToSourceFile = EngineInstance.Files.FromID(imageLink.FileID).FileName.ParentFolder + '/' + imageLink.Path; File fileRelativeToSourceFile = EngineInstance.Files.FromPath(pathRelativeToSourceFile); if (fileRelativeToSourceFile != null && fileRelativeToSourceFile.Deleted == false) { int score = Manager.Score(imageLink, fileRelativeToSourceFile, bestMatchScore); if (score > bestMatchScore) { bestMatchScore = score; bestMatchFileID = fileRelativeToSourceFile.ID; } } // Next try the path relative to any image folders. foreach (var fileSource in EngineInstance.Files.FileSources) { if (fileSource.Type == InputType.Image) { Path pathRelativeToFileSource = fileSource.MakeAbsolute(imageLink.Path); File fileRelativeToFileSource = EngineInstance.Files.FromPath(pathRelativeToFileSource); if (fileRelativeToFileSource != null && fileRelativeToFileSource.Deleted == false) { int score = Manager.Score(imageLink, fileRelativeToFileSource, bestMatchScore); if (score > bestMatchScore) { bestMatchScore = score; bestMatchFileID = fileRelativeToFileSource.ID; } } } } // Update the database if (bestMatchFileID != imageLink.TargetFileID || bestMatchScore != imageLink.TargetScore) { int oldTargetFileID = imageLink.TargetFileID; imageLink.TargetFileID = bestMatchFileID; imageLink.TargetScore = bestMatchScore; accessor.UpdateImageLinkTarget(imageLink, oldTargetFileID); } }
/* Function: CopyNonIDPropertiesFrom * Makes this link copy all the properties not tested by <CompareIDPropertiesTo()> and <SameIDPropertiesAs()> from * the passed link. */ public void CopyNonIDPropertiesFrom(ImageLink other) { // DEPENDENCY: What this copies depends on what CompareIDPropertiesTo() does not. imageLinkID = other.imageLinkID; path = other.path; classID = other.classID; targetFileID = other.targetFileID; targetScore = other.targetScore; }
// Group: Image Link Scoring Functions // __________________________________________________________________________ /* Function: Score * * Generates a numeric score representing how well the <File> serves as a match for the <ImageLink>. Higher scores * are better, and zero means they don't match at all. * * If a score has to beat a certain threshold to be relevant, you can pass it to lessen the processing load. This function * may be able to tell it can't beat the score early and return without performing later steps. In these cases it will return * -1. */ public int Score(ImageLink imageLink, File file, int minimumScore = 0) { // If it's already resolved to the maximum value we can quit without checking anything. if (minimumScore == int.MaxValue) { return(-1); } // If it's a deleted file it obviously can't be a match. if (file.Deleted) { return(0); } // See if the target path is the same as the path relative to the source file. This is our best choice. // We always ignore case regardless of the underlying file system's case sensitivity. Path pathRelativeToSourceFile = EngineInstance.Files.FromID(imageLink.FileID).FileName.ParentFolder + '/' + imageLink.Path; if (string.Compare(pathRelativeToSourceFile, file.FileName, true) == 0) { return(int.MaxValue); } // Otherwise see if the target path is the same as the path relative to an image folder. This is our next best choice. We // rank image folders by their number, lower being better. var fileSource = EngineInstance.Files.FileSourceOf(file); if (fileSource.Type == InputType.Image) { Path pathRelativeToFileSource = fileSource.MakeAbsolute(imageLink.Path); if (string.Compare(pathRelativeToFileSource, file.FileName, true) == 0) { return(int.MaxValue - fileSource.Number); } } // Nope. return(0); }
/* Function: CompareIDPropertiesTo * * Compares the identifying properties of the link (<OriginalText>, <FileID>, <ClassString>) and returns a value similar to a string * comparison result which is suitable for sorting a list of ImageLinks. It will return zero if all the properties are equal. */ public int CompareIDPropertiesTo(ImageLink other) { // DEPENDENCY: What CopyNonIDPropertiesFrom() does depends on what this compares. int result = originalText.CompareTo(other.originalText); if (result != 0) { return(result); } if (fileID != other.fileID) { return(fileID - other.fileID); } if (classString == null) { if (other.classString == null) { return(0); } else { return(-1); } } else { if (other.classString == null) { return(1); } else { return(classString.ToString().CompareTo(other.classString.ToString())); } } }
/* Function: SameIDPropertiesAs * Returns whether the identifying properties of the link (<OriginalText>, <FileID>, <ClassString>) are the same as the passed one. */ public bool SameIDPropertiesAs(ImageLink other) { return(CompareIDPropertiesTo(other) == 0); }
public void OnDeleteImageLink(ImageLink imageLink, CodeDB.EventAccessor eventAccessor) { // xxx placeholder }
public void OnChangeImageLinkTarget(ImageLink imageLink, int oldTargetFileID, CodeDB.EventAccessor eventAccessor) { // xxx placeholder }
public void OnDeleteImageLink(ImageLink imageLink, CodeDB.EventAccessor eventAccessor) { unprocessedChanges.DeleteImageLink(imageLink); }
public void OnChangeImageLinkTarget(ImageLink imageLink, int oldTargetFileID, CodeDB.EventAccessor eventAccessor) { // We're going to be the one causing this event, not responding to it. No other code should be changing link definitions. }
/* Function: DeleteImageLink */ public void DeleteImageLink(ImageLink imageLink) { lock (accessLock) { imageLinksToResolve.Remove(imageLink.ImageLinkID); } }
/* Function: AddImageLink */ public void AddImageLink(ImageLink imageLink) { lock (accessLock) { imageLinksToResolve.Add(imageLink.ImageLinkID); } }