@@ -98,12 +98,11 @@ void TResourceManager::RefreshFiles() {
9898 dbEntry[" exists" ] = true ;
9999
100100 mMods .push_back (nlohmann::json {
101- { " file_name" , std::filesystem::path (File).filename () },
102- { " file_size" , std::filesystem::file_size (File) },
103- { " hash_algorithm" , " sha256" },
104- { " hash" , dbEntry[" hash" ] },
105- { " protected" , dbEntry[" protected" ] }
106- });
101+ { " file_name" , std::filesystem::path (File).filename () },
102+ { " file_size" , std::filesystem::file_size (File) },
103+ { " hash_algorithm" , " sha256" },
104+ { " hash" , dbEntry[" hash" ] },
105+ { " protected" , dbEntry[" protected" ] } });
107106
108107 beammp_debugf (" Mod '{}' loaded from cache" , File);
109108
@@ -170,8 +169,7 @@ void TResourceManager::RefreshFiles() {
170169 { " file_size" , std::filesystem::file_size (File) },
171170 { " hash_algorithm" , " sha256" },
172171 { " hash" , result },
173- { " protected" , false }
174- });
172+ { " protected" , false } });
175173
176174 modsDB[std::filesystem::path (File).filename ().string ()] = {
177175 { " lastwrite" , entry.last_write_time ().time_since_epoch ().count () },
@@ -186,7 +184,7 @@ void TResourceManager::RefreshFiles() {
186184 }
187185 }
188186
189- for (auto it = modsDB.begin (); it != modsDB.end (); ) {
187+ for (auto it = modsDB.begin (); it != modsDB.end ();) {
190188 if (!it.value ().contains (" exists" )) {
191189 it = modsDB.erase (it);
192190 } else {
@@ -205,3 +203,39 @@ void TResourceManager::RefreshFiles() {
205203 beammp_error (" Failed to update mod DB: " + std::string (e.what ()));
206204 }
207205}
206+
207+ void TResourceManager::SetProtected (const std::string& ModName, bool Protected) {
208+ std::unique_lock Lock (mModsMutex );
209+
210+ for (auto & mod : mMods ) {
211+ if (mod[" file_name" ].get <std::string>() == ModName) {
212+ mod[" protected" ] = Protected;
213+ break ;
214+ }
215+ }
216+
217+ auto modsDBPath = Application::Settings.getAsString (Settings::Key::General_ResourceFolder) + " /Client/mods.json" ;
218+
219+ if (std::filesystem::exists (modsDBPath)) {
220+ try {
221+ nlohmann::json modsDB;
222+
223+ std::fstream stream (modsDBPath);
224+
225+ stream >> modsDB;
226+
227+ if (modsDB.contains (ModName)) {
228+ modsDB[ModName][" protected" ] = Protected;
229+ }
230+
231+ stream.clear ();
232+ stream.seekp (0 , std::ios::beg);
233+
234+ stream << modsDB.dump (4 );
235+
236+ stream.close ();
237+ } catch (const std::exception& e) {
238+ beammp_errorf (" Failed to update mods.json: {}" , e.what ());
239+ }
240+ }
241+ }
0 commit comments