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; } }
private void OpenFile(string filename) { if (!File.Exists(filename)) { MessageBoxViewModel.ShowMessage("Could not open " + filename); return; } int line = 1; int column = 1; string selectedEditor = null; if (Game != null && Game.Script.Filename == filename) { if (Game.Script.CompareState == GeneratedCompareState.LocalDiffers) { var vm = new MessageBoxViewModel("Revert to the last saved state? Your changes will be lost."); vm.DialogTitle = "Revert Script"; if (vm.ShowOkCancelDialog() == DialogResult.Cancel) { return; } } // capture current location so we can restore it after refreshing line = Game.Script.Editor.CursorLine; column = Game.Script.Editor.CursorColumn; selectedEditor = Game.SelectedEditor.Title; } else if (!CloseEditor()) { return; } var backupFilename = ScriptViewModel.GetBackupFilename(filename); bool usingBackup = false; if (File.Exists(backupFilename)) { var vm2 = new MessageBoxViewModel("Found an autosave file from " + File.GetLastWriteTime(backupFilename) + ".\nDo you want to open it instead?"); vm2.DialogTitle = Path.GetFileName(filename); switch (vm2.ShowYesNoCancelDialog()) { 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) { MessageBoxViewModel.ShowMessage(ex.Message); return; } var tokenizer = Tokenizer.CreateTokenizer(content); var expressionGroup = new AchievementScriptParser().Parse(tokenizer); int gameId = 0; var idComment = expressionGroup.Comments.FirstOrDefault(c => c.Value.Contains("#ID")); if (idComment != null) { var tokens = idComment.Value.Split('='); if (tokens.Length > 1) { Int32.TryParse(tokens[1].ToString(), out gameId); } } if (gameId == 0) { logger.WriteVerbose("Could not find game ID"); MessageBoxViewModel.ShowMessage("Could not find game id"); return; } if (!usingBackup) { AddRecentFile(filename); } logger.WriteVerbose("Game ID: " + gameId); var gameTitle = expressionGroup.Comments[0].Value.Substring(2).Trim(); GameViewModel viewModel = null; foreach (var directory in ServiceRepository.Instance.FindService <ISettings>().DataDirectories) { var notesFile = Path.Combine(directory, gameId + "-Notes2.txt"); if (File.Exists(notesFile)) { logger.WriteVerbose("Found code notes in " + directory); viewModel = new GameViewModel(gameId, gameTitle, directory.ToString()); } else { notesFile = Path.Combine(directory, gameId + "-Notes.json"); if (File.Exists(notesFile)) { logger.WriteVerbose("Found code notes in " + directory); viewModel = new GameViewModel(gameId, gameTitle, directory.ToString()); } } } if (viewModel == null) { logger.WriteVerbose("Could not find code notes"); MessageBoxViewModel.ShowMessage("Could not locate notes file for game " + gameId); viewModel = new GameViewModel(gameId, gameTitle); } var existingViewModel = Game as GameViewModel; // 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) { existingViewModel.Script.SetContent(content); viewModel = existingViewModel; existingViewModel.SelectedEditor = Game.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; } }