Skip to content

Commit 5e5a457

Browse files
committed
bug fixes
1 parent 0a55e58 commit 5e5a457

8 files changed

Lines changed: 117 additions & 19 deletions

File tree

LibVideo/App.xaml.cs

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,38 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Configuration;
4-
using System.Data;
5-
using System.Linq;
6-
using System.Threading.Tasks;
1+
using System;
2+
using System.IO;
73
using System.Windows;
4+
using System.Threading.Tasks;
85

96
namespace LibVideo
107
{
11-
/// <summary>
12-
/// App.xaml 的交互逻辑
13-
/// </summary>
148
public partial class App : Application
159
{
10+
protected override void OnStartup(StartupEventArgs e)
11+
{
12+
base.OnStartup(e);
13+
14+
AppDomain.CurrentDomain.UnhandledException += (s, args) =>
15+
LogException(args.ExceptionObject as Exception);
16+
17+
DispatcherUnhandledException += (s, args) =>
18+
{
19+
LogException(args.Exception);
20+
args.Handled = true;
21+
Environment.Exit(1);
22+
};
23+
24+
TaskScheduler.UnobservedTaskException += (s, args) =>
25+
LogException(args.Exception);
26+
}
1627

17-
28+
private void LogException(Exception ex)
29+
{
30+
if (ex == null) return;
31+
try
32+
{
33+
File.AppendAllText("crash.log", $"[{DateTime.Now}] {ex.ToString()}\n\n");
34+
}
35+
catch { }
36+
}
1837
}
1938
}

LibVideo/Data/DatabaseManager.cs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,30 @@ public DatabaseManager(string path = "libvideo_db.db")
1414
dbPath = path;
1515
}
1616

17-
public void InsertOrUpdateItems(IEnumerable<VideoItem> items)
17+
public void SyncDiskItems(IEnumerable<VideoItem> currentFilesList, HashSet<string> scannedRoots)
1818
{
1919
using (var db = new LiteDatabase(dbPath))
2020
{
2121
var col = db.GetCollection<VideoItem>("videos");
2222
col.EnsureIndex(x => x.FullName, true);
2323

24-
var existingFiles = new HashSet<string>(col.FindAll().Select(v => v.FullName));
25-
var newItems = items.Where(i => !existingFiles.Contains(i.FullName)).ToList();
24+
var allDb = col.FindAll().ToList();
25+
var currentPaths = new HashSet<string>(currentFilesList.Select(x => x.FullName), System.StringComparer.OrdinalIgnoreCase);
26+
27+
foreach(var dbItem in allDb)
28+
{
29+
bool isUnderScannedRoot = scannedRoots.Any(root => dbItem.FullName.StartsWith(root, System.StringComparison.OrdinalIgnoreCase));
30+
if (isUnderScannedRoot)
31+
{
32+
if (!currentPaths.Contains(dbItem.FullName))
33+
{
34+
col.Delete(dbItem.Id);
35+
}
36+
}
37+
}
38+
39+
var existingFiles = new HashSet<string>(col.FindAll().Select(v => v.FullName), System.StringComparer.OrdinalIgnoreCase);
40+
var newItems = currentFilesList.Where(i => !existingFiles.Contains(i.FullName)).ToList();
2641

2742
if (newItems.Count > 0)
2843
{

LibVideo/FodyWeavers.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<Weavers xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="FodyWeavers.xsd">
2+
<Costura />
3+
</Weavers>

LibVideo/LibVideo2.csproj

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,11 @@
7676
<SignAssembly>false</SignAssembly>
7777
</PropertyGroup>
7878
<ItemGroup>
79+
<PackageReference Include="Costura.Fody">
80+
<Version>6.0.0</Version>
81+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
82+
<PrivateAssets>all</PrivateAssets>
83+
</PackageReference>
7984
<PackageReference Include="WindowsAPICodePack-Core" Version="1.1.1" />
8085
<PackageReference Include="WindowsAPICodePack-Shell" Version="1.1.1" />
8186
<PackageReference Include="LiteDB" Version="5.0.21" />
@@ -161,7 +166,7 @@
161166
<Resource Include="周 图标.ico" />
162167
</ItemGroup>
163168
<ItemGroup>
164-
<Content Include="background.jpg" />
169+
<Resource Include="background.jpg" />
165170
</ItemGroup>
166171
<ItemGroup>
167172
<BootstrapperPackage Include="Microsoft.Net.Framework.3.5.SP1">

LibVideo/MainWindow.xaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
<Button Style="{StaticResource MaterialDesignFloatingActionMiniButton}" Command="{Binding RefreshCacheCommand}" Height="40" Width="40" ToolTip="清空海报与简介缓存库 (重置识别)" Margin="0,0,10,0" Background="{DynamicResource PrimaryHueDarkBrush}" BorderBrush="Transparent">
5757
<materialDesign:PackIcon Kind="DatabaseRefresh" Height="24" Width="24" Foreground="White" />
5858
</Button>
59-
<Button x:Name="btnSettings" Style="{StaticResource MaterialDesignFloatingActionMiniButton}" Click="btnSettings_Click" Height="40" Width="40" ToolTip="管理视频目录">
59+
<Button x:Name="btnSettings" Style="{StaticResource MaterialDesignFloatingActionMiniButton}" Click="btnSettings_Click" Height="40" Width="40" ToolTip="设置与管理视频目录">
6060
<materialDesign:PackIcon Kind="Settings" Height="24" Width="24" />
6161
</Button>
6262
</StackPanel>

LibVideo/ViewModels/MainViewModel.cs

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public MainViewModel()
3939
RefreshCacheCommand = new AsyncRelayCommand(RefreshCacheAsync);
4040

4141
LoadDirectories();
42+
SetupWatchers();
4243
if (File.Exists("player.txt")) customPlayerPath = File.ReadAllText("player.txt");
4344
_ = InitializeDataAsync();
4445
}
@@ -202,6 +203,51 @@ private async void LoadMetadata(string path)
202203
public IAsyncRelayCommand RefreshCacheCommand { get; }
203204

204205
private List<VideoItem> _allDatabaseItems = new List<VideoItem>();
206+
private List<FileSystemWatcher> _watchers = new List<FileSystemWatcher>();
207+
private System.Windows.Threading.DispatcherTimer _debounceTimer;
208+
209+
private void SetupWatchers()
210+
{
211+
foreach (var w in _watchers)
212+
{
213+
w.EnableRaisingEvents = false;
214+
w.Dispose();
215+
}
216+
_watchers.Clear();
217+
218+
foreach (var dir in Directories)
219+
{
220+
if (Directory.Exists(dir))
221+
{
222+
var watcher = new FileSystemWatcher(dir);
223+
watcher.IncludeSubdirectories = false;
224+
watcher.Created += Watcher_Changed;
225+
watcher.Deleted += Watcher_Changed;
226+
watcher.Renamed += Watcher_Changed;
227+
watcher.EnableRaisingEvents = true;
228+
_watchers.Add(watcher);
229+
}
230+
}
231+
}
232+
233+
private void Watcher_Changed(object sender, FileSystemEventArgs e)
234+
{
235+
Application.Current.Dispatcher.Invoke(() =>
236+
{
237+
if (_debounceTimer == null)
238+
{
239+
_debounceTimer = new System.Windows.Threading.DispatcherTimer();
240+
_debounceTimer.Interval = TimeSpan.FromSeconds(1);
241+
_debounceTimer.Tick += async (s, args) =>
242+
{
243+
_debounceTimer.Stop();
244+
await RefreshFromDiskAsync();
245+
};
246+
}
247+
_debounceTimer.Stop();
248+
_debounceTimer.Start();
249+
});
250+
}
205251
private int searchHistoryIndex = -1;
206252

207253
private void LoadDirectories()
@@ -224,6 +270,7 @@ private async Task AddDirectory(string path)
224270
{
225271
Directories.Add(path);
226272
SaveDirectories();
273+
SetupWatchers();
227274
await RefreshFromDiskAsync();
228275
}
229276
}
@@ -235,6 +282,7 @@ private async Task DeleteSelectedDirectory()
235282
_dbManager.RemoveDirectoryItems(SelectedDirectory);
236283
Directories.Remove(SelectedDirectory);
237284
SaveDirectories();
285+
SetupWatchers();
238286
await LoadFromDatabaseAsync();
239287
}
240288
}
@@ -276,13 +324,18 @@ private async Task RefreshFromDiskAsync()
276324
{
277325
IsLoading = true;
278326
var currentFilesList = new List<VideoItem>();
327+
var scannedRoots = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
279328

280329
await Task.Run(() =>
281330
{
282331
foreach (var dir in Directories)
283332
{
284333
if (Directory.Exists(dir))
285334
{
335+
string safeDir = dir;
336+
if (!safeDir.EndsWith("\\")) safeDir += "\\";
337+
scannedRoots.Add(safeDir);
338+
286339
var di = new DirectoryInfo(dir);
287340
try {
288341
foreach (var fi in di.EnumerateFiles("*.*"))
@@ -297,7 +350,7 @@ await Task.Run(() =>
297350
} catch { }
298351
}
299352
}
300-
_dbManager.InsertOrUpdateItems(currentFilesList);
353+
_dbManager.SyncDiskItems(currentFilesList, scannedRoots);
301354
});
302355

303356
await LoadFromDatabaseAsync();

libvideo_db.db

32 KB
Binary file not shown.

readme.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,15 @@ LibVideo 是一款基于 WPF (MVVM) 架构打造的现代化、高性能本地
2424
- 如果您有特定的需求(例如 VLC 或 MPC-HC),同样在设置界面点击 **“选择自定义播放器”** 手动指定 `*.exe` 程序即可全局覆盖。如需恢复默认,点击“清除”按钮。
2525

2626
### 4. 强制纠错与缓存清盘
27-
- 有时候刚下载的一部新电影由于名字里多了一些怪符号匹配失败,您在电脑文件夹里将它改名之后。
28-
- 请直接点击界面 **右上角齿轮边上的 刷新按钮(圆圈刷新图标)**
29-
- 这将彻底抹除当前数据库中被错误记录的历史“元数据记忆”。随后按照您的新文件名进行干干净净的重新检索(请放心,您之前配置好的管理目录均不会丢失)。
27+
- 有时候刚下载的一部新电影由于名字里多了一些怪符号匹配失败,您在电脑文件夹里将它改名之后,请直接点击界面 **右上角齿轮边上的 刷新按钮(圆圈刷新图标)**
28+
- 这将彻底抹除当前数据库中被错误记录的历史“元数据记忆”,随后按照您的新文件名进行干干净净的重-新检索(请放心,之前配置好的管理目录均不会丢失)。
3029

3130
### 5. 极智历史搜索系统
3231
- 在左上角的搜索框中输入任何影片关键词如“星际迷航”,此时输入框下方会瞬间为您即刻筛选视频项目。
3332
- 若看毕不想保留该次搜索结果,直接点击搜索框右侧带圆圈的 **X(清空按钮)** 即可。
3433
- 系统为您独创了**“按键意图探测器”**,只要在这个过程中您没有按键盘上的退格或Delete主动删字,这个词就会被系统判定为您刚想使用的热门长词,并将它自动收录进您下拉历史框的“置顶第一位”。
3534
- 通过搜索框左侧的 **“<前进、后退>”键** 即可无缝穿梭您的“搜索历史快照”,再也不会为遗漏关键词而烦恼!
35+
36+
### 6. 自动化目录监控 (无缝同步)
37+
- 软件已在底层植入了超低功耗的系统级文件探测网(`FileSystemWatcher`)。
38+
- 任何时候当您在外部环境中(比如通过迅雷或者浏览器)向已挂载的目录中下载、新增、删除或者重命名了影视文件,软件的后台神经系统都能在 1 秒钟内光速察觉,并立刻引发一次平滑的自动列表校准。界面的数字与条目将瞬间刷新出您刚倒腾好的新影片,真正做到无人值守!

0 commit comments

Comments
 (0)