| 32 | } |
| 33 | |
| 34 | void ScriptManager::refreshScripts(ScriptType type) |
| 35 | { |
| 36 | if(type == ScriptType::UNKNOWN_STYPE) return; |
| 37 | QString scriptPath(getScriptPath()); |
| 38 | scriptPath += subDirs[type]; |
| 39 | |
| 40 | QHash<QString, QSharedPointer<ScriptBase>> curScripts; |
| 41 | for(auto &s : scriptLists[type]) |
| 42 | { |
| 43 | curScripts.insert(s->getValue("path"), s); |
| 44 | } |
| 45 | QSet<QString> existPaths; |
| 46 | QDir folder(scriptPath); |
| 47 | bool changed = false; |
| 48 | for (QFileInfo fileInfo : folder.entryInfoList()) |
| 49 | { |
| 50 | if (fileInfo.isFile() && fileInfo.suffix().toLower()=="lua") |
| 51 | { |
| 52 | QString path(fileInfo.absoluteFilePath()); |
| 53 | qint64 modifyTime = fileInfo.fileTime(QFile::FileModificationTime).toSecsSinceEpoch(); |
| 54 | bool add = !curScripts.contains(path); |
| 55 | existPaths.insert(path); |
| 56 | if (!add) |
| 57 | { |
| 58 | if (curScripts[path]->getValue("time").toLongLong() < modifyTime) |
| 59 | { |
| 60 | QString id = curScripts[path]->id(); |
| 61 | scriptLists[type].removeAll(curScripts[path]); |
| 62 | curScripts.remove(path); |
| 63 | id2scriptHash.remove(id); |
| 64 | add = true; |
| 65 | changed = true; |
| 66 | } |
| 67 | } |
| 68 | if (add) |
| 69 | { |
| 70 | QSharedPointer<ScriptBase> cs; |
| 71 | if (type == ScriptType::DANMU) cs.reset(new DanmuScript); |
| 72 | else if (type == ScriptType::MATCH) cs.reset(new MatchScript); |
| 73 | else if (type == ScriptType::LIBRARY) cs.reset(new LibraryScript); |
| 74 | else if (type == ScriptType::RESOURCE) cs.reset(new ResourceScript); |
| 75 | else if (type == ScriptType::BGM_CALENDAR) cs.reset(new BgmCalendarScript); |
| 76 | if (cs) |
| 77 | { |
| 78 | ScriptState state = cs->loadScript(path); |
| 79 | if (state) |
| 80 | { |
| 81 | scriptLists[type].append(cs); |
| 82 | id2scriptHash[cs->id()] = cs; |
| 83 | changed = true; |
| 84 | } |
| 85 | else |
| 86 | { |
| 87 | Logger::logger()->log(Logger::Script, QString("[ERROR][%1]%2").arg(path, state.info)); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | } |