VPet/VPet-Simulator.Windows/MainWindow.xaml.cs

229 lines
8.8 KiB
C#
Raw Normal View History

2022-12-22 08:13:05 +00:00
using Steamworks;
using System;
2022-12-13 07:10:18 +00:00
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;
2023-01-20 17:01:10 +00:00
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;
2023-01-24 06:56:16 +00:00
using System.Timers;
2022-12-13 07:10:18 +00:00
namespace VPet_Simulator.Windows
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
2023-01-20 17:01:10 +00:00
private NotifyIcon notifyIcon;
2023-01-24 06:56:16 +00:00
public System.Timers.Timer AutoSaveTimer = new System.Timers.Timer();
2022-12-13 07:10:18 +00:00
public MainWindow()
{
2022-12-22 08:13:05 +00:00
//判断是不是Steam用户,因为本软件会发布到Steam
//在 https://store.steampowered.com/app/1920960/VPet
try
{
2023-01-23 17:31:16 +00:00
#if DEBUG
SteamClient.Init(2293870, true);
#else
2022-12-22 08:13:05 +00:00
SteamClient.Init(1920960, true);
2023-01-23 17:31:16 +00:00
#endif
2022-12-22 08:13:05 +00:00
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
2023-01-08 16:57:10 +00:00
Set = new Setting("Setting#VPET:|\n");
2023-01-24 09:06:06 +00:00
//this.Width = 400 * ZoomSlider.Value;
//this.Height = 450 * ZoomSlider.Value;
2023-01-27 17:44:57 +00:00
2022-12-13 07:10:18 +00:00
InitializeComponent();
2023-01-27 17:44:57 +00:00
this.Height = 1000 * Set.ZoomLevel;
this.Width = 1000 * Set.ZoomLevel;
2022-12-13 07:10:18 +00:00
//不存在就关掉
var modpath = new DirectoryInfo(ModPath + @"\0000_core\pet\vup");
if (!modpath.Exists)
{
MessageBox.Show("缺少模组Core,无法启动桌宠", "启动错误", MessageBoxButton.OK, MessageBoxImage.Error);
Close();
return;
}
2023-01-08 16:57:10 +00:00
Task.Run(GameLoad);
}
2023-01-25 16:23:09 +00:00
private long lastclicktime;
2023-01-08 16:57:10 +00:00
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 = "尝试加载游戏内容"));
2023-01-23 17:31:16 +00:00
2023-01-08 16:57:10 +00:00
//加载游戏内容
Core.Controller = new MWController(this);
Core.Save = new Save("萝莉斯");
2023-01-24 06:56:16 +00:00
AutoSaveTimer.Elapsed += AutoSaveTimer_Elapsed;
if (Set.AutoSaveInterval > 0)
{
AutoSaveTimer.Interval = Set.AutoSaveInterval * 60000;
AutoSaveTimer.Start();
}
2023-01-20 17:01:10 +00:00
Dispatcher.Invoke(new Action(() =>
{
2023-01-22 17:33:13 +00:00
Core.Graph = Pets[0].Graph();
2023-01-10 10:43:32 +00:00
LoadingText.Visibility = Visibility.Collapsed;
2023-01-20 17:01:10 +00:00
winSetting = new winGameSetting(this);
2023-01-24 06:56:16 +00:00
Main = new Main(Core) { };
2023-01-25 16:23:09 +00:00
Main.DefaultClickAction = () => {
if (new TimeSpan(DateTime.Now.Ticks - lastclicktime).TotalMinutes > 1)
{
lastclicktime = DateTime.Now.Ticks;
Dispatcher.Invoke(() => { Main.Say("你知道吗? 鼠标右键可以打开菜单栏"); });
}
};
2023-01-24 06:56:16 +00:00
DisplayGrid.Child = Main;
Main.ToolBar.AddMenuButton(VPet_Simulator.Core.ToolBar.MenuType.Setting, "退出桌宠", () => { Close(); });
2023-01-25 16:23:09 +00:00
Main.ToolBar.AddMenuButton(VPet_Simulator.Core.ToolBar.MenuType.Setting, "管理控制台", () => { new winConsole(this).Show(); });
2023-01-24 07:55:02 +00:00
Main.ToolBar.AddMenuButton(VPet_Simulator.Core.ToolBar.MenuType.Setting, "反馈中心", () => { new winReport(this).Show(); });
2023-01-24 06:56:16 +00:00
Main.ToolBar.AddMenuButton(VPet_Simulator.Core.ToolBar.MenuType.Setting, "设置面板", () =>
2023-01-24 07:55:02 +00:00
{
Topmost = false;
winSetting.Show();
});
2023-01-20 17:01:10 +00:00
2023-01-24 06:56:16 +00:00
Main.SetMoveMode(Set.AllowMove, Set.SmartMove, Set.SmartMoveInterval * 1000);
Main.SetLogicInterval((int)(Set.LogicInterval * 1000));
2023-01-20 17:01:10 +00:00
//加载图标
notifyIcon = new NotifyIcon();
2023-01-24 09:06:06 +00:00
notifyIcon.Text = "虚拟桌宠模拟器";
2023-01-20 17:01:10 +00:00
ContextMenu m_menu;
m_menu = new ContextMenu();
2023-01-23 17:31:16 +00:00
m_menu.MenuItems.Add(new MenuItem("重置状态", (x, y) =>
{
2023-01-24 06:56:16 +00:00
Main.CleanState();
Main.DisplayNomal();
2023-01-20 17:01:10 +00:00
Left = (SystemParameters.PrimaryScreenWidth - Width) / 2;
Top = (SystemParameters.PrimaryScreenHeight - Height) / 2;
}));
2023-01-24 07:55:02 +00:00
m_menu.MenuItems.Add(new MenuItem("反馈中心", (x, y) => { new winReport(this).Show(); }));
2023-01-23 17:31:16 +00:00
m_menu.MenuItems.Add(new MenuItem("设置面板", (x, y) =>
{
Topmost = false;
winSetting.Show();
}));
m_menu.MenuItems.Add(new MenuItem("退出桌宠", (x, y) => Close()));
2023-01-20 17:01:10 +00:00
notifyIcon.ContextMenu = m_menu;
2023-01-23 17:31:16 +00:00
notifyIcon.Icon = new System.Drawing.Icon(Application.GetResourceStream(new Uri("pack://application:,,,/vpeticon.ico")).Stream);
2023-01-20 17:01:10 +00:00
notifyIcon.Visible = true;
2023-01-23 17:31:16 +00:00
if (Set["SingleTips"].GetBool("helloworld"))
{
Set["SingleTips"].SetBool("helloworld", true);
notifyIcon.ShowBalloonTip(10, "你好 " + (IsSteamUser ? Steamworks.SteamClient.Name : Environment.UserName),
"欢迎使用虚拟桌宠模拟器!\n如果遇到桌宠爬不见了,可以在我这里设置居中或退出桌宠", ToolTipIcon.Info);
Task.Run(() =>
{
Thread.Sleep(1000);
2023-01-24 06:56:16 +00:00
Main.Say("欢迎使用虚拟桌宠模拟器\n这是个早期的测试版,若有bug请多多包涵\n欢迎在菜单栏-管理-反馈中提交bug或建议");
2023-01-23 17:31:16 +00:00
});
}
2023-01-10 10:43:32 +00:00
}));
2022-12-13 07:10:18 +00:00
}
2023-01-08 16:57:10 +00:00
2023-01-24 06:56:16 +00:00
private void AutoSaveTimer_Elapsed(object sender, ElapsedEventArgs e)
{
Save();
}
public Main Main;
2023-01-20 17:01:10 +00:00
2023-01-12 22:11:55 +00:00
private void Window_Closed(object sender, EventArgs e)
{
2023-01-24 06:56:16 +00:00
Save();
Main?.Dispose();
2023-01-24 09:06:06 +00:00
notifyIcon.Visible = false;
2023-01-23 17:31:16 +00:00
notifyIcon?.Dispose();
2023-01-20 17:01:10 +00:00
System.Environment.Exit(0);
2023-01-12 22:11:55 +00:00
}
2022-12-13 07:10:18 +00:00
//public void DEBUGValue()
//{
// Dispatcher.Invoke(() =>
// {
// Console.WriteLine("Left: " + mwc.GetWindowsDistanceLeft());
// Console.WriteLine("Right: " + mwc.GetWindowsDistanceRight());
// });
// Thread.Sleep(1000);
// DEBUGValue();
//}
}
}