@@ -944,8 +944,49 @@ async def install_plugin_from_file(self, zip_file_path: str):
944944 dir_name = os .path .basename (zip_file_path ).replace (".zip" , "" )
945945 dir_name = dir_name .removesuffix ("-master" ).removesuffix ("-main" ).lower ()
946946 desti_dir = os .path .join (self .plugin_store_path , dir_name )
947+
948+ # 第一步:检查是否已安装同目录名的插件,先终止旧插件
949+ existing_plugin = None
950+ for star in self .context .get_all_stars ():
951+ if star .root_dir_name == dir_name :
952+ existing_plugin = star
953+ break
954+
955+ if existing_plugin :
956+ logger .info (f"检测到插件 { existing_plugin .name } 已安装,正在终止旧插件..." )
957+ try :
958+ await self ._terminate_plugin (existing_plugin )
959+ except Exception :
960+ logger .warning (traceback .format_exc ())
961+ if existing_plugin .name and existing_plugin .module_path :
962+ await self ._unbind_plugin (
963+ existing_plugin .name , existing_plugin .module_path
964+ )
965+
947966 self .updator .unzip_file (zip_file_path , desti_dir )
948967
968+ # 第二步:解压后,读取新插件的 metadata.yaml,检查是否存在同名但不同目录的插件
969+ try :
970+ new_metadata = self ._load_plugin_metadata (desti_dir )
971+ if new_metadata and new_metadata .name :
972+ for star in self .context .get_all_stars ():
973+ if (
974+ star .name == new_metadata .name
975+ and star .root_dir_name != dir_name
976+ ):
977+ logger .warning (
978+ f"检测到同名插件 { star .name } 存在于不同目录 { star .root_dir_name } ,正在终止..."
979+ )
980+ try :
981+ await self ._terminate_plugin (star )
982+ except Exception :
983+ logger .warning (traceback .format_exc ())
984+ if star .name and star .module_path :
985+ await self ._unbind_plugin (star .name , star .module_path )
986+ break # 只处理第一个匹配的
987+ except Exception as e :
988+ logger .debug (f"读取新插件 metadata.yaml 失败,跳过同名检查: { e !s} " )
989+
949990 # remove the zip
950991 try :
951992 os .remove (zip_file_path )
0 commit comments