Open() public static method

public static Open ( ) : void
return void
示例#1
0
        /// <summary>
        /// Run all child simulations with the given optimal values,
        /// and store the results in the given checkpoint name.
        /// </summary>
        /// <param name="checkpointName">Name of the checkpoint.</param>
        /// <param name="optimalValues">Changes to be applied to the models.</param>
        /// <param name="fileName">Name of the apsimx file run by the optimiser.</param>
        private void RunSimsWithOptimalValues(string fileName, string checkpointName, IEnumerable <CompositeFactor> optimalValues)
        {
            IDataStore storage = FindInScope <IDataStore>();

            // First, clone the simulations (we don't want to change the values
            // of the parameters in the original file).
            Simulations clonedSims = FileFormat.ReadFromFile <Simulations>(fileName, out List <Exception> errors);

            if (errors != null && errors.Count > 0)
            {
                throw errors[0];
            }

            // Apply the optimal values to the cloned simulations.
            clonedSims = EditFile.ApplyChanges(clonedSims, optimalValues);

            DataStore clonedStorage = clonedSims.FindChild <DataStore>();

            clonedStorage.Close();
            clonedStorage.CustomFileName = storage.FileName;
            clonedStorage.Open();

            // Run the child models of the cloned CroptimizR.
            Runner runner = new Runner(clonedSims);

            errors = runner.Run();
            if (errors != null && errors.Count > 0)
            {
                throw errors[0];
            }
            storage.Writer.AddCheckpoint(checkpointName);
            storage.Writer.SetCheckpointShowGraphs(checkpointName, true);
        }
示例#2
0
        public void ForeignCharacterTest()
        {
            string    path    = Path.Combine(Path.GetTempPath(), "文档.db");
            DataStore storage = new DataStore();

            storage.FileName = path;
            Assert.DoesNotThrow(() => storage.Open(false));
            try
            {
                storage.Close();
                File.Delete(path);
            }
            catch
            {
            }
        }
示例#3
0
 /// <summary>Look through all models. For each simulation found set the filename.</summary>
 private void SetFileNameInAllSimulations()
 {
     foreach (Model child in Apsim.ChildrenRecursively(this))
     {
         if (child is Simulation)
         {
             (child as Simulation).FileName = FileName;
         }
         else if (child is DataStore)
         {
             DataStore storage = child as DataStore;
             storage.Close();
             storage.UpdateFileName();
             storage.Open();
         }
     }
 }
示例#4
0
 /// <summary>Look through all models. For each simulation found set the filename.</summary>
 private void SetFileNameInAllSimulations()
 {
     foreach (Model child in this.FindAllDescendants().ToList())
     {
         if (child is Simulation)
         {
             (child as Simulation).FileName = FileName;
         }
         else if (child is DataStore)
         {
             DataStore storage = child as DataStore;
             storage.Close();
             storage.UpdateFileName();
             storage.Open();
         }
     }
 }
        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.");
        }
示例#6
0
        public static PendingUpdates GetPendingUpdateQueues(ClientService.ClientService service)
        {
            //Initialize Checksum class to use SHA1
            var encryption = new Encryption <SHA1>();

            PendingUpdates returnValue = new PendingUpdates();

            var lobbies = ServiceHandler.Service.CheckAvailableLobbies();

            foreach (LobbyResult lobby in lobbies)
            {
                //Get autoupdate files associated with lobby
                var results = service.CheckForUpdates(lobby.LobbyId, true);
                //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();

                    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;
                    }

                    if (lastResults == null)
                    {
                        Log.Write("No prior autoupdate records, will updated: " + path);
                        updateQueue.Add(file);
                        continue;
                    }

                    //Check if we don't have a record of the file at all
                    if (!lastResults.ContainsKey(file.Filename))
                    {
                        Log.Write("No record of file in autoupdate history, will update: " + path);
                        updateQueue.Add(file);
                        continue;
                    }

                    //Check that all file versions match
                    if (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;
                    }

                    //Compare hashes with all protected files to those on-disk
                    if (file.IsProtected)
                    {
                        var checksum = encryption.Calculate(path);
                        if (!string.Equals(file.ValidChecksum, checksum))
                        {
                            Log.Write("File checksum mismatch, will update: " + path + ", server checksum: " + file.ValidChecksum + ", local checksum: " + checksum);

                            updateQueue.Add(file);
                        }
                    }
                }

                returnValue.AutoUpdateBaseAddress.Add(lobby.LobbyId, results.AutoUpdateBaseAddress);
                returnValue.AllFilesInUpdatePackage.Add(lobby.LobbyId, results.Files);
                returnValue.PendingUpdateList.Add(lobby.LobbyId, updateQueue);
            }

            return(returnValue);
        }
示例#7
0
        public static bool ProcessPendingUpdates(PendingUpdates pendingUpdates, LobbyResult lobby, AutoupdateProgressCallback progressCallback, Control parentControl)
        {
            string dataKey    = "Files_" + lobby.LobbyId;
            var    autoUpdate = DataStore.Open("autoupdate.ds", "Ga46^#a042");
            bool   updateExe  = false;
            int    received   = 0;
            bool   continueDownloadingFiles = true;

            //Get files from auto-update
            foreach (var file in pendingUpdates.PendingUpdateList[lobby.LobbyId])
            {
                if (continueDownloadingFiles == false)
                {
                    return(false);
                }

                var filename     = file.Filename;
                var downloadPath = String.Empty;

                //Check if filename matches the ClientExecutableName
                if (string.Equals(file.Filename, GlobalSettings.ClientExecutableName))
                {
                    filename     = GlobalSettings.TempExecutableName;
                    updateExe    = true;
                    downloadPath = Path.Combine(AllegianceRegistry.LobbyPath, filename);
                }
                // Launcher.PDB also goes into the lobby root.
                else if (string.Equals(file.Filename, GlobalSettings.ClientExecutablePDB))
                {
                    downloadPath = Path.Combine(AllegianceRegistry.LobbyPath, file.Filename);
                }
                else
                {
                    downloadPath = GetLobbyPath(lobby, filename);
                }

                if (!Directory.Exists(Path.GetDirectoryName(downloadPath)))
                {
                    Directory.CreateDirectory(Path.GetDirectoryName(downloadPath));
                }

                var message = string.Format("Updating {0}...", file.Filename);

                try
                {
                    continueDownloadingFiles = DownloadFile(++received, pendingUpdates.PendingUpdateList[lobby.LobbyId].Count, pendingUpdates.AutoUpdateBaseAddress[lobby.LobbyId],
                                                            lobby.LobbyId, file.Filename, downloadPath, lobby.Name, progressCallback);

                    message += " Succeeded.";
                }
                catch (Exception error)
                {
                    message += string.Concat(" Failed: ", error.Message);
                    message += "\n autoUpdateURL: " + pendingUpdates.AutoUpdateBaseAddress[lobby.LobbyId];
                    message += "\n lobby.LobbyId: " + lobby.LobbyId;
                    message += "\n received: " + received;
                    message += "\n updateQueue.Count: " + pendingUpdates.PendingUpdateList[lobby.LobbyId].Count;
                    message += "\n file.Filename: " + file.Filename;
                    message += "\n downloadPath: " + downloadPath;
                    message += "\n exception details: " + error.ToString();
                    throw;
                }
                finally
                {
                    Log.Write(message);
                }
            }

            //Save updated AutoUpdateResults dictionary to datastore
            if (pendingUpdates.PendingUpdateList[lobby.LobbyId].Count != 0)
            {
                var dictionary = new Dictionary <string, FindAutoUpdateFilesResult>();
                foreach (var f in pendingUpdates.AllFilesInUpdatePackage[lobby.LobbyId])
                {
                    dictionary.Add(f.Filename, f);
                }

                autoUpdate[dataKey] = dictionary;
                autoUpdate.Save();
            }

            if (updateExe)             //Start the new executable, which will wait for this process to complete its autoupdate
            {
                var signal = new UiInteraction(delegate()
                {
                    MessageBox.Show("The Allegiance Security Client has an update and must restart.");
                    var tempLauncher = Path.Combine(AllegianceRegistry.LobbyPath, GlobalSettings.TempExecutableName);
                    Process.Start(tempLauncher);
                    Application.Exit();
                });

                if (parentControl.InvokeRequired == true)
                {
                    parentControl.Invoke(signal);
                }
                else
                {
                    signal();
                }
            }
            else
            {
                //Update the registry to assign the configuration url to the lobby's host value
                UpdateConfigurationUrlForSelectedLobby(lobby);
            }

            return(updateExe);
        }
示例#8
0
        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);
        }