Skip to content

Commit

Permalink
fix crash if no workshop addons are present
Browse files Browse the repository at this point in the history
  • Loading branch information
committed Sep 5, 2020
1 parent 1e21d03 commit f81e752
Show file tree
Hide file tree
Showing 8 changed files with 145 additions and 12 deletions.
14 changes: 12 additions & 2 deletions L4d2AddonsMgr.sln
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.28307.168
# Visual Studio Version 16
VisualStudioVersion = 16.0.30413.136
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "L4d2AddonsMgr", "L4d2AddonsMgr\L4d2AddonsMgr.csproj", "{E66621F6-7012-4D61-A1CB-E054671DE59A}"
EndProject
Expand All @@ -15,17 +15,27 @@ EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Debug|x64 = Debug|x64
Release|Any CPU = Release|Any CPU
Release|x64 = Release|x64
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{E66621F6-7012-4D61-A1CB-E054671DE59A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E66621F6-7012-4D61-A1CB-E054671DE59A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E66621F6-7012-4D61-A1CB-E054671DE59A}.Debug|x64.ActiveCfg = Debug|x64
{E66621F6-7012-4D61-A1CB-E054671DE59A}.Debug|x64.Build.0 = Debug|x64
{E66621F6-7012-4D61-A1CB-E054671DE59A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E66621F6-7012-4D61-A1CB-E054671DE59A}.Release|Any CPU.Build.0 = Release|Any CPU
{E66621F6-7012-4D61-A1CB-E054671DE59A}.Release|x64.ActiveCfg = Release|x64
{E66621F6-7012-4D61-A1CB-E054671DE59A}.Release|x64.Build.0 = Release|x64
{1DFFC99A-5D2A-478B-A01D-7808C9682CFF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1DFFC99A-5D2A-478B-A01D-7808C9682CFF}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1DFFC99A-5D2A-478B-A01D-7808C9682CFF}.Debug|x64.ActiveCfg = Debug|x64
{1DFFC99A-5D2A-478B-A01D-7808C9682CFF}.Debug|x64.Build.0 = Debug|x64
{1DFFC99A-5D2A-478B-A01D-7808C9682CFF}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1DFFC99A-5D2A-478B-A01D-7808C9682CFF}.Release|Any CPU.Build.0 = Release|Any CPU
{1DFFC99A-5D2A-478B-A01D-7808C9682CFF}.Release|x64.ActiveCfg = Release|x64
{1DFFC99A-5D2A-478B-A01D-7808C9682CFF}.Release|x64.Build.0 = Release|x64
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
10 changes: 7 additions & 3 deletions L4d2AddonsMgr/AddonsLibrary/GameDirAddonsLibrary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,13 @@ public bool MoveNext() {
currentEnum.Dispose();
// Enumerate than list.
// https://docs.microsoft.com/en-us/dotnet/api/system.io.directory.enumeratefiles?view=netframework-4.8
currentEnum = new DirectoryInfo(
Path.Combine(basePath, CommonConsts.AddonsWorkshopDirectoryName)
).EnumerateFiles(CommonConsts.VpkFileSearchPattern).GetEnumerator();
try {
currentEnum = new DirectoryInfo(
Path.Combine(basePath, CommonConsts.AddonsWorkshopDirectoryName)
).EnumerateFiles(CommonConsts.VpkFileSearchPattern).GetEnumerator();
}catch (IOException) {
return res;
}
isEnumWorkshop = true;
return currentEnum.MoveNext();
}
Expand Down
2 changes: 1 addition & 1 deletion L4d2AddonsMgr/App.config
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
</startup>
</configuration>
25 changes: 24 additions & 1 deletion L4d2AddonsMgr/L4d2AddonsMgr.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<OutputType>WinExe</OutputType>
<RootNamespace>L4d2AddonsMgr</RootNamespace>
<AssemblyName>L4d2AddonsMgr</AssemblyName>
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
<TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<ProjectTypeGuids>{60dc8134-eba5-43b8-bcc9-bb4bc16c2548};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
<WarningLevel>4</WarningLevel>
Expand Down Expand Up @@ -37,6 +37,28 @@
<PropertyGroup>
<StartupObject>L4d2AddonsMgr.App</StartupObject>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'">
<DebugSymbols>true</DebugSymbols>
<OutputPath>bin\x64\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<DebugType>full</DebugType>
<PlatformTarget>x64</PlatformTarget>
<LangVersion>7.3</LangVersion>
<ErrorReport>prompt</ErrorReport>
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
<Prefer32Bit>true</Prefer32Bit>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'">
<OutputPath>bin\x64\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<Optimize>true</Optimize>
<DebugType>pdbonly</DebugType>
<PlatformTarget>x64</PlatformTarget>
<LangVersion>7.3</LangVersion>
<ErrorReport>prompt</ErrorReport>
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
<Prefer32Bit>true</Prefer32Bit>
</PropertyGroup>
<ItemGroup>
<Reference Include="ChnCharInfo, Version=1.0.0.0, Culture=neutral, PublicKeyToken=18f031bd02e5e291, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.PinYinConverter.1.0.0\lib\ChnCharInfo.dll</HintPath>
Expand Down Expand Up @@ -119,6 +141,7 @@
<Compile Include="MeowTask\MeowTask.cs" />
<Compile Include="RealFolderPicker\FolderPicker.cs" />
<Compile Include="Utils\CStringUtil.cs" />
<Compile Include="Utils\DependencyObjectExtension.cs" />
<Compile Include="Utils\FileInodeComparer.cs" />
<Compile Include="Utils\LocalizationUtil.cs" />
<Compile Include="Utils\ProcessExtensions.cs" />
Expand Down
6 changes: 6 additions & 0 deletions L4d2AddonsMgr/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,12 @@

<ListBox ScrollViewer.HorizontalScrollBarVisibility="Disabled" BorderThickness="0"
Name="AddonsListBox" ItemsSource="{Binding Addons.Files}" Grid.Row="2"
PreviewMouseLeftButtonDown="AddonsListBox_PreviewMouseLeftButtonDown"
PreviewMouseLeftButtonUp="AddonsListBox_PreviewMouseLeftButtonUp"
PreviewMouseMove="AddonsListBox_PreviewMouseMove"
AllowDrop="False" DragOver="AddonsListBox_DragValidate"
DragEnter="AddonsListBox_DragValidate"
Drop="AddonsListBox_Drop"
SelectionMode="Extended">

<!--Templating? More like html than Android's adapter view.
Expand Down
79 changes: 74 additions & 5 deletions L4d2AddonsMgr/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Interop;
using Fluent;
Expand All @@ -13,6 +14,7 @@
using L4d2AddonsMgr.MeowTaskSpace;
using L4d2AddonsMgr.OperationSpace;
using L4d2AddonsMgr.RealFolderPicker;
using L4d2AddonsMgr.Utils;

namespace L4d2AddonsMgr {
/// <summary>
Expand Down Expand Up @@ -64,9 +66,14 @@ public partial class MainWindow : RibbonWindow {

private AddonsListTxt addonsList;

private Point dragPoint;

private DataObject dragObj;

public MainWindow() {
InitializeComponent();
libraryPath = null;
dragPoint = new Point(-1.0, 0.0);
}

public MainWindow(string libraryPath) {
Expand Down Expand Up @@ -232,7 +239,8 @@ private async void RefreshListCommand_Invoke(object sender, ExecutedRoutedEventA
private void ToggleFilterCommand_CanInvoke(object sender, CanExecuteRoutedEventArgs e) => e.CanExecute = (null != Addons) ? !Addons.IsLoading : false;

private void ToggleFilterCommand_Invoke(object sender, ExecutedRoutedEventArgs e) {
if (e.Parameter is AddonsCollection.VpkFilter filter) Addons.ToggleFilter(filter);
if (e.Parameter is AddonsCollection.VpkFilter filter)
Addons.ToggleFilter(filter);
}

private void ToggleFilterDownloadSourceCommand_Invoke(object sender, ExecutedRoutedEventArgs e) {
Expand All @@ -259,10 +267,12 @@ private void OpAutoRenameCommand_Invoke(object sender, ExecutedRoutedEventArgs e
// https://stackoverflow.com/questions/541194/c-sharp-version-of-javas-synchronized-keyword
lock (AddonsListBox.SelectedItems) {
list = new List<VpkHolder>(AddonsListBox.SelectedItems.Count);
foreach (object o in AddonsListBox.SelectedItems) list.Add((VpkHolder)o);
foreach (object o in AddonsListBox.SelectedItems)
list.Add((VpkHolder)o);
}
break;
default: return;
default:
return;
}
Addons.IsLoading = true;
var renameTask = new AutoRenameTask(list, dialog.Model, gameDir, addonsList);
Expand All @@ -274,7 +284,8 @@ private void OpAutoRenameCommand_Invoke(object sender, ExecutedRoutedEventArgs e

// How would it be known by just guessing.
// https://www.wpf-tutorial.com/dialogs/the-messagebox/
if (res2 ?? false) MessageBox.Show("已完成。", "自动重命名");
if (res2 ?? false)
MessageBox.Show("已完成。", "自动重命名");
Addons.IsLoading = false;
}
}
Expand Down Expand Up @@ -322,10 +333,68 @@ private void UpdateAccentColor() {

private void LogErrorAndQuit(string text, Exception exception) {
MessageBox.Show(text ?? string.Format("应用程序发生错误:{0}", exception.Message));
if (text != null) Debug.WriteLine(text);
if (text != null)
Debug.WriteLine(text);
Debug.WriteLine(exception);
Close();
}

private void AddonsListBox_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e) {
dragPoint = e.GetPosition(null);
}

private void AddonsListBox_PreviewMouseMove(object sender, MouseEventArgs e) {
if (dragPoint.X < 0.0)
return;
var pt = e.GetPosition(null);
double a = pt.X - dragPoint.X;
double b = pt.Y - dragPoint.Y;
if (a * a + b * b > SystemParameters.MinimumHorizontalDragDistance * SystemParameters.MinimumHorizontalDragDistance) {
dragPoint = new Point(-1.0, 0.0);
var list = sender as ListBox;
var li =
((DependencyObject)e.OriginalSource).FindAnchestor<ListBoxItem>();
if (li == null)
return;
var holder = (VpkHolder)list.ItemContainerGenerator.ItemFromContainer(li);
var dobj = dragObj = new DataObject();
dobj.SetFileDropList(new System.Collections.Specialized.StringCollection() { holder.FileInf.FullName });
var ret = DragDrop.DoDragDrop(li, dobj, DragDropEffects.Copy | DragDropEffects.Move);
Console.WriteLine("ret = " + ret);
dragObj = null;
//if (ret.HasFlag(DragDropEffects.Move))
//Addons.ReloadFromLibrary();
}
}

private void AddonsListBox_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e) {
dragPoint = new Point(-1.0, 0.0);
}

private void AddonsListBox_DragValidate(object sender, DragEventArgs e) {
// No dropping to self.
if (dragObj != null) {
e.Effects = DragDropEffects.None;
} else if (e.Data.GetDataPresent(DataFormats.FileDrop))
e.Effects = DragDropEffects.Copy;
else
e.Effects = DragDropEffects.None;
e.Handled = true;
}

private void AddonsListBox_Drop(object sender, DragEventArgs e) {
// TODO support linking.
// TODO differentiate copy/move.
//if (e.Effects.HasFlag(DragDropEffects.Move)) {
// e.Data
// }else
MessageBox.Show("123");
if (e.Effects.HasFlag(DragDropEffects.Copy)) {
string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
foreach (string file in files) {
Console.WriteLine(file);
}
}
}
}
}
19 changes: 19 additions & 0 deletions L4d2AddonsMgr/Utils/DependencyObjectExtension.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System.Windows;
using System.Windows.Media;

namespace L4d2AddonsMgr.Utils {
public static class DependencyObjectExtension {

public static T FindAnchestor<T>(this DependencyObject current)
where T : DependencyObject {
do {
if (current is T) {
return (T)current;
}
current = VisualTreeHelper.GetParent(current);
}
while (current != null);
return null;
}
}
}
2 changes: 2 additions & 0 deletions L4d2AddonsMgrTest/L4d2AddonsMgrTest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
<TargetFramework>netcoreapp2.1</TargetFramework>

<IsPackable>false</IsPackable>

<Platforms>AnyCPU;x64</Platforms>
</PropertyGroup>

<ItemGroup>
Expand Down

0 comments on commit f81e752

Please sign in to comment.