public override bool Init()
        {
            QFileInfo program = new QFileInfo(MainScript());

            KMimeType mime = KMimeType.FindByFileContent(program.AbsoluteFilePath());
            try {
                if (mime.Name().StartsWith("text/")) {
                    Compiler c = new Compiler(program);
                    dataEngineAssembly = c.GetAssembly();
                } else {
                    dataEngineAssembly = Assembly.LoadFile(program.AbsoluteFilePath());
                }
            } catch (Exception e) {
                Console.WriteLine(e);
                return false;
            }

            // the newly loaded assembly might reference other bindings that need to be initialized
            foreach (AssemblyName an in dataEngineAssembly.GetReferencedAssemblies()) {
                Assembly a = null;
                try {
                    a = Assembly.Load(an);
                } catch (FileNotFoundException e) {
                    a = Assembly.LoadFile(Path.Combine(Path.GetDirectoryName(dataEngineAssembly.Location), an.Name + ".dll"));
                }
                // if the binding has already been initialized (e.g. in SmokeInvocation.InitRuntime()), continue.
                if (SmokeInvocation.InitializedAssemblies.Contains(a)) continue;
                AssemblySmokeInitializer attr = (AssemblySmokeInitializer) Attribute.GetCustomAttribute(a, typeof(AssemblySmokeInitializer));
                if (attr != null) attr.CallInitSmoke();
                SmokeInvocation.InitializedAssemblies.Add(a);
            }

            string typeName = Camelize(Package().Metadata().PluginName()) + ".";  // namespace
            typeName += Camelize(program.CompleteBaseName());
            dataEngineType = dataEngineAssembly.GetType(typeName);
            if (dataEngineType == null) {
                foreach (Type t in dataEngineAssembly.GetTypes()) {
                    for (Type tmp = t.BaseType; tmp != null; tmp = tmp.BaseType) {
                        if (tmp == typeof(PlasmaScripting.DataEngine)) {
                            dataEngineType = t;
                            break;
                        }
                    }
                    if (dataEngineType != null) break;
                }
            }

            dataEngine = (PlasmaScripting.DataEngine) Activator.CreateInstance(dataEngineType, new object[] { this });
            dataEngine.Init();
            return true;
        }
Exemplo n.º 2
0
        public override bool Init()
        {
            QSizeF oldSize = Applet().Size;
            QFileInfo program = new QFileInfo(MainScript());

            KMimeType mime = KMimeType.FindByFileContent(program.AbsoluteFilePath());
            try {
                if (mime.Name().StartsWith("text/")) {
                    Compiler c = new Compiler(program);
                    appletAssembly = c.GetAssembly();
                } else {
                    appletAssembly = Assembly.LoadFile(program.AbsoluteFilePath());
                }
            } catch (Exception e) {
                Console.WriteLine(e);
                Object[] parameters = new Object[2];
                parameters[0] = true;
                parameters[1] = e.ToString();
                MethodInfo method = Applet().GetType().GetMethod("SetFailedToLaunch",
                    BindingFlags.Instance | BindingFlags.NonPublic, null, new Type[] { typeof(System.Boolean),  typeof(System.String) }, null);
                method.Invoke(Applet(), parameters);
                return false;
            }

            // the newly loaded assembly might reference other bindings that need to be initialized
            foreach (AssemblyName an in appletAssembly.GetReferencedAssemblies()) {
                Assembly a = null;
                try {
                    a = Assembly.Load(an);
                } catch (FileNotFoundException e) {
                    a = Assembly.LoadFile(Path.Combine(Path.GetDirectoryName(appletAssembly.Location), an.Name + ".dll"));
                }
                // if the binding has already been initialized (e.g. in SmokeInvocation.InitRuntime()), continue.
                if (SmokeInvocation.InitializedAssemblies.Contains(a)) continue;
                AssemblySmokeInitializer attr = (AssemblySmokeInitializer) Attribute.GetCustomAttribute(a, typeof(AssemblySmokeInitializer));
                if (attr != null) attr.CallInitSmoke();
                SmokeInvocation.InitializedAssemblies.Add(a);
            }

            string typeName = Camelize(Package().Metadata().PluginName()) + ".";  // namespace
            typeName += Camelize(program.CompleteBaseName());
            appletType = appletAssembly.GetType(typeName);
            if (appletType == null) {
                foreach (Type t in appletAssembly.GetTypes()) {
                    for (Type tmp = t.BaseType; tmp != null; tmp = tmp.BaseType) {
                        if (tmp == typeof(PlasmaScripting.Applet)) {
                            appletType = t;
                            break;
                        }
                    }
                    if (appletType != null) break;
                }
            }

            applet = (PlasmaScripting.Applet) Activator.CreateInstance(appletType, new object[] { this });
            applet.Init();
            if (oldSize.Width() > 10 && oldSize.Height() > 10)
                Applet().Size = oldSize;
            SetUpEventHandlers();
            return true;
        }