添加 保存和读取最近打开的内容

This commit is contained in:
Hakoyu 2023-09-03 18:16:49 +08:00
parent cfc81f7650
commit 45a9c5bed5
7 changed files with 112 additions and 40 deletions

View File

@ -1,20 +0,0 @@
using LinePutScript.Converter;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace VPet.ModMaker.Models;
public class ModMakeHistory
{
[Line(ignoreCase: true)]
public string Name { get; set; }
[Line(ignoreCase: true)]
public string Path { get; set; }
[Line(ignoreCase: true)]
public DateTime LastOpenTime { get; set; }
}

View File

@ -0,0 +1,38 @@
using LinePutScript.Converter;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media.Imaging;
namespace VPet.ModMaker.Models;
public class ModMakerHistory
{
public BitmapImage Image { get; set; }
[Line(ignoreCase: true)]
public string Name { get; set; }
private string _path;
[Line(ignoreCase: true)]
public string SourcePath
{
get => _path;
set
{
_path = value;
var imagePath = Path.Combine(_path, "icon.png");
if (File.Exists(imagePath))
Image = Utils.LoadImageToMemoryStream(imagePath);
}
}
public string LastTimeString => LastTime.ToString("yyyy/MM/dd HH:mm:ss");
[Line(ignoreCase: true)]
public DateTime LastTime { get; set; }
}

View File

@ -8,7 +8,6 @@ namespace VPet.ModMaker.Models;
public static class ModMakerInfo
{
// TODO: 可修改的主要文化 ?
public const string MainCulture = "zh-Hans";
public const string BaseDirectory = nameof(ModMaker);
public const string HistoryFile = $"{BaseDirectory}\\history.lps";
}

View File

@ -97,7 +97,7 @@
<Compile Include="Models\I18nModel.cs" />
<Compile Include="Models\LowTextModel.cs" />
<Compile Include="Models\ModLoader.cs" />
<Compile Include="Models\ModMakeHistory.cs" />
<Compile Include="Models\ModMakerHistory.cs" />
<Compile Include="Models\ModMakerInfo.cs" />
<Compile Include="Models\PetModel.cs" />
<Compile Include="ViewModels\ModEdit\ClickTextEdit\ClickTextEditWindowVM.cs" />

View File

@ -1,4 +1,6 @@
using HKW.HKWViewModels.SimpleObservable;
using LinePutScript;
using LinePutScript.Converter;
using LinePutScript.Localization.WPF;
using Microsoft.Win32;
using System;
@ -26,6 +28,8 @@ public class ModMakerWindowVM
public ObservableCollection<ModInfoModel> ShowMods { get; set; }
public ObservableCollection<ModInfoModel> Mods { get; } = new();
public ObservableCollection<ModMakerHistory> Histories { get; } = new();
#endregion
#region Command
public ObservableCommand CreateNewModCommand { get; } = new();
@ -35,7 +39,7 @@ public class ModMakerWindowVM
public ModMakerWindowVM(ModMakerWindow window)
{
LoadMods();
LoadHistories();
ModMakerWindow = window;
ShowMods = Mods;
CreateNewModCommand.ExecuteAction = CreateNewMod;
@ -43,16 +47,49 @@ public class ModMakerWindowVM
ModFilterText.ValueChanged += ModFilterText_ValueChanged;
}
private void LoadMods()
private void LoadHistories()
{
var dic = Directory.CreateDirectory(ModMakerInfo.BaseDirectory);
foreach (var modDic in dic.EnumerateDirectories())
if (File.Exists(ModMakerInfo.HistoryFile) is false)
return;
var lps = new LPS(File.ReadAllText(ModMakerInfo.HistoryFile));
foreach (var line in lps)
Histories.Add(LPSConvert.DeserializeObject<ModMakerHistory>(line));
}
private void SaveHistories()
{
if (File.Exists(ModMakerInfo.HistoryFile) is false)
File.Create(ModMakerInfo.HistoryFile).Close();
var lps = new LPS();
foreach (var history in Histories)
lps.Add(
new Line(nameof(history))
{
new Sub("Name", history.Name),
new Sub("SourcePath", history.SourcePath),
new Sub("LastTime", history.LastTimeString)
}
);
File.WriteAllText(ModMakerInfo.HistoryFile, lps.ToString());
}
private void AddHistories(ModInfoModel modInfo)
{
if (Histories.FirstOrDefault(h => h.Name == modInfo.Name.Value) is ModMakerHistory history)
{
var mod = new ModLoader(modDic);
if (mod.SuccessLoad is false)
continue;
var modModel = new ModInfoModel(mod);
Mods.Add(modModel);
history.SourcePath = modInfo.SourcePath.Value;
history.LastTime = DateTime.Now;
}
else
{
Histories.Add(
new()
{
Name = modInfo.Name.Value,
SourcePath = modInfo.SourcePath.Value,
LastTime = DateTime.Now,
}
);
}
}
@ -67,12 +104,15 @@ public class ModMakerWindowVM
public void CreateNewMod()
{
ModInfoModel.Current ??= new();
// I18nHelper.Current = new();
ModEditWindow = new();
ModEditWindow.Show();
ModMakerWindow.Hide();
ModEditWindow.Closed += (s, e) =>
{
var modInfo = ModInfoModel.Current;
if (string.IsNullOrEmpty(modInfo.SourcePath.Value) is false)
AddHistories(modInfo);
SaveHistories();
ModMakerWindow.Close();
};
}

View File

@ -32,7 +32,7 @@
<ListBox
Grid.Row="2"
d:ItemsSource="{d:SampleData ItemCount=5}"
ItemsSource="{Binding Mods}"
ItemsSource="{Binding Histories}"
Style="{StaticResource SideMenuListBoxStyle}">
<ListBox.ItemContainerStyle>
<Style BasedOn="{StaticResource {x:Type ListBoxItem}}" TargetType="ListBoxItem">
@ -41,12 +41,12 @@
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<Grid ToolTip="{Binding Summary.Value}">
<Grid Background="White">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Image Width="64" Source="{Binding Image.Value}" />
<Image Width="64" Source="{Binding Image}" />
<Grid Grid.Column="1">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
@ -54,12 +54,18 @@
</Grid.RowDefinitions>
<TextBlock
d:Text="{ll:Str Mod名称}"
Text="{Binding Name.Value}"
FontWeight="Bold"
Text="{Binding Name}"
TextWrapping="Wrap" />
<TextBlock
HorizontalAlignment="Right"
VerticalAlignment="Top"
Text="{Binding LastTimeString}" />
<TextBlock
Grid.Row="1"
VerticalAlignment="Bottom"
d:Text="{ll:Str Mod描述}"
Text="{Binding Description.Value}"
Text="{Binding SourcePath}"
TextWrapping="Wrap" />
</Grid>
</Grid>

View File

@ -1,4 +1,5 @@
using LinePutScript;
using LinePutScript.Localization.WPF;
using System;
using System.Collections.Generic;
using System.Linq;
@ -39,7 +40,15 @@ public partial class ModMakerWindow : Window
{
if (sender is not ListBoxItem item)
return;
ModInfoModel.Current = (ModInfoModel)item.DataContext;
ViewModel.CreateNewMod();
if (item.DataContext is not ModMakerHistory history)
return;
var loader = new ModLoader(new(history.SourcePath));
if (loader.SuccessLoad)
{
ModInfoModel.Current = new(loader);
ViewModel.CreateNewMod();
}
else
MessageBox.Show($"载入失败".Translate());
}
}