Example #1
0
        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;
            }
        }