Skip to content

Commit

Permalink
v1.3.0: more stable on code generation
Browse files Browse the repository at this point in the history
  • Loading branch information
sator-imaging committed Mar 24, 2023
1 parent a1dd782 commit 1afd96d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 9 deletions.
35 changes: 27 additions & 8 deletions Editor/USGEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using System.Text;
using UnityEditor;
using UnityEngine;
using UnityEditor.Callbacks;


namespace SatorImaging.UnitySourceGenerator
Expand All @@ -19,6 +20,8 @@ public class USGEngine : AssetPostprocessor
public static bool IgnoreOverwriteSettingByAttribute = false;


const string EDITOR_PREFS_LENGTH = "__STMG_USG__TARGET_LENGTH";
const string EDITOR_PREFS_PREFIX = "__STMG_USG__TARGET_";
const int BUFFER_LENGTH = 61_440;
const int BUFFER_MAX_CHAR_LENGTH = BUFFER_LENGTH / 3; // worst case of UTF-8
const string GENERATOR_PREFIX = ".";
Expand Down Expand Up @@ -64,24 +67,40 @@ static void OnPostprocessAllAssets(
// menu command but OnPostprocessAllAssets event doesn't work as expected.
// (script runs with static field cleared even though .Clear() is only in ProcessingFiles().
// it's weird that event happens and asset paths retrieved but hashset items gone.)
////EditorApplication.delayCall += () =>
// NOTE: Use EditorPrefs as a temporary storage.
var nPaths = 0;
for (int i = 0; i < importedAssets.Length; i++)
{
ProcessingFiles(importedAssets);
if (!IsAppropriateTarget(importedAssets[i])) continue;
EditorPrefs.SetString(EDITOR_PREFS_PREFIX + nPaths++, importedAssets[i]);
Debug.Log($"[USG]: Saved into EditorPrefs: {importedAssets[i]}");
}
EditorPrefs.SetInt(EDITOR_PREFS_LENGTH, nPaths);

// NOTE: [DidReloadScripts] is executed before AssetPostprocessor, cannot be used.
EditorApplication.delayCall += () =>
{
ProcessingFiles();
};
}


readonly static HashSet<string> s_updatedGeneratorNames = new();
static void ProcessingFiles(string[] targetPaths)
static void ProcessingFiles()
{
bool somethingUpdated = false;
for (int i = 0; i < targetPaths.Length; i++)

var nPaths = EditorPrefs.GetInt(EDITOR_PREFS_LENGTH, 0);
EditorPrefs.DeleteKey(EDITOR_PREFS_LENGTH);
for (int i = 0; i < nPaths; i++)
{
// NOTE: Do NOT early return in this method.
// check path here to allow generator class can be lie outside of Assets/ folder.
if (!IsAppropriateTarget(targetPaths[i])) continue;
var key = EDITOR_PREFS_PREFIX + i;
//if (!EditorPrefs.HasKey(key)) continue;

var path = EditorPrefs.GetString(key);
EditorPrefs.DeleteKey(key);

if (ProcessFile(targetPaths[i]))
if (ProcessFile(path))
somethingUpdated = true;
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "com.sator-imaging.alt-source-generator",
"displayName": "Alternative Source Generator for Unity",
"version": "1.2.1",
"version": "1.3.0",
"unity": "2021.3",
"description": "Ease-of-Use Source Generator Alternative for Unity.",
"author": {
Expand Down

0 comments on commit 1afd96d

Please sign in to comment.