Ejemplo n.º 1
0
 public static void ExportZipResource(string dir, string resourcename, Assembly assembly = null)
 {
     new FastZip().ExtractZip(EmbeddedResource.GetResource(resourcename, assembly), dir, FastZip.Overwrite.Always, (FastZip.ConfirmOverwriteDelegate)(name => true), (string)null, (string)null, true, true);
 }
Ejemplo n.º 2
0
        public static int Main(string[] args)
        {
            Console.Title = "FontRegister By Eli Belash / Nucs 2017";
            bool          haltonend = false;
            List <string> fonts     = new List <string>();

            void ProcessPath(string path)
            {
                try {
                    Console.WriteLine($"Processing \"{path}\"");
                    FileAttributes attr = File.GetAttributes(path);
                    if (attr.HasFlag(FileAttributes.Directory))
                    {
                        fonts.AddRange(GetFiles(new DirectoryInfo(path), "*.fon", "*.ttf", "*.ttc", "*.otf").Select(f => f.FullName));
                    }
                    else
                    {
                        var ex = Path.GetExtension(path);
                        if (!ex.EndsWith(".fon", true, CultureInfo.InvariantCulture) && !ex.EndsWith(".ttc", true, CultureInfo.InvariantCulture) && !ex.EndsWith(".ttf", true, CultureInfo.InvariantCulture) && !ex.EndsWith(".otf", true, CultureInfo.InvariantCulture))
                        {
                            return;
                        }
                        fonts.Add(path);
                    }
                } catch (IOException e) {
                    Console.WriteLine($"Failed on \"{path}\" because \"{e.Message}\"");
                }
            }

            if (args != null && args.Length == 1 && (args[0].StartsWith("/h") || args[0].Contains("-h")))
            {
                PrintInfo();
                Exit(false);
            }
            else if (args != null && args.Length == 1 && args[0].StartsWith("--"))
            {
                if (args[0] == "--clear" || args[0] == "--cleanup")
                {
                    RunCleanup();
                    return(0);
                }
                else
                {
                    Console.WriteLine("Unknown command: " + args[0]);
                    PrintInfo();
                    Exit(false);
                }
            }
            else if (args != null && args.Length >= 1)
            {
                Console.WriteLine("Accepted arguments:");
                var cargs = args.Distinct().Where(a => a.StartsWith("--") == false).Where(s => string.IsNullOrEmpty(s.Trim())).ToArray();
                foreach (var path in cargs)
                {
                    Console.WriteLine(path);
                }

                foreach (var path in args.Where(path => path.StartsWith("-") == false))
                {
                    ProcessPath(path);
                }

                fonts = fonts.Distinct().ToList();

                if (fonts.Count == 0)
                {
                    Console.WriteLine("No fonts were found.");
                    Exit();
                }
                Console.WriteLine();
            }
            else
            {
                haltonend = true;
                Console.WriteLine("Enter the search directories path [enter for working directory] [n/next to continue]");
                while (true)
                {
                    Console.Write(">");
                    var path = Console.ReadLine();
                    if (string.IsNullOrEmpty(path))
                    {
                        path = Directory.GetCurrentDirectory();
                        if (Directory.Exists(path) == false)
                        {
                            path = ExecutingDir.FullName;
                        }
                    }
                    if (path == "n" || path == "next")
                    {
                        break;
                    }

                    ProcessPath(path);
                }

                Console.WriteLine();
                Console.WriteLine("Searching...");

                Console.WriteLine();
                if (fonts.Count == 0)
                {
                    Console.WriteLine("No fonts were found.");
                    Exit();
                }
                fonts = fonts.Distinct().ToList();
                Console.WriteLine($"About to attempt to install {fonts.Count} fonts...");
                AskContinue();
                Console.WriteLine();
            }

            DirectoryInfo cache = null;

            try {
                cache = Directory.CreateDirectory(Path.Combine(Path.GetTempPath(), $"cache_" + Path.ChangeExtension(Path.GetRandomFileName(), null)));
                if (!IsDirectoryWritable(cache))
                {
                    throw new Exception($"Couldn't write to generated temporary directory. Test has failed, fallbacking.\n{cache.FullName}"); //fallback
                }
            } catch (Exception e) {
                try {
                    //once you have the path you get the directory with:
                    cache = Directory.CreateDirectory(Path.Combine(ExecutingDir.FullName, "cache"));
                    if (!IsDirectoryWritable(cache))
                    {
                        throw new Exception($"Couldn't write to the generated temporary directory. Test has failed, failed.\n{cache.FullName}"); //fallback
                    }
                } catch (Exception ee) {
                    Console.WriteLine($"Failed creating temporary directory at\n{cache?.FullName ?? ""}\n{e}\n{ee}");
                    Exit();
                    return(1);
                }
            }

            Console.WriteLine("Cache Directory: " + cache.FullName);
            Console.WriteLine();
            Console.WriteLine("Copying...");

            foreach (var font in fonts)
            {
                try {
                    var dest = Path.Combine(cache.FullName, font.Contains("\\") || font.Contains("/") ? Path.GetFileName(font) : font);
                    if (File.Exists(dest))
                    {
                        continue;
                    }
                    File.Copy(font, dest, false);
                    MarkForDeletion(dest);
                } catch (IOException) { }
            }
            //export fontreg.exe
            var installer = Path.Combine(cache.FullName, "FontReg.exe");

            EmbeddedResource.ExportZipResource(cache, is64BitOperatingSystem ? "x64.zip" : "x86.zip", Assembly.GetEntryAssembly());
            MarkForDeletion(installer);

            var info = new ProcessStartInfo {
                FileName         = "cmd.exe",
                Arguments        = $"/C FontReg.exe /move",
                WorkingDirectory = Path.GetDirectoryName(installer),
                UseShellExecute  = false,
                WindowStyle      = ProcessWindowStyle.Hidden
            };

            Console.WriteLine($"Installing {fonts.Count} fonts......");
            var p = Process.Start(info);

            p.WaitForExit();

            File.Delete(installer);
            Directory.Delete(cache.FullName, true);

            Console.WriteLine();
            Console.WriteLine("Exit code: " + (p.ExitCode == 0 ? "Succesfull" : "Errornous"));

            Exit(haltonend);
            return(p.ExitCode);
        }