private void WriteImageSheetJson(JsonWriter output, string key, ImageSheet sheet)
 {
     using (output.BeginObject(key))
     {
         using (output.BeginArray("entries"))
         {
             foreach (ImageSheetEntry entry in sheet)
             {
                 entry.WriteJson(output);
             }
         }
     }
 }
 public void WriteDialogJson(JsonWriter output, string key, AGS.Types.Dialog dialog)
 {
     if (dialog == null)
     {
         output.WriteNull(key);
         return;
     }
     using (output.BeginObject(key))
     {
         output.WriteValue("scriptName", dialog.Name);
         output.WriteValue("showTextParser", dialog.ShowTextParser);
         using (output.BeginArray("options"))
         {
             foreach (AGS.Types.DialogOption option in dialog.Options)
             {
                 WriteDialogOptionJson(output, option);
             }
         }
     }
 }
 private void WriteSpritesJson(JsonWriter output, string key)
 {
     using (output.BeginArray(key))
     {
         foreach (SpriteImageSheetEntry entry in spriteImageSheetEntries)
         {
             if (entry == null || entry.Width == 0 || entry.Height == 0)
             {
                 output.WriteNull();
             }
             else
             {
                 using (output.BeginObject())
                 {
                     output.WriteValue("s", GameImageSheets.IndexOf(entry.OwningSheet));
                     output.WriteValue("n", entry.EntryNumber);
                 }
             }
         }
     }
 }
 private void WriteView(JsonWriter output, string key, AGS.Types.View view)
 {
     if (view == null)
     {
         output.WriteNull(key);
         return;
     }
     using (output.BeginObject(key))
     {
         using (output.BeginArray("loops"))
         {
             foreach (AGS.Types.ViewLoop loop in view.Loops)
             {
                 WriteLoopJson(output, loop);
             }
         }
     }
 }
 private void WriteStringArrayJson(JsonWriter output, string key, IEnumerable strings)
 {
     using (output.BeginArray(key))
     {
         foreach (string str in strings)
         {
             output.WriteValue(str);
         }
     }
 }
 public void WriteLoopJson(JsonWriter output, string key, AGS.Types.ViewLoop loop)
 {
     if (loop == null)
     {
         output.WriteNull(key);
         return;
     }
     using (output.BeginObject(key))
     {
         output.WriteValue("runNextLoop", loop.RunNextLoop);
         using (output.BeginArray("frames"))
         {
             foreach (AGS.Types.ViewFrame frame in loop.Frames)
             {
                 WriteFrameJson(output, frame);
             }
         }
     }
 }
        public void WriteGuiJson(JsonWriter output, string key, AGS.Types.GUI gui)
        {
            if (gui == null)
            {
                output.WriteNull(key);
                return;
            }
            using (output.BeginObject(key))
            {
                output.WriteValue("scriptName", gui.Name);
                output.WriteValue("backgroundColor", gui.BackgroundColor);
                output.WriteValue("backgroundImage", gui.BackgroundImage);

                if (gui is AGS.Types.NormalGUI)
                {
                    output.WriteValue("type", "normal");
                    AGS.Types.NormalGUI normalGui = (AGS.Types.NormalGUI)gui;

                    output.WriteValue("borderColor", normalGui.BorderColor);
                    output.WriteValue("clickable", normalGui.Clickable);
                    output.WriteValue("left", normalGui.Left);
                    output.WriteValue("top", normalGui.Top);
                    output.WriteValue("width", normalGui.Width);
                    output.WriteValue("height", normalGui.Height);
                    output.WriteValue("onClick", normalGui.OnClick);
                    output.WriteValue("popupYPos", normalGui.PopupYPos);
                    output.WriteValue("transparency", normalGui.Transparency);
                    switch (normalGui.Visibility)
                    {
                        case AGS.Types.GUIVisibility.MouseYPos:
                            output.WriteValue("visibility", "mouseYPos");
                            break;
                        case AGS.Types.GUIVisibility.Normal:
                            output.WriteValue("visibility", "normal");
                            break;
                        case AGS.Types.GUIVisibility.NormalButInitiallyOff:
                            output.WriteValue("visibility", "normalButInitiallyOff");
                            break;
                        case AGS.Types.GUIVisibility.Persistent:
                            output.WriteValue("visibility", "persistent");
                            break;
                        case AGS.Types.GUIVisibility.PopupModal:
                            output.WriteValue("visibility", "popupModal");
                            break;
                        default:
                            throw new Exception("Unknown GUIVisibility: " + normalGui.Visibility);
                    }
                    output.WriteValue("zOrder", normalGui.ZOrder);
                }
                else if (gui is AGS.Types.TextWindowGUI)
                {
                    output.WriteValue("type", "textWindow");
                    AGS.Types.TextWindowGUI textWindowGui = (AGS.Types.TextWindowGUI)gui;

                    output.WriteValue("borderColor", textWindowGui.BorderColor);
                    output.WriteValue("textColor", textWindowGui.TextColor);
                }
                else
                {
                    throw new Exception("Unknown GUI Type: " + gui);
                }

                using (output.BeginArray("controls"))
                {
                    foreach (AGS.Types.GUIControl control in gui.Controls)
                    {
                        WriteGuiControlJson(output, control);
                    }
                }
            }
        }
        public void WriteGameDef(JsonWriter output, string key)
        {
            using (output.BeginObject(key))
            {
                output.WriteValue("guid", GetCurrentGameGuid());

                WriteSettingsJson(output, "settings");

                // mouseCursors
                using (output.BeginArray("mouseCursors"))
                {
                    foreach (AGS.Types.MouseCursor cursor in editor.CurrentGame.Cursors)
                    {
                        WriteMouseCursorJson(output, cursor);
                    }
                }

                // room numbers
                using (output.BeginArray("roomNumbers"))
                {
                    foreach (AGS.Types.IRoom room in editor.CurrentGame.Rooms)
                    {
                        output.WriteValue(room.Number);
                    }
                }

                // translation names
                using (output.BeginArray("translationNames"))
                {
                    foreach (AGS.Types.Translation translation in editor.CurrentGame.Translations)
                    {
                        output.WriteValue(translation.Name);
                    }
                }

                // views
                using (output.BeginArray("views"))
                {
                    foreach (AGS.Types.View view in GetAllViews())
                    {
                        WriteViewJson(output, view);
                    }
                }

                // dialogs
                using (output.BeginArray("dialogs"))
                {
                    foreach (AGS.Types.Dialog dialog in editor.CurrentGame.Dialogs)
                    {
                        WriteDialogJson(output, dialog);
                    }
                }

                // characters
                using (output.BeginArray("characters"))
                {
                    foreach (AGS.Types.Character character in editor.CurrentGame.Characters)
                    {
                        WriteCharacterJson(output, character);
                    }
                }

                // inventory items
                using (output.BeginArray("inventoryItems"))
                {
                    foreach (AGS.Types.InventoryItem inventoryItem in editor.CurrentGame.InventoryItems)
                    {
                        WriteInventoryItemJson(output, inventoryItem);
                    }
                }

                // initial inventory items
                using (output.BeginArray("initialInventoryItems"))
                {
                    foreach (AGS.Types.InventoryItem inventoryItem in editor.CurrentGame.InventoryItems)
                    {
                        if (inventoryItem.PlayerStartsWithItem)
                        {
                            output.WriteValue(inventoryItem.ID);
                        }
                    }
                }

                // image sheets
                using (output.BeginArray("imageSheets"))
                foreach (ImageSheet sheet in GameImageSheets)
                {
                    WriteImageSheetJson(output, sheet);
                }

                // numbered images (sprites)
                WriteSpritesJson(output, "numberedImages");

                // fonts
                WriteFontsJson(output, "fonts");

                // GUIs
                using (output.BeginArray("guis"))
                {
                    foreach (AGS.Types.GUI gui in editor.CurrentGame.GUIs)
                    {
                        WriteGuiJson(output, gui);
                    }
                }

                output.WriteValue("playerCharacter", HacksAndKludges.GetPlayerCharacter(editor.CurrentGame).ID);

                // global messages
                using (output.BeginArray("globalMessages"))
                {
                    foreach (string s in HacksAndKludges.GetGlobalMessages(editor.CurrentGame))
                    {
                        output.WriteValue(s);
                    }
                }

                // text parser
                using (output.BeginObject("textParserWords"))
                {
                    foreach (AGS.Types.TextParserWord word in HacksAndKludges.GetTextParser(editor.CurrentGame).Words)
                    {
                        output.WriteValue(word.Word, word.WordGroup);
                    }
                }

                // custom property schema
                using (output.BeginObject("customPropertySchema"))
                {
                    using (output.BeginArray("definitions"))
                    {
                        foreach (AGS.Types.CustomPropertySchemaItem item in HacksAndKludges.GetPropertySchema(editor.CurrentGame).PropertyDefinitions)
                        using (output.BeginObject())
                        {
                            output.WriteValue("appliesToCharacters", item.AppliesToCharacters);
                            output.WriteValue("appliesToHotspots", item.AppliesToHotspots);
                            output.WriteValue("appliesToInvItems", item.AppliesToInvItems);
                            output.WriteValue("appliesToObjects", item.AppliesToObjects);
                            output.WriteValue("appliesToRooms", item.AppliesToRooms);
                            output.WriteValue("name", item.Name);
                            output.WriteValue("description", item.Description);
                            output.WriteValue("type", EnumName(item.Type));
                            switch (item.Type)
                            {
                                case AGS.Types.CustomPropertyType.Boolean:
                                    output.WriteValue("defaultValue", item.DefaultValue == "true");
                                    break;
                                case AGS.Types.CustomPropertyType.Number:
                                    output.WriteValue("defaultValue", double.Parse(item.DefaultValue));
                                    break;
                                case AGS.Types.CustomPropertyType.Text:
                                    output.WriteValue("defaultValue", item.DefaultValue);
                                    break;
                            }
                        }
                    }
                }

                // lip sync
                using (output.BeginObject("lipSync"))
                {
                    output.WriteValue("type", EnumName(HacksAndKludges.GetLipSync(editor.CurrentGame).Type));
                    output.WriteValue("defaultFrame", HacksAndKludges.GetLipSync(editor.CurrentGame).DefaultFrame);
                    using (output.BeginArray("charactersPerFrame"))
                    {
                        foreach (string cpf in HacksAndKludges.GetLipSync(editor.CurrentGame).CharactersPerFrame)
                        {
                            output.WriteValue(cpf);
                        }
                    }
                }

                // old interaction variables
                using (output.BeginArray("oldInteractionVariables"))
                {
                    foreach (AGS.Types.OldInteractionVariable old in HacksAndKludges.GetOldInteractionVariables(editor.CurrentGame))
                    using (output.BeginObject())
                    {
                        output.WriteValue("scriptName", old.ScriptName);
                        output.WriteValue("value", old.Value);
                    }
                }
            }
        }
 private void WriteFontsJson(JsonWriter output, string key)
 {
     using (output.BeginArray(key))
     {
         foreach (AgsFont font in fonts)
         {
             WriteFontJson(output, font);
         }
     }
 }
 private void WriteFontJson(JsonWriter output, string key, AgsFont font)
 {
     if (font == null)
     {
         output.WriteNull(key);
         return;
     }
     using (output.BeginObject(key))
     {
         using (output.BeginArray("chars"))
         {
             foreach (AgsFontChar c in font.Chars)
             {
                 WriteFontCharJson(output, c);
             }
         }
     }
 }
        public void WriteRoomDefJson(JsonWriter output, string key)
        {
            AGS.Types.ILoadedRoom room = editor.RoomController.CurrentRoom;
            if (room == null)
            {
                output.WriteNull(key);
                return;
            }
            AGS.Types.IRoom room2 = null;
            for (int i = 0; i < editor.CurrentGame.Rooms.Count; i++)
            {
                if (editor.CurrentGame.Rooms[i].Number == room.Number)
                {
                    room2 = editor.CurrentGame.Rooms[i];
                }
            }
            if (room2 == null)
            {
                throw new Exception("Room #" + room.Number + " not found in the rooms list!");
            }
            using (output.BeginObject(key))
            {
                output.WriteValue("game", GetCurrentGameGuid());
                output.WriteValue("name", room2.Description);
                output.WriteValue("number", room.Number);
                output.WriteValue("stateSaving", room2.StateSaving);
                output.WriteValue("bottomEdgeY", room.BottomEdgeY);
                output.WriteValue("leftEdgeX", room.LeftEdgeX);
                output.WriteValue("rightEdgeX", room.RightEdgeX);
                output.WriteValue("topEdgeY", room.TopEdgeY);
                output.WriteValue("backgroundAnimationDelay", room.BackgroundAnimationDelay);
                output.WriteValue("legacyMusicVolumeAdjustment", EnumName(room.MusicVolumeAdjustment));
                output.WriteValue("playerCharacterView", room.PlayerCharacterView);
                output.WriteValue("colorDepth", room.ColorDepth);
                output.WriteValue("width", room.Width);
                output.WriteValue("height", room.Height);
                output.WriteValue("numBackgrounds", room.BackgroundCount);

                Dictionary<int,bool> usedHotspots;
                Dictionary<int,bool> usedRegions;
                Dictionary<int,bool> usedWalkables;
                Dictionary<int,bool> usedWalkBehinds;

                using (output.BeginObject("masks"))
                {
                    usedHotspots = WriteRoomMaskJson(output, "hotspots", AGS.Types.RoomAreaMaskType.Hotspots);
                    usedRegions = WriteRoomMaskJson(output, "regions", AGS.Types.RoomAreaMaskType.Regions);
                    usedWalkables = WriteRoomMaskJson(output, "walkableAreas", AGS.Types.RoomAreaMaskType.WalkableAreas);
                    usedWalkBehinds = WriteRoomMaskJson(output, "walkBehinds", AGS.Types.RoomAreaMaskType.WalkBehinds);
                }

                using (output.BeginArray("hotspots"))
                {
                    foreach (AGS.Types.RoomHotspot hs in room.Hotspots)
                    {
                        WriteRoomHotspotJson(output, hs, usedHotspots.ContainsKey(hs.ID));
                    }
                }

                using (output.BeginArray("objects"))
                {
                    foreach (AGS.Types.RoomObject obj in room.Objects)
                    {
                        WriteRoomObjectJson(output, obj);
                    }
                }

                using (output.BeginArray("walkbehinds"))
                {
                    foreach (AGS.Types.RoomWalkBehind wb in room.WalkBehinds)
                    {
                        WriteRoomWalkbehindJson(output, wb, usedWalkBehinds.ContainsKey(wb.ID));
                    }
                }

                using (output.BeginArray("walkableAreas"))
                {
                    foreach (AGS.Types.RoomWalkableArea area in room.WalkableAreas)
                    {
                        WriteRoomWalkableAreaJson(output, area, usedWalkables.ContainsKey(area.ID));
                    }
                }

                using (output.BeginArray("regions"))
                {
                    foreach (AGS.Types.RoomRegion reg in room.Regions)
                    {
                        WriteRoomRegionJson(output, reg, usedRegions.ContainsKey(reg.ID));
                    }
                }

                WriteCustomPropertiesJson(output, "properties", room.Properties);
            }
        }
        public Dictionary<int, bool> WriteRoomMaskJson(JsonWriter output, string key, AGS.Types.RoomAreaMaskType maskType)
        {
            Dictionary<int,bool> usedZones = new Dictionary<int,bool>();
            using (output.BeginArray(key))
            {
                QuadTreeTools.ZoneGetter getZone =
                    delegate(int x, int y)
                    {
                        int zone;
                        if (x < 0 || y < 0 || y >= editor.RoomController.CurrentRoom.Height || x >= editor.RoomController.CurrentRoom.Width)
                        {
                            zone = 0;
                        }
                        else
                        {
                            zone = editor.RoomController.GetAreaMaskPixel(maskType, x, y);
                        }
                        usedZones[zone] = true;
                        return zone;
                    };

                int rootSize = QuadTreeTools.GetRootSize(
                    editor.RoomController.CurrentRoom.Width,
                    editor.RoomController.CurrentRoom.Height);

                QuadTreeNode rootNode = new QuadTreeNode(getZone, QuadTreeTools.RootSizeToPixels(rootSize), 0, 0);
                rootNode.process(editor.RoomController.CurrentRoom.Width, editor.RoomController.CurrentRoom.Height);
                WriteQuadTreeNode(output, rootNode);
            }
            return usedZones;
        }