예제 #1
0
        private Plugin(string path, PluginGroup group)
        {
            string name = Path.GetFileName(path);

            System.Diagnostics.Debug.Assert(name != null);

            this.pathRoot = path;
            this.pathData = Path.Combine(Program.PluginDataPath, group.GetIdentifierPrefix(), name);

            this.identifier   = group.GetIdentifierPrefix() + name;
            this.Group        = group;
            this.Environments = PluginEnvironment.None;
        }
예제 #2
0
 public Builder(PluginGroup group, string name, string pathRoot, string pathData)
 {
     this.group      = group;
     this.pathRoot   = pathRoot;
     this.pathData   = pathData;
     this.identifier = group.GetIdentifierPrefix() + name;
 }
예제 #3
0
        public static IEnumerable <Result <Plugin> > AllInFolder(string pluginFolder, string pluginDataFolder, PluginGroup group)
        {
            string path = Path.Combine(pluginFolder, group.GetSubFolder());

            if (!Directory.Exists(path))
            {
                yield break;
            }

            foreach (string fullDir in Directory.EnumerateDirectories(path, "*", SearchOption.TopDirectoryOnly))
            {
                string name   = Path.GetFileName(fullDir);
                string prefix = group.GetIdentifierPrefix();

                if (string.IsNullOrEmpty(name))
                {
                    yield return(new Result <Plugin>(new DirectoryNotFoundException($"{prefix}(?): Could not extract directory name from path: {fullDir}")));

                    continue;
                }

                Result <Plugin> result;

                try {
                    result = new Result <Plugin>(FromFolder(name, fullDir, Path.Combine(pluginDataFolder, prefix, name), group));
                } catch (Exception e) {
                    result = new Result <Plugin>(new Exception($"{prefix}{name}: {e.Message}", e));
                }

                yield return(result);
            }
        }
예제 #4
0
        private IEnumerable <Plugin> LoadPluginsFrom(string path, PluginGroup group)
        {
            if (!Directory.Exists(path))
            {
                yield break;
            }

            foreach (string fullDir in Directory.EnumerateDirectories(path, "*", SearchOption.TopDirectoryOnly))
            {
                Plugin plugin = Plugin.CreateFromFolder(fullDir, group, out string error);

                if (plugin == null)
                {
                    loadErrors.Add(group.GetIdentifierPrefix() + Path.GetFileName(fullDir) + ": " + error);
                }
                else
                {
                    yield return(plugin);
                }
            }
        }
예제 #5
0
        public static Plugin FromFolder(string path, PluginGroup group)
        {
            string name = Path.GetFileName(path);

            if (string.IsNullOrEmpty(name))
            {
                throw new ArgumentException("Could not extract directory name from path: " + path);
            }

            Plugin.Builder builder = new Plugin.Builder(group, name, path, Path.Combine(Program.PluginDataPath, group.GetIdentifierPrefix(), name));

            foreach (string file in Directory.EnumerateFiles(path, "*.js", SearchOption.TopDirectoryOnly).Select(Path.GetFileName))
            {
                builder.AddEnvironment(PluginEnvironmentExtensions.Values.FirstOrDefault(env => file.Equals(env.GetPluginScriptFile(), StringComparison.Ordinal)));
            }

            string metaFile = Path.Combine(path, ".meta");

            if (!File.Exists(metaFile))
            {
                throw new ArgumentException("Plugin is missing a .meta file");
            }

            string currentTag = null, currentContents = string.Empty;

            foreach (string line in File.ReadAllLines(metaFile, Encoding.UTF8).Concat(EndTag).Select(line => line.TrimEnd()).Where(line => line.Length > 0))
            {
                if (line[0] == '[' && line[line.Length - 1] == ']')
                {
                    if (currentTag != null)
                    {
                        SetProperty(builder, currentTag, currentContents);
                    }

                    currentTag      = line.Substring(1, line.Length - 2).ToUpper();
                    currentContents = string.Empty;

                    if (line.Equals(EndTag[0]))
                    {
                        break;
                    }
                }
                else if (currentTag != null)
                {
                    currentContents = currentContents.Length == 0 ? line : currentContents + Environment.NewLine + line;
                }
                else
                {
                    throw new FormatException("Missing metadata tag before value: " + line);
                }
            }

            return(builder.BuildAndSetup());
        }