添加项目文件。

This commit is contained in:
ShakeFlower committed 2024-10-22 22:23:15 +08:00
1 parent 4a9faaae91
commit b3724bc752
19 files changed
+2123

No files matched your search

+155
View File
@@ -0,0 +1,155 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics.Eventing.Reader;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Input;
using System.Xml.Linq;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Drawing;
namespace H5MotaUpdate.ViewModels
{
internal static class FileUtils
{
/// <summary>
/// 检查文件夹路径是否合法
/// </summary>
public static bool IsFolderPathValid(string? folderPath, string folderName)
{
try
{
if (!Directory.Exists(folderPath))
{
MessageBox.Show(folderName + "文件夹不存在,请检查", "错误", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
return false;
}
}
catch (Exception e)
{
MessageBox.Show(folderName + $"文件夹不存在,请检查,错误:{e.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
return false;
}
return true;
}
/// <summary>
/// 检查指定文件夹是否存在指定的子文件夹,若不存在,创建一个
/// </summary>
public static bool checkFolderExist(string folderPath, string subFolderName)
{
string subFolderPath = Path.Combine(folderPath, subFolderName);
if (!Directory.Exists(subFolderPath))
{
return tryCreateFolder(subFolderName, subFolderPath);
}
else { return true; }
}
/// <summary>
/// 尝试在指定路径创建文件夹
/// </summary>
public static bool tryCreateFolder(string folderDirectory, string folderName)
{
try { Directory.CreateDirectory(folderDirectory); }
catch (Exception e)
{
MessageBox.Show($"错误:{e.Message},目标文件夹不存在" + folderName + "子文件夹,且创建失败,请检查", "错误", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
return false;
}
if (!Directory.Exists(folderDirectory))
{
MessageBox.Show("目标文件夹不存在" + folderName + "子文件夹,且创建失败,请检查", "错误", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
return false;
}
return true;
}
/// <summary>
/// 将源文件夹的所有文件拷贝至目标文件夹
/// </summary>
public static void CopyFolderContents(string sourceFolderPath, string destFolderPath)
{
string[] files = Directory.GetFiles(sourceFolderPath);
foreach (string file in files)
{
try
{
string targetFilePath = Path.Combine(destFolderPath, Path.GetFileName(file));
File.Copy(file, targetFilePath, true);
}
catch (Exception e)
{
MessageBox.Show("迁移" + file + $"过程中出现错误:{e.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
}
/// <summary>
/// 将源文件夹的所有文件(含子文件夹及其所有文件)拷贝至目标文件夹
/// </summary>
public static void CopyFolderContentsAndSubFolders(string sourceFolderPath, string destFolderPath)
{
string[] files = Directory.GetFiles(sourceFolderPath);
foreach (string file in files)
{
try
{
string targetFilePath = Path.Combine(destFolderPath, Path.GetFileName(file));
File.Copy(file, targetFilePath, true);
}
catch (Exception e)
{
MessageBox.Show("迁移" + file + $"过程中出现错误:{e.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
string[] dirs = Directory.GetDirectories(sourceFolderPath);
foreach (string dir in dirs)
{
try
{
string targetFolderPath = Path.Combine(destFolderPath, Path.GetFileName(dir));
CopyFolderContentsAndSubFolders(dir, targetFolderPath);
}
catch (Exception e)
{
MessageBox.Show("迁移" + dir + $"过程中出现错误:{e.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
}
/// <summary>
/// 将源文件夹名为fileName的指定文件复制到目标文件夹
/// </summary>
public static void CopyFile(string sourceFolderPath, string destFolderPath, string fileName)
{
try
{
string SourcePath = sourceFolderPath;
string DestPath = destFolderPath;
if (!File.Exists(SourcePath))
{
MessageBox.Show("源文件夹不存在" + fileName + "子文件,请检查", "错误", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
else
{
System.IO.File.Copy(SourcePath, DestPath, true);
}
}
catch (Exception e)
{
MessageBox.Show("迁移" + fileName + $"文件出现错误:{e.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
}
}
+174
View File
@@ -0,0 +1,174 @@
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Windows;
using System.Windows.Forms;
namespace H5MotaUpdate.ViewModels
{
internal static class StringUtils
{
/// <summary>
/// 从指定路径的.js文件中提取JObject文件
/// </summary>
public static JObject getValidJson(string path)
{
try
{
string jsContent = File.ReadAllText(path);
string jsonContent;
int start = jsContent.IndexOf("{");
int end = jsContent.LastIndexOf("}") + 1;
if (end != -1)
{
jsonContent = jsContent.Substring(start, end - start);
}
else
{
jsonContent = jsContent.Substring(start);
}
JObject jsonObject = JObject.Parse(jsonContent);
return jsonObject;
}
catch (Exception e)
{
System.Windows.Forms.MessageBox.Show("从" + path + "中读取JSON时出错:" + $"{e.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
throw;
}
}
/// <summary>
/// 对于字符串str,找到${...}内部的内容,将其中的oldWord替换为newWord
/// </summary>
public static string ReplaceInBetweenCurlyBraces(string str, string oldWord, string newWord)
{
// 定义正则表达式模式,匹配 ${...} 中的内容
string pattern = @"\$\{([^}]+)\}";
// 使用 Regex.Replace 方法进行替换
string replacedStr = Regex.Replace(str, pattern, match =>
{
// 获取匹配的内容,去掉 ${ 和 }
string content = match.Groups[1].Value;
// 在匹配的内容中替换 oldWord 为 newWord
string modifiedContent = content.Replace(oldWord, newWord);
// 返回替换后的完整匹配字符串,包含 ${ 和 }
return "${" + modifiedContent + "}";
});
return replacedStr;
}
/// <summary>
/// 输入要加减的变量名varName,增减值value,符号+=或-=或=,返回一个执行该事件的JSON
/// </summary>
public static JObject getAddValueJson(string varName, string value, string o)
{
JObject adder = new JObject();
adder.Add(new JProperty("type", "setValue"));
adder.Add(new JProperty("name", varName));
if (o != "=") adder.Add(new JProperty("operator", o));
adder.Add(new JProperty("value", value));
return adder;
}
/// <summary>
/// 输入effect语句,返回一个包含所有事件的数组
/// </summary>
/// 参考: events.js/doEffect
public static JArray doEffect(string effects)
{
JArray newEffectsArray = new JArray();
string[] effectArr = effects.Split([';']);
foreach (string effect in effectArr)
{
string[] arr = effect.Split("+=");
JObject currEffectJson = getAddValueJson(arr[0], arr[1], "+=");
newEffectsArray.Add(currEffectJson);
}
return newEffectsArray;
}
public static string ReplaceOldNames(string input, Version version)
{
input = input.Replace("Jewel", "Gem");
input = input.Replace("ratio", "core.status.thisMap.ratio");
return input;
}
/// <summary>
/// 合并两个JObject,jobject2中的键值对覆盖jobject1的键值对(不论原先是否存在),返回jobject1。
/// </summary>
/// <param name="targetJObject">要合并到并返回的对象。</param>
/// <param name="sourceJObject">提供要合并内容的对象。</param>
/// <returns>合并后的targetJObject。</returns>
public static JObject MergeJObjects(JObject targetJObject, JObject sourceJObject)
{
foreach (JProperty prop in sourceJObject.Properties())
{
string key = prop.Name;
targetJObject[key] = prop.Value;
}
return targetJObject;
}
public static JArray CreateMatrix(int width, int height)
{
if (width <= 0 || height <= 0)
{
throw new ArgumentException("Width and height must be greater than 0.");
}
JArray matrix = new JArray();
for (int i = 0; i < height; i++)
{
JArray row = new JArray();
for (int j = 0; j < width; j++)
{
row.Add(0);
}
matrix.Add(row);
}
return matrix;
}
/// <summary>
/// 读取塔的地图尺寸,默认值为13
/// <summary>
public static int ReadMapWidth(string filePath)
{
int width;
try
{
string fileContent = File.ReadAllText(filePath);
string widthPattern = @"this\._WIDTH_\s*=\s*(\d+);";
Match widthMatch = Regex.Match(fileContent, widthPattern);
if (widthMatch.Success)
{
width = int.Parse(widthMatch.Groups[1].Value);
}
else
{
width = 13;
}
}
catch
{
width = 13;
}
return width;
}
}
}
+57
View File
@@ -0,0 +1,57 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics.Eventing.Reader;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Input;
using System.Xml.Linq;
namespace H5MotaUpdate.ViewModels
{
internal static class VersionUtils
{
public static bool IsValidVersion(string? version)
{
if (String.IsNullOrEmpty(version)) return false;
string[] segments = version.Split('.');
foreach (string segment in segments)
{
if (!int.TryParse(segment, out int part)) return false;
}
return true;
}
public static string GetVersion(string? folderPath)
{
if (folderPath == null) return "文件夹路径不合法";
string filePath = Path.Combine(folderPath, "main.js");
string version;
try
{
if (!File.Exists(filePath)) return "给定文件夹未找到文件main.js";
string fileContent = File.ReadAllText(filePath);
Regex versionRegex = new Regex(@"this\.version\s*=\s*['""](\d+(\.\d+)+)['""];");
Match match = versionRegex.Match(fileContent);
if (!match.Success) return "文件 main.js中未找到版本号!";
version = match.Groups[1].Value;
if (!IsValidVersion(version)) return "文件 main.js中未找到格式合法的版本号!";
}
catch (Exception ex)
{
return "读取版本号失败,原因: " + ex.Message;
}
return version;
}
}
}