VPet/VPet-Simulator.Windows/Function/CoreMOD.cs

353 lines
16 KiB
C#
Raw Normal View History

2023-01-08 16:57:10 +00:00
using LinePutScript;
2023-06-08 08:46:53 +00:00
using LinePutScript.Converter;
2023-07-01 07:46:08 +00:00
using LinePutScript.Dictionary;
using LinePutScript.Localization.WPF;
2023-01-08 16:57:10 +00:00
using System;
using System.Collections.Generic;
using System.Diagnostics;
2023-01-08 16:57:10 +00:00
using System.IO;
using System.Linq;
using System.Reflection;
2023-07-25 23:32:31 +00:00
using System.Windows;
2023-01-08 16:57:10 +00:00
using VPet_Simulator.Core;
2023-04-01 19:31:28 +00:00
using VPet_Simulator.Windows.Interface;
2023-01-08 16:57:10 +00:00
namespace VPet_Simulator.Windows
{
public class CoreMOD
{
2023-08-10 15:48:27 +00:00
public static HashSet<string> LoadedDLL { get; } = new HashSet<string>()
2023-04-01 19:31:28 +00:00
{
"ChatGPT.API.Framework.dll","Panuon.WPF.dll","steam_api.dll","Panuon.WPF.UI.dll","steam_api64.dll",
"LinePutScript.dll","Newtonsoft.Json.dll","Facepunch.Steamworks.Win32.dll", "Facepunch.Steamworks.Win64.dll",
2023-08-10 15:48:27 +00:00
"VPet-Simulator.Core.dll","VPet-Simulator.Windows.Interface.dll","LinePutScript.Localization.WPF.dll",
"CSCore.dll"
2023-04-01 19:31:28 +00:00
};
2023-01-08 16:57:10 +00:00
public static string NowLoading = null;
public string Name;
public string Author;
/// <summary>
/// 如果是上传至Steam,则为SteamUserID
/// </summary>
public long AuthorID;
/// <summary>
/// 上传至Steam的ItemID
/// </summary>
public ulong ItemID;
public string Intro;
public DirectoryInfo Path;
public int GameVer;
public int Ver;
2023-07-01 07:46:08 +00:00
public HashSet<string> Tag = new HashSet<string>();
2023-01-08 16:57:10 +00:00
public bool SuccessLoad = true;
2023-06-16 03:39:29 +00:00
public DateTime CacheDate;
2023-01-08 16:57:10 +00:00
public static string INTtoVER(int ver) => $"{ver / 100}.{ver % 100:00}";
2023-06-08 08:46:53 +00:00
public static void LoadImage(MainWindow mw, DirectoryInfo di)
{
//加载其他放在文件夹的图片
foreach (FileInfo fi in di.EnumerateFiles("*.png"))
{
mw.ImageSources.AddSource(fi.Name.ToLower().Substring(0, fi.Name.Length - 4), fi.FullName);
}
//加载其他放在文件夹中文件夹的图片
foreach (DirectoryInfo fordi in di.EnumerateDirectories())
{
LoadImage(mw, fordi);
}
//加载标志好的图片和图片设置
foreach (FileInfo fi in di.EnumerateFiles("*.lps"))
{
var tmp = new LpsDocument(File.ReadAllText(fi.FullName));
if (fi.Name.ToLower().StartsWith("set_"))
foreach (var line in tmp)
mw.ImageSources.ImageSetting.AddorReplaceLine(line);
else
mw.ImageSources.AddImages(tmp, di.FullName);
}
}
2023-01-08 16:57:10 +00:00
public CoreMOD(DirectoryInfo directory, MainWindow mw)
{
Path = directory;
LpsDocument modlps = new LpsDocument(File.ReadAllText(directory.FullName + @"\info.lps"));
Name = modlps.FindLine("vupmod").Info;
NowLoading = Name;
Intro = modlps.FindLine("intro").Info;
GameVer = modlps.FindSub("gamever").InfoToInt;
Ver = modlps.FindSub("ver").InfoToInt;
2023-06-14 11:13:52 +00:00
Author = modlps.FindSub("author").Info.Split('[').First();
2023-01-08 16:57:10 +00:00
if (modlps.FindLine("authorid") != null)
AuthorID = modlps.FindLine("authorid").InfoToInt64;
else
AuthorID = 0;
if (modlps.FindLine("itemid") != null)
ItemID = Convert.ToUInt64(modlps.FindLine("itemid").info);
else
ItemID = 0;
2023-06-16 03:39:29 +00:00
CacheDate = modlps.GetDateTime("cachedate", DateTime.MinValue);
2023-07-25 23:32:31 +00:00
//MOD未加载时支持翻译
foreach (var line in modlps.FindAllLine("lang"))
{
List<ILine> ls = new List<ILine>();
foreach (var sub in line)
{
ls.Add(new Line(sub.Name, sub.info));
}
LocalizeCore.AddCulture(line.info, ls);
}
2023-06-25 18:55:03 +00:00
if (!IsOnMOD(mw))
2023-01-08 16:57:10 +00:00
{
2023-08-13 14:39:46 +00:00
Tag.Add("该模组已停用");
2023-01-08 16:57:10 +00:00
return;
}
foreach (DirectoryInfo di in Path.EnumerateDirectories())
{
switch (di.Name.ToLower())
{
case "pet":
//宠物模型
2023-07-01 07:46:08 +00:00
Tag.Add("pet");
2023-01-08 16:57:10 +00:00
foreach (FileInfo fi in di.EnumerateFiles("*.lps"))
{
LpsDocument lps = new LpsDocument(File.ReadAllText(fi.FullName));
if (lps.First().Name.ToLower() == "pet")
{
2023-01-10 10:43:32 +00:00
var name = lps.First().Info;
var p = mw.Pets.FirstOrDefault(x => x.Name == name);
if (p == null)
2023-01-20 07:08:28 +00:00
mw.Pets.Add(new PetLoader(lps, di));
2023-01-10 10:43:32 +00:00
else
{
p.path.Add(di.FullName + "\\" + lps.First()["path"].Info);
2023-01-20 07:08:28 +00:00
p.Config.Set(lps);
2023-01-10 10:43:32 +00:00
}
2023-01-08 16:57:10 +00:00
}
}
break;
2023-06-08 08:46:53 +00:00
case "food":
2023-07-01 07:46:08 +00:00
Tag.Add("food");
2023-06-08 08:46:53 +00:00
foreach (FileInfo fi in di.EnumerateFiles("*.lps"))
{
var tmp = new LpsDocument(File.ReadAllText(fi.FullName));
foreach (ILine li in tmp)
{
string tmps = li.Find("name").info;
mw.Foods.RemoveAll(x => x.Name == tmps);
mw.Foods.Add(LPSConvert.DeserializeObject<Food>(li));
}
}
break;
case "image":
2023-07-01 07:46:08 +00:00
Tag.Add("image");
2023-06-08 08:46:53 +00:00
LoadImage(mw, di);
break;
2023-06-18 22:20:06 +00:00
case "text":
2023-07-01 07:46:08 +00:00
Tag.Add("text");
2023-06-18 22:20:06 +00:00
foreach (FileInfo fi in di.EnumerateFiles("*.lps"))
{
var tmp = new LpsDocument(File.ReadAllText(fi.FullName));
foreach (ILine li in tmp)
{
switch (li.Name.ToLower())
{
case "lowfoodtext":
mw.LowFoodText.Add(LPSConvert.DeserializeObject<LowText>(li));
break;
case "lowdrinktext":
mw.LowDrinkText.Add(LPSConvert.DeserializeObject<LowText>(li));
break;
2023-08-13 16:31:37 +00:00
case "clicktext":
mw.ClickTexts.Add(LPSConvert.DeserializeObject<ClickText>(li));
break;
2023-06-18 22:20:06 +00:00
}
}
}
break;
2023-07-01 07:46:08 +00:00
case "lang":
Tag.Add("lang");
2023-08-08 14:09:49 +00:00
foreach (FileInfo fi in di.EnumerateFiles("*.lps"))
{
LocalizeCore.AddCulture(fi.Name.Substring(0, fi.Name.Length - fi.Extension.Length), new LPS_D(File.ReadAllText(fi.FullName)));
}
2023-07-01 07:46:08 +00:00
foreach (DirectoryInfo dis in di.EnumerateDirectories())
{
foreach (FileInfo fi in dis.EnumerateFiles("*.lps"))
{
LocalizeCore.AddCulture(dis.Name, new LPS_D(File.ReadAllText(fi.FullName)));
}
}
if (mw.Set.Language == "null")
{
LocalizeCore.LoadDefaultCulture();
}
else
LocalizeCore.LoadCulture(mw.Set.Language);
2023-07-01 07:46:08 +00:00
break;
2023-04-01 19:31:28 +00:00
case "plugin":
2023-07-01 07:46:08 +00:00
Tag.Add("plugin");
SuccessLoad = true;
2023-08-12 11:47:06 +00:00
string authtype = "";
2023-04-01 19:31:28 +00:00
foreach (FileInfo tmpfi in di.EnumerateFiles("*.dll"))
{
try
{
2023-04-03 18:11:12 +00:00
var path = tmpfi.Name;
2023-04-01 19:31:28 +00:00
if (LoadedDLL.Contains(path))
continue;
LoadedDLL.Add(path);
2023-04-03 18:11:12 +00:00
Assembly dll = Assembly.LoadFrom(tmpfi.FullName);
2023-06-14 11:13:52 +00:00
var certificate = dll.GetModules()?.First()?.GetSignerCertificate();
if (certificate != null)
{
if (certificate.Subject == "CN=\"Shenzhen Lingban Computer Technology Co., Ltd.\", O=\"Shenzhen Lingban Computer Technology Co., Ltd.\", L=Shenzhen, S=Guangdong Province, C=CN, SERIALNUMBER=91440300MA5H8REU3K, OID.2.5.4.15=Private Organization, OID.1.3.6.1.4.1.311.60.2.1.1=Shenzhen, OID.1.3.6.1.4.1.311.60.2.1.2=Guangdong Province, OID.1.3.6.1.4.1.311.60.2.1.3=CN"
&& certificate.Issuer == "CN=DigiCert Trusted G4 Code Signing RSA4096 SHA384 2021 CA1, O=\"DigiCert, Inc.\", C=US")
{//LBGame 信任的证书
2023-08-12 11:47:06 +00:00
if (authtype != "FAIL")
authtype = "[认证]".Translate();
}
2023-07-05 03:22:34 +00:00
else if (certificate.Subject != "CN=Microsoft Corporation, O=Microsoft Corporation, L=Redmond, S=Washington, C=US" && !IsPassMOD(mw))
{//不是通过模组,不加载
SuccessLoad = false;
continue;
}
2023-08-12 11:47:06 +00:00
else if (authtype != "")
{
2023-08-12 11:47:06 +00:00
authtype = "[签名]".Translate();
2023-08-13 17:29:45 +00:00
//Intro += $"\nSubject:{certificate.Subject}\nIssuer:{certificate.Subject}";
}
2023-06-14 11:13:52 +00:00
}
else
{
2023-08-12 11:47:06 +00:00
authtype = "FAIL";
2023-06-14 11:13:52 +00:00
if (!IsPassMOD(mw))
{//不是通过模组,不加载
2023-08-08 14:09:49 +00:00
SuccessLoad = false;
2023-08-12 11:47:06 +00:00
Author = modlps.FindSub("author").Info.Split('[').First();
2023-06-14 11:13:52 +00:00
continue;
}
}
2023-04-01 19:31:28 +00:00
var v = dll.GetExportedTypes();
foreach (Type exportedType in v)
{
if (exportedType.BaseType == typeof(MainPlugin))
{
mw.Plugins.Add((MainPlugin)Activator.CreateInstance(exportedType, mw));
}
}
}
2023-08-12 09:58:57 +00:00
catch (Exception Exception)
2023-04-01 19:31:28 +00:00
{
SuccessLoad = false;
2023-08-12 09:58:57 +00:00
string errstr = "MOD加载错误,是MOD({0})导致的\n如有可能请发送 错误信息截图和引发错误之前的操作 给开发者:service@exlb.net\n感谢您对游戏开发的支持\n".Translate() + Exception.ToString();
new winReport(mw, errstr).Show();
2023-04-01 19:31:28 +00:00
}
}
2023-08-12 11:47:06 +00:00
if (authtype != "FAIL")
Author += authtype;
2023-04-01 19:31:28 +00:00
break;
2023-01-08 16:57:10 +00:00
}
}
}
2023-06-25 18:55:03 +00:00
public bool IsOnMOD(MainWindow mw) => mw.Set.IsOnMod(Name);
2023-01-08 16:57:10 +00:00
public bool IsPassMOD(MainWindow mw) => mw.Set.IsPassMOD(Name);
public void WriteFile()
{
LpsDocument modlps = new LpsDocument(File.ReadAllText(Path.FullName + @"\info.lps"));
modlps.FindLine("vupmod").Info = Name;
modlps.FindLine("intro").Info = Intro;
modlps.FindSub("gamever").InfoToInt = GameVer;
modlps.FindSub("ver").InfoToInt = Ver;
modlps.FindSub("author").Info = Author;
modlps.FindorAddLine("authorid").InfoToInt64 = AuthorID;
modlps.FindorAddLine("itemid").info = ItemID.ToString();
File.WriteAllText(Path.FullName + @"\info.lps", modlps.ToString());
2023-06-08 08:46:53 +00:00
}
2023-05-25 18:44:31 +00:00
}
public static class ExtensionSetting
{
public static void StartURL(string url)
{
try
{
Process.Start(url);
}
catch
{
Process.Start("explorer.exe", url);
}
}
2023-08-10 15:48:27 +00:00
/// <summary>
/// 吃食物 附带倍率
/// </summary>
/// <param name="save">存档</param>
/// <param name="food">食物</param>
/// <param name="buff">默认1倍</param>
public static void EatFood(this GameSave save, IFood food, double buff)
{
save.Exp += food.Exp * buff;
var tmp = food.Strength / 2 * buff;
save.StrengthChange(tmp);
save.StoreStrength += tmp;
tmp = food.StrengthFood / 2 * buff;
save.StrengthChangeFood(tmp);
save.StoreStrengthFood += tmp;
tmp = food.StrengthDrink / 2 * buff;
save.StrengthChangeDrink(tmp);
save.StoreStrengthDrink += tmp;
tmp = food.Feeling / 2 * buff;
save.FeelingChange(tmp);
save.StoreFeeling += tmp * buff;
save.Health += food.Health * buff;
save.Likability += food.Likability * buff;
}
2023-06-25 18:55:03 +00:00
public static bool IsOnMod(this Setting t, string ModName)
2023-05-25 18:44:31 +00:00
{
2023-06-25 18:55:03 +00:00
if (ModName == "Core")
return true;
var line = t.FindLine("onmod");
2023-05-25 18:44:31 +00:00
if (line == null)
return false;
return line.Find(ModName.ToLower()) != null;
}
public static bool IsPassMOD(this Setting t, string ModName)
{
var line = t.FindLine("passmod");
if (line == null)
return false;
return line.Find(ModName.ToLower()) != null;
}
public static bool IsMSGMOD(this Setting t, string ModName)
{
var line = t.FindorAddLine("msgmod");
if (line.GetBool(ModName))
return false;
line.SetBool(ModName, true);
return true;
}
2023-06-25 18:55:03 +00:00
public static void OnMod(this Setting t, string ModName)
2023-05-25 18:44:31 +00:00
{
if (string.IsNullOrWhiteSpace(ModName))
return;
2023-06-25 18:55:03 +00:00
t.FindorAddLine("onmod").AddorReplaceSub(new Sub(ModName.ToLower()));
2023-05-25 18:44:31 +00:00
}
2023-06-25 18:55:03 +00:00
public static void OnModRemove(this Setting t, string ModName)
2023-05-25 18:44:31 +00:00
{
2023-06-25 18:55:03 +00:00
t.FindorAddLine("onmod").Remove(ModName.ToLower());
2023-05-25 18:44:31 +00:00
}
public static void PassMod(this Setting t, string ModName)
{
t.FindorAddLine("passmod").AddorReplaceSub(new Sub(ModName.ToLower()));
}
public static void PassModRemove(this Setting t, string ModName)
{
t.FindorAddLine("passmod").Remove(ModName.ToLower());
2023-01-08 16:57:10 +00:00
}
}
}