@@ -181,6 +181,16 @@ cpp::result<bool, std::string> EngineService::UninstallEngineVariant(
181181 const std::string& engine, const std::optional<std::string> version,
182182 const std::optional<std::string> variant) {
183183 auto ne = NormalizeEngine (engine);
184+ if (IsEngineLoaded (ne)) {
185+ CTL_INF (" Engine " << ne << " is already loaded, unloading it" );
186+ auto unload_res = UnloadEngine (ne);
187+ if (unload_res.has_error ()) {
188+ CTL_INF (" Failed to unload engine: " << unload_res.error ());
189+ return cpp::fail (unload_res.error ());
190+ } else {
191+ CTL_INF (" Engine " << ne << " unloaded successfully" );
192+ }
193+ }
184194
185195 std::optional<std::filesystem::path> path_to_remove = std::nullopt ;
186196 if (version == std::nullopt && variant == std::nullopt ) {
@@ -264,6 +274,16 @@ cpp::result<void, std::string> EngineService::DownloadEngineV2(
264274 if (selected_variant == std::nullopt ) {
265275 return cpp::fail (" Failed to find a suitable variant for " + engine);
266276 }
277+ if (IsEngineLoaded (engine)) {
278+ CTL_INF (" Engine " << engine << " is already loaded, unloading it" );
279+ auto unload_res = UnloadEngine (engine);
280+ if (unload_res.has_error ()) {
281+ CTL_INF (" Failed to unload engine: " << unload_res.error ());
282+ return cpp::fail (unload_res.error ());
283+ } else {
284+ CTL_INF (" Engine " << engine << " unloaded successfully" );
285+ }
286+ }
267287 auto normalize_version = " v" + selected_variant->version ;
268288
269289 auto variant_folder_name = engine_matcher_utils::GetVariantFromNameAndVersion (
@@ -276,7 +296,7 @@ cpp::result<void, std::string> EngineService::DownloadEngineV2(
276296 auto variant_path = variant_folder_path / selected_variant->name ;
277297 std::filesystem::create_directories (variant_folder_path);
278298 CLI_LOG (" variant_folder_path: " + variant_folder_path.string ());
279- auto on_finished = [this , engine, selected_variant,
299+ auto on_finished = [this , engine, selected_variant, variant_folder_path,
280300 normalize_version](const DownloadTask& finishedTask) {
281301 // try to unzip the downloaded file
282302 CLI_LOG (" Engine zip path: " << finishedTask.items [0 ].localPath .string ());
@@ -299,6 +319,22 @@ cpp::result<void, std::string> EngineService::DownloadEngineV2(
299319 CTL_INF (" Set default engine variant: " << res.value ().variant );
300320 }
301321
322+ // remove other engines
323+ auto engine_directories = file_manager_utils::GetEnginesContainerPath () /
324+ engine / selected_variant->name ;
325+
326+ for (const auto & entry : std::filesystem::directory_iterator (
327+ variant_folder_path.parent_path ())) {
328+ if (entry.is_directory () &&
329+ entry.path ().filename () != normalize_version) {
330+ try {
331+ std::filesystem::remove_all (entry.path ());
332+ } catch (const std::exception& e) {
333+ CTL_WRN (" Could not delete directory: " << e.what ());
334+ }
335+ }
336+ }
337+
302338 // remove the downloaded file
303339 try {
304340 std::filesystem::remove (finishedTask.items [0 ].localPath );
@@ -364,6 +400,16 @@ cpp::result<bool, std::string> EngineService::DownloadEngine(
364400 CTL_INF (" Creating " << engine_folder_path.string ());
365401 std::filesystem::create_directories (engine_folder_path);
366402 }
403+ if (IsEngineLoaded (engine)) {
404+ CTL_INF (" Engine " << engine << " is already loaded, unloading it" );
405+ auto unload_res = UnloadEngine (engine);
406+ if (unload_res.has_error ()) {
407+ CTL_INF (" Failed to unload engine: " << unload_res.error ());
408+ return cpp::fail (unload_res.error ());
409+ } else {
410+ CTL_INF (" Engine " << engine << " unloaded successfully" );
411+ }
412+ }
367413 CTL_INF (" Engine folder path: " << engine_folder_path.string () << " \n " );
368414 auto local_path = engine_folder_path / asset.name ;
369415 auto downloadTask{
@@ -918,6 +964,17 @@ cpp::result<EngineUpdateResult, std::string> EngineService::UpdateEngine(
918964 CTL_INF (" Default variant: " << default_variant->variant
919965 << " , version: " + default_variant->version );
920966
967+ if (IsEngineLoaded (ne)) {
968+ CTL_INF (" Engine " << ne << " is already loaded, unloading it" );
969+ auto unload_res = UnloadEngine (ne);
970+ if (unload_res.has_error ()) {
971+ CTL_INF (" Failed to unload engine: " << unload_res.error ());
972+ return cpp::fail (unload_res.error ());
973+ } else {
974+ CTL_INF (" Engine " << ne << " unloaded successfully" );
975+ }
976+ }
977+
921978 auto latest_version = GetLatestEngineVersion (ne);
922979 if (latest_version.has_error ()) {
923980 // if can't get latest version, stop
0 commit comments