Example #1
0
        public void UpdateCache()
        {
            TileEntities.Clear();
            Entities.Clear();
            SQLiteTransaction trans = database.BeginTransaction();
            int numChunksChanged    = 0;
            int numNewChunks        = 0;
            int numChunksTotal      = 0;

            //mThread = new Thread(delegate()
            //{
            map.SetBusy("Updating cache...");
            map.SetBusy(string.Format("Please wait, {0}...\r\n{1} new, {2} changed", (FirstCache) ? "creating cache (may take a while)" : "updating cache", numNewChunks, numChunksChanged));
            Dimension[] dims = (Dimension[])map.GetDimensions();
            Dimension   dim  = dims[map.Dimension];

            map.ForEachChunkFile(dim.ID, delegate(IMapHandler _map, string file)
            {
                bool NeedsUpdate = false;
                bool NewChunk    = false;
                using (SQLiteCommand cmd = database.CreateCommand())
                {
                    Vector2i pos;
                    cmd.CommandText      = "SELECT cnkMD5,cnkX,cnkZ,dimID FROM Chunks WHERE cnkFile='" + file + "';";
                    SQLiteDataReader rdr = cmd.ExecuteReader();
                    if (!rdr.HasRows)
                    {
                        NewChunk    = true;
                        NeedsUpdate = true;
                        pos         = map.GetChunkCoordsFromFile(file, true);
                    }
                    else
                    {
                        pos = new Vector2i(
                            (int)((long)rdr["cnkX"]),
                            (int)((long)rdr["cnkZ"])
                            );
                        if (!mFileCoords.ContainsKey(file))
                        {
                            mFileCoords.Add(file, new Vector3i(pos.X, pos.Y,
                                                               (int)((long)rdr["dimID"])));
                        }
                    }

                    if (dim.MinimumChunk.X > pos.X)
                    {
                        dim.MinimumChunk.X = pos.X;
                    }
                    if (dim.MaximumChunk.X < pos.X)
                    {
                        dim.MaximumChunk.X = pos.X;
                    }
                    if (dim.MinimumChunk.Y > pos.Y)
                    {
                        dim.MinimumChunk.Y = pos.Y;
                    }
                    if (dim.MaximumChunk.Y < pos.Y)
                    {
                        dim.MaximumChunk.Y = pos.Y;
                    }

                    if (!NeedsUpdate)
                    {
                        if (rdr["cnkMD5"].ToString() != GetMD5HashFromFile(file))
                        {
                            NeedsUpdate = true;
                        }
                    }
                    if (NeedsUpdate)
                    {
                        if (NewChunk)
                        {
                            numNewChunks++;
                        }
                        else
                        {
                            numChunksChanged++;
                        }
                        if (pos == null)
                        {
                            return;
                        }
                        //Console.WriteLine(string.Format("Updating chunk {0} in {1} ({2})...", pos, dim.Name, file));
                        map.SetBusy(string.Format("Please wait, {0}...\r\n{1} new, {2} changed", (FirstCache) ? "creating cache (may take a while)" : "updating cache", numNewChunks, numChunksChanged));
                        Chunk c = map.GetChunk(pos.X, pos.Y);
                        map.UnloadChunks();
                    }
                    System.Windows.Forms.Application.DoEvents();
                }

                // Dump it from RAM onto disk to prevent bloat.
                if (numChunksChanged + numNewChunks % 500 == 499)
                {
                    Console.WriteLine("Saving cache...");
                    map.SetBusy(string.Format("Please wait, {0}...\r\n[Saving]", (FirstCache) ? "creating cache (may take a while)" : "updating cache"));
                    trans.Commit();
                    trans = database.BeginTransaction();
                    map.UnloadChunks();
                }
            });
            UpdateDimension(dim);
            map.SetIdle();
            trans.Commit();     // NOW save to disk.
            //});
            //mThread.Start();
        }
Example #2
0
 private void UpdateDimension(Dimension dim)
 {
     try
     {
         rwLock.AcquireWriterLock(300);
         using (SQLiteCommand cmd = database.CreateCommand())
         {
             cmd.CommandText = "REPLACE INTO Dimensions (dimID,dimFolder,dimMinX,dimMinZ,dimMaxX,dimMaxZ) VALUES (@dimID,@dimFolder,@dimMinX,@dimMinZ,@dimMaxX,@dimMaxZ);";
             cmd.Parameters.AddRange(new SQLiteParameter[]{
                 new SQLiteParameter("@dimID",dim.ID),
                 new SQLiteParameter("@dimFolder",dim.Folder),
                 new SQLiteParameter("@dimMinX",dim.MinimumChunk.X),
                 new SQLiteParameter("@dimMinZ",dim.MinimumChunk.Y),
                 new SQLiteParameter("@dimMaxX",dim.MaximumChunk.X),
                 new SQLiteParameter("@dimMaxZ",dim.MaximumChunk.Y)
             });
             cmd.Prepare();
             cmd.ExecuteNonQuery();
         }
     }
     finally
     {
         if (rwLock.IsWriterLockHeld)
             rwLock.ReleaseWriterLock();
     }
 }