Exemplo n.º 1
0
        public formBrowseGwDat(string path, Settings.IGwDatFile existing)
        {
            InitializeComponents();

            Util.CheckedButton.Group(radioFileAutoCopy, radioFileExisting);

            this.existing = existing;

            if (path == null && existing != null)
            {
                path = existing.Path;
            }

            if (path != null)
            {
                radioFileExisting.Checked = true;
                textExisting.Text         = path;
                textExisting.Tag          = path;
                textExisting.Select(textExisting.TextLength, 0);
            }
            else
            {
                radioFileAutoCopy.Checked = true;
                radioFileAutoCopy.Select();
            }
        }
Exemplo n.º 2
0
 public SelectedFile(Settings.IGwDatFile dat)
 {
     this.File = dat;
 }
Exemplo n.º 3
0
        private void buttonOK_Click(object sender, EventArgs e)
        {
            if (radioFileAutoCopy.Checked)
            {
                if (!Settings.GuildWars1.Path.HasValue || !File.Exists(Settings.GuildWars1.Path.Value))
                {
                    using (var f = new OpenFileDialog())
                    {
                        f.Filter = "Guild Wars 1|Gw.exe|All executables|*.exe";
                        f.Title  = "Open Gw.exe";

                        if (f.ShowDialog(this) != System.Windows.Forms.DialogResult.OK)
                        {
                            return;
                        }

                        Settings.GuildWars1.Path.Value = f.FileName;
                    }
                }

                var gwpath = Path.GetDirectoryName(Settings.GuildWars1.Path.Value);
                var exe    = Path.GetFileName(Settings.GuildWars1.Path.Value);
                var path   = Path.Combine(gwpath, Client.FileManager.LOCALIZED_EXE_FOLDER_NAME);

                if (!Directory.Exists(path))
                {
                    try
                    {
                        Directory.CreateDirectory(path);
                        if (!Util.FileUtil.AllowFolderAccess(path, System.Security.AccessControl.FileSystemRights.Modify))
                        {
                            throw new Exception("Unable to set folder permissions");
                        }
                    }
                    catch (Exception ex)
                    {
                        if (!Util.ProcessUtil.CreateFolder(path))
                        {
                            MessageBox.Show(this, "Unable to create folder\n\n" + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            return;
                        }
                    }
                }

                var success = false;
                var created = false;

                try
                {
                    path = Util.FileUtil.GetTemporaryFolderName(path);
                    if (path == null)
                    {
                        throw new Exception("Unable to create temporary folder");
                    }
                    Directory.CreateDirectory(path);

                    created = true;

                    //bool canLink;

                    //try
                    //{
                    //    CreateLink(exe, path, gwpath, false);
                    //    canLink = true;
                    //}
                    //catch
                    //{
                    //    canLink = false;
                    //}

                    //var files = new List<formCopyFileDialog.FilePath>();

                    //if (canLink)
                    //{
                    //    CreateLink("GwLoginClient.dll", path, gwpath, false);
                    //    if (checkLinkScreenshots.Checked)
                    //        CreateLink("Screens", path, gwpath, true);
                    //    if (checkLinkTemplates.Checked)
                    //        CreateLink("Templates", path, gwpath, true);
                    //}
                    //else
                    //{
                    //    files.Add(new formCopyFileDialog.FilePath(Path.Combine(gwpath, exe), Path.Combine(path, exe)));
                    //    files.Add(new formCopyFileDialog.FilePath(Path.Combine(gwpath, "GwLoginClient.dll"), Path.Combine(path, "GwLoginClient.dll")));
                    //}

                    var datSource = Path.Combine(gwpath, "Gw.dat");
                    var dat       = Path.Combine(path, "Gw.dat");

                    if (!File.Exists(datSource))
                    {
                        MessageBox.Show(this, "Unable to location Gw.dat", "Gw.dat not found", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }

                    var files = new formCopyFileDialog.FilePath[]
                    {
                        new formCopyFileDialog.FilePath(datSource, dat)
                    };

                    using (var f = new formCopyFileDialog(files))
                    {
                        var r = f.ShowDialog(this);

                        if (r == System.Windows.Forms.DialogResult.OK)
                        {
                            success           = true;
                            this.Result       = new SelectedFile(dat, SelectedFile.FileFlags.Created);
                            this.DialogResult = System.Windows.Forms.DialogResult.OK;
                            return;
                        }
                        else if (r == System.Windows.Forms.DialogResult.Cancel && f.CancelReason != null)
                        {
                            if (f.CancelReason is System.ComponentModel.Win32Exception)
                            {
                                switch (((System.ComponentModel.Win32Exception)f.CancelReason).NativeErrorCode)
                                {
                                case 32:

                                    throw new IOException("Gw.dat is currently being used and cannot be copied");
                                }
                            }

                            throw f.CancelReason;
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(this, ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                finally
                {
                    if (!success)
                    {
                        if (created)
                        {
                            try
                            {
                                Util.FileUtil.DeleteDirectory(path);
                            }
                            catch { }
                        }
                    }
                }
            }
            else if (textExisting.Text.Equals((string)textExisting.Tag, StringComparison.OrdinalIgnoreCase))
            {
                this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            }
            else
            {
                if (textExisting.TextLength > 0 && File.Exists(textExisting.Text))
                {
                    var path = Path.GetFullPath(textExisting.Text);
                    Settings.IGwDatFile dat = null;

                    foreach (var uid in Settings.GwDatFiles.GetKeys())
                    {
                        var d = Settings.GwDatFiles[uid];
                        if (d.HasValue && d.Value.Path.Equals(path, StringComparison.OrdinalIgnoreCase))
                        {
                            dat = d.Value;
                            break;
                        }
                    }

                    if (dat != null)
                    {
                        if (dat == existing)
                        {
                            this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
                            return;
                        }

                        if (dat.References == 1) //assuming if more than 1 account is already using it, the user doesn't care
                        {
                            if (MessageBox.Show(this, "Another account is already using this file.\n\nAre you sure?", "Are you sure?", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation) != System.Windows.Forms.DialogResult.Yes)
                            {
                                return;
                            }
                        }

                        this.Result = new SelectedFile(dat);
                    }
                    else
                    {
                        this.Result = new SelectedFile(path, SelectedFile.FileFlags.None);
                    }

                    this.DialogResult = System.Windows.Forms.DialogResult.OK;
                }
                else
                {
                    MessageBox.Show(this, "The selected path does not exist", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }