Implement better progress indicator

This commit is contained in:
Lily Tsuru 2023-08-21 21:39:54 -04:00
parent 96eb9c99dd
commit 3176c53629
2 changed files with 51 additions and 32 deletions

View File

@ -20,6 +20,7 @@ elseif(NOT SSXTOOLS_LINKER)
set(SSXTOOLS_LINKER "bfd") set(SSXTOOLS_LINKER "bfd")
endif() endif()
include(FetchContent)
ssxtools_set_alternate_linker() ssxtools_set_alternate_linker()
add_executable(swsf_scramble add_executable(swsf_scramble

View File

@ -1,8 +1,6 @@
#include <boost/asio/experimental/concurrent_channel.hpp>
#include <boost/asio/io_context.hpp> #include <boost/asio/io_context.hpp>
#include <boost/asio/post.hpp> #include <boost/asio/post.hpp>
#include <boost/asio/thread_pool.hpp> #include <boost/asio/thread_pool.hpp>
#include <boost/system/detail/error_code.hpp>
#include <format> #include <format>
#include <iostream> #include <iostream>
@ -65,49 +63,69 @@ bool NextCode(std::string& buffer, size_t start) {
return false; return false;
} }
void ThreadFunction(char prefix) { // see https://bisqwit.iki.fi/jutut/kuvat/programming_examples/cpp_thread_tutorial/ver05.cc
namespace {
unsigned ln = 1;
auto Color(int n, const std::string& s) {
return "\33[38;5;" + std::to_string(n) + 'm' + s + "\33[m";
}
auto Line(int l) {
int m = l - ln;
ln = l;
return "\r" + (m < 0 ? "\33[" + std::to_string(-m) + 'A' : std::string(m, '\n'));
}
std::mutex print_lock;
} // namespace
struct BruteThreadState {
BruteThreadState(unsigned line) : line(line) {}
void DisplayProgress(const std::string& code) {
std::lock_guard<std::mutex> lk(print_lock);
std::cout << Line(line) << std::format("Thread {:2}: Trying code {}", line, Color(140, code)) << std::flush;
}
void BruteForce(char prefix) {
for(std::uint32_t i = 2; i < code_length; ++i) { for(std::uint32_t i = 2; i < code_length; ++i) {
std::string test_buffer(i, 'a'); std::string test_buffer(i, 'a');
test_buffer[0] = prefix; test_buffer[0] = prefix;
// test all possible combinations // test all possible combinations
while(true) { while(true) {
std::cout << std::format("trying code \"{}\"...\r", test_buffer); // std::cout << std::format("trying code \"{}\"...\r", test_buffer);
DisplayProgress(test_buffer);
if(SwsfScramble(test_buffer) == code_hash) { if(SwsfScramble(test_buffer) == code_hash) {
std::cerr << std::format("match: {}", test_buffer); std::cerr << std::format("match: {}\n", test_buffer);
} }
if(!NextCode(test_buffer, 1)) if(!NextCode(test_buffer, 1))
break; break;
} }
} }
#if 0
std::string test_buffer(code_length, 'a');
test_buffer[0] = prefix;
while(true) {
std::cout << std::format("trying code \"{}\"...\r", test_buffer);
if(SwsfScramble(test_buffer) == code_hash) {
std::cerr << std::format("match: {}", test_buffer);
} }
if(!NextCode(test_buffer, 1)) private:
return; unsigned line;
} };
#endif
}
int main() { int main() {
asio::io_context ioc; asio::io_context ioc;
asio::thread_pool pool(26); asio::thread_pool pool(26);
// Channel auto line = 0u;
// ChannelType channel(ioc);
// post onto the thread pool // post onto the thread pool
for(int i = 0; i < 26; ++i) for(int i = 0; i < 26; ++i)
asio::post(pool, [i]() { ThreadFunction('a' + i); }); asio::post(pool, [i, l = line++]() {
BruteThreadState state(l);
state.BruteForce('a' + i);
});
pool.join(); pool.join();
return 0; return 0;