public virtual IList<ItemToolboxNode> Load (LoaderContext ctx, string filename)
		{
			SystemPackage sp = Runtime.SystemAssemblyService.DefaultAssemblyContext.GetPackageFromPath (filename);
			ReferenceType rt;
			string rname;
			
			if (sp != null) {
				rt = ReferenceType.Package;
				rname = Runtime.SystemAssemblyService.DefaultAssemblyContext.GetAssemblyFullName (filename, null);
			} else {
				rt = ReferenceType.Assembly;
				rname = filename;
			}
			
			List<ItemToolboxNode> list = new List<ItemToolboxNode> ();
			var types = Runtime.RunInMainThread (delegate {
				// Stetic is not thread safe, it has to be used from the gui thread
				return GuiBuilderService.SteticApp.GetComponentTypes (filename);
			}).Result;
			foreach (ComponentType ct in types) {
				if (ct.Category == "window")
					continue;
				ComponentToolboxNode cn = new ComponentToolboxNode (ct);
				cn.ReferenceType = rt;
				cn.Reference = rname;
				list.Add (cn);
			}
			return list;
		}
		public IList<ItemToolboxNode> Load (LoaderContext ctx, string filename)
		{
			List<ItemToolboxNode> items = new List<ItemToolboxNode> ();
			foreach (TargetRuntime runtime in GetSupportedRuntimes (filename)) {
				items.AddRange (ctx.LoadItemsIsolated (runtime, GetType (), filename));
			}
			return items;
		}
		public ComponentIndexFile AddFile (string file)
		{
			ComponentIndexFile cf = new ComponentIndexFile (file);
			LoaderContext ctx = new LoaderContext ();
			try {
				cf.Update (ctx);
			} finally {
				ctx.Dispose ();
			}
			if (cf.Components.Count == 0)
				return null;
			else {
				files.Add (cf);
				return cf;
			}
		}
		public void Update (LoaderContext ctx)
		{
			IList<ItemToolboxNode> items = DesignerSupport.Service.ToolboxService.GetFileItems (ctx, fileName);
			
			location = null;
			this.timestamp = GetFileTimestamp (fileName);
			entries = new List<ItemToolboxNode> (items);
			
			// Set the location field only if this is a widget library
			if (entries.Count > 0 && Runtime.SystemAssemblyService.GetPackageFromPath (fileName) != null)
				location = Runtime.SystemAssemblyService.DefaultAssemblyContext.GetAssemblyFullName (fileName, TargetFramework.Default);
		}
		internal IList<ItemToolboxNode> GetFileItems (LoaderContext ctx, string fileName)
		{
			// Gets the list of items provided by a file.
			List<ItemToolboxNode> items = new List<ItemToolboxNode> ();
			
			foreach (IToolboxLoader loader in loaders) {
				//check whether the loader can handle this file extension
				bool match = false;
				foreach (string ext in loader.FileTypes) {
					if (fileName.EndsWith (ext)) {
						match = true;
						break;
					}
				}
				if (!match)
					continue;
				
				if (!fileName.EndsWith (loader.FileTypes[0]))
					continue;
				
				try {
					IList<ItemToolboxNode> loadedItems = loader.Load (ctx, fileName);
					items.AddRange (loadedItems);
				}
				catch (Exception ex) {
					// Ignore
					LoggingService.LogError (ex.ToString ());
				}
			}
			return items;
		}
		internal ComponentIndex GetComponentIndex (ProgressMonitor monitor)
		{
			// Returns an index of all components that can be added to the toolbox.
			
			ComponentIndex index = ComponentIndex.Load ();
			
			// Get the list of assemblies that need to be updated
			
			HashSet<string> files = new HashSet<string> ();
			List<ComponentIndexFile> toupdate = new List<ComponentIndexFile> ();
			List<ComponentIndexFile> todelete = new List<ComponentIndexFile> ();
			foreach (ComponentIndexFile ia in index.Files) {
				files.Add (ia.FileName);
				if (!File.Exists (ia.FileName))
					todelete.Add (ia);
				if (ia.NeedsUpdate)
					toupdate.Add (ia);
				if (monitor.CancellationToken.IsCancellationRequested)
					return index;
			}
			
			// Look for new assemblies
			
			foreach (TargetRuntime runtime in Runtime.SystemAssemblyService.GetTargetRuntimes ()) {
				foreach (SystemAssembly asm in runtime.AssemblyContext.GetAssemblies ()) {
					if (files.Add (asm.Location)) {
						ComponentIndexFile c = new ComponentIndexFile (asm.Location);
						index.Files.Add (c);
						toupdate.Add (c);
					}
					if (monitor.CancellationToken.IsCancellationRequested)
						return index;
				}
			}
			
			foreach (ComponentIndexFile ia in todelete) {
				index.Files.Remove (ia);
			}
			
			if (toupdate.Count > 0) {
				monitor.BeginTask (GettextCatalog.GetString ("Looking for components..."), toupdate.Count);
				LoaderContext ctx = new LoaderContext ();
				try {
					foreach (ComponentIndexFile ia in toupdate) {
						ia.Update (ctx);
						monitor.Step (1);
						if (monitor.CancellationToken.IsCancellationRequested)
							return index;
					}
				} finally {
					ctx.Dispose ();
					monitor.EndTask ();
				}
			}
			
			if (toupdate.Count > 0 || todelete.Count > 0)
				index.Save ();
			
			return index;
		}
		public IList<ItemToolboxNode> Load (LoaderContext ctx, string filename)
		{
			Mono.Linker.AssemblyResolver resolver = new Mono.Linker.AssemblyResolver ();
			MC.AssemblyDefinition assem = MC.AssemblyDefinition.ReadAssembly (filename, new MC.ReaderParameters { AssemblyResolver = resolver });

			//Grab types that should be in System.dll
			Version runtimeVersion;
			switch (assem.MainModule.Runtime) {
			case MC.TargetRuntime.Net_1_0:
			case MC.TargetRuntime.Net_1_1:
				runtimeVersion = new Version (1, 0, 5000, 0); break;
			case MC.TargetRuntime.Net_2_0:
				runtimeVersion = new Version (2, 0, 0, 0); break;
			default:
				throw new NotSupportedException ("Runtime '" + assem.MainModule.Runtime + "' is not supported.");
			}
			
			MC.AssemblyNameReference sysAssem = new MC.AssemblyNameReference ("System", runtimeVersion);
			MC.TypeReference toolboxItemType 
				= new MC.TypeReference ("System.ComponentModel", "ToolboxItemAttribute", assem.MainModule, sysAssem, false);
			MC.TypeReference categoryType 
				= new MC.TypeReference ("ToolboxItemAttribute", "CategoryAttribute", assem.MainModule, sysAssem, false);
			
			if (resolver.Resolve (toolboxItemType) == null)
				return null;
			
			List<ItemToolboxNode> list = new List<ItemToolboxNode> ();
			
			foreach (MC.ModuleDefinition module in assem.Modules) {
				foreach (MC.TypeDefinition typedef in module.Types) {
					//only show visible concrete classes
					if (typedef.IsAbstract || !typedef.IsClass ||!typedef.IsPublic)
						continue;
					
					MC.TypeDefinition toolboxItem = null;
					string category = null;
					
					//find the ToolboxItem and Category attributes
					foreach (MC.CustomAttribute att in AllCustomAttributes (resolver, typedef)) {
						
						//find the value of the toolbox attribute
						if (toolboxItem == null
						    && IsEqualOrSubclass (resolver, att.Constructor.DeclaringType, toolboxItemType)) {
							
							//check if it's ToolboxItemAttribute(false) (or null)

							IList args = GetArgumentsToBaseConstructor (att, toolboxItemType);
							if (args != null) {
								if (args.Count != 1)
									throw new InvalidOperationException ("Malformed toolboxitem attribute");
								object o = args[0];	
								if (o == null || (o is bool && ((bool)o) == false)) {
									break;
								} else {
									//w have a type name for the toolbox item. try to resolve it
									string typeName = (string) o;
									toolboxItem = null;
									try {
										resolver.Resolve (TypeReferenceFromString (module, typeName));
									} catch (Exception ex) {
										System.Diagnostics.Debug.WriteLine (ex);
									}
									if (toolboxItem == null) {
										System.Diagnostics.Debug.WriteLine (
										    "CecilToolboxItemScanner: Error resolving type "
										    + typeName);
										break;
									}
								}
							}
						}
						
						//find the value of the category attribute
						if (category == null
						    && IsEqualOrSubclass (resolver, att.Constructor.DeclaringType, categoryType)) {

							IList args = GetArgumentsToBaseConstructor (att, categoryType);
							if (args != null && args.Count == 1)
								category = (string) args[0];
							else
								throw new InvalidOperationException ("Malformed category attribute");
						}
						
						if (toolboxItem != null && category != null)
							break;
					}
					
					if (toolboxItem == null)
						continue;
					
					Load (assem, typedef, toolboxItem, category);
				}
			}
			
			return list;
		}
		public void RegisterDefaultToolboxProvider (IToolboxDefaultProvider provider)
		{
			string pname = provider.GetType().FullName;
			
			if (!Configuration.LoadedDefaultProviders.Contains (pname)) {
				Configuration.LoadedDefaultProviders.Add (pname);
				initializing++;
				OnToolboxContentsChanged ();

				System.Threading.ThreadPool.QueueUserWorkItem (delegate {
					List<ItemToolboxNode> nodes = new List<ItemToolboxNode> ();
					try {
						IEnumerable<ItemToolboxNode> newItems = provider.GetDefaultItems ();
						if (newItems != null)
							nodes.AddRange (newItems);
					} catch (Exception ex) {
						LoggingService.LogError ("Error getting default items from a IToolboxDefaultProvider", ex);
					}

					LoaderContext ctx = null;
					try {
						IEnumerable<string> files = provider.GetDefaultFiles ();
						if (files != null) {
							ctx = new LoaderContext ();
							foreach (string f in files)
								nodes.AddRange (GetFileItems (ctx, f));
						}
					} finally {
						if (ctx != null)
							ctx.Dispose ();
					}

					Runtime.RunInMainThread (delegate {
						AddUserItems (nodes);
						initializing--;
						SaveConfiguration ();
						OnToolboxContentsChanged ();
					});
				});
			}
		}
Example #9
0
 /// <summary>
 /// Loads a SWF, JPEG, progressive JPEG, unanimated GIF, or PNG file into an object that is a child of this Loader object.
 /// </summary>
 public void load(URLRequest request, LoaderContext context)
 {
     return;
 }
Example #10
0
 /// <summary>
 /// Loads from binary data stored in a <see cref="ByteArray"/> object.
 /// </summary>
 public void loadBytes(ByteArray bytes, LoaderContext context)
 {
     return;
 }
Example #11
0
        internal async Task <ComponentIndex> GetComponentIndex(ProgressMonitor monitor)
        {
            // Returns an index of all components that can be added to the toolbox.

            ComponentIndex index = ComponentIndex.Load();

            // Get the list of assemblies that need to be updated

            HashSet <string>          files    = new HashSet <string> ();
            List <ComponentIndexFile> toupdate = new List <ComponentIndexFile> ();
            List <ComponentIndexFile> todelete = new List <ComponentIndexFile> ();

            foreach (ComponentIndexFile ia in index.Files)
            {
                files.Add(ia.FileName);
                if (!File.Exists(ia.FileName))
                {
                    todelete.Add(ia);
                }
                if (ia.NeedsUpdate)
                {
                    toupdate.Add(ia);
                }
                if (monitor.CancellationToken.IsCancellationRequested)
                {
                    return(index);
                }
            }

            // Look for new assemblies

            foreach (TargetRuntime runtime in Runtime.SystemAssemblyService.GetTargetRuntimes())
            {
                foreach (SystemAssembly asm in runtime.AssemblyContext.GetAssemblies())
                {
                    if (files.Add(asm.Location))
                    {
                        ComponentIndexFile c = new ComponentIndexFile(asm.Location);
                        index.Files.Add(c);
                        toupdate.Add(c);
                    }
                    if (monitor.CancellationToken.IsCancellationRequested)
                    {
                        return(index);
                    }
                }
            }

            foreach (ComponentIndexFile ia in todelete)
            {
                index.Files.Remove(ia);
            }

            if (toupdate.Count > 0)
            {
                monitor.BeginTask(GettextCatalog.GetString("Looking for components..."), toupdate.Count);
                LoaderContext ctx = new LoaderContext();
                try {
                    await Task.Run(() => {
                        foreach (ComponentIndexFile ia in toupdate)
                        {
                            ia.Update(ctx);
                            monitor.Step(1);
                            if (monitor.CancellationToken.IsCancellationRequested)
                            {
                                return;
                            }
                        }
                    });

                    if (monitor.CancellationToken.IsCancellationRequested)
                    {
                        return(index);
                    }
                } finally {
                    ctx.Dispose();
                    monitor.EndTask();
                }
            }

            if (toupdate.Count > 0 || todelete.Count > 0)
            {
                index.Save();
            }

            return(index);
        }
Example #12
0
        public void RegisterDefaultToolboxProvider(IToolboxDefaultProvider provider)
        {
            string pname = provider.GetType().FullName;

            if (!Configuration.LoadedDefaultProviders.Contains(pname))
            {
                Configuration.LoadedDefaultProviders.Add(pname);
                initializing++;
                OnToolboxContentsChanged();

                System.Threading.ThreadPool.QueueUserWorkItem(delegate {
                    if (!Runtime.Initialized)
                    {
                        return;
                    }
                    List <ItemToolboxNode> nodes = new List <ItemToolboxNode> ();
                    try {
                        IEnumerable <ItemToolboxNode> newItems = provider.GetDefaultItems();
                        if (newItems != null)
                        {
                            nodes.AddRange(newItems);
                        }
                    } catch (Exception ex) {
                        LoggingService.LogError("Error getting default items from a IToolboxDefaultProvider", ex);
                    }

                    LoaderContext ctx = null;
                    try {
                        IEnumerable <string> files = provider.GetDefaultFiles();
                        if (files != null)
                        {
                            ctx = new LoaderContext();
                            foreach (string f in files)
                            {
                                if (ctx.CancellationToken.IsCancellationRequested)
                                {
                                    break;
                                }
                                nodes.AddRange(GetFileItems(ctx, f));
                            }
                        }
                    } finally {
                        if (ctx != null)
                        {
                            ctx.Dispose();
                        }
                    }

                    if (!Runtime.Initialized)
                    {
                        return;
                    }

                    Runtime.RunInMainThread(delegate {
                        AddUserItems(nodes);
                        initializing--;
                        SaveConfiguration();
                        OnToolboxContentsChanged();
                    });
                });
            }
        }
Example #13
0
        internal DbConnection GetConnection()
        {
            if (_DbConnection == null)
            {
                //Create connection
                string moduleRoot = System.IO.Path.Combine(KraftGlobalConfigurationSettings.GeneralSettings.ModulesRootFolder(ProcessingContext.InputModel.Module), ProcessingContext.InputModel.Module);
                // Support for @moduleroot@ variable replacement for connection strings that refer to file(s)
                string connectionString = (CustomSettings != null && CustomSettings.ContainsKey("ConnectionString"))
                         ? CustomSettings["ConnectionString"].Replace("@moduleroot@", moduleRoot)
                         : null;

                if (string.IsNullOrEmpty(connectionString))
                {
                    throw new NullReferenceException("The Connection String must not be null or empty.");
                }

                // MatchCollection matches = _DynamicParameterRegEx.Matches(connectionString);
                connectionString = _DynamicParameterRegEx.Replace(connectionString, m => {
                    string varname = m.Groups[1].Value;
                    var val        = LoaderContext.Evaluate(varname);
                    if (val.ValueType == EResolverValueType.Invalid)
                    {
                        KraftLogger.LogError($"Expected parameter in connection string: {m.Groups[1].Value} was not resolved! Check that parameter's expression. It is recommended to not define it on node basis, but only in a nodeset root!");
                        // TODO: What shall we return on error? This is temporary decision - there should be something better or just excepton.
                        return(m.Value);
                    }
                    if (!string.IsNullOrWhiteSpace(val.Value + ""))
                    {
                        return(val.Value.ToString());
                    }
                    else
                    {
                        KraftLogger.LogError($"Expected parameter in connection string: {m.Groups[1].Value} was not found or cannot be resolved!");
                        return(m.Value);
                    }
                });

                /*if (matches.Count > 0) {
                 *  for (int i = 0; i < matches.Count; i++) {
                 *
                 *      string parameter = matches[i].Groups["OnlyParameter"].ToString();
                 *
                 *      if (ProcessingContext.InputModel.Data.ContainsKey(parameter)) {
                 *
                 *          connectionString = connectionString.Replace(matches[i].ToString(), ProcessingContext.InputModel.Data[parameter].ToString());
                 *      }
                 *      else if (ProcessingContext.InputModel.Client.ContainsKey(parameter)) {
                 *
                 *          connectionString = connectionString.Replace(matches[i].ToString(), ProcessingContext.InputModel.Client[parameter].ToString());
                 *      }
                 *      else {
                 *
                 *          KraftLogger.LogError($"Expected parameter in connection string: {matches[i]} was not found and the connection string remains invalid! Please consider the casing!");
                 *          //connectionString = connectionString.Replace(matches[i].ToString(), string.Empty);
                 *      }
                 *  }
                 * }*/

                _DbConnection = KraftProfiler.Current.ProfiledDbConnection(new XConnection());
                _DbConnection.ConnectionString = connectionString;
            }
            if (_DbConnection.State != ConnectionState.Open)
            {
                _DbConnection.Open();
            }
            return(_DbConnection);
        }
Example #14
0
        /// <summary>
        /// Creates a server configuration from a CLI config instance
        /// </summary>
        /// <returns>The server configuration.</returns>
        /// <param name="config">The CLI config instance.</param>
        public static ServerConfig ValidateConfig(CLIServerConfiguration config)
        {
            var cfg = CreateServerConfig(config);

            // Set up the module context
            var ctx = LoaderContext.StartContext();

            cfg.LoaderContext = ctx;

            // Expose our configuration to allow modules to call back,
            // beware that these values should never be reported to the client
            // as there might be proxies in front which will make them useless
            // to clients not running on the local machine
            if (config.ListenHttp)
            {
                var parsedaddr = ParseUtil.ParseEndPoint(config.HttpAddress, config.HttpPort);
                if (parsedaddr is IPEndPoint ipe)
                {
                    var ip = ipe.Address == IPAddress.Any ? "127.0.0.1" : ipe.Address.ToString();
                    Environment.SetEnvironmentVariable("CEEN_SELF_HTTP_URL", "http://" + ip + ":" + ipe.Port);
                }
                // else
                // {
                //  Environment.SetEnvironmentVariable("CEEN_SELF_HTTP_URL", config.HttpAddress);
                // }
            }
            if (config.ListenHttps)
            {
                var parsedaddr = ParseUtil.ParseEndPoint(config.HttpsAddress, config.HttpsPort);
                if (parsedaddr is IPEndPoint ipe)
                {
                    var ip = ipe.Address == IPAddress.Any ? "127.0.0.1" : ipe.Address.ToString();
                    Environment.SetEnvironmentVariable("CEEN_SELF_HTTPS_URL", "http://" + ip + ":" + ipe.Port);
                }
                // else
                // {
                //  Environment.SetEnvironmentVariable("CEEN_SELF_HTTPS_URL", config.HttpsAddress);
                // }
            }

            if (config.AutoLoadAssemblies)
            {
                // Workaround for unittests not providing EntryAssembly
                var callingasm = Assembly.GetEntryAssembly();
                var callingdir = callingasm == null ? null : Path.GetDirectoryName(callingasm.Location);

                var paths = (config.Assemblypath ?? string.Empty)
                            .Split(new char[] { Path.PathSeparator }, StringSplitOptions.RemoveEmptyEntries)
                            .Union(new[] { config.Basepath, callingdir })
                            .Where(x => !string.IsNullOrWhiteSpace(x) && Directory.Exists(x))
                            .Distinct();

                foreach (var p in paths)
                {
                    foreach (var file in Directory.GetFiles(p, "*.*", SearchOption.TopDirectoryOnly))
                    {
                        if (file.EndsWith(".dll", StringComparison.OrdinalIgnoreCase) || file.EndsWith(".exe", StringComparison.OrdinalIgnoreCase))
                        {
                            try { Assembly.LoadFrom(file); }
                            catch { }
                        }
                    }
                }

                var ceenpath = Path.GetDirectoryName(typeof(CLIServerConfiguration).Assembly.Location);
                if (Directory.Exists(ceenpath))
                {
                    foreach (var dll in Directory.GetFiles(ceenpath, "Ceen.*.dll", SearchOption.TopDirectoryOnly))
                    {
                        try { Assembly.LoadFrom(dll); }
                        catch { }
                    }
                }
            }

            if (config.Loggers != null)
            {
                foreach (var logger in config.Loggers)
                {
                    var inst = CreateInstance(logger.Classname, logger.ConstructorArguments, typeof(ILogger));
                    if (logger.Options != null)
                    {
                        SetProperties(inst, logger.Options);
                    }

                    if (inst is IWithSetup mse)
                    {
                        mse.AfterConfigure();
                    }

                    cfg.AddLogger((ILogger)inst);
                }
            }

            if (config.Modules != null)
            {
                foreach (var module in config.Modules)
                {
                    object handler;
                    var    moduletype = ResolveType(module.Classname);
                    handler = CreateInstance(moduletype, module.ConstructorArguments, typeof(IModule));

                    if (module.Options != null)
                    {
                        SetProperties(handler, module.Options);
                    }

                    if (handler is IWithSetup mse)
                    {
                        mse.AfterConfigure();
                    }


                    cfg.AddModule((IModule)handler);
                }
            }

            if (config.PostProcessors != null)
            {
                foreach (var postProcessor in config.PostProcessors)
                {
                    var inst = CreateInstance(postProcessor.Classname, postProcessor.ConstructorArguments, typeof(IPostProcessor));
                    if (postProcessor.Options != null)
                    {
                        SetProperties(inst, postProcessor.Options);
                    }

                    if (inst is IWithSetup mse)
                    {
                        mse.AfterConfigure();
                    }

                    cfg.AddPostProcessor((IPostProcessor)inst);
                }
            }

            if (config.Routes != null)
            {
                Ceen.Mvc.ManualRoutingController wirecontroller       = null;
                Ceen.Mvc.ControllerRouterConfig  wirecontrollerconfig = null;

                foreach (var route in config.Routes)
                {
                    // If we have a wired controller, and a non-wire item
                    // add it now to ensure the order is maintained
                    if (wirecontroller != null && !(route is WiredRouteDefinition))
                    {
                        cfg.AddRoute(wirecontroller.ToRoute(wirecontrollerconfig));
                        wirecontroller = null;
                    }

                    // Check for wireups
                    if (route is WiredRouteDefinition wired)
                    {
                        // Make a new manual router, if none is present or if we add one
                        if (wirecontroller == null || (wired.RoutePrefix == null))
                        {
                            // Add any previous wireup to the routes
                            if (wirecontroller != null)
                            {
                                cfg.AddRoute(wirecontroller.ToRoute(wirecontrollerconfig));
                            }

                            wirecontrollerconfig = new Ceen.Mvc.ControllerRouterConfig();
                            if (route.Options != null)
                            {
                                SetProperties(wirecontrollerconfig, route.Options);
                            }
                            wirecontroller = new Ceen.Mvc.ManualRoutingController();
                        }

                        if (wired.Verbs == null)
                        {
                            var itemtype = ResolveType(wired.Classname);
                            wirecontroller.WireController(itemtype, wired.RoutePrefix, wirecontrollerconfig);
                        }
                        else
                        {
                            var ix         = wired.Classname.LastIndexOf('.');
                            var classname  = wired.Classname.Substring(0, ix);
                            var methodname = wired.Classname.Substring(ix + 1);

                            var itemtype      = ResolveType(classname);
                            var argumenttypes = wired.ConstructorArguments.Select(x => ResolveType(x)).ToArray();
                            if (argumenttypes.Any(x => x == null))
                            {
                                throw new Exception($"Cannot resolve type argument {argumenttypes.Select((a, b) => a == null ? wired.ConstructorArguments[b] : null).Where(x => x != null).First()}");
                            }

                            wirecontroller.Wire(itemtype, wired.Verbs + " " + wired.RoutePrefix, methodname, argumenttypes);
                        }
                    }
                    // Check if this is a module
                    else if (route.RoutePrefix != null)
                    {
                        object handler;
                        var    moduletype = ResolveType(route.Classname);
                        if (typeof(Ceen.Httpd.Handler.FileHandler).IsAssignableFrom(moduletype))
                        {
                            Func <IHttpRequest, string, string> mimehandler = null;
                            if (config.MimeTypes != null && config.MimeTypes.Count > 0)
                            {
                                string default_mimetype;
                                config.MimeTypes.TryGetValue("*", out default_mimetype);
                                if (string.IsNullOrWhiteSpace(default_mimetype))
                                {
                                    default_mimetype = null;
                                }

                                if (config.IgnoreDefaultMimeTypes)
                                {
                                    mimehandler = (req, mappedpath) =>
                                    {
                                        var    ext = Path.GetExtension(mappedpath).ToLowerInvariant();
                                        string mime;
                                        config.MimeTypes.TryGetValue(ext, out mime);
                                        if (default_mimetype != null && string.IsNullOrWhiteSpace(mime))
                                        {
                                            return(default_mimetype);
                                        }

                                        return(mime);
                                    };
                                }
                                else
                                {
                                    mimehandler = (req, mappedpath) =>
                                    {
                                        var    ext = Path.GetExtension(mappedpath).ToLowerInvariant();
                                        string mime;
                                        config.MimeTypes.TryGetValue(ext, out mime);
                                        if (!string.IsNullOrWhiteSpace(mime))
                                        {
                                            return(mime);
                                        }
                                        else if (default_mimetype != null && string.IsNullOrWhiteSpace(mime))
                                        {
                                            return(default_mimetype);
                                        }
                                        else
                                        {
                                            return(Ceen.Httpd.Handler.FileHandler.DefaultMimeTypes(mappedpath));
                                        }
                                    };
                                }
                            }
                            else if (config.IgnoreDefaultMimeTypes)
                            {
                                mimehandler = (req, path) => null;
                            }

                            handler = Activator.CreateInstance(
                                moduletype,
                                ExpandEnvironmentVariables(route.ConstructorArguments.First()),
                                mimehandler
                                );

                            if (config.IndexDocuments.Count != 0)
                            {
                                if (route.Options.ContainsKey(nameof(Handler.FileHandler.IndexDocuments)))
                                {
                                    throw new Exception($"Cannot use both the `Index` option and {nameof(Handler.FileHandler.IndexDocuments)} in the configuration for {moduletype.FullName}");
                                }
                                route.Options[nameof(Handler.FileHandler.IndexDocuments)] = string.Join(", ", config.IndexDocuments.Select(x => $"\"{x}\""));
                            }
                        }
                        else
                        {
                            handler = CreateInstance(moduletype, route.ConstructorArguments, typeof(IHttpModule));
                        }

                        if (route.Options != null)
                        {
                            SetProperties(handler, route.Options);
                        }

                        if (handler is IWithSetup mse)
                        {
                            mse.AfterConfigure();
                        }

                        if (string.IsNullOrWhiteSpace(route.RoutePrefix))
                        {
                            cfg.AddRoute((IHttpModule)handler);
                        }
                        else
                        {
                            cfg.AddRoute(route.RoutePrefix, (IHttpModule)handler);
                        }
                    }
                    // Otherwise it is automatic routing of an assembly
                    else
                    {
                        var assembly = Assembly.Load(route.Assembly);
                        if (assembly == null)
                        {
                            throw new Exception($"Failed to load assembly {route.Assembly}");
                        }

                        Type defaulttype = null;
                        if (!string.IsNullOrWhiteSpace(route.Classname))
                        {
                            defaulttype = assembly.GetType(route.Classname, false);
                            if (defaulttype == null)
                            {
                                throw new Exception($"Failed to find class {route.Classname} in {route.Assembly}");
                            }
                        }

                        var rt = new Ceen.Mvc.ControllerRouterConfig(defaulttype);
                        if (route.Options != null)
                        {
                            SetProperties(rt, route.Options);
                        }

                        cfg.AddRoute(new Ceen.Mvc.ControllerRouter(rt, assembly));
                    }
                }

                // If we have a wired controller, add it
                if (wirecontroller != null)
                {
                    cfg.AddRoute(wirecontroller.ToRoute(wirecontrollerconfig));
                }
            }

            ctx.Freeze();
            return(cfg);
        }
        public YouTubePlayer(
            string TargetContent = "http://www.youtube.com/apiplayer?version=3",
            int DefaultWidth     = 1280,
            int DefaultHeight    = 720,
            string DefaultVideo  = "Z__-3BbPq6g",

            string suggestedQuality = "hd720",


            Action yield_init = null
            )
        {
            var ldr = new Loader();

            this.Loader = ldr;
            var urlReq = new URLRequest(TargetContent);

            var ctx_app = ApplicationDomain.currentDomain;
            var ctx_sec = SecurityDomain.currentDomain;

            // http://www.youtube.com/crossdomain.xml
            ctx_app = null;
            ctx_sec = null;

            // http://www.zedia.net/2010/using-the-actionscript-3-youtube-api/

            bool once = false;

            #region onReady
            Action <Event> onReady = e =>
            {
                if (once)
                {
                    return;
                }

                once = true;


#if JSC_FEATURE_dynamic
                dynamic player = ldr.content;

                player.setSize(160, 120);
#endif
                ldr.content.setSize(DefaultWidth, DefaultHeight);

                pauseVideo = delegate
                {
                    ldr.content.pauseVideo();
                };

                loadVideoById = x =>
                {
                    CurrentVideoId = x;

                    ldr.content.loadVideoById(x, suggestedQuality: suggestedQuality);
                };

                loadVideoById(DefaultVideo);

                if (yield_init != null)
                {
                    yield_init();
                    yield_init = null;
                }
            };
            #endregion


            var PreviousCurrentState = YouTubePlayerState.unknown;
            var CurrentState         = YouTubePlayerState.unknown;

            DefaultScene = this["default", 0, 1000];
            var    CurrentScene     = DefaultScene;
            Action CurrentSceneDone = delegate { };

            #region onStateChange
            Action <Event> onStateChange = e =>
            {
                PreviousCurrentState = CurrentState;
                CurrentState         = e.get_data_as_YouTubePlayerState();

                if (PreviousCurrentState != CurrentState)
                {
                    if (CurrentState == YouTubePlayerState.playing)
                    {
                        // notify other scenes of delinking?

                        if (this.Playing != null)
                        {
                            this.Playing(SceneTranslate(CurrentScene));
                        }

                        this.ReferencedScenes.WithEach(
                            k =>
                        {
                            k.RaiseLinkDenotification();
                        }
                            );
                    }
                    else
                    {
                        if (this.NotPlaying != null)
                        {
                            this.NotPlaying(SceneTranslate(CurrentScene));
                        }
                    }

                    if (CurrentState == YouTubePlayerState.paused)
                    {
                        if (this.Paused != null)
                        {
                            this.Paused(SceneTranslate(CurrentScene));
                        }
                    }
                }
            };
            #endregion


            var TimeToPause = 0.4;

            var t = new Timer(1000 / 100);

            var PlaySceneCounter = 0;

            t.timer +=
                delegate
            {
                if (ldr.content == null)
                {
                    return;
                }

                if (CurrentState == YouTubePlayerState.playing)
                {
                    var time = ldr.content.getCurrentTime();

                    var time_index = (int)time;

                    var duration = ldr.content.getDuration();

                    var playall = CurrentScene.end > duration;

                    // flag4 = ((double0 < (double2 - 500)) == 0);
                    // 1 second is 1.0!! :)
                    var notending = time < (duration - 0.500);
                    //var xending = time >= (duration - 500);
                    var ending = !notending;

                    // ReferenceError: Error #1069: Property getDuration not found on flash.display.Loader and there is no default value.
                    var m = new { PlaySceneCounter, time, time_index, CurrentScene.end, duration, playall, ending }.ToString();

                    if (StatusToClients != null)
                    {
                        StatusToClients(m);
                    }

                    // phone activated

                    if (playall)
                    {
                        if (ending)
                        {
                            ldr.content.pauseVideo();
                            CurrentSceneDone();
                        }
                    }
                    else if (time >= (TimeToPause))
                    {
                        ldr.content.pauseVideo();
                        CurrentSceneDone();
                    }
                }
            };

            t.start();

            #region PlayScene
            this.PlayScene =
                (e, Done) =>
            {
                PlaySceneCounter++;

                //if (e.end == 0)
                //    e.end = ldr.content.getDuration() - 1000;

                CurrentScene     = e;
                CurrentSceneDone = Done;

                TimeToPause = e.end;

                ldr.content.seekTo(e.start);
                ldr.content.playVideo();
            };
            #endregion



            ldr.contentLoaderInfo.ioError +=
                delegate
            {
            };

            ldr.contentLoaderInfo.init +=
                delegate
            {
                ldr.content.addEventListener("onReady", onReady.ToFunction(), false, 0, false);
                ldr.content.addEventListener("onStateChange", onStateChange.ToFunction(), false, 0, false);
            };

            var ctx = new LoaderContext(true, ctx_app, ctx_sec);
            ldr.load(urlReq, ctx);


            this.Scenes = new SceneSequenzer {
                Owner = this
            };
        }