119 lines
3.4 KiB
Plaintext
119 lines
3.4 KiB
Plaintext
import "LoadingMessages.bs"
|
|
import "pkg:/source/Logger.bs"
|
|
|
|
function Init() as void
|
|
m.scene = m.top.getScene()
|
|
m.top.backgroundColor = "#242424FF"
|
|
m.top.backgroundURI = ""
|
|
|
|
m.loadingIndicator = m.top.findNode("LoadingIndicator")
|
|
m.global.addFields({
|
|
loadingIndicator: m.loadingIndicator
|
|
})
|
|
|
|
m.loadingIndicator.visible = true
|
|
m.loadingIndicator.message = GetLoadingMessage()
|
|
|
|
m.playletLibUrls = GetPlayletLibUrls()
|
|
m.playletLibUrlIndex = 0
|
|
|
|
LoadPlayletLib()
|
|
end function
|
|
|
|
function GetPlayletLibUrls() as object
|
|
#if DEBUG
|
|
debug = { type: "debug", link: Manifest.ReadValue("playlet_lib_debug_url") }
|
|
embedded = { type: "embedded", link: Manifest.ReadValue("playlet_lib_embedded_url") }
|
|
#if DEBUG_FALLBACK_TO_EMBEDDED
|
|
return [debug, embedded]
|
|
#else
|
|
return [debug]
|
|
#end if
|
|
#else
|
|
savedUrls = Registry.GetPlayletLibUrlsFromRegistry()
|
|
if savedUrls <> invalid
|
|
m.shouldClearRegistryOnLoadFail = true
|
|
return savedUrls
|
|
end if
|
|
|
|
github = { type: "github", link: Manifest.ReadValue("playlet_lib_remote_url") }
|
|
embedded = { type: "embedded", link: Manifest.ReadValue("playlet_lib_embedded_url") }
|
|
return [github, embedded]
|
|
#end if
|
|
end function
|
|
|
|
function LoadPlayletLib() as void
|
|
if m.playletLibUrlIndex >= m.playletLibUrls.Count()
|
|
m.loadingIndicator.visible = false
|
|
if m.shouldClearRegistryOnLoadFail = true
|
|
Registry.DeleteKey("playlet_lib_urls", "Playlet")
|
|
end if
|
|
ShowPlayletLibLoadErrorDialog()
|
|
return
|
|
end if
|
|
|
|
RemoveComponentLibrary()
|
|
uri = m.playletLibUrls[m.playletLibUrlIndex].link
|
|
LoadComponentLibrary(uri)
|
|
end function
|
|
|
|
function OnLoadStatusChanged() as void
|
|
libUrl = m.playletLibUrls[m.playletLibUrlIndex]
|
|
if m.lib.loadStatus = "ready"
|
|
RemoveComponentLibrary()
|
|
Logger.Log(`Playlet lib loaded from ${libUrl.link} in ${m.timer.TotalMilliseconds()} ms`)
|
|
m.timer = invalid
|
|
m.scene.playletLibLoadedUrl = libUrl
|
|
container = m.scene.findNode("MainSceneContainer")
|
|
container.createChild("PlayletLib:MainScene")
|
|
return
|
|
end if
|
|
|
|
if m.lib.loadStatus = "failed"
|
|
Logger.Log(`Playlet lib failed to load from ${libUrl.link}`)
|
|
' Try next url
|
|
m.playletLibUrlIndex += 1
|
|
LoadPlayletLib()
|
|
end if
|
|
end function
|
|
|
|
function RemoveComponentLibrary() as void
|
|
if m.lib = invalid
|
|
return
|
|
end if
|
|
|
|
m.lib.unobserveField("loadStatus")
|
|
m.scene.removeChild(m.lib)
|
|
m.lib = invalid
|
|
end function
|
|
|
|
function LoadComponentLibrary(uri as string)
|
|
m.timer = CreateObject("roTimespan")
|
|
m.timer.mark()
|
|
|
|
m.lib = m.scene.createChild("ComponentLibrary")
|
|
' bs:disable-next-line NO_OBSERVE_STRING_CALLBACK
|
|
m.lib.observeField("loadStatus", "OnLoadStatusChanged")
|
|
|
|
Logger.Log(`Loading Playlet lib from ${uri}`)
|
|
m.lib.uri = uri
|
|
end function
|
|
|
|
function ShowPlayletLibLoadErrorDialog()
|
|
message = [
|
|
"Could not load Playlet component library from any of the following urls:",
|
|
]
|
|
|
|
bulletText = []
|
|
for each url in m.playletLibUrls
|
|
bulletText.push("[" + url.type + "] " + url.link)
|
|
end for
|
|
|
|
bottomText = [
|
|
"Please restart Playlet.",
|
|
"If the problem persist, contact Playlet authors."
|
|
]
|
|
|
|
DialogShowError(message, bulletText, bottomText)
|
|
end function
|