public FindApplicationViewModel(FindApplicationModel dataModel, IDialogService dialogService, Func<IOpenFileDialog> openFileDialogFactory)
        {
            Contract.Requires(dialogService != null);
            Contract.Requires(openFileDialogFactory != null);

            _dialogService = dialogService;
            _openFileDialogFactory = openFileDialogFactory;
            _dataModel = dataModel;

            // Will bubble property change events from the Model to the ViewModel.
            _dataModel.PropertyChanged += (sender, e) => OnPropertyChanged(e.PropertyName);
        }
Beispiel #2
0
        public bool Init(string[] args)
        {
            // Detection and correction of local settings of SE install location.
            var filePath = ToolboxUpdater.GetApplicationFilePath();

            var validApps = new string[] {
                "SpaceEngineers.exe",
                "SpaceEngineersDedicated.exe",
                "MedievalEngineers.exe",
                "MedievalEngineersDedicated.exe"
            };

            if (GlobalSettings.Default.PromptUser || !ToolboxUpdater.ValidateSpaceEngineersInstall(filePath))
            {
                if (Directory.Exists(filePath))
                {
                    foreach (var validApp in validApps)
                    {
                        var testPath = Path.Combine(filePath, validApp);
                        if (File.Exists(testPath))
                        {
                            filePath = testPath;
                            break;
                        }
                    }
                }

                var faModel = new FindApplicationModel()
                {
                    GameApplicationPath = filePath
                };
                var faViewModel = new FindApplicationViewModel(faModel);
                var faWindow = new WindowFindApplication(faViewModel);

                if (faWindow.ShowDialog() == true)
                {
                    filePath = faModel.GameBinPath;
                }
                else
                {
                    return false;
                }
            }

            // Update and save user path.
            GlobalSettings.Default.SEBinPath = filePath;
            GlobalSettings.Default.Save();

            var ignoreUpdates = args.Any(a => a.ToUpper() == "/X" || a.ToUpper() == "-X");
            var oldDlls = true; // args.Any(a => a.ToUpper() == "/OLDDLL" || a.ToUpper() == "-OLDDLL");
            var altDlls = !oldDlls;

            // Go looking for any changes in the Dependant Space Engineers assemblies and immediately attempt to update.
            if (!ignoreUpdates && !altDlls && ToolboxUpdater.IsBaseAssembliesChanged() && !Debugger.IsAttached)
            {
                ToolboxUpdater.RunElevated(Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "SEToolboxUpdate"), "/B " + String.Join(" ", args), false, false);
                return false;
            }

            var proc = Process.GetCurrentProcess();
            if (Process.GetProcessesByName(proc.ProcessName).Length == 1)
            {
                // Clean up Temp files if this is the only instance running.
                TempfileUtil.DestroyTempFiles();
            }

            // Dot not load any of the Space Engineers assemblies or dependant classes before this point.
            // ============================================

            // Alternate experimental method for loading the Space Engineers API assemblies.
            // Copy them to temporary path, then load with reflection on demand through the AppDomain.
            //if (altDlls)
            //{
            //    _tempBinPath = ToolboxUpdater.GetBinCachePath();
            //    var searchPath = GlobalSettings.Default.SEBinPath;

            //    DirectoryInfo checkDir = null;
            //    var counter = 0;

            //    while (checkDir == null && counter < 10)
            //    {
            //        if (Directory.Exists(_tempBinPath))
            //            break;

            //        checkDir = Directory.CreateDirectory(_tempBinPath);

            //        if (checkDir == null)
            //        {
            //            // wait a while, as the drive may be processing Windows Explorer which had a lock on the Directory after prior cleanup.
            //            System.Threading.Thread.Sleep(100);
            //        }

            //        counter++;
            //    }

            //    foreach (var file in ToolboxUpdater.CoreSpaceEngineersFiles)
            //    {
            //        var filename = Path.Combine(searchPath, file);
            //        var destFilename = Path.Combine(_tempBinPath, file);

            //        if (ToolboxUpdater.DoFilesDiffer(searchPath, _tempBinPath, file))
            //        {
            //            try
            //            {
            //                File.Copy(filename, destFilename, true);
            //            }
            //            catch { }
            //        }
            //    }

            //    // Copy directories which contain Space Engineers language resources.
            //    var dirs = Directory.GetDirectories(searchPath);

            //    foreach (var sourceDir in dirs)
            //    {
            //        var dirName = Path.GetFileName(sourceDir);
            //        var destDir = Path.Combine(_tempBinPath, dirName);
            //        Directory.CreateDirectory(destDir);

            //        foreach (string oldFile in Directory.GetFiles(sourceDir, "*.*", SearchOption.AllDirectories))
            //        {
            //            try
            //            {
            //                File.Copy(oldFile, Path.Combine(destDir, Path.GetFileName(oldFile)), true);
            //            }
            //            catch { }
            //        }
            //    }

            //    AppDomain currentDomain = AppDomain.CurrentDomain;
            //    currentDomain.AssemblyResolve += currentDomain_AssemblyResolve;
            //}

#if DEBUG
            // This will make it hairy for testing the AppDomain stuff.
            //#warning Force the local debugger to load the Types allowing inspection.
            //var settings0 = new Sandbox.Common.ObjectBuilders.MyObjectBuilder_SessionSettings();
#endif

            return true;
        }
 public FindApplicationViewModel(FindApplicationModel dataModel)
     : this(dataModel, ServiceLocator.Resolve<IDialogService>(), ServiceLocator.Resolve<IOpenFileDialog>)
 {
 }