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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
|
//-----------------------------------------------------------------------------
// Name: scriptfile.cpp
// Developer: Wolfire Games LLC
// Description:
// License: Read below
//-----------------------------------------------------------------------------
//
// Copyright 2022 Wolfire Games LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
//-----------------------------------------------------------------------------
#include "scriptfile.h"
#include <Internal/error.h>
#include <Internal/filesystem.h>
#include <Internal/stopwatch.h>
#include <Internal/returnpathutil.h>
#include <Internal/checksum.h>
#include <Internal/config.h>
#include <Internal/profiler.h>
#include <Scripting/scriptlogging.h>
#include <Compat/fileio.h>
#include <Main/engine.h>
#include <errno.h>
#include <algorithm>
using std::min;
using std::max;
extern std::string script_dir_path;
const int _num_script_open_tries = 10;
std::string ScriptFile::GetModPollutionInformation() const {
std::stringstream ss;
for( unsigned i = 0; i < dependencies.size(); i++ ) {
if( dependencies[i].path.GetModsource() != CoreGameModID ) {
ss << "Includes " << dependencies[i].path << " from mod " << ModLoading::Instance().GetModName(dependencies[i].path.GetModsource()) << "." << std::endl;
}
}
return ss.str();
}
static bool AlreadyAddedIncludeFileInParentHierarchy(const ScriptFile* parent_script_file, const Path &path) {
bool already_loaded = false;
while(parent_script_file != NULL){
if(path == parent_script_file->file_path){
already_loaded = true;
break;
}
parent_script_file = parent_script_file->parent;
}
return already_loaded;
}
bool ScriptFile::AlreadyAddedIncludeFile(const Path &path) {
bool already_loaded = false;
for(unsigned i=0; i<dependencies.size(); ++i){
if(path == dependencies[i].path){
already_loaded = true;
break;
}
}
return already_loaded;
}
unsigned GetLineNumber(std::string &script, unsigned char_pos){
unsigned line_number = 1;
for(unsigned i=0; i<char_pos; i++){
if(script[i] == '\n'){
line_number++;
}
}
return line_number;
}
unsigned GetNumLines(std::string &script){
unsigned line_number = 1;
for(unsigned i=0; i<script.size(); i++){
if(script[i] == '\n'){
line_number++;
}
}
return line_number;
}
FileRangeList::iterator GetFileRange(FileRangeList& ranges, unsigned line) {
FileRangeList::iterator iter = ranges.begin();
FileRangeList::iterator good_iter = ranges.end();
for(;iter != ranges.end(); ++iter){
IncludeFileRange& range = (*iter);
if(line >= range.start){
good_iter = iter;
}
}
return good_iter;
}
FileRangeList::const_iterator GetFileRange(const FileRangeList& ranges,
unsigned line) {
FileRangeList::const_iterator iter = ranges.begin();
FileRangeList::const_iterator good_iter = ranges.end();
for(;iter != ranges.end(); ++iter){
const IncludeFileRange& range = (*iter);
if(line >= range.start){
good_iter = iter;
}
}
return good_iter == ranges.end() ? ranges.begin() : good_iter;
}
static FILE *robust_fopen(const char* path, const char* mode, int max_tries = 10) {
int tries = 0;
FILE *file = my_fopen(path, mode);
while(file == NULL && tries < max_tries){
file = my_fopen(path, mode);
if(file == NULL){
tries++;
BusyWaitMilliseconds(50);
}
}
return file;
}
static std::string ReadScriptFile(const Path &path) {
int max_tries = 10;
int tries = 0;
int len = 0;
bool file_exists = false;
FILE *file;
while(len == 0 && tries < max_tries){
file = robust_fopen(path.GetFullPath(), "rb");
if( file == NULL ) {
FatalError("Error","Failed to open the script file: %s\n%s",
path.GetFullPath(), strerror(errno));
}
file_exists = true;
// Determine the size of the file
fseek(file, 0, SEEK_END);
len = ftell(file);
fseek(file, 0, SEEK_SET);
if(len == 0){
fclose(file);
tries++;
BusyWaitMilliseconds(50);
}
}
// Read the entire file
std::string script;
script.resize(len);
int c = fread(&script[0], len, 1, file);
if(file_exists) {
if( len == 0 ) {
FatalError("Error","Script file was present, but empty:\n%s\n%s",
path.GetFullPath(), strerror(errno));
} else if( c == 0 ) {
FatalError("Error","Failed to load script file:\n%s\n%s",
path.GetFullPath(), strerror(errno));
}
} else {
FatalError("Error","Failed to load script file:\n%s\n%s",
path.GetFullPath(), strerror(errno));
}
fclose(file);
return script;
}
static void UpdateFile( const ScriptFile* parent_script_file, ScriptFile &file, const std::string &source, const Path& path ) {
file.parent = parent_script_file;
file.unexpanded_contents = source;
file.contents = source;
file.file_path = path;
file.dependencies.clear();
file.dependencies.resize(1);
file.dependencies[0].path = path;
file.dependencies[0].modified = GetDateModifiedInt64(path);
file.ExpandIncludePaths();
file.hash = djb2_string_hash(file.contents.c_str());
std::vector<Dependency>::iterator iter = file.dependencies.begin();
file.latest_modification = (*iter).modified;
++iter;
for(; iter != file.dependencies.end(); ++iter){
file.latest_modification = max(file.latest_modification, (*iter).modified);
}
}
static const ScriptFile* GetScriptFileRecursive(const ScriptFile* parent_script_file, const Path& path) {
// Look in script file cache for desired script file
ScriptFileMap& file_map = ScriptFileCache::Instance()->files;
ScriptFileMap::iterator iter;
iter = file_map.find(path.GetFullPathStr());
ModID modsource;
if(iter == file_map.end()){
// Desired script file not found, so load it
std::string source = ReadScriptFile(path);
if (!source.empty()) {
ScriptFile& file = file_map[path.GetFullPathStr()];
UpdateFile(parent_script_file, file, source, path);
return &file;
}
} else {
// Script file found -- make sure it hasn't changed
ScriptFile &script_file = (*iter).second;
if(ScriptFileUtil::DetectScriptFileChanged(path)){
std::string source = ReadScriptFile(path);
if (!source.empty()) {
UpdateFile(parent_script_file, script_file, source, path);
return &script_file;
}
} else {
return &script_file;
}
}
return NULL;
}
static void InsertFileRange(FileRangeList& ranges,
unsigned start,
unsigned length,
const Path& path)
{
FileRangeList::iterator parent_iter = GetFileRange(ranges, start);
// Split parent in half
IncludeFileRange& parent = (*parent_iter);
unsigned local_cut = start - parent.start;
if(parent.length-local_cut>0){
FileRangeList::iterator after_parent_iter = parent_iter;
after_parent_iter++;
ranges.insert(after_parent_iter,
IncludeFileRange(parent.start+local_cut,
parent.length-local_cut,
parent.offset,
parent.file_path));
parent.length = local_cut;
}
// Shift all elements to the right of the cut
{
FileRangeList::iterator shift_iter = parent_iter;
shift_iter++;
for(;shift_iter != ranges.end(); ++shift_iter){
IncludeFileRange& range = (*shift_iter);
range.start += length;
range.offset += length-1;
}
}
// Add new range in between parent halves
{
FileRangeList::iterator after_parent_iter = parent_iter;
after_parent_iter++;
ranges.insert(after_parent_iter,
IncludeFileRange(start,length,start-1,path));
}
// Delete original parent half if length is now zero
if(parent.length == 0){
ranges.erase(parent_iter);
}
}
void ScriptFile::ExpandIncludePaths(){
std::string &script = contents;
file_range.clear();
file_range.push_back(IncludeFileRange(1,GetNumLines(script),0,file_path));
// Get the position of the first letter of the first "#include"
// directive in the script
size_t found_pos = script.find("#include");
while (found_pos != std::string::npos){
bool disabled_include = false;
for( int i = found_pos-1; i >= 0; i-- ) {
if(script[i] != ' ' && script[i] != '\t') {
if( script[i] == '\n' || script[i] == '\r' ) {
break;
} else {
if( script[i] != '/' ) {
LOGE << "Unexpected character \'" << script[i] << "\' before #include in " << file_path << std::endl;
int line_count = 0;
for( int k = 0; k < i; k++ ){
if( script[k] == '\n' ){
line_count++;
}
}
LOGE << " line " << line_count << std::endl;
} else {
disabled_include = true;
}
}
}
}
size_t path_start = found_pos + 10; // Get pos of first letter of path
size_t path_end = script.find('\"',path_start); // Find end of path
std::string path = std::string(script_dir_path) +
script.substr(path_start, path_end-path_start);
// If include file has not already been added, then replace #include
// directive with contents of included file. Otherwise, just remove
// the directive.
Path include_path = FindFilePath(path, kDataPaths | kModPaths);
if(disabled_include == false && !AlreadyAddedIncludeFileInParentHierarchy(this, include_path) && !AlreadyAddedIncludeFile(include_path)){
dependencies.resize(dependencies.size()+1);
dependencies.back().path = include_path;
if( include_path.isValid() ) {
dependencies.back().modified = GetDateModifiedInt64(include_path.GetFullPath());
std::string new_script = GetScriptFileRecursive(this, include_path)->unexpanded_contents;
if( config["dump_include_scripts"].toBool() )
{
new_script =
+ "/*include - START - " + path + " */\n"
+ new_script
+ "/*include - END - " + path + " */\n";
}
script.replace(found_pos,path_end+1-found_pos,new_script);
unsigned line_number = GetLineNumber(script, found_pos);
unsigned include_length = GetNumLines(new_script);
InsertFileRange(file_range,line_number,include_length,include_path);
} else {
LOGE << "Could not resolve script include: " << path << std::endl;
}
} else {
script.replace(found_pos,path_end+1-found_pos,"");
}
found_pos = script.find("#include",found_pos);
}
}
LineFile ScriptFile::GetCorrectedLine( unsigned line ) const
{
LOGD << "Getting corrected line A." << std::endl;
FileRangeList::const_iterator iter = GetFileRange(file_range, line);
LOGD << "Getting corrected line B." << std::endl;
const IncludeFileRange& range = (*iter);
LOGD << "Getting corrected line C." << std::endl;
return LineFile(line - range.offset, range.file_path);
}
bool ScriptFileUtil::DetectScriptFileChanged(const Path &path) {
ScriptFileMap& file_map = ScriptFileCache::Instance()->files;
ScriptFileMap::iterator iter;
iter = file_map.find(path.GetFullPathStr());
if(iter == file_map.end()){
return true;
} else {
ScriptFile &file = (*iter).second;
return GetLatestModification(path) > file.latest_modification;
}
}
int64_t ScriptFileUtil::GetLatestModification(const Path &path) {
PROFILER_ZONE(g_profiler_ctx, "GetLatestModification");
ScriptFileMap& file_map = ScriptFileCache::Instance()->files;
ScriptFileMap::iterator iter;
iter = file_map.find(path.GetFullPathStr());
if(iter == file_map.end()){
DisplayError("Error", "Getting latest modification of unloaded script");
return 0;
} else {
ScriptFile &file = (*iter).second;
std::vector<Dependency>::iterator iter = file.dependencies.begin();
int64_t latest_modification = GetDateModifiedInt64((*iter).path);
++iter;
for(; iter != file.dependencies.end(); ++iter){
latest_modification = max(latest_modification, GetDateModifiedInt64((*iter).path));
}
return latest_modification;
}
}
const ScriptFile* ScriptFileUtil::GetScriptFile(const Path& path) {
return GetScriptFileRecursive(NULL, path);
}
void ScriptFileUtil::ReturnPaths(const Path &script_path, PathSet &path_set) {
path_set.insert("script "+script_path.GetOriginalPathStr());
const ScriptFile &script_file = *ScriptFileUtil::GetScriptFile(script_path);
const std::string &script = script_file.contents;
std::string load_path;
std::string type;
std::string::size_type index = 0;
while(1){
index = script.find("\"Data/", index); // Set index to first quote of "Data/" path
if(index == std::string::npos){
break;
}
++index; // Set index to first character of Data/ path
std::string::size_type end_quote = script.find('\"', index); // Find last quote
load_path = script.substr(index, end_quote - index);
ReturnPathUtil::ReturnPathsFromPath(load_path, path_set);
}
}
void ScriptFileCache::Dispose()
{
files.clear();
}
|