Exemplo n.º 1
0
            public static DoorStruct CreateDoorStruct(ref FileStruct.TSWedDoor tsWedDoor)
            {
                DoorStruct ss = new DoorStruct();

                ss.refDoor   = Utils.CharsToString(tsWedDoor.refDoor).ToUpper();
                ss.state     = tsWedDoor.state;
                ss.openPoly  = new PolygonStruct[tsWedDoor.openPolyCount];
                ss.closePoly = new PolygonStruct[tsWedDoor.closePolyCount];
                ss.tileMaps  = new TileMapStruct[tsWedDoor.doorTileCount];
                return(ss);
            }
Exemplo n.º 2
0
        public static ResourceClass.IResource ReadGWEDResourceFile(bool headerOnly,
                                                                   ChitinKey ckey, FileStream fileStream, int fileOffset, int fileSize)
        {
            ResourceClass.GWEDFile wedFile = new ResourceClass.GWEDFile(ckey.name);

            #region Check file header

            buff = new byte[FileStruct.TSWedHeaderSize];
            fileStream.Position = fileOffset;

            FileStruct.TSWedHeader wedHead =
                (FileStruct.TSWedHeader)ReadBuffer <FileStruct.TSWedHeader>
                    (fileStream, buff, FileStruct.TSWedHeaderSize);

            if (GlobalDefs.RES_GWED_SIGNATURE != Utils.CharsToString(wedHead.signature))
            {
                throw new ArgumentException("Warning: Invalid signature in *.wed file.");
            }

            buff = new byte[FileStruct.TSWedSecHeadSize];
            fileStream.Position = wedHead.secHeadOffset + fileOffset;

            FileStruct.TSWedSecHead wedSecHead =
                (FileStruct.TSWedSecHead)ReadBuffer <FileStruct.TSWedSecHead>
                    (fileStream, buff, FileStruct.TSWedSecHeadSize);

            #endregion Check file header

            int tileMapOffset = -1;
            int tileRefOffset = -1;

            byte[] tmBuff     = new byte[FileStruct.TSWedTileMapSize];
            byte[] polyBuff   = new byte[FileStruct.TSWedPolygonSize];
            byte[] vtBuff     = new byte[4];    // Point
            int    vertOffset = wedSecHead.verticeOffset + fileOffset;

            #region Load overlays

            // --- overlay list --- //
            buff = new byte[FileStruct.TSWedOverlaySize];

            fileStream.Position = wedHead.overlayOffset + fileOffset;

            wedFile.overlayList = new ResourceClass.GWEDFile.OverlayStruct[wedHead.overlayCount];
            for (int c = 0; c < wedHead.overlayCount; c++)
            {
                FileStruct.TSWedOverlay tsWedOverlay =
                    (FileStruct.TSWedOverlay)ReadBuffer <FileStruct.TSWedOverlay>
                        (fileStream, buff, FileStruct.TSWedOverlaySize);

                ResourceClass.GWEDFile.OverlayStruct olay =
                    ResourceClass.GWEDFile.CreateOverlayStruct(ref tsWedOverlay);

                wedFile.overlayList[c] = olay;

                if (tileMapOffset < 0) // get the offsets for overlay[0]
                {
                    tileMapOffset = tsWedOverlay.tileMapOffset;
                    tileRefOffset = tsWedOverlay.tileRefOffset;
                }

                long fpos = fileStream.Position;

                fileStream.Position = tsWedOverlay.tileMapOffset + fileOffset;
                // for each (x,y) tile in the overlay
                for (int y = 0; y < olay.ytile; y++)
                {
                    for (int x = 0; x < olay.xtile; x++)
                    {
                        olay.tileMaps[x, y] = GetGWEDTileMap(fileStream, tmBuff,
                                                             tsWedOverlay.tileRefOffset + fileOffset);
                    }
                }

                fileStream.Position = fpos;
            }

            #endregion Load overlays

            #region Load doors

            // --- door list --- //

            buff = new byte[FileStruct.TSWedDoorSize];
            fileStream.Position = wedHead.doorOffset + fileOffset;

            wedFile.doorList = new ResourceClass.GWEDFile.DoorStruct[wedHead.doorCount];
            for (int c = 0; c < wedHead.doorCount; c++)
            {
                FileStruct.TSWedDoor tsWedDoor =
                    (FileStruct.TSWedDoor)ReadBuffer <FileStruct.TSWedDoor>
                        (fileStream, buff, FileStruct.TSWedDoorSize);

                ResourceClass.GWEDFile.DoorStruct door =
                    ResourceClass.GWEDFile.CreateDoorStruct(ref tsWedDoor);

                wedFile.doorList[c] = door;

                long fpos = fileStream.Position;
                fileStream.Position = wedHead.doorTileOffset + (tsWedDoor.doorTileStart * 2) + fileOffset;

                ushort[] doorTiles    = new ushort[tsWedDoor.doorTileCount];
                byte[]   doorTileBuff = new byte[tsWedDoor.doorTileCount * 2];
                fileStream.Read(doorTileBuff, 0, doorTileBuff.Length);
                Buffer.BlockCopy(doorTileBuff, 0, doorTiles, 0, doorTileBuff.Length);

                for (int d = 0; d < tsWedDoor.doorTileCount; d++)
                {
                    fileStream.Position = tileMapOffset +
                                          (doorTiles[d] * FileStruct.TSWedTileMapSize) + fileOffset;

                    door.tileMaps[d] = GetGWEDTileMap(fileStream, tmBuff, tileRefOffset + fileOffset);
                }

                fileStream.Position = tsWedDoor.openPolyOffset + fileOffset;
                // for each open polygon
                for (int p = 0; p < tsWedDoor.openPolyCount; p++)
                {
                    door.openPoly[p] = GetGWEDPolygon(fileStream, polyBuff, vtBuff, vertOffset);
                }

                fileStream.Position = tsWedDoor.openPolyOffset + fileOffset;
                // for each closed polygon
                for (int p = 0; p < tsWedDoor.closePolyCount; p++)
                {
                    door.closePoly[p] = GetGWEDPolygon(fileStream, polyBuff, vtBuff, vertOffset);
                }

                fileStream.Position = fpos;
            }

            #endregion Load doors

            #region Load walls

            // --- wall list --- //

            buff = new byte[FileStruct.TSWedWallSize];
            fileStream.Position = wedSecHead.wallOffset + fileOffset;

            ArrayList walls        = new ArrayList();
            int       sumPolyRef   = 0;
            int       totalPolyRef = (wedSecHead.verticeOffset - wedSecHead.wallPolyRefOffset) / 2;
            // totalpolyref = sum of each wall { wall[i].wallPolyRefCount }

            // since we don't know how many walls there are at the start, we use
            // (sumPolyRef < totalPolyRef) as the stopping point
            while (sumPolyRef < totalPolyRef)
            {
                FileStruct.TSWedWall tsWedWall =
                    (FileStruct.TSWedWall)ReadBuffer <FileStruct.TSWedWall>
                        (fileStream, buff, FileStruct.TSWedWallSize);

                ResourceClass.GWEDFile.WallStruct wall =
                    ResourceClass.GWEDFile.CreateWallStruct(ref tsWedWall);

                walls.Add(wall);

                long fpos = fileStream.Position;
                fileStream.Position = wedSecHead.wallPolyRefOffset +
                                      (tsWedWall.wallPolyRefStart * 2) + fileOffset;

                ushort[] polyRefs    = new ushort[tsWedWall.wallPolyRefCount];
                byte[]   polyRefBuff = new byte[tsWedWall.wallPolyRefCount * 2];
                fileStream.Read(polyRefBuff, 0, polyRefBuff.Length);
                Buffer.BlockCopy(polyRefBuff, 0, polyRefs, 0, polyRefBuff.Length);

                // for each wall
                for (int p = 0; p < tsWedWall.wallPolyRefCount; p++)
                {
                    fileStream.Position = wedSecHead.wallPolyOffset +
                                          (polyRefs[p] * FileStruct.TSWedPolygonSize) + fileOffset;

                    wall.wallPoly[p] = GetGWEDPolygon(fileStream, polyBuff, vtBuff, vertOffset);
                }

                sumPolyRef += tsWedWall.wallPolyRefCount;

                fileStream.Position = fpos;
            }

            wedFile.wallList = (ResourceClass.GWEDFile.WallStruct[])
                               walls.ToArray(typeof(ResourceClass.GWEDFile.WallStruct));

            #endregion Load walls

            fileStream.Close();

            return(wedFile);
        }