Ejemplo n.º 1
0
        /// <summary>
        /// Function called by the Open Button. Open a file and load the application
        /// </summary>
        /// <param name="sender">The object calling this function.</param>
        /// <param name="e">The RoutedEventArgs associated</param>
        private void OpenFile(object sender, RoutedEventArgs e)
        {
            m_windowData.Clean();

            OpenFileDialog openFile = new OpenFileDialog();

            if (openFile.ShowDialog() == true)
            {
                //Unzip the database (.db) file
                String tempDir = $"c:/Temp/DandDAdventures/{DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1)).TotalSeconds}/";
                String tempDB  = tempDir + "database.db";
                Directory.CreateDirectory(tempDir);
                using (FileStream fs = new FileStream(openFile.FileName, FileMode.Open))
                {
                    using (ZipArchive zip = new ZipArchive(fs))
                    {
                        ZipArchiveEntry dbEntry = zip.GetEntry("database.db");
                        using (Stream dbStream = dbEntry.Open())
                        {
                            using (FileStream fileStream = File.Create(tempDB))
                            {
                                dbStream.CopyTo(fileStream);
                            }
                        }
                    }
                }

                //Open and load the Database
                if (m_windowData.SQLDatabase == null)
                {
                    m_windowData.SQLDatabase = new DBHandler(String.Format("DataSource = {0}", tempDB));
                }
                else
                {
                    var sql = new SQLiteConnection(String.Format("DataSource = {0}", tempDB));
                    sql.Open();
                    m_windowData.SQLDatabase.ChangeSQLiteConnection(sql);
                }

                //Change to the memory
                m_windowData.SQLDatabase.ChangeSQLiteConnection(m_windowData.SQLDatabase.Backup(":memory:"));

                //Delete the temporary database
                File.Delete(tempDB);

                //Load the content in the FrontEnd
                m_CharacterTabItem.LoadContent(m_windowData);
                m_CharacterView.LoadContent(m_windowData);

                m_placeTabItem.LoadContent(m_windowData);
                m_placeView.LoadContent(m_windowData);

                m_windowData.SQLPath   = openFile.FileName;
                m_windowData.CanSave   = true;
                m_mainPanel.Visibility = Visibility.Visible;
            }
        }