From 4e206fae5be6093b36c7779ba2a4a3679f0e754b Mon Sep 17 00:00:00 2001 From: Star Rauchenberger Date: Thu, 23 Nov 2023 01:47:18 +0000 Subject: something --- script.cpp | 106 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 106 insertions(+) create mode 100644 script.cpp (limited to 'script.cpp') diff --git a/script.cpp b/script.cpp new file mode 100644 index 0000000..509d4a7 --- /dev/null +++ b/script.cpp @@ -0,0 +1,106 @@ +#include +#include +#include +#include +#include +#include +#include + +#include "script.h" + +Script::Script(const std::filesystem::path& path, std::string_view file_prefix) + : file_prefix_(file_prefix) { + { + std::ifstream script_file(path); + std::string line; + std::ostringstream text_stream; + while (std::getline(script_file, line)) { + text_stream << line << std::endl; + } + text_ = text_stream.str(); + } + + std::string search = std::string("load(\"") + std::string(file_prefix); + std::string_view corpus = text_; + int location; + while ((location = corpus.find(search)) != std::string_view::npos) { + corpus.remove_prefix(location + search.size()); + int divider = corpus.find_first_of("\""); + if (divider == std::string_view::npos) { + throw std::invalid_argument( + "I don't know how this is happening but I guess I gotta report it."); + } + std::string script_path(corpus.substr(0, divider)); + corpus.remove_prefix(divider + 1); + loaded_scripts_.push_back(script_path); + } +} + +void Script::ReplaceScriptReferences( + const std::map& script_ids) { + std::ostringstream new_text; + std::string_view corpus = text_; + int divider = corpus.find_first_of("\n"); + if (divider == std::string_view::npos) { + throw std::invalid_argument("Script is one line??"); + } + new_text << corpus.substr(0, divider + 1) << std::endl; + ; + corpus.remove_prefix(divider + 1); + + std::map path_to_export; + for (const std::string& loaded_script : loaded_scripts_) { + std::ostringstream export_str; + export_str << "COMPACTED"; + + std::filesystem::path name_path(loaded_script); + name_path.replace_filename(name_path.stem()); + for (const auto& segment : name_path) { + std::string segment_str(segment); + export_str << "_" << segment_str; + } + + std::string export_name = export_str.str(); + exports_.emplace_back(export_name, script_ids.at(loaded_script)); + + new_text << "export(Resource) var " << export_name << std::endl; + + path_to_export[loaded_script] = export_name; + } + + new_text << std::endl; + + std::string search = std::string("load(\"") + file_prefix_; + std::string search_alt1 = std::string("preload(\"") + file_prefix_; + std::string search_alt2 = + std::string("ResourceLoader.load(\"") + file_prefix_; + int location; + while ((location = corpus.find(search)) != std::string_view::npos) { + int alt1 = corpus.find(search_alt1); + int alt2 = corpus.find(search_alt2); + int remove_size = search.size(); + + if (alt1 != std::string_view::npos && alt1 < location) { + location = alt1; + remove_size = search_alt1.size(); + } else if (alt2 != std::string_view::npos && alt2 < location) { + location = alt2; + remove_size = search_alt2.size(); + } + + new_text << corpus.substr(0, location); + corpus.remove_prefix(location + remove_size); + + int divider = corpus.find_first_of("\""); + // TODO: ehhh error reporting + std::string referenced_path(corpus.substr(0, divider)); + corpus.remove_prefix(divider + 2); // the quote and the close paren + new_text << path_to_export.at(referenced_path); + } + + new_text << corpus; + + text_ = new_text.str(); +} + +std::string Script::ToString() const { return text_; } -- cgit 1.4.1