Files
HauntedBloodlines/Assets/Editor/DebugLogReplacerTool.cs
2025-05-29 22:31:40 +03:00

163 lines
6.1 KiB
C#

using UnityEditor;
using UnityEngine;
using System.IO;
using System.Text.RegularExpressions;
public class DebugLogReplacerTool : EditorWindow
{
private string selectedFolder = "Assets";
[MenuItem("Tools/Logger Tools/Replace Debug Logs")]
public static void ShowWindow()
{
GetWindow<DebugLogReplacerTool>("Debug Log Replacer");
}
void OnGUI()
{
GUILayout.Label("📁 Target Folder:", EditorStyles.boldLabel);
EditorGUILayout.BeginHorizontal();
selectedFolder = EditorGUILayout.TextField(selectedFolder);
if (GUILayout.Button("Browse", GUILayout.Width(70)))
{
string path = EditorUtility.OpenFolderPanel("Select Folder", "Assets", "");
if (!string.IsNullOrEmpty(path) && path.StartsWith(Application.dataPath))
selectedFolder = "Assets" + path.Substring(Application.dataPath.Length);
}
EditorGUILayout.EndHorizontal();
GUILayout.Space(10);
GUILayout.Label("💾 Backup Folder (any path)", EditorStyles.boldLabel);
string backupFolderSetting = EditorPrefs.GetString("logger_backup_folder", Path.Combine(Application.dataPath, "DebugLogBackups"));
EditorGUILayout.BeginHorizontal();
backupFolderSetting = EditorGUILayout.TextField(backupFolderSetting);
if (GUILayout.Button("Browse", GUILayout.Width(70)))
{
string path = EditorUtility.OpenFolderPanel("Select Backup Folder", "", "");
if (!string.IsNullOrEmpty(path))
{
backupFolderSetting = path;
EditorPrefs.SetString("logger_backup_folder", backupFolderSetting);
}
}
EditorGUILayout.EndHorizontal();
EditorPrefs.SetString("logger_backup_folder", backupFolderSetting);
GUILayout.Space(10);
GUILayout.Label("🛠 Convert Logs", EditorStyles.boldLabel);
if (GUILayout.Button("🚀 Convert All Debug Logs in Folder", GUILayout.Height(30)))
{
ReplaceLogsInFolder(selectedFolder, backupFolderSetting);
}
GUILayout.Space(10);
GUILayout.Label("✂️ Convert Selected Scripts", EditorStyles.boldLabel);
if (GUILayout.Button("🔁 Convert Selected .cs Files Only", GUILayout.Height(30)))
{
ConvertSelectedScripts(backupFolderSetting);
}
GUILayout.Space(10);
GUILayout.Label("⚙️ Logger Settings", EditorStyles.boldLabel);
bool loggingEnabled = PlayerPrefs.GetInt("logger_enabled", 1) == 1;
bool newValue = EditorGUILayout.Toggle("Enable Logger Output?", loggingEnabled);
if (newValue != loggingEnabled)
{
PlayerPrefs.SetInt("logger_enabled", newValue ? 1 : 0);
PlayerPrefs.Save();
Debug.Log("Logger.Enabled set to: " + newValue);
}
}
void ReplaceLogsInFolder(string relativeFolderPath, string backupFolder)
{
string fullPath = Path.Combine(Application.dataPath, relativeFolderPath.Substring("Assets".Length));
if (!Directory.Exists(backupFolder))
Directory.CreateDirectory(backupFolder);
string[] csFiles = Directory.GetFiles(fullPath, "*.cs", SearchOption.AllDirectories);
int filesChanged = 0;
foreach (var file in csFiles)
{
if (!File.Exists(file)) continue;
string content = File.ReadAllText(file);
string original = content;
content = Regex.Replace(content, @"\bDebug\.LogWarning\s*\(", "Logger.LogWarning(");
content = Regex.Replace(content, @"\bDebug\.LogError\s*\(", "Logger.LogError(");
content = Regex.Replace(content, @"\bDebug\.Log\s*\(", "Logger.Log(");
content = Regex.Replace(content, @"\bprint\s*\(", "Logger.Print(");
if (content != original)
{
string relativePath = file.Substring(Application.dataPath.Length + 1);
string backupPath = Path.Combine(backupFolder, relativePath);
Directory.CreateDirectory(Path.GetDirectoryName(backupPath));
File.Copy(file, backupPath, true);
File.WriteAllText(file, content);
Debug.Log("✅ Modified: " + file);
filesChanged++;
}
}
AssetDatabase.Refresh();
EditorUtility.DisplayDialog("Done ✅", $"Modified {filesChanged} script(s).\nBackup saved to:\n{backupFolder}", "OK");
}
void ConvertSelectedScripts(string backupFolder)
{
Object[] selectedObjects = Selection.objects;
int filesChanged = 0;
if (!Directory.Exists(backupFolder))
Directory.CreateDirectory(backupFolder);
foreach (Object obj in selectedObjects)
{
string assetPath = AssetDatabase.GetAssetPath(obj);
if (assetPath.EndsWith(".cs"))
{
string fullPath = Path.Combine(Application.dataPath, assetPath.Substring("Assets/".Length));
if (!File.Exists(fullPath)) continue;
string content = File.ReadAllText(fullPath);
string original = content;
content = Regex.Replace(content, @"\bDebug\.LogWarning\s*\(", "Logger.LogWarning(");
content = Regex.Replace(content, @"\bDebug\.LogError\s*\(", "Logger.LogError(");
content = Regex.Replace(content, @"\bDebug\.Log\s*\(", "Logger.Log(");
content = Regex.Replace(content, @"\bprint\s*\(", "Logger.Print(");
if (content != original)
{
string backupPath = Path.Combine(backupFolder, assetPath.Substring("Assets/".Length));
Directory.CreateDirectory(Path.GetDirectoryName(backupPath));
File.Copy(fullPath, backupPath, true);
File.WriteAllText(fullPath, content);
Debug.Log("✅ Modified: " + assetPath);
filesChanged++;
}
}
}
AssetDatabase.Refresh();
EditorUtility.DisplayDialog("Done ✅", $"Modified {filesChanged} selected script(s).\nBackup saved to:\n{backupFolder}", "OK");
}
}