///
// DllSwitcher
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using UnityEditor;
using UnityEngine;
public class DllSwitcher : EditorWindow
{
private enum PathType
{
Reference,
AbsolutePath
}
private enum DirectoryType
{
Root,
SpecificDirectory
}
public const string DEFAULT_FILE_ID_OF_SCRIPT = "11500000";
public Object dllFile;
public Object replaceDir;
public Object srcDir;
private Dictionary<string, string> fileIDMappingTableFromDll;
private Dictionary<string, string> guidMappingTableFromScripts;
private string guidOfDllFile;
private string dllFilePath;
private const int PreLabelWidth = 140;
private PathType dllInputPath;
private DirectoryType srcDirectory;
private DirectoryType resDirectory;
[MenuItem("Window/DllSwitcher")]
public static void ShowWindow()
{
EditorWindow.GetWindow(typeof(DllSwitcher));
}
private void OnGUI()
{
//IL_0006: Unknown result type (might be due to invalid IL or missing references)
//IL_00b2: Unknown result type (might be due to invalid IL or missing references)
//IL_0145: Unknown result type (might be due to invalid IL or missing references)
EditorGUILayout.BeginHorizontal((GUILayoutOption[])(object)new GUILayoutOption[0]);
EditorGUILayout.LabelField("Dll File", (GUILayoutOption[])(object)new GUILayoutOption[1] { GUILayout.MaxWidth(140f) });
dllInputPath = (PathType)(object)EditorGUILayout.EnumPopup((Enum)dllInputPath, (GUILayoutOption[])(object)new GUILayoutOption[1] { GUILayout.ExpandWidth(true) });
EditorGUILayout.EndHorizontal();
if (dllInputPath.Equals(PathType.Reference))
{
dllFile = EditorGUILayout.ObjectField(dllFile, typeof(Object), false, (GUILayoutOption[])(object)new GUILayoutOption[0]);
}
else
{
dllFilePath = EditorGUILayout.TextField(dllFilePath, (GUILayoutOption[])(object)new GUILayoutOption[0]);
}
EditorGUILayout.BeginHorizontal((GUILayoutOption[])(object)new GUILayoutOption[0]);
EditorGUILayout.LabelField("Source Code Directory", (GUILayoutOption[])(object)new GUILayoutOption[1] { GUILayout.MaxWidth(140f) });
srcDirectory = (DirectoryType)(object)EditorGUILayout.EnumPopup((Enum)srcDirectory, (GUILayoutOption[])(object)new GUILayoutOption[1] { GUILayout.ExpandWidth(true) });
EditorGUILayout.EndHorizontal();
if (srcDirectory.Equals(DirectoryType.SpecificDirectory))
{
srcDir = EditorGUILayout.ObjectField(srcDir, typeof(Object), false, (GUILayoutOption[])(object)new GUILayoutOption[0]);
}
EditorGUILayout.BeginHorizontal((GUILayoutOption[])(object)new GUILayoutOption[0]);
EditorGUILayout.LabelField("Replace Dirctory", (GUILayoutOption[])(object)new GUILayoutOption[1] { GUILayout.MaxWidth(140f) });
resDirectory = (DirectoryType)(object)EditorGUILayout.EnumPopup((Enum)resDirectory, (GUILayoutOption[])(object)new GUILayoutOption[1] { GUILayout.ExpandWidth(true) });
EditorGUILayout.EndHorizontal();
if (resDirectory.Equals(DirectoryType.SpecificDirectory))
{
replaceDir = EditorGUILayout.ObjectField(replaceDir, typeof(Object), false, (GUILayoutOption[])(object)new GUILayoutOption[0]);
}
if (GUILayout.Button("Replace From Dll To Src", (GUILayoutOption[])(object)new GUILayoutOption[0]))
{
replaceSriptReference(dllToSrc: true);
}
if (GUILayout.Button("Replace From Src To Dll", (GUILayoutOption[])(object)new GUILayoutOption[0]))
{
replaceSriptReference(dllToSrc: false);
}
}
public void replaceSriptReference(bool dllToSrc)
{
if (resDirectory.Equals(DirectoryType.SpecificDirectory))
{
replaceSriptReferenceOfSelectDirectory(dllToSrc);
}
else
{
replaceSriptReferenceOfAllScripts(dllToSrc);
}
}
public void replaceSriptReferenceOfSelectDirectory(bool dllToSrc = true)
{
string assetPath = AssetDatabase.GetAssetPath(replaceDir);
initFileIDMappingTableOfDll(dllToSrc);
InitGuidMappingTable(dllToSrc);
ReplaceSriptReferenceOfPath(assetPath, dllToSrc);
}
public void replaceSriptReferenceOfAllScripts(bool dllToSrc = true)
{
initFileIDMappingTableOfDll(dllToSrc);
InitGuidMappingTable(dllToSrc);
ReplaceSriptReferenceOfPath(Application.get_dataPath(), dllToSrc);
}
private void ReplaceSriptReferenceOfPath(string path, bool dllToSrc = true)
{
List<string> list = FindAllFileWithSuffixs(path, new string[3] { ".asset", ".prefab", ".unity" });
for (int i = 0; i < list.Count; i++)
{
EditorUtility.DisplayProgressBar("Replace Dll", list[i], (float)i * 1f / (float)list.Count);
ReplaceScriptReference(list[i], dllToSrc);
}
AssetDatabase.Refresh();
EditorUtility.ClearProgressBar();
}
private void initFileIDMappingTableOfDll(bool dllToSrc = true)
{
if (dllInputPath.Equals(PathType.Reference))
{
dllFilePath = AssetDatabase.GetAssetPath(dllFile);
}
fileIDMappingTableFromDll = new Dictionary<string, string>();
Assembly assembly = Assembly.LoadFrom(dllFilePath);
Type[] array = null;
try
{
array = assembly.GetTypes();
}
catch (ReflectionTypeLoadException ex)
{
array = ex.Types.Where((Type t) => t != null).ToArray();
Exception[] loaderExceptions = ex.LoaderExceptions;
for (int i = 0; i < loaderExceptions.Length; i++)
{
Debug.LogWarning((object)loaderExceptions[i]);
}
}
Type[] array2 = array;
foreach (Type type in array2)
{
if (dllToSrc)
{
if (fileIDMappingTableFromDll.ContainsKey(FileIDUtil.Compute(type).ToString()))
{
Debug.LogWarning((object)("Reduplicated GUID:" + FileIDUtil.Compute(type) + ";Script Name:" + type.Name));
}
else
{
fileIDMappingTableFromDll[FileIDUtil.Compute(type).ToString()] = type.Name;
}
}
else
{
fileIDMappingTableFromDll[type.Name] = FileIDUtil.Compute(type).ToString();
}
}
if (!dllToSrc)
{
initGuidOfDllFile();
}
}
private Assembly CurrentDomain_ReflectionOnlyAssemblyResolve(object sender, ResolveEventArgs args)
{
Debug.LogWarning((object)("Need Loading:" + args.Name));
return Assembly.ReflectionOnlyLoad(dllFilePath.Substring(0, dllFilePath.LastIndexOf("\\")) + "\\" + args.Name);
}
private void InitGuidMappingTable(bool dllToSrc)
{
if (srcDirectory.Equals(DirectoryType.SpecificDirectory))
{
InitGuidMappingTableOfSelectScripts(dllToSrc);
}
else
{
InitGuidMappingTableOfAllScripts(dllToSrc);
}
}
private void InitGuidMappingTableOfAllScripts(bool dllToSrc = true)
{
guidMappingTableFromScripts = new Dictionary<string, string>();
InitGuidMappingTableOfPath(Application.get_dataPath(), dllToSrc);
}
private void InitGuidMappingTableOfSelectScripts(bool dllToSrc = true)
{
guidMappingTableFromScripts = new Dictionary<string, string>();
string assetPath = AssetDatabase.GetAssetPath(srcDir);
InitGuidMappingTableOfPath(assetPath, dllToSrc);
}
private void InitGuidMappingTableOfPath(string path, bool dllToSrc = true)
{
foreach (string item in FindAllFileWithSuffixs(path, new string[2] { ".cs.meta", ".js.meta" }))
{
if (dllToSrc)
{
guidMappingTableFromScripts[getFileNameFromPath(item)] = GetGuidFromMeta(item);
}
else
{
guidMappingTableFromScripts[GetGuidFromMeta(item)] = getFileNameFromPath(item);
}
}
}
private void initGuidOfDllFile()
{
guidOfDllFile = AssetDatabase.AssetPathToGUID(AssetDatabase.GetAssetPath(dllFile));
}
private void ReplaceScriptReference(string filePath, bool dllToSrc = true)
{
Debug.Log((object)("Ready to replace:" + filePath));
string[] array = File.ReadAllLines(filePath);
int i = 0;
bool flag = false;
for (; i < array.Length; i++)
{
if (array[i].StartsWith("MonoBehaviour:"))
{
do
{
i++;
}
while (!array[i].TrimStart().StartsWith("m_Script:"));
flag = ((!dllToSrc) ? (flag | replaceGUIDAnfFileIDFromSrcToDll(ref array[i])) : (flag | replaceGUIDAnfFileIDFromDllToSrc(ref array[i])));
}
}
if (flag)
{
File.WriteAllLines(filePath, array);
}
}
private string GetGuidFromMeta(string filePath)
{
string result = "";
using StreamReader streamReader = new StreamReader(filePath);
while (!streamReader.EndOfStream)
{
string text = streamReader.ReadLine();
if (text.StartsWith("guid:"))
{
result = text.Substring(text.IndexOf(":") + 2);
break;
}
}
streamReader.Close();
return result;
}
private bool replaceGUIDAnfFileIDFromDllToSrc(ref string lineStr)
{
bool result = false;
string fileIDFrommScriptReferenceLine = getFileIDFrommScriptReferenceLine(lineStr);
if (fileIDFrommScriptReferenceLine == null || fileIDFrommScriptReferenceLine.Equals("11500000"))
{
return false;
}
if (fileIDMappingTableFromDll.TryGetValue(fileIDFrommScriptReferenceLine, out var value))
{
if (guidMappingTableFromScripts.TryGetValue(value, out var value2))
{
string gUIDFrommScriptReferenceLine = getGUIDFrommScriptReferenceLine(lineStr);
Debug.Log((object)("Replacing script reference:" + value));
lineStr = lineStr.Replace(fileIDFrommScriptReferenceLine, "11500000");
lineStr = lineStr.Replace(gUIDFrommScriptReferenceLine, value2);
result = true;
}
else
{
Debug.LogWarning((object)("Can't find the GUID of file:" + value));
}
}
else
{
Debug.LogWarning((object)("Can't find the file of fileID:" + fileIDFrommScriptReferenceLine));
}
return result;
}
private bool replaceGUIDAnfFileIDFromSrcToDll(ref string lineStr)
{
bool result = false;
string gUIDFrommScriptReferenceLine = getGUIDFrommScriptReferenceLine(lineStr);
if (gUIDFrommScriptReferenceLine == null)
{
return false;
}
if (guidMappingTableFromScripts.TryGetValue(gUIDFrommScriptReferenceLine, out var value))
{
if (fileIDMappingTableFromDll.TryGetValue(value, out var value2))
{
Debug.Log((object)("Replacing script reference:" + value));
lineStr = lineStr.Replace("11500000", value2);
lineStr = lineStr.Replace(gUIDFrommScriptReferenceLine, guidOfDllFile);
result = true;
}
else
{
Debug.LogWarning((object)("Can't find the GUID of file:" + value));
}
}
else
{
Debug.LogWarning((object)("Can't find the file of GUID:" + gUIDFrommScriptReferenceLine));
}
return result;
}
private string getFileIDFrommScriptReferenceLine(string lineStr)
{
int num = lineStr.IndexOf("fileID:") + "fileID: ".Length;
int num2 = lineStr.IndexOf(",") - num;
if (num2 <= 0)
{
return null;
}
return lineStr.Substring(num, num2);
}
private string getGUIDFrommScriptReferenceLine(string lineStr)
{
int num = lineStr.IndexOf("guid:") + "guid: ".Length;
int num2 = lineStr.LastIndexOf(",") - num;
if (num2 <= 0)
{
return null;
}
return lineStr.Substring(num, num2);
}
private string getFileNameFromPath(string path)
{
string text = path.Substring(path.LastIndexOf("\\") + 1);
return text.Substring(0, text.IndexOf("."));
}
private List<string> FindAllFileWithSuffixs(string path, string[] suffixs)
{
List<string> resultList = new List<string>();
FindAllFileWithSuffixs(path, suffixs, ref resultList);
return resultList;
}
private void FindAllFileWithSuffixs(string path, string[] suffixs, ref List<string> resultList)
{
if (File.Exists(path))
{
resultList.Add(path);
}
else
{
if (string.IsNullOrEmpty(path))
{
return;
}
string[] files = Directory.GetFiles(path);
foreach (string text in files)
{
foreach (string value in suffixs)
{
if (text.EndsWith(value))
{
resultList.Add(text);
break;
}
}
}
files = Directory.GetDirectories(path);
foreach (string path2 in files)
{
FindAllFileWithSuffixs(path2, suffixs, ref resultList);
}
}
}
private void WriteDebugFile(string[] lines, string filename)
{
}
}
///
// FileIDUtil
using System;
using System.Security.Cryptography;
using System.Text;
public static class FileIDUtil
{
public static int Compute(Type t)
{
string s = "s\0\0\0" + t.Namespace + t.Name;
using HashAlgorithm hashAlgorithm = new MD4();
byte[] array = hashAlgorithm.ComputeHash(Encoding.UTF8.GetBytes(s));
int num = 0;
for (int num2 = 3; num2 >= 0; num2--)
{
num <<= 8;
num |= array[num2];
}
return num;
}
}
///
// MD4
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
public class MD4 : HashAlgorithm
{
private uint _a;
private uint _b;
private uint _c;
private uint _d;
private uint[] _x;
private int _bytesProcessed;
public MD4()
{
_x = new uint[16];
Initialize();
}
public override void Initialize()
{
_a = 1732584193u;
_b = 4023233417u;
_c = 2562383102u;
_d = 271733878u;
_bytesProcessed = 0;
}
protected override void HashCore(byte[] array, int offset, int length)
{
ProcessMessage(Bytes(array, offset, length));
}
protected override byte[] HashFinal()
{
try
{
ProcessMessage(Padding());
return new uint[4] { _a, _b, _c, _d }.SelectMany((uint word) => Bytes(word)).ToArray();
}
finally
{
Initialize();
}
}
private void ProcessMessage(IEnumerable<byte> bytes)
{
foreach (byte @byte in bytes)
{
int num = _bytesProcessed & 0x3F;
int num2 = num >> 2;
int num3 = (num & 3) << 3;
_x[num2] = (_x[num2] & (uint)(~(255 << num3))) | (uint)(@byte << num3);
if (num == 63)
{
Process16WordBlock();
}
_bytesProcessed++;
}
}
private static IEnumerable<byte> Bytes(byte[] bytes, int offset, int length)
{
for (int i = offset; i < length; i++)
{
yield return bytes[i];
}
}
private IEnumerable<byte> Bytes(uint word)
{
yield return (byte)(word & 0xFFu);
yield return (byte)((word >> 8) & 0xFFu);
yield return (byte)((word >> 16) & 0xFFu);
yield return (byte)((word >> 24) & 0xFFu);
}
private IEnumerable<byte> Repeat(byte value, int count)
{
for (int i = 0; i < count; i++)
{
yield return value;
}
}
private IEnumerable<byte> Padding()
{
return Repeat(128, 1).Concat(Repeat(0, ((_bytesProcessed + 8) & 0x7FFFFFC0) + 55 - _bytesProcessed)).Concat(Bytes((uint)(_bytesProcessed << 3))).Concat(Repeat(0, 4));
}
private void Process16WordBlock()
{
uint num = _a;
uint num2 = _b;
uint num3 = _c;
uint num4 = _d;
int[] array = new int[4] { 0, 4, 8, 12 };
foreach (int num5 in array)
{
num = Round1Operation(num, num2, num3, num4, _x[num5], 3);
num4 = Round1Operation(num4, num, num2, num3, _x[num5 + 1], 7);
num3 = Round1Operation(num3, num4, num, num2, _x[num5 + 2], 11);
num2 = Round1Operation(num2, num3, num4, num, _x[num5 + 3], 19);
}
array = new int[4] { 0, 1, 2, 3 };
foreach (int num6 in array)
{
num = Round2Operation(num, num2, num3, num4, _x[num6], 3);
num4 = Round2Operation(num4, num, num2, num3, _x[num6 + 4], 5);
num3 = Round2Operation(num3, num4, num, num2, _x[num6 + 8], 9);
num2 = Round2Operation(num2, num3, num4, num, _x[num6 + 12], 13);
}
array = new int[4] { 0, 2, 1, 3 };
foreach (int num7 in array)
{
num = Round3Operation(num, num2, num3, num4, _x[num7], 3);
num4 = Round3Operation(num4, num, num2, num3, _x[num7 + 8], 9);
num3 = Round3Operation(num3, num4, num, num2, _x[num7 + 4], 11);
num2 = Round3Operation(num2, num3, num4, num, _x[num7 + 12], 15);
}
_a += num;
_b += num2;
_c += num3;
_d += num4;
}
private static uint ROL(uint value, int numberOfBits)
{
return (value << numberOfBits) | (value >> 32 - numberOfBits);
}
private static uint Round1Operation(uint a, uint b, uint c, uint d, uint xk, int s)
{
return ROL(a + ((b & c) | (~b & d)) + xk, s);
}
private static uint Round2Operation(uint a, uint b, uint c, uint d, uint xk, int s)
{
return ROL(a + ((b & c) | (b & d) | (c & d)) + xk + 1518500249, s);
}
private static uint Round3Operation(uint a, uint b, uint c, uint d, uint xk, int s)
{
return ROL(a + (b ^ c ^ d) + xk + 1859775393, s);
}
}
//