| 1 | package com.longtailvideo.jwplayer.controller { |
|---|
| 2 | import com.longtailvideo.jwplayer.utils.AssetLoader; |
|---|
| 3 | import com.longtailvideo.jwplayer.utils.Strings; |
|---|
| 4 | |
|---|
| 5 | import flash.display.DisplayObject; |
|---|
| 6 | import flash.events.ErrorEvent; |
|---|
| 7 | import flash.events.Event; |
|---|
| 8 | import flash.events.EventDispatcher; |
|---|
| 9 | import flash.utils.Dictionary; |
|---|
| 10 | |
|---|
| 11 | /** |
|---|
| 12 | * Sent when the plugin loader has loaded all valid plugins. |
|---|
| 13 | * |
|---|
| 14 | * @eventType flash.events.Event.COMPLETE |
|---|
| 15 | */ |
|---|
| 16 | [Event(name="complete", type = "flash.events.Event")] |
|---|
| 17 | |
|---|
| 18 | /** |
|---|
| 19 | * Sent when an error occured during plugin loading. |
|---|
| 20 | * |
|---|
| 21 | * @eventType flash.events.ErrorEvent.ERROR |
|---|
| 22 | */ |
|---|
| 23 | [Event(name="error", type = "flash.events.ErrorEvent")] |
|---|
| 24 | |
|---|
| 25 | |
|---|
| 26 | /** |
|---|
| 27 | * Loads plugins during player startup |
|---|
| 28 | * |
|---|
| 29 | * @author Pablo Schklowsky |
|---|
| 30 | */ |
|---|
| 31 | public class PluginLoader extends EventDispatcher { |
|---|
| 32 | |
|---|
| 33 | public var plugins:Object; |
|---|
| 34 | |
|---|
| 35 | private var loaders:Dictionary; |
|---|
| 36 | |
|---|
| 37 | protected function get pluginRepository():String { return "http://plugins.longtailvideo.com/"; } |
|---|
| 38 | |
|---|
| 39 | public function PluginLoader() { |
|---|
| 40 | loaders = new Dictionary(); |
|---|
| 41 | plugins = {}; |
|---|
| 42 | } |
|---|
| 43 | |
|---|
| 44 | public function loadPlugins(pluginList:String):void { |
|---|
| 45 | if (pluginList) { |
|---|
| 46 | var plugins:Array = pluginList.replace(/\s*/g,"").split(","); |
|---|
| 47 | for each(var plugin:String in plugins) { |
|---|
| 48 | if (plugin){ |
|---|
| 49 | loadLocalPlugin(plugin); //Testing |
|---|
| 50 | } |
|---|
| 51 | } |
|---|
| 52 | } else { |
|---|
| 53 | dispatchEvent(new Event(Event.COMPLETE)); |
|---|
| 54 | } |
|---|
| 55 | } |
|---|
| 56 | |
|---|
| 57 | private function loadLocalPlugin(plugin:String):void { |
|---|
| 58 | if (plugin.indexOf("/") >= 0) { |
|---|
| 59 | var loader:AssetLoader = new AssetLoader(); |
|---|
| 60 | loader.addEventListener(Event.COMPLETE, loadSuccess); |
|---|
| 61 | loader.addEventListener(ErrorEvent.ERROR, loadLocalFailed); |
|---|
| 62 | loaders[loader] = plugin; |
|---|
| 63 | loader.load(plugin); |
|---|
| 64 | } else { |
|---|
| 65 | loadV5Plugin(plugin); |
|---|
| 66 | } |
|---|
| 67 | } |
|---|
| 68 | |
|---|
| 69 | private function loadLocalFailed(evt:ErrorEvent):void { |
|---|
| 70 | var loader:AssetLoader = evt.target as AssetLoader; |
|---|
| 71 | var plugin:String = loaders[loader]; |
|---|
| 72 | loader.removeEventListener(ErrorEvent.ERROR, loadLocalFailed); |
|---|
| 73 | delete loaders[loader]; |
|---|
| 74 | checkComplete(); |
|---|
| 75 | } |
|---|
| 76 | |
|---|
| 77 | private function loadV5Plugin(plugin:String):void { |
|---|
| 78 | var loader:AssetLoader = new AssetLoader(); |
|---|
| 79 | loader.addEventListener(Event.COMPLETE, loadSuccess); |
|---|
| 80 | loader.addEventListener(ErrorEvent.ERROR, loadV5Failed); |
|---|
| 81 | |
|---|
| 82 | var split:Array = plugin.substr(plugin.lastIndexOf("/")+1).replace(/(.*)\.swf$/i, "$1").split("-"); |
|---|
| 83 | var name:String = split[0]; |
|---|
| 84 | var version:String = split.length > 1 ? ("-" + split[1]) : ""; |
|---|
| 85 | var url:String = pluginRepository + "5/" + name + "/" + name + version + ".swf"; |
|---|
| 86 | |
|---|
| 87 | loaders[loader] = plugin; |
|---|
| 88 | loader.load(url); |
|---|
| 89 | } |
|---|
| 90 | |
|---|
| 91 | private function loadV5Failed(evt:ErrorEvent):void { |
|---|
| 92 | var loader:AssetLoader = evt.target as AssetLoader; |
|---|
| 93 | var url:String = loaders[loader] as String; |
|---|
| 94 | loader.removeEventListener(ErrorEvent.ERROR, loadV5Failed); |
|---|
| 95 | delete loaders[loader]; |
|---|
| 96 | loadV4Plugin(url); |
|---|
| 97 | } |
|---|
| 98 | |
|---|
| 99 | private function loadV4Plugin(plugin:String):void { |
|---|
| 100 | var loader:AssetLoader = new AssetLoader(); |
|---|
| 101 | loader.addEventListener(Event.COMPLETE, loadSuccess); |
|---|
| 102 | loader.addEventListener(ErrorEvent.ERROR, loadV4Failed); |
|---|
| 103 | loaders[loader] = plugin; |
|---|
| 104 | |
|---|
| 105 | if (Strings.extension(plugin) == "swf") { plugin = plugin.substring(0, plugin.length-4); } |
|---|
| 106 | loader.load(pluginRepository + '4/' + plugin + ".swf"); |
|---|
| 107 | } |
|---|
| 108 | |
|---|
| 109 | private function loadV4Failed(evt:ErrorEvent):void { |
|---|
| 110 | var loader:AssetLoader = evt.target as AssetLoader; |
|---|
| 111 | delete loaders[loader]; |
|---|
| 112 | checkComplete(); |
|---|
| 113 | } |
|---|
| 114 | |
|---|
| 115 | private function loadSuccess(evt:Event):void { |
|---|
| 116 | var loader:AssetLoader = evt.target as AssetLoader; |
|---|
| 117 | var url:String = loaders[loader] as String; |
|---|
| 118 | var pluginId:String = url.substr(url.lastIndexOf("/")+1).replace(/(.*)\.swf$/i, "$1").split("-")[0]; |
|---|
| 119 | plugins[pluginId] = loader.loadedObject as DisplayObject; |
|---|
| 120 | loader.removeEventListener(Event.COMPLETE, loadSuccess); |
|---|
| 121 | delete loaders[loader]; |
|---|
| 122 | checkComplete(); |
|---|
| 123 | } |
|---|
| 124 | |
|---|
| 125 | private function checkComplete():void { |
|---|
| 126 | var waiting:Boolean = false; |
|---|
| 127 | for each(var remaining:String in loaders) { |
|---|
| 128 | // Still waiting for some plugins to load |
|---|
| 129 | waiting = true; |
|---|
| 130 | continue; |
|---|
| 131 | } |
|---|
| 132 | |
|---|
| 133 | if (!waiting) { |
|---|
| 134 | dispatchEvent(new Event(Event.COMPLETE)); |
|---|
| 135 | } |
|---|
| 136 | } |
|---|
| 137 | |
|---|
| 138 | |
|---|
| 139 | } |
|---|
| 140 | } |
|---|