@@ -43,12 +43,30 @@ fs::path to_dds_path(const fs::path& png_path, const fs::path& output) {
4343 return (output / png_path.stem ()) += dds_extension.data ();
4444}
4545
46- void add_files (const fs::path& png_path, const fs::path& dds_path, paths_vector& paths, bool overwrite) {
47- if (overwrite || !fs::exists (dds_path)) { paths.emplace_back (png_path, dds_path); }
46+ class should_generate_dds final {
47+ public:
48+ should_generate_dds (bool overwrite, bool overwrite_new)
49+ : _overwrite{overwrite}
50+ , _overwrite_new{overwrite_new} {}
51+
52+ bool operator ()(const fs::path& png_path, const fs::path& dds_path) const {
53+ return _overwrite || !fs::exists (dds_path) ||
54+ _overwrite_new && (fs::last_write_time (png_path) > fs::last_write_time (dds_path));
55+ }
56+
57+ private:
58+ bool _overwrite;
59+ bool _overwrite_new;
60+ };
61+
62+ void add_files (
63+ const fs::path& png_path, const fs::path& dds_path, paths_vector& paths, const should_generate_dds& should_generate) {
64+ if (should_generate (png_path, dds_path)) { paths.emplace_back (png_path, dds_path); }
4865}
4966
5067void process_directory (paths_vector& paths, const fs::path& input, const fs::path& output, bool different_output,
51- const todds::regex& regex, todds::regex::scratch_type& scratch, bool overwrite, std::size_t depth) {
68+ const todds::regex& regex, todds::regex::scratch_type& scratch, const should_generate_dds& should_generate,
69+ std::size_t depth) {
5270 fs::path current_output = output;
5371 const fs::directory_entry dir{input};
5472 for (fs::recursive_directory_iterator itr{dir}; itr != fs::recursive_directory_iterator{}; ++itr) {
@@ -60,7 +78,7 @@ void process_directory(paths_vector& paths, const fs::path& input, const fs::pat
6078 }
6179 const fs::path dds_path =
6280 to_dds_path (current_input, different_output ? current_output : current_input.parent_path ());
63- add_files (current_input, dds_path, paths, overwrite );
81+ add_files (current_input, dds_path, paths, should_generate );
6482 if (different_output && !fs::exists (current_output)) {
6583 // Create the output folder if necessary.
6684 fs::create_directories (current_output);
@@ -75,28 +93,28 @@ paths_vector get_paths(const todds::args::data& arguments) {
7593 const bool different_output = static_cast <bool >(arguments.output );
7694 const fs::path output = different_output ? arguments.output .value () : input.parent_path ();
7795
78- const bool overwrite = arguments.overwrite ;
96+ const should_generate_dds should_generate (arguments. overwrite , arguments.overwrite_new ) ;
7997 const auto depth = arguments.depth ;
8098
8199 const auto & regex = arguments.regex ;
82100 todds::regex::scratch_type scratch = regex.allocate_scratch ();
83101
84102 paths_vector paths{};
85103 if (fs::is_directory (input)) {
86- process_directory (paths, input, output, different_output, regex, scratch, overwrite , depth);
104+ process_directory (paths, input, output, different_output, regex, scratch, should_generate , depth);
87105 } else if (is_valid_source (input, arguments.regex , scratch)) {
88106 const fs::path dds_path = to_dds_path (input, output);
89- add_files (input, dds_path, paths, overwrite );
107+ add_files (input, dds_path, paths, should_generate );
90108 } else if (has_extension (input, txt_extension)) {
91109 boost::nowide::fstream stream{input};
92110 std::string buffer;
93111 while (std::getline (stream, buffer)) {
94112 const fs::path current_path{buffer};
95113 if (fs::is_directory (current_path)) {
96- process_directory (paths, current_path, current_path, false , regex, scratch, overwrite , depth);
114+ process_directory (paths, current_path, current_path, false , regex, scratch, should_generate , depth);
97115 } else if (is_valid_source (current_path, arguments.regex , scratch)) {
98116 const fs::path dds_path = to_dds_path (current_path, current_path.parent_path ());
99- add_files (current_path, dds_path, paths, overwrite );
117+ add_files (current_path, dds_path, paths, should_generate );
100118 } else {
101119 boost::nowide::cerr << current_path.string () << " is not a PNG file or a directory.\n " ;
102120 }
0 commit comments