示例#1
0
        internal static async Task <int> CreateAddonFile(DirectoryInfo folder, FileInfo fileOutput, bool warninvalid = false)
        {
            if (fileOutput is null)
            {
                fileOutput = new FileInfo(folder.FullName + ".gma");
            }

            var jsonFileInfo = new FileInfo(Path.Combine(folder.FullName, "addon.json"));

            AddonInfo addonInfo = await OpenAddonInfo(jsonFileInfo);

            //open every file in the folder, then feed it as a string:stream dictionary
            Dictionary <string, Stream> files = new Dictionary <string, Stream>();

            foreach (var fileInput in folder.EnumerateFiles("*", SearchOption.AllDirectories))
            {
                //turn the file paths into relatives and also lowercase
                string relativeFilePath = Path.GetRelativePath(folder.FullName, fileInput.FullName).ToLower();

                relativeFilePath = relativeFilePath.Replace("\\", "/");


                //this could PROBABLY be streamlined in a Addon.IsIgnoreMatching function but for now I just want this to work

                if (Addon.IsWildcardMatching(relativeFilePath, Addon.DefaultIgnores))
                {
                    continue;
                }

                if (addonInfo.IgnoreWildcard != null && Addon.IsWildcardMatching(relativeFilePath, addonInfo.IgnoreWildcard))
                {
                    continue;
                }

                //if it's not ignored and still not allowed, throw out an error

                if (!Addon.IsPathAllowed(relativeFilePath))
                {
                    if (warninvalid)
                    {
                        Console.Error.WriteLine($"{relativeFilePath} \t\t[Not allowed by whitelist]");
                    }
                    return(1);
                }

                var fileInputStream = fileInput.OpenRead();

                files.Add(relativeFilePath, fileInputStream);
            }

            if (files.Count == 0)
            {
                Console.Error.WriteLine("No files found, can't continue!");
                return(1);
            }

            //now open the stream for the output
            bool success;

            using (var outputStream = fileOutput.OpenWrite())
            {
                success = await Addon.Create(files, outputStream, addonInfo);
            }

            foreach (var kv in files)
            {
                kv.Value?.Dispose();
            }

            if (success)
            {
                SaveAddonInfo(jsonFileInfo, addonInfo);
                Console.WriteLine($"Successfully saved to {fileOutput.FullName}");
            }

            return(Convert.ToInt32(!success));
        }