GetChunkCoordsFromFile() public method

public GetChunkCoordsFromFile ( string file, bool fromMapCache = false ) : Vector2i
file string
fromMapCache bool
return Vector2i
Beispiel #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();
        }