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

537 lines
20 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.Threading;
using System.Threading.Tasks;
using System.Windows;
using VPet_Simulator.Core;
2023-01-20 17:01:10 +00:00
using System.Windows.Forms;
2023-01-24 06:56:16 +00:00
using System.Timers;
2023-02-18 03:49:13 +00:00
using LinePutScript;
2023-04-01 15:15:50 +00:00
using Panuon.WPF.UI;
using VPet_Simulator.Windows.Interface;
2023-06-16 03:39:29 +00:00
using System.Linq;
2023-07-01 07:46:08 +00:00
using LinePutScript.Localization.WPF;
2023-07-03 11:41:16 +00:00
using System.Runtime.InteropServices;
using System.Windows.Interop;
using static VPet_Simulator.Windows.PerformanceDesktopTransparentWindow;
using Line = LinePutScript.Line;
2023-07-16 23:58:09 +00:00
using static VPet_Simulator.Core.GraphInfo;
2023-08-14 18:30:36 +00:00
using System.Globalization;
using LinePutScript.Dictionary;
2024-01-25 14:40:50 +00:00
using Steamworks.Data;
2022-12-13 07:10:18 +00:00
namespace VPet_Simulator.Windows
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
2023-04-01 15:15:50 +00:00
public partial class MainWindow : WindowX
2022-12-13 07:10:18 +00:00
{
2023-01-20 17:01:10 +00:00
private NotifyIcon notifyIcon;
2023-07-03 11:41:16 +00:00
public PetHelper petHelper;
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()
{
//处理ARGS
Args = new LPS_D();
foreach (var str in App.Args)
{
Args.Add(new Line(str));
}
//存档前缀
if (Args.ContainsLine("prefix"))
2023-10-11 17:24:35 +00:00
{
2023-10-10 17:03:23 +00:00
PrefixSave = '-' + Args["prefix"].Info;
2023-10-11 17:24:35 +00:00
}
#if X64
2023-08-31 17:43:10 +00:00
PNGAnimation.MaxLoadNumber = 50;
#else
PNGAnimation.MaxLoadNumber = 20;
#endif
ExtensionValue.BaseDirectory = new FileInfo(System.Reflection.Assembly.GetExecutingAssembly().Location).DirectoryName;
2023-07-01 07:46:08 +00:00
LocalizeCore.StoreTranslation = true;
2023-08-14 18:30:36 +00:00
CultureInfo.CurrentCulture = new CultureInfo(CultureInfo.CurrentCulture.Name);
CultureInfo.CurrentCulture.NumberFormat = new CultureInfo("en-US").NumberFormat;
2022-12-22 08:13:05 +00:00
//判断是不是Steam用户,因为本软件会发布到Steam
//在 https://store.steampowered.com/app/1920960/VPet
try
{
2023-01-29 08:27:24 +00:00
#if DEMO
2023-04-01 19:31:28 +00:00
SteamClient.Init(2293870, true);
2023-01-23 17:31:16 +00:00
#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))
2023-03-28 13:18:14 +00:00
// dlcToolStripMenuItem.Visible = true;
2022-12-22 08:13:05 +00:00
}
catch
{
IsSteamUser = false;
}
//更新存档系统
if (Directory.Exists(ExtensionValue.BaseDirectory + @"\BackUP"))
2022-12-22 08:13:05 +00:00
{
if (!Directory.Exists(ExtensionValue.BaseDirectory + @"\Saves"))
Directory.Move(ExtensionValue.BaseDirectory + @"\BackUP", ExtensionValue.BaseDirectory + @"\Saves");
2023-08-25 17:07:14 +00:00
else
{
foreach (var file in new DirectoryInfo(ExtensionValue.BaseDirectory + @"\BackUP").GetFiles())
if (!File.Exists(ExtensionValue.BaseDirectory + @"\Saves\" + file.Name))
file.MoveTo(ExtensionValue.BaseDirectory + @"\Saves\" + file.Name);
else
file.Delete();
Directory.Delete(ExtensionValue.BaseDirectory + @"\BackUP", true);
}
}
2023-07-03 11:41:16 +00:00
_dwmEnabled = Win32.Dwmapi.DwmIsCompositionEnabled();
_hwnd = new WindowInteropHelper(this).EnsureHandle();
2023-07-03 11:41:16 +00:00
GameInitialization();
2022-12-13 07:10:18 +00:00
Task.Run(() =>
{
//加载所有MOD
List<DirectoryInfo> Path = new List<DirectoryInfo>();
Path.AddRange(new DirectoryInfo(ModPath).EnumerateDirectories());
2023-01-28 06:12:01 +00:00
bool NOCancel = true;
CancellationTokenSource source = new CancellationTokenSource();
var tsk = Task.Run(async () =>
2023-04-03 18:11:12 +00:00
{
if (IsSteamUser)//如果是steam用户,尝试加载workshop
{
2024-01-25 14:40:50 +00:00
//Leaderboard? leaderboard = await SteamUserStats.FindLeaderboardAsync("chatgpt_auth");
//leaderboard?.ReplaceScore(Function.Rnd.Next());
var workshop = new Line_D("workshop");
await Dispatcher.InvokeAsync(new Action(() =>
{
LoadingText.Content = "Loading Steam Workshop\nDouble Click To Skip";
LoadingText.MouseDoubleClick += (_, _) =>
{
if ((string)LoadingText.Content == "Loading Steam Workshop\nDouble Click To Skip")
{
NOCancel = false;
}
};
}));
int i = 1;
while (true)
{
2024-01-25 14:40:50 +00:00
var page = await Steamworks.Ugc.Query.ItemsReadyToUse.GetPageAsync(i++);
if (page.HasValue && page.Value.ResultCount != 0)
{
foreach (Steamworks.Ugc.Item entry in page.Value.Entries)
{
2023-11-14 13:24:57 +00:00
if (!NOCancel)
{
return;
}
if (entry.Directory != null)
{
Path.Add(new DirectoryInfo(entry.Directory));
workshop.Add(new Sub(entry.Directory, ""));
}
}
}
else
{
break;
}
}
if (workshop.Count != 0)
Set["workshop"] = workshop;
}
else
2023-08-25 17:07:14 +00:00
{
var workshop = Set["workshop"];
foreach (Sub ws in workshop)
{
Path.Add(new DirectoryInfo(ws.Name));
}
2023-08-25 17:07:14 +00:00
}
}, source.Token);
while (NOCancel && !tsk.IsCompleted)
2023-08-25 17:07:14 +00:00
{
Thread.Sleep(500);
2023-04-03 18:11:12 +00:00
}
if (!NOCancel)
2023-09-04 05:01:16 +00:00
{
source.Cancel();
var workshop = Set["workshop"];
foreach (Sub ws in workshop)
{
Path.Add(new DirectoryInfo(ws.Name));
}
2023-09-04 05:01:16 +00:00
}
2023-04-03 18:11:12 +00:00
2023-11-24 07:10:30 +00:00
Dispatcher.InvokeAsync(new Action(() => LoadingText.Content = "Loading Translate")).Wait();
//加载语言
LocalizeCore.StoreTranslation = true;
if (Set.Language == "null")
2023-08-25 17:07:14 +00:00
{
LocalizeCore.LoadDefaultCulture();
if (LocalizeCore.CurrentCulture == "null")
LocalizeCore.CurrentCulture = "en";
Set.Language = LocalizeCore.CurrentCulture;
2023-08-25 17:07:14 +00:00
}
else
LocalizeCore.LoadCulture(Set.Language);
2023-09-10 17:32:13 +00:00
//旧版本设置兼容
var cgpte = Set.FindLine("CGPT");
if (cgpte != null)
2023-09-24 13:17:48 +00:00
{
var cgpteb = cgpte.Find("enable");
if (cgpteb != null)
{
if (Set["CGPT"][(gbol)"enable"])
{
Set["CGPT"][(gstr)"type"] = "API";
}
else
{
Set["CGPT"][(gstr)"type"] = "LB";
}
Set["CGPT"].Remove(cgpteb);
}
}
else if (Set["CGPT"][(gstr)"type"] == "OFF")
{//为老玩家开启选项聊天功能
Set["CGPT"][(gstr)"type"] = "LB";
}
else//新玩家,默认设置为
Set["CGPT"][(gstr)"type"] = "LB";
2023-09-24 13:17:48 +00:00
GameLoad(Path);
});
2023-01-08 16:57:10 +00:00
}
2023-10-11 17:24:35 +00:00
2023-10-13 12:02:48 +00:00
2023-03-07 08:11:23 +00:00
public new void Close()
{
if (Main == null)
{
base.Close();
}
else
{
2023-07-16 23:58:09 +00:00
Main.Display(GraphType.Shutdown, AnimatType.Single, () => Dispatcher.Invoke(base.Close));
2023-03-07 08:11:23 +00:00
}
}
public void Restart()
{
this.Closed -= Window_Closed;
this.Closed += Restart_Closed;
base.Close();
}
2023-07-03 11:41:16 +00:00
2023-03-07 08:11:23 +00:00
private void Restart_Closed(object sender, EventArgs e)
{
2023-08-12 07:13:30 +00:00
CloseConfirm = false;
2023-04-01 19:31:28 +00:00
try
{
//关闭所有插件
foreach (MainPlugin mp in Plugins)
mp.EndGame();
}
catch { }
2023-08-16 03:09:44 +00:00
Save();
2023-10-13 15:31:52 +00:00
if (App.MainWindows.Count == 1)
System.Diagnostics.Process.Start(System.Reflection.Assembly.GetExecutingAssembly().Location);
else
{
new MainWindow(PrefixSave).Show();
}
2023-08-16 03:09:44 +00:00
Exit();
}
private void Exit()
{
if (App.MainWindows.Count <= 1)
2023-08-16 03:09:44 +00:00
{
2023-10-13 15:31:52 +00:00
try
2024-01-25 14:40:50 +00:00
{
if (Core != null && Core.Graph != null)
2023-08-16 03:09:44 +00:00
{
2023-10-13 15:31:52 +00:00
foreach (var igs in Core.Graph.GraphsList.Values)
2023-08-16 03:09:44 +00:00
{
2023-10-13 15:31:52 +00:00
foreach (var ig2 in igs.Values)
2023-08-17 08:39:26 +00:00
{
2023-10-13 15:31:52 +00:00
foreach (var ig3 in ig2)
{
ig3.Stop();
}
2023-08-17 08:39:26 +00:00
}
2023-08-16 03:09:44 +00:00
}
}
2023-11-20 15:57:54 +00:00
while (Windows.Count != 0)
2023-10-13 15:31:52 +00:00
{
2023-11-20 15:57:54 +00:00
Windows[0].Close();
2023-10-13 15:31:52 +00:00
}
2023-11-20 15:57:54 +00:00
Main?.Dispose();
2023-10-13 15:31:52 +00:00
AutoSaveTimer?.Stop();
MusicTimer?.Stop();
petHelper?.Close();
2023-11-20 15:57:54 +00:00
winSetting?.Close();
winBetterBuy?.Close();
if (IsSteamUser)
SteamClient.Shutdown();//关掉和Steam的连线
2023-10-13 15:31:52 +00:00
if (notifyIcon != null)
{
notifyIcon.Visible = false;
notifyIcon.Dispose();
}
notifyIcon?.Dispose();
2023-08-16 03:09:44 +00:00
}
2023-10-13 15:31:52 +00:00
finally
2023-08-17 08:39:26 +00:00
{
2023-10-13 15:31:52 +00:00
Environment.Exit(0);
2023-08-17 08:39:26 +00:00
}
2023-10-13 15:31:52 +00:00
while (true)
Environment.Exit(0);
2023-08-16 03:09:44 +00:00
}
2023-10-13 15:31:52 +00:00
else
2023-10-15 13:12:00 +00:00
{
2023-11-20 15:57:54 +00:00
if (Core != null && Core.Graph != null)
{
foreach (var igs in Core.Graph.GraphsList.Values)
{
foreach (var ig2 in igs.Values)
{
foreach (var ig3 in ig2)
{
ig3.Stop();
}
}
}
}
while (Windows.Count != 0)
{
Windows[0].Close();
}
2023-11-20 15:57:54 +00:00
Main?.Dispose();
AutoSaveTimer?.Stop();
MusicTimer?.Stop();
petHelper?.Close();
2023-10-15 13:12:00 +00:00
winSetting?.Close();
winBetterBuy?.Close();
2023-10-13 15:31:52 +00:00
App.MainWindows.Remove(this);
if (notifyIcon != null)
{
notifyIcon.Visible = false;
notifyIcon.Dispose();
}
notifyIcon?.Dispose();
2023-10-15 13:12:00 +00:00
}
2023-03-07 08:11:23 +00:00
}
2023-08-16 03:09:44 +00:00
2023-04-01 19:31:28 +00:00
public long lastclicktime { get; set; }
2023-08-09 14:17:28 +00:00
public void LoadLatestSave(string petname)
{
2023-09-24 13:17:48 +00:00
if (Directory.Exists(ExtensionValue.BaseDirectory + @"\Saves"))
{
2023-10-10 17:03:23 +00:00
var ds = new List<string>(Directory.GetFiles(ExtensionValue.BaseDirectory + @"\Saves", $@"Save{PrefixSave}_*.lps"))
.OrderBy(x =>
{
if (int.TryParse(x.Split('_').Last().Split('.')[0], out int i))
return i;
return 0;
}).ToList();
if (ds.Count != 0)
{
2023-10-10 17:03:23 +00:00
int.TryParse(ds.Last().Split('_').Last().Split('.')[0], out int lastid);
if (Set.SaveTimes < lastid)
{
Set.SaveTimes = lastid;
}
}
2023-10-02 12:03:25 +00:00
for (int i = ds.Count - 1; i >= 0; i--)
{
2023-10-02 12:03:25 +00:00
var latestsave = ds[i];
if (latestsave != null)
{
#if !DEBUG
2023-10-02 12:03:25 +00:00
try
{
#endif
if (SavesLoad(new LPS(File.ReadAllText(latestsave))))
2023-10-02 12:03:25 +00:00
return;
//MessageBoxX.Show("存档损毁,无法加载该存档\n可能是上次储存出错或Steam云同步导致的\n请在设置中加载备份还原存档", "存档损毁".Translate());
#if !DEBUG
2023-10-02 12:03:25 +00:00
}
catch (Exception ex)
2023-10-02 12:03:25 +00:00
{
MessageBoxX.Show("存档损毁,无法加载该存档\n可能是数据溢出/超模导致的" + '\n' + ex.Message, "存档损毁".Translate());
2023-10-02 12:03:25 +00:00
}
#endif
}
}
2023-10-02 12:03:25 +00:00
}
GameSavesData = new GameSave_v2(petname.Translate());
Core.Save = GameSavesData.GameSave;
}
2023-08-25 22:39:13 +00:00
2023-09-24 15:35:49 +00:00
private void WorkTimer_E_FinishWork(WorkTimer.FinishWorkInfo obj)
{
if (obj.work.Type == GraphHelper.Work.WorkType.Work)
{
GameSavesData.Statistics[(gint)"stat_single_profit_money"] = (int)obj.count;
}
else
{
GameSavesData.Statistics[(gint)"stat_single_profit_exp"] = (int)obj.count;
}
}
2023-08-11 06:49:43 +00:00
2023-07-23 20:46:21 +00:00
private void Main_Event_TouchBody()
{
2023-09-22 13:24:02 +00:00
GameSavesData.Statistics[(gint)"stat_touch_body"]++;
2023-07-23 20:46:21 +00:00
}
private void Main_Event_TouchHead()
{
2023-09-22 13:24:02 +00:00
GameSavesData.Statistics[(gint)"stat_touch_head"]++;
2023-07-23 20:46:21 +00:00
}
private void Main_OnSay(string obj)
{
2023-09-22 13:24:02 +00:00
GameSavesData.Statistics[(gint)"stat_say_times"]++;
2023-07-23 20:46:21 +00:00
}
private void MoveTimer_Elapsed(object sender, ElapsedEventArgs e)
{
2023-09-22 13:24:02 +00:00
GameSavesData.Statistics[(gint)"stat_move_length"] += (int)(Math.Abs(Main.MoveTimerPoint.X) + Math.Abs(Main.MoveTimerPoint.Y));
2023-07-23 20:46:21 +00:00
}
2023-06-18 18:11:40 +00:00
private void AutoSaveTimer_Elapsed(object sender, ElapsedEventArgs e)
{
Save();
}
2023-06-16 03:39:29 +00:00
2023-01-20 17:01:10 +00:00
2023-06-18 18:11:40 +00:00
private void Window_Closed(object sender, EventArgs e)
2023-01-12 22:11:55 +00:00
{
2023-08-12 07:13:30 +00:00
CloseConfirm = false;
2023-06-18 18:11:40 +00:00
try
{
//关闭所有插件
foreach (MainPlugin mp in Plugins)
mp.EndGame();
}
catch { }
2023-07-05 03:22:34 +00:00
Save();
2023-08-16 03:09:44 +00:00
Exit();
2023-01-12 22:11:55 +00:00
}
2023-07-03 11:41:16 +00:00
[DllImport("user32", EntryPoint = "SetWindowLong")]
private static extern uint SetWindowLong(IntPtr hwnd, int nIndex, uint dwNewLong);
2023-04-01 19:31:28 +00:00
2023-07-03 11:41:16 +00:00
[DllImport("user32", EntryPoint = "GetWindowLong")]
private static extern uint GetWindowLong(IntPtr hwnd, int nIndex);
private void Window_SourceInitialized(object sender, EventArgs e)
{
//const int WS_EX_TRANSPARENT = 0x20;
//const int GWL_EXSTYLE = -20;
//IntPtr hwnd = new WindowInteropHelper(this).Handle;
//uint extendedStyle = GetWindowLong(hwnd, GWL_EXSTYLE);
//SetWindowLong(hwnd, GWL_EXSTYLE, extendedStyle | WS_EX_TRANSPARENT);
((HwndSource)PresentationSource.FromVisual(this)).AddHook((IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled) =>
{
//想要让窗口透明穿透鼠标和触摸等,需要同时设置 WS_EX_LAYERED 和 WS_EX_TRANSPARENT 样式,
//确保窗口始终有 WS_EX_LAYERED 这个样式,并在开启穿透时设置 WS_EX_TRANSPARENT 样式
//但是WPF窗口在未设置 AllowsTransparency = true 时,会自动去掉 WS_EX_LAYERED 样式(在 HwndTarget 类中)
//如果设置了 AllowsTransparency = true 将使用WPF内置的低性能的透明实现
//所以这里通过 Hook 的方式在不使用WPF内置的透明实现的情况下强行保证这个样式存在。
if (msg == (int)Win32.WM.STYLECHANGING && (long)wParam == (long)Win32.GetWindowLongFields.GWL_EXSTYLE)
{
var styleStruct = (STYLESTRUCT)Marshal.PtrToStructure(lParam, typeof(STYLESTRUCT));
styleStruct.styleNew |= (int)Win32.ExtendedWindowStyles.WS_EX_LAYERED;
// Hide windows from alt+tab: https://stackoverflow.com/questions/357076/best-way-to-hide-a-window-from-the-alt-tab-program-switcher
if (Set.HideFromTaskControl)
{
styleStruct.styleNew |= (int)Win32.ExtendedWindowStyles.WS_EX_TOOLWINDOW;
}
2023-07-03 11:41:16 +00:00
Marshal.StructureToPtr(styleStruct, lParam, false);
handled = true;
}
return IntPtr.Zero;
});
}
private readonly bool _dwmEnabled;
private readonly IntPtr _hwnd;
public bool HitThrough { get; private set; } = false;
2023-07-16 23:58:09 +00:00
public bool MouseHitThrough
{
get => HitThrough;
set
{
if (value != HitThrough)
SetTransparentHitThrough();
}
}
2023-07-03 11:41:16 +00:00
/// <summary>
/// 设置点击穿透到后面透明的窗口
/// </summary>
public void SetTransparentHitThrough()
{
if (_dwmEnabled)
{
//const int WS_EX_TRANSPARENT = 0x20;
//const int GWL_EXSTYLE = -20;
//IntPtr hwnd = new WindowInteropHelper(this).Handle;
//uint extendedStyle = GetWindowLong(hwnd, GWL_EXSTYLE);
//SetWindowLong(hwnd, GWL_EXSTYLE, extendedStyle | WS_EX_TRANSPARENT);
HitThrough = !HitThrough;
2024-02-22 18:45:18 +00:00
(notifyIcon.ContextMenuStrip.Items.Find("NotifyIcon_HitThrough", false).First() as ToolStripMenuItem).Checked = HitThrough;
2023-07-03 11:41:16 +00:00
if (HitThrough)
{
Win32.User32.SetWindowLongPtr(_hwnd, Win32.GetWindowLongFields.GWL_EXSTYLE,
(IntPtr)(int)((long)Win32.User32.GetWindowLongPtr(_hwnd, Win32.GetWindowLongFields.GWL_EXSTYLE) | (long)Win32.ExtendedWindowStyles.WS_EX_TRANSPARENT));
petHelper?.SetOpacity(false);
}
else
{
Win32.User32.SetWindowLongPtr(_hwnd, Win32.GetWindowLongFields.GWL_EXSTYLE,
(IntPtr)(int)((long)Win32.User32.GetWindowLongPtr(_hwnd, Win32.GetWindowLongFields.GWL_EXSTYLE) & ~(long)Win32.ExtendedWindowStyles.WS_EX_TRANSPARENT));
petHelper?.SetOpacity(true);
}
}
}
private void WindowX_LocationChanged(object sender, EventArgs e)
{
2023-07-03 23:17:17 +00:00
petHelper?.SetLocation();
2023-07-03 11:41:16 +00:00
}
2023-06-18 18:11:40 +00:00
//public void DEBUGValue()
//{
// Dispatcher.Invoke(() =>
// {
// Console.WriteLine("Left:" + mwc.GetWindowsDistanceLeft());
// Console.WriteLine("Right:" + mwc.GetWindowsDistanceRight());
// });
// Thread.Sleep(1000);
// DEBUGValue();
//}
2023-07-03 11:41:16 +00:00
//
2023-06-18 18:11:40 +00:00
}
2022-12-13 07:10:18 +00:00
}