From 8599e275abf406ee20e98409c212646c9db979cb Mon Sep 17 00:00:00 2001 From: Brahim Hadriche Date: Tue, 7 Jul 2026 11:00:06 -0400 Subject: [PATCH] sb fix --- CHANGELOG.md | 4 + .../SponsorBlockCategoriesPreferences.bs | 12 +-- .../Notifications/SponsorBlockNotification.bs | 4 +- .../SponsorBlock/SponsorBlockConfig.bs | 75 +++++++++++++++++ .../SponsorBlockSegmentViewedJob.bs | 3 +- .../SponsorBlock/SponsorBlockSegmentsJob.bs | 19 +++-- .../SponsorBlock/SponsorBlockService.bs | 80 +++---------------- .../components/VideoPlayer/SponsorBlock.bs | 2 - .../TrickPlayBar/TrickPlayBar.bs | 5 -- 9 files changed, 113 insertions(+), 91 deletions(-) create mode 100644 playlet-lib/src/components/Services/SponsorBlock/SponsorBlockConfig.bs diff --git a/CHANGELOG.md b/CHANGELOG.md index 33262d3b..b6ee5c4d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Fixed + +- Wrong SponsorBlock in the trackbar due to image caching + ### Changed - Video Player HUD now focuses initially on the trackbar, instead of the pause button diff --git a/playlet-lib/src/components/Screens/SettingsScreen/SponsorBlockCategories/SponsorBlockCategoriesPreferences.bs b/playlet-lib/src/components/Screens/SettingsScreen/SponsorBlockCategories/SponsorBlockCategoriesPreferences.bs index 5f5641bd..ff766ba3 100644 --- a/playlet-lib/src/components/Screens/SettingsScreen/SponsorBlockCategories/SponsorBlockCategoriesPreferences.bs +++ b/playlet-lib/src/components/Screens/SettingsScreen/SponsorBlockCategories/SponsorBlockCategoriesPreferences.bs @@ -1,7 +1,7 @@ import "pkg:/components/Dialog/DialogUtils.bs" import "pkg:/components/Navigation/Navigation.bs" import "pkg:/components/parts/AutoBind/OnNodeReadyNoOp.bs" -import "pkg:/components/Services/SponsorBlock/SponsorBlockService.bs" +import "pkg:/components/Services/SponsorBlock/SponsorBlockConfig.bs" import "pkg:/components/VideoPlayer/SponsorBlockSegmentAction.bs" import "pkg:/source/utils/Logging.bs" import "pkg:/source/utils/RemoteKeys.bs" @@ -10,7 +10,7 @@ import "pkg:/source/utils/Types.bs" function Init() m.categoryList = m.top.findNode("categoryList") m.categoryList.ObserveField("itemSelected", FuncName(OnCategorySelected)) - m.service = new SponsorBlock.SponsorBlockService() + m.config = new SponsorBlock.SponsorBlockConfig() optionTitles = {} optionTitles[SponsorBlock.SegmentAction.Disable] = Tr(Locale.SponsorBlock.Disable) @@ -36,7 +36,7 @@ function Init() end function function SetupCategoriesUi() as void - categories = m.service.GetCategories() + categories = m.config.GetCategories() if categories.Count() = 0 return end if @@ -47,11 +47,11 @@ function SetupCategoriesUi() as void for each category in categories categoryNode = CreateObject("roSGNode", "SponsorBlockCategoryContent") categoryNode.category = category - categoryNode.categoryTitle = Tr(m.service.SegmentTitle(category)) + categoryNode.categoryTitle = Tr(m.config.SegmentTitle(category)) categoryNode.selectedOption = SponsorBlock.SegmentAction.Disable - categoryNode.options = m.service.GetOptionsForCategory(category) + categoryNode.options = m.config.GetOptionsForCategory(category) categoryNode.optionTitles = m.optionTitles - categoryNode.color = m.service.SegmentColor(category) + categoryNode.color = m.config.SegmentColor(category) categoryContentNodes.push(categoryNode) end for diff --git a/playlet-lib/src/components/Services/SponsorBlock/Notifications/SponsorBlockNotification.bs b/playlet-lib/src/components/Services/SponsorBlock/Notifications/SponsorBlockNotification.bs index 472f2459..60773d54 100644 --- a/playlet-lib/src/components/Services/SponsorBlock/Notifications/SponsorBlockNotification.bs +++ b/playlet-lib/src/components/Services/SponsorBlock/Notifications/SponsorBlockNotification.bs @@ -1,5 +1,5 @@ -import "pkg:/components/Services/SponsorBlock/SponsorBlockService.bs" import "pkg:/components/VideoPlayer/SponsorBlockSegmentAction.bs" +import "pkg:/source/utils/Types.bs" function Init() m.categoryLabel = m.top.findNode("categoryLabel") @@ -10,8 +10,6 @@ function Init() m.top.translation = [1280, 20] m.translationAnimation.observeField("state", FuncName(OnAnimationState)) m.animationTimer.observeField("fire", FuncName(OnAnimationTimer)) - - m.service = SponsorBlock.SponsorBlockService() end function function OnDoShow() as void diff --git a/playlet-lib/src/components/Services/SponsorBlock/SponsorBlockConfig.bs b/playlet-lib/src/components/Services/SponsorBlock/SponsorBlockConfig.bs new file mode 100644 index 00000000..1798ebf9 --- /dev/null +++ b/playlet-lib/src/components/Services/SponsorBlock/SponsorBlockConfig.bs @@ -0,0 +1,75 @@ +import "pkg:/source/utils/ColorUtils.bs" + +namespace SponsorBlock + class SponsorBlockConfig + function new() + ' TODO:P2 this is being read and parsed on every skipped segment. It should be cached. + m.config = StringUtils.ParseJsonSafe(ReadAsciiFile("libpkg:/config/sponsorblock_config.json5")) + end function + + function GetServerAddress() as string + if m.config = invalid + return "" + end if + + return ValidString(m.config.serverAddress) + end function + + function GetCategories() as object + if m.config = invalid + return [] + end if + + return ValidArray(m.config.categoryList) + end function + + function GetOptionsForCategory(category as string) as object + if m.config = invalid + return [] + end if + + if not m.config.categories.doesexist(category) + return [] + end if + + return ValidArray(m.config.categories[category].options) + end function + + function SegmentColor(category as string) as integer + if m.config = invalid + return 0 + end if + + if not m.config.categories.doesexist(category) + return 0 + end if + + color = m.config.categories[category].color + return ColorUtils.HexToInt(color) + end function + + function SegmentTitle(category as string) as string + if m.config = invalid + return category + end if + + if not m.config.categories.doesexist(category) + return category + end if + + return m.config.categories[category].title + end function + + function SegmentShortTitle(category as string) as string + if m.config = invalid + return category + end if + + if not m.config.categories.doesexist(category) + return category + end if + + return m.config.categories[category].short_title + end function + end class +end namespace diff --git a/playlet-lib/src/components/Services/SponsorBlock/SponsorBlockSegmentViewedJob.bs b/playlet-lib/src/components/Services/SponsorBlock/SponsorBlockSegmentViewedJob.bs index 9d7663d1..3209e906 100644 --- a/playlet-lib/src/components/Services/SponsorBlock/SponsorBlockSegmentViewedJob.bs +++ b/playlet-lib/src/components/Services/SponsorBlock/SponsorBlockSegmentViewedJob.bs @@ -1,3 +1,4 @@ +import "pkg:/components/Services/SponsorBlock/SponsorBlockConfig.bs" import "pkg:/components/Services/SponsorBlock/SponsorBlockService.bs" @job("SponsorBlockSegmentViewedJob") @@ -5,7 +6,7 @@ function ExecuteJob() input = JobGetInput() segmentId = input.segmentId - service = new SponsorBlock.SponsorBlockService() + service = new SponsorBlock.SponsorBlockService(new SponsorBlock.SponsorBlockConfig()) service.PostSegmentViewed(segmentId, m.top.cancellation) end function diff --git a/playlet-lib/src/components/Services/SponsorBlock/SponsorBlockSegmentsJob.bs b/playlet-lib/src/components/Services/SponsorBlock/SponsorBlockSegmentsJob.bs index 7e107276..b913e181 100644 --- a/playlet-lib/src/components/Services/SponsorBlock/SponsorBlockSegmentsJob.bs +++ b/playlet-lib/src/components/Services/SponsorBlock/SponsorBlockSegmentsJob.bs @@ -1,3 +1,4 @@ +import "pkg:/components/Services/SponsorBlock/SponsorBlockConfig.bs" import "pkg:/components/Services/SponsorBlock/SponsorBlockService.bs" import "pkg:/components/VideoPlayer/SponsorBlockSegmentAction.bs" import "pkg:/source/utils/Types.bs" @@ -11,15 +12,21 @@ function ExecuteJob() as void showNotifications = input.showNotifications categories = input.categories - service = new SponsorBlock.SponsorBlockService() + config = new SponsorBlock.SponsorBlockConfig() + service = new SponsorBlock.SponsorBlockService(config) cancellation = JobGetCancellation() skipSegments = service.GetSkipSegmentsForVideo(videoId, cancellation) - skipSegments = ProcessSkipSegments(skipSegments, showNotifications, categories, service) + skipSegments = ProcessSkipSegments(skipSegments, showNotifications, categories, config) barPath = invalid if skipSegments <> invalid - barPath = `tmp:/sponsorblock_bar.png` + barDir = "tmp:/sponsorblock" + fs = CreateObject("roFileSystem") + fs.Delete(barDir) + fs.CreateDirectory(barDir) + ' path contains video id to avoid texture cache reuse + barPath = `${barDir}/${videoId}.png` service.GenerateProgressBarBackground(skipSegments, lengthSeconds, barPath) end if @@ -42,7 +49,7 @@ function ExecuteJob() as void }) end function -function ProcessSkipSegments(skipSegments as object, showNotifications as boolean, categories as object, service as SponsorBlock.SponsorBlockService) as object +function ProcessSkipSegments(skipSegments as object, showNotifications as boolean, categories as object, config as SponsorBlock.SponsorBlockConfig) as object if skipSegments = invalid return invalid end if @@ -80,8 +87,8 @@ function ProcessSkipSegments(skipSegments as object, showNotifications as boolea segment.start = segmentStart segment.end = segmentEnd - segment.title = Tr(ValidString(service.SegmentTitle(segment.category))) - segment.shortTitle = Tr(ValidString(service.SegmentShortTitle(segment.category))) + segment.title = Tr(ValidString(config.SegmentTitle(segment.category))) + segment.shortTitle = Tr(ValidString(config.SegmentShortTitle(segment.category))) processedSegments.push(segment) end for diff --git a/playlet-lib/src/components/Services/SponsorBlock/SponsorBlockService.bs b/playlet-lib/src/components/Services/SponsorBlock/SponsorBlockService.bs index 0df8c6ab..4d928c69 100644 --- a/playlet-lib/src/components/Services/SponsorBlock/SponsorBlockService.bs +++ b/playlet-lib/src/components/Services/SponsorBlock/SponsorBlockService.bs @@ -1,5 +1,5 @@ +import "pkg:/components/Services/SponsorBlock/SponsorBlockConfig.bs" import "pkg:/source/services/HttpClient.bs" -import "pkg:/source/utils/ColorUtils.bs" import "pkg:/source/utils/CryptoUtils.bs" namespace SponsorBlock @@ -7,70 +7,13 @@ namespace SponsorBlock const VIEWED_SEGMENT_ENDPOINT = "/api/viewedVideoSponsorTime" class SponsorBlockService - function new() - ' TODO:P2 this is being read and parsed on every skipped segment. It should be cached. - m.sponsorBlockConfig = StringUtils.ParseJsonSafe(ReadAsciiFile("libpkg:/config/sponsorblock_config.json5")) - end function - - function GetCategories() as object - if m.sponsorBlockConfig = invalid - return [] - end if - - return ValidArray(m.sponsorBlockConfig.categoryList) - end function - - function GetOptionsForCategory(category as string) as object - if m.sponsorBlockConfig = invalid - return [] - end if - - if not m.sponsorBlockConfig.categories.doesexist(category) - return [] - end if - - return ValidArray(m.sponsorBlockConfig.categories[category].options) - end function - - function SegmentColor(category as string) as integer - if m.sponsorBlockConfig = invalid - return 0 - end if - - if not m.sponsorBlockConfig.categories.doesexist(category) - return 0 - end if - - color = m.sponsorBlockConfig.categories[category].color - return ColorUtils.HexToInt(color) - end function - - function SegmentTitle(category as string) as string - if m.sponsorBlockConfig = invalid - return category - end if - - if not m.sponsorBlockConfig.categories.doesexist(category) - return category - end if - - return m.sponsorBlockConfig.categories[category].title - end function - - function SegmentShortTitle(category as string) as string - if m.sponsorBlockConfig = invalid - return category - end if - - if not m.sponsorBlockConfig.categories.doesexist(category) - return category - end if - - return m.sponsorBlockConfig.categories[category].short_title + function new(config as SponsorBlock.SponsorBlockConfig) + m.config = config end function function GetSkipSegmentsForVideo(videoId as string, cancellation = invalid as dynamic) as object - if m.sponsorBlockConfig = invalid or StringUtils.IsNullOrEmpty(m.sponsorBlockConfig.serverAddress) + serverAddress = m.config.GetServerAddress() + if StringUtils.IsNullOrEmpty(serverAddress) return invalid end if @@ -84,7 +27,7 @@ namespace SponsorBlock ' From SponsorBlock docs: It should be the first 4 - 32 characters (4 is recommended). hashPrefix = CryptoUtils.GetSha256(videoId).Left(4) - url = `${m.sponsorBlockConfig.serverAddress}${SponsorBlock.SKIP_SEGMENT_ENDPOINT}/${hashPrefix}?categories=${categoriesJson}&actionTypes=${actionTypesJson}` + url = `${serverAddress}${SponsorBlock.SKIP_SEGMENT_ENDPOINT}/${hashPrefix}?categories=${categoriesJson}&actionTypes=${actionTypesJson}` response = HttpClient.Get(url).Cancellation(cancellation).Await() if not response.IsSuccess() if response.IsCancelled() @@ -112,16 +55,18 @@ namespace SponsorBlock end function function PostSegmentViewed(segmentId as string, cancellation = invalid as object) as void - if m.sponsorBlockConfig = invalid or StringUtils.IsNullOrEmpty(m.sponsorBlockConfig.serverAddress) + serverAddress = m.config.GetServerAddress() + if StringUtils.IsNullOrEmpty(serverAddress) return end if - url = `${m.sponsorBlockConfig.serverAddress}${SponsorBlock.VIEWED_SEGMENT_ENDPOINT}?UUID=${segmentId}` + url = `${serverAddress}${SponsorBlock.VIEWED_SEGMENT_ENDPOINT}?UUID=${segmentId}` HttpClient.Post(url, "").Cancellation(cancellation).Await() end function function GenerateProgressBarBackground(segments as object, lengthSeconds as integer, path as string) - bar = CreateObject("roBitmap", { width: 1000, height: 20, AlphaEnable: true }) + ' 1144 = trackbar width, so the bar renders 1:1 without horizontal scaling + bar = CreateObject("roBitmap", { width: 1144, height: 20, AlphaEnable: true }) bar.Clear(&hFFFFFF80) width = bar.GetWidth() height = bar.GetHeight() @@ -138,7 +83,7 @@ namespace SponsorBlock pixelStart = (segment.start / videoDuration) * width pixelEnd = (segment.end / videoDuration) * width - color = m.SegmentColor(segment.category) + color = m.config.SegmentColor(segment.category) ' highlight's duration is zero, so it is not visble on the bar. ' Add a few pixels to see it if pixelStart = pixelEnd @@ -150,6 +95,5 @@ namespace SponsorBlock buffer = bar.GetPng(0, 0, width, height) buffer.WriteFile(path) end function - end class end namespace diff --git a/playlet-lib/src/components/VideoPlayer/SponsorBlock.bs b/playlet-lib/src/components/VideoPlayer/SponsorBlock.bs index 3dd64577..5da304db 100644 --- a/playlet-lib/src/components/VideoPlayer/SponsorBlock.bs +++ b/playlet-lib/src/components/VideoPlayer/SponsorBlock.bs @@ -1,7 +1,6 @@ import "pkg:/components/JobSystem/Jobs.bs" import "pkg:/components/JobSystem/JobSystem.bs" import "pkg:/components/Services/SponsorBlock/Notifications/SponsorBlockNotificationUtils.bs" -import "pkg:/components/Services/SponsorBlock/SponsorBlockService.bs" import "SponsorBlockSegmentAction.bs" import "VideoPlayer.bs" @@ -12,7 +11,6 @@ namespace SponsorBlock return end if - m.sponsorBlockService = new SponsorBlock.SponsorBlockService() SponsorBlock.SetVisible(m.notifications, m.container.fullscreen) end function diff --git a/playlet-lib/src/components/VideoPlayerDev/TrickPlayBar/TrickPlayBar.bs b/playlet-lib/src/components/VideoPlayerDev/TrickPlayBar/TrickPlayBar.bs index de3d3cec..15363a9c 100644 --- a/playlet-lib/src/components/VideoPlayerDev/TrickPlayBar/TrickPlayBar.bs +++ b/playlet-lib/src/components/VideoPlayerDev/TrickPlayBar/TrickPlayBar.bs @@ -44,7 +44,6 @@ function Repaint() as void end if end function -' --- VOD ------------------------------------------------------------------------------------------------ function RenderVodBar() as void transportActive = (m.top.transportMode <> TransportController.Mode.Idle) if transportActive @@ -78,7 +77,6 @@ function RenderVodBar() as void m.liveLabel.visible = false end function -' --- LIVE ----------------------------------------------------------------------------------------------- function RenderLiveBar() as void transportActive = (m.top.transportMode = TransportController.Mode.LiveDvr) @@ -160,7 +158,6 @@ function OnFocusChange() as void m.playheadFocus.control = "start" end function -' --- glyph swap (handle child) -------------------------------------------------------------------------- function OnGlyph() as void name = GlyphAssetName(m.top.glyph) if name = "" @@ -194,7 +191,6 @@ function GlyphAssetName(glyph as integer) as string return "" end function -' --- SponsorBlock fill tint ----------------------------------------------------------------------------- ' A SponsorBlock tint overrides the red fill; an empty (fully transparent) value keeps the red. function OnSbFillBlend() as void blend = m.top.sbFillBlend @@ -203,7 +199,6 @@ function OnSbFillBlend() as void end if end function -' --- time format ---------------------------------------------------------------------------------------- ' under 1min -> "0:SS"; else "MM:SS"; "H:MM:SS" when >= 1hr. function FormatBarTime(seconds as dynamic) as string if seconds = invalid