NLDClient-yudde/ProjectNLD/Assets/_Test/Editor/TestCode.cs

387 lines
16 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using Framework;
using Gameplay.UI.Common;
using Gameplay.Unit.Data;
using PhxhSDK.AOT;
using PhxhSDK.AOT.Encrypt;
using PhxhSDK.AOT.PreConfig;
using UnityEditor;
using UnityEngine;
using UnityFS;
using Object = UnityEngine.Object;
public class TestCode: Editor
{
// [MenuItem("GameObject/LTTest/检查材质路径", false, 0)]
public static void Test6()
{
Object selectObj = Selection.activeObject;
if (null == selectObj)
{
DebugUtil.Log("尚未选择任何对象");
return;
}
var selectGo = selectObj as GameObject;
if (selectGo == null)
{
DebugUtil.Log("选择的对象不是GameObject类型");
return;
}
var render = selectGo.GetComponent<Renderer>();
var materials = render.sharedMaterials;
if (materials == null || materials.Length == 0)
{
DebugUtil.LogError("渲染器没有材质:" + selectGo.name);
return;
}
foreach (var mat in materials)
{
var materialPath = AssetDatabase.GetAssetPath(mat);
DebugUtil.LogError("材质路径:" + materialPath);
}
}
[MenuItem("Test/测试float转int")]
public static void Test7()
{
var checkStr = "50000410";
var floatVal = float.Parse(checkStr);
DebugUtil.LogError("floatVal:" + floatVal);
var intVal = (int)floatVal;
DebugUtil.LogError("intVal from float:" + intVal);
var doubleVal = double.Parse(checkStr);
DebugUtil.LogError("doubleVal:" + doubleVal);
var intVal2 = (int)doubleVal;
DebugUtil.LogError("intVal from double:" + intVal2);
}
// [MenuItem("Test/测试")]
public static void Test5()
{
var checkPath = "Art/Grass01/FlowerGroup_2 (8)";
var findObj = GameObjectEx.FindEx(checkPath);
DebugUtil.LogError("查找对象:" + checkPath + " 是否存在:" + (findObj != null));
}
// [MenuItem("Test/测试")]
public static void Test4()
{
var dataSkill = EditorTableManager.instance.tables.SkillConfig.DataList[0];
var cloneSkill = dataSkill.Clone();
cloneSkill.ReferIds.Add(100);
DebugUtil.LogError("refer equal:" + (dataSkill == cloneSkill));
DebugUtil.LogError("技能ID:" + dataSkill.ID + " cloneSkillID:" + cloneSkill.ID);
DebugUtil.LogError("referIds:" + dataSkill.ReferIds.Count + " cloneReferIds:" + cloneSkill.ReferIds.Count);
}
// [MenuItem("Test/测试")]
public static void Test3()
{
var testList = new List<int>();
for (int i = 0; i < 100; i++)
{
testList.Add(i);
}
var jsonStr = LitJson.JsonMapper.ToJson(testList);
Debug.LogError(jsonStr);
}
// [MenuItem("Test/测试加密2")]
public static void Test2()
{
var path = "C:\\Users\\82158\\Desktop\\加密测试\\server.Dev.0.1.0.xml";
var loadBytes = File.ReadAllBytes(path);
var loadStr = Encoding.UTF8.GetString(loadBytes);
Debug.LogError("原始数据(字符串):" + loadStr);
// 输出bytes内容
var sb = new StringBuilder();
for (int i = 0; i < loadBytes.Length; i++)
{
sb.Append(loadBytes[i].ToString("X") + ",");
}
Debug.LogError("原始数据(字节):" + sb.ToString());
var encodeBytes = PreConfig.EncodeBytes(loadBytes);
// 写入新文件
var savePath = "C:\\Users\\82158\\Desktop\\加密测试\\server.Dev.0.1.0_encrypt.xml";
File.WriteAllBytes(savePath, encodeBytes);
Debug.LogError("加密后数据长度:" + encodeBytes.Length);
// 输出加密后bytes内容
sb.Clear();
for (int i = 0; i < encodeBytes.Length; i++)
{
sb.Append(encodeBytes[i].ToString("X") + ",");
}
Debug.LogError("加密后数据(字节):" + sb.ToString());
var forceStr = Encoding.UTF8.GetString(encodeBytes);
Debug.LogError("加密后数据(字符串):" + forceStr);
var decodeStr = PreConfig.DecodeBytes(encodeBytes);
Debug.LogError("解密后数据:" + decodeStr);
// 对比
var isSame = loadStr.Equals(decodeStr);
Debug.LogError("对比结果:" + isSame);
}
[MenuItem("Test/测试LocalInfoEncrypt加密解密")]
public static void TestLocalInfoEncrypt()
{
Debug.Log("========== 开始测试 LocalInfoEncrypt 加密解密功能 ==========");
// 测试1: 基础字节数组加密解密
Debug.Log("\n【测试1】基础字节数组加密解密:");
var testData1 = "Hello World! 这是测试数据 123456";
var originalBytes1 = Encoding.UTF8.GetBytes(testData1);
Debug.Log($"原始数据: {testData1}");
Debug.Log($"原始字节长度: {originalBytes1.Length}");
var encryptedBytes1 = PhxhSDK.AOT.VersionUpdate.LocalInfoEncrypt.Encrypt(originalBytes1);
Debug.Log($"加密后字节长度: {encryptedBytes1.Length}");
var decryptedBytes1 = PhxhSDK.AOT.VersionUpdate.LocalInfoEncrypt.Decrypt(encryptedBytes1, out bool isEncrypted1);
var decryptedData1 = Encoding.UTF8.GetString(decryptedBytes1);
Debug.Log($"解密后数据: {decryptedData1}");
Debug.Log($"是否加密: {isEncrypted1}");
Debug.Log($"测试1结果: {(testData1 == decryptedData1 ? " " : " ")}");
// 测试2: JSON数据加密解密
Debug.Log("\n【测试2】JSON数据加密解密:");
var testJson = "{\"name\":\"测试\",\"value\":123,\"flag\":true}";
var originalBytes2 = Encoding.UTF8.GetBytes(testJson);
Debug.Log($"原始JSON: {testJson}");
var encryptedBytes2 = PhxhSDK.AOT.VersionUpdate.LocalInfoEncrypt.Encrypt(originalBytes2);
var decryptedBytes2 = PhxhSDK.AOT.VersionUpdate.LocalInfoEncrypt.Decrypt(encryptedBytes2, out bool isEncrypted2);
var decryptedJson = Encoding.UTF8.GetString(decryptedBytes2);
Debug.Log($"解密后JSON: {decryptedJson}");
Debug.Log($"是否有效JSON: {PhxhSDK.AOT.VersionUpdate.LocalInfoEncrypt.IsValidJson(decryptedBytes2)}");
Debug.Log($"测试2结果: {(testJson == decryptedJson ? " " : " ")}");
// 测试3: Base64字符串加密解密
Debug.Log("\n【测试3】Base64字符串加密解密:");
var testString3 = "{\"test\":\"base64加密测试\",\"number\":999}";
Debug.Log($"原始字符串: {testString3}");
var encryptedBase64 = PhxhSDK.AOT.VersionUpdate.LocalInfoEncrypt.EncryptToBase64(testString3);
Debug.Log($"加密后Base64长度: {encryptedBase64.Length}");
var decryptedString3 = PhxhSDK.AOT.VersionUpdate.LocalInfoEncrypt.DecryptFromBase64(encryptedBase64);
Debug.Log($"解密后字符串: {decryptedString3}");
Debug.Log($"测试3结果: {(testString3 == decryptedString3 ? " " : " ")}");
// 测试4: 明文数据兼容性测试
Debug.Log("\n【测试4】明文数据兼容性测试:");
var plainJson = "{\"plain\":\"明文数据\",\"encrypted\":false}";
var plainBytes = Encoding.UTF8.GetBytes(plainJson);
Debug.Log($"明文数据: {plainJson}");
var decryptedPlainBytes = PhxhSDK.AOT.VersionUpdate.LocalInfoEncrypt.Decrypt(plainBytes, out bool isEncrypted4);
var decryptedPlainJson = Encoding.UTF8.GetString(decryptedPlainBytes);
Debug.Log($"解密后数据: {decryptedPlainJson}");
Debug.Log($"是否识别为加密: {isEncrypted4}");
Debug.Log($"测试4结果: {(plainJson == decryptedPlainJson && !isEncrypted4 ? " " : " ")}");
// 测试5: 大数据加密解密
Debug.Log("\n【测试5】大数据加密解密:");
var largeData = new StringBuilder();
for (int i = 0; i < 1000; i++)
{
largeData.Append($"Line{i}:这是第{i}行测试数据,包含中英文和数字123456789;");
}
var largeDataStr = largeData.ToString();
var originalBytes5 = Encoding.UTF8.GetBytes(largeDataStr);
Debug.Log($"大数据原始长度: {originalBytes5.Length} 字节");
var encryptedBytes5 = PhxhSDK.AOT.VersionUpdate.LocalInfoEncrypt.Encrypt(originalBytes5);
Debug.Log($"加密后长度: {encryptedBytes5.Length} 字节");
var decryptedBytes5 = PhxhSDK.AOT.VersionUpdate.LocalInfoEncrypt.Decrypt(encryptedBytes5, out bool isEncrypted5);
var decryptedLargeData = Encoding.UTF8.GetString(decryptedBytes5);
Debug.Log($"解密后长度: {decryptedBytes5.Length} 字节");
Debug.Log($"测试5结果: {(largeDataStr == decryptedLargeData ? " " : " ")}");
Debug.Log("\n========== LocalInfoEncrypt 测试完成 ==========");
EditorUtility.DisplayDialog("测试完成", "LocalInfoEncrypt加密解密测试已完成请查看控制台日志", "确定");
}
[MenuItem("Test/本地信息解密")]
public static void FakeDecryptTest()
{
// 输入文件路径
var inputPath = @"C:\Users\82158\Downloads\DeobfuscateStackTrace\net8.0\LocalInfo.bytes";
// 输出文件路径
var outputPath = @"C:\Users\82158\Downloads\DeobfuscateStackTrace\net8.0\Decrypt_LocalInfo.json";
try
{
// 检查输入文件是否存在
if (!File.Exists(inputPath))
{
Debug.LogError($"输入文件不存在: {inputPath}");
return;
}
// 读取加密数据
var encryptedBytes = File.ReadAllBytes(inputPath);
Debug.Log($"读取加密数据,长度: {encryptedBytes.Length} 字节");
// 输出前20个字节用于调试
var headerInfo = new StringBuilder();
headerInfo.Append("文件头部字节 (前20字节): ");
for (int i = 0; i < Math.Min(20, encryptedBytes.Length); i++)
{
headerInfo.Append($"{encryptedBytes[i]:X2} ");
}
Debug.Log(headerInfo.ToString());
// 检查是否有加密标识头 (NLDE = 0x4E, 0x4C, 0x44, 0x45)
if (encryptedBytes.Length >= 4)
{
var hasHeader = encryptedBytes[0] == 0x4E && encryptedBytes[1] == 0x4C &&
encryptedBytes[2] == 0x44 && encryptedBytes[3] == 0x45;
Debug.Log($"是否包含加密标识头 (NLDE): {hasHeader}");
if (hasHeader && encryptedBytes.Length >= 5)
{
Debug.Log($"加密版本号: {encryptedBytes[4]:X2}");
}
}
// 使用LocalInfoEncrypt.Decrypt进行解密
var decryptedBytes = PhxhSDK.AOT.VersionUpdate.LocalInfoEncrypt.Decrypt(encryptedBytes, out bool isEncrypted);
Debug.Log($"解密完成,数据长度: {decryptedBytes.Length} 字节,是否加密: {isEncrypted}");
// 输出解密后前100个字节
var decryptedPreview = new StringBuilder();
decryptedPreview.Append("解密后数据预览 (前100字节): ");
for (int i = 0; i < Math.Min(100, decryptedBytes.Length); i++)
{
if (decryptedBytes[i] >= 32 && decryptedBytes[i] <= 126)
decryptedPreview.Append((char)decryptedBytes[i]);
else
decryptedPreview.Append($"[{decryptedBytes[i]:X2}]");
}
Debug.Log(decryptedPreview.ToString());
// 转换为字符串假设是JSON格式
var jsonString = Encoding.UTF8.GetString(decryptedBytes);
Debug.Log($"解密后的JSON数据 (前500字符):\n{jsonString.Substring(0, Math.Min(500, jsonString.Length))}");
// 验证是否为有效JSON
bool isValidJson = PhxhSDK.AOT.VersionUpdate.LocalInfoEncrypt.IsValidJson(decryptedBytes);
Debug.Log($"是否为有效JSON: {isValidJson}");
// 保存到新文件中
File.WriteAllBytes(outputPath + ".bytes", decryptedBytes); // 同时保存字节文件
File.WriteAllText(outputPath, jsonString, Encoding.UTF8);
Debug.Log($"解密后的数据已保存到: {outputPath}");
Debug.Log($"同时保存字节文件到: {outputPath}.bytes");
// 成功提示
if (isValidJson)
{
EditorUtility.DisplayDialog("解密成功", $"文件已成功解密并保存到:\n{outputPath}\n\n数据是有效的JSON格式", "确定");
}
else
{
EditorUtility.DisplayDialog("解密警告", $"文件已解密并保存到:\n{outputPath}\n\n但数据可能不是有效的JSON格式请检查", "确定");
}
}
catch (Exception ex)
{
Debug.LogError($"解密失败: {ex.Message}\n{ex.StackTrace}");
EditorUtility.DisplayDialog("解密失败", $"解密过程中出现错误:\n{ex.Message}", "确定");
}
}
[MenuItem("Test/测试加密")]
public static void Test()
{
LTSimpleKey.InitKeys();
LTSimpleEncrypt.InitAES(LTSimpleKey.key1, LTSimpleKey.key2);
var pathParent = "C:/Users/82158/Documents/WXWork/1688856871498430/Cache/File/2025-05";
var srcName = "defaultpackage_assets_hotupdatedlls_hotupdatedll.bundle";
var loadSrcBytes = File.ReadAllBytes(pathParent + "/" + srcName);
var key3 = System.Text.Encoding.UTF8.GetBytes(Hash128.Compute(srcName).ToString());
var encryptBytes = LTSimpleEncrypt.EncryptBytes(key3, loadSrcBytes);
var decryptBytesTest = new byte[loadSrcBytes.Length];
for (int i = 0; i < encryptBytes.Length; i++)
{
var encryptByte = encryptBytes[i];
var decryptByte = LTSimpleEncrypt.DecryptByte(encryptByte, key3, i);
decryptBytesTest[i] = decryptByte;
}
var isSameTest = true;
for (int i = 0; i < loadSrcBytes.Length; i++)
{
var loadSrcByte = loadSrcBytes[i];
var decryptByte = decryptBytesTest[i];
if (loadSrcByte != decryptByte)
{
isSameTest = false;
break;
}
}
Debug.LogError("单字节解密测试结果:" + isSameTest);
// 把加密后的数据写入到一个新文件中
var saveName = "encrypt_" + srcName;
var savePath = pathParent + "/" + saveName;
File.WriteAllBytes(savePath, encryptBytes);
using (var customFs = new CustomFileStream(savePath, key3))
{
// 测试1: 传统测试 - 从头读取完整文件
var decryptBytes = new byte[loadSrcBytes.Length];
customFs.Read(decryptBytes, 0, decryptBytes.Length);
var isSame = true;
for (int i = 0; i < loadSrcBytes.Length; i++)
{
var loadSrcByte = loadSrcBytes[i];
var decryptByte = decryptBytes[i];
if (loadSrcByte != decryptByte)
{
isSame = false;
break;
}
}
Debug.LogError("传统测试结果:" + isSame);
// 测试2: Seek测试 - 先读取后一半(通过seek),再读取前一半,拼到一起
customFs.Seek(loadSrcBytes.Length / 2, SeekOrigin.Begin);
var decryptBytes2 = new byte[loadSrcBytes.Length];
var readLen2 = customFs.Read(decryptBytes2, loadSrcBytes.Length / 2, loadSrcBytes.Length / 2);
customFs.Seek(0, SeekOrigin.Begin);
var readLen1 = customFs.Read(decryptBytes2, 0, loadSrcBytes.Length / 2);
Debug.LogError("读取长度:" + readLen1 + " , " + readLen2);
isSame = true;
for (int i = 0; i < loadSrcBytes.Length; i++)
{
var loadSrcByte = loadSrcBytes[i];
var decryptByte = decryptBytes2[i];
if (loadSrcByte != decryptByte)
{
isSame = false;
break;
}
}
Debug.LogError("Seek测试结果:" + isSame);
}
}
}