All pastes #277372 Raw Edit

Miscellany

public text v1 · immutable
·published 2006-12-13 03:46 UTC
rendered paste body
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace PatchSwap
{
    public partial class Settings : Form
    {
        private PatchSwap m_PatchSwapper;
        public PatchSwap PatchSwapper { get { return m_PatchSwapper; } set { m_PatchSwapper = value; } }

        public Settings( PatchSwap ps )
        {
            m_PatchSwapper = ps;
            InitializeComponent();
        }

        public void LoadSettings()
        {
            string settings_path = m_PatchSwapper.BaseDirectory + @"\Settings";
            string path = m_PatchSwapper.BaseDirectory + @"\Settings\settings.pss";

            if( !Directory.Exists( settings_path ) )
                Directory.CreateDirectory( settings_path );

            if( !File.Exists( path ) )
                SaveSettings();

            using( BinaryReader reader = new BinaryReader( new StreamReader( path ).BaseStream ) )
            {
                _DiabloInstallDirectory.Text = reader.ReadString();
                _LaunchArgs.Text = reader.ReadString();
                _MHInstallDirectory.Text = reader.ReadString();
            }

            m_PatchSwapper.BaseDirectory = _DiabloInstallDirectory.Text;
            m_PatchSwapper.LaunchArgs = _LaunchArgs.Text;
            m_PatchSwapper.MapHackDirectory = _MHInstallDirectory.Text;
        }

        public void SaveSettings()
        {
            string settings_path = m_PatchSwapper.BaseDirectory + @"\Settings";
            string path = m_PatchSwapper.BaseDirectory + @"\Settings\settings.pss";

            if( !Directory.Exists( settings_path ) )
                Directory.CreateDirectory( settings_path );

            if( File.Exists( path ) )
            {
                try
                {
                    File.Delete( path );
                }
                catch( Exception e )
                {
                    using( StreamWriter log = new StreamWriter( settings_path + @"\log.txt" ) )
                    {
                        log.WriteLine( "A problem occured while saving PatchSwap settings." );
                        log.WriteLine( "Exception: {0}", e );
                        log.WriteLine();
                        log.WriteLine();
                    }
                }
            }

            using( BinaryWriter writer = new BinaryWriter( new StreamWriter( path ).BaseStream ) )
            {
                writer.Write( ( string )_DiabloInstallDirectory.Text );
                writer.Write( ( string )_LaunchArgs.Text );
                writer.Write( ( string )_MHInstallDirectory.Text );
            }
        }

        private void _BrowseButton_Click( object sender, EventArgs e )
        {
            _FolderBrowser.ShowDialog();
            _DiabloInstallDirectory.Text = _FolderBrowser.SelectedPath;
        }

        private void _MHBrowseButton_Click( object sender, EventArgs e )
        {
            _FolderBrowser.ShowDialog();
            _MHInstallDirectory.Text = _FolderBrowser.SelectedPath;
        }

        private void _ApplySettings_Click( object sender, EventArgs e )
        {
            m_PatchSwapper.BaseDirectory = _DiabloInstallDirectory.Text;
            m_PatchSwapper.LaunchArgs = _LaunchArgs.Text;
            m_PatchSwapper.MapHackDirectory = _MHInstallDirectory.Text;
            SaveSettings();
            this.Close();
        }
    }
}