解决JSON序列化的BUG和PInvoke函数与Unity兼容问题

This commit is contained in:
小红帽 2024-01-05 00:02:58 +08:00
parent 1d727a09ef
commit 1305f55fef
6 changed files with 38 additions and 17 deletions

View File

@ -1490,8 +1490,10 @@ BLENDFUNCTION blendFunction // alpha-blending function
//[DllImport("shell32.dll", CharSet = CharSet.Auto)] //[DllImport("shell32.dll", CharSet = CharSet.Auto)]
//public static extern int DragQueryFile(HandleRef hDrop, int iFile, StringBuilder lpszFile, int cch); //public static extern int DragQueryFile(HandleRef hDrop, int iFile, StringBuilder lpszFile, int cch);
[DllImport("dwmapi.dll", PreserveSig = false)] //[DllImport("dwmapi.dll", PreserveSig = false)]
public static extern bool DwmIsCompositionEnabled(); //public static extern bool DwmIsCompositionEnabled();
[DllImport("dwmapi.dll", BestFitMapping = false)]
public static extern int DwmIsCompositionEnabled(out Int32 enabled);
[DllImport("dwmapi.dll")] [DllImport("dwmapi.dll")]
public static extern int DwmEnableBlurBehindWindow(IntPtr hWnd, ref DWM_BLURBEHIND pBlurBehind); public static extern int DwmEnableBlurBehindWindow(IntPtr hWnd, ref DWM_BLURBEHIND pBlurBehind);

View File

@ -83,7 +83,7 @@ namespace CPF.Windows
} }
posttime = Application.Elapsed; posttime = Application.Elapsed;
var v = System.Environment.OSVersion.Version; var v = System.Environment.OSVersion.Version;
if (v.Major >= 6 && DwmIsCompositionEnabled() && !Application.DesignMode) if (v.Major >= 6 && DwmIsCompositionEnabled(out var e) == 0 && e == 1 && !Application.DesignMode)
{ {
} }
else else
@ -92,7 +92,7 @@ namespace CPF.Windows
} }
if ((v.Major == 6 && v.Minor < 2)) if ((v.Major == 6 && v.Minor < 2))
{ {
touchMsg = RegisterTouchWindow(handle, RegisterTouchFlags.TWF_NONE); touchMsg = RegisterTouchWindow(handle, RegisterTouchFlags.TWF_NONE);
} }
_className = "CPFWindow-" + Guid.NewGuid(); _className = "CPFWindow-" + Guid.NewGuid();
// 初始化窗口类结构 // 初始化窗口类结构

View File

@ -620,7 +620,7 @@ namespace CPF
throw new Exception($"错误:{ex}"); throw new Exception($"错误:{ex}");
} }
} }
CpfObject SourceProperty = null; //CpfObject SourceProperty = null;
private void Target_PropertyChanged(object sender, PropertyChangedEventArgs e) private void Target_PropertyChanged(object sender, PropertyChangedEventArgs e)
{ {
//重新绑定 //重新绑定

View File

@ -163,6 +163,10 @@ namespace CPF.Json
{ {
sd = new Dictionary<string, myPropInfo>(); sd = new Dictionary<string, myPropInfo>();
var bf = BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static; var bf = BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static;
if (type.Name == "Nullable`1")
{
type = type.GetGenericArguments()[0];
}
PropertyInfo[] pr = type.GetProperties(bf); PropertyInfo[] pr = type.GetProperties(bf);
foreach (PropertyInfo p in pr) foreach (PropertyInfo p in pr)
{ {
@ -317,16 +321,26 @@ namespace CPF.Json
} }
else // structs else // structs
{ {
DynamicMethod dynMethod = new DynamicMethod("_", typeof(object), null); if (objtype.Name == "Nullable`1")
ILGenerator ilGen = dynMethod.GetILGenerator(); {
var lv = ilGen.DeclareLocal(objtype); var sType = objtype.GetGenericArguments()[0];
ilGen.Emit(OpCodes.Ldloca_S, lv); var create = objtype.GetConstructor(new Type[] { sType });
ilGen.Emit(OpCodes.Initobj, objtype); c = () => create.Invoke(new object[] { null });
ilGen.Emit(OpCodes.Ldloc_0); _constrcache.Add(objtype, c);
ilGen.Emit(OpCodes.Box, objtype); }
ilGen.Emit(OpCodes.Ret); else
c = (CreateObject)dynMethod.CreateDelegate(typeof(CreateObject)); {
_constrcache.Add(objtype, c); DynamicMethod dynMethod = new DynamicMethod("_", typeof(object), null);
ILGenerator ilGen = dynMethod.GetILGenerator();
var lv = ilGen.DeclareLocal(objtype);
ilGen.Emit(OpCodes.Ldloca_S, lv);
ilGen.Emit(OpCodes.Initobj, objtype);
ilGen.Emit(OpCodes.Ldloc_0);
ilGen.Emit(OpCodes.Box, objtype);
ilGen.Emit(OpCodes.Ret);
c = (CreateObject)dynMethod.CreateDelegate(typeof(CreateObject));
_constrcache.Add(objtype, c);
}
} }
return c(); return c();
} }

View File

@ -228,6 +228,11 @@ namespace CPF.Platform
get; get;
set; set;
} }
/// <summary>
/// 某些平台下不能使用CPF的消息循环需要将该属性改成false
/// </summary>
public static bool NeedRunLoop { get; set; } = true;
//#if NETSTANDARD //#if NETSTANDARD
// /// <summary> // /// <summary>
// /// 启用AOT功能net5以及以上版本才能使用其中包括禁用弱引用事件功能一般在Aot的release发布的时候设置。如果不用Aot请勿设置否则可能有内存泄露风险而且会导致性能降低。 以及切换windows的com包装 // /// 启用AOT功能net5以及以上版本才能使用其中包括禁用弱引用事件功能一般在Aot的release发布的时候设置。如果不用Aot请勿设置否则可能有内存泄露风险而且会导致性能降低。 以及切换windows的com包装

View File

@ -46,7 +46,7 @@ namespace CPF.Threading
if (timeThread == null) if (timeThread == null)
{ {
timers = new List<DispatcherTimer>(); timers = new List<DispatcherTimer>();
tempTimers = new List<DispatcherTimer>(); tempTimers = new List<DispatcherTimer>();
timeThread = new Thread(SetTime) { IsBackground = true, Name = "定时器线程" }; timeThread = new Thread(SetTime) { IsBackground = true, Name = "定时器线程" };
timeThread.Start(); timeThread.Start();
} }
@ -100,7 +100,7 @@ namespace CPF.Threading
} }
if (tempTimers.Count > 0) if (tempTimers.Count > 0)
{ {
if (CPF.Platform.Application.Main != null) if ((CPF.Platform.Application.NeedRunLoop && CPF.Platform.Application.Main != null) || !CPF.Platform.Application.NeedRunLoop)
{ {
Dispatcher.MainThread.Invoke(() => Dispatcher.MainThread.Invoke(() =>
{ {