Skip to content

Commit

Permalink
Added logging
Browse files Browse the repository at this point in the history
  • Loading branch information
Kaszanas committed May 26, 2021
1 parent 198a907 commit f13a15f
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 30 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ SC2MapLocaleExtractor.exe <input_directory> <output_filepath_with_filename>.json

1. [StormLib](https://github.com/Kaszanas/StormLib)
2. [nlohmann/json](https://github.com/nlohmann/json)
3. [spdlog](https://github.com/gabime/spdlog/tree/v1.8.5)
3. [spdlog v1.8.5](https://github.com/gabime/spdlog/tree/v1.8.5)

## Build

Expand Down
25 changes: 18 additions & 7 deletions src/SC2MapLocaleExtractor/SC2MapLocaleExtractor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,37 +22,48 @@ int main(int argc, char** argv)

helpers::initialize_logger();
auto logger = spdlog::get("logger");

logger->info("Initialized logger!");

if(argc < 3) {
std::cout << "You have not provided required arguments!" << "\n";
std::cout << "Usage: "<< argv[0] <<" <input_directory> <output_filepath_with_filename>.json";
return 1;
}
//if(argc < 3) {
// logger->warn("You have not provided required arguments!");
// logger->warn("Usage: {} <input_directory> <output_filepath_with_filename>.json", argv[0]);
// std::cout << "Press ENTER to continue...";
// std::getchar();
// return 1;
//}

std::string directoryString = "D:/Projects/GolangProjects/src/GoSC2Science/map_translator/MAPS";
std::string outputFile = "output.json";

std::string directoryString = argv[1];
//std::string directoryString = argv[1];
logger->info("directoryString was specified as: {}", directoryString);
std::string outputFile = argv[2];
//std::string outputFile = argv[2];
logger->info("outputFile was specified as: {}", outputFile);

std::optional<nlohmann::json> maybe_mapping = extractors::locale_extraction_pipeline(directoryString);
if (maybe_mapping.has_value())
{
logger->info("Detected that maybe_mapping contains a value.");
// Getting the final string value from json structure:
nlohmann::json final_json_mapping = *maybe_mapping;
std::string final_json_string = final_json_mapping.dump(4);
logger->info("Dumped final_json_mapping into a string.");

// Writing to drive at output location:
std::ofstream out(outputFile);
out << final_json_string;
logger->info("Saved final_json_string to a file.");
}
else
{
logger->warn("Failed to obtain mapping, returning EXIT_FAILURE.");
return EXIT_FAILURE;
}

std::cout << "Press ENTER to continue...";
std::getchar();

logger->info("Finished main(), returning EXIT_SUCCESS.");
return EXIT_SUCCESS;
}
55 changes: 33 additions & 22 deletions src/SC2MapLocaleExtractor/extractors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@ namespace extractors
// Function that extracts one locale file information from MPQ Archive:
std::optional<std::string> extract_locale_from_mpq(HANDLE mpqArchive, const SFILE_FIND_DATA& file_data)
{
auto logger = spdlog::get("logger");
logger->info("Entered extract_locale_from_mpq()");
HANDLE openedLocaleFile;
bool result = SFileOpenFileEx(mpqArchive, file_data.cFileName, SFILE_OPEN_FROM_MPQ, &openedLocaleFile);
if (!result)
{
std::cout << "Couldn't open the file that is within MPQ Archive, error: " << GetLastError() << "\n";
logger->warn("Couldn't open the file that is within MPQ Archive, error: {}", GetLastError());
return std::nullopt;
}

Expand All @@ -31,20 +33,23 @@ namespace extractors
DWORD bytesRead;
result = SFileReadFile(openedLocaleFile, localeFileBuffer.data(), localeFileBuffer.size() * sizeof(decltype(localeFileBuffer)::value_type), &bytesRead, nullptr);
if (!result) {
std::cout << "Couldn't read the file that was found within MPQ Archive, error: " << GetLastError() << "\n";
logger->warn("Couldn't read the file that was found within MPQ Archive, error: {}", GetLastError());
return std::nullopt;
}

SFileCloseFile(openedLocaleFile);

logger->info("Closed MPQ file!");
std::string locale(localeFileBuffer.begin(), localeFileBuffer.end());
logger->info("Finished extract_locale_from_mpq() returning.");
return std::make_optional(locale);
}


// Function extracting localized map name:
std::optional<std::string> extract_map_name_string(std::string game_description)
{
auto logger = spdlog::get("logger");
logger->info("Entered extract_map_name_string()");
// Splitting the string into lines:
std::vector<std::string> newline_split = helpers::split_string(game_description, "\r\n");

Expand All @@ -64,13 +69,14 @@ namespace extractors

if (map_name.empty())
{
std::cout << "Detected empty map name returning \n";
// TODO: Empty map check doesn't allow to find what is the currently processed map and doesn't allow for sufficient debugging later on.
logger->warn("Detected empty map_name returning.");
return std::nullopt;
}

logger->info("Finished extract_map_name_string(), returning.");
return std::make_optional(map_name);
}
std::cout << "Error! Detected empty map name returning \n";
logger->warn("Detected empty map name returning.");
return std::nullopt;
}

Expand All @@ -79,20 +85,25 @@ namespace extractors
// "koKR.SC2Data\\LocalizedData\\GameStrings.txt" -> "koKR"
std::string locale_region_extractor(const std::string& region_name)
{
auto logger = spdlog::get("logger");
logger->info("Entered locale_region_extractor()");
std::string dot_delimiter = ".";
std::string locale_region = region_name.substr(0, region_name.find(dot_delimiter));
logger->info("Calculated locale_region = {}", locale_region);
return locale_region;
}

// Function extracting locale information from a single MPQ ".SC2Map" file
// sample output: {"koKR": "koreanMapName", "enUS": "englishMapName"}
std::optional<nlohmann::json> locale_extractor(const std::filesystem::path& filepath)
{
auto logger = spdlog::get("logger");
logger->info("Entered locale_extractor()");
// Opening MPQ Archive:
HANDLE MPQArchive;
if (!SFileOpenArchive(filepath.c_str(), 0, 0, &MPQArchive))
{
std::cout << "Couldn't open MPQ archive, error: " << GetLastError() << "\n";
logger->warn("Couldn't open MPQ archive, error: {}", GetLastError());
return std::nullopt;
}

Expand All @@ -102,7 +113,7 @@ namespace extractors
HANDLE searchHandle = SFileFindFirstFile(MPQArchive, "*GameStrings.txt", &foundLocaleFileData, nullptr);
if (searchHandle == NULL)
{
std::cout << "Couldn't find file in MPQ archive, error: " << GetLastError() << "\n";
logger->warn("Couldn't find file in MPQ archive, error: {}", GetLastError());
return std::nullopt;
}

Expand All @@ -114,35 +125,32 @@ namespace extractors
auto maybe_locale = extract_locale_from_mpq(MPQArchive, foundLocaleFileData);
// Checking if data was extracted:
if (!maybe_locale.has_value()) {
std::cout << "Couldn't extract locale from MPQ, error: " << "\n";
logger->warn("Couldn't extract locale from MPQ");
continue;
}

// Obtaining region code:
std::string region_name = foundLocaleFileData.cFileName;
std::string locale_region = locale_region_extractor(region_name);

// Extracting map name:
std::optional<std::string> maybe_map_name_string = extract_map_name_string(*maybe_locale);
if (!maybe_map_name_string.has_value())
{
std::cout << "Detected empty map_name_string, skipping." << "\n";
logger->warn("Detected empty map_name_string in filepath: {}. This happened while parsing region: {}. Skipping!", filepath.string(), locale_region);
continue;
}
std::string map_name_string = *maybe_map_name_string;

// Obtaining region code:
std::string region_name = foundLocaleFileData.cFileName;
std::string locale_region = locale_region_extractor(region_name);

if (map_name_string == "")
{
std::cout << "Detected empty string as a foreign_map_name, skipping. This happened when reading: " << locale_region << "\n";
}

myMapping[locale_region] = map_name_string;

} while (SFileFindNextFile(searchHandle, &foundLocaleFileData));

// Freeing up resource:
SFileCloseFile(searchHandle);
logger->info("Closed searched file.");
SFileCloseArchive(MPQArchive);
logger->info("Closed MPQ archive.");

return std::make_optional(myMapping);
}
Expand All @@ -152,15 +160,18 @@ namespace extractors
// Sample output: {"koreanMapName": "englishMapName", "polishMapName": "englishMapName"}
std::optional<nlohmann::json> locale_extraction_pipeline(const std::string& filepath)
{
auto logger = spdlog::get("logger");
logger->info("Entered locale_extraction_pipeline()");
// Initializing empty mapping container:
std::vector<nlohmann::json> mapping_container;
std::vector<std::filesystem::path> detected_files;
logger->info("Initialized variables mapping_container and detected_files.");

// Reading directory contents from filepath:
helpers::directory_reader(detected_files, filepath, ".SC2Map");
if (detected_files.empty())
{
std::cout << "directory_reader returned empty vector" << "\n";
logger->warn("directory_reader failed to populated detected_files! Returning std::nullopt");
return std::nullopt;
}

Expand All @@ -175,7 +186,7 @@ namespace extractors
}
else
{
std::cout << "Couldn't extract locale information from a file." << "\n";
logger->warn("Couldn't extract locale information from a file: {}", file.string());
continue;
}
}
Expand All @@ -188,7 +199,7 @@ namespace extractors
return std::make_optional(final_mapping);
}

std::cout << "Couldn't extract locale information from a file." << "\n";
logger->warn("Failed to generate final locale mapping! Returning std::nullopt");
return std::nullopt;
}

Expand Down
15 changes: 15 additions & 0 deletions src/SC2MapLocaleExtractor/helpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ namespace helpers
// string to wstring helper function:
std::wstring s2ws(const std::string& str)
{
auto logger = spdlog::get("logger");
logger->info("Entered s2ws()");

int size_needed = MultiByteToWideChar(CP_UTF8, 0, &str[0], (int)str.size(), NULL, 0);
std::wstring wstrTo(size_needed, 0);
MultiByteToWideChar(CP_UTF8, 0, &str[0], (int)str.size(), &wstrTo[0], size_needed);
Expand All @@ -32,6 +35,9 @@ namespace helpers
// wstring to string helper function:
std::string ws2s(const std::wstring& wstr)
{
auto logger = spdlog::get("logger");
logger->info("Entered ws2s()");

if (wstr.empty()) return std::string();
int size_needed = WideCharToMultiByte(CP_UTF8, 0, &wstr[0], -1, NULL, 0, NULL, NULL);
std::string strTo(size_needed, 0);
Expand All @@ -43,6 +49,9 @@ namespace helpers
// Function that reads and lists a directory into a supplied vector:
void directory_reader(std::vector<std::filesystem::path>& vector_to_populate, const std::string& input_directory, const std::string& get_extension)
{
auto logger = spdlog::get("logger");
logger->info("Entered directory_reader()");

// Iterating over files that were detected in a directory:
for (const auto& file : std::filesystem::directory_iterator(input_directory))
{
Expand All @@ -59,6 +68,9 @@ namespace helpers
// Function that splits a string and returns a vector of strings:
std::vector<std::string> split_string(const std::string& str, const std::string& delimiter)
{
auto logger = spdlog::get("logger");
logger->info("Entered split_string()");

std::vector<std::string> strings;

std::string::size_type pos = 0;
Expand All @@ -78,6 +90,9 @@ namespace helpers
// Function generating a final locale mapping from {"foreignName": "englishName"}
std::optional<nlohmann::json> generate_final_locale_mapping(const std::vector<nlohmann::json>& not_mapped_locales)
{
auto logger = spdlog::get("logger");
logger->info("entered generate_final_locale_mapping()");

// Initializing function variables:
nlohmann::json final_mapping;

Expand Down

0 comments on commit f13a15f

Please sign in to comment.