aternosorg / thanos

PHP library to automatically detect and remove unused chunks from Minecraft worlds.
https://packagist.org/packages/aternos/thanos
MIT License
220 stars 20 forks source link

`hasExistingChunks()` seems to return false for the first region #15

Closed ChillerDragon closed 1 year ago

ChillerDragon commented 1 year ago

While trying to implement https://github.com/aternosorg/thanos/issues/14 my self.

https://github.com/aternosorg/thanos/blob/8c08de679875d58c8ae9f3caf478a4d9f737be33/src/Region/AnvilRegion.php#L153-L161

I changed this to

    public function save(bool $verify = true): void
    {
        if(!$this->hasExistingChunks()) {
            echo "[-] want to delete this savedchunks=" . $this->countSavedChunks() . " file=" . $this->getDestination() . "\n";
            unlink($this->getDestination());
        } else {
            echo "[+] want to keep this savedchunks=" . $this->countSavedChunks() . " file=" . $this->getDestination() . "\n";
        }
    }

Which always said the first region has 0 chunks. Meaning after running this multiple times it continued deleting more and more chunks until the whole world was gone. Is this a flaw in my messy change or in thanos?

Im not quite sure how the iterator magic works but is there any chance that the save() is called before it was able to caculate the saved chunks?

KurtThiemann commented 1 year ago

hasExistingChunks() returns true, if any chunk of that region was already saved by calling $chunk->save(). It is used internally to determine whether a region file needs to be created in the output directory. If no chunks from the original region should be saved, the file can just be skipped instead of writing an empty region file.

To be fair, the method could have a better name that more accurately describes what it is checking, but I don’t think that would be worth making a breaking change.

ChillerDragon commented 1 year ago

hasExistingChunks() returns true, if any chunk of that region was already saved by calling $chunk->save(). It is used internally to determine whether a region file needs to be created in the output directory. If no chunks from the original region should be saved, the file can just be skipped instead of writing an empty region file.

To be fair, the method could have a better name that more accurately describes what it is checking, but I don’t think that would be worth making a breaking change.

That much I understood/assumed already.

But why does it seem to always return false for the first region file it finds?

ChillerDragon commented 1 year ago
[chiller@arch ~/Desktop/git/wipe_lgl/src_thanos]$ rm -rf ../cleaned_world/ && ./thanos.php ../world/ ../cleaned_world
[-] want to delete this file=../cleaned_world/region/r.-1.-1.mca
[+] want to keep this file=../cleaned_world/region/r.-1.-1.mca
[+] want to keep this file=../cleaned_world/region/r.-1.0.mca
[+] want to keep this file=../cleaned_world/region/r.0.-1.mca
[+] want to keep this file=../cleaned_world/region/r.0.0.mca
Removed 2421 chunks in 0.09 seconds
[chiller@arch ~/Desktop/git/wipe_lgl/src_thanos]$ rm ../world/region/r.-1.-1.mca
[chiller@arch ~/Desktop/git/wipe_lgl/src_thanos]$ rm -rf ../cleaned_world/ && ./thanos.php ../world/ ../cleaned_world
[-] want to delete this file=../cleaned_world/region/r.-1.0.mca
[+] want to keep this file=../cleaned_world/region/r.-1.0.mca
[+] want to keep this file=../cleaned_world/region/r.0.-1.mca
[+] want to keep this file=../cleaned_world/region/r.0.0.mca
Removed 1891 chunks in 0.07 seconds
[chiller@arch ~/Desktop/git/wipe_lgl/src_thanos]$ rm ../world/region/r.-1.0.mca
[chiller@arch ~/Desktop/git/wipe_lgl/src_thanos]$ rm -rf ../cleaned_world/ && ./thanos.php ../world/ ../cleaned_world
[-] want to delete this file=../cleaned_world/region/r.0.-1.mca
[+] want to keep this file=../cleaned_world/region/r.0.-1.mca
[+] want to keep this file=../cleaned_world/region/r.0.0.mca
Removed 1356 chunks in 0.05 seconds
[chiller@arch ~/Desktop/git/wipe_lgl/src_thanos]$ rm ../world/region/r.0.-1.mca
[chiller@arch ~/Desktop/git/wipe_lgl/src_thanos]$ rm -rf ../cleaned_world/ && ./thanos.php ../world/ ../cleaned_world
[-] want to delete this file=../cleaned_world/region/r.0.0.mca
[+] want to keep this file=../cleaned_world/region/r.0.0.mca
Removed 675 chunks in 0.03 seconds
[chiller@arch ~/Desktop/git/wipe_lgl/src_thanos]$ rm ../world/region/r.0.0.mca
[chiller@arch ~/Desktop/git/wipe_lgl/src_thanos]$ rm -rf ../cleaned_world/ && ./thanos.php ../world/ ../cleaned_world
Removed 0 chunks in 0.00 seconds
[chiller@arch ~/Desktop/git/wipe_lgl/src_thanos]$ git show
commit 9c7c383df62375920ac39926d9812d90c22d467f (HEAD -> chiller_add_print)
Author: ChillerDragon <ChillerDragon@gmail.com>
Date:   Mon Apr 10 12:48:42 2023 +0200

    Add print

diff --git a/src/Region/AnvilRegion.php b/src/Region/AnvilRegion.php
index 0b98130..8c52270 100644
--- a/src/Region/AnvilRegion.php
+++ b/src/Region/AnvilRegion.php
@@ -157,6 +157,9 @@ class AnvilRegion implements RegionInterface
                 $this->getDestination(),
                 $verify
             );
+            echo "[+] want to keep this file=" . $this->getDestination() . "\n";
+        } else {
+            echo "[-] want to delete this file=" . $this->getDestination() . "\n";
         }
     }

When snapping it seems to mark the first region as ready for deletion. Okay weird because I did stuff in those chunks but okay. It says all other regions have active chunks okay. So I delete the first region file and run thanos again without touching the region files in any other way (sever is offline during the whole time), When running thanos again it says that the first region file again is without any active chunks. Which used to be the second region file in the prior run where thanos detected active chunks. So if I delete that too and repeate the process thanos eats up the whole world until nothing is left.

ChillerDragon commented 1 year ago

Okay the problem seems to be me deleting it manually when calling thanos on thanos output it does not delete more. So its for sure a me issue then.