static GumProjectSave LoadFromTitleStorage(string fileName, LinkLoadingPreference linkLoadingPreference, GumLoadResult result) { using (System.IO.Stream stream = Microsoft.Xna.Framework.TitleContainer.OpenStream(fileName)) { GumProjectSave gps = FileManager.XmlDeserializeFromStream <GumProjectSave>(stream); gps.FullFileName = fileName; return(gps); } }
public static GumProjectSave Load(string fileName, LinkLoadingPreference linkLoadingPreference, out GumLoadResult result) { result = new GumLoadResult(); if (string.IsNullOrEmpty(fileName)) { result.ErrorMessage = "Passed null file name, could not load GumProjectSave"; return(null); } if (FileManager.IsRelative(fileName)) { fileName = FileManager.MakeAbsolute(fileName); } GumProjectSave gps = null; #if ANDROID || IOS || WINDOWS_8 gps = LoadFromTitleStorage(fileName, linkLoadingPreference, result); #else try { gps = FileManager.XmlDeserialize <GumProjectSave>(fileName); } catch (FileNotFoundException) { result.MissingFiles.Add(fileName); return(null); } catch (IOException ex) { result.ErrorMessage = ex.Message; return(null); } #endif string projectRootDirectory = FileManager.GetDirectory(fileName); gps.PopulateElementSavesFromReferences(projectRootDirectory, linkLoadingPreference, result); gps.FullFileName = fileName.Replace('\\', '/'); return(gps); }
private void PopulateElementSavesFromReferences(string projectRootDirectory, LinkLoadingPreference linkLoadingPreference, GumLoadResult result) { string errors = ""; Screens.Clear(); Components.Clear(); StandardElements.Clear(); Behaviors.Clear(); foreach (ElementReference reference in ScreenReferences) { ScreenSave toAdd = null; try { toAdd = reference.ToElementSave <ScreenSave>(projectRootDirectory, ScreenExtension, result); } catch (Exception e) { errors += "\nError loading " + reference.Name + ":\n" + e.Message; } if (toAdd != null) { Screens.Add(toAdd); } } foreach (ElementReference reference in ComponentReferences) { ComponentSave toAdd = null; try { toAdd = reference.ToElementSave <ComponentSave>(projectRootDirectory, ComponentExtension, result); } catch (Exception e) { errors += "\nError loading " + reference.Name + ":\n" + e.Message; } if (toAdd != null) { Components.Add(toAdd); } } foreach (ElementReference reference in StandardElementReferences) { StandardElementSave toAdd = null; try { toAdd = reference.ToElementSave <StandardElementSave>(projectRootDirectory, StandardExtension, result); } catch (Exception e) { errors += "\nError loading " + reference.Name + ":\n" + e.Message; } if (toAdd != null) { StandardElements.Add(toAdd); } } foreach (var reference in BehaviorReferences) { BehaviorSave toAdd = null; try { toAdd = reference.ToBehaviorSave(projectRootDirectory); } catch (Exception e) { errors += "\nError loading " + reference.Name + ":\n" + e.Message; } if (toAdd != null) { Behaviors.Add(toAdd); } } result.ErrorMessage += errors; }
//public ElementSave ToElementSave(string projectroot, string extension) //{ // string fullName = projectroot + Subfolder + "/" + Name + "." + extension; // ElementSave elementSave = FileManager.XmlDeserialize<ElementSave>(fullName); // return elementSave; //} public T ToElementSave <T>(string projectroot, string extension, GumLoadResult result, LinkLoadingPreference linkLoadingPreference = LinkLoadingPreference.PreferLinked) where T : ElementSave, new() { FilePath linkedName = null; FilePath containedReferenceName = null; if (!string.IsNullOrWhiteSpace(this.Link)) { linkedName = projectroot + this.Link; } containedReferenceName = projectroot + Subfolder + "/" + Name + "." + extension; if (linkedName != null && ToolsUtilities.FileManager.IsRelative(linkedName.Original)) { linkedName = ToolsUtilities.FileManager.RelativeDirectory + linkedName.Original; } if (ToolsUtilities.FileManager.IsRelative(containedReferenceName.Original)) { containedReferenceName = ToolsUtilities.FileManager.RelativeDirectory + containedReferenceName.Original; } if (linkedName?.Exists() == true) { T elementSave = FileManager.XmlDeserialize <T>(linkedName.FullPath); return(elementSave); } else if (containedReferenceName.Exists() && (linkedName == null || linkLoadingPreference == LinkLoadingPreference.PreferLinked)) { T elementSave = FileManager.XmlDeserialize <T>(containedReferenceName.FullPath); if (Name != elementSave.Name) { // The file name doesn't match the name of the element. This can cause errors // at runtime so let's tell the user: result.ErrorMessage += "\nThe project references an element named " + Name + ", but the XML for this element has its name set to " + elementSave.Name + "\n"; } return(elementSave); } else { // I don't think we want to consider this an error anymore // because Gum can handle it - it doesn't allow saving that // individual element and it shows a red ! next to the element. // We should just tolerate this and let the user deal with it. // If we do treat this as an error, then Gum goes into a state // where it can't save anything. //errors += "\nCould not find the file name " + fullName; // Update Feb 20, 2015 // But we can record it: result.MissingFiles.Add(containedReferenceName.FullPath); T elementSave = new T(); elementSave.Name = Name; elementSave.IsSourceFileMissing = true; return(elementSave); } }