コード例 #1
0
        public static void Create(ListView modsList, ListView patchesList)
        {
            // Save location for snapshot
            SaveFileDialog snapshot = new SaveFileDialog()
            {
                Title    = "Save snapshot...",
                Filter   = "Snapshot (*.06mm)|*.06mm",
                FileName = $"sonic06mm-snapshot-{DateTime.Now:ddMMyy}.06mm"
            };

            if (snapshot.ShowDialog() == DialogResult.OK)
            {
                string architecture = "x86";
                int    gpuCount     = 0;

                // Get application architecture
                if (IntPtr.Size == 4)
                {
                    architecture = "x86";
                }
                else if (IntPtr.Size == 8)
                {
                    architecture = "x64";
                }
                else
                {
                    architecture = "Unknown";
                }

                // Create snapshot
                using (StreamWriter sw = File.CreateText(snapshot.FileName)) {
#if !DEBUG
                    try {
#endif
                    sw.WriteLine($"Sonic '06 Mod Manager");
                    sw.WriteLine(DateTime.Now);

                    sw.WriteLine("\nBuild:");
#if !DEBUG
                    sw.WriteLine($"Type: Release");
#elif DEBUG
                    sw.WriteLine($"Type: Debug");
#endif
                    sw.WriteLine($"Version: {Program.VersionNumber}");
                    sw.WriteLine($"Architecture: {architecture}");

                    sw.WriteLine("\nSpecifications:");

                    // Get OS version
                    sw.WriteLine($"OS: {new ComputerInfo().OSFullName}");

                    // Get CPU name
                    ManagementObjectSearcher getCPU = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_Processor");
                    foreach (ManagementObject mo in getCPU.Get())
                    {
                        sw.WriteLine($"CPU: {mo["Name"]}");
                    }

                    // Format RAM as long to readable bytes
                    sw.WriteLine($"RAM: {Literal.FormatBytes(new ManagementObjectSearcher("SELECT Capacity FROM Win32_PhysicalMemory").Get().Cast<ManagementObject>().Sum(x => Convert.ToInt64(x.Properties["Capacity"].Value)))}");

                    // Get GPU names
                    ManagementObjectSearcher getGPU = new ManagementObjectSearcher("SELECT * FROM Win32_VideoController");
                    foreach (ManagementObject mo in getGPU.Get())
                    {
                        foreach (PropertyData property in mo.Properties)
                        {
                            if (property.Name == "Description")
                            {
                                sw.WriteLine($"GPU #{gpuCount}: {property.Value}");
                                gpuCount++;
                            }
                        }
                    }

                    // Print list of checked mods
                    if (modsList.CheckedItems.Count != 0)
                    {
                        sw.WriteLine("\nMods:");
                        // Writes the list in reverse so the mods list writes it in it's preferred order
                        for (int i = modsList.Items.Count - 1; i >= 0; i--)
                        {
                            if (modsList.Items[i].Checked)     // Get checked state
                            // Write mod name by folder name to prevent duplicate mod names conflicting
                            {
                                sw.WriteLine(Path.GetFileName(Path.GetDirectoryName(modsList.Items[i].SubItems[6].Text)));
                            }
                        }
                    }

                    // Print list of checked patches
                    if (patchesList.CheckedItems.Count != 0)
                    {
                        sw.WriteLine("\nPatches:");
                        // Writes in reverse so the patches list writes it in it's preferred order
                        for (int i = patchesList.Items.Count - 1; i >= 0; i--)
                        {
                            if (patchesList.Items[i].Checked)     // Get checked state
                            // Write patch name by file name to prevent duplicate patch names conflicting
                            {
                                sw.WriteLine(Path.GetFileName(patchesList.Items[i].SubItems[5].Text));
                            }
                        }
                    }

                    // Print list of current settings
                    sw.WriteLine("\nSettings:");
                    foreach (SettingsPropertyValue property in Properties.Settings.Default.PropertyValues)
                    {
                        if (property.Name == "General_AccentColour")
                        {
                            Color colour = Properties.Settings.Default.General_AccentColour;
                            sw.WriteLine($"{property.Name}: {colour.R}, {colour.G}, {colour.B}");
                        }
                        else
                        {
                            sw.WriteLine($"{property.Name}: {property.PropertyValue}");
                        }
                    }

                    Console.WriteLine($"[{DateTime.Now:HH:mm:ss tt}] [Success] Created a snapshot");
#if !DEBUG
                } catch (Exception ex) {
                    // Print exception if something failed
                    sw.WriteLine($"\nExceptions:\n{ex}");
                    Console.WriteLine($"[{DateTime.Now:HH:mm:ss tt}] [Error] Failed to create snapshot...\n{ex}");
                }
#endif
                }
            }
        }