FastScriptReload icon indicating copy to clipboard operation
FastScriptReload copied to clipboard

Improvement: FindFileOrThrow for dotnet tools caching result between project sessions

Open labbbirder opened this issue 6 months ago • 0 comments

Hi, handzlikchris. Thank you for your excellent work.

I noticed that every time project opens, FSR always search deeply into Unity installation path for dotnet tools, which spends seconds to complete. Because of storing in SessionState, first-time project launch can't benefit from it.

In my opinion, utilize EditorPrefs with unityVersion could be better.

Here is my snippet for this:

private static string FindFileOrThrow(string fileName)
{
    var cacheKey = $"FSR:FilePath_{fileName}:{Application.unityVersion}";
    var result = EditorPrefs.GetString(cacheKey, null);

    if (result != null && !File.Exists(result))
    {
        EditorPrefs.DeleteKey(cacheKey);
        result = null;
    }

    if (result == null)
    {
        result = Directory
            .GetFiles(ApplicationContentsPath, fileName, SearchOption.AllDirectories)
            .FirstOrDefault();

        if (result == null)
        {
            throw new Exception($"Unable to find '{fileName}', make sure Editor version supports it. You can also add preprocessor directive 'FastScriptReload_CompileViaMCS' which will use Mono compiler instead");
        }
    }

    EditorPrefs.SetString(cacheKey, result);
    return result;
}

labbbirder avatar May 09 '25 08:05 labbbirder