Ejemplo n.º 1
0
        private void setupFontsDirectory(string fontsFile, string fontname, string fontfilename)
        {
            string extension = Path.GetExtension(fontfilename);

            File.WriteAllText(fontsFile, FileTemplates.fonts_conf(fontname, extension, fontfilename, getCSGOPixelSize().ToString().Replace(",", ".")));
        }
Ejemplo n.º 2
0
        private void apply_button_Click(object sender, EventArgs e)
        {
            if (CurrentFormView == FormViews.Main)
            {
                string message = "Do you want to apply " + listBox1.SelectedItem + " to CS:GO?";
                if (listBox1.SelectedItem.Equals(defaultFontName))
                {
                    message = "Do you want to reset to the default font for CS:GO?";
                }
                DialogResult dialogResult = MessageBox.Show(message, "Are you sure?", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (dialogResult == DialogResult.Yes)
                {
                    //Get the csgo path (home folder...) if data file not found...
                    if (Settings.CsgoPath != null)
                    {
                        // Make sure the folders exists
                        string csgoConfD = csgoFontsFolder + "\\conf.d";
                        if (!Directory.Exists(csgoFontsFolder))
                        {
                            Directory.CreateDirectory(csgoFontsFolder);
                        }
                        if (!Directory.Exists(csgoConfD))
                        {
                            Directory.CreateDirectory(csgoConfD);
                        }

                        // Remove all existing font files (.ttf, .otf, etc...)
                        foreach (string file in Directory.GetFiles(csgoFontsFolder))
                        {
                            if (IsFontExtension(Path.GetExtension(file)))
                            {
                                File.Delete(file);
                            }
                        }

                        if (listBox1.SelectedItem.Equals(defaultFontName))
                        {
                            string csgoFontsConf = csgoFontsFolder + "\\fonts.conf";

                            File.WriteAllText(csgoFontsConf, FileTemplates.fonts_conf_backup());
                            MessageBox.Show("Successfully reset to the default font!", "Default Font Applied!",
                                            MessageBoxButtons.OK, MessageBoxIcon.Information);
                        }
                        else
                        {
                            string fontsConfPath = FontsFolder + listBox1.SelectedItem + "\\fonts.conf";
                            string csgoFontsConf = csgoFontsFolder + "\\fonts.conf";


                            // Generate a new fonts.conf
                            string fontFilePath = null;
                            foreach (string file in Directory.GetFiles(FontsFolder + listBox1.SelectedItem))
                            {
                                if (IsFontExtension(Path.GetExtension(file)))
                                {
                                    fontFilePath = file;
                                    break;
                                }
                            }

                            if (fontFilePath != null)
                            {
                                FontFamily fontFamily = GetFontFamilyByName(listBox1.SelectedItem.ToString());
                                setupFontsDirectory(fontsConfPath, fontFamily.Name, Path.GetFileName(fontFilePath));
                            }


                            if (File.Exists(fontsConfPath))
                            {
                                new FileIOPermission(FileIOPermissionAccess.Write, csgoFontsConf).Demand();
                                File.Copy(fontsConfPath, csgoFontsConf, true);

                                // Add font file into csgo path
                                foreach (string file in Directory.GetFiles(FontsFolder + listBox1.SelectedItem))
                                {
                                    if (IsFontExtension(Path.GetExtension(file)))
                                    {
                                        File.Copy(file, csgoFontsFolder + @"\" + Path.GetFileName(file), true);
                                    }
                                }

                                bool csgoIsRunning = Process.GetProcessesByName("csgo").Length != 0;

                                MessageBox.Show($"Successfully applied {listBox1.SelectedItem}!" +
                                                (csgoIsRunning ? "\n\nRestart CS:GO for the font to take effect." : ""),
                                                "Font Applied!", MessageBoxButtons.OK, MessageBoxIcon.Information);

                                Settings.ActiveFont = listBox1.SelectedItem.ToString();
                            }
                            else
                            {
                                MessageBox.Show("Failed to apply font " + listBox1.SelectedItem + "!\nThe fonts.conf file was not found...", "Failed to Apply", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            }
                        }
                    }
                    else
                    {
                        MessageBox.Show("Ooops! Seems like the CS:GO folder is unknown, please try to restart the program...", "No CS:GO Fonts folder", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }
            else if (CurrentFormView == FormViews.AddSystemFont)
            {
                FontFamily fontFamily   = new FontFamily(listBox1.SelectedItem.ToString());
                Font       selectedFont = new Font(fontFamily, 14);

                string fontFilePath = null;

                string fontFileName = GetSystemFontFileName(selectedFont); // These return null verry often
                if (fontFileName == null)
                {
                    List <string> matchedFontFileNames = GetFilesForFont(selectedFont.Name).ToList(); // These return null verry often
                    if (matchedFontFileNames.Count > 0)
                    {
                        string[] mffn = matchedFontFileNames[0].Split('\\');
                        fontFileName = mffn[mffn.Length - 1];
                        fontFilePath = matchedFontFileNames[0];
                    }
                }
                else
                {
                    fontFilePath = Environment.GetFolderPath(Environment.SpecialFolder.Fonts) + "\\" + fontFileName;
                }

                bool   fontAlreadyExisted = false;
                string fileFontDirectory  = FontsFolder + sanitizeFilename(selectedFont.Name) + @"\";
                // Check if font already exists
                if (Directory.Exists(fileFontDirectory))
                {
                    if (MessageBox.Show(
                            $"The font '{selectedFont.Name}' is already in your library, do you want to overwrite it?",
                            "Overwrite Font?", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
                    {
                        // Directory.Delete(fileFontDirectory, true);
                        fontAlreadyExisted = true;
                    }
                    else
                    {
                        listBox1.Enabled = true;
                        return;
                    }
                }
                // AddFont(filename, fontpath);

                if (fontFilePath != null && File.Exists(fontFilePath))
                {
                    // Copy from C:\Windows\Fonts\[FONTNAME] to FontsFolder
                    if (!fontAlreadyExisted)
                    {
                        Directory.CreateDirectory(fileFontDirectory);
                    }

                    new FileIOPermission(FileIOPermissionAccess.Read, fontFilePath).Demand();
                    new FileIOPermission(FileIOPermissionAccess.Write, fileFontDirectory + fontFileName).Demand();
                    File.Copy(fontFilePath, fileFontDirectory + fontFileName, true);

                    // Initialize the font
                    string fontsConfPath = fileFontDirectory + "fonts.conf";
                    setupFontsDirectory(fontsConfPath, fontFamily.Name, Path.GetFileName(fontFilePath));

                    MessageBox.Show("Success! The following font(s) has been added to your library!\n---\n" + selectedFont.Name, "Font(s) Added!", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    selectedFont.Dispose();
                    switchView(FormViews.Main);
                }
                else
                {
                    MessageBox.Show($"The filepath to '{selectedFont.Name}' could not be found. Please select another font.", "Font not found", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    // Call itself again
                    addFont_button_Click(null, null);
                }

                refreshFontList();
            }
        }