Beispiel #1
0
        void Connect(int rpUserID)
        {
            if (r_UserID == rpUserID)
            {
                return;
            }

            Disconnect();

            r_UserID = rpUserID;

            var rFilename     = r_UserID + ".db";
            var rDatabaseFile = new FileInfo(Path.Combine(RecordDirectory.FullName, rFilename));

            if (!rDatabaseFile.Exists)
            {
                TryMigrateFromOldVersion(rFilename, rDatabaseFile);
            }

            r_Connection = new SQLiteConnection($@"Data Source={rDatabaseFile.FullName}; Page Size=8192", true).OpenAndReturn();

            if (IsReadOnlyMode)
            {
                using (var rSourceConnection = r_Connection)
                {
                    r_Connection = new SQLiteConnection("Data Source=:memory:; Page Size=8192").OpenAndReturn();
                    rSourceConnection.BackupDatabase(r_Connection, "main", "main", -1, null, 0);
                }
            }

            using (var rCommand = r_Connection.CreateCommand())
            {
                rCommand.CommandText =
                    "PRAGMA journal_mode = DELETE; " +
                    "PRAGMA foreign_keys = ON;";

                rCommand.ExecuteNonQuery();
            }

            using (var rTransaction = r_Connection.BeginTransaction())
            {
                CheckVersion();

                r_Resources         = new ResourcesRecords(r_Connection).ConnectAndReturn();
                r_Experience        = new ExperienceRecords(r_Connection).ConnectAndReturn();
                r_Expedition        = new ExpeditionRecords(r_Connection).ConnectAndReturn();
                r_Construction      = new ConstructionRecords(r_Connection).ConnectAndReturn();
                r_Development       = new DevelopmentRecords(r_Connection).ConnectAndReturn();
                r_Sortie            = new SortieRecords(r_Connection).ConnectAndReturn();
                r_Battle            = new BattleRecords(r_Connection).ConnectAndReturn();
                r_Fate              = new FateRecords(r_Connection).ConnectAndReturn();
                r_RankingPoints     = new RankingPointsRecords(r_Connection).ConnectAndReturn();
                r_SortieConsumption = new SortieConsumptionRecords(r_Connection).ConnectAndReturn();

                QuestProgress = new QuestProgressRecords(r_Connection).ConnectAndReturn();

                foreach (var rProvider in r_CustomRecordsGroupProviders)
                {
                    var rGroup = rProvider.Create(r_Connection).ConnectAndReturn();
                    r_CustomRecordsGroups[rGroup.GroupName] = rGroup;
                }

                rTransaction.Commit();
            }

            r_BattleDetail = new BattleDetailRecords(r_Connection, r_UserID).ConnectAndReturn();

            r_Connection.Update += OnDatabaseUpdate;

            IsConnected = true;
        }
        void Connect(int rpUserID)
        {
            if (r_UserID == rpUserID)
            {
                return;
            }

            Resources?.Dispose();
            Ships?.Dispose();
            Experience?.Dispose();
            Expedition?.Dispose();
            Construction?.Dispose();
            Development?.Dispose();
            Sortie?.Dispose();
            Battle?.Dispose();
            RankingPoints?.Dispose();
            Fate?.Dispose();
            QuestProgress?.Dispose();
            BattleDetail?.Dispose();

            if (r_Connection != null)
            {
                r_Connection.Update -= OnDatabaseUpdate;

                r_Connection.Dispose();
            }

            foreach (var rCustomGroup in r_CustomRecordsGroups.Values)
            {
                rCustomGroup.Dispose();
            }

            IsConnected = false;

            r_UserID = rpUserID;

            r_Connection = new SQLiteConnection($@"Data Source=Records\{r_UserID}.db;Page Size=8192").OpenAndReturn();

            if (IsReadOnlyMode)
            {
                using (var rSourceConnection = r_Connection)
                {
                    r_Connection = new SQLiteConnection("Data Source=:memory:;Page Size=8192").OpenAndReturn();
                    rSourceConnection.BackupDatabase(r_Connection, "main", "main", -1, null, 0);
                }
            }

            using (var rTransaction = r_Connection.BeginTransaction())
            {
                CheckVersion();

                Resources     = new ResourcesRecords(r_Connection).ConnectAndReturn();
                Ships         = new ShipsRecords(r_Connection).ConnectAndReturn();
                Experience    = new ExperienceRecords(r_Connection).ConnectAndReturn();
                Expedition    = new ExpeditionRecords(r_Connection).ConnectAndReturn();
                Construction  = new ConstructionRecords(r_Connection).ConnectAndReturn();
                Development   = new DevelopmentRecords(r_Connection).ConnectAndReturn();
                Sortie        = new SortieRecords(r_Connection).ConnectAndReturn();
                Battle        = new BattleRecords(r_Connection).ConnectAndReturn();
                RankingPoints = new RankingPointsRecords(r_Connection).ConnectAndReturn();
                Fate          = new FateRecords(r_Connection).ConnectAndReturn();

                QuestProgress = new QuestProgressRecords(r_Connection).ConnectAndReturn();

                foreach (var rProvider in r_CustomRecordsGroupProviders)
                {
                    var rGroup = rProvider.Create(r_Connection).ConnectAndReturn();
                    r_CustomRecordsGroups[rGroup.GroupName] = rGroup;
                }

                rTransaction.Commit();
            }

            BattleDetail = new BattleDetailRecords(r_Connection, r_UserID).ConnectAndReturn();

            r_Connection.Update += OnDatabaseUpdate;

            IsConnected = true;
        }