diff options
Diffstat (limited to 'script.cpp')
-rw-r--r-- | script.cpp | 106 |
1 files changed, 106 insertions, 0 deletions
diff --git a/script.cpp b/script.cpp new file mode 100644 index 0000000..509d4a7 --- /dev/null +++ b/script.cpp | |||
@@ -0,0 +1,106 @@ | |||
1 | #include <filesystem> | ||
2 | #include <fstream> | ||
3 | #include <map> | ||
4 | #include <sstream> | ||
5 | #include <stdexcept> | ||
6 | #include <string> | ||
7 | #include <string_view> | ||
8 | |||
9 | #include "script.h" | ||
10 | |||
11 | Script::Script(const std::filesystem::path& path, std::string_view file_prefix) | ||
12 | : file_prefix_(file_prefix) { | ||
13 | { | ||
14 | std::ifstream script_file(path); | ||
15 | std::string line; | ||
16 | std::ostringstream text_stream; | ||
17 | while (std::getline(script_file, line)) { | ||
18 | text_stream << line << std::endl; | ||
19 | } | ||
20 | text_ = text_stream.str(); | ||
21 | } | ||
22 | |||
23 | std::string search = std::string("load(\"") + std::string(file_prefix); | ||
24 | std::string_view corpus = text_; | ||
25 | int location; | ||
26 | while ((location = corpus.find(search)) != std::string_view::npos) { | ||
27 | corpus.remove_prefix(location + search.size()); | ||
28 | int divider = corpus.find_first_of("\""); | ||
29 | if (divider == std::string_view::npos) { | ||
30 | throw std::invalid_argument( | ||
31 | "I don't know how this is happening but I guess I gotta report it."); | ||
32 | } | ||
33 | std::string script_path(corpus.substr(0, divider)); | ||
34 | corpus.remove_prefix(divider + 1); | ||
35 | loaded_scripts_.push_back(script_path); | ||
36 | } | ||
37 | } | ||
38 | |||
39 | void Script::ReplaceScriptReferences( | ||
40 | const std::map<std::string, int>& script_ids) { | ||
41 | std::ostringstream new_text; | ||
42 | std::string_view corpus = text_; | ||
43 | int divider = corpus.find_first_of("\n"); | ||
44 | if (divider == std::string_view::npos) { | ||
45 | throw std::invalid_argument("Script is one line??"); | ||
46 | } | ||
47 | new_text << corpus.substr(0, divider + 1) << std::endl; | ||
48 | ; | ||
49 | corpus.remove_prefix(divider + 1); | ||
50 | |||
51 | std::map<std::string, std::string> path_to_export; | ||
52 | for (const std::string& loaded_script : loaded_scripts_) { | ||
53 | std::ostringstream export_str; | ||
54 | export_str << "COMPACTED"; | ||
55 | |||
56 | std::filesystem::path name_path(loaded_script); | ||
57 | name_path.replace_filename(name_path.stem()); | ||
58 | for (const auto& segment : name_path) { | ||
59 | std::string segment_str(segment); | ||
60 | export_str << "_" << segment_str; | ||
61 | } | ||
62 | |||
63 | std::string export_name = export_str.str(); | ||
64 | exports_.emplace_back(export_name, script_ids.at(loaded_script)); | ||
65 | |||
66 | new_text << "export(Resource) var " << export_name << std::endl; | ||
67 | |||
68 | path_to_export[loaded_script] = export_name; | ||
69 | } | ||
70 | |||
71 | new_text << std::endl; | ||
72 | |||
73 | std::string search = std::string("load(\"") + file_prefix_; | ||
74 | std::string search_alt1 = std::string("preload(\"") + file_prefix_; | ||
75 | std::string search_alt2 = | ||
76 | std::string("ResourceLoader.load(\"") + file_prefix_; | ||
77 | int location; | ||
78 | while ((location = corpus.find(search)) != std::string_view::npos) { | ||
79 | int alt1 = corpus.find(search_alt1); | ||
80 | int alt2 = corpus.find(search_alt2); | ||
81 | int remove_size = search.size(); | ||
82 | |||
83 | if (alt1 != std::string_view::npos && alt1 < location) { | ||
84 | location = alt1; | ||
85 | remove_size = search_alt1.size(); | ||
86 | } else if (alt2 != std::string_view::npos && alt2 < location) { | ||
87 | location = alt2; | ||
88 | remove_size = search_alt2.size(); | ||
89 | } | ||
90 | |||
91 | new_text << corpus.substr(0, location); | ||
92 | corpus.remove_prefix(location + remove_size); | ||
93 | |||
94 | int divider = corpus.find_first_of("\""); | ||
95 | // TODO: ehhh error reporting | ||
96 | std::string referenced_path(corpus.substr(0, divider)); | ||
97 | corpus.remove_prefix(divider + 2); // the quote and the close paren | ||
98 | new_text << path_to_export.at(referenced_path); | ||
99 | } | ||
100 | |||
101 | new_text << corpus; | ||
102 | |||
103 | text_ = new_text.str(); | ||
104 | } | ||
105 | |||
106 | std::string Script::ToString() const { return text_; } | ||