Esempio n. 1
0
        private void GenSettings_Click(object sender, EventArgs e)
        {
            List <char> invalidCharsList = new List <char>(Path.GetInvalidPathChars());

            invalidCharsList.Add(' ');
            char[] invalidChars = invalidCharsList.ToArray();

            if (!File.Exists(ServerPath.Text))
            {
                MessageBox.Show("The server file path cannot be found or does not exist.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            if (ServerPath.Text.IndexOfAny(invalidChars) != -1)
            {
                MessageBox.Show("The server file path contains invalid characters.\nPlease remove all invalid characters.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            if (!FindBackupSource.Checked)
            {
                if (!Directory.Exists(BackupSource.Text))
                {
                    MessageBox.Show("The backup source directory cannot be found or does not exist.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                if (BackupSource.Text.IndexOfAny(invalidChars) != -1)
                {
                    MessageBox.Show("The backup source directory path contains invalid characters.\nPlease remove all invalid characters.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }

            if (!Directory.Exists(BackupLocation.Text))
            {
                MessageBox.Show("The backup location directory cannot be found or does not exist.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            if (BackupLocation.Text.IndexOfAny(invalidChars) != -1)
            {
                MessageBox.Show("The backup location directory path contains invalid characters.\nPlease remove all invalid characters.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            Settings newSettings = new Settings()
            {
                ServerPath           = ServerPath.Text,
                MinRam               = MinRamSlider.Value * 256,
                MaxRam               = MaxRamSlider.Value * 256,
                SameMaxMin           = SameMaxMin.Checked,
                BackupSource         = BackupSource.Text,
                BackupLocation       = BackupLocation.Text,
                BackupInterval       = (int)BackupInterval.Value,
                BackupNumber         = (int)BackupNumber.Value,
                WrapperColor         = (ConsoleColor)ConsoleColorCbo.SelectedItem,
                ZipCompressionLevel  = CurrentSettings.ZipCompressionLevel,
                ShowCpuRamUsage      = ShowRamCpuUsage.Checked,
                LaunchFlags          = LaunchFlags.Text,
                AutoFindBackupSource = FindBackupSource.Checked,
            };

            try
            {
                if (File.Exists(@"Wrapper\Settings.json"))
                {
                    File.Delete(@"Wrapper\Settings.json");
                }
            }
            catch (Exception ex)
            {
                ExceptionMessage.PrintException(ex, "Error deleting old Settings.json while generating new Settings.json.");
                return;
            }

            try
            {
                string json = JsonConvert.SerializeObject(newSettings, Formatting.Indented);
                File.WriteAllText(@"Wrapper\Settings.json", json);
            }
            catch (Exception ex)
            {
                ExceptionMessage.PrintException(ex, "Error creating Settings.json while generating new Settings.json.");
                return;
            }

            MessageBox.Show("Successfully generated Settings.json.", "Success", MessageBoxButtons.OK, MessageBoxIcon.None);
        }
Esempio n. 2
0
        public Main()
        {
            InitializeComponent();

            CompressionLevelCbo.Items.AddRange(new object[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 });

            ConsoleColorCbo.DataSource = Enum.GetValues(typeof(ConsoleColor)).Cast <ConsoleColor>();

            ulong          totalmemB = 0;
            MEMORYSTATUSEX memStatus = new MEMORYSTATUSEX();

            if (GlobalMemoryStatusEx(memStatus))
            {
                totalmemB = memStatus.ullTotalPhys;
            }

            int usableMemory;

            try
            {
                usableMemory = Convert.ToInt32((totalmemB / (1024 * 1024)) - ((totalmemB / (1024 * 1024)) % 256));
            }
            catch
            {
                usableMemory = int.MaxValue - (int.MaxValue % 256);
            }

            MinRamSlider.Maximum       = usableMemory / 256;
            MinRamSlider.Minimum       = 0;
            MinRamSlider.SmallChange   = 1;
            MinRamSlider.TickFrequency = 1;
            MinRamText.Text            = (MinRamSlider.Value * 256).ToString();

            MaxRamSlider.Maximum       = usableMemory / 256;
            MaxRamSlider.Minimum       = 0;
            MaxRamSlider.SmallChange   = 1;
            MaxRamSlider.TickFrequency = 1;
            MaxRamText.Text            = (MinRamSlider.Value * 256).ToString();

            CurrentSettings = new Settings();

            try
            {
                if (!Directory.Exists(@"Wrapper"))
                {
                    Directory.CreateDirectory(@"Wrapper");
                }
            }
            catch (Exception ex)
            {
                ExceptionMessage.PrintException(ex, "Error creating wrapper directory.");
                return;
            }

            CurrentSettings = new Settings();

            ServerPath.Text                  = CurrentSettings.ServerPath;
            MinRamSlider.Value               = CurrentSettings.MinRam / 256;
            MinRamText.Text                  = CurrentSettings.MinRam + " MB";
            MaxRamSlider.Value               = CurrentSettings.MaxRam / 256;
            MaxRamText.Text                  = CurrentSettings.MaxRam + " MB";
            SameMaxMin.Checked               = CurrentSettings.SameMaxMin;
            BackupSource.Text                = CurrentSettings.BackupSource;
            BackupLocation.Text              = CurrentSettings.BackupLocation;
            BackupInterval.Value             = CurrentSettings.BackupInterval;
            BackupNumber.Value               = CurrentSettings.BackupNumber;
            ConsoleColorCbo.SelectedItem     = CurrentSettings.WrapperColor;
            CompressionLevelCbo.SelectedItem = CurrentSettings.ZipCompressionLevel;
            ShowRamCpuUsage.Checked          = CurrentSettings.ShowCpuRamUsage;

            if (string.IsNullOrWhiteSpace(ServerPath.Text))
            {
                string[] files = Directory.GetFiles(Path.GetDirectoryName(Application.ExecutablePath));
                foreach (string file in files)
                {
                    if (file.ToLower().Contains("minecraft_server") && file.ToLower().EndsWith(".jar"))
                    {
                        ServerPath.Text = file;
                        return;
                    }
                }
            }
        }