Esempio n. 1
0
        // Remove a loadable
        public void RemoveLoadable(Loadable loadable)
        {
            Loadables.Remove(loadable);

            if (loadable is EntityDefinition)
            {
                EntityDefinition ent = (EntityDefinition)loadable;
                // Remove existing elements
                foreach (var enemy in ent.Instances)
                {
                    this.entities.Remove(enemy);
                }
            }
        }
Esempio n. 2
0
        public void LoadPlugins(List <PluginResourceInfo> plugins)
        {
            if (plugins.Count == 0)
            {
                return;
            }

            var assembly           = Assembly.GetEntryAssembly();
            var blacklistedPlugins = new[]
            {
                new Guid(new byte[]
                         { 0x74, 0x56, 0xee, 0xe6, 0x94, 0xbb, 0xc7, 0x46, 0x8b, 0xbc, 0x57, 0x29, 0xaf, 0x6e, 0x2c, 0x28 })
            };

            var clientPluginIds = new Dictionary <ClientController, Guid>();

            foreach (var plugin in plugins)
            {
                if (blacklistedPlugins.Contains(plugin.Guid))
                {
                    continue;
                }

                AvailablePlugins.Add(plugin, false);
                var stream = assembly.GetManifestResourceStream(plugin.ResourceName);
                if (stream == null)
                {
                    continue;
                }

                Assembly pluginAssembly;
                using (var memoryStream = new MemoryStream())
                {
                    stream.CopyToEx(memoryStream);
                    stream.Dispose();
                    pluginAssembly = Assembly.Load(Decompress(memoryStream.ToArray()));
                }

                try
                {
                    var types = pluginAssembly.GetTypes();
                    switch (plugin.ResourceType)
                    {
                    case ResourceType.Command:
                        var commandType = types.First(x => x.IsSubclassOf(typeof(Command)));
                        Commands.Add(commandType);
                        break;

                    case ResourceType.ClientPlugin:
                        var clientType   = types.First(x => x.IsSubclassOf(typeof(ClientController)));
                        var clientPlugin = (ClientController)Activator.CreateInstance(clientType);
                        try
                        {
                            clientPlugin.Initialize(ClientOperator.Instance);
                        }
                        catch (Exception ex)
                        {
                            ErrorReporter.Current.ReportError(ex,
                                                              "Initialize() (ClientPlugin) at plugin: \"" + clientPlugin.GetType() + "\"");
                        }
                        ClientPlugins.Add(clientPlugin);
                        Loadables.Add(clientPlugin);
                        clientPluginIds.Add(clientPlugin, plugin.Guid);
                        break;

                    case ResourceType.FactoryCommand:
                        var factoryCommandPluginType =
                            types.First(x => x.GetInterface("IFactoryClientCommand") != null);
                        var factoryCommandPlugin =
                            (IFactoryClientCommand)Activator.CreateInstance(factoryCommandPluginType);
                        FactoryCommandPlugins.Add(factoryCommandPlugin);
                        try
                        {
                            factoryCommandPlugin.Factory.Initialize(ClientOperator.Instance);
                        }
                        catch (Exception ex)
                        {
                            ErrorReporter.Current.ReportError(ex,
                                                              "Initialize() (FactoryCommand) at plugin: \"" + factoryCommandPlugin.GetType() +
                                                              "\"");
                        }
                        Loadables.Add(factoryCommandPlugin.Factory);
                        break;

                    default:
                        continue;
                    }
                    AvailablePlugins[plugin] = true;
                }
                catch (Exception ex)
                {
                    ErrorReporter.Current.ReportError(ex,
                                                      $"Error loading and creating {plugin.ResourceType} of plugin {plugin.PluginName}");
                }
            }

            var requiredTypes           = new List <Type>();
            var propertyProvider        = new List <ClientControllerProvideEditablePropertyGrid>();
            var builderPropertyProvider = new List <ClientControllerBuilderSettings>();

            foreach (var clientController in ClientPlugins)
            {
                var providesProperties = clientController as ClientControllerProvideEditablePropertyGrid;
                if (providesProperties != null)
                {
                    requiredTypes.AddRange(providesProperties.Properties.Select(x => x.PropertyType));
                    propertyProvider.Add(providesProperties);
                    continue;
                }

                var providesBuilderSettings = clientController as ClientControllerBuilderSettings;
                if (providesBuilderSettings != null)
                {
                    requiredTypes.AddRange(providesBuilderSettings.BuilderSettings.Select(x => x.BuilderProperty.GetType()));
                    builderPropertyProvider.Add(providesBuilderSettings);
                }
            }

            if (propertyProvider.Count == 0 && builderPropertyProvider.Count == 0)
            {
                return;
            }

            var pluginSettings = Settings.GetPluginSettings(requiredTypes);

            foreach (var clientController in propertyProvider)
            {
                var settings =
                    pluginSettings.FirstOrDefault(x => x.PluginId == clientPluginIds[clientController]);
                if (settings != null)
                {
                    PropertyGridExtensions.InitializeProperties(clientController, settings.Properties);
                }
            }

            foreach (var clientController in builderPropertyProvider)
            {
                var settings =
                    pluginSettings.Where(x => x.PluginId == clientPluginIds[clientController]).ToList();
                if (settings.Count > 0)
                {
                    var builderSettings = new List <IBuilderProperty>();
                    foreach (var pluginSetting in settings)
                    {
                        var type = Type.GetType(pluginSetting.SettingsType);
                        if (type == null)
                        {
                            continue;
                        }

                        var settingInstance = Activator.CreateInstance(type) as IBuilderProperty;
                        if (settingInstance == null)
                        {
                            continue;
                        }

                        BuilderPropertyHelper.ApplyProperties(settingInstance, pluginSetting.Properties);
                        builderSettings.Add(settingInstance);
                    }

                    clientController.InitializeSettings(builderSettings);
                }
            }
        }
Esempio n. 3
0
 // Add new loadable to loadables
 public void AddLoadable(Loadable loadable)
 {
     Loadables.Add(loadable);
 }
Esempio n. 4
0
        // Loads level
        public void LoadLevel()
        {
            // Reset loadables
            Loadables.Clear();

            // Get level's load table
            var loadTable = GetExpandedLevelLoadTable().Split('\n');

            // Variables used in parsing
            List <int> cycles = new List <int>();

            // Current Entity definition being worked on
            Loadable loadable = null;

            // Current component
            Component component = null;

            foreach (string line in loadTable)
            {
                var vec = line.Split(' ');

                // Look for starting cycle
                if (line.StartsWith("@cycle") || line.StartsWith("@immediate"))
                {
                    // Clear old loadable
                    loadable = null;
                    cycles.Clear();
                    for (int i = 1; i < vec.Length; i++)
                    {
                        cycles.Add(Int32.Parse(vec[i]));
                    }
                }

                // Look for an ent declaration
                else if (line.StartsWith("ent"))
                {
                    // Generate new entities
                    loadable = new EntityDefinition(cycles);
                    Loadables.Add(loadable);
                }

                // Sets flags on which component to update
                else if (line.StartsWith("+"))
                {
                    string   name       = line.Substring(1).Split(' ')[0];
                    string[] parameters = line.Split(' ').Skip(1).ToArray();

                    component = Component.TranslateToComponentType(name);
                    if (component is Object)
                    {
                        component.Initialise(parameters, (EntityDefinition)loadable);
                        ((EntityDefinition)loadable).AddComponent(name, component);
                    }
                }

                // Conduct update to component
                else if (line.StartsWith("->"))
                {
                    if (component is Object)
                    {
                        component.UpdateEntity(line, (EntityDefinition)loadable);
                    }
                }

                // Update script
                else if (line.StartsWith("<<"))
                {
                    // Either extend or generate new script
                    if (!(loadable is Script))
                    {
                        loadable = new Script(cycles);
                        Loadables.Add(loadable);
                    }
                    ((Script)loadable).Extend(line.Substring(2));
                }

                // Add a template
                else if (line.StartsWith("#"))
                {
                    // Look up template
                    string        templateName = line.Substring(1).Split(' ')[0];
                    List <string> parameters   = line.Substring(1).Split(' ').Skip(1).ToList();

                    if (Templates.ContainsKey(templateName))
                    {
                        var template = Templates[templateName];
                        ((EntityDefinition)loadable).AddTemplate(template, parameters);
                    }
                }
            }

            EvaluateEntities();
        }