2024-03-07 10:41:18 +00:00
|
|
|
|
using System;
|
2024-01-15 20:46:14 +00:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Collections.ObjectModel;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
2024-02-04 09:37:45 +00:00
|
|
|
|
using System.Windows;
|
2024-01-15 20:46:14 +00:00
|
|
|
|
using System.Windows.Media;
|
2024-03-07 10:41:18 +00:00
|
|
|
|
using HKW.HKWUtils.Observable;
|
|
|
|
|
using LinePutScript;
|
|
|
|
|
using LinePutScript.Localization.WPF;
|
2024-02-04 09:37:45 +00:00
|
|
|
|
using VPet.Solution.Views.SettingEditor;
|
2024-03-09 18:05:27 +00:00
|
|
|
|
using VPet_Simulator.Windows;
|
2024-01-15 20:46:14 +00:00
|
|
|
|
|
|
|
|
|
namespace VPet.Solution.Models.SettingEditor;
|
|
|
|
|
|
|
|
|
|
public class ModSettingModel : ObservableClass<ModSettingModel>
|
|
|
|
|
{
|
|
|
|
|
public const string ModLineName = "onmod";
|
|
|
|
|
public const string PassModLineName = "passmod";
|
|
|
|
|
public const string MsgModLineName = "msgmod";
|
2024-01-18 19:44:39 +00:00
|
|
|
|
public const string WorkShopLineName = "workshop";
|
2024-01-15 20:46:14 +00:00
|
|
|
|
public static string ModDirectory = Path.Combine(Environment.CurrentDirectory, "mod");
|
2024-03-07 10:41:18 +00:00
|
|
|
|
public static Dictionary<string, ModLoader> LocalMods { get; private set; } = null!;
|
2024-02-04 09:37:45 +00:00
|
|
|
|
|
2024-01-15 20:46:14 +00:00
|
|
|
|
#region Mods
|
|
|
|
|
private ObservableCollection<ModModel> _mods = new();
|
|
|
|
|
public ObservableCollection<ModModel> Mods
|
|
|
|
|
{
|
|
|
|
|
get => _mods;
|
|
|
|
|
set => SetProperty(ref _mods, value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ModSettingModel(Setting setting)
|
|
|
|
|
{
|
2024-02-04 09:37:45 +00:00
|
|
|
|
LocalMods ??= GetLocalMods();
|
2024-01-15 20:46:14 +00:00
|
|
|
|
foreach (var item in setting[ModLineName])
|
|
|
|
|
{
|
2024-02-20 10:45:02 +00:00
|
|
|
|
var modID = item.Name;
|
|
|
|
|
if (LocalMods.TryGetValue(modID, out var loader) && loader.IsSuccesses)
|
2024-01-15 20:46:14 +00:00
|
|
|
|
{
|
|
|
|
|
var modModel = new ModModel(loader);
|
2024-02-20 10:45:02 +00:00
|
|
|
|
modModel.IsMsg = setting[MsgModLineName].GetBool(modModel.ID);
|
|
|
|
|
modModel.IsPass = setting[PassModLineName].Contains(modID);
|
2024-01-15 20:46:14 +00:00
|
|
|
|
Mods.Add(modModel);
|
|
|
|
|
}
|
|
|
|
|
else
|
2024-01-18 20:13:27 +00:00
|
|
|
|
{
|
|
|
|
|
Mods.Add(
|
|
|
|
|
new()
|
|
|
|
|
{
|
2024-02-20 10:45:02 +00:00
|
|
|
|
Name = modID,
|
|
|
|
|
ModPath = "未知, 可能是{0}".Translate(Path.Combine(ModDirectory, modID))
|
2024-01-18 20:13:27 +00:00
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
}
|
2024-01-15 20:46:14 +00:00
|
|
|
|
}
|
2024-01-18 19:44:39 +00:00
|
|
|
|
foreach (var modPath in setting[WorkShopLineName])
|
|
|
|
|
{
|
|
|
|
|
var loader = new ModLoader(modPath.Name);
|
2024-02-20 10:45:02 +00:00
|
|
|
|
if (loader.IsSuccesses)
|
|
|
|
|
{
|
|
|
|
|
var modModel = new ModModel(loader);
|
|
|
|
|
modModel.IsMsg = setting[MsgModLineName].GetBool(modModel.ID);
|
|
|
|
|
modModel.IsPass = setting[PassModLineName].Contains(modModel.ID.ToLower());
|
|
|
|
|
Mods.Add(modModel);
|
|
|
|
|
}
|
|
|
|
|
else
|
2024-01-18 20:13:27 +00:00
|
|
|
|
{
|
|
|
|
|
Mods.Add(new() { Name = loader.Name, ModPath = loader.ModPath });
|
|
|
|
|
}
|
2024-01-18 19:44:39 +00:00
|
|
|
|
}
|
2024-01-15 20:46:14 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-02-04 09:37:45 +00:00
|
|
|
|
private static Dictionary<string, ModLoader> GetLocalMods()
|
|
|
|
|
{
|
|
|
|
|
var dic = new Dictionary<string, ModLoader>(StringComparer.OrdinalIgnoreCase);
|
|
|
|
|
foreach (var dir in Directory.EnumerateDirectories(ModDirectory))
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var loader = new ModLoader(dir);
|
|
|
|
|
dic.TryAdd(loader.Name, loader);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
MessageBox.Show("模组载入错误\n路径:{0}\n异常:{1}".Translate(dir, ex));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return dic;
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-15 20:46:14 +00:00
|
|
|
|
public void Close()
|
|
|
|
|
{
|
|
|
|
|
foreach (var modLoader in LocalMods)
|
|
|
|
|
{
|
|
|
|
|
modLoader.Value.Image.CloseStream();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Save(Setting setting)
|
|
|
|
|
{
|
|
|
|
|
setting.Remove(ModLineName);
|
|
|
|
|
setting.Remove(PassModLineName);
|
|
|
|
|
setting.Remove(MsgModLineName);
|
|
|
|
|
if (Mods.Any() is false)
|
|
|
|
|
return;
|
|
|
|
|
foreach (var mod in Mods)
|
|
|
|
|
{
|
2024-02-20 10:45:02 +00:00
|
|
|
|
setting[ModLineName].Add(new Sub(mod.ID.ToLower()));
|
|
|
|
|
setting[MsgModLineName].Add(new Sub(mod.ID, "True"));
|
2024-01-15 20:46:14 +00:00
|
|
|
|
if (mod.IsPass)
|
2024-02-20 10:45:02 +00:00
|
|
|
|
setting[PassModLineName].Add(new Sub(mod.ID.ToLower()));
|
2024-01-15 20:46:14 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#endregion
|
|
|
|
|
}
|