Browse Source

调整部分局部变量可见性
完善新版工作区创建
群聊数据改用标准方式获取

Suxue 1 year ago
parent
commit
e83d3869cc

+ 2 - 2
Helpers/ToolsHelper.cs

@@ -11,7 +11,7 @@ namespace WechatPCMsgBakTool.Helpers
 {
     public class ToolsHelper
     {
-        public static TaskFactory factory = new TaskFactory(new LimitedConcurrencyLevelTaskScheduler(10));
+        private static TaskFactory factory = new TaskFactory(new LimitedConcurrencyLevelTaskScheduler(10));
         public static string DecodeVoice(string source,string pcm,string to)
         {
             string ffmpeg = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Tools", "ffmpeg.exe");
@@ -45,7 +45,7 @@ namespace WechatPCMsgBakTool.Helpers
 
     }
 
-    public class LimitedConcurrencyLevelTaskScheduler : TaskScheduler
+    partial class LimitedConcurrencyLevelTaskScheduler : TaskScheduler
     {
         // Indicates whether the current thread is processing work items.
         [ThreadStatic]

+ 25 - 0
Model/ProtobufModel.cs

@@ -0,0 +1,25 @@
+using ProtoBuf;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace WechatPCMsgBakTool.Model
+{
+    [ProtoContract]
+    public class TVType
+    {
+        [ProtoMember(1)]
+        public int Type;
+        [ProtoMember(2)]
+        public string TypeValue = "";
+    }
+
+    [ProtoContract]
+    public class ProtoMsg
+    {
+        [ProtoMember(3)]
+        public List<TVType>? TVMsg;
+    }
+}

+ 28 - 2
Pages/CreateWork.xaml

@@ -5,11 +5,37 @@
       xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
       xmlns:local="clr-namespace:WechatPCMsgBakTool.Pages"
       mc:Ignorable="d" 
-      d:DesignHeight="450" d:DesignWidth="800"
+      d:DesignHeight="550" d:DesignWidth="800"
       Title="Welcome" Background="White">
 
     <Grid>
         <Label FontSize="20" Margin="30,15" Content="新建工作区" HorizontalAlignment="Left" VerticalAlignment="Top" />
-        
+        <Label Margin="30,55,0,0" Content="请选择要创建工作区的微信,可以通过微信路径判断是哪一个微信哦!" HorizontalAlignment="Left" VerticalAlignment="Top" />
+        <ListView Name="list_process" Margin="35,95,35,0" VerticalAlignment="Top" Height="160" ItemsSource="{Binding ProcessInfos}" SelectionChanged="list_process_SelectionChanged" SelectedItem="{Binding SelectProcess}">
+            <ListView.View>
+                <GridView>
+                    <GridViewColumn Header="进程名" Width="120" DisplayMemberBinding="{Binding ProcessName}" />
+                    <GridViewColumn Header="PID" Width="80" DisplayMemberBinding="{Binding ProcessId}" />
+                    <GridViewColumn Header="路径" Width="430" DisplayMemberBinding="{Binding DBPath}" />
+                </GridView>
+            </ListView.View>
+        </ListView>
+        <Label Margin="30,275,0,0" Content="选择微信后,请确认下方自动获取的微信名是否正确。不正确请修改!" FontWeight="Bold" HorizontalAlignment="Left" VerticalAlignment="Top"/>
+        <TextBox x:Name="txt_username" Margin="35,300,0,0" Width="280" HorizontalAlignment="Left" VerticalAlignment="Top" BorderThickness="0,0,0,1" Text="{Binding UserName}" />
+
+        <Label Margin="30,350,0,0" Content="请选择解密方式:" FontWeight="Bold" HorizontalAlignment="Left" VerticalAlignment="Top"/>
+        <RadioButton Margin="35,380,0,0" Content="固定地址查找" HorizontalAlignment="Left" VerticalAlignment="Top" GroupName="rb_find_key" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" />
+        <RadioButton Margin="35,405,0,0" Content="用户名推断查找" HorizontalAlignment="Left" VerticalAlignment="Top" GroupName="rb_find_key" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" />
+
+        <Button Name="btn_create_worksapce" Margin="0,0,35,50" Height="60" Width="100" HorizontalAlignment="Right" VerticalAlignment="Bottom" Content="创建工作区" BorderThickness="0" Background="#2775b6" Foreground="White" Click="btn_create_worksapce_Click">
+            <Button.Resources>
+                <Style TargetType="{x:Type Border}">
+                    <Setter Property="CornerRadius" Value="8"/>
+                </Style>
+            </Button.Resources>
+        </Button>
+        <Label Margin="210,350,0,0" Content="其他选项:" FontWeight="Bold" HorizontalAlignment="Left" VerticalAlignment="Top"/>
+        <CheckBox Margin="215,380,0,0" Content="打包资源文件夹(功能规划中)" IsEnabled="False" HorizontalAlignment="Left" VerticalAlignment="Top" />
+        <CheckBox Margin="215,405,0,0" Content="手动模式(功能规划中)" IsEnabled="False" HorizontalAlignment="Left" VerticalAlignment="Top" />
     </Grid>
 </Page>

+ 76 - 0
Pages/CreateWork.xaml.cs

@@ -1,5 +1,7 @@
 using System;
 using System.Collections.Generic;
+using System.Diagnostics;
+using System.IO;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
@@ -12,6 +14,9 @@ using System.Windows.Media;
 using System.Windows.Media.Imaging;
 using System.Windows.Navigation;
 using System.Windows.Shapes;
+using WechatPCMsgBakTool.Helpers;
+using WechatPCMsgBakTool.Model;
+using WechatPCMsgBakTool.ViewModel;
 
 namespace WechatPCMsgBakTool.Pages
 {
@@ -20,9 +25,80 @@ namespace WechatPCMsgBakTool.Pages
     /// </summary>
     public partial class CreateWork : Page
     {
+        private CreateWorkViewModel ViewModel = new CreateWorkViewModel();
         public CreateWork()
         {
+            DataContext = ViewModel;
             InitializeComponent();
+            GetWechatProcessInfos();
+        }
+
+        private void GetWechatProcessInfos()
+        {
+            ViewModel.ProcessInfos.Clear();
+            Process[] processes = Process.GetProcessesByName("wechat");
+            foreach (Process p in processes)
+            {
+                var lHandles = NativeAPIHelper.GetHandleInfoForPID((uint)p.Id);
+                foreach (var h in lHandles)
+                {
+                    string name = NativeAPIHelper.FindHandleName(h, p);
+                    if (name != "")
+                    {
+                        // 预留handle log
+                        if (File.Exists("handle.log"))
+                        {
+                            File.AppendAllText("handle.log", string.Format("{0}|{1}|{2}|{3}\n", p.Id, h.ObjectTypeIndex, h.HandleValue, name));
+                        }
+                        if (name.Contains("\\MicroMsg.db") && name.Substring(name.Length - 3, 3) == ".db")
+                        {
+                            ProcessInfo info = new ProcessInfo();
+                            info.ProcessId = p.Id.ToString();
+                            info.ProcessName = p.ProcessName;
+                            info.DBPath = DevicePathMapper.FromDevicePath(name);
+                            ViewModel.ProcessInfos.Add(info);
+                        }
+                    }
+                }
+            }
+        }
+
+        private void list_process_SelectionChanged(object sender, SelectionChangedEventArgs e)
+        {
+            if (ViewModel.SelectProcess != null)
+            {
+                string[] name_raw = ViewModel.SelectProcess.DBPath.Split("\\");
+                ViewModel.UserName = name_raw[name_raw.Length - 3];
+
+                FileInfo fileInfo = new FileInfo(ViewModel.SelectProcess.DBPath);
+                DirectoryInfo msgParent = fileInfo.Directory!.Parent!;
+                DirectoryInfo[] accounts = msgParent.GetDirectories();
+
+                DirectoryInfo? newUserName = null;
+                foreach ( DirectoryInfo account in accounts )
+                {
+                    if(account.Name.Contains("account_")) {
+                        if(newUserName == null)
+                            newUserName = account;
+                        else
+                        {
+                            if (newUserName.LastWriteTime < account.LastWriteTime)
+                                newUserName = account;
+                        }
+                    }
+                }
+                if(newUserName != null)
+                {
+                    ViewModel.UserName = newUserName.Name.Split("_")[1];
+                }
+            }
+
+            
+        }
+
+        private void btn_create_worksapce_Click(object sender, RoutedEventArgs e)
+        {
+
         }
     }
 }

+ 0 - 2
SelectWechat.xaml.cs

@@ -42,9 +42,7 @@ namespace WechatPCMsgBakTool
             Process[] processes = Process.GetProcessesByName("wechat");
             foreach (Process p in processes)
             {
-                File.AppendAllText("debug.log", "wechat=>" + p.Id + "\r\n");
                 var lHandles = NativeAPIHelper.GetHandleInfoForPID((uint)p.Id);
-                
                 foreach (var h in lHandles)
                 {
                     string name = NativeAPIHelper.FindHandleName(h, p);

+ 22 - 0
ViewModel/CreateWorkViewModel.cs

@@ -0,0 +1,22 @@
+using CommunityToolkit.Mvvm.ComponentModel;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using WechatPCMsgBakTool.Model;
+
+namespace WechatPCMsgBakTool.ViewModel
+{
+    partial class CreateWorkViewModel : ObservableObject
+    {
+        [ObservableProperty]
+        private List<ProcessInfo> processInfos = new List<ProcessInfo>();
+
+        [ObservableProperty]
+        private ProcessInfo? selectProcess;
+
+        [ObservableProperty]
+        private string userName = "";
+    }
+}

+ 2 - 2
ViewModel/WorkspaceViewModel.cs

@@ -12,10 +12,10 @@ namespace WechatPCMsgBakTool.ViewModel
     partial class WorkspaceViewModel : ObservableObject
     {
         [ObservableProperty]
-        public WXContact? wXContact;
+        private WXContact? wXContact;
 
         [ObservableProperty]
-        public ObservableCollection<WXContact>? contacts;
+        private ObservableCollection<WXContact>? contacts;
 
         private string searchString = "";
         public string SearchString

+ 18 - 21
WXUserReader.cs

@@ -196,29 +196,26 @@ namespace WechatPCMsgBakTool
                             if (w.BytesExtra == null)
                                 continue;
 
-                            string talkId = Encoding.UTF8.GetString(w.BytesExtra!);
-                            if (!w.IsSender)
+                            string sl = BitConverter.ToString(w.BytesExtra).Replace("-", "");
+
+                            ProtoMsg protoMsg;
+                            using (MemoryStream stream = new MemoryStream(w.BytesExtra))
                             {
-                                if (w.BytesExtra[0] == 26)
-                                {
-                                    int usrLength = w.BytesExtra[1];
-                                    userId = Encoding.UTF8.GetString(w.BytesExtra.Skip(6).Take(usrLength - 4).ToArray());
-                                }
-                                if (w.BytesExtra[0] == 10)
+                                protoMsg = ProtoBuf.Serializer.Deserialize<ProtoMsg>(stream);
+                            }
+
+                            if(protoMsg.TVMsg != null)
+                            {
+                                foreach(TVType _tmp in protoMsg.TVMsg)
                                 {
-                                    int version = w.BytesExtra[10];
-                                    if (version == 16)
-                                    {
-                                        int usrLength = w.BytesExtra[13];
-                                        userId = Encoding.UTF8.GetString(w.BytesExtra.Skip(18).Take(usrLength - 4).ToArray());
-                                    }
-                                    else if (version == 18)
-                                    {
-                                        int usrLength = w.BytesExtra[7];
-                                        userId = Encoding.UTF8.GetString(w.BytesExtra.Skip(12).Take(usrLength - 4).ToArray());
-                                    }
+                                    if (_tmp.Type == 1)
+                                        userId = _tmp.TypeValue;
                                 }
-                                
+                            }
+                            
+
+                            if (!w.IsSender)
+                            {
                                 if(UserNameCache.ContainsKey(userId))
                                 {
                                     WXContact? contact = UserNameCache[userId] as WXContact;
@@ -227,7 +224,7 @@ namespace WechatPCMsgBakTool
                                 }
                                 else
                                 {
-
+                                    w.NickName = userId;
                                 }
                             }
                             else

+ 1 - 0
WechatPCMsgBakTool.csproj

@@ -15,6 +15,7 @@
     <PackageReference Include="CommunityToolkit.Mvvm" Version="8.2.2" />
     <PackageReference Include="K4os.Compression.LZ4.Streams" Version="1.3.6" />
     <PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
+    <PackageReference Include="protobuf-net" Version="3.2.30" />
     <PackageReference Include="sqlite-net-pcl" Version="1.8.116" />
   </ItemGroup>