MCPcopy Create free account
hub / github.com/OpenTTD/OpenTTD / IsSameScript

Function IsSameScript

src/script/script_scanner.cpp:188–223  ·  view source on GitHub ↗

* Check whether the script given in info is the same as in ci based * on the shortname and md5 sum. * @param ci The information to compare to. * @param md5sum Whether to check the MD5 checksum. * @param info The script to get the shortname and md5 sum from. * @return True iff they're the same. */

Source from the content-addressed store, hash-verified

186 * @return True iff they're the same.
187 */
188static bool IsSameScript(const ContentInfo &ci, bool md5sum, const ScriptInfo &info, Subdirectory dir)
189{
190 uint32_t id = 0;
191 auto str = std::string_view{info.GetShortName()}.substr(0, 4);
192 for (size_t j = 0; j < str.size(); j++) id |= static_cast<uint8_t>(str[j]) << (8 * j);
193
194 if (id != ci.unique_id) return false;
195 if (!md5sum) return true;
196
197 ScriptFileChecksumCreator checksum(dir);
198 const auto &tar_filename = info.GetTarFile();
199 TarList::iterator iter;
200 if (!tar_filename.empty() && (iter = _tar_list[dir].find(tar_filename)) != _tar_list[dir].end()) {
201 /* The main script is in a tar file, so find all files that
202 * are in the same tar and add them to the MD5 checksumming. */
203 for (const auto &tar : _tar_filelist[dir]) {
204 /* Not in the same tar. */
205 if (tar.second.tar_filename != iter->first) continue;
206
207 /* Check the extension. */
208 auto ext = tar.first.rfind('.');
209 if (ext == std::string_view::npos || !StrEqualsIgnoreCase(tar.first.substr(ext), ".nut")) continue;
210
211 checksum.AddFile(tar.first, 0, tar_filename);
212 }
213 } else {
214 /* There'll always be at least 1 path separator character in a script
215 * main script name as the search algorithm requires the main script to
216 * be in a subdirectory of the script directory; so <dir>/<path>/main.nut. */
217 const std::string &main_script = info.GetMainScript();
218 std::string path = main_script.substr(0, main_script.find_last_of(PATHSEPCHAR));
219 checksum.Scan(".nut", path);
220 }
221
222 return ci.md5sum == checksum.md5sum;
223}
224
225bool ScriptScanner::HasScript(const ContentInfo &ci, bool md5sum)
226{

Callers 2

HasScriptMethod · 0.85
FindMainScriptMethod · 0.85

Calls 10

StrEqualsIgnoreCaseFunction · 0.85
substrMethod · 0.80
GetTarFileMethod · 0.80
GetMainScriptMethod · 0.80
sizeMethod · 0.45
emptyMethod · 0.45
findMethod · 0.45
endMethod · 0.45
AddFileMethod · 0.45
ScanMethod · 0.45

Tested by

no test coverage detected