public void Deserialize(Packet inPacket = null)
        {
            NotifyPropertyChanged();
            //isDirty = true;
            HasUnsavedAuthorChanges = true;
            HasUnsavedChanges       = true;

            if (inPacket != null)
            {
                Flush(true);
                _data = new List <byte>(inPacket.getData());
            }

            try
            {
                roomFlags      = ReadSInt32();
                facesID        = ReadSInt32();
                roomID         = ReadSInt16();
                roomNameOfst   = ReadSInt16();
                pictNameOfst   = ReadSInt16();
                artistNameOfst = ReadSInt16();
                passwordOfst   = ReadSInt16();
                nbrHotspots    = ReadSInt16();
                hotspotOfst    = ReadSInt16();
                nbrPictures    = ReadSInt16();
                pictureOfst    = ReadSInt16();
                nbrDrawCmds    = ReadSInt16();
                firstDrawCmd   = ReadSInt16();
                DropBytes(2); //nbrPeople
                nbrLProps  = ReadSInt16();
                firstLProp = ReadSInt16();
                DropBytes(2);
                lenVars = ReadSInt16();

                // Get the strings
                roomName     = PeekPString(32, roomNameOfst);
                roomPicture  = PeekPString(32, pictNameOfst);
                roomArtist   = PeekPString(32, artistNameOfst);
                roomPassword = PeekPString(32, passwordOfst);

                // Paint
                #region Paint

                for (int i = 0; i < nbrDrawCmds; i++)
                {
                    Seek(firstDrawCmd + i * DrawCmdRec.SizeOf);

                    var drawCmd = new DrawCmdRec();
                    drawCmd.nextOfst  = PeekSInt16();
                    drawCmd.reserved  = PeekSInt16();
                    drawCmd.drawCmd   = PeekSInt16();
                    drawCmd.cmdLength = PeekUInt16();
                    drawCmd.dataOfst  = PeekSInt16();
                    drawCmd.data      = _data
                                        .Skip(drawCmd.dataOfst)
                                        .Take(drawCmd.cmdLength)
                                        .ToArray();

                    _drawCmds.Add(drawCmd);
                }

                #endregion

                // Loose Props
                #region Loose Props
                for (int i = 0; i < nbrLProps; i++)
                {
                    Seek(firstLProp + i * LoosePropRec.SizeOf);

                    var prop = new LoosePropRec();
                    prop.nextOfst = PeekSInt16();
                    prop.reserved = PeekSInt16();

                    prop.propSpec     = new AssetSpec();
                    prop.propSpec.id  = PeekSInt32();
                    prop.propSpec.crc = (UInt32)PeekSInt32();
                    prop.flags        = PeekSInt32();
                    prop.refCon       = PeekSInt32();

                    prop.loc   = new Point();
                    prop.loc.v = PeekSInt16();
                    prop.loc.h = PeekSInt16();

                    _looseProps.Add(prop);
                }
                #endregion

                // Pictures
                #region Pictures
                for (int i = 0; i < nbrPictures; i++)
                {
                    Seek(pictureOfst + PictureRec.SizeOf * i);

                    var picture = new PictureRec();
                    picture.refCon      = PeekSInt32();
                    picture.picID       = PeekSInt16();
                    picture.picNameOfst = PeekSInt16();
                    picture.transColor  = PeekSInt16();
                    picture.reserved    = PeekSInt16();

                    if (picture.picNameOfst > 0 && picture.picNameOfst < Count)
                    {
                        picture.name = PeekPString(32, picture.picNameOfst);

                        _pictures.Add(picture);
                    }
                }
                #endregion

                //Hotspots
                #region Hotspots

                for (int i = 0; i < nbrHotspots; i++)
                {
                    Seek(hotspotOfst + HotspotRec.SizeOf * i);

                    var hotspot = new HotspotRec
                    {
                        Vortexes = new List <Point>(),
                        states   = new List <HotspotStateRec>(),
                    };
                    hotspot.scriptEventMask = PeekSInt32();
                    hotspot.flags           = PeekSInt32();
                    hotspot.secureInfo      = PeekSInt32();
                    hotspot.refCon          = PeekSInt32();

                    hotspot.loc   = new Point();
                    hotspot.loc.v = PeekSInt16();
                    hotspot.loc.h = PeekSInt16();

                    hotspot.id             = PeekSInt16();
                    hotspot.dest           = PeekSInt16();
                    hotspot.nbrPts         = PeekSInt16();
                    hotspot.ptsOfst        = PeekSInt16();
                    hotspot.type           = (HotspotTypes)PeekSInt16();
                    hotspot.groupID        = PeekSInt16();
                    hotspot.nbrScripts     = PeekSInt16();
                    hotspot.scriptRecOfst  = PeekSInt16();
                    hotspot.state          = PeekSInt16();
                    hotspot.nbrStates      = PeekSInt16();
                    hotspot.stateRecOfst   = PeekSInt16();
                    hotspot.nameOfst       = PeekSInt16();
                    hotspot.scriptTextOfst = PeekSInt16();
                    hotspot.alignReserved  = PeekSInt16();

                    if (hotspot.nameOfst > 0 && hotspot.nameOfst < Count)
                    {
                        hotspot.name = PeekPString(32, hotspot.nameOfst);
                    }

                    if (hotspot.scriptTextOfst > 0 && hotspot.scriptTextOfst < Count)
                    {
                        hotspot.script = ReadCString(hotspot.scriptTextOfst);
                    }

                    if (hotspot.nbrPts > 0 && hotspot.ptsOfst > 0 && hotspot.ptsOfst < Count - (Point.SizeOf * hotspot.nbrPts))
                    {
                        for (int s = 0; s < hotspot.nbrPts; s++)
                        {
                            Seek(hotspot.ptsOfst + s * Point.SizeOf);

                            var p = new Point();
                            p.v = PeekSInt16();
                            p.h = PeekSInt16();

                            hotspot.Vortexes.Add(p);
                        }
                    }

                    for (int s = 0; s < hotspot.nbrStates; s++)
                    {
                        Seek(hotspot.stateRecOfst + s * HotspotStateRec.SizeOf);

                        var hs = new HotspotStateRec();
                        hs.pictID   = PeekSInt16();
                        hs.reserved = PeekSInt16();

                        hs.picLoc   = new Point();
                        hs.picLoc.v = PeekSInt16();
                        hs.picLoc.h = PeekSInt16();

                        hotspot.states.Add(hs);
                    }

                    _hotspots.Add(hotspot);
                }

                #endregion
            }
            catch (Exception ex)
            {
                ex.DebugLog();
            }
        }
Example #2
0
        public void Read()
        {
            var hotspots   = new List <HotspotRec>();
            var pictures   = new List <PictureRec>();
            var looseProps = new List <LoosePropRec>();
            //var drawCmds = new List<DrawCmdRec>();

            var workingHotspot   = new HotspotRec();
            var workingPicture   = new PictureRec();
            var workingLooseProp = new LoosePropRec();
            var workingScript    = new StringBuilder();
            //var workingDrawCmd = new DrawCmdRec();
            var workingRoom = new RoomRec();

            var insideRoom    = false;
            var insideProp    = false;
            var insideScript  = false;
            var insidePicture = false;
            var insideHotspot = false;
            var insidePicts   = false;

            var line = string.Empty;

            using (var reader = new StreamReader(_fileStream, Encoding.ASCII))
            {
                using (var dbContext = Database.Database.For <ThePalaceEntities>())
                {
                    while ((line = reader.ReadLine()) != null)
                    {
                        line = line.Trim();

                        var tokens = Regex.Split(line, @"\s+", RegexOptions.IgnoreCase | RegexOptions.Singleline);
                        var value  = tokens.Length > 1 ? Regex.Match(line, @"^[a-z]+\s+""(.*)""$", RegexOptions.IgnoreCase | RegexOptions.Singleline).Groups[1].Value : string.Empty;

                        switch (tokens[0].ToUpper())
                        {
                        case "ROOM":
                            insideRoom  = true;
                            workingRoom = new RoomRec();
                            hotspots    = new List <HotspotRec>();
                            pictures    = new List <PictureRec>();
                            looseProps  = new List <LoosePropRec>();
                            //drawCmds = new List<DrawCmdRec>();

                            break;

                        case "ENDROOM":
                            if (!insideHotspot)
                            {
                                insideRoom = false;

                                if (!RoomData.Peek(dbContext, workingRoom.roomID, out RoomRec roomData) && roomData == null)
                                {
                                    RoomData.WriteRoom(dbContext, workingRoom.roomID, workingRoom);
                                    RoomData.WriteHotspots(dbContext, workingRoom.roomID, hotspots);
                                    RoomData.WritePictures(dbContext, workingRoom.roomID, pictures);
                                    RoomData.WriteLooseProps(dbContext, workingRoom.roomID, looseProps);
                                    //RoomData.WriteDrawCmds(dbContext, workingRoom.roomID, drawCmds);

                                    if (dbContext.HasUnsavedChanges())
                                    {
                                        dbContext.SaveChanges();
                                    }
                                }
                            }

                            break;

                        case "DOOR":
                            if (insideRoom)
                            {
                                insideHotspot  = true;
                                workingHotspot = new HotspotRec();
                                workingScript  = new StringBuilder();

                                workingHotspot.type = HotspotTypes.HS_Door;
                            }

                            break;

                        case "ENDDOOR":
                            if (insideRoom && insideHotspot)
                            {
                                insideHotspot = false;

                                hotspots.Add(workingHotspot);
                            }

                            break;

                        case "SPOT":
                        case "HOTSPOT":
                            if (insideRoom)
                            {
                                insideHotspot  = true;
                                workingHotspot = new HotspotRec();
                                workingScript  = new StringBuilder();

                                workingHotspot.type = HotspotTypes.HS_Normal;
                            }

                            break;

                        case "ENDSPOT":
                        case "ENDHOTSPOT":
                            if (insideRoom && insideHotspot)
                            {
                                insideHotspot = false;

                                hotspots.Add(workingHotspot);
                            }

                            break;

                        case "BOLT":
                            if (insideRoom)
                            {
                                insideHotspot  = true;
                                workingHotspot = new HotspotRec();
                                workingScript  = new StringBuilder();

                                workingHotspot.type = HotspotTypes.HS_Bolt;
                            }

                            break;

                        case "ENDBOLT":
                            if (insideRoom && insideHotspot)
                            {
                                insideHotspot = false;

                                hotspots.Add(workingHotspot);
                            }

                            break;

                        case "NAVAREA":
                            if (insideRoom)
                            {
                                insideHotspot  = true;
                                workingHotspot = new HotspotRec();
                                workingScript  = new StringBuilder();

                                workingHotspot.type = HotspotTypes.HS_NavArea;
                            }

                            break;

                        case "ENDNAVAREA":
                            if (insideRoom && insideHotspot)
                            {
                                insideHotspot = false;

                                hotspots.Add(workingHotspot);
                            }

                            break;

                        case "PICTURE":
                            if (insideRoom)
                            {
                                insidePicture  = true;
                                workingPicture = new PictureRec();
                            }

                            if (insidePicture && tokens.Length > 2 && tokens[1] == "ID")
                            {
                                workingPicture.picID = tokens[2].TryParse <short>(0).Value;
                            }

                            break;

                        case "ENDPICTURE":
                            if (insideRoom && insidePicture)
                            {
                                insidePicture = false;

                                pictures.Add(workingPicture);
                            }

                            break;

                        case "PROP":
                            if (insideRoom)
                            {
                                insideProp                = true;
                                workingLooseProp          = new LoosePropRec();
                                workingLooseProp.propSpec = new AssetSpec();
                            }

                            break;

                        case "ENDPROP":
                            if (insideRoom && insideProp)
                            {
                                insideProp = false;

                                looseProps.Add(workingLooseProp);
                            }

                            break;

                        case "SCRIPT":
                            if (insideRoom && insideHotspot)
                            {
                                insideScript = true;
                            }

                            break;

                        case "ENDSCRIPT":
                            if (insideRoom && insideHotspot && insideScript)
                            {
                                insideScript = false;

                                workingHotspot.script = workingScript.ToString();
                            }

                            break;

                        case "PICTS":
                            if (insideRoom && insideHotspot)
                            {
                                insidePicts           = true;
                                workingHotspot.states = new List <HotspotStateRec>();
                            }

                            break;

                        case "ENDPICTS":
                            if (insideRoom && insideHotspot && insidePicts)
                            {
                                insidePicts = false;
                            }

                            break;

                        case "ID":
                            if (tokens.Length < 2)
                            {
                                break;
                            }

                            if (insidePicture)
                            {
                                workingPicture.picID = tokens[1].TryParse <short>(0).Value;
                            }
                            else if (insideHotspot)
                            {
                                workingHotspot.id = tokens[1].TryParse <short>(0).Value;
                            }
                            else if (insideRoom)
                            {
                                workingRoom.roomID = tokens[1].TryParse <short>(0).Value;
                            }

                            break;

                        case "NAME":
                            if (tokens.Length < 2)
                            {
                                break;
                            }

                            if (insidePicture)
                            {
                                workingPicture.name = value;
                            }
                            else if (insideHotspot)
                            {
                                workingHotspot.name = value;
                            }
                            else if (insideRoom)
                            {
                                workingRoom.roomName = value;
                            }

                            break;

                        case "ARTIST":
                            if (tokens.Length < 2)
                            {
                                break;
                            }

                            if (insideRoom)
                            {
                                workingRoom.roomArtist = value;
                            }

                            break;

                        case "PICT":
                            if (tokens.Length < 2)
                            {
                                break;
                            }

                            if (insideRoom)
                            {
                                workingRoom.roomPicture = value;
                            }

                            break;

                        case "LOCKED":
                            if (tokens.Length < 2)
                            {
                                break;
                            }

                            if (insideRoom && !string.IsNullOrWhiteSpace(value))
                            {
                                workingRoom.roomFlags   |= (int)RoomFlags.RF_AuthorLocked;
                                workingRoom.roomPassword = value.ReadPalaceString().DecryptString();
                            }

                            break;

                        case "MAXMEMBERS":
                            if (tokens.Length < 2)
                            {
                                break;
                            }

                            if (insideRoom)
                            {
                                workingRoom.roomMaxOccupancy = tokens[1].TryParse <short>(0).Value;
                            }

                            break;

                        case "FACES":
                            if (tokens.Length < 2)
                            {
                                break;
                            }

                            if (insideRoom)
                            {
                                workingRoom.facesID = tokens[1].TryParse <short>(0).Value;
                            }

                            break;

                        case "DROPZONE":
                            if (insideRoom)
                            {
                                workingRoom.roomFlags |= (int)RoomFlags.RF_DropZone;
                            }

                            break;

                        case "NOLOOSEPROPS":
                            if (insideRoom)
                            {
                                workingRoom.roomFlags |= (int)RoomFlags.RF_NoLooseProps;
                            }

                            break;

                        case "PRIVATE":
                            if (insideRoom)
                            {
                                workingRoom.roomFlags |= (int)RoomFlags.RF_Private;
                            }

                            break;

                        case "NOPAINTING":
                            if (insideRoom)
                            {
                                workingRoom.roomFlags |= (int)RoomFlags.RF_NoPainting;
                            }

                            break;

                        case "NOCYBORGS":
                            if (insideRoom)
                            {
                                workingRoom.roomFlags |= (int)RoomFlags.RF_CyborgFreeZone;
                            }

                            break;

                        case "HIDDEN":
                            if (insideRoom)
                            {
                                workingRoom.roomFlags |= (int)RoomFlags.RF_Hidden;
                            }

                            break;

                        case "NOGUESTS":
                            if (insideRoom)
                            {
                                workingRoom.roomFlags |= (int)RoomFlags.RF_NoGuests;
                            }

                            break;

                        case "WIZARDSONLY":
                        case "OPERATORSONLY":
                            if (insideRoom)
                            {
                                workingRoom.roomFlags |= (int)RoomFlags.RF_WizardsOnly;
                            }

                            break;

                        case "LOCKABLE":
                        case "SHUTABLE":
                            if (insideRoom)
                            {
                                workingHotspot.type = HotspotTypes.HS_LockableDoor;
                            }

                            break;

                        case "DRAGGABLE":
                            if (insideRoom)
                            {
                                workingHotspot.flags |= (int)HotspotFlags.HS_Draggable;
                            }

                            break;

                        case "FORBIDDEN":
                            if (insideRoom)
                            {
                                workingHotspot.flags |= (int)HotspotFlags.HS_Forbidden;
                            }

                            break;

                        case "MANDATORY":
                            if (insideRoom)
                            {
                                workingHotspot.flags |= (int)HotspotFlags.HS_Mandatory;
                            }

                            break;

                        case "LANDINGPAD":
                            if (insideRoom)
                            {
                                workingHotspot.flags |= (int)HotspotFlags.HS_LandingPad;
                            }

                            break;

                        case "DONTMOVEHERE":
                            if (insideRoom)
                            {
                                workingHotspot.flags |= (int)HotspotFlags.HS_DontMoveHere;
                            }

                            break;

                        case "INVISIBLE":
                            if (insideRoom)
                            {
                                workingHotspot.flags |= (int)HotspotFlags.HS_Invisible;
                            }

                            break;

                        case "SHOWNAME":
                            if (insideRoom)
                            {
                                workingHotspot.flags |= (int)HotspotFlags.HS_ShowName;
                            }

                            break;

                        case "SHOWFRAME":
                            if (insideRoom)
                            {
                                workingHotspot.flags |= (int)HotspotFlags.HS_ShowFrame;
                            }

                            break;

                        case "SHADOW":
                            if (insideRoom)
                            {
                                workingHotspot.flags |= (int)HotspotFlags.HS_Shadow;
                            }

                            break;

                        case "FILL":
                            if (insideRoom)
                            {
                                workingHotspot.flags |= (int)HotspotFlags.HS_Fill;
                            }

                            break;

                        case "DEST":
                            if (tokens.Length < 2)
                            {
                                break;
                            }

                            if (insideHotspot)
                            {
                                workingHotspot.dest = tokens[1].TryParse <short>(0).Value;
                            }

                            break;

                        case "OUTLINE":
                            if (tokens.Length < 3)
                            {
                                break;
                            }

                            if (insideHotspot)
                            {
                                workingHotspot.Vortexes = new List <Point>();

                                for (var j = 1; j < tokens.Length; j++)
                                {
                                    var coords = tokens[j].Split(',');
                                    var h      = coords[0].TryParse <short>(0).Value;
                                    var v      = coords[1].TryParse <short>(0).Value;

                                    workingHotspot.Vortexes.Add(new Point(h, v));
                                }
                            }

                            break;

                        case "LOC":
                            if (tokens.Length < 2)
                            {
                                break;
                            }

                            {
                                var coords = tokens[1].Split(',');
                                var h      = coords[0].TryParse <short>(0).Value;
                                var v      = coords[1].TryParse <short>(0).Value;

                                if (insideHotspot)
                                {
                                    workingHotspot.loc = new Point(h, v);
                                }
                                else if (insideProp)
                                {
                                    workingLooseProp.loc = new Point(h, v);
                                }
                            }

                            break;

                        case "PROPID":
                            if (tokens.Length < 2)
                            {
                                break;
                            }

                            if (insideProp)
                            {
                                workingLooseProp.propSpec.id = Convert.ToInt32(tokens[1], 16);
                            }

                            break;

                        case "CRC":
                            if (tokens.Length < 2)
                            {
                                break;
                            }

                            if (insideProp)
                            {
                                workingLooseProp.propSpec.crc = Convert.ToUInt32(tokens[1], 16);
                            }

                            break;

                        case "TRANSCOLOR":
                            if (tokens.Length < 2)
                            {
                                break;
                            }

                            if (insideProp)
                            {
                                workingPicture.transColor = tokens[1].TryParse <short>(0).Value;
                            }

                            break;

                        default:
                            if (insidePicts)
                            {
                                for (var j = 0; j < tokens.Length; j++)
                                {
                                    var state = tokens[j].Split(',');

                                    workingHotspot.states.Add(new HotspotStateRec
                                    {
                                        pictID = state[0].TryParse <short>(0).Value,
                                        picLoc = new Point
                                        {
                                            h = state[1].TryParse <short>(0).Value,
                                            v = state[2].TryParse <short>(0).Value,
                                        },
                                    });
                                }
                            }
                            else if (insideScript)
                            {
                                workingScript.AppendLine(line);
                            }

                            break;
                        }
                    }
                }
            }
        }