Can discover "real" junks

This commit is contained in:
2020-12-27 16:24:55 +01:00
parent db07dd89ab
commit fdad270558
2 changed files with 65 additions and 105 deletions
+41 -96
View File
@@ -91,72 +91,31 @@ class optimizer
private static function build_removand_sublist(string $folder) : array private static function build_removand_sublist(string $folder) : array
{ {
$queue = array(); $queue = array();
$lowercase = array();
foreach (glob(utils::globsafe($folder) . "/*") as $file) foreach (glob(utils::globsafe($folder) . "/*") as $file)
{ {
if (is_dir($file)) if (is_dir($file))
{ {
list($new_queue, $new_lowercase) = self::build_removand_sublist($file); // recursion $queue = array_merge($queue, self::build_removand_sublist($file)); // recursion
$lowercase = array_merge($lowercase, $new_lowercase);
$queue = array_merge($queue, $new_queue);
} }
else else
{ {
$queue[] = $file; $queue[] = $file;
$lowercase[] = strtolower($file);
}
}
return array($queue, $lowercase);
}
public static function build_removand_list(osu_library $library) : array
{
$queue = array();
$lowercase = array();
foreach ($library->get_folders() as $folder)
{
list($new_queue, $new_lowercase) = self::build_removand_sublist($folder);
$queue = array_merge($queue, $new_queue);
$lowercase = array_merge($lowercase, $new_lowercase);
}
return array($queue, $lowercase);
}
public static function build_empty_dir_sublist(string $folder) : array
{
$queue = array();
$glob = glob(utils::globsafe($folder) . "/*");
if (empty($glob))
{
$queue[] = strtolower($file);
}
else
{
foreach ($glob as $file)
{
if (is_dir($file))
{
$queue = array_merge($queue, self::build_empty_dir_sublist($file));
}
} }
} }
return $queue; return $queue;
} }
public static function build_empty_dir_list(osu_library $library) : array public static function build_removand_list(osu_library $library) : array
{ {
$queue = array(); $queue = array();
foreach ($library->get_folders() as $folder) foreach ($library->get_folders() as $folder)
{ {
$queue = array_merge($queue, self::build_empty_dir_sublist($folder)); $queue = array_merge($queue, self::build_removand_sublist($folder));
} }
return $queue; return $queue;
} }
@@ -164,76 +123,62 @@ class optimizer
{ {
$background_files = $library->get_backgrounds(); $background_files = $library->get_backgrounds();
$video_files = $library->get_videos(); $video_files = $library->get_videos();
$audio_files = $library->get_audiofiles();
$storyboard_files = $library->get_storyboards(); $storyboard_files = $library->get_storyboards();
$hitsound_files = $library->get_hitsounds(); $hitsound_files = $library->get_hitsounds();
$audio_files = $library->get_audiofiles();
$osb_files = $library->get_osb_files(); // $osu_files = $library->get_osu_files();
$osu_files = $library->get_osu_files(); // $osb_files = $library->get_osb_files();
$physical_excluded = $library->get_parsed_files();
$essential_excluded = array_merge($background_files, $video_files, $audio_files); $essential_excluded = array_merge($background_files, $video_files, $audio_files);
$physical_excluded = array_merge($osb_files, $osu_files); // $physical_excluded = array_merge($osb_files, $osu_files);
$other_excluded = array_merge($storyboard_files, $hitsound_files); $other_excluded = array_merge($storyboard_files, $hitsound_files);
$excluded = array_merge($essential_excluded, $physical_excluded, $other_excluded); $excluded = array_merge($essential_excluded, $physical_excluded, $other_excluded);
$lowercase_excluded = array(); return $excluded;
foreach ($excluded as $key => $value)
{
$lowercase_excluded[$key] = strtolower($value);
} }
$peppy_excluded_lowercase = array(); public static function cut_extension(string $path) : string
foreach ($lowercase_excluded as $value)
{ {
$extension = pathinfo($value, PATHINFO_EXTENSION) ?? ""; $directory = pathinfo($path, PATHINFO_DIRNAME) ?? "";
if (!empty($directory)) $directory .= "/"; // append slash if set
$directory = pathinfo($value, PATHINFO_DIRNAME) ?? ""; return $directory . (pathinfo($path, PATHINFO_FILENAME) ?? "");
$directory = !empty($directory) ? $directory. "/" : ""; // append slash if set
$filename = pathinfo($value, PATHINFO_FILENAME);
if (mb_ereg_match("([jJ][pP][eE]?[gG]|[pP][nN][gG])", $extension))
{
$image_extensions = [ "png", "jpg", "jpeg", strtolower($extension) ];
$image_extensions = array_unique($image_extensions);
foreach ($image_extensions as $image_extension)
{
$peppy_excluded_lowercase[] = $directory . $filename . "." . $image_extension;
}
}
else if (mb_ereg_match("([wW][aA][vV]|[mM][pP]3|[oO][gG][gG])", $extension))
{
$sound_extensions = [ "wav", "mp3", "ogg", strtolower($extension) ];
$sound_extensions = array_unique($sound_extensions);
foreach ($sound_extensions as $sound_extension)
{
$peppy_excluded_lowercase[] = $directory . $filename . "." . $sound_extension;
}
}
else
{
$peppy_excluded_lowercase[] = $value;
}
} }
return $peppy_excluded_lowercase; // because peppy thinks file extensions are wildcards:
// you can have image.mp3 in storyboards in jfif
// and you can have ogg vorbis hitsounds in mysound.wav.png.whatever
public static function array_diff_ver_peppy(array $files, array $exclusions) : array
{
// osu! is case insensitive
$files_lowercase = array();
foreach ($files as $key => $value)
{
$files_lowercase[$key] = self::cut_extension(mb_strtolower($value));
}
$exclusions_lowercase = array();
foreach ($exclusions as $key => $value)
{
$exclusions_lowercase[$key] = self::cut_extension(mb_strtolower($value));
}
// return values from the original, but only the ones that did not get removed
return array_intersect_key($files, array_diff($files_lowercase, $exclusions_lowercase));
} }
public static function remove_other(osu_library $library) : void public static function remove_other(osu_library $library) : void
{ {
// get the path and lowercase paths $removand = self::build_removand_list($library);
list($removand, $removand_lower) = self::build_removand_list($library);
// get the lowercase exclusions
$exclusions = self::build_excluded_list($library); $exclusions = self::build_excluded_list($library);
// subtract the exclusions (lowercase because osu! is case-insensitive) $junk_files = self::array_diff_ver_peppy($removand, $exclusions);
$check = array_diff($removand_lower, $exclusions);
// take the original paths based on the keys from the subtraction above foreach ($junk_files as $file)
// (this is only important for case-sensitive file systems like unix)
$final = array_intersect_key($removand, $check);
foreach ($final as $file)
{ {
if (self::is_skinnable($file)) continue; // ignore default hitsounds if (self::is_skinnable($file)) continue; // ignore default hitsounds
// if (file_exists($file)) unlink($file); // if (file_exists($file)) unlink($file);
+24 -9
View File
@@ -307,14 +307,15 @@ class osu_library
return $videos; return $videos;
} }
public function get_parsed_files() : array public function get_parsed_files_with_details() : array
{ {
$db = $this->get_library(); $db = $this->get_library();
$files = array(); $files = array();
foreach ($db as $beatmapset) foreach ($db as $beatmapset)
{ {
foreach($beatmapset["difficulties"] as $beatmap) // warning: key isn't globally unique!!
foreach($beatmapset["difficulties"] as $key => $beatmap)
{ {
if (!empty($beatmap["path"]) && !empty($beatmap["format"])) if (!empty($beatmap["path"]) && !empty($beatmap["format"]))
{ {
@@ -326,16 +327,30 @@ class osu_library
return $files; return $files;
} }
public function get_parsed_files() : array
{
$files = $this->get_parsed_files_with_details();
$paths = array();
foreach ($files as $file)
{
$paths[] = $file["path"];
}
return $paths;
}
public function get_osu_files() : array public function get_osu_files() : array
{ {
$files = $this->get_parsed_files(); $files = $this->get_parsed_files_with_details();
$osu_file_format_declaration = "osu file format ";
$osu_files = array(); $osu_files = array();
foreach ($files as $file) foreach ($files as $file)
{ {
if (mb_strpos($beatmap["format"], "osu file format " !== false)) if (mb_strpos($file["format"], $osu_file_format_declaration) !== false)
{ {
$osu_files[] = $beatmap["path"]; $osu_files[] = $file["path"];
} }
} }
@@ -344,14 +359,14 @@ class osu_library
public function get_osb_files() : array public function get_osb_files() : array
{ {
$files = $this->get_parsed_files(); $files = $this->get_parsed_files_with_details();
$osb_files = array(); $osb_files = array();
foreach ($files as $file) foreach ($files as $file)
{ {
if ($beatmap["format"] == "storyboard") if ($file["format"] == "storyboard")
{ {
$osb_files[] = $beatmap["path"]; $osb_files[] = $file["path"];
} }
} }
@@ -360,7 +375,7 @@ class osu_library
public function get_broken_files() : array public function get_broken_files() : array
{ {
$files = $this->get_parsed_files(); $files = $this->get_parsed_files_with_details();
$broken_files = array(); $broken_files = array();
foreach ($files as $file) foreach ($files as $file)