示例#1
0
        static DS1 Load(byte[] bytes)
        {
            using (var stream = new MemoryStream(bytes))
                using (var reader = new BinaryReader(stream))
                {
                    DS1 ds1 = new DS1();
                    ds1.version = reader.ReadInt32();
                    ds1.width   = reader.ReadInt32() + 1;
                    ds1.height  = reader.ReadInt32() + 1;

                    int act = 0;
                    if (ds1.version >= 8)
                    {
                        act = reader.ReadInt32();
                        act = Mathf.Min(act, 4);
                    }

                    int tagType = 0;
                    if (ds1.version >= 10)
                    {
                        tagType = reader.ReadInt32();
                    }

                    if (ds1.version >= 3)
                    {
                        var palette = Palette.GetPalette(act);
                        ds1.dt1Files    = ReadDependencies(reader);
                        ds1.tileSampler = new DT1.Sampler();
                        foreach (var dt1Filename in ds1.dt1Files)
                        {
                            var dt1 = DT1.Load(dt1Filename, palette);
                            ds1.tileSampler.Add(dt1.tiles);
                        }
                    }

                    if ((ds1.version >= 9) && (ds1.version <= 13))
                    {
                        stream.Seek(8, SeekOrigin.Current);
                    }

                    ReadLayers(ds1, bytes, reader, stream, tagType);
                    ReadObjects(ds1, reader, act);
                    try
                    {
                        ReadGroups(ds1, reader, tagType);
                    }
                    catch (EndOfStreamException)
                    {
                        // in fact there can be less groups than expected
                    }

                    return(ds1);
                }
        }
示例#2
0
        public static DT1 Load(string filename, Color32[] palette, bool mpq = true)
        {
            string lowerFilename = filename.ToLower();

            if (cache.ContainsKey(lowerFilename))
            {
                return(cache[lowerFilename]);
            }

            UnityEngine.Profiling.Profiler.BeginSample("DT1.Load");
            try
            {
                var sw    = System.Diagnostics.Stopwatch.StartNew();
                var bytes = mpq ? Mpq.ReadAllBytes(filename) : File.ReadAllBytes(filename);
                var dt1   = new DT1();
                dt1.filename = filename;

                using (var stream = new MemoryStream(bytes))
                    using (var reader = new BinaryReader(stream))
                    {
                        int version1 = reader.ReadInt32();
                        int version2 = reader.ReadInt32();
                        if (version1 != 7 || version2 != 6)
                        {
                            Debug.Log(string.Format("Can't read dt1 file, bad version ({0}.{1})", version1, version2));
                            return(dt1);
                        }

                        stream.Seek(260, SeekOrigin.Current);
                        ReadTiles(dt1, stream, reader, bytes, palette);
                    }

                cache[lowerFilename] = dt1;
                Debug.Log(dt1.filename + ", tiles " + dt1.tiles.Length + ", " + dt1.textures.Count + " textures, " +
                          sw.ElapsedMilliseconds + " ms");
                return(dt1);
            }
            finally
            {
                UnityEngine.Profiling.Profiler.EndSample();
            }
        }
示例#3
0
        static void ReadTiles(DT1 dt1, Stream stream, BinaryReader reader, byte[] bytes, Color32[] palette)
        {
            int tileCount = reader.ReadInt32();

            reader.ReadInt32(); //  Pointer in file to Tile Headers (= 276)
            dt1.tiles = new Tile[tileCount];

            for (int i = 0; i < tileCount; ++i)
            {
                dt1.tiles[i].Read(reader);
            }

            int       textureSize = CalcTextureSize(dt1.tiles);
            var       packer      = new TexturePacker(textureSize, textureSize);
            Material  material    = null;
            Texture2D texture     = null;

            Color32[] pixels = null;

            for (int i = 0; i < tileCount; ++i)
            {
                var tile = dt1.tiles[i];

                if (tile.width == 0 || tile.height == 0)
                {
                    Debug.Log(string.Format("Zero size {0}x{1}", tile.width, tile.height));
                    continue;
                }

                var pack = packer.put(tile.width, -tile.height);
                if (pack.newTexture)
                {
                    if (texture != null)
                    {
                        texture.SetPixels32(pixels);
                        texture.Apply(false);
                    }

                    texture            = new Texture2D(textureSize, textureSize, TextureFormat.ARGB32, false);
                    texture.filterMode = FilterMode.Point;
                    dt1.textures.Add(texture);
                    material             = new Material(Shader.Find("Sprite"));
                    material.mainTexture = texture;
                    pixels = new Color32[textureSize * textureSize];
                }

                tile.textureX = pack.x;
                tile.textureY = pack.y;
                tile.texture  = texture;
                tile.material = material;

                if ((tile.orientation == 0 || tile.orientation == 15) && tile.height != 0)
                {
                    // floor or roof
                    tile.height = -79;
                }
                else if (tile.orientation > 0 && tile.orientation < 15)
                {
                    tile.textureY += (-tile.height);
                }
                else if (tile.orientation > 15)
                {
                    tile.textureY += Math.Min(96, -tile.height);
                }

                dt1.tiles[i] = tile;

                stream.Seek(tile.blockHeaderPointer, SeekOrigin.Begin);
                for (int block = 0; block < tile.blockCount; ++block)
                {
                    int x = reader.ReadInt16();
                    int y = reader.ReadInt16();
                    reader.ReadBytes(2); // zeros
                    reader.ReadByte();   // gridX
                    reader.ReadByte();   // gridY
                    short format = reader.ReadInt16();
                    int   length = reader.ReadInt32();
                    reader.ReadBytes(2); // zeros
                    int fileOffset        = reader.ReadInt32();
                    int blockDataPosition = tile.blockHeaderPointer + fileOffset;

                    if (format == 1)
                    {
                        drawBlockIsometric(pixels, palette, textureSize, tile.textureX + x, tile.textureY + y, bytes, blockDataPosition, length);
                    }
                    else
                    {
                        drawBlockNormal(pixels, palette, textureSize, tile.textureX + x, tile.textureY + y, bytes, blockDataPosition, length);
                    }
                }
            }

            if (texture != null)
            {
                texture.SetPixels32(pixels);
                texture.Apply(false);
            }
        }