context: using System;
  using System.Collections.Generic;
  using System.Drawing;
  using System.IO;
  using System.Reflection;
  using System.Resources;
  using System.Text;
  using System.Xml;
  
  namespace AGS.Types
  {
      public class Game : IGame
      {
  		private readonly string[] RESERVED_SCRIPT_NAMES = { "inventory", "character", 
  			"views", "player", "object", "mouse", "system", "game", "palette",
  			"hotspot", "region", "dialog", "gui", "GUI"};
          private const int PALETTE_SIZE = 256;
          private const int NUMBER_OF_GLOBAL_MESSAGES = 500;
          private const int GLOBAL_MESSAGE_ID_START = 500;
  
          public const int MAX_CURSORS = 20;
          public const int MAX_DIALOGS = 500;
          public const int MAX_FONTS = 30;
          public const int MAX_INV_ITEMS = 300;
          public const int MAX_SPRITES = 30000;
  
          public delegate void GUIAddedOrRemovedHandler(GUI theGUI);
          public delegate void GUIControlAddedOrRemovedHandler(GUI owningGUI, GUIControl control);
          public delegate void ViewListUpdatedHandler();
          public event GUIAddedOrRemovedHandler GUIAddedOrRemoved;
          public event GUIControlAddedOrRemovedHandler GUIControlAddedOrRemoved;
          /// <summary>
          /// Fired when an external client adds/removes views
          /// </summary>
          public event ViewListUpdatedHandler ViewListUpdated;
  
          private GUIFolders _guis;
          private InventoryItemFolders _inventoryItems;
          private CharacterFolders _characters;
          private List<MouseCursor> _cursors;
          private List<Font> _fonts;
          private DialogFolders _dialogs;
          private List<Plugin> _plugins;
          private List<Translation> _translations;
          private UnloadedRoomFolders _rooms;
          private List<OldInteractionVariable> _oldInteractionVariables;
          private string[] _globalMessages;
          private Character _playerCharacter;
          private Settings _settings;
          private PaletteEntry[] _palette;
          private SpriteFolder _sprites;
          private ViewFolders _views;
          private AudioClipFolders _audioClips;
          private List<AudioClipType> _audioClipTypes;
          private ScriptFolders _scripts;
          private Scripts _scriptsToCompile;
          private TextParser _textParser;
          private LipSync _lipSync;
          private CustomPropertySchema _propertySchema;
          private GlobalVariables _globalVariables;
          private IList<AudioClip> _cachedAudioClipListForCompile;
          private Dictionary<int, int> _cachedAudioClipIndexMapping;
  		private string _directoryPath;
  		private bool _roomsAddedOrRemoved = false;
  		private Dictionary<int, object> _deletedViewIDs;
  		private string _savedXmlVersion = null;
          private int? _savedXmlVersionIndex = null;
          private string _savedXmlEditorVersion = null;
  
          public Game()
          {
              _guis = new GUIFolders(GUIFolder.MAIN_GUI_FOLDER_NAME);
              _inventoryItems = new InventoryItemFolders(InventoryItemFolder.MAIN_INVENTORY_ITEM_FOLDER_NAME);
              _cursors = new List<MouseCursor>();
              _dialogs = new DialogFolders(DialogFolder.MAIN_DIALOG_FOLDER_NAME);
              _fonts = new List<Font>();
              _characters = new CharacterFolders(CharacterFolder.MAIN_CHARACTER_FOLDER_NAME);
              _plugins = new List<Plugin>();
              _translations = new List<Translation>();
              _rooms = new UnloadedRoomFolders(UnloadedRoomFolder.MAIN_UNLOADED_ROOM_FOLDER_NAME);
              _oldInteractionVariables = new List<OldInteractionVariable>();
              _settings = new Settings();
              _palette = new PaletteEntry[PALETTE_SIZE];
              _sprites = new SpriteFolder("Main");
              _views = new ViewFolders("Main");
              _audioClips = new AudioClipFolders("Main");
              _audioClipTypes = new List<AudioClipType>();
              _textParser = new TextParser();
              _lipSync = new LipSync();
              _propertySchema = new CustomPropertySchema();
              _globalVariables = new GlobalVariables();
              _globalMessages = new string[NUMBER_OF_GLOBAL_MESSAGES];
  			_deletedViewIDs = new Dictionary<int, object>();
              _scripts = new ScriptFolders(ScriptFolder.MAIN_SCRIPT_FOLDER_NAME);
              _scriptsToCompile = new Scripts();
              ScriptAndHeader globalScript = new ScriptAndHeader(
                  new Script(Script.GLOBAL_HEADER_FILE_NAME, "// script header\r\n", true),
                  new Script(Script.GLOBAL_SCRIPT_FILE_NAME, "// global script\r\n", false));
              ((IList<ScriptAndHeader>)_scripts).Add(globalScript);            
              _playerCharacter = null;
  
              for (int i = 0; i < _globalMessages.Length; i++)
              {
                  _globalMessages[i] = string.Empty;
              }
  
              InitializeDefaultPalette();
          }
  
          public string[] GlobalMessages
          {
              get { return _globalMessages; }
          }
  
          public IList<GUI> GUIs
          {
              get { return _guis; }
          }
  
          public IList<InventoryItem> InventoryItems
          {
              get { return _inventoryItems; }
          }
  
          public IList<Character> Characters
          {
              get { return _characters; }
          }
  
          public IList<Dialog> Dialogs
          {
              get { return _dialogs; }            
          }
  
          public IList<MouseCursor> Cursors
          {
              get { return _cursors; }
          }
  
          public IList<Font> Fonts
          {
              get { return _fonts; }
          }
  
          public IList<Translation> Translations
          {
              get { return _translations; }
          }
  
          public List<Plugin> Plugins
          {
              get { return _plugins; }
          }
  
          public IList<IRoom> Rooms
          {
              get { return _rooms; }
          }
  
          public List<OldInteractionVariable> OldInteractionVariables
          {
              get { return _oldInteractionVariables; }
          }
  
          public CustomPropertySchema PropertySchema
          {
              get { return _propertySchema; }
          }
  
          public Character PlayerCharacter
          {
              get { return _playerCharacter; }
              set { _playerCharacter = value; }
          }
  
          public Settings Settings
          {
              get { return _settings; }
          }
  
          public PaletteEntry[] Palette
          {
              get { return _palette; }
          }
  
          public TextParser TextParser
          {
              get { return _textParser; }
          }
  
          public LipSync LipSync
          {
              get { return _lipSync; }
          }
  
          public SpriteFolder RootSpriteFolder
          {
              get { return _sprites; }
              set { _sprites = value; }
          }
  
          public CharacterFolder RootCharacterFolder
          {
              get { return _characters.RootFolder; }
          }
  
          public DialogFolder RootDialogFolder
          {
              get { return _dialogs.RootFolder; }
          }
  
          public ViewFolder RootViewFolder
          {
              get { return _views.RootFolder; }
              set { _views = new ViewFolders(value); }
          }
  
          public ScriptFolder RootScriptFolder
          {
              get { return _scripts.RootFolder; }
          }
  
          public InventoryItemFolder RootInventoryItemFolder
          {
              get { return _inventoryItems.RootFolder; }
          }
  
          public GUIFolder RootGUIFolder
          {
              get { return _guis.RootFolder; }
          }
  
          public UnloadedRoomFolder RootRoomFolder
          {
  
gt:             get { return _rooms.RootFolder; }