Skip to content

Commit 3b38e25

Browse files
committed
v2.0.0
1 parent 35f1d77 commit 3b38e25

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+1332
-403
lines changed

CHANGELOG.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Changelog
2+
3+
All notable changes to this project will be documented in this file.
4+
5+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7+
8+
9+
## [Unreleased]
10+
11+
12+
## [2.0.0]()
13+
14+
### Breaking Changes ;-)
15+
16+
- *USGEngine*.`ProcessFile(string assetsRelPath)`
17+
👉 signature changed: `ProcessFile(string assetsRelPath,
18+
bool ignoreOverwriteSettingOnAttribute, bool autoRunReferencingEmittersNow = false)`
19+
20+
- ~~public~~ static bool *USGEngine*.`IgnoreOverwriteSettingByAttribute`
21+
👉 now private. use <code>ProcessFile(path, **true**)</code> instead.
22+
23+
- *USGUtility*.<code>ForceGenerateByName(string clsName, bool showInProjectPanel = **false**)</code>
24+
👉 now false by default.
25+
26+
- `usg(Type cls, params string[] memberNames)`
27+
👉 signature changed: `usg(object obj, bool isFullName = true)`
28+
👉 `global::` namespace will be added.

CHANGELOG.md.meta

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Editor/EditorEvent.cs

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
#if UNITY_EDITOR
2+
3+
using System;
4+
using System.Reflection;
5+
using UnityEditor;
6+
7+
namespace SatorImaging.UnitySourceGenerator
8+
{
9+
public static class EditorEvent
10+
{
11+
// NOTE: Unity doesn't invoke asset import event correctly when load script in background.
12+
// This will prevent Unity to reload updated scripts in background.
13+
[InitializeOnLoadMethod]
14+
static void InitializeOnLoad()
15+
{
16+
ProjectSettingsData.instance.SuspendAutoReloadWhileEditorInBackground
17+
= ProjectSettingsData.instance.SuspendAutoReloadWhileEditorInBackground;
18+
}
19+
20+
21+
static bool _eventWasRegistered = false; // no way to determine other classes use focusChanged event?
22+
internal static void RegisterFocusChangedEvent(bool registerOrRemove)
23+
{
24+
//https://github.com/Unity-Technologies/UnityCsReference/blob/master/Editor/Mono/EditorApplication.cs#L275
25+
var focusChanged = typeof(EditorApplication).GetField("focusChanged",
26+
BindingFlags.Static | BindingFlags.NonPublic);
27+
if (focusChanged == null) return;
28+
29+
// TODO: better cleanup.
30+
// currently, event can be unregistered but it seems empty action runs on focus changed event...?
31+
if (!_eventWasRegistered)
32+
{
33+
if (!registerOrRemove)
34+
return;
35+
EditorApplication.quitting += () => OnEditorApplicationFocus(true);
36+
}
37+
38+
var currentAction = focusChanged.GetValue(null) as Action<bool>;
39+
if (registerOrRemove)
40+
{
41+
currentAction -= OnEditorApplicationFocus;
42+
currentAction += OnEditorApplicationFocus;
43+
_eventWasRegistered = true;
44+
}
45+
else
46+
{
47+
currentAction -= OnEditorApplicationFocus;
48+
}
49+
focusChanged.SetValue(null, currentAction);
50+
//Debug.Log($"[USG] Null? {currentAction == null} Method:{currentAction.Method} Target:{currentAction.Target}");
51+
52+
_restoreAutoRefresh = EditorPrefs.GetInt(PREF_AUTO_REFRESH, EditorPrefs.GetInt(PREF_AUTO_REFRESH_OLD, DEFAULT_AUTO_REFRESH));
53+
_restoreDirMonitoring = EditorPrefs.GetBool(PREF_DIR_MONITORING, DEFAULT_DIR_MONITORING);
54+
}
55+
56+
const bool DEFAULT_DIR_MONITORING = true;
57+
const int DEFAULT_AUTO_REFRESH = 1;
58+
const string PREF_AUTO_REFRESH = "kAutoRefreshMode";
59+
const string PREF_AUTO_REFRESH_OLD = "kAutoRefresh";
60+
const string PREF_DIR_MONITORING = "DirectoryMonitoring";
61+
static bool _restoreDirMonitoring = DEFAULT_DIR_MONITORING;
62+
static int _restoreAutoRefresh = DEFAULT_AUTO_REFRESH;
63+
static void OnEditorApplicationFocus(bool focus)
64+
{
65+
//https://github.com/Unity-Technologies/UnityCsReference/blob/master/Editor/Mono/PreferencesWindow/AssetPipelinePreferences.cs#L94
66+
if (focus == false)
67+
{
68+
_restoreAutoRefresh = EditorPrefs.GetInt(PREF_AUTO_REFRESH, EditorPrefs.GetInt(PREF_AUTO_REFRESH_OLD, DEFAULT_AUTO_REFRESH));
69+
_restoreDirMonitoring = EditorPrefs.GetBool(PREF_DIR_MONITORING, DEFAULT_DIR_MONITORING);
70+
//AssetDatabase.DisallowAutoRefresh();
71+
EditorApplication.LockReloadAssemblies();
72+
EditorPrefs.SetBool(PREF_DIR_MONITORING, false);
73+
EditorPrefs.SetInt(PREF_AUTO_REFRESH, 0);
74+
EditorPrefs.SetInt(PREF_AUTO_REFRESH_OLD, 0);
75+
}
76+
else
77+
{
78+
EditorPrefs.SetBool(PREF_DIR_MONITORING, _restoreDirMonitoring);
79+
EditorPrefs.SetInt(PREF_AUTO_REFRESH, _restoreAutoRefresh);
80+
EditorPrefs.SetInt(PREF_AUTO_REFRESH_OLD, _restoreAutoRefresh);
81+
82+
//AssetDatabase.AllowAutoRefresh();
83+
EditorApplication.UnlockReloadAssemblies();
84+
AssetDatabase.Refresh(ImportAssetOptions.ForceUpdate); // option is required...?
85+
}
86+
//Debug.Log($"[USG] Focus:{focus} Restore:{_restoreAutoRefresh}/{_restoreDirMonitoring}");
87+
}
88+
89+
90+
}
91+
}
92+
93+
#endif

Sample/MethodGeneratorSample.cs.meta renamed to Editor/EditorEvent.cs.meta

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Editor/EditorExtension.cs

Lines changed: 14 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,11 @@
11
#if UNITY_EDITOR
22

3-
using System.Collections;
4-
using System.Collections.Generic;
3+
using System.IO;
54
using UnityEditor;
6-
using UnityEngine;
7-
using UnityEditor.Callbacks;
8-
using System;
9-
using System.Reflection;
105

116
namespace SatorImaging.UnitySourceGenerator
127
{
13-
class EditorExtension
8+
static class EditorExtension
149
{
1510
const string ROOT_MENU = @"Assets/Unity Source Generator/";
1611
const string TEMPLATE_PATH = @"Packages/com.sator-imaging.alt-source-generator/Template/Template_";
@@ -21,12 +16,15 @@ class EditorExtension
2116
[MenuItem(ROOT_MENU + "Force Generate while Overwriting Disabled")]
2217
static void ForceGenerateSelectedScripts()
2318
{
24-
USGEngine.IgnoreOverwriteSettingByAttribute = true; // always disabled after import event.
25-
26-
foreach (var GUID in Selection.assetGUIDs)
19+
// NOTE: when multiple files selected, first import event initialize C# environment.
20+
// --> https://docs.unity3d.com/2021.3/Documentation/Manual/DomainReloading.html
21+
// need to use USGEngine.ProcessFile() instead.
22+
foreach (var guid in Selection.assetGUIDs)
2723
{
28-
var path = AssetDatabase.GUIDToAssetPath(GUID);
29-
AssetDatabase.ImportAsset(path);
24+
var path = AssetDatabase.GUIDToAssetPath(guid);
25+
//USGEngine.IgnoreOverwriteSettingOnAttribute = true;
26+
//USGEngine.ProcessFile(path);
27+
USGUtility.ForceGenerateByName(Path.GetFileNameWithoutExtension(path), false);
3028
}
3129
}
3230

@@ -40,66 +38,14 @@ static void MethodGenerator()
4038
}
4139

4240

43-
[MenuItem(ROOT_MENU + "Generic Generator Template", priority = 100)]
44-
static void GenericGenerator()
41+
[MenuItem(ROOT_MENU + "Self-Emit Generator Template", priority = 100)]
42+
static void SelfEmitGenerator()
4543
{
4644
ProjectWindowUtil.CreateScriptAssetFromTemplateFile(
47-
TEMPLATE_PATH + nameof(GenericGenerator) + TXT_EXT,
48-
nameof(GenericGenerator) + CS_EXT);
49-
}
50-
51-
52-
/* kept for reference. USGEngine update seems to work as expected.
53-
// NOTE: This will prevent Unity to reload updated scripts in background.
54-
// Unity doesn't invoke asset import event correctly when load script in background.
55-
[InitializeOnLoadMethod]
56-
static void InitializeFocusChangedEvent()
57-
{
58-
//https://github.com/Unity-Technologies/UnityCsReference/blob/master/Editor/Mono/EditorApplication.cs#L275
59-
var focusChanged = typeof(EditorApplication).GetField("focusChanged",
60-
BindingFlags.Static | BindingFlags.NonPublic);
61-
if (focusChanged == null) return;
62-
63-
var focusAction = focusChanged.GetValue(null) as Action<bool>;
64-
focusAction -= OnEditorApplicationFocus;
65-
focusAction += OnEditorApplicationFocus;
66-
focusChanged.SetValue(null, focusAction);
67-
68-
s_restoreAutoRefresh = EditorPrefs.GetInt(PREF_AUTO_REFRESH, EditorPrefs.GetInt(PREF_AUTO_REFRESH_OLD, 1));
69-
s_restoreDirMonitoring = EditorPrefs.GetBool(PREF_DIR_MONITORING, true);
45+
TEMPLATE_PATH + nameof(SelfEmitGenerator) + TXT_EXT,
46+
nameof(SelfEmitGenerator) + CS_EXT);
7047
}
7148

72-
const string PREF_AUTO_REFRESH = "kAutoRefreshMode";
73-
const string PREF_AUTO_REFRESH_OLD = "kAutoRefresh";
74-
const string PREF_DIR_MONITORING = "DirectoryMonitoring";
75-
static bool s_restoreDirMonitoring;
76-
static int s_restoreAutoRefresh;
77-
static void OnEditorApplicationFocus(bool focus)
78-
{
79-
//https://github.com/Unity-Technologies/UnityCsReference/blob/master/Editor/Mono/PreferencesWindow/AssetPipelinePreferences.cs#L94
80-
if (focus == false)
81-
{
82-
s_restoreAutoRefresh = EditorPrefs.GetInt(PREF_AUTO_REFRESH, EditorPrefs.GetInt(PREF_AUTO_REFRESH_OLD, 1));
83-
s_restoreDirMonitoring = EditorPrefs.GetBool(PREF_DIR_MONITORING, true);
84-
//AssetDatabase.DisallowAutoRefresh();
85-
EditorApplication.LockReloadAssemblies();
86-
EditorPrefs.SetBool(PREF_DIR_MONITORING, false);
87-
EditorPrefs.SetInt(PREF_AUTO_REFRESH, 0);
88-
EditorPrefs.SetInt(PREF_AUTO_REFRESH_OLD, 0);
89-
}
90-
else
91-
{
92-
//AssetDatabase.AllowAutoRefresh();
93-
EditorApplication.UnlockReloadAssemblies();
94-
EditorPrefs.SetBool(PREF_DIR_MONITORING, s_restoreDirMonitoring);
95-
EditorPrefs.SetInt(PREF_AUTO_REFRESH, s_restoreAutoRefresh);
96-
EditorPrefs.SetInt(PREF_AUTO_REFRESH_OLD, s_restoreAutoRefresh);
97-
98-
AssetDatabase.Refresh();
99-
}
100-
Debug.Log($"[USG] Focus:{focus} / AutoRefresh:{s_restoreAutoRefresh} / DirMonitoring:{s_restoreDirMonitoring}");
101-
}
102-
*/
10349

10450
}
10551
}

Editor/ProjectSettingsData.cs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#if UNITY_EDITOR
2+
3+
using System;
4+
using System.Collections.Generic;
5+
using UnityEngine;
6+
7+
8+
namespace SatorImaging.UnitySourceGenerator
9+
{
10+
public class ProjectSettingsData : ProjectSettingsSingleton<ProjectSettingsData>
11+
{
12+
public void Save() => base.Save(true);
13+
14+
[SerializeField] public bool AutoEmitOnScriptUpdate = true;
15+
[SerializeField] public List<string> AutoEmitDisabledPaths = new();
16+
[Range(0, 1920)]
17+
[SerializeField] public int DenseViewWidthThreshold = 512;
18+
19+
//properties
20+
[HideInInspector][SerializeField] bool _disableAutoReloadInBackground = false;
21+
public bool SuspendAutoReloadWhileEditorInBackground
22+
{
23+
get => _disableAutoReloadInBackground;
24+
set
25+
{
26+
_disableAutoReloadInBackground = value;
27+
EditorEvent.RegisterFocusChangedEvent(value);
28+
if (value)
29+
Debug.Log($"[{nameof(UnitySourceGenerator)}] Auto Reload completely disabled while Unity Editor in Background.");
30+
//else
31+
// Debug.Log($"[{nameof(UnitySourceGenerator)}] Unity Editor event was unregistered.");
32+
}
33+
}
34+
35+
36+
// temporary storage between domain reloading.
37+
[HideInInspector][SerializeField] internal List<string> ImportedScriptPaths = new();
38+
[HideInInspector][SerializeField] internal List<string> PathsToSkipImportEvent = new();
39+
[HideInInspector][SerializeField] internal List<string> PathsToIgnoreOverwriteSettingOnAttribute = new();
40+
41+
}
42+
}
43+
44+
#endif

Sample/MinimalGenerator.cs.meta renamed to Editor/ProjectSettingsData.cs.meta

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)