public void ExtractEntities()
        {
            string levelPath;
            string jsonPath;
            DateTime saveTime;
            lock (ThreadLock)
            {
                levelPath = LevelPath;
                jsonPath = JsonPath;

                saveTime = saveJsonTime;

            }
            ThreadPool.QueueUserWorkItem((object state) =>
            {
                lock (ThreadLock)
                {
                    if (saveTime == null || (DateTime.Now - saveTime).TotalMilliseconds > 50)
                    {
                        OnEvent(String.Format("Extracting entities from '{0}'", Path.GetFileName(levelPath)));

                        IMap map = null;
                        int count = 0;
                        while (map == null)
                        {
                            try
                            {
                                map = new Map(File.ReadAllBytes(levelPath));
                            }
                            catch (IOException)
                            {
                                if (count < 15)
                                {
                                    count++;
                                    Thread.Sleep(100);
                                }
                                else
                                {
                                    throw;
                                }
                            }
                        }

                        JsonMapParser jsonMapParser = new JsonMapParser();
                        string json = jsonMapParser.ExtractEntities(map, new string[] { "light_point", "light_spot", "light_ambient", "fog_controls", "fog_area_modifier", "ambient_sound", "reverb" });

                        OnEvent(String.Format("Saving extracted entities to '{0}'", jsonPath));

                        Directory.CreateDirectory(Path.GetDirectoryName(jsonPath));
                        File.WriteAllText(jsonPath, json);

                        OnEvent(String.Format("Finished extracting entities from '{0}'", Path.GetFileName(levelPath)));

                        saveLevelTime = DateTime.Now;
                    }
                }
            });
        }
        protected void ConvertToLevel(string jsonPath, string levelPath, string outPath)
        {
            Console.WriteLine("LEVEL " + outPath + " " + jsonPath + " " + levelPath);
            IMap map = new Map(File.ReadAllBytes(levelPath));
            string jsonStr = File.ReadAllText(jsonPath);

            JsonMapParser jsonMapParser = new JsonMapParser();
            jsonMapParser.ImportJson(map, jsonStr);

            Directory.CreateDirectory(Path.GetDirectoryName(outPath));
            File.WriteAllBytes(outPath, map.Write());
        }
        public void OnChangeJson(string path)
        {
            string levelPath;
            string jsonPath;
            DateTime saveTime;
            string backupDir;
            bool createBackups;
            lock (ThreadLock)
            {
                createBackups = CreateBackups;
                backupDir = BackupDir;
                levelPath = LevelPath;
                jsonPath = JsonPath;
                saveTime = saveLevelTime;
            }

            ThreadPool.QueueUserWorkItem((object state) =>
            {
                lock (ThreadLock)
                {
                    if (saveTime == null || (DateTime.Now - saveTime).TotalMilliseconds > 50)
                    {
                        if (createBackups && !String.IsNullOrWhiteSpace(backupDir))
                        {
                            Directory.CreateDirectory(backupDir);
                            string levelName = Path.GetFileName(LevelPath);
                            string backup = MapUtils.FindNextFileVersionName(Path.Combine(backupDir, levelName));
                            OnEvent(String.Format("Creating backup at '{0}'", backup));
                            File.Copy(levelPath, backup);
                        }
                        OnEvent(String.Format("Parsing '{0}'", jsonPath));


                        IMap map = new Map(File.ReadAllBytes(levelPath));
                        string jsonStr = File.ReadAllText(path);

                        JsonMapParser jsonMapParser = new JsonMapParser();
                        jsonMapParser.ImportJson(map, jsonStr);

                        OnEvent(String.Format("Rebuilding '{0}'", Path.GetFileName(levelPath)));

                        Directory.CreateDirectory(Path.GetDirectoryName(levelPath));
                        File.WriteAllBytes(levelPath, map.Write());

                        OnEvent(String.Format("Finished rebuilding '{0}'", Path.GetFileName(levelPath)));

                        
                        saveJsonTime = DateTime.Now;
                    }
                }
            });
        }