Skip to content

Instantly share code, notes, and snippets.

@insominx
Last active August 7, 2024 22:58
Show Gist options
  • Save insominx/67b8b5ffd13d2838cca1a7f781e9378c to your computer and use it in GitHub Desktop.
Save insominx/67b8b5ffd13d2838cca1a7f781e9378c to your computer and use it in GitHub Desktop.
Text Dump for LLMs - combines multiple files into a single text file and identifies where the text originally came from
using UnityEngine;
using UnityEditor;
using System.IO;
using System.Collections.Generic;
public class TextDumpEditorWindow : EditorWindow
{
readonly List<string> inputFolders = new();
string outputFilePath = "Assets/code-dump.txt";
string fileExtensions = "cs"; // Default to .cs files
const string lastOpenedFolderKey = "CodeDumpEditorWindow_LastOpenedFolder";
const float buttonWidth = 75f;
Vector2 scrollPosition;
//---------------------------------------------------------------------------
[MenuItem("Tools/Text Dump Window")]
public static void ShowWindow()
{
GetWindow<TextDumpEditorWindow>("Text Dump Window");
}
//---------------------------------------------------------------------------
void OnGUI()
{
GUILayout.Space(10);
DrawFileExtensionsField();
GUILayout.Space(10);
DrawInputFoldersField();
GUILayout.Space(10);
DrawOutputFileField();
GUILayout.Space(5);
GUILayout.BeginHorizontal();
GUILayout.FlexibleSpace();
if (GUILayout.Button("Dump", GUILayout.Width(buttonWidth * 2)))
DumpCodeFiles();
GUILayout.FlexibleSpace();
GUILayout.EndHorizontal();
}
//---------------------------------------------------------------------------
void DrawFileExtensionsField()
{
GUILayout.Label("File Extensions (comma separated):");
fileExtensions = EditorGUILayout.TextField(fileExtensions);
}
//---------------------------------------------------------------------------
void DrawInputFoldersField()
{
GUILayout.Label("Input Folder Locations:");
if (inputFolders.Count == 0)
{
GUILayout.Label("No folders added.");
}
else
{
foreach (string folder in inputFolders)
{
EditorGUILayout.BeginHorizontal();
GUILayout.Label(folder, GUILayout.ExpandWidth(true));
if (GUILayout.Button("Remove", GUILayout.Width(buttonWidth)))
{
inputFolders.Remove(folder);
EditorGUILayout.EndHorizontal();
break; // Exit the loop to avoid modifying the collection while iterating
}
EditorGUILayout.EndHorizontal();
}
}
if (GUILayout.Button("Add Folder", GUILayout.Width(buttonWidth)))
{
string lastOpenedFolder = EditorPrefs.GetString(lastOpenedFolderKey, Application.dataPath);
string path = EditorUtility.OpenFolderPanel("Select Input Folder", lastOpenedFolder, "");
if (!string.IsNullOrEmpty(path))
{
inputFolders.Add(path);
EditorPrefs.SetString(lastOpenedFolderKey, path);
}
}
}
//---------------------------------------------------------------------------
void DrawOutputFileField()
{
GUILayout.Label("Output File Name and Location:");
EditorGUILayout.BeginHorizontal();
outputFilePath = EditorGUILayout.TextField(outputFilePath);
if (GUILayout.Button("Browse", GUILayout.Width(buttonWidth)))
{
string lastOpenedFolder = EditorPrefs.GetString(lastOpenedFolderKey, Path.GetDirectoryName(outputFilePath));
string path = EditorUtility.SaveFilePanel("Select Output File", lastOpenedFolder, Path.GetFileName(outputFilePath), "txt");
if (!string.IsNullOrEmpty(path))
{
outputFilePath = path;
EditorPrefs.SetString(lastOpenedFolderKey, Path.GetDirectoryName(path));
}
}
EditorGUILayout.EndHorizontal();
}
//---------------------------------------------------------------------------
void DumpCodeFiles()
{
try
{
using StreamWriter writer = new(outputFilePath, false);
int totalFiles = 0;
string[] extensions = fileExtensions.Split(',');
foreach (string folder in inputFolders)
{
if (!Directory.Exists(folder))
{
Debug.LogError($"Input folder not found: {folder}");
continue;
}
foreach (string extension in extensions)
{
string searchPattern = $"*.{extension.Trim()}";
string[] files = Directory.GetFiles(folder, searchPattern, SearchOption.AllDirectories);
totalFiles += files.Length;
foreach (string file in files)
{
string fileContents = File.ReadAllText(file);
string relativePath = Path.GetRelativePath(folder, file);
writer.WriteLine($"// --- Beginning of file: \"{relativePath}\" ---");
writer.WriteLine(fileContents);
writer.WriteLine($"// --- End of file: \"{relativePath}\" ---");
writer.WriteLine();
}
}
}
writer.Flush();
AssetDatabase.Refresh();
Debug.Log($"Exported {totalFiles} files from {inputFolders.Count} folders to {outputFilePath}");
}
catch (IOException ex)
{
Debug.LogError($"Error writing to output file: {ex.Message}");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment