/// <summary> /// Sets up cached PropertyInfo and determines the best delegate to use to compare values /// retrieved from that property. /// </summary> public void Initialize() { if (fFoundProperty == false) { fFoundProperty = true; if (pi == null) { PropertyDescriptorCollection props = TypeDescriptor.GetProperties(typeof(T)); property = props[sPropertyName]; pi = typeof(T).GetProperty(sPropertyName); if (pi == null) { throw new Exception("Property name " + sPropertyName + " not found while trying to compare objects of type " + typeof(T).Name); } } typ = pi.PropertyType; // Set up the property comparison delegate to use based on the type of values we will be comparing if (sortType == SortType.eUsePropertyOrFieldType) { sortType = Sorting.GetSortTypeEnumForType(typ); if (typ == typeof(string)) { if (stringComparisonToUse == StringComparison.Ordinal) { DoCompare = StringCompareOrdinal; } else { DoCompare = StringCompare; } } else if (typ == typeof(int) && !fSortDescending) { DoCompare = CompareInt; } else if (typ == typeof(int)) { DoCompare = CompareIntDesc; } else if (typ == typeof(DateTime)) { DoCompare = CompareDates; } else if (typ == typeof(long)) { DoCompare = CompareTypeSensitive <long>; } else if (typ == typeof(double)) { DoCompare = CompareTypeSensitive <double>; } else if (typ == typeof(float)) { DoCompare = CompareTypeSensitive <float>; } else if (typ == typeof(short)) { DoCompare = CompareTypeSensitive <short>; } else if (typ == typeof(byte)) { DoCompare = CompareTypeSensitive <byte>; } else if (typ == typeof(bool)) { DoCompare = CompareTypeSensitive <bool>; } else if (typ.BaseType == typeof(Enum)) { FastEnumLookup = new Dictionary <int, string>(32); if (fSortDescending) { DoCompare = FastCompareEnumsDesc; } else { DoCompare = FastCompareEnumsAsc; } } else { DoCompare = CompareUsingToString; } } else { if (sortType == SortType.eString) { DoCompare = CompareUsingToString; } else if (sortType == SortType.eByte) { DoCompare = CompareUsingToByte; } else if (sortType == SortType.eDateTime) { DoCompare = CompareUsingToDate; } else if (sortType == SortType.eInteger) { DoCompare = CompareUsingToInt; } else if (sortType == SortType.eLong) { DoCompare = CompareUsingToInt64; } else if (sortType == SortType.eDoubleOrFloat) { DoCompare = CompareUsingToDouble; } else { DoCompare = CompareUsingToString; } } } }
/// <summary> /// Set the Episode image and overview, also over-write the episode name if user has selected this /// </summary> /// <param name="dictTvDBEpisodes"></param> /// <param name="dictTvDBSeasons"></param> /// <param name="dictTvDBSeasonsSpecials"></param> /// <param name="tvDBCrossRef"></param> public void SetTvDBInfo(TvDBSummary tvSummary) { TvDBLinkExists = false; TvDBLinkMissing = true; #region episode override // check if this episode has a direct tvdb over-ride if (tvSummary.DictTvDBCrossRefEpisodes.ContainsKey(AniDB_EpisodeID)) { foreach (TvDB_EpisodeVM tvep in tvSummary.DictTvDBEpisodes.Values) { if (tvSummary.DictTvDBCrossRefEpisodes[AniDB_EpisodeID] == tvep.Id) { if (string.IsNullOrEmpty(tvep.Overview)) { this.EpisodeOverviewLoading = "Episode Overview Not Available"; } else { this.EpisodeOverviewLoading = tvep.Overview; } if (string.IsNullOrEmpty(tvep.FullImagePath) || !File.Exists(tvep.FullImagePath)) { if (string.IsNullOrEmpty(tvep.OnlineImagePath)) { this.EpisodeImageLoading = @"/Images/EpisodeThumb_NotFound.png"; // if there is no proper image to show, we will hide it on the dashboard ShowEpisodeImageInDashboard = false; } //else // this.EpisodeImageLoading = tvep.OnlineImagePath; } else { this.EpisodeImageLoading = tvep.FullImagePath; } if (JMMServerVM.Instance.EpisodeTitleSource == DataSourceType.TheTvDB && !string.IsNullOrEmpty(tvep.EpisodeName)) { EpisodeName = tvep.EpisodeName; } TvDBLinkExists = true; TvDBLinkMissing = false; return; } } } #endregion logger.Trace("SetTvDBInfo: normal episodes start"); #region normal episodes // now do stuff to improve performance if (this.EpisodeTypeEnum == JMMClient.EpisodeType.Episode) { if (tvSummary != null && tvSummary.CrossRefTvDBV2 != null && tvSummary.CrossRefTvDBV2.Count > 0) { logger.Trace("SetTvDBInfo: sorting TvDB cross refs: {0} records", tvSummary.CrossRefTvDBV2.Count); // find the xref that is right // relies on the xref's being sorted by season number and then episode number (desc) List <SortPropOrFieldAndDirection> sortCriteria = new List <SortPropOrFieldAndDirection>(); sortCriteria.Add(new SortPropOrFieldAndDirection("AniDBStartEpisodeNumber", true, JMMClient.SortType.eInteger)); List <CrossRef_AniDB_TvDBVMV2> tvDBCrossRef = Sorting.MultiSort <CrossRef_AniDB_TvDBVMV2>(tvSummary.CrossRefTvDBV2, sortCriteria); logger.Trace("SetTvDBInfo: looking for starting points"); bool foundStartingPoint = false; CrossRef_AniDB_TvDBVMV2 xrefBase = null; foreach (CrossRef_AniDB_TvDBVMV2 xrefTV in tvDBCrossRef) { if (xrefTV.AniDBStartEpisodeType != (int)JMMClient.EpisodeType.Episode) { continue; } if (this.EpisodeNumber >= xrefTV.AniDBStartEpisodeNumber) { foundStartingPoint = true; xrefBase = xrefTV; break; } } logger.Trace("SetTvDBInfo: looking for starting points - done"); // we have found the starting epiosde numbder from AniDB // now let's check that the TvDB Season and Episode Number exist if (foundStartingPoint) { logger.Trace("SetTvDBInfo: creating dictionary"); Dictionary <int, int> dictTvDBSeasons = null; Dictionary <int, TvDB_EpisodeVM> dictTvDBEpisodes = null; foreach (TvDBDetails det in tvSummary.TvDetails.Values) { if (det.TvDBID == xrefBase.TvDBID) { dictTvDBSeasons = det.DictTvDBSeasons; dictTvDBEpisodes = det.DictTvDBEpisodes; break; } } logger.Trace("SetTvDBInfo: creating dictionary - done"); if (dictTvDBSeasons.ContainsKey(xrefBase.TvDBSeasonNumber)) { int episodeNumber = dictTvDBSeasons[xrefBase.TvDBSeasonNumber] + (this.EpisodeNumber + xrefBase.TvDBStartEpisodeNumber - 2) - (xrefBase.AniDBStartEpisodeNumber - 1); if (dictTvDBEpisodes.ContainsKey(episodeNumber)) { logger.Trace("SetTvDBInfo: loading episode overview"); TvDB_EpisodeVM tvep = dictTvDBEpisodes[episodeNumber]; if (string.IsNullOrEmpty(tvep.Overview)) { this.EpisodeOverviewLoading = "Episode Overview Not Available"; } else { this.EpisodeOverviewLoading = tvep.Overview; } logger.Trace("SetTvDBInfo: loading episode overview - done"); bool imagePathMissing = string.IsNullOrEmpty(tvep.FullImagePath); logger.Trace("SetTvDBInfo: checking for image path missing - done\n - {0}\n - {1}\n", tvep.FullImagePath, tvep.FullImagePathPlain); bool imagePhysicalMissing = !File.Exists(tvep.FullImagePath); logger.Trace("SetTvDBInfo: checking for physical image missing - done\n - {0}\n - {1}\n", tvep.FullImagePath, tvep.FullImagePathPlain); if (imagePathMissing || imagePhysicalMissing) { if (string.IsNullOrEmpty(tvep.OnlineImagePath)) { this.EpisodeImageLoading = @"/Images/EpisodeThumb_NotFound.png"; // if there is no proper image to show, we will hide it on the dashboard ShowEpisodeImageInDashboard = false; } //else // this.EpisodeImageLoading = tvep.OnlineImagePath; } else { this.EpisodeImageLoading = tvep.FullImagePath; } logger.Trace("SetTvDBInfo: episode image - done"); if (JMMServerVM.Instance.EpisodeTitleSource == DataSourceType.TheTvDB && !string.IsNullOrEmpty(tvep.EpisodeName)) { EpisodeName = tvep.EpisodeName; } } } } } } #endregion logger.Trace("SetTvDBInfo: normal episodes finish"); #region special episodes if (this.EpisodeTypeEnum == JMMClient.EpisodeType.Special) { // find the xref that is right // relies on the xref's being sorted by season number and then episode number (desc) List <SortPropOrFieldAndDirection> sortCriteria = new List <SortPropOrFieldAndDirection>(); sortCriteria.Add(new SortPropOrFieldAndDirection("AniDBStartEpisodeNumber", true, JMMClient.SortType.eInteger)); List <CrossRef_AniDB_TvDBVMV2> tvDBCrossRef = Sorting.MultiSort <CrossRef_AniDB_TvDBVMV2>(tvSummary.CrossRefTvDBV2, sortCriteria); bool foundStartingPoint = false; CrossRef_AniDB_TvDBVMV2 xrefBase = null; foreach (CrossRef_AniDB_TvDBVMV2 xrefTV in tvDBCrossRef) { if (xrefTV.AniDBStartEpisodeType != (int)JMMClient.EpisodeType.Special) { continue; } if (this.EpisodeNumber >= xrefTV.AniDBStartEpisodeNumber) { foundStartingPoint = true; xrefBase = xrefTV; break; } } if (tvSummary != null && tvSummary.CrossRefTvDBV2 != null && tvSummary.CrossRefTvDBV2.Count > 0) { // we have found the starting epiosde numbder from AniDB // now let's check that the TvDB Season and Episode Number exist if (foundStartingPoint) { Dictionary <int, int> dictTvDBSeasons = null; Dictionary <int, TvDB_EpisodeVM> dictTvDBEpisodes = null; foreach (TvDBDetails det in tvSummary.TvDetails.Values) { if (det.TvDBID == xrefBase.TvDBID) { dictTvDBSeasons = det.DictTvDBSeasons; dictTvDBEpisodes = det.DictTvDBEpisodes; break; } } if (dictTvDBSeasons.ContainsKey(xrefBase.TvDBSeasonNumber)) { int episodeNumber = dictTvDBSeasons[xrefBase.TvDBSeasonNumber] + (this.EpisodeNumber + xrefBase.TvDBStartEpisodeNumber - 2) - (xrefBase.AniDBStartEpisodeNumber - 1); if (dictTvDBEpisodes.ContainsKey(episodeNumber)) { TvDB_EpisodeVM tvep = dictTvDBEpisodes[episodeNumber]; this.EpisodeOverviewLoading = tvep.Overview; if (string.IsNullOrEmpty(tvep.FullImagePath) || !File.Exists(tvep.FullImagePath)) { if (string.IsNullOrEmpty(tvep.OnlineImagePath)) { this.EpisodeImageLoading = @"/Images/EpisodeThumb_NotFound.png"; // if there is no proper image to show, we will hide it on the dashboard ShowEpisodeImageInDashboard = false; } //else // this.EpisodeImageLoading = tvep.OnlineImagePath; } else { this.EpisodeImageLoading = tvep.FullImagePath; } if (JMMServerVM.Instance.EpisodeTitleSource == DataSourceType.TheTvDB && !string.IsNullOrEmpty(tvep.EpisodeName)) { EpisodeName = tvep.EpisodeName; } } } } } } #endregion }
/// <summary> /// Sets up cached FieldInfo and determines the best delegate to use to compare values /// retrieved from that field. /// </summary> public void Initialize() { if (fFoundField == false) { fFoundField = true; if (fi == null) { // You can play around with binding flags if you really want to access nonpublic fields, etc... // note that there is a significant performance hit on accessing protected and private fields, // since security / permissions are checked every time, from what I can tell. It's better // just to go through public properties if you're not accessing public fields. // fi = typeof(T).GetField(sFieldName, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance); fi = typeof(T).GetField(sFieldName); if (fi == null) { throw new Exception("Field name " + sFieldName + " not found while trying to compare objects of type " + typeof(T).Name); } } typ = fi.FieldType; if (sortType == SortType.eUsePropertyOrFieldType) { sortType = Sorting.GetSortTypeEnumForType(typ); if (typ == typeof(string)) { if (stringComparisonToUse == StringComparison.Ordinal) { DoCompare = StringCompareOrdinal; } else { DoCompare = StringCompare; } } else if (typ == typeof(int) && !fSortDescending) { DoCompare = CompareInt; } else if (typ == typeof(int)) { DoCompare = CompareIntDesc; } else if (typ == typeof(DateTime)) { DoCompare = CompareDates; } else if (typ == typeof(long)) { DoCompare = CompareTypeSensitive <long>; } else if (typ == typeof(double)) { DoCompare = CompareTypeSensitive <double>; } else if (typ == typeof(float)) { DoCompare = CompareTypeSensitive <float>; } else if (typ == typeof(short)) { DoCompare = CompareTypeSensitive <short>; } else if (typ == typeof(byte)) { DoCompare = CompareTypeSensitive <byte>; } else if (typ == typeof(bool)) { DoCompare = CompareTypeSensitive <bool>; } else if (typ.BaseType == typeof(Enum)) { FastEnumLookup = new Dictionary <int, string>(32); if (fSortDescending) { DoCompare = FastCompareEnumsDesc; } else { DoCompare = FastCompareEnumsAsc; } } else { DoCompare = CompareUsingToString; } // optimize to use the ABOVE path if the property or field type matches // the requested sort type (i.e. below) } else { if (sortType == SortType.eString) { DoCompare = CompareUsingToString; } else if (sortType == SortType.eByte) { DoCompare = CompareUsingToByte; } else if (sortType == SortType.eDateTime) { DoCompare = CompareUsingToDate; } else if (sortType == SortType.eInteger) { DoCompare = CompareUsingToInt; } else if (sortType == SortType.eLong) { DoCompare = CompareUsingToInt64; } else if (sortType == SortType.eDoubleOrFloat) { DoCompare = CompareUsingToDouble; } else { DoCompare = CompareUsingToString; } } } }