private Image TryGetPlanetTexture(string name, MyModContext context, string p, out string fullPath)
        {
            bool found = false;

            name    += p;
            fullPath = Path.Combine(context.ModPathData, "PlanetDataFiles", name) + ".png";

            // Check for modded textures
            if (!context.IsBaseGame)
            {
                if (!MyFileSystem.FileExists(fullPath))
                {
                    fullPath = Path.Combine(context.ModPathData, "PlanetDataFiles", name) + ".dds";
                    if (MyFileSystem.FileExists(fullPath))
                    {
                        found = true;
                    }
                }
                else
                {
                    found = true;
                }
            }

            // Check for default textures
            if (!found)
            {
                fullPath = Path.Combine(m_planetDataFolder, name) + ".png";

                if (!MyFileSystem.FileExists(fullPath))
                {
                    fullPath = Path.Combine(m_planetDataFolder, name) + ".dds";
                    if (!MyFileSystem.FileExists(fullPath))
                    {
                        return(null);
                    }
                }
            }

            if (fullPath.Contains(".sbm"))
            {
                string archivePath             = fullPath.Substring(0, fullPath.IndexOf(".sbm") + 4);
                string fileRelativeArchivePath = fullPath.Replace(archivePath + "\\", "");
                using (var sbm = VRage.Compression.MyZipArchive.OpenOnFile(archivePath))
                {
                    try
                    {
                        return(SharpDXImage.Load(sbm.GetFile(fileRelativeArchivePath).GetStream()));
                    }
                    catch (Exception ex)
                    {
                        MyDebug.FailRelease("Failed to load existing " + p + " file from .sbm archive. " + fullPath);
                        return(null);
                    }
                }
            }

            return(SharpDXImage.Load(fullPath));
        }
        private static Image LoadTexture(string path)
        {
            if (!MyFileSystem.FileExists(path))
            {
                return(null);
            }

            using (Stream textureStream = MyFileSystem.OpenRead(path))
            {
                return(textureStream != null?SharpDXImage.Load(textureStream) : null);
            }
        }
        public MyTileTexture <byte> GetTerrainBlendTexture(MyPlanetMaterialBlendSettings settings)
        {
            MyTileTexture <byte> tex;

            string path     = settings.Texture;
            int    cellSize = settings.CellSize;

            if (!m_ditherTilesets.TryGetValue(path, out tex))
            {
                string fullPath = Path.Combine(MyFileSystem.ContentPath, path) + ".png";
                if (!File.Exists(fullPath))
                {
                    fullPath = Path.Combine(MyFileSystem.ContentPath, path) + ".dds";
                }

                SharpDXImage image = null;
                try
                {
                    image = SharpDXImage.Load(fullPath);
                }
                catch (Exception e)
                {
                    MyLog.Default.WriteLine(e.Message);
                }

                if (image == null)
                {
                    return(MyTileTexture <byte> .Default);
                }

                PixelBuffer buffer = image.GetPixelBuffer(0, 0, 0);

                Debug.Assert(buffer.Format == Format.R8_UNorm);

                if (buffer.Format != Format.R8_UNorm)
                {
                    return(MyTileTexture <byte> .Default);
                }

                tex = new MyTileTexture <byte>(buffer, cellSize);
                image.Dispose();
            }

            return(tex);
        }
        public MyHeightmapFace GetHeightMap(string folderName, string faceName, MyModContext context)
        {
            ProfilerShort.Begin("MyHeightmapLoadingSystem::GetHeightMap()");
            if (m_first)
            {
                PreloadCrashingData();
                m_first = false;
            }
            string fullPath = null;
            bool   found    = false;


            // Look for modded textures
            if (!context.IsBaseGame)
            {
                fullPath = Path.Combine(Path.Combine(context.ModPathData, "PlanetDataFiles"), folderName, faceName);
                if (MyFileSystem.FileExists(fullPath + ".png"))
                {
                    found     = true;
                    fullPath += ".png";
                }
                else if (MyFileSystem.FileExists(fullPath + ".dds"))
                {
                    found     = true;
                    fullPath += ".dds";
                }
            }

            // Use default ones
            if (!found)
            {
                fullPath = Path.Combine(m_planetDataFolder, folderName, faceName);
                if (MyFileSystem.FileExists(fullPath + ".png"))
                {
                    found     = true;
                    fullPath += ".png";
                }
                else if (MyFileSystem.FileExists(fullPath + ".dds"))
                {
                    fullPath += ".dds";
                }
            }

            MyHeightmapFace value;

            if (m_heightMaps.TryGetValue(fullPath, out value))
            {
                ProfilerShort.End();
                return(value);
            }
            try
            {
                using (SharpDXImage image = LoadTexture(fullPath))
                {
                    if (image == null)
                    {
                        MyLog.Default.WriteLine("Could not load texture {0}, no suitable format found. " + fullPath);
                    }
                    else
                    {
                        PixelBuffer buffer = image.GetPixelBuffer(0, 0, 0);

                        value = new MyHeightmapFace(buffer.Height);

                        if (buffer.Format == Format.R16_UNorm)
                        {
                            PrepareHeightMap(value, buffer);
                        }
                        else if (buffer.Format == Format.R8_UNorm)
                        {
                            PrepareHeightMap8Bit(value, buffer);
                        }
                        else
                        {
                            MyDebug.FailRelease(String.Format("Heighmap texture {0}: Invalid format {1} (expecting R16_UNorm or R8_UNorm).", fullPath, buffer.Format));
                        }
                        buffer = null;
                    }
                }
                m_heightMaps[fullPath] = value;
            }
            catch (Exception e)
            {
                MyLog.Default.WriteLine(e.Message);
            }
            ProfilerShort.End();

            return(value);
        }