/// <summary> /// Write settings to disk /// </summary> /// <param name="canQueue">True if multiple calls can be queued and then some /// milliseconds time later be turned into one save settings call.</param> public void SaveSettings(bool canQueue) { if (!Active) return; if (canQueue) { // Indicate that we need to save later m_saveSettingsNeeded = true; m_saveSettingsTime = DateTime.Now; return; } try { var readOnly = SledUtil.IsFileReadOnly(ActiveProject.Uri.LocalPath); // Fire event SavingSettings.Raise(this, new SledProjectServiceProjectEventArgs(ActiveProject)); try { // Remove read only flag if set if (readOnly) SledUtil.SetReadOnly(ActiveProject.Uri.LocalPath, false); // Write to disk var writer = new SledSpfWriter(SchemaLoader.TypeCollection); writer.Write(ActiveProject.DomNode, ActiveProject.Uri, true); } catch (Exception ex) { SledOutDevice.OutLine( SledMessageType.Error, "{0}: Exception encountered when writing " + "SLED project file \"{1}\" to disk: {2}", this, ActiveProject.Uri.LocalPath, ex.Message); } finally { ActiveProject.Dirty = false; // Add back read only flag (if previously set) if (readOnly) SledUtil.SetReadOnly(ActiveProject.Uri.LocalPath, true); } } finally { // Fire event SavedSettings.Raise(this, new SledProjectServiceProjectEventArgs(ActiveProject)); m_saveSettingsNeeded = false; m_saveSettingsTime = DateTime.Now; } }
public bool CreateProject(string name, string projDir, string assetDir, IEnumerable<string> absPathFiles) { bool bRetval; try { var szAbsSpfPath = Path.Combine(projDir + Path.DirectorySeparatorChar, name + ".spf"); var uri = new Uri(szAbsSpfPath); var guid = GetExistingProjectGuid(uri, SchemaLoader); // Does this exist? var projectFileFinderService = SledServiceInstance.TryGet<ISledProjectFileFinderService>(); var domNode = new DomNode( SledSchema.SledProjectFilesType.Type, SledSchema.SledProjectFilesRootElement); // Create project var tempProject = domNode.As<SledProjectFilesType>(); tempProject.Name = name; tempProject.Uri = uri; tempProject.AssetDirectory = assetDir; tempProject.Guid = guid; // Add each file to the project foreach (var file in absPathFiles) { var projFile = SledProjectFilesFileType.Create(file, tempProject); if (projFile == null) continue; // Check for duplicates if (projectFileFinderService != null) { // Check if this file already exists in the project var projFoundFile = projectFileFinderService.Find(projFile, tempProject); // Actual file that projFile represents is in the // project so this is a duplicate if (projFoundFile != null) continue; } // Add file to project tempProject.Files.Add(projFile); } // Write .spf to disk var writer = new SledSpfWriter(SchemaLoader.TypeCollection); writer.Write(tempProject.DomNode, uri, false); bRetval = true; } catch (Exception ex) { ex.ToString(); bRetval = false; } return bRetval; }
/// <summary> /// Open the project file and remove any duplicates /// </summary> /// <param name="szAbsPath"></param> public static void CleanupProjectFileDuplicates(string szAbsPath) { if (!File.Exists(szAbsPath)) return; if (SledUtil.IsFileReadOnly(szAbsPath)) return; try { var schemaLoader = SledServiceInstance.TryGet<SledSharedSchemaLoader>(); if (schemaLoader == null) return; var uri = new Uri(szAbsPath); var reader = new SledSpfReader(schemaLoader); var root = reader.Read(uri, false); if (root == null) return; var lstProjFiles = new List<SledProjectFilesFileType>(); // Gather up all project files in the project SledDomUtil.GatherAllAs(root, lstProjFiles); if (lstProjFiles.Count <= 1) return; var uniquePaths = new Dictionary<string, SledProjectFilesFileType>(StringComparer.CurrentCultureIgnoreCase); var lstDuplicates = new List<SledProjectFilesFileType>(); foreach (var projFile in lstProjFiles) { if (uniquePaths.ContainsKey(projFile.Path)) lstDuplicates.Add(projFile); else uniquePaths.Add(projFile.Path, projFile); } if (lstDuplicates.Count <= 0) return; foreach (var projFile in lstDuplicates) projFile.DomNode.RemoveFromParent(); var writer = new SledSpfWriter(schemaLoader.TypeCollection); // Write changes back to disk writer.Write(root, uri, false); } catch (Exception ex) { SledOutDevice.OutLine( SledMessageType.Error, SledUtil.TransSub(Localization.SledProjectFilesErrorRemovingDuplicates, ex.Message, szAbsPath)); } }