From b9fe58a482a5abc62fd81f7e4562a75dce2c5ac1 Mon Sep 17 00:00:00 2001
From: Evgenij Titarenko <frundle@teasanctuary.ru>
Date: Sun, 23 Jul 2023 23:31:28 +0300
Subject: [PATCH] =?UTF-8?q?=D0=9F=D1=80=D0=BE=D0=B4=D0=BE=D0=BB=D0=B6?=
 =?UTF-8?q?=D0=B0=D1=8E=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D1=83=20=D0=BD?=
 =?UTF-8?q?=D0=B0=D0=B4=20=D0=91=D0=94.=20=D0=9D=D0=B0=D1=87=D0=B0=D0=BB?=
 =?UTF-8?q?=D1=8C=D0=BD=D0=B0=D1=8F=20=D1=80=D0=B5=D0=B0=D0=BB=D0=B8=D0=B7?=
 =?UTF-8?q?=D0=B0=D1=86=D0=B8=D1=8F=20=D1=83=D0=B4=D0=B0=D0=BB=D0=B5=D0=BD?=
 =?UTF-8?q?=D0=B8=D1=8F=20=D0=BF=D0=B0=D0=BA=D0=B5=D1=82=D0=BE=D0=B2.?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 mc-get.py  | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++--
 mcfs.py    |  8 ++++++++
 mcgetdb.py | 12 ++++++++++--
 3 files changed, 68 insertions(+), 4 deletions(-)

diff --git a/mc-get.py b/mc-get.py
index dee62b8..423e18f 100755
--- a/mc-get.py
+++ b/mc-get.py
@@ -49,13 +49,13 @@ def install(projects: list, mc_ver, loader):
     projects_ids = []
     already_installed = []
 
-    def get_mod_info_from_db(slug):
+    def get_project_info_from_db(slug):
         _db = mcgetdb.McGetDB(mcfs.mc_dir)
         return _db.select_mod(slug) + _db.select_shader(slug) + _db.select_resourcepack(slug) + _db.select_modpack(slug)
 
     def check_project(slug):
         if __name__ == "__main__":
-            mod_info = get_mod_info_from_db(slug)
+            mod_info = get_project_info_from_db(slug)
             if mod_info:
                 already_installed.append(mod_info[0])
                 return
@@ -204,6 +204,50 @@ def clean():
     print("Nothing to clear.")
 
 
+def remove(projects):  # TODO: 1. Проверка зависимостей? 2. Модпаки
+    if __name__ == "__main__":
+        db = mcgetdb.McGetDB(mcfs.mc_dir)
+
+        mods_to_remove = []
+        resources_to_remove = []
+        shaders_to_remove = []
+        modpacks_to_remove = []
+
+        for project in projects:
+            for res in db.select_mod(project):
+                mods_to_remove.append(res)
+            for res in db.select_resourcepack(project):
+                resources_to_remove.append(res)
+            for res in db.select_shader(project):
+                shaders_to_remove.append(res)
+            for res in db.select_modpack(project):
+                modpacks_to_remove.append(res)
+
+        for slug, title, filename, version, _ in mods_to_remove:
+            local_path = os.path.join("mods", filename)
+            mcfs.remove(local_path)
+            db.remove_mod(slug)
+            print(f"Mod {title} v.{version} was removed.")
+        for _, title, filename, version, _ in resources_to_remove:
+            local_path = os.path.join("resourcepacks", filename)
+            mcfs.remove(local_path)
+            db.remove_resourcepack(slug)
+            print(f"Resource pack {title} v.{version} was removed.")
+        for _, title, filename, version, _ in shaders_to_remove:
+            local_path = os.path.join("shaderpacks", filename)
+            mcfs.remove(local_path)
+            db.remove_shader(slug)
+            print(f"Shader pack {title} v.{version} was removed.")
+        for _, title, filename, version, _ in modpacks_to_remove:
+            raise NotImplementedError
+            # local_path = os.path.join("modpacks", filename)
+            # mcfs.remove(local_path)
+            # db.remove_modpack(slug)
+            # print(f"Mod pack {title} v.{version} was removed.")
+    else:
+        raise NotImplementedError("Not available in module mode")
+
+
 if __name__ == "__main__":
 
     def exit():
@@ -273,6 +317,10 @@ if __name__ == "__main__":
     parser_validate = subparsers.add_parser("validate", help="Validate the installation")
 
     parser_clean = subparsers.add_parser("clean", help="Clean the cache of this program")
+
+    parser_remove = subparsers.add_parser("remove", help="Remove installed packages")
+    parser_remove.add_argument("projects", nargs="+")
+
     kwargs = vars(parser.parse_args())  # Получаем все поля получившегося Namespace и пихаем в словарь
     if not properties:
         exit()
diff --git a/mcfs.py b/mcfs.py
index 7eb3833..ca6858a 100644
--- a/mcfs.py
+++ b/mcfs.py
@@ -129,3 +129,11 @@ def check_file_hash(path, ref_hash):
             data = f.read(buffer_size)
             hash.update(data)
     return hash.hexdigest() == ref_hash
+
+
+def remove(mc_dir_rel_path):
+    path = os.path.join(mc_dir, mc_dir_rel_path)
+    if is_path_exist(path):
+        os.remove(path)
+    else:
+        raise FileNotFoundError
diff --git a/mcgetdb.py b/mcgetdb.py
index 7e195dc..8b6caa2 100644
--- a/mcgetdb.py
+++ b/mcgetdb.py
@@ -63,6 +63,11 @@ class McGetDB:
         self.select_shader = self.__db_select_by_col("shaderpacks", "slug")
         self.select_modpack = self.__db_select_by_col("modpacks", "slug")
 
+        self.remove_mod = self.__db_remove_by_col("mods", "slug")
+        self.remove_resourcepack = self.__db_remove_by_col("resourcepacks", "slug")
+        self.remove_shader = self.__db_remove_by_col("shaderpacks", "slug")
+        self.remove_modpack = self.__db_remove_by_col("modpacks", "slug")
+
     def get_properties(self):
         properties = None
         with self.db as db:
@@ -100,8 +105,11 @@ class McGetDB:
             return res.fetchall()
         return selection_func
 
-    def remove_mod(self):
-        pass
+    def __db_remove_by_col(self, table, col):
+        def removal_func(col_value):
+            with self.db as db:
+                db.execute(f'DELETE FROM {table} WHERE {col} = "{col_value}"')
+        return removal_func
 
     def update_mod(self):
         pass