blob: 509d4a72669115c6ed3f3c6d22c0f72ef17f41b5 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
|
#include <filesystem>
#include <fstream>
#include <map>
#include <sstream>
#include <stdexcept>
#include <string>
#include <string_view>
#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<std::string, int>& 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<std::string, std::string> 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_; }
|