Beispiel #1
0
        //saving/loading ----------------------------------------------------
        public bool SaveAs(string filePath, System.IO.MemoryStream previewImage = null)
        {
            var newElement = new XElement(SubmarineElement.Name,
                                          SubmarineElement.Attributes().Where(a => !string.Equals(a.Name.LocalName, "previewimage", StringComparison.InvariantCultureIgnoreCase) &&
                                                                              !string.Equals(a.Name.LocalName, "name", StringComparison.InvariantCultureIgnoreCase)),
                                          SubmarineElement.Elements());

            if (Type == SubmarineType.OutpostModule)
            {
                OutpostModuleInfo.Save(newElement);
                OutpostModuleInfo = new OutpostModuleInfo(this, newElement);
            }
            XDocument doc = new XDocument(newElement);

            doc.Root.Add(new XAttribute("name", Name));

            if (previewImage != null)
            {
                doc.Root.Add(new XAttribute("previewimage", Convert.ToBase64String(previewImage.ToArray())));
            }
            try
            {
                SaveUtil.CompressStringToFile(filePath, doc.ToString());
                Md5Hash.RemoveFromCache(filePath);
            }
            catch (Exception e)
            {
                DebugConsole.ThrowError("Saving submarine \"" + filePath + "\" failed!", e);
                return(false);
            }

            return(true);
        }
Beispiel #2
0
        partial void InitProjectSpecific()
        {
            string previewImageData = SubmarineElement.GetAttributeString("previewimage", "");

            if (!string.IsNullOrEmpty(previewImageData))
            {
                try
                {
                    using (System.IO.MemoryStream mem = new System.IO.MemoryStream(Convert.FromBase64String(previewImageData)))
                    {
                        var texture = TextureLoader.FromStream(mem, path: FilePath, compress: false);
                        if (texture == null)
                        {
                            throw new Exception("PreviewImage texture returned null");
                        }
                        PreviewImage = new Sprite(texture, sourceRectangle: null, newOffset: null, path: FilePath);
                    }
                }
                catch (Exception e)
                {
                    DebugConsole.ThrowError("Loading the preview image of the submarine \"" + Name + "\" failed. The file may be corrupted.", e);
                    GameAnalyticsManager.AddErrorEventOnce("Submarine..ctor:PreviewImageLoadingFailed", GameAnalyticsSDK.Net.EGAErrorSeverity.Error,
                                                           "Loading the preview image of the submarine \"" + Name + "\" failed. The file may be corrupted.");
                    PreviewImage = null;
                }
            }
        }
Beispiel #3
0
        /// <summary>
        /// Calculated from <see cref="SubmarineElement"/>. Can be used when the sub hasn't been loaded and we can't access <see cref="Submarine.RealWorldCrushDepth"/>.
        /// </summary>
        public float GetRealWorldCrushDepth()
        {
            if (SubmarineElement == null)
            {
                return(Level.DefaultRealWorldCrushDepth);
            }
            bool  structureCrushDepthsDefined = false;
            float realWorldCrushDepth         = float.PositiveInfinity;

            foreach (var structureElement in SubmarineElement.GetChildElements("structure"))
            {
                string name            = structureElement.Attribute("name")?.Value ?? "";
                string identifier      = structureElement.GetAttributeString("identifier", "");
                var    structurePrefab = Structure.FindPrefab(name, identifier);
                if (structurePrefab == null || !structurePrefab.Body)
                {
                    continue;
                }
                if (!structureCrushDepthsDefined && structureElement.Attribute("crushdepth") != null)
                {
                    structureCrushDepthsDefined = true;
                }
                float structureCrushDepth = structureElement.GetAttributeFloat("crushdepth", float.PositiveInfinity);
                realWorldCrushDepth = Math.Min(structureCrushDepth, realWorldCrushDepth);
            }
            if (!structureCrushDepthsDefined)
            {
                realWorldCrushDepth = Level.DefaultRealWorldCrushDepth;
            }
            realWorldCrushDepth *= GetRealWorldCrushDepthMultiplier();
            return(realWorldCrushDepth);
        }
Beispiel #4
0
        private void Init()
        {
            DisplayName = TextManager.Get("Submarine.Name." + Name, true);
            if (string.IsNullOrEmpty(DisplayName))
            {
                DisplayName = Name;
            }

            Description = TextManager.Get("Submarine.Description." + Name, true);
            if (string.IsNullOrEmpty(Description))
            {
                Description = SubmarineElement.GetAttributeString("description", "");
            }

            GameVersion = new Version(SubmarineElement.GetAttributeString("gameversion", "0.0.0.0"));
            if (Enum.TryParse(SubmarineElement.GetAttributeString("tags", ""), out SubmarineTag tags))
            {
                Tags = tags;
            }
            Dimensions                = SubmarineElement.GetAttributeVector2("dimensions", Vector2.Zero);
            RecommendedCrewSizeMin    = SubmarineElement.GetAttributeInt("recommendedcrewsizemin", 0);
            RecommendedCrewSizeMax    = SubmarineElement.GetAttributeInt("recommendedcrewsizemax", 0);
            RecommendedCrewExperience = SubmarineElement.GetAttributeString("recommendedcrewexperience", "Unknown");

            //backwards compatibility (use text tags instead of the actual text)
            if (RecommendedCrewExperience == "Beginner")
            {
                RecommendedCrewExperience = "CrewExperienceLow";
            }
            else if (RecommendedCrewExperience == "Intermediate")
            {
                RecommendedCrewExperience = "CrewExperienceMid";
            }
            else if (RecommendedCrewExperience == "Experienced")
            {
                RecommendedCrewExperience = "CrewExperienceHigh";
            }

            RequiredContentPackages.Clear();
            string[] contentPackageNames = SubmarineElement.GetAttributeStringArray("requiredcontentpackages", new string[0]);
            foreach (string contentPackageName in contentPackageNames)
            {
                RequiredContentPackages.Add(contentPackageName);
            }

            InitProjectSpecific();
        }
Beispiel #5
0
        public SubmarineInfo(string filePath, string hash = "", XElement element = null, bool tryLoad = true)
        {
            FilePath = filePath;
            if (!string.IsNullOrEmpty(filePath) && File.Exists(filePath))
            {
                LastModifiedTime = File.GetLastWriteTime(filePath);
            }
            try
            {
                Name = DisplayName = Path.GetFileNameWithoutExtension(filePath);
            }
            catch (Exception e)
            {
                DebugConsole.ThrowError("Error loading submarine " + filePath + "!", e);
            }

            if (!string.IsNullOrWhiteSpace(hash))
            {
                this.hash = new Md5Hash(hash);
            }

            IsFileCorrupted = false;

            RequiredContentPackages = new HashSet <string>();

            if (element == null && tryLoad)
            {
                Reload();
            }
            else
            {
                SubmarineElement = element;
            }

            Name = SubmarineElement.GetAttributeString("name", null) ?? Name;

            Init();
        }
Beispiel #6
0
        //saving/loading ----------------------------------------------------
        public bool SaveAs(string filePath, MemoryStream previewImage = null)
        {
            var newElement = new XElement(SubmarineElement.Name,
                                          SubmarineElement.Attributes().Where(a => !string.Equals(a.Name.LocalName, "previewimage", StringComparison.InvariantCultureIgnoreCase)),
                                          SubmarineElement.Elements());
            XDocument doc = new XDocument(newElement);

            if (previewImage != null)
            {
                doc.Root.Add(new XAttribute("previewimage", Convert.ToBase64String(previewImage.ToArray())));
            }

            try
            {
                SaveUtil.CompressStringToFile(filePath, doc.ToString());
            }
            catch (Exception e)
            {
                DebugConsole.ThrowError("Saving submarine \"" + filePath + "\" failed!", e);
                return(false);
            }

            return(true);
        }
Beispiel #7
0
        private void Init()
        {
            DisplayName = TextManager.Get("Submarine.Name." + Name, true);
            if (string.IsNullOrEmpty(DisplayName))
            {
                DisplayName = Name;
            }

            Description = TextManager.Get("Submarine.Description." + Name, true);
            if (string.IsNullOrEmpty(Description))
            {
                Description = SubmarineElement.GetAttributeString("description", "");
            }

            EqualityCheckVal = SubmarineElement.GetAttributeInt("checkval", 0);

            Price = SubmarineElement.GetAttributeInt("price", 1000);

            InitialSuppliesSpawned = SubmarineElement.GetAttributeBool("initialsuppliesspawned", false);

            GameVersion = new Version(SubmarineElement.GetAttributeString("gameversion", "0.0.0.0"));
            if (Enum.TryParse(SubmarineElement.GetAttributeString("tags", ""), out SubmarineTag tags))
            {
                Tags = tags;
            }
            Dimensions                = SubmarineElement.GetAttributeVector2("dimensions", Vector2.Zero);
            RecommendedCrewSizeMin    = SubmarineElement.GetAttributeInt("recommendedcrewsizemin", 0);
            RecommendedCrewSizeMax    = SubmarineElement.GetAttributeInt("recommendedcrewsizemax", 0);
            RecommendedCrewExperience = SubmarineElement.GetAttributeString("recommendedcrewexperience", "Unknown");

            if (SubmarineElement?.Attribute("type") != null)
            {
                if (Enum.TryParse(SubmarineElement.GetAttributeString("type", ""), out SubmarineType type))
                {
                    Type = type;
                    if (Type == SubmarineType.OutpostModule)
                    {
                        OutpostModuleInfo = new OutpostModuleInfo(this, SubmarineElement);
                    }
                }
            }

            if (Type == SubmarineType.Player)
            {
                if (SubmarineElement?.Attribute("class") != null)
                {
                    if (Enum.TryParse(SubmarineElement.GetAttributeString("class", "Undefined"), out SubmarineClass submarineClass))
                    {
                        SubmarineClass = submarineClass;
                    }
                }
            }
            else
            {
                SubmarineClass = SubmarineClass.Undefined;
            }

            //backwards compatibility (use text tags instead of the actual text)
            if (RecommendedCrewExperience == "Beginner")
            {
                RecommendedCrewExperience = "CrewExperienceLow";
            }
            else if (RecommendedCrewExperience == "Intermediate")
            {
                RecommendedCrewExperience = "CrewExperienceMid";
            }
            else if (RecommendedCrewExperience == "Experienced")
            {
                RecommendedCrewExperience = "CrewExperienceHigh";
            }

            RequiredContentPackages.Clear();
            string[] contentPackageNames = SubmarineElement.GetAttributeStringArray("requiredcontentpackages", new string[0]);
            foreach (string contentPackageName in contentPackageNames)
            {
                RequiredContentPackages.Add(contentPackageName);
            }

            InitProjectSpecific();
        }