private void SaveBtn_Click(object sender, EventArgs e)
        {
            string serverZip = Settings.Default.LastUsedZip;
            string appPath   = Path.Combine(Application.StartupPath, "Temp");

            // Show task form
            TaskForm.Show(this, "Saving Archive", "Saving Changes to Archive", false);

            // Get our AI files
            TaskForm.UpdateStatus("Fetching AI Files...");
            IEnumerable <string> files = (
                from x in Directory.GetFiles(appPath, "*.ai", SearchOption.AllDirectories)
                let dir = x.Remove(0, appPath.Length + 1)
                          select dir
                );

            // Save all AI files
            TaskForm.UpdateStatus("Saving AI file changes...");
            foreach (TreeNode N in treeView1.Nodes)
            {
                SaveAiFiles(N);
            }

            // Create Backup!
            if (!File.Exists(serverZip + ".original"))
            {
                TaskForm.UpdateStatus("Creating Backup of server Archive...");
                File.Copy(serverZip, serverZip + ".original");
                RestoreBtn.Enabled = true;
            }

            // Remove readonly from zip
            FileInfo zipFile = new FileInfo(serverZip);

            zipFile.Attributes = FileAttributes.Normal;

            // Save files to zip
            using (ZipFile file = new ZipFile(serverZip))
            {
                foreach (string pth in files)
                {
                    TaskForm.UpdateStatus("Updating Entries...");

                    // Remove old entry
                    file.RemoveEntry(pth);

                    // Set file attributes to read only on the file, and add it to the zip
                    ZipEntry t = file.AddEntry(pth, File.OpenRead(Path.Combine(appPath, pth)));
                    t.Attributes = FileAttributes.ReadOnly;
                }

                file.Save();
            }

            zipFile.Attributes = FileAttributes.ReadOnly;
            TaskForm.CloseForm();
        }
Exemple #2
0
        /// <summary>
        /// Open and displays the task form.
        /// </summary>
        /// <param name="Parent">The calling form, so the task form can be centered</param>
        /// <param name="WindowTitle">The task dialog window title</param>
        /// <param name="InstructionText">Instruction text displayed after the info icon. Leave null
        /// to hide the instruction text and icon.</param>
        /// <param name="SubMessage">Detail text that is displayed just above the progress bar</param>
        /// <param name="Cancelable">Specifies whether the operation can be canceled</param>
        /// <param name="Style">The progress bar style</param>
        /// <exception cref="Exception">Thrown if the Task form is already open and running. Use the IsOpen property
        /// to determine if the form is already running</exception>
        public static void Show(Form Parent, string WindowTitle, string InstructionText, string SubMessage, bool Cancelable, ProgressBarStyle Style, int ProgressBarSteps)
        {
            // Make sure we dont have an already active form
            if (Instance != null && !Instance.IsDisposed)
            {
                throw new Exception("Task Form is already being displayed!");
            }

            // Create new instance
            Instance      = new TaskForm();
            Instance.Text = WindowTitle;
            Instance.labelInstructionText.Text = InstructionText;
            Instance.labelContent.Text         = SubMessage;
            Instance.Cancelable        = Cancelable;
            Instance.progressBar.Style = Style;

            // Setup progress bar
            if (ProgressBarSteps > 0)
            {
                double Percent = (100 / ProgressBarSteps);
                Instance.progressBar.Step    = (int)Math.Round(Percent, 0);
                Instance.progressBar.Maximum = Instance.progressBar.Step * ProgressBarSteps;
            }

            // Hide Instruction panel if Instruction Text is empty
            if (String.IsNullOrWhiteSpace(InstructionText))
            {
                Instance.panelMain.Hide();
                Instance.labelContent.Location    = new Point(10, 15);
                Instance.labelContent.MaximumSize = new Size(410, 0);
                Instance.labelContent.Size        = new Size(410, 0);
                Instance.progressBar.Location     = new Point(10, 1);
                Instance.progressBar.Size         = new Size(410, 18);
            }

            // Hide Cancel
            if (!Cancelable)
            {
                Instance.panelButton.Hide();
                Instance.Padding   = new Padding(0, 0, 0, 15);
                Instance.BackColor = Color.White;
            }

            // Set window position to center parent
            double H = Parent.Location.Y + (Parent.Height / 2) - (Instance.Height / 2);
            double W = Parent.Location.X + (Parent.Width / 2) - (Instance.Width / 2);

            Instance.Location = new Point((int)Math.Round(W, 0), (int)Math.Round(H, 0));

            // Run form in a new thread
            Thread thread = new Thread(new ThreadStart(ShowForm));

            thread.IsBackground = true;
            thread.SetApartmentState(ApartmentState.STA);
            thread.Start();
            Thread.Sleep(100); // Wait for Run to work
        }
        /// <summary>
        /// Event fired when the "Load Archive" button is clicked
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void NewProfileBtn_Click(object sender, EventArgs e)
        {
            OpenFileDialog Dialog = new OpenFileDialog();

            Dialog.FileName = "Objects_Server.zip";
            Dialog.Filter   = "Zip File|*.zip";

            // Set the initial search directory if we found an install path via registry
            if (!String.IsNullOrWhiteSpace(Settings.Default.LastUsedPath))
            {
                Dialog.InitialDirectory = Settings.Default.LastUsedPath;
            }

            // Show Dialog
            if (Dialog.ShowDialog() == DialogResult.OK)
            {
                // Save our last used directory
                string file = Dialog.FileName;
                Settings.Default.LastUsedPath = Path.GetDirectoryName(file);
                Settings.Default.LastUsedZip  = file;
                Settings.Default.Save();

                // Show our task form
                TaskForm.Show(this, "Loading Archive", "Extracting Archive", false);
                TaskForm.UpdateStatus("Removing old temp files...");

                // Clear old temp data
                string targetDirectory = Path.Combine(Application.StartupPath, "Temp");
                if (Directory.Exists(targetDirectory))
                {
                    Directory.Delete(targetDirectory, true);
                    Thread.Sleep(250);
                }

                // Load zip
                TaskForm.UpdateStatus("Extracting Zip Contents...");
                using (ZipFile Zip = ZipFile.Read(file))
                {
                    // Extract just the files we need
                    ZipEntry[] Entries = Zip.Entries.Where(
                        x => (
                            x.FileName.StartsWith("Vehicles", StringComparison.InvariantCultureIgnoreCase) ||
                            x.FileName.StartsWith("Weapons", StringComparison.InvariantCultureIgnoreCase) ||
                            x.FileName.StartsWith("Kits", StringComparison.InvariantCultureIgnoreCase)
                            ) && x.FileName.EndsWith(".ai", StringComparison.InvariantCultureIgnoreCase)
                        ).ToArray();

                    // Extract entries
                    foreach (ZipEntry entry in Entries)
                    {
                        // Extract the entry
                        entry.Extract(targetDirectory, ExtractExistingFileAction.Throw);

                        // Get rid of read only attributes!
                        FileInfo F = new FileInfo(Path.Combine(targetDirectory, entry.FileName));
                        F.Attributes = FileAttributes.Normal;
                    }
                }

                // Prase the extracted ai files
                ParseTemplates();
            }
        }
        /// <summary>
        /// Loads all of the *.ai files located in the SubDir name, into
        /// the ObjectManager
        /// </summary>
        /// <param name="Name"></param>
        protected void LoadTemplates(string Name)
        {
            string   path    = Path.Combine(Application.StartupPath, "Temp", Name);
            TreeNode topNode = new TreeNode(Name);

            // Make sure our tempalte directory exists
            if (!Directory.Exists(path))
            {
                return;
            }

            // Now loop through each object type
            foreach (string dir in Directory.EnumerateDirectories(path))
            {
                string dirName = dir.Remove(0, path.Length + 1);

                // Skip common folder
                if (dirName.ToLowerInvariant() == "common")
                {
                    continue;
                }

                TreeNode dirNode = new TreeNode(dirName);
                foreach (string subdir in Directory.EnumerateDirectories(dir))
                {
                    string subdirName = subdir.Remove(0, dir.Length + 1);

                    // Skip common folder
                    if (subdirName.ToLowerInvariant() == "common")
                    {
                        continue;
                    }

                    // Skip dirs that dont have an ai folder
                    if (!Directory.Exists(Path.Combine(subdir, "ai")))
                    {
                        continue;
                    }

                    TreeNode subNode = new TreeNode(subdirName);
                    Dictionary <AiFileType, AiFile> files = new Dictionary <AiFileType, AiFile>();

                    // Load the Objects.ai file if we have one
                    if (File.Exists(Path.Combine(subdir, "ai", "Objects.ai")))
                    {
                        TaskForm.UpdateStatus("Loading: " + Path.Combine(subdirName, "ai", "Object.ai"));
                        AiFile file = new AiFile(Path.Combine(subdir, "ai", "Objects.ai"), AiFileType.Vehicle);
                        if (ObjectManager.RegisterFileObjects(file))
                        {
                            files.Add(AiFileType.Vehicle, file);
                        }
                    }

                    // Load the Weapons.ai file if we have one
                    if (File.Exists(Path.Combine(subdir, "ai", "Weapons.ai")))
                    {
                        TaskForm.UpdateStatus("Loading: " + Path.Combine(subdirName, "ai", "Weapons.ai"));
                        AiFile file = new AiFile(Path.Combine(subdir, "ai", "Weapons.ai"), AiFileType.Weapon);
                        if (ObjectManager.RegisterFileObjects(file))
                        {
                            files.Add(AiFileType.Weapon, file);
                        }
                    }

                    if (files.Count > 0)
                    {
                        subNode.Tag = files;
                        dirNode.Nodes.Add(subNode);
                    }
                }

                if (dirNode.Nodes.Count > 0)
                {
                    topNode.Nodes.Add(dirNode);
                }
            }

            if (topNode.Nodes.Count > 0)
            {
                treeView1.Nodes.Add(topNode);
            }
        }
Exemple #5
0
 private void TaskForm_FormClosed(object sender, FormClosedEventArgs e)
 {
     Instance = null;
 }