private void UpdateLocal() { var game = Game; if (game == null) { TaskDialogViewModel.ShowErrorMessage("No game loaded", "Local data cannot be written without an associated game."); return; } if (game.Script.Editor.ErrorsToolWindow.References.Count > 0) { game.Script.Editor.ErrorsToolWindow.IsVisible = true; TaskDialogViewModel.ShowErrorMessage("Errors exist in script", "Local data cannot be updated until errors are resolved."); return; } if (String.IsNullOrEmpty(game.RACacheDirectory)) { TaskDialogViewModel.ShowErrorMessage("Could not identify emulator directory.", "Local data cannot be updated if the emulator directory for the game is not known."); return; } var dialog = new UpdateLocalViewModel(game); dialog.ShowDialog(); }
private void OpenFile(string filename) { if (!File.Exists(filename)) { TaskDialogViewModel.ShowErrorMessage("Could not open " + Path.GetFileName(filename), filename + " was not found"); return; } int line = 1; int column = 1; string selectedEditor = null; if (Game != null && Game.Script.Filename == filename) { if (Game.Script.CompareState == GeneratedCompareState.LocalDiffers) { if (TaskDialogViewModel.ShowWarningPrompt("Revert to the last saved state?", "Your changes will be lost.") == DialogResult.No) { return; } } // capture current location so we can restore it after refreshing line = Game.Script.Editor.CursorLine; column = Game.Script.Editor.CursorColumn; if (Game.SelectedEditor != null) { selectedEditor = Game.SelectedEditor.Title; } } else if (!CloseEditor()) { return; } var backupFilename = ScriptViewModel.GetBackupFilename(filename); bool usingBackup = false; if (File.Exists(backupFilename)) { switch (TaskDialogViewModel.ShowWarningPrompt("Open autosave file?", "An autosave file from " + File.GetLastWriteTime(backupFilename) + " was found for " + Path.GetFileName(filename) + ".", TaskDialogViewModel.Buttons.YesNoCancel)) { case DialogResult.Cancel: return; case DialogResult.Yes: filename = backupFilename; usingBackup = true; break; default: break; } } var logger = ServiceRepository.Instance.FindService <ILogService>().GetLogger("RATools"); logger.WriteVerbose("Opening " + filename); string content; try { content = File.ReadAllText(filename); } catch (IOException ex) { TaskDialogViewModel.ShowErrorMessage("Unable to read " + Path.GetFileName(filename), ex.Message); return; } int gameId = 0; string gameTitle = null; var tokenizer = Tokenizer.CreateTokenizer(content); tokenizer.SkipWhitespace(); while (tokenizer.Match("//")) { var comment = tokenizer.ReadTo('\n'); if (comment.Contains("#ID")) { var tokens = comment.Split('='); if (tokens.Length > 1) { Int32.TryParse(tokens[1].ToString(), out gameId); } break; } else if (gameTitle == null) { gameTitle = comment.Trim().ToString(); } tokenizer.SkipWhitespace(); } if (gameId == 0) { logger.WriteVerbose("Could not find game ID"); TaskDialogViewModel.ShowWarningMessage("Could not find game ID", "The loaded file did not contain an #ID comment indicating which game the script is associated to."); } if (gameTitle == null) { gameTitle = Path.GetFileNameWithoutExtension(filename); } if (!usingBackup) { AddRecentFile(filename); } GameViewModel viewModel = null; if (gameId != 0) { logger.WriteVerbose("Game ID: " + gameId); foreach (var directory in ServiceRepository.Instance.FindService <ISettings>().EmulatorDirectories) { var dataDirectory = Path.Combine(directory, "RACache", "Data"); var notesFile = Path.Combine(dataDirectory, gameId + "-Notes.json"); if (!File.Exists(notesFile)) { notesFile = Path.Combine(dataDirectory, gameId + "-Notes2.txt"); } if (File.Exists(notesFile)) { logger.WriteVerbose("Found code notes in " + dataDirectory); viewModel = new GameViewModel(gameId, gameTitle); viewModel.AssociateRACacheDirectory(dataDirectory); } } if (viewModel == null) { logger.WriteVerbose("Could not find code notes"); TaskDialogViewModel.ShowWarningMessage("Could not locate code notes for game " + gameId, "The game does not appear to have been recently loaded in any of the emulators specified in the Settings dialog."); viewModel = new GameViewModel(gameId, gameTitle); } } else { viewModel = new GameViewModel(gameId, gameTitle); } var existingViewModel = Game; // if we're just refreshing the current game script, only update the script content, // which will be reprocessed and update the editor list. If it's not the same script, // or notes have changed, use the new view model. if (existingViewModel != null && existingViewModel.GameId == viewModel.GameId && existingViewModel.Script.Filename == filename && existingViewModel.Notes.Count == viewModel.Notes.Count) { // refresh any data from the emulator (code notes, published/local achievement definitions) if (!String.IsNullOrEmpty(viewModel.RACacheDirectory)) { existingViewModel.AssociateRACacheDirectory(viewModel.RACacheDirectory); } existingViewModel.Script.SetContent(content); viewModel = existingViewModel; existingViewModel.SelectedEditor = existingViewModel.Editors.FirstOrDefault(e => e.Title == selectedEditor); existingViewModel.Script.Editor.MoveCursorTo(line, column, Jamiras.ViewModels.CodeEditor.CodeEditorViewModel.MoveCursorFlags.None); } else { if (usingBackup) { viewModel.Script.Filename = Path.GetFileName(filename); var title = viewModel.Title + " (from backup)"; viewModel.SetValue(GameViewModel.TitleProperty, title); } else { viewModel.Script.Filename = filename; } viewModel.Script.SetContent(content); Game = viewModel; } if (viewModel.Script.Editor.ErrorsToolWindow.References.Count > 0) { viewModel.Script.Editor.ErrorsToolWindow.IsVisible = true; } }