mirror of
https://github.com/StarBeat/UnityDependencyAnalyzer.git
synced 2026-03-08 05:35:27 +08:00
Compare commits
4 Commits
d2239d7f29
...
835ede37f6
| Author | SHA1 | Date | |
|---|---|---|---|
| 835ede37f6 | |||
| dcac85599f | |||
| 0f2937a67a | |||
| 3dd9d23981 |
21
LICENSE
Normal file
21
LICENSE
Normal file
@ -0,0 +1,21 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2025 StarBeats
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
@ -1,4 +1,5 @@
|
||||
using MongoDB.Bson.Serialization.Attributes;
|
||||
using MemoryPack;
|
||||
using MongoDB.Bson.Serialization.Attributes;
|
||||
using MongoDB.Driver;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
@ -8,7 +9,8 @@ using System.Text.Json.Serialization;
|
||||
namespace AssetDependencyGraph
|
||||
{
|
||||
[BsonIgnoreExtraElements]
|
||||
public class AssetIdentify
|
||||
[MemoryPackable]
|
||||
public partial class AssetIdentify
|
||||
{
|
||||
public string Path = null!;
|
||||
public string AssetType = null!;
|
||||
@ -44,7 +46,8 @@ namespace AssetDependencyGraph
|
||||
}
|
||||
|
||||
[BsonIgnoreExtraElements]
|
||||
public class AssetNode
|
||||
[MemoryPackable]
|
||||
public partial class AssetNode
|
||||
{
|
||||
public AssetIdentify Self=null!;
|
||||
public string AssetType=null!;
|
||||
|
||||
@ -1,4 +1,5 @@
|
||||
using MongoDB.Bson.Serialization.Attributes;
|
||||
using MemoryPack;
|
||||
using MongoDB.Bson.Serialization.Attributes;
|
||||
using MongoDB.Bson.Serialization.Options;
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
@ -239,7 +240,7 @@ namespace AssetDependencyGraph
|
||||
[BsonDictionaryOptions(Representation = DictionaryRepresentation.ArrayOfArrays)]
|
||||
private ConcurrentDictionary<AssetIdentify, AssetNode> assetIdentify2AssetNodeDic = new();
|
||||
private ConcurrentDictionary<string, AssetIdentify> path2Id = new();
|
||||
private List<(string path, bool isDir)> allPath = new();
|
||||
private ConcurrentBag<(string path, bool isDir)> allPath = new();
|
||||
private UnityLmdb unityLmdb;
|
||||
private static Regex isGuid = new Regex("^[\\da-f]{32}$");
|
||||
|
||||
@ -335,19 +336,19 @@ namespace AssetDependencyGraph
|
||||
unityLmdb.ResolveGuidPath();
|
||||
}
|
||||
|
||||
public void AnalyzeMainProcess(string rootFolder, int processCnt = 8)
|
||||
public async ValueTask AnalyzeMainProcess(string projectPath, string rootFolder, int processCnt = 8)
|
||||
{
|
||||
Stopwatch sw = Stopwatch.StartNew();
|
||||
sw.Start();
|
||||
Utils.TraverseDirectory(rootFolder, Visivt, -1);
|
||||
Utils.TraverseDirectoryParallel(rootFolder, Visivt);
|
||||
sw.Stop();
|
||||
Console.WriteLine($"遍历目录耗时:{sw.ElapsedMilliseconds / 1000f}s");
|
||||
|
||||
sw.Restart();
|
||||
var itemCnt = allPath.Count / processCnt;
|
||||
List<string> subProcessArgs = new();
|
||||
List<string> resultJsonPaths = new();
|
||||
var projectPath = Environment.GetCommandLineArgs()[1];
|
||||
List<string> resultPaths = new();
|
||||
var allPathArray = allPath.ToArray();
|
||||
for (int i = 0; i < processCnt; i++)
|
||||
{
|
||||
int r = (itemCnt * (i + 1));
|
||||
@ -356,11 +357,11 @@ namespace AssetDependencyGraph
|
||||
r = allPath.Count;
|
||||
}
|
||||
|
||||
var s = JsonSerializer.Serialize(allPath[(i * itemCnt)..r], options);
|
||||
var s = JsonSerializer.Serialize(allPathArray[(i * itemCnt)..r], options);
|
||||
var jsonPath = Path.Combine(Path.GetTempPath(), $"path{i}.json");
|
||||
var resulJsonPath = Path.Combine(Path.GetTempPath(), $"result{i}.json");
|
||||
resultJsonPaths.Add(resulJsonPath);
|
||||
subProcessArgs.Add($"-reference {projectPath} SubProcess {jsonPath} {resulJsonPath}");
|
||||
var resulPath = Path.Combine(Path.GetTempPath(), $"result{i}.bin");
|
||||
resultPaths.Add(resulPath);
|
||||
subProcessArgs.Add($"-reference {projectPath} SubProcess {jsonPath} {resulPath}");
|
||||
File.WriteAllText(jsonPath, s);
|
||||
}
|
||||
|
||||
@ -400,10 +401,10 @@ namespace AssetDependencyGraph
|
||||
|
||||
Task.WaitAll(subProcessTask);
|
||||
List<Dictionary<string, HashSet<string>>> subProcessResults = new();
|
||||
foreach (var item in resultJsonPaths)
|
||||
foreach (var item in resultPaths)
|
||||
{
|
||||
var s = File.ReadAllText(item);
|
||||
subProcessResults.Add(JsonSerializer.Deserialize<Dictionary<string, HashSet<string>>>(s, options)!);
|
||||
var s = File.ReadAllBytes(item);
|
||||
subProcessResults.Add(MemoryPackSerializer.Deserialize<Dictionary<string, HashSet<string>>>(s.AsSpan())!);
|
||||
}
|
||||
sw.Stop();
|
||||
Console.WriteLine($"分析引用耗时:{sw.ElapsedMilliseconds / 1000f}s");
|
||||
@ -418,14 +419,10 @@ namespace AssetDependencyGraph
|
||||
item.Value.DependentSet = item.Value.Dependent.ToHashSet();
|
||||
}
|
||||
|
||||
string js = JsonSerializer.Serialize(assetIdentify2AssetNodeDic, options: new()
|
||||
{
|
||||
IncludeFields = true,
|
||||
Converters = { new AssetIdentifyJsonConverter(), new AssetNodeJsonConverter() }
|
||||
});
|
||||
|
||||
|
||||
File.WriteAllText(Path.Combine(UnityLmdb.ProjPath, "Library", "dependencyGraph.json"), js);
|
||||
using var wr = File.OpenWrite(Path.Combine(UnityLmdb.ProjPath, "Library", "dependencyGraph.bin"));
|
||||
await MemoryPackSerializer.SerializeAsync(wr, assetIdentify2AssetNodeDic);
|
||||
sw.Stop();
|
||||
Console.WriteLine($"写入文件耗时:{sw.ElapsedMilliseconds / 1000f}s");
|
||||
|
||||
//AssetDependencyGraphDB db = new AssetDependencyGraphDB(Environment.GetCommandLineArgs()[2], Environment.GetCommandLineArgs()[3], Environment.GetCommandLineArgs()[4]);
|
||||
//sw.Restart();
|
||||
@ -434,8 +431,8 @@ namespace AssetDependencyGraph
|
||||
//{
|
||||
// db.Insert(item.Value);
|
||||
//});
|
||||
sw.Stop();
|
||||
Console.WriteLine($"更新数据库:{sw.ElapsedMilliseconds / 1000f}s");
|
||||
//sw.Stop();
|
||||
//Console.WriteLine($"更新数据库:{sw.ElapsedMilliseconds / 1000f}s");
|
||||
}
|
||||
|
||||
private void ResolveSubProcessResult(Dictionary<string, HashSet<string>> subProcessResult)
|
||||
@ -491,25 +488,26 @@ namespace AssetDependencyGraph
|
||||
});
|
||||
}
|
||||
|
||||
public void AnalyzeSubProcess(string pathFile, string resultFilePath)
|
||||
public async ValueTask AnalyzeSubProcess(string pathFile, string resultFilePath)
|
||||
{
|
||||
var s = File.ReadAllText(pathFile);
|
||||
allPath = JsonSerializer.Deserialize<List<(string path, bool isDir)>>(s, options)!;
|
||||
var allPath = JsonSerializer.Deserialize<List<(string path, bool isDir)>>(s, options)!;
|
||||
if (allPath != null)
|
||||
{
|
||||
foreach (var item in allPath)
|
||||
for (int i = 0; i < allPath.Count; i++)
|
||||
{
|
||||
var path = allPath[i];
|
||||
foreach (var item1 in dependencyAnalysisDic)
|
||||
{
|
||||
if (item1.Key(item))
|
||||
if (item1.Key(path))
|
||||
{
|
||||
item1.Value.Analyze(item.path, path2Dependences);
|
||||
item1.Value.Analyze(path.path, path2Dependences);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var json = JsonSerializer.Serialize(path2Dependences, options);
|
||||
File.WriteAllText(resultFilePath, json);
|
||||
using var wr = File.OpenWrite(resultFilePath);
|
||||
await MemoryPackSerializer.SerializeAsync(wr, path2Dependences);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Binary file not shown.
@ -34,14 +34,15 @@ switch (Environment.GetCommandLineArgs()[1])
|
||||
{
|
||||
UnityLmdb.ProjPath = Environment.GetCommandLineArgs()[2];
|
||||
Utils.DataPath = Path.Combine(UnityLmdb.ProjPath, "Assets").ToUniversalPath();
|
||||
|
||||
Console.WriteLine(string.Join(' ', Environment.GetCommandLineArgs()));
|
||||
if (Environment.GetCommandLineArgs().Length > 3 && Environment.GetCommandLineArgs()[3].Equals("SubProcess"))
|
||||
{
|
||||
new DependencyAnalyzer().AnalyzeSubProcess(Environment.GetCommandLineArgs()[4], Environment.GetCommandLineArgs()[5]);
|
||||
//System.Diagnostics.Process.GetCurrentProcess().ProcessorAffinity = (IntPtr)0x00FF;
|
||||
await new DependencyAnalyzer().AnalyzeSubProcess(Environment.GetCommandLineArgs()[4], Environment.GetCommandLineArgs()[5]);
|
||||
}
|
||||
else
|
||||
{
|
||||
new DependencyAnalyzer().AnalyzeMainProcess(Utils.DataPath, 10);
|
||||
await new DependencyAnalyzer().AnalyzeMainProcess(UnityLmdb.ProjPath, Utils.DataPath, 10);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
@ -2,7 +2,7 @@
|
||||
"profiles": {
|
||||
"UnityDependencyAnalyzer": {
|
||||
"commandName": "Project",
|
||||
"commandLineArgs": "G:/G_android \" \" \" \" localhost"
|
||||
"commandLineArgs": "-reference G:/G_android \" \" \" \" localhost"
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -8,6 +8,10 @@
|
||||
<PublishAot>False</PublishAot>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="MemoryPack" Version="1.21.4" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\UnityFileDumper\UnityFileDumper.csproj" />
|
||||
</ItemGroup>
|
||||
@ -51,4 +55,8 @@
|
||||
</Reference>
|
||||
</ItemGroup>
|
||||
|
||||
<Target Name="PostBuild" AfterTargets="PostBuildEvent">
|
||||
<Exec Command="copy /Y $(ProjectDir)\Libs\lmdb.dll $(TargetDir)
copy /Y $(ProjectDir)\Libs\UnityFileSystemApi.dll $(TargetDir)" />
|
||||
</Target>
|
||||
|
||||
</Project>
|
||||
|
||||
@ -25,8 +25,26 @@ namespace AssetDependencyGraph
|
||||
}
|
||||
}
|
||||
|
||||
public static void TraverseDirectoryParallel(string path, Action<string> action)
|
||||
{
|
||||
Parallel.ForEach(Directory.EnumerateFileSystemEntries(path, "*", SearchOption.AllDirectories), action);
|
||||
}
|
||||
|
||||
public static void TraverseDirectory(string path, Action<string> action, int depth = 1)
|
||||
{
|
||||
//if (depth == -1)
|
||||
//{
|
||||
// //foreach (string file in Directory.EnumerateFiles(path, "*.*", SearchOption.AllDirectories))
|
||||
// //{
|
||||
// // action.Invoke(file);
|
||||
// //}
|
||||
|
||||
// //foreach (string directory in Directory.EnumerateDirectories(path, "*.*", SearchOption.AllDirectories))
|
||||
// //{
|
||||
// // action.Invoke(directory);
|
||||
// //}
|
||||
//}
|
||||
|
||||
if (depth == 0)
|
||||
{
|
||||
return;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user