/// <summary> /// Loads every .ai file and parses it for use within this form /// </summary> protected void ParseTemplates() { // Clear nodesand Globals! treeView1.Nodes.Clear(); ObjectManager.ReleaseAll(); // OPen task form if it isnt open if (!TaskForm.IsOpen) { TaskForm.Show(this, "Parsing AI FIles", "Parsing AI Files", false); } // Load kit Templates TaskForm.UpdateStatus("Loading Kits, Please Wait..."); LoadKitTemplate(); // Load Vehicle templates TaskForm.UpdateStatus("Loading Vehicles, Please Wait..."); LoadTemplates("Vehicles"); // Load Weapon templates TaskForm.UpdateInstructionText("Lodding Weapons, Please Wait..."); LoadTemplates("Weapons"); TaskForm.CloseForm(); // Set desc DescLabel.Text = "Loaded Archive: " + ShrinkPath(Settings.Default.LastUsedZip, 95); // Enable action buttons SaveBtn.Enabled = true; RestoreBtn.Enabled = File.Exists(Settings.Default.LastUsedZip + ".original"); RefreshBtn.Enabled = true; }
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(); }
/// <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); } }