From c190e555e0379b13112556b15514eb4d835c02d8 Mon Sep 17 00:00:00 2001
From: sin365 <353374337@qq.com>
Date: Mon, 23 Oct 2023 11:48:37 +0800
Subject: [PATCH] ..
---
FileHelper.cs | 60 ++++++++++
HexHelper.cs | 232 +++++++++++++++++++++++++++++++++++++
MHFOldEquipQuestFix.csproj | 10 ++
MHFOldEquipQuestFix.sln | 25 ++++
Program.cs | 142 +++++++++++++++++++++++
5 files changed, 469 insertions(+)
create mode 100644 FileHelper.cs
create mode 100644 HexHelper.cs
create mode 100644 MHFOldEquipQuestFix.csproj
create mode 100644 MHFOldEquipQuestFix.sln
create mode 100644 Program.cs
diff --git a/FileHelper.cs b/FileHelper.cs
new file mode 100644
index 0000000..495f4a6
--- /dev/null
+++ b/FileHelper.cs
@@ -0,0 +1,60 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace MHFOldEquipQuestFix
+{
+ public class FileHelper
+ {
+ public static bool LoadFile(string FilePath, out byte[] data)
+ {
+ using (FileStream fs = new FileStream(FilePath, FileMode.Open, FileAccess.Read))
+ {
+ try
+ {
+ byte[] buffur = new byte[fs.Length];
+ fs.Read(buffur, 0, (int)fs.Length);
+ fs.Close();
+ data = buffur;
+ return true;
+ }
+ catch (Exception ex)
+ {
+ data = null;
+ return false;
+ }
+ }
+ }
+
+ public static string[] GetDirFile(string Path)
+ {
+ return Directory.GetFiles(Path);
+ }
+
+ public static byte[] String2Byte(string value)
+ {
+ return Encoding.Default.GetBytes(value);
+ }
+ public static bool SaveFile(string FilePath, byte[] buffer)
+ {
+ try
+ {
+ //创建一个文件流
+ using (FileStream wfs = new FileStream(FilePath, FileMode.Create))
+ {
+ //将byte数组写入文件中
+ wfs.Write(buffer, 0, buffer.Length);
+ //所有流类型都要关闭流,否则会出现内存泄露问题
+ wfs.Close();
+ return true;
+ }
+ }
+ catch
+ {
+ return false;
+ }
+ }
+ }
+}
diff --git a/HexHelper.cs b/HexHelper.cs
new file mode 100644
index 0000000..ad2920e
--- /dev/null
+++ b/HexHelper.cs
@@ -0,0 +1,232 @@
+using System;
+using System.Collections;
+using System.Collections.Generic;
+using System.Linq;
+using System.Net.Http.Headers;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace MHFOldEquipQuestFix
+{
+ public class HexHelper
+ {
+
+ public static byte[] CopyByteArr(byte[] src)
+ {
+ byte[] target = new byte[src.Length];
+ //加载数据
+ target = new byte[src.Length];
+ for (int i = 0; i < src.Length; i++)
+ target[i] = src[i];
+ return target;
+ }
+
+ ///
+ /// 读取byte[]数据
+ ///
+ ///
+ ///
+ ///
+ ///
+ public static byte[] ReadBytes(byte[] src, int lenght, int offset = 0)
+ {
+ byte[] data = new byte[lenght];
+ for (int i = 0; i < lenght; i++)
+ {
+ data[i] = src[offset + i];
+ }
+ return data;
+ }
+
+ /**
+ * byte[]转换int byte高位在前
+ */
+ public static int bytesToInt(byte[] src,int lenght, int offset = 0)
+ {
+ if (lenght == 1)
+ return src[offset + 0];
+
+ byte[] data = new byte[lenght];
+ for (int i = 0; i < lenght; i++)
+ {
+ data[i] = src[offset + i];
+ }
+
+ if(lenght == 2)
+ return BitConverter.ToInt16(data, 0);
+ else //if (lenght == 4)
+ return BitConverter.ToInt32(data, 0);
+ }
+
+ /**
+ * byte[]转换int byte高位在前
+ */
+ public static uint bytesToUInt(byte[] src, int lenght, int offset = 0)
+ {
+ if (lenght == 1)
+ return src[offset + 0];
+
+ byte[] data = new byte[lenght];
+ for (int i = 0; i < lenght; i++)
+ {
+ data[i] = src[offset + i];
+ }
+
+ if (lenght == 2)
+ return BitConverter.ToUInt16(data, 0);
+ else //if (lenght == 4)
+ return BitConverter.ToUInt32(data, 0);
+ }
+
+ /**
+ * int 转 byte[] byte高位在前
+ */
+ public static byte[] intToBytes(int value)
+ {
+ return BitConverter.GetBytes(value);
+ }
+
+ /**
+ * 从字节读取字符串
+ */
+ public static string ReadBytesToString(byte[] src, int Start, Encoding encoding = null)
+ {
+ List bytes = new List();
+
+ int index = 0;
+ while (true)
+ {
+ bytes.Add(src[Start + index]);
+
+ if (src[Start + index + 1] == 0x00)
+ break;
+
+ index++;
+ }
+ if (encoding == null)
+ encoding = Encoding.GetEncoding("Shift-JIS");
+ string str = encoding.GetString(bytes.ToArray());
+ return str;
+ }
+
+
+ /**
+ * 从字节读取字符串
+ */
+ public static string ReadBytesToString(byte[] src, Encoding encoding = null)
+ {
+ if (encoding == null)
+ encoding = Encoding.GetEncoding("Shift-JIS");
+ string str = encoding.GetString(src.ToArray());
+ return str;
+ }
+
+
+ /**
+ * 写入int到byte[] byte高位在前
+ */
+ public static void ModifyIntHexToBytes(byte[] srcdata, int targetvalue,int startoffset, int srclenght)
+ {
+ byte[] targetVal = intToBytes(targetvalue);
+
+ //抹去数据
+ for (int i = 0; i < srclenght; i++)
+ srcdata[startoffset + i] = 0x00;
+
+ for (int i = 0; i < targetVal.Length && i < srclenght; i++)
+ srcdata[startoffset + i] = targetVal[i];
+ }
+
+
+ /**
+ * 写入byte[]到byte[] byte高位在前
+ */
+ public static void ModifyDataToBytes(byte[] srcdata, byte[] targetVal, int startoffset)
+ {
+ //抹去数据
+ for (int i = 0; i < targetVal.Length; i++)
+ srcdata[startoffset + i] = 0x00;
+
+ for (int i = 0; i < targetVal.Length && i < targetVal.Length; i++)
+ srcdata[startoffset + i] = targetVal[i];
+ }
+
+
+
+ /**
+ * 对比数据
+ */
+ public static bool CheckDataEquals(byte[] srcdata, byte[] targetVal, int startoffset)
+ {
+ byte[] temp = new byte[targetVal.Length];
+ for (int i = 0; i < targetVal.Length && i < targetVal.Length; i++)
+ temp[i] = srcdata[startoffset + i];
+
+ return Array.Equals(targetVal, temp);
+ }
+
+ ///
+ /// 另一种16进制转10进制的处理方式,Multiplier参与*16的循环很巧妙,对Multiplier的处理很推荐,逻辑统一
+ ///
+ ///
+ ///
+ public static int HexaToDecimal(string HexaDecimalString)
+ {
+ int Decimal = 0;
+ int Multiplier = 1;
+
+ for (int i = HexaDecimalString.Length - 1; i >= 0; i--)
+ {
+ Decimal += HexaToDecimal(HexaDecimalString[i]) * Multiplier;
+ Multiplier *= 16;
+ }
+ return Decimal;
+ }
+
+ static int HexaToDecimal(char c)
+ {
+ switch (c)
+ {
+ case '0':
+ return 0;
+ case '1':
+ return 1;
+ case '2':
+ return 2;
+ case '3':
+ return 3;
+ case '4':
+ return 4;
+ case '5':
+ return 5;
+ case '6':
+ return 6;
+ case '7':
+ return 7;
+ case '8':
+ return 8;
+ case '9':
+ return 9;
+ case 'A':
+ case 'a':
+ return 10;
+ case 'B':
+ case 'b':
+ return 11;
+ case 'C':
+ case 'c':
+ return 12;
+ case 'D':
+ case 'd':
+ return 13;
+ case 'E':
+ case 'e':
+ return 14;
+ case 'F':
+ case 'f':
+ return 15;
+ }
+ return -1;
+ }
+ }
+}
diff --git a/MHFOldEquipQuestFix.csproj b/MHFOldEquipQuestFix.csproj
new file mode 100644
index 0000000..f02677b
--- /dev/null
+++ b/MHFOldEquipQuestFix.csproj
@@ -0,0 +1,10 @@
+
+
+
+ Exe
+ net7.0
+ enable
+ enable
+
+
+
diff --git a/MHFOldEquipQuestFix.sln b/MHFOldEquipQuestFix.sln
new file mode 100644
index 0000000..b9d4769
--- /dev/null
+++ b/MHFOldEquipQuestFix.sln
@@ -0,0 +1,25 @@
+
+Microsoft Visual Studio Solution File, Format Version 12.00
+# Visual Studio Version 17
+VisualStudioVersion = 17.7.34031.279
+MinimumVisualStudioVersion = 10.0.40219.1
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MHFOldEquipQuestFix", "MHFOldEquipQuestFix.csproj", "{D502E3BE-ACC8-4464-B143-96B0C7BFDEB7}"
+EndProject
+Global
+ GlobalSection(SolutionConfigurationPlatforms) = preSolution
+ Debug|Any CPU = Debug|Any CPU
+ Release|Any CPU = Release|Any CPU
+ EndGlobalSection
+ GlobalSection(ProjectConfigurationPlatforms) = postSolution
+ {D502E3BE-ACC8-4464-B143-96B0C7BFDEB7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+ {D502E3BE-ACC8-4464-B143-96B0C7BFDEB7}.Debug|Any CPU.Build.0 = Debug|Any CPU
+ {D502E3BE-ACC8-4464-B143-96B0C7BFDEB7}.Release|Any CPU.ActiveCfg = Release|Any CPU
+ {D502E3BE-ACC8-4464-B143-96B0C7BFDEB7}.Release|Any CPU.Build.0 = Release|Any CPU
+ EndGlobalSection
+ GlobalSection(SolutionProperties) = preSolution
+ HideSolutionNode = FALSE
+ EndGlobalSection
+ GlobalSection(ExtensibilityGlobals) = postSolution
+ SolutionGuid = {BD6993EA-B55B-4FC3-9981-5620EE8745E3}
+ EndGlobalSection
+EndGlobal
diff --git a/Program.cs b/Program.cs
new file mode 100644
index 0000000..57615a1
--- /dev/null
+++ b/Program.cs
@@ -0,0 +1,142 @@
+using System.Runtime.ConstrainedExecution;
+
+namespace MHFOldEquipQuestFix
+{
+ internal class Program
+ {
+ static string loc = Path.GetDirectoryName(AppContext.BaseDirectory) + "\\";
+ const string InDir = "Input";
+ const string OutDir = "Out";
+ const string Ver = "0.2";
+ static void Main(string[] args)
+ {
+ string title = $"MHFOldEquipQuestFix Ver.{Ver} By 皓月云 axibug.com";
+ Console.Title = title;
+ Console.WriteLine(title);
+
+ if (!Directory.Exists(loc + InDir))
+ {
+ Console.WriteLine("Input文件不存在");
+ Console.ReadLine();
+ return;
+ }
+
+ if (!Directory.Exists(loc + OutDir))
+ {
+ Console.WriteLine("Out文件不存在");
+ Console.ReadLine();
+ return;
+ }
+
+ Console.WriteLine($"-----------原数据读取完毕-----------");
+
+ string[] files = FileHelper.GetDirFile(loc + InDir);
+ Console.WriteLine($"共{files.Length}个文件,是否处理? (y/n)");
+
+ string yn = Console.ReadLine();
+ if (yn.ToLower() != "y")
+ return;
+
+ int index = 0;
+ int errcount = 0;
+ for (int i = 0; i < files.Length; i++)
+ {
+ string FileName = files[i].Substring(files[i].LastIndexOf("\\"));
+
+ if (!FileName.ToLower().Contains(".mib") && !FileName.ToLower().Contains(".bin"))
+ {
+ continue;
+ }
+ index++;
+
+ Console.WriteLine($">>>>>>>>>>>>>>开始处理 第{index}个文件 {FileName}<<<<<<<<<<<<<<<<<<<");
+ FileHelper.LoadFile(files[i], out byte[] data);
+ if (Do(data, out byte[] targetdata))
+ {
+ string newfileName = FileName;
+ string outstring = loc + OutDir + "\\" + newfileName;
+ FileHelper.SaveFile(outstring, targetdata);
+ Console.WriteLine($">>>>>>>>>>>>>>成功处理 第{index}个:{outstring}");
+ }
+ else
+ {
+ errcount++;
+ Console.WriteLine($">>>>>>>>>>>>>>处理失败 第{index}个");
+ }
+ }
+ Console.WriteLine($"已处理{files.Length}个文件,其中{errcount}个失败");
+ Console.WriteLine($"完毕");
+ Console.ReadLine();
+ }
+
+ static bool Do(byte[] src,out byte[] target)
+ {
+ try
+ {
+
+ target = HexHelper.CopyByteArr(src);//加载数据
+
+ byte src1;
+ byte src2;
+ byte src3;
+ byte src4;
+ //Weapon
+ src1 = src[0x0124];
+ src2 = src[0x0125];
+ src3 = src[0x0126];
+ src4 = src[0x0127];
+ target[0x0120] = src1;
+ target[0x0121] = src2;
+ target[0x0122] = src3;
+ target[0x0123] = src4;
+
+ //Head
+ src1 = src[0x012C];
+ src2 = src[0x012D];
+ src3 = src[0x012E];
+ src4 = src[0x012F];
+ target[0x0124] = src1;
+ target[0x0125] = src2;
+ target[0x0126] = src3;
+ target[0x0127] = src4;
+
+ //Chest
+ src1 = src[0x0134];
+ src2 = src[0x0135];
+ src3 = src[0x0136];
+ src4 = src[0x0137];
+ target[0x0128] = src1;
+ target[0x0129] = src2;
+ target[0x012A] = src3;
+ target[0x012B] = src4;
+
+ //Arms
+ src1 = src[0x013C];
+ src2 = src[0x013D];
+ src3 = src[0x013E];
+ src4 = src[0x013F];
+ target[0x012C] = src1;
+ target[0x012D] = src2;
+ target[0x012E] = src3;
+ target[0x012F] = src4;
+
+
+ //Waist
+ src1 = src[0x0144];
+ src2 = src[0x0145];
+ src3 = src[0x0146];
+ src4 = src[0x0147];
+ target[0x0130] = src1;
+ target[0x0131] = src2;
+ target[0x0132] = src3;
+ target[0x0133] = src4;
+ return true;
+ }
+ catch (Exception e)
+ {
+ target = null;
+ return false;
+ }
+ }
+ }
+}
\ No newline at end of file