Essentials + extra crap

Loadings
Startups
Movement
Camera
This commit is contained in:
ThomasCharester committed 2026-01-12 18:43:51 +03:00
1 parent 662f4a1d05
commit 5f5ace1b52
734 files changed
+107323 -1713

No files matched your search

@@ -0,0 +1,87 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using UnityEngine;
public static class PartialClassFinder
{
//TODO: optimize lookup, currently this loads all files in directory and checks if they have 'partial' string in them. For directories with many files this will not be sufficient
public static IEnumerable<string> FindPartialClassFilesInDirectory(string filePath, int maxDepth = int.MaxValue)
{
if (string.IsNullOrEmpty(filePath) || !File.Exists(filePath))
return Enumerable.Empty<string>();
string directory = Path.GetDirectoryName(filePath);
string fileContent = File.ReadAllText(filePath);
string partialClassName = ExtractPartialClassName(fileContent);
if (string.IsNullOrEmpty(partialClassName))
{
Debug.LogWarning($"No partial class/struct found in file: {filePath}");
return Enumerable.Empty<string>();
}
return FindPartialClassFilesRecursively(directory, partialClassName, maxDepth, currentDepth: 0);
}
private static IEnumerable<string> FindPartialClassFilesRecursively(string directory, string partialClassName, int maxDepth, int currentDepth)
{
var files = new List<string>();
try
{
// Search in current directory
files.AddRange(Directory.GetFiles(directory, "*.cs")
.Where(file => IsPartialClassFile(file, partialClassName)));
// If we haven't reached max depth, search in subdirectories
if (currentDepth < maxDepth)
{
foreach (var subDir in Directory.GetDirectories(directory))
{
files.AddRange(FindPartialClassFilesRecursively(subDir, partialClassName, maxDepth, currentDepth + 1));
}
}
}
catch (UnauthorizedAccessException ex)
{
Debug.LogWarning($"Access denied to directory: {directory}. Error: {ex.Message}");
}
catch (Exception ex)
{
Debug.LogError($"Error searching directory: {directory}. Error: {ex.Message}");
}
return files;
}
private static bool IsPartialClassFile(string filePath, string className)
{
try
{
string content = File.ReadAllText(filePath);
return content.Contains($"partial class {className}")
|| content.Contains($"partial struct {className}");
}
catch (Exception ex)
{
Debug.LogError($"Error reading file: {filePath}. Error: {ex.Message}");
return false;
}
}
private static string ExtractPartialClassName(string fileContent)
{
// This is a simple implementation and might need to be more robust
// depending on your specific needs
var partialClassDeclaration = fileContent.Split(new[] { "partial class", "partial struct" }, StringSplitOptions.RemoveEmptyEntries)
.Skip(1)
.FirstOrDefault();
if (partialClassDeclaration == null)
return string.Empty;
return partialClassDeclaration.TrimStart().Split(new[] { ' ', '{', ':' }, StringSplitOptions.RemoveEmptyEntries).FirstOrDefault().Trim();
}
}
@@ -0,0 +1,10 @@
fileFormatVersion: 2
guid: 6f182f9db524441e80f367f8f99d8a5f
timeCreated: 1719778688
AssetOrigin:
serializedVersion: 1
productId: 239351
packageName: Fast Script Reload
packageVersion: 1.6.1
assetPath: Assets/FastScriptReload/Scripts/Editor/Compilation/CodeRewriting/Partials/PartialClassFinder.cs
uploadId: 710802
@@ -0,0 +1,92 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
namespace FastScriptReload.Editor.Compilation.CodeRewriting
{
internal static class PartialTreeProcessor
{
/// <summary>
/// Processes a syntax tree containing partial type definitions, merging it with other partial definitions found in the same directory.
/// </summary>
/// <param name="tree">The original syntax tree to process.</param>
/// <param name="definedPreprocessorSymbols">A collection of defined preprocessor symbols.</param>
/// <returns>A new SyntaxTree with merged partial types and added InternalsVisibleTo attribute.</returns>
/// <remarks>
/// This method performs the following steps:
/// 1. Checks if the tree contains partial types.
/// 2. If it does, processes the current tree and other partial files in the same directory.
/// 3. Combines all partial type declarations.
/// 4. Creates a new compilation unit with merged types and using directives.
/// 5. Adds the InternalsVisibleTo attribute to the resulting tree.
/// </remarks>
internal static SyntaxTree ProcessPartialTree(SyntaxTree tree, IEnumerable<string> definedPreprocessorSymbols)
{
if (!HasPartialTypes(tree))
{
return tree;
}
var combinedTypes = new Dictionary<string, List<TypeDeclarationSyntax>>();
var combinedUsingDirectives = new HashSet<UsingDirectiveSyntax>();
var combinedTypesDefined = new HashSet<string>();
ProcessTree(tree, combinedTypes, combinedUsingDirectives, combinedTypesDefined);
const int fileSearchMaxDepth = 5;
var otherPartialFiles = PartialClassFinder.FindPartialClassFilesInDirectory(tree.FilePath, fileSearchMaxDepth)
.Where(file => file != tree.FilePath);
foreach (var file in otherPartialFiles)
{
var partialTree = CSharpSyntaxTree.ParseText(File.ReadAllText(file), path: file);
ProcessTree(partialTree, combinedTypes, combinedUsingDirectives, combinedTypesDefined);
}
var combinedTypeDeclarations = combinedTypes
.Select(kvp => PartialTypeCombiner.CombinePartialType(kvp.Value))
.ToList<MemberDeclarationSyntax>();
var newRoot = SyntaxFactory.CompilationUnit()
.WithUsings(SyntaxFactory.List(combinedUsingDirectives))
.WithMembers(SyntaxFactory.List(combinedTypeDeclarations))
.WithAdditionalAnnotations(new SyntaxAnnotation("PreprocessorSymbols", string.Join(",", definedPreprocessorSymbols)));
return CSharpSyntaxTree.Create(newRoot, null, tree.FilePath)
.AddInternalsVisibleToAttribute();
}
private static bool HasPartialTypes(SyntaxTree tree)
{
var root = tree.GetCompilationUnitRoot();
return root.DescendantNodes().OfType<TypeDeclarationSyntax>().Any(type => type.Modifiers.Any(SyntaxKind.PartialKeyword));
}
private static void ProcessTree(
SyntaxTree tree,
Dictionary<string, List<TypeDeclarationSyntax>> combinedTypes,
HashSet<UsingDirectiveSyntax> combinedUsingDirectives,
HashSet<string> combinedTypesDefined)
{
var root = tree.GetCompilationUnitRoot();
combinedUsingDirectives.UnionWith(root.Usings);
foreach (var typeDecl in root.DescendantNodes().OfType<TypeDeclarationSyntax>())
{
var fullName = NamespaceHelper.GetFullyQualifiedName(typeDecl);
combinedTypesDefined.Add(fullName);
if (!combinedTypes.TryGetValue(fullName, out var declarations))
{
declarations = new List<TypeDeclarationSyntax>();
combinedTypes[fullName] = declarations;
}
declarations.Add(typeDecl);
}
}
}
}
@@ -0,0 +1,10 @@
fileFormatVersion: 2
guid: 4aafad9b85b44ebe902ba3cee7bda205
timeCreated: 1719954990
AssetOrigin:
serializedVersion: 1
productId: 239351
packageName: Fast Script Reload
packageVersion: 1.6.1
assetPath: Assets/FastScriptReload/Scripts/Editor/Compilation/CodeRewriting/Partials/PartialTreeProcessor.cs
uploadId: 710802
@@ -0,0 +1,45 @@
using System.Collections.Generic;
using System.Linq;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
namespace FastScriptReload.Editor.Compilation.CodeRewriting
{
internal static class PartialTypeCombiner
{
internal static TypeDeclarationSyntax CombinePartialType(List<TypeDeclarationSyntax> declarations)
{
if (declarations.Count == 1)
return declarations[0];
var firstDeclaration = declarations[0];
var combinedMembers = declarations.SelectMany(d => d.Members).ToList();
var combinedModifiers = declarations
.SelectMany(d => d.Modifiers)
.Where(m => !m.IsKind(SyntaxKind.PartialKeyword))
.GroupBy(m => m.ValueText)
.Select(g => g.First().WithTrailingTrivia(SyntaxFactory.Space))
.ToList();
if (!combinedModifiers.Any(m => m.IsKind(SyntaxKind.PartialKeyword)))
combinedModifiers.Add(SyntaxFactory.Token(SyntaxKind.PartialKeyword).WithTrailingTrivia(SyntaxFactory.Space));
var combinedBaseList = declarations
.Where(d => d.BaseList != null)
.SelectMany(d => d.BaseList.Types)
.GroupBy(t => t.ToString())
.Select(g => g.First())
.ToList();
return firstDeclaration
.WithMembers(SyntaxFactory.List(combinedMembers))
.WithModifiers(SyntaxFactory.TokenList(combinedModifiers))
.WithKeyword(firstDeclaration.Keyword.WithLeadingTrivia(SyntaxFactory.Space))
.WithBaseList(combinedBaseList.Any()
? SyntaxFactory.BaseList(SyntaxFactory.SeparatedList(combinedBaseList))
: null);
}
}
}
@@ -0,0 +1,10 @@
fileFormatVersion: 2
guid: cc8170320f3a463590d434f7e0570ea0
timeCreated: 1719955225
AssetOrigin:
serializedVersion: 1
productId: 239351
packageName: Fast Script Reload
packageVersion: 1.6.1
assetPath: Assets/FastScriptReload/Scripts/Editor/Compilation/CodeRewriting/Partials/PartialTypeCombiner.cs
uploadId: 710802
@@ -0,0 +1,52 @@
using System.Collections.Generic;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
namespace FastScriptReload.Editor.Compilation.CodeRewriting
{
internal static class NamespaceHelper
{
internal static string GetFullyQualifiedName(TypeDeclarationSyntax typeDeclaration)
{
var namespaces = new List<string>();
var currentNode = typeDeclaration.Parent;
while (currentNode != null)
{
if (currentNode is NamespaceDeclarationSyntax namespaceDeclaration)
{
namespaces.Add(namespaceDeclaration.Name.ToString());
}
currentNode = currentNode.Parent;
}
namespaces.Reverse();
var namespaceName = string.Join(".", namespaces);
return string.IsNullOrEmpty(namespaceName)
? typeDeclaration.Identifier.Text
: $"{namespaceName}.{typeDeclaration.Identifier.Text}";
}
}
internal static class SyntaxTreeExtensions
{
internal static SyntaxTree AddInternalsVisibleToAttribute(this SyntaxTree tree)
{
var root = tree.GetCompilationUnitRoot();
var attributeArgument = SyntaxFactory.AttributeArgument(
SyntaxFactory.LiteralExpression(SyntaxKind.StringLiteralExpression, SyntaxFactory.Literal("Assembly-CSharp"))
);
var attribute = SyntaxFactory.Attribute(
SyntaxFactory.ParseName("System.Runtime.CompilerServices.InternalsVisibleTo"),
SyntaxFactory.AttributeArgumentList(SyntaxFactory.SeparatedList(new[] { attributeArgument }))
);
var attributeList = SyntaxFactory.AttributeList(SyntaxFactory.SingletonSeparatedList(attribute))
.WithTarget(SyntaxFactory.AttributeTargetSpecifier(SyntaxFactory.Token(SyntaxKind.AssemblyKeyword)));
var newRoot = root.AddAttributeLists(attributeList);
return tree.WithRootAndOptions(newRoot, tree.Options);
}
}
}
@@ -0,0 +1,10 @@
fileFormatVersion: 2
guid: 1c2f0e22f1a24fcda6d0c0516b428420
timeCreated: 1719954970
AssetOrigin:
serializedVersion: 1
productId: 239351
packageName: Fast Script Reload
packageVersion: 1.6.1
assetPath: Assets/FastScriptReload/Scripts/Editor/Compilation/CodeRewriting/Partials/SyntaxTreeHelpers.cs
uploadId: 710802
@@ -0,0 +1,18 @@
using System.Collections.Generic;
using System.Linq;
using Microsoft.CodeAnalysis;
namespace FastScriptReload.Editor.Compilation.CodeRewriting
{
public static class SyntaxTreeMergePartialsExtensions
{
public static IEnumerable<SyntaxTree> MergePartials(
this IEnumerable<SyntaxTree> trees,
IEnumerable<string> definedPreprocessorSymbols)
{
return trees
.Select(tree => PartialTreeProcessor.ProcessPartialTree(tree, definedPreprocessorSymbols))
.ToList();
}
}
}
@@ -0,0 +1,10 @@
fileFormatVersion: 2
guid: 550c942c025f4fd5a1062891f3623c60
timeCreated: 1719174342
AssetOrigin:
serializedVersion: 1
productId: 239351
packageName: Fast Script Reload
packageVersion: 1.6.1
assetPath: Assets/FastScriptReload/Scripts/Editor/Compilation/CodeRewriting/Partials/SyntaxTreeMergePartialsExtensions.cs
uploadId: 710802