diff options
Diffstat (limited to 'Source/Scripting/scriptfile.cpp')
-rw-r--r-- | Source/Scripting/scriptfile.cpp | 223 |
1 files changed, 108 insertions, 115 deletions
diff --git a/Source/Scripting/scriptfile.cpp b/Source/Scripting/scriptfile.cpp index 5ba3345b..1b963260 100644 --- a/Source/Scripting/scriptfile.cpp +++ b/Source/Scripting/scriptfile.cpp @@ -37,8 +37,8 @@ #include <errno.h> #include <algorithm> -using std::min; using std::max; +using std::min; extern std::string script_dir_path; @@ -46,18 +46,18 @@ const int _num_script_open_tries = 10; std::string ScriptFile::GetModPollutionInformation() const { std::stringstream ss; - for(const auto & dependencie : dependencies) { - if( dependencie.path.GetModsource() != CoreGameModID ) { + for (const auto& dependencie : dependencies) { + if (dependencie.path.GetModsource() != CoreGameModID) { ss << "Includes " << dependencie.path << " from mod " << ModLoading::Instance().GetModName(dependencie.path.GetModsource()) << "." << std::endl; } } return ss.str(); } -static bool AlreadyAddedIncludeFileInParentHierarchy(const ScriptFile* parent_script_file, const Path &path) { +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){ + while (parent_script_file != NULL) { + if (path == parent_script_file->file_path) { already_loaded = true; break; } @@ -66,10 +66,10 @@ static bool AlreadyAddedIncludeFileInParentHierarchy(const ScriptFile* parent_sc return already_loaded; } -bool ScriptFile::AlreadyAddedIncludeFile(const Path &path) { +bool ScriptFile::AlreadyAddedIncludeFile(const Path& path) { bool already_loaded = false; - for(auto & dependencie : dependencies){ - if(path == dependencie.path){ + for (auto& dependencie : dependencies) { + if (path == dependencie.path) { already_loaded = true; break; } @@ -77,20 +77,20 @@ bool ScriptFile::AlreadyAddedIncludeFile(const Path &path) { return already_loaded; } -unsigned GetLineNumber(std::string &script, unsigned char_pos){ +unsigned GetLineNumber(std::string& script, unsigned char_pos) { unsigned line_number = 1; - for(unsigned i=0; i<char_pos; i++){ - if(script[i] == '\n'){ + for (unsigned i = 0; i < char_pos; i++) { + if (script[i] == '\n') { line_number++; } } return line_number; } -unsigned GetNumLines(std::string &script){ +unsigned GetNumLines(std::string& script) { unsigned line_number = 1; - for(char i : script){ - if(i == '\n'){ + for (char i : script) { + if (i == '\n') { line_number++; } } @@ -100,34 +100,34 @@ unsigned GetNumLines(std::string &script){ FileRangeList::iterator GetFileRange(FileRangeList& ranges, unsigned line) { FileRangeList::iterator iter = ranges.begin(); FileRangeList::iterator good_iter = ranges.end(); - for(;iter != ranges.end(); ++iter){ + for (; iter != ranges.end(); ++iter) { IncludeFileRange& range = (*iter); - if(line >= range.start){ + if (line >= range.start) { good_iter = iter; } } return good_iter; } -FileRangeList::const_iterator GetFileRange(const FileRangeList& ranges, +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){ + for (; iter != ranges.end(); ++iter) { const IncludeFileRange& range = (*iter); - if(line >= range.start){ + 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) { +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* file = my_fopen(path, mode); + while (file == NULL && tries < max_tries) { file = my_fopen(path, mode); - if(file == NULL){ + if (file == NULL) { tries++; BusyWaitMilliseconds(50); } @@ -135,29 +135,29 @@ static FILE *robust_fopen(const char* path, const char* mode, int max_tries = 10 return file; } -static std::string ReadScriptFile(const Path &path) { +static std::string ReadScriptFile(const Path& path) { int max_tries = 10; int tries = 0; int len = 0; bool file_exists = false; - FILE *file; + FILE* file; - while(len == 0 && tries < max_tries){ + 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", + 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 + // Determine the size of the file fseek(file, 0, SEEK_END); len = ftell(file); fseek(file, 0, SEEK_SET); - if(len == 0){ + if (len == 0) { fclose(file); tries++; BusyWaitMilliseconds(50); @@ -167,27 +167,27 @@ static std::string ReadScriptFile(const Path &path) { // 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)); + 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)); + 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 ) { +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; @@ -201,7 +201,7 @@ static void UpdateFile( const ScriptFile* parent_script_file, ScriptFile &file, std::vector<Dependency>::iterator iter = file.dependencies.begin(); file.latest_modification = (*iter).modified; ++iter; - for(; iter != file.dependencies.end(); ++iter){ + for (; iter != file.dependencies.end(); ++iter) { file.latest_modification = max(file.latest_modification, (*iter).modified); } } @@ -212,7 +212,7 @@ static const ScriptFile* GetScriptFileRecursive(const ScriptFile* parent_script_ ScriptFileMap::iterator iter; iter = file_map.find(path.GetFullPathStr()); ModID modsource; - if(iter == file_map.end()){ + if (iter == file_map.end()) { // Desired script file not found, so load it std::string source = ReadScriptFile(path); if (!source.empty()) { @@ -222,8 +222,8 @@ static const ScriptFile* GetScriptFileRecursive(const ScriptFile* parent_script_ } } else { // Script file found -- make sure it hasn't changed - ScriptFile &script_file = (*iter).second; - if(ScriptFileUtil::DetectScriptFileChanged(path)){ + 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); @@ -237,23 +237,22 @@ static const ScriptFile* GetScriptFileRecursive(const ScriptFile* parent_script_ return NULL; } -static void InsertFileRange(FileRangeList& ranges, - unsigned start, - unsigned length, - const Path& path) -{ +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){ + + 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, + IncludeFileRange(parent.start + local_cut, + parent.length - local_cut, parent.offset, parent.file_path)); parent.length = local_cut; @@ -263,48 +262,48 @@ static void InsertFileRange(FileRangeList& ranges, { FileRangeList::iterator shift_iter = parent_iter; shift_iter++; - for(;shift_iter != ranges.end(); ++shift_iter){ + for (; shift_iter != ranges.end(); ++shift_iter) { IncludeFileRange& range = (*shift_iter); range.start += length; - range.offset += length-1; + range.offset += length - 1; } } // Add new range in between parent halves { FileRangeList::iterator after_parent_iter = parent_iter; - after_parent_iter++; + after_parent_iter++; ranges.insert(after_parent_iter, - IncludeFileRange(start,length,start-1,path)); + IncludeFileRange(start, length, start - 1, path)); } // Delete original parent half if length is now zero - if(parent.length == 0){ + if (parent.length == 0) { ranges.erase(parent_iter); } } -void ScriptFile::ExpandIncludePaths(){ - std::string &script = contents; +void ScriptFile::ExpandIncludePaths() { + std::string& script = contents; file_range.clear(); - file_range.push_back(IncludeFileRange(1,GetNumLines(script),0,file_path)); + 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){ + 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' ) { + 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] != '/' ) { + 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' ){ + for (int k = 0; k < i; k++) { + if (script[k] == '\n') { line_count++; } } @@ -315,49 +314,45 @@ void ScriptFile::ExpandIncludePaths(){ } } } - 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 + 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); - + 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); + 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() ) { + 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"; + + 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); - + 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); + 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,""); + script.replace(found_pos, path_end + 1 - found_pos, ""); } - found_pos = script.find("#include",found_pos); + found_pos = script.find("#include", found_pos); } } -LineFile ScriptFile::GetCorrectedLine( unsigned line ) const -{ +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; @@ -367,32 +362,32 @@ LineFile ScriptFile::GetCorrectedLine( unsigned line ) const return LineFile(line - range.offset, range.file_path); } -bool ScriptFileUtil::DetectScriptFileChanged(const Path &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()){ + if (iter == file_map.end()) { return true; } else { - ScriptFile &file = (*iter).second; + ScriptFile& file = (*iter).second; return GetLatestModification(path) > file.latest_modification; } } -int64_t ScriptFileUtil::GetLatestModification(const Path &path) { - PROFILER_ZONE(g_profiler_ctx, "GetLatestModification"); +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()){ + if (iter == file_map.end()) { DisplayError("Error", "Getting latest modification of unloaded script"); return 0; } else { - ScriptFile &file = (*iter).second; + 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){ + for (; iter != file.dependencies.end(); ++iter) { latest_modification = max(latest_modification, GetDateModifiedInt64((*iter).path)); } return latest_modification; @@ -403,27 +398,25 @@ 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; +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){ + 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 + ++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() -{ +void ScriptFileCache::Dispose() { files.clear(); } - |