增加文件校验

This commit is contained in:
夏云 2023-12-26 18:06:11 +08:00
parent c4e5b14bac
commit fcde6485b6
6 changed files with 165 additions and 9 deletions

View File

@ -0,0 +1,54 @@
using System;
using System.Security.Cryptography;
using System.Text;
using System.Xml;
namespace FileChecker
{
internal class FileValdate
{
public static bool Valdate(string rootPath, string resPath)
{
try
{
string path = rootPath + "\\" + resPath;
string text = System.IO.File.ReadAllText(path);
byte[] data = Convert.FromBase64String(text);
byte[] buffer = new byte[data.Length - 4];
Array.Copy(data, 2, buffer, 0, data.Length - 4);
text = Encoding.UTF8.GetString(buffer);
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(text);
XmlElement root = (XmlElement)xmlDoc.ChildNodes[0];
var fileNodes = root.ChildNodes;
foreach (XmlNode node in fileNodes)
{
XmlElement fileNode = (XmlElement)node;
string filePath = rootPath + "\\" + fileNode.GetAttribute("path");
string fileCode = fileNode.GetAttribute("code");
string realCode = GetFileHash(filePath);
if (!fileCode.Equals(realCode))
return false;
}
return true;
}
catch { return false; }
}
public static string GetFileHash(string filePath)
{
string hash = "ABCD123";
using (var md5 = MD5.Create())
{
using (var stream = System.IO.File.OpenRead(filePath))
{
byte[] hashBytes = md5.ComputeHash(stream);
hash = BitConverter.ToString(hashBytes).Replace("-", "").ToLower();
}
}
return hash;
}
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 803f09ab69219b54f8e99f1eb4213aa9
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

33
Assets/Scripts/ValDate.cs Normal file
View File

@ -0,0 +1,33 @@
using FileChecker;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ValDate : MonoBehaviour
{
string dirPath = "";
string resPath = "App_Data\\StreamingAssets\\file.res";
bool fileChecked = false;
void Awake()
{
dirPath = AppDomain.CurrentDomain.BaseDirectory;
fileChecked = FileValdate.Valdate(dirPath, resPath);
Debug.Log("====================");
Debug.Log(dirPath + "\\" + resPath);
Debug.Log(fileChecked);
Debug.Log("====================");
if (!fileChecked)
{
Application.Quit();
}
}
//void OnGUI()
//{
// GUILayout.Label(dirPath);
// GUILayout.Label(resPath);
// GUILayout.Label(fileChecked.ToString());
//}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 25fe509e1c5026e47b21c210e52f5fa5
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 1bceb84a4caa12747ba3e677114d677b
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -38,12 +38,12 @@ RenderSettings:
m_ReflectionIntensity: 1
m_CustomReflection: {fileID: 0}
m_Sun: {fileID: 0}
m_IndirectSpecularColor: {r: 0.37311953, g: 0.38074014, b: 0.3587274, a: 1}
m_IndirectSpecularColor: {r: 0, g: 0, b: 0, a: 1}
m_UseRadianceAmbientProbe: 0
--- !u!157 &3
LightmapSettings:
m_ObjectHideFlags: 0
serializedVersion: 12
serializedVersion: 11
m_GIWorkflowMode: 1
m_GISettings:
serializedVersion: 2
@ -98,13 +98,13 @@ LightmapSettings:
m_TrainingDataDestination: TrainingData
m_LightProbeSampleCountMultiplier: 4
m_LightingDataAsset: {fileID: 0}
m_LightingSettings: {fileID: 0}
m_UseShadowmask: 1
--- !u!196 &4
NavMeshSettings:
serializedVersion: 2
m_ObjectHideFlags: 0
m_BuildSettings:
serializedVersion: 3
serializedVersion: 2
agentTypeID: 0
agentRadius: 0.5
agentHeight: 2
@ -117,9 +117,7 @@ NavMeshSettings:
cellSize: 0.16666667
manualTileSize: 0
tileSize: 256
buildHeightMesh: 0
maxJobWorkers: 0
preserveTilesOutsideBounds: 0
accuratePlacement: 0
debug:
m_Flags: 0
m_NavMeshData: {fileID: 0}
@ -162,7 +160,6 @@ Transform:
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: -0.6491707, y: -3.7765083, z: 13.72781}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 1
@ -194,7 +191,6 @@ Transform:
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 0
@ -212,3 +208,46 @@ MonoBehaviour:
m_Name:
m_EditorClassIdentifier:
AnimName:
--- !u!1 &1828764152
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 1828764154}
- component: {fileID: 1828764153}
m_Layer: 0
m_Name: ValObject
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!114 &1828764153
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1828764152}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 25fe509e1c5026e47b21c210e52f5fa5, type: 3}
m_Name:
m_EditorClassIdentifier:
--- !u!4 &1828764154
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1828764152}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 2
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}