Example #1
0
    public bool Load(string Filename)
    {
        if (string.IsNullOrEmpty(Filename))
        {
            return(false);
        }
        if (!System.IO.File.Exists(Filename))
        {
            return(false);
        }

        this.Filename = Filename;

        Clear();

        System.IO.FileStream fs     = new System.IO.FileStream(Filename, System.IO.FileMode.Open, System.IO.FileAccess.Read);
        BinaryReader         Stream = new BinaryReader(fs);

        PreviewTex       = new Texture2D(0, 0);
        TexturemapTex    = new Texture2D(0, 0);
        TexturemapTex2   = new Texture2D(0, 0);
        NormalmapTex     = new Texture2D(0, 0);
        WatermapTex      = new Texture2D(0, 0);
        WaterDataTexture = new Texture2D(0, 0);

        byte[] TexturemapData  = new byte[0];
        byte[] TexturemapData2 = new byte[0];
        byte[] NormalmapData   = new byte[0];
        byte[] WatermapData    = new byte[0];


        int Count = 0;

        BinaryReader _with1 = Stream;

        //# Header Section #
        if (_with1.ReadInt32() == MAP_MAGIC)
        {
            VersionMajor = _with1.ReadInt32();
            //? always 2
            Unknown10 = _with1.ReadInt32();
            //? always EDFE EFBE
            Unknown11 = _with1.ReadInt32();
            //? always 2
            Unknown16 = _with1.ReadSingle();
            //Map Width (in float)
            Unknown17 = _with1.ReadSingle();
            //Map Height (in float)
            Unknown12 = _with1.ReadSingle();
            //? always 0
            Unknown13 = _with1.ReadInt16();
            //? always 0
            PreviewImageLength = _with1.ReadInt32();
            PreviewData        = _with1.ReadBytes(PreviewImageLength);

            VersionMinor = _with1.ReadInt32();
            if (VersionMinor <= 0)
            {
                VersionMinor = 56;
            }


            //# Heightmap Section #
            Width  = _with1.ReadInt32();
            Height = _with1.ReadInt32();

            HeightScale = _with1.ReadSingle();
            //Height Scale, usually 1/128

            HeightmapData = new ushort[(Height + 1) * (Width + 1)];
            for (int i = 0; i < HeightmapData.Length; i++)
            {
                HeightmapData[i] = _with1.ReadUInt16();
            }
            //HeightmapData = _with1.ReadInt16Array((Height + 1) * (Width + 1));//TODO: Current saving method gets a memory overload on trying to reload the map here.
            //heightmap dimension is always 1 more than texture dimension!

            if (VersionMinor >= 56)
            {
                Unknown15 = _with1.ReadByte();
            }
            else
            {
                Unknown15 = 0;
            }
            //Always 0?

            //# Texture Definition Section #
            TerrainShader = _with1.ReadStringNull();
            //Terrain Shader, usually "TTerrain"
            TexPathBackground = _with1.ReadStringNull();
            TexPathSkyCubemap = _with1.ReadStringNull();

            if (VersionMinor >= 56)
            {
                Count = _with1.ReadInt32();
                //always 1?
                EnvCubemapsName = new string[Count];
                EnvCubemapsFile = new string[Count];
                for (int i = 0; i <= Count - 1; i++)
                {
                    EnvCubemapsName[i] = _with1.ReadStringNull();
                    EnvCubemapsFile[i] = _with1.ReadStringNull();
                }
            }
            else
            {
                EnvCubemapsName    = new string[3];
                EnvCubemapsFile    = new string[3];
                EnvCubemapsName[0] = "<aeon>";
                EnvCubemapsName[1] = "<default>";
                EnvCubemapsName[2] = "<seraphim>";
                EnvCubemapsFile[0] = "/textures/environment/envcube_aeon_evergreen.dds";
                EnvCubemapsFile[1] = _with1.ReadStringNull();
                EnvCubemapsFile[2] = "/textures/environment/envcube_seraphim_evergreen.dds";
            }

            LightingMultiplier = _with1.ReadSingle();
            SunDirection       = _with1.ReadVector3();
            SunAmbience        = _with1.ReadVector3();
            SunColor           = _with1.ReadVector3();
            ShadowFillColor    = _with1.ReadVector3();
            SpecularColor      = _with1.ReadVector4();
            Bloom = _with1.ReadSingle();

            FogColor = _with1.ReadVector3();
            FogStart = _with1.ReadSingle();
            FogEnd   = _with1.ReadSingle();


            Water.Load(Stream);

            Count = _with1.ReadInt32();
            WaveGenerators.Clear();
            for (int i = 0; i <= Count - 1; i++)
            {
                WaveGenerator WaveGen = new WaveGenerator();
                WaveGen.Load(Stream);
                WaveGenerators.Add(WaveGen);
            }

            if (VersionMinor < 56)
            {
                _with1.ReadStringNull();
                // always "No Tileset"
                Count = _with1.ReadInt32();
                //always 6
                for (int i = 0; i <= 4; i++)
                {
                    Layer Layer = new Layer();
                    Layer.Load(Stream);
                    Layers.Add(Layer);
                }
                for (int i = 5; i <= 8; i++)
                {
                    Layers.Add(new Layer());
                }
                for (int i = 9; i <= 9; i++)
                {
                    Layer Layer = new Layer();
                    Layer.Load(Stream);
                    Layers.Add(Layer);
                }
            }
            else
            {
                MinimapContourInterval = _with1.ReadInt32();

                int argb = _with1.ReadInt32();
                MinimapDeepWaterColor = Int32ToColor(argb);

                /*int r = (argb)&0xFF;
                 * int g = (argb>>8)&0xFF;
                 * int b = (argb>>16)&0xFF;
                 * int a = (argb>>24)&0xFF;
                 * MinimapDeepWaterColor = new Color(r,g,b,a);*/
                int argb2 = _with1.ReadInt32();

                /*int r2 = (argb2)&0xFF;
                 * int g2 = (argb2>>8)&0xFF;
                 * int b2 = (argb2>>16)&0xFF;
                 * int a2 = (argb2>>24)&0xFF;
                 * MinimapContourColor = new Color(r2,g2,b2,a2);*/
                MinimapContourColor = Int32ToColor(argb2);
                int argb3 = _with1.ReadInt32();

                /*int r3 = (argb3)&0xFF;
                 * int g3 = (argb3>>8)&0xFF;
                 * int b3 = (argb3>>16)&0xFF;
                 * int a3 = (argb3>>24)&0xFF;
                 * MinimapShoreColor = new Color(r3,g3,b3,a3);*/
                MinimapShoreColor = Int32ToColor(argb3);
                int argb4 = _with1.ReadInt32();

                /*int r4 = (argb4)&0xFF;
                 * int g4 = (argb4>>8)&0xFF;
                 * int b4 = (argb4>>16)&0xFF;
                 * int a4 = (argb4>>24)&0xFF;
                 * MinimapLandStartColor = new Color(r4,g4,b4,a4);*/
                MinimapLandStartColor = Int32ToColor(argb4);
                int argb5 = _with1.ReadInt32();

                /*int r5 = (argb5)&0xFF;
                 * int g5 = (argb5>>8)&0xFF;
                 * int b5 = (argb5>>16)&0xFF;
                 * int a5 = (argb5>>24)&0xFF;
                 * MinimapLandEndColor = new Color(r5,g5,b5,a5);*/
                MinimapLandEndColor = Int32ToColor(argb5);

                if (VersionMinor > 56)
                {
                    Unknown14 = _with1.ReadSingle();                     //Not sure what this is.
                }
                Count = 10;
                for (int i = 0; i <= Count - 1; i++)
                {
                    Layer Layer = new Layer();
                    Layer.LoadAlbedo(Stream);
                    Layers.Add(Layer);
                }
                for (int i = 0; i <= Count - 2; i++)
                {
                    Layers[i].LoadNormal(Stream);
                }
            }
            Unknown7 = _with1.ReadInt32();
            //?
            Unknown8 = _with1.ReadInt32();
            //?

            int DecalCount = _with1.ReadInt32();
            //Debug.Log(DecalCount);
            for (int i = 0; i < DecalCount; i++)
            {
                Decal Feature = new Decal();
                Feature.Load(Stream);
                Decals.Add(Feature);
            }

            int GroupCount = _with1.ReadInt32();
            for (int i = 0; i <= GroupCount - 1; i++)
            {
                IntegerGroup Group = new IntegerGroup();
                Group.Load(Stream);
                DecalGroups.Add(Group);
            }

            _with1.ReadInt32();
            //Width again
            _with1.ReadInt32();
            //Height again

            int Length         = 0;
            int NormalmapCount = _with1.ReadInt32();
            //always 1
            for (int i = 0; i < NormalmapCount; i++)
            {
                Length = _with1.ReadInt32();
                if (i == 0)
                {
                    NormalmapData = _with1.ReadBytes(Length);
                }
                else
                {
                    _with1.BaseStream.Position += Length;
                    // just to make sure that it doesn't crash if it is not just 1 normalmap for some reason
                }
            }


            if (VersionMinor < 56)
            {
                _with1.ReadInt32();
            }
            //always 1
            Length         = _with1.ReadInt32();
            TexturemapData = _with1.ReadBytes(Length);

            if (VersionMinor >= 56)
            {
                Length          = _with1.ReadInt32();
                TexturemapData2 = _with1.ReadBytes(Length);
            }


            //Watermap
            _with1.ReadInt32();
            //always 1
            Length       = _with1.ReadInt32();
            WatermapData = _with1.ReadBytes(Length);

            int HalfSize = (Width / 2) * (Height / 2);
            WaterFoamMask      = _with1.ReadBytes(HalfSize);
            WaterFlatnessMask  = _with1.ReadBytes(HalfSize);
            WaterDepthBiasMask = _with1.ReadBytes(HalfSize);

            WaterDataTexture = new Texture2D(Width / 2, Height / 2, TextureFormat.RGB24, false);
            Color32[] NewColors = new Color32[HalfSize];
            for (int i = 0; i < HalfSize; i++)
            {
                NewColors[i] = new Color32(WaterFoamMask[i], WaterFlatnessMask[i], WaterDepthBiasMask[i], 0);
            }
            WaterDataTexture.SetPixels32(NewColors);
            WaterDataTexture.Apply();

            TerrainTypeData = _with1.ReadBytes(Width * Height);
            //            TerrainTypeData = _with1.ReadBytes(Width * Height).ToList();

            if (VersionMinor <= 52)
            {
                _with1.ReadInt16();
            }


            //Debug.Log("Scmap file version: " + VersionMinor);

            if (VersionMinor >= 60)
            {
                AdditionalSkyboxData.Load(_with1);
            }
            else
            {
                ScmapEditor.Current.LoadDefaultSkybox();
            }


            try
            {
                int PropCount = _with1.ReadInt32();
                //Debug.Log ("PropCount: " + PropCount + ", v" + VersionMinor );
                for (int i = 0; i < PropCount; i++)
                {
                    Prop Prop = new Prop();
                    Prop.Load(Stream);
                    Props.Add(Prop);
                    //Debug.Log(Prop.BlueprintPath);
                }
            }
            catch
            {
                Debug.LogError("Loading props crashed");
            }
        }
        _with1.Close();
        fs.Close();
        fs.Dispose();

        PreviewTextHeader        = GetGamedataFile.GetDdsFormat(PreviewData);
        PreviewTextHeader.Format = TextureFormat.BGRA32;
        PreviewTex = TextureLoader.ConvertToRGBA(TextureLoader.LoadTextureDXT(PreviewData, PreviewTextHeader));

        try
        {
            TextureMapHeader        = GetGamedataFile.GetDdsFormat(TexturemapData);
            TextureMapHeader.Format = TextureFormat.BGRA32;
            //TexturemapTex = TextureLoader.LoadTextureDXT(TexturemapData, TextureMapHeader);
            TexturemapTex  = TextureLoader.ConvertToRGBA(TextureLoader.LoadTextureDXT(TexturemapData, TextureMapHeader));
            TexturemapData = new byte[0];
        }
        catch (Exception e)
        {
            TexturemapTex = new Texture2D(Width / 2, Height / 2);
            Debug.LogException(e);
        }

        if (TexturemapData2.Length > 0)
        {
            TextureMap2Header        = GetGamedataFile.GetDdsFormat(TexturemapData2);
            TextureMap2Header.Format = TextureFormat.BGRA32;
            TexturemapTex2           = TextureLoader.ConvertToRGBA(TextureLoader.LoadTextureDXT(TexturemapData2, TextureMap2Header));
            TexturemapData2          = new byte[0];
        }
        else
        {
            TextureMap2Header = TextureMapHeader;
            TexturemapTex2    = new Texture2D(Width / 2, Height / 2);
            Color[] Pixels = TexturemapTex2.GetPixels();
            for (int p = 0; p < Pixels.Length; p++)
            {
                Pixels[p] = new Color(0, 0, 0, 0);
            }
            TexturemapTex2.SetPixels(Pixels);
            TexturemapTex2.Apply();
        }
        TexturemapTex.wrapMode  = TextureWrapMode.Clamp;
        TexturemapTex2.wrapMode = TextureWrapMode.Clamp;

        NormalmapHeader        = GetGamedataFile.GetDdsFormat(NormalmapData);
        NormalmapHeader.Format = TextureFormat.DXT5;
        NormalmapTex           = TextureLoader.LoadTextureDXT(NormalmapData, NormalmapHeader);
        NormalmapData          = new byte[0];

        UncompressedNormalmapTex = new Texture2D(NormalmapTex.width, NormalmapTex.height, TextureFormat.RGBA32, false);
        UncompressedNormalmapTex.SetPixels(NormalmapTex.GetPixels());
        UncompressedNormalmapTex.wrapMode = TextureWrapMode.Clamp;
        UncompressedNormalmapTex.Apply();

        try
        {
            WatermapHeader        = GetGamedataFile.GetDdsFormat(WatermapData);
            WatermapHeader.Format = TextureFormat.DXT5;
            WatermapTex           = TextureLoader.LoadTextureDXT(WatermapData, WatermapHeader);
            WatermapData          = new byte[0];
        }
        catch (Exception e)
        {
            WatermapTex = new Texture2D(Width / 2, Height / 2);
            Debug.LogException(e);
        }

        UncompressedWatermapTex = new Texture2D(WatermapTex.width, WatermapTex.height, TextureFormat.RGBA32, false);
        UncompressedWatermapTex.SetPixels(WatermapTex.GetPixels());
        UncompressedWatermapTex.wrapMode = TextureWrapMode.Clamp;

        UncompressedWatermapTex.Apply();

        if (VersionMinor < 56)
        {
            ConvertToV56();
        }

        return(true);
    }