public void GetPendingUpdateQueues()
        {
            LobbyResult lobbyResult = new LobbyResult()
            {
                Host = "www.test.com",
                LobbyId = 1,
                LobbyIdSpecified = true,
                Name = "TestUpdate"
            };

            string testPath = Path.Combine(Allegiance.CommunitySecuritySystem.Client.Integration.AllegianceRegistry.LobbyPath, lobbyResult.Name);

            if(Directory.Exists(testPath) == false)
                Directory.CreateDirectory(testPath);

            File.WriteAllText(Path.Combine(testPath, "test1.txt"), "this is a test file. It is for testing.");

            var encryption = new Encryption<SHA1>();
            var checksum = encryption.Calculate(Path.Combine(testPath, "test1.txt"));

            AutoUpdateResult autoUpdateResult = new AutoUpdateResult()
            {
                AutoUpdateBaseAddress = "http://www.pork.com",
                Files = new FindAutoUpdateFilesResult[]
                {
                    new FindAutoUpdateFilesResult()
                    {
                        AutoUpdateFileId = 1,
                        AutoUpdateFileIdSpecified = true,
                         CurrentVersion = "1.0.0.0",
                         DateCreated = DateTime.Parse("4/8/2014"),
                         DateCreatedSpecified = true,
                         DateModified = DateTime.Parse("4/8/2014"),
                         DateModifiedSpecified = true,
                         Filename = "test1-notfound.txt",
                         IsProtected = false,
                         IsProtectedSpecified = true,
                         LobbyId = 1,
                         LobbyIdSpecified = true,
                         ValidChecksum = checksum
                    }
                }
            };

            if (File.Exists("autoupdate.ds") == true)
                File.Delete("autoupdate.ds");

            List<FindAutoUpdateFilesResult> result = Client.Service.AutoUpdate.ProcessPendingUpdates(lobbyResult, autoUpdateResult);

            Assert.AreEqual(1, result.Count, "File was not found, one update should have applied.");

            autoUpdateResult.Files[0].Filename = "test1.txt";

            result = Client.Service.AutoUpdate.ProcessPendingUpdates(lobbyResult, autoUpdateResult);

            Assert.AreEqual(0, result.Count, "No updates should have been done, checksums match.");

            autoUpdateResult.Files[0].ValidChecksum = "INVALID_CHECKSUM";

            result = Client.Service.AutoUpdate.ProcessPendingUpdates(lobbyResult, autoUpdateResult);

            Assert.AreEqual(1, result.Count, "One update should have been done, checksum miss-match with no prior update record.");

            var autoUpdate = DataStore.Open("autoupdate.ds", "Ga46^#a042");

            var lastResults = new Dictionary<string, FindAutoUpdateFilesResult>();
            //foreach (var res in result)
            //	lastResults.Add(res.Filename, res);

            string dataKey = "Files_" + lobbyResult.LobbyId;
            autoUpdate[dataKey] = lastResults;

            autoUpdate.Save();

            result = Client.Service.AutoUpdate.ProcessPendingUpdates(lobbyResult, autoUpdateResult);

            Assert.AreEqual(1, result.Count, "One update should have been done, checksum miss-match prior update dictionary exists, but no record for file.");

            autoUpdateResult.Files[0].ValidChecksum = checksum;

            foreach (var res in result)
                lastResults.Add(res.Filename, res);

            autoUpdate[dataKey] = lastResults;

            autoUpdate.Save();

            result = Client.Service.AutoUpdate.ProcessPendingUpdates(lobbyResult, autoUpdateResult);

            Assert.AreEqual(0, result.Count, "No updates should have been done, versions match.");

            autoUpdateResult.Files[0].CurrentVersion = "1.0.0.1";

            result = Client.Service.AutoUpdate.ProcessPendingUpdates(lobbyResult, autoUpdateResult);

            Assert.AreEqual(1, result.Count, "File version miss-match with previous version of file, update applied.");

            autoUpdateResult.Files[0].CurrentVersion = "1.0.0.0";
            autoUpdateResult.Files[0].IsProtected = false;
            autoUpdateResult.Files[0].ValidChecksum = "INVALID_CHECKSUM";

            result = Client.Service.AutoUpdate.ProcessPendingUpdates(lobbyResult, autoUpdateResult);

            Assert.AreEqual(1, result.Count, "updates should have been done, versions are same, checksum is different, file is not protected and the user has not modified the file.");

            autoUpdateResult.Files[0].DateModified = DateTime.Parse("1/1/2015");

            result = Client.Service.AutoUpdate.ProcessPendingUpdates(lobbyResult, autoUpdateResult);

            Assert.AreEqual(0, result.Count, "updates should not have been done, versions are same, checksum is different, file is not protected and the user has modified the file since the last update.");

            //autoUpdateResult.Files[0].DateModified = DateTime.Parse("4/8/2014");

            autoUpdateResult.Files[0].CurrentVersion = "1.0.0.0";
            autoUpdateResult.Files[0].IsProtected = true;
            autoUpdateResult.Files[0].ValidChecksum = "INVALID_CHECKSUM";

            result = Client.Service.AutoUpdate.ProcessPendingUpdates(lobbyResult, autoUpdateResult);

            Assert.AreEqual(1, result.Count, "updates should have been done, versions are same, checksum is different, file is protected and the user has modified the file.");
        }
		public static List<FindAutoUpdateFilesResult> ProcessPendingUpdates(LobbyResult lobby, AutoUpdateResult results)
		{
			//Initialize Checksum class to use SHA1
			var encryption = new Encryption<SHA1>();

			//var root        = AllegianceRegistry.EXEPath; //Allegiance root directory
			var autoUpdate = DataStore.Open("autoupdate.ds", "Ga46^#a042");
			string dataKey = "Files_" + lobby.LobbyId;
			var lastResults = autoUpdate[dataKey] as Dictionary<string, FindAutoUpdateFilesResult>;
			List<FindAutoUpdateFilesResult> updateQueue = new List<FindAutoUpdateFilesResult>();

			//Check files which need update
			foreach (var file in results.Files)
			{
				DebugDetector.AssertCheckRunning();

				// You can put this in if you are testing the launcher with the production CSS server. 
				// Turn off file protection on the launcher on the CSS server's Auto Update version of the launcher, 
				// then drop the debug version of the launcher into your local game directory. Then you can 
				// launch the launcher from the debugger and work with it.
//#if DEBUG
//                if (file.Filename.EndsWith("Launcher.exe", StringComparison.InvariantCultureIgnoreCase) == true
//                    || file.Filename.EndsWith("Launcher.pdb", StringComparison.InvariantCultureIgnoreCase) == true)
//                    continue;
//#endif 

				string path;

				if (string.Equals(file.Filename, GlobalSettings.ClientExecutableName)
					|| string.Equals(file.Filename, GlobalSettings.ClientExecutablePDB))
					path = Path.Combine(AllegianceRegistry.LobbyPath, file.Filename);
				else
					path = GetLobbyPath(lobby, file.Filename);

				//Check that all files exist
				if (!File.Exists(path))
				{
					Log.Write("File did not exist: " + path + ", will update.");
					updateQueue.Add(file);
					continue;
				}

				//Check that all file versions match
				if (lastResults != null && lastResults.ContainsKey(file.Filename)
					&& (file.CurrentVersion != lastResults[file.Filename].CurrentVersion))
				{
					Log.Write("File version mismatch, will update: " + file.Filename + ", server version: " + file.CurrentVersion + ", local version: " + lastResults[file.Filename].CurrentVersion);

					updateQueue.Add(file);
					continue;
				}

				// Test for checksum match, and if they don't replace the file if:
				// * This is the first time AutoUpdate has run for this installation.
				// * This is the first time this file has been seen by AutoUpdate
				// * The file has the protected flag turned on.
				var checksum = encryption.Calculate(path);
				if (!string.Equals(file.ValidChecksum, checksum))
				{
					// If there haven't been any updates at all, and the checksums don't match, then overwrite the unknown file. (old installation / left over files from a previous uninstall)
					if (lastResults == null)
					{
						Log.Write("No prior autoupdate records, will updated: " + path);
						updateQueue.Add(file);
						continue;
					}

					// If there wasn't a prior update applied for the file, and the checksums don't match, then overwrite the unknown file. (An older file is on disk, and was re-added to the auto update system)
					if (!lastResults.ContainsKey(file.Filename))
					{
						Log.Write("No record of file in autoupdate history, will update: " + path);
						updateQueue.Add(file);
						continue;
					}

					// If the file was changed on the server, but not modified by the user, then update.
					if (lastResults[file.Filename].DateModified == file.DateModified)
					{
						Log.Write("file was not user modified, and a change was detected from AU, will update: " + path);
						updateQueue.Add(file);
						continue;
					}

					// If the file is protected and the hashes don't match, then update.
					if (file.IsProtected)
					{
						Log.Write("File checksum mismatch, will update: " + path + ", server checksum: " + file.ValidChecksum + ", local checksum: " + checksum);
						updateQueue.Add(file);
						continue;
					}
				}
			}

			return updateQueue;
		}