Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
1. add subkeyname can get last segement name
  • Loading branch information
oven425 committed Dec 20, 2021
1 parent 66e13cc commit 560e7c1
Show file tree
Hide file tree
Showing 10 changed files with 331 additions and 264 deletions.
54 changes: 30 additions & 24 deletions ConsoleApp1/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,6 @@ static void Main(string[] args)
//Regex regex = new Regex(@"[\\]([A-Za-z0-9]+)$");
//Regex regex = new Regex(@"^(?<base>\w+)[\\]");

var biosquery = RegistryHive.LocalMachine.OpenView64(@"HARDWARE\DESCRIPTION\System\BIOS", false);
var BiosMajorRelease = biosquery.GetValue<uint>("BiosMajorRelease");
var iiu = Convert.ToUInt32(BiosMajorRelease);
var queryreg = RegistryHive.LocalMachine.OpenView64(@"SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\1A", false);


Expand All @@ -142,28 +139,30 @@ static void Main(string[] args)



var rr = queryable.GroupBy(x => x.GetValue<string>("DisplayName"), (x, y) => new { x, y = y.Select(xuu => new InstalledApp() { }) });

//var rr = queryable.GroupBy(x => x.GetValue<string>("DisplayName"), (x, y) => new { x, y = y.Select(xuu => new InstalledApp() { }) });
var rr = queryable.Select(x => x.Name.GetLastSegement());
var ttype = rr.GetType();
MethodCallExpression methodcall = rr.Expression as MethodCallExpression;
var unary = methodcall.Arguments[2] as UnaryExpression;
var lambda = unary.Operand as LambdaExpression;


var newexpr = lambda.Body as NewExpression;
methodcall = newexpr.Arguments[1] as MethodCallExpression;
var ggw = methodcall.Method.GetGenericArguments();
lambda = methodcall.Arguments[1] as LambdaExpression;
ttype = methodcall.Arguments[1].GetType();

var bios_reg = new RegQuery<BIOS>()
.useSetting(x =>
{
x.Hive = RegistryHive.LocalMachine;
x.SubKey = @"HARDWARE\DESCRIPTION\System\BIOS";
x.View = RegistryView.Registry64;
});
var bios = bios_reg.Dump();
//var unary = methodcall.Arguments[2] as UnaryExpression;
//var lambda = unary.Operand as LambdaExpression;


//var newexpr = lambda.Body as NewExpression;
//methodcall = newexpr.Arguments[1] as MethodCallExpression;
//var ggw = methodcall.Method.GetGenericArguments();
//lambda = methodcall.Arguments[1] as LambdaExpression;
//ttype = methodcall.Arguments[1].GetType();

//var direct_reg = new RegQuery<A2>()
// .useSetting(x =>
// {
// x.Hive = RegistryHive.LocalMachine;
// x.SubKey = @"SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\2A";
// x.View = RegistryView.Registry64;
// });
//direct_reg.CreateOrUpdate(new A2() { A = "12345" });
//var bios = direct_reg.Get();
//direct_reg.Delete();

var regt = new RegQuery<InstalledApp>()
.useSetting(x =>
Expand All @@ -180,12 +179,14 @@ static void Main(string[] args)
x.SubKey = @"SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\1A";
x.View = RegistryView.Registry64;
});
int remove_count2 = regt.Where(x => x.Version == new Version(1, 1, 1, 1)).RemoveAll();
//var yj = regt1.Take(2);
//var aaaa = regt.Except(regt1.Take(2));
//var aaaa = regt.Where(x=>x.DisplayName!="").Except(regt1.Take(2));
//var aaaa = regt.Except(installs, new InstallAppCompare());
var aaaa = regt.Except(regt.ToList().Take(2));
//var aaaa = regt.Union(regt.ToList().Take(2));
//var aaaa = regt.Select(x => x).Where(x => x.DisplayName != "");
var aaaa = regt.Where(x => x.Key == $"{x.Key}");
foreach (var oo in aaaa)
{

Expand Down Expand Up @@ -504,6 +505,11 @@ public AppData(string name)
public bool IsOfficial { set; get; }
}

public class A2
{
public string A { set; get; }
}

public class BIOS
{
[RegSubKeyName]
Expand Down
1 change: 1 addition & 0 deletions QSoft.Reg/RegAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@ public class RegPropertyName : Attribute
[AttributeUsage(AttributeTargets.Property)]
public class RegSubKeyName:Attribute
{
public bool IsFullName { set; get; } = false;
}
}
56 changes: 30 additions & 26 deletions QSoft.Reg/RegExpressionVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -360,25 +360,28 @@ protected override Expression VisitMember(MemberExpression node)
else
{
var regexs = typeof(RegistryKeyEx).GetMethods().Where(x => "GetValue" == x.Name && x.IsGenericMethod == true);
//if (node.Member.GetCustomAttributes(typeof(RegIgnore), true).Length > 0)
//{
// this.Fail = $"{node.Member.Name} is ignored, please do not use";
//}
var regname = node.Member.GetCustomAttributes(true).FirstOrDefault();

var attr = node.Member.GetCustomAttributes(true).FirstOrDefault();
Expression member = null;
Expression left_args_1 = null;
if(regname is RegIgnore)
if (attr is RegIgnore)
{
this.Fail = $"{node.Member.Name} is ignored, please do not use";
}
else if (regname is RegPropertyName)
else if (attr is RegPropertyName)
{
left_args_1 = Expression.Constant((regname as RegPropertyName).Name);
left_args_1 = Expression.Constant((attr as RegPropertyName).Name);
member = Expression.Call(regexs.ElementAt(0).MakeGenericMethod(node.Type), exprs.ElementAt(0).Value, left_args_1);
}
else if(regname is RegSubKeyName)
else if (attr is RegSubKeyName)
{
var subkeyname = attr as RegSubKeyName;
member = Expression.Property(exprs.First().Value, "Name");
if(subkeyname.IsFullName == false)
{
var method = typeof(RegQueryHelper).GetMethod("GetLastSegement");
member = Expression.Call(method, member);
}
}
if(member ==null&&left_args_1 == null)
{
Expand Down Expand Up @@ -577,25 +580,26 @@ protected override Expression VisitMethodCall(MethodCallExpression node)
{
switch(expr.Method.Name)
{
case "Except":
{
var pps = expr.Method.GetParameters();
var vv = typeof(RegQueryEx).GetMethods().Where(x =>
{
bool result = false;
if(x.Name == $"{expr.Method.Name}_RegistryKey" && x.GetParameters().Length == pps.Length)
{
result = true;
}
return result;
}).First();
ttypes1 = exprs1.Select(x => x.Value).GetTypes(vv);
methodcall = Expression.Call(vv.MakeGenericMethod(ttypes1), exprs1.Select(x => x.Value));
}
break;
//case "Except":
case "Union":
//case "Intersect":
// {
// var pps = expr.Method.GetParameters();
// var vv = typeof(RegQueryEx).GetMethods().Where(x =>
// {
// bool result = false;
// if(x.Name == $"{expr.Method.Name}_RegistryKey" && x.GetParameters().Length == pps.Length)
// {
// result = true;
// }
// return result;
// }).First();
// ttypes1 = exprs1.Select(x => x.Value).GetTypes(vv);
// methodcall = Expression.Call(vv.MakeGenericMethod(ttypes1), exprs1.Select(x => x.Value));
// }
// break;
case "Except":
case "Intersect":
case "Union":
case "Distinct":
{
var sd = typeof(TData).ToSelectData();
Expand Down
36 changes: 1 addition & 35 deletions QSoft.Reg/RegProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,6 @@ public IEnumerable<T> Enumerable<T>(IQueryable<RegistryKey> query)

bool m_IsWritable = false;
IQueryable<RegistryKey> m_Regs;
//List<IQueryable<RegistryKey>> m_Temps = new List<IQueryable<RegistryKey>>();
public IQueryable<RegistryKey> CreateRegs()
{
if (this.m_Regs?.Count() > 0)
Expand All @@ -176,15 +175,12 @@ public IQueryable<RegistryKey> CreateRegs()
}

List<RegistryKey> regs = new List<RegistryKey>();
RegistryKey reg = this.Setting;
RegistryKey reg = this.Setting.Create();
var subkeynames = reg.GetSubKeyNames();
foreach (var subkeyname in subkeynames)
{
regs.Add(reg.OpenSubKey(subkeyname, m_IsWritable));
}
//var qqr = regs.AsQueryable();
//m_Temps.Add(qqr);
//return qqr;
return m_Regs = regs.AsQueryable();
}

Expand Down Expand Up @@ -239,7 +235,6 @@ public TResult Execute<TResult>(Expression expression)
Expression arg2 = updatemethod.Arguments[1];
if(type3[1] == typeof(TData))
{
//arg2 = this.ToSelectData();
arg2 = typeof(TData).ToSelectData();
}
updatemethod = Expression.Call(oo, updatemethod.Arguments[0], arg2, typeof(TData).ToSelectData());
Expand Down Expand Up @@ -314,33 +309,6 @@ public TResult Execute<TResult>(Expression expression)
if (excute_reg != null)
{
inst = excute_reg.ToFunc<TResult>()(excute_reg);
//var pps = typeof(TResult).GetProperties().Where(x => x.CanWrite == true&&x.GetCustomAttributes(typeof(RegIgnore), true).Length==0);
//inst = Activator.CreateInstance(typeof(TResult));
//foreach (var pp in pps)
//{
// var regattrs = pp.GetCustomAttributes(true);
// string subkeyname = pp.Name;
// object yyy = null;
// if(regattrs.Length>0)
// {
// var regattr = regattrs.FirstOrDefault();
// if(regattr is RegSubKeyName)
// {
// yyy = excute_reg.Name;
// }
// else if(regattr is RegPropertyName)
// {
// subkeyname = (regattr as RegPropertyName)?.Name;
// yyy = regexs.ElementAt(0).MakeGenericMethod(pp.PropertyType).Invoke(excute_reg, new object[] { excute_reg, subkeyname });
// }
// }
// else
// {
// yyy = regexs.ElementAt(0).MakeGenericMethod(pp.PropertyType).Invoke(excute_reg, new object[] { excute_reg, subkeyname });
// }
// pp.SetValue(inst, yyy, null);
//}

}
else
{
Expand All @@ -354,8 +322,6 @@ public TResult Execute<TResult>(Expression expression)
oo.Dispose();
}
}


return_hr = (TResult)inst;
}

Expand Down
102 changes: 61 additions & 41 deletions QSoft.Reg/RegQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,51 @@ IEnumerator IEnumerable.GetEnumerator()
throw new NotImplementedException();
}

public T Dump()
{
RegistryKey reg = (this.Provider as RegProvider<T>)?.Setting;
return reg.ToFunc<T>()(reg);
}
//public void CreateOrUpdate(T data)
//{
// var setting = (this.Provider as RegProvider<T>)?.Setting;
// RegistryKey reg = setting?.Create(true);
// if (reg == null)
// {
// Regex regex1 = new Regex(@"^(?<parent>.+)(?<=\\)(?<path>.*)", RegexOptions.Compiled);
// string subkey = setting.SubKey;
// var match = regex1.Match(subkey);
// if(match.Success)
// {
// var basekey = RegistryKey.OpenBaseKey(setting.Hive, setting.View);
// var parentreg = basekey.OpenSubKey(match.Groups["parent"].Value, true);
// reg = parentreg.CreateSubKey(match.Groups["path"].Value, RegistryKeyPermissionCheck.ReadWriteSubTree);
// }
// }


// if(reg != null)
// {
// var pps = typeof(T).GetProperties().Where(x => x.CanRead == true);
// foreach(var pp in pps)
// {
// reg.SetValue(pp.Name, pp.GetValue(data, null));
// }
// }
//}

//public T Get()
//{
// RegistryKey reg = (this.Provider as RegProvider<T>)?.Setting?.Create();
// return reg.ToFunc<T>()(reg);
//}

//public void Delete()
//{
// RegistryKey reg = (this.Provider as RegProvider<T>)?.Setting?.Create();
// var parent = reg.GetParent();
// Regex regex1 = new Regex(@"^(.+)(?<=\\)(?<path>.*)", RegexOptions.Compiled);
// var match = regex1.Match(reg.Name);
// if (match.Success)
// {
// parent.DeleteSubKeyTree(match.Groups["path"].Value);
// }
//}
}

public class RegSetting
Expand All @@ -66,21 +106,31 @@ public class RegSetting
public RegistryHive Hive { set; get; }
public RegistryView View { set; get; }


public static implicit operator RegistryKey(RegSetting data)
public RegistryKey Create(bool write=false)
{
RegistryKey reg_base = RegistryKey.OpenBaseKey(data.Hive, RegistryView.Registry64);
if (string.IsNullOrEmpty(data.SubKey) == false)
RegistryKey reg_base = RegistryKey.OpenBaseKey(this.Hive, this.View);
if (string.IsNullOrEmpty(this.SubKey) == false)
{
RegistryKey reg = reg_base.OpenSubKey(data.SubKey);
RegistryKey reg = reg_base.OpenSubKey(this.SubKey, write);
reg_base.Dispose();
return reg;
}
return reg_base;
}
//public static implicit operator RegistryKey(RegSetting data)
//{
// RegistryKey reg_base = RegistryKey.OpenBaseKey(data.Hive, RegistryView.Registry64);
// if (string.IsNullOrEmpty(data.SubKey) == false)
// {
// RegistryKey reg = reg_base.OpenSubKey(data.SubKey);
// reg_base.Dispose();
// return reg;
// }
// return reg_base;
//}
}

public class EqualityComparerAll<T> : IEqualityComparer<T>
public class EqualityComparerForce<T> : IEqualityComparer<T>
{
public bool Equals(T x, T y)
{
Expand All @@ -102,36 +152,6 @@ public bool Equals(T x, T y)
public int GetHashCode(T obj)
{
return 0;
//Check whether the object is null
if (Object.ReferenceEquals(obj, null)) return 0;
List<int> hashcodes = new List<int>();
var pps = typeof(T).GetProperties().Where(p => p.CanRead == true);
foreach (var pp in pps)
{
var typecode = Type.GetTypeCode(pp.PropertyType);

var ss1 = pp.GetValue(obj, null);
if(ss1 == null)
{
hashcodes.Add(0);
}
else
{
hashcodes.Add(ss1.GetHashCode());
}
}

int hashcode = hashcodes.Aggregate((x, y) => x ^ y);
System.Diagnostics.Trace.WriteLine($"hashcode:{hashcode}");
return hashcode;
//Get hash code for the Name field if it is not null.
//int hashProductName = product.Name == null ? 0 : product.Name.GetHashCode();

////Get hash code for the Code field.
//int hashProductCode = product.Code.GetHashCode();

////Calculate the hash code for the product.
//return hashProductName ^ hashProductCode;
}
}
}
Loading

0 comments on commit 560e7c1

Please sign in to comment.