//mpPreference 安全设置
import fsys.path;

namespace sys.mpPreference;

getExclusionPaths = function(paths){
    if(!_WIN10_LATER) return;
    
    import dotNet.ps;
	var ret,err = dotNet.ps("Get-MpPreference  | Select-Object -ExpandProperty ExclusionPath")

	if(ret!==null) {
		var exclusionPaths = ..string.splitEx(..string.trim(ret));
		if(!exclusionPaths){
			return null,err;
		}
			
		if(#exclusionPaths==1 && !..io.exist(exclusionPaths[1])){
			return null,exclusionPaths[1]
		}
		
		for(k,path in paths){
			path = ..fsys.path.removeBackslash(path);  
			
			if(!..table.find(exclusionPaths,lambda(v,k) ..fsys.path.cmp(v,path)===0 || ..fsys.path.ischild(v,path) ) ){
				..table.push(exclusionPaths,path); 
			} 
		}
		
		return exclusionPaths;	
	}
}

isExclusionPath = function(path){
	path = ..fsys.path.removeBackslash(path);
	
	var exclusionPaths = getExclusionPaths() : {};
	if(!..table.find(exclusionPaths,path) ){
		..table.push(exclusionPaths,path); 
	}
	
	for(k,e in exclusionPaths){
		if(..fsys.path.cmp(e,path)){
			return true;
		}
		
		if(..fsys.path.ischild(e,path) ){
			return true;
		}
	}
}

addExclusionPath = function(path){
    if(!_WIN10_LATER) return;
    
    import dotNet.ps;
	path = ..fsys.path.removeBackslash(path); 
	
	var exclusionPaths = getExclusionPaths() : {};
	if(!..table.find(exclusionPaths,path) ){
		..table.push(exclusionPaths,path); 
	}
	
	return dotNet.ps.command("Set-MpPreference",{ExclusionPath=#exclusionPaths?exclusionPaths:""})
}

setExclusionPaths = function(exclusionPaths){
    if(!_WIN10_LATER) return;
    
    import dotNet.ps;
    for(k,v in exclusionPaths){
    	exclusionPaths[k] = ..fsys.path.removeBackslash(v); 
    }
    
    exclusionPaths = ..table.unique(exclusionPaths);
	return dotNet.ps.command("Set-MpPreference",{ExclusionPath=#exclusionPaths?exclusionPaths:""})
}

/**intellisense()
sys.mpPreference = Windows Defender 设置
sys.mpPreference.getExclusionPaths() = 获取例外目录列表，返回路径数组,\n可选在参数中指定要添加到返回列表中的路径,\n如果参数中传入的目录或该目录的父目录已经在排除列表中，则自动排除
sys.mpPreference.addExclusionPaths(__) = 添加一个路径到例外目录列表
sys.mpPreference.setExclusionPaths(__) = 修改例外目录列表，参数传入路径数组,\n此函数自动除重
sys.mpPreference.isExclusionPath(__) = 参数指定的路径是否属于例外目录
end intellisense**/