import subprocess
import os
import ctypes
import re
import shutil

class disable_file_system_redirection:
    _disable = ctypes.windll.kernel32.Wow64DisableWow64FsRedirection
    _revert = ctypes.windll.kernel32.Wow64RevertWow64FsRedirection
    def __enter__(self):
        self.old_value = ctypes.c_long()
        self.success = self._disable(ctypes.byref(self.old_value))
    def __exit__(self, type, value, traceback):
        if self.success:
            self._revert(self.old_value)

def call_command(command):
    with disable_file_system_redirection():
        process=subprocess.Popen(command, stdin=subprocess.PIPE,stdout=subprocess.PIPE, shell=True)
    stdout,stderr = process.communicate()
    ret = process.returncode
    return ret, stdout, stderr


def check_and_uninstall():
    with disable_file_system_redirection():
        userout = os.popen('query user').read()
        users=os.popen("wmic UserAccount get Name").read().strip().splitlines()

    fil_users=[i.strip() for i in users if i.strip()!="Administrator" and i.strip()!="DefaultAccount" and i.strip()!="Guest" and i.strip()!="WDAGUtilityAccount"]
    try:
        try:
            curusername = re.findall(r"(.*)\s+\S+\s+\S+\s+Active",userout)[0].strip()
        except:
            raise Exception("no user seems to be logged in on this system")
        curcheck = list(filter(lambda x: x.lower()==curusername.lower(),fil_users))
        if curcheck:
                sid = os.popen("wmic useraccount where name=\"%s\" get sid"%(curusername)).read().splitlines()[1].strip()
                query = os.popen("REG QUERY HKEY_USERS\\%s\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\"%(sid)).read()
                for rk in query.splitlines():
                    displayname = os.popen('REG QUERY "%s" /v DisplayName'%(rk)).read()
                    if "Roblox" in displayname:
                        RobloxPath =  r"C:\Users\%s\AppData\Local\Roblox"%(curusername)
                        print("Roblox found for the user - %s, uninstalling...."%(curusername))
                        print(call_command('reg delete "%s" /f'%(rk)))
                        try:
                            shutil.rmtree(RobloxPath)
                            shutil.rmtree(r'C:\Users\%s\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Roblox'%(curusername))
                            os.remove(r"C:\Users\%s\Desktop\Roblox Studio.lnk"%(curusername))
                            os.remove(r"C:\Users\%s\Desktop\Roblox Player.lnk"%(curusername))
                            print("Sucessfully uninstalled Roblox for the user %s"%(curusername))
                        except Exception as delete_error:
                            print(delete_error) 
    except Exception as err:
        print(err)
    else:
        fil_users.remove(curcheck[0])
    for us in fil_users:
        if os.path.exists("C:\\Users\\%s\\ntuser.dat"%(us)):
            ret,out,err = call_command('reg load "HKU\\%s" "C:\\Users\\%s\\ntuser.dat"'%(us,us))
            if out:
                try:
                    query = os.popen("REG QUERY HKEY_USERS\\%s\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\"%(us)).read()
                    for rk in query.splitlines():
                        displayname = os.popen('REG QUERY "%s" /v DisplayName'%(rk)).read()
                        if "Roblox" in displayname:
                            RobloxPath =  r"C:\Users\%s\AppData\Local\Roblox"%(us)
                            print("Roblox found for the user - %s, uninstalling...."%(us))
                            print(call_command('reg delete "%s" /f'%(rk)))
                            try:
                                shutil.rmtree(RobloxPath)
                                shutil.rmtree(r'C:\Users\%s\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Roblox'%(us))
                                os.remove(r"C:\Users\%s\Desktop\Roblox Studio.lnk"%(us))
                                os.remove(r"C:\Users\%s\Desktop\Roblox Player.lnk"%(us))
                                print("Sucessfully uninstalled Roblox for the user %s"%(us))
                            except Exception as delete_error:
                                print(delete_error)
                except Exception as err:
                    print(err)
                else:
                    unLoad = os.popen('reg unload "HKU\\%s"'%(us)).read()
            else:
                print(err)

check_and_uninstall()