-
Notifications
You must be signed in to change notification settings - Fork 5
/
ExtractProjectInformationUtility.cs
167 lines (136 loc) · 6.88 KB
/
ExtractProjectInformationUtility.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using UnityEditor;
using UnityEngine;
using Object = UnityEngine.Object;
namespace Nomnom.LCProjectPatcher.Editor {
public static class ExtractProjectInformationUtility {
private readonly static Regex GuidPattern = new(@"guid:\s(?<guid>[0-9A-Za-z]+)", RegexOptions.Compiled);
[MenuItem("Tools/Nomnom/LC - Project Patcher/Extract Project Information")]
public static ExtractedResults ExtractProjectInformation() {
var outputFilePath = EditorUtility.SaveFilePanel("Save Project Information", "", "ProjectInformation", "json");
if (string.IsNullOrEmpty(outputFilePath)) {
return default;
}
var results = CreateExtractedResults(true);
var json = JsonUtility.ToJson(results, true);
System.IO.File.WriteAllText(outputFilePath, json);
Debug.Log($"Extracted {results.guids.Length} guids to {outputFilePath}");
EditorUtility.RevealInFinder(outputFilePath);
return results;
}
public static ExtractedResults CreateExtractedResults(bool onlyInProjectAssets) {
Debug.Log("Extracting project information...");
var scriptGuids = GetScriptResults(onlyInProjectAssets);
var assetGuids = GetAssetResults(onlyInProjectAssets);
var results = new ExtractedResults();
results.guids = scriptGuids.ToArray();
results.assetGuids = assetGuids.ToArray();
return results;
}
private static IEnumerable<GuidResult> GetScriptResults(bool onlyInProjectAssets) {
var allMonoScripts = !onlyInProjectAssets
? AssetDatabase.FindAssets("t:MonoScript")
// : AssetDatabase.FindAssets("t:MonoScript", new[] { "Assets/LethalCompany" });
: AssetDatabase.FindAssets("t:MonoScript", new[] { "Assets" });
var badGuids = 0;
for (var i = 0; i < allMonoScripts.Length; i++) {
var guid = allMonoScripts[i];
var assetPath = AssetDatabase.GUIDToAssetPath(guid);
EditorUtility.DisplayProgressBar("Extracting Project Information", $"Processing {assetPath}", (float)i / allMonoScripts.Length);
var obj = AssetDatabase.LoadAssetAtPath<Object>(assetPath);
if (obj is not MonoScript monoScript) continue;
var objType = monoScript.GetClass();
if (objType == null) {
badGuids++;
continue;
}
yield return new GuidResult(objType.Assembly.FullName, objType.FullName, guid);
}
EditorUtility.ClearProgressBar();
}
private static IEnumerable<AssetGuidResult> GetAssetResults(bool onlyInProjectAssets) {
// var settings = ModuleUtility.GetPatcherSettings();
// var gamePath = settings.GetLethalCompanyGamePath();
// string sos;
// if (settings.AssetRipperSettings.TryGetMapping("MonoBehaviour", out var finalFolder)) {
// sos = Path.Combine(settings.GetLethalCompanyGamePath(), finalFolder);
// } else {
// sos = Path.Combine(settings.GetLethalCompanyGamePath(), "MonoBehaviour");
// }
var allAssets = !onlyInProjectAssets
//? AssetDatabase.FindAssets("t:Object", new[] { gamePath })
? AssetDatabase.FindAssets("t:Object")
: AssetDatabase.FindAssets("t:Object", new[] { "Assets" });
for (var i = 0; i < allAssets.Length; i++) {
var guid = allAssets[i];
var assetPath = AssetDatabase.GUIDToAssetPath(guid);
if (assetPath.EndsWith(".shadergraph")) {
continue;
}
EditorUtility.DisplayProgressBar("Extracting Project Information", $"Processing {assetPath}", (float)i / allAssets.Length);
var fullAssetPath = Path.GetFullPath(assetPath);
var metaPath = fullAssetPath + ".meta";
if (!File.Exists(metaPath)) {
Debug.LogWarning($"No meta file found for {assetPath}");
continue;
}
Object obj = null;
try {
obj = AssetDatabase.LoadAssetAtPath<Object>(assetPath);
} catch (Exception e) {
Debug.LogWarning($"Error loading {assetPath}: {e.Message}");
}
if (!obj) continue;
var metaContent = File.ReadAllText(metaPath);
var match = GuidPattern.Match(metaContent);
if (!match.Success) {
continue;
}
var actualGuid = match.Groups["guid"].Value;
if (string.IsNullOrEmpty(actualGuid)) {
continue;
}
// if (AssetDatabase.TryGetGUIDAndLocalFileIdentifier(obj, out _, out long fileId)) {
// yield return new AssetGuidResult(assetPath, actualGuid, fileId.ToString());
// }
yield return new AssetGuidResult(assetPath, actualGuid, null);
}
EditorUtility.ClearProgressBar();
}
public static ExtractedResults GetExtractedResults(string filePath) {
var json = System.IO.File.ReadAllText(filePath);
return JsonUtility.FromJson<ExtractedResults>(json);
}
[System.Serializable]
public class ExtractedResults {
public GuidResult[] guids = Array.Empty<GuidResult>();
public AssetGuidResult[] assetGuids = Array.Empty<AssetGuidResult>();
}
[System.Serializable]
public struct GuidResult {
public string assemblyName;
public string fullTypeName;
public string originalGuid;
public GuidResult(string assemblyName, string fullTypeName, string originalGuid) {
this.assemblyName = assemblyName;
this.fullTypeName = fullTypeName;
this.originalGuid = originalGuid;
}
}
[System.Serializable]
public struct AssetGuidResult {
public string assetPath;
public string originalGuid;
public string fileId;
public AssetGuidResult(string assetPath, string originalGuid, string fileId) {
this.assetPath = assetPath;
this.originalGuid = originalGuid;
this.fileId = fileId;
}
}
}
}