VPet/VPet-Simulator.Windows/MainWindow.xaml.cs
2023-01-23 01:33:13 +08:00

172 lines
6.4 KiB
C#

using Steamworks;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using VPet_Simulator.Core;
using static VPet_Simulator.Core.GraphCore;
using Microsoft.Win32;
using System.Windows.Forms;
using MessageBox = System.Windows.MessageBox;
using ContextMenu = System.Windows.Forms.ContextMenu;
using MenuItem = System.Windows.Forms.MenuItem;
using Application = System.Windows.Application;
namespace VPet_Simulator.Windows
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
private NotifyIcon notifyIcon;
public MainWindow()
{
//判断是不是Steam用户,因为本软件会发布到Steam
//在 https://store.steampowered.com/app/1920960/VPet
try
{
SteamClient.Init(1920960, true);
SteamClient.RunCallbacks();
IsSteamUser = SteamClient.IsValid;
////同时看看有没有买dlc,如果有就添加dlc按钮
//if (Steamworks.SteamApps.IsDlcInstalled(1386450))
// dlcToolStripMenuItem.Visible = true;
}
catch
{
IsSteamUser = false;
}
//加载游戏设置
if (new FileInfo(AppDomain.CurrentDomain.BaseDirectory + @"\Setting.lps").Exists)
{
Set = new Setting(File.ReadAllText(AppDomain.CurrentDomain.BaseDirectory + @"\Setting.lps"));
}
else
Set = new Setting("Setting#VPET:|\n");
InitializeComponent();
//不存在就关掉
var modpath = new DirectoryInfo(ModPath + @"\0000_core\pet\vup");
if (!modpath.Exists)
{
MessageBox.Show("缺少模组Core,无法启动桌宠", "启动错误", MessageBoxButton.OK, MessageBoxImage.Error);
Close();
return;
}
Task.Run(GameLoad);
}
public void GameLoad()
{
//加载所有MOD
List<DirectoryInfo> Path = new List<DirectoryInfo>();
Path.AddRange(new DirectoryInfo(ModPath).EnumerateDirectories());
if (IsSteamUser)//如果是steam用户,尝试加载workshop
{
Dispatcher.Invoke(new Action(() => LoadingText.Content = "尝试加载 Steam Workshop"));
int i = 1;
while (true)
{
var page = Steamworks.Ugc.Query.ItemsReadyToUse.GetPageAsync(i++).Result;
if (page.HasValue && page.Value.ResultCount != 0)
{
foreach (Steamworks.Ugc.Item entry in page.Value.Entries)
{
if (entry.Directory != null)
Path.Add(new DirectoryInfo(entry.Directory));
}
}
else
{
break;
}
}
}
//加载mod
foreach (DirectoryInfo di in Path)
{
if (!File.Exists(di.FullName + @"\info.lps"))
continue;
Dispatcher.Invoke(new Action(() => LoadingText.Content = $"尝试加载 MOD数据: {di.Name}"));
CoreMODs.Add(new CoreMOD(di, this));
}
Dispatcher.Invoke(new Action(() => LoadingText.Content = "尝试加载游戏内容"));
//加载游戏内容
Core.Controller = new MWController(this);
Core.Save = new Save("萝莉斯");
Dispatcher.Invoke(new Action(() =>
{
Core.Graph = Pets[0].Graph();
LoadingText.Visibility = Visibility.Collapsed;
winSetting = new winGameSetting(this);
var main = new Main(Core) { };
main.DefaultClickAction = () => { Dispatcher.Invoke(() => { main.Say("你知道吗? 鼠标右键可以打开菜单栏"); }); };
DisplayGrid.Child = main;
main.ToolBar.AddMenuButton(VPet_Simulator.Core.ToolBar.MenuType.DIY, "退出桌宠", () => { Close(); });
//加载图标
notifyIcon = new NotifyIcon();
ContextMenu m_menu;
m_menu = new ContextMenu();
m_menu.MenuItems.Add(new MenuItem("重置", (x, y) => {
main.CleanState();
main.DisplayNomal();
}));
m_menu.MenuItems.Add(new MenuItem("居中", (x, y) =>
{
Left = (SystemParameters.PrimaryScreenWidth - Width) / 2;
Top = (SystemParameters.PrimaryScreenHeight - Height) / 2;
}));
m_menu.MenuItems.Add(new MenuItem("设置", (x, y) => Core.Controller.ShowSetting()));
m_menu.MenuItems.Add(new MenuItem("退出", (x, y) => Close()));
notifyIcon.ContextMenu = m_menu;
var streamResourceInfo = Application.GetResourceStream(new Uri("pack://application:,,,/vpeticon.ico"));
if (streamResourceInfo != null)
notifyIcon.Icon = new System.Drawing.Icon(streamResourceInfo.Stream);
notifyIcon.Visible = true;
//notifyIcon.ShowBalloonTip(5, "你好 " + Environment.UserName,
// "Press Alt+C to show Clock\nRight Click on Tray to Close", ToolTipIcon.Info);
}));
}
private void Window_Closed(object sender, EventArgs e)
{
((Main)DisplayGrid.Child).Dispose();
notifyIcon.Dispose();
System.Environment.Exit(0);
}
//public void DEBUGValue()
//{
// Dispatcher.Invoke(() =>
// {
// Console.WriteLine("Left: " + mwc.GetWindowsDistanceLeft());
// Console.WriteLine("Right: " + mwc.GetWindowsDistanceRight());
// });
// Thread.Sleep(1000);
// DEBUGValue();
//}
}
}