Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not working with VRCFury #134

Merged
merged 3 commits into from
May 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG-PRERELEASE.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ The format is based on [Keep a Changelog].
### Fixed
- Can't remove SkinnedMeshRenderer error `#133`
- This error should do nothing bad but it confuses everyone
- Bad behaviour with VRCFury on build `#134`

### Security

Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ The format is based on [Keep a Changelog].
### Fixed
- Can't remove SkinnedMeshRenderer error `#133`
- This error should do nothing bad but it confuses everyone
- Bad behaviour with VRCFury on build `#134`

### Security

Expand Down
49 changes: 26 additions & 23 deletions Editor/OptimizerProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,18 @@ public bool OnPreprocessAvatar(GameObject avatarGameObject)
private static void ProcessObject(OptimizerSession session)
{
if (_processing) return;
try
{
AssetDatabase.StartAssetEditing();
_processing = true;
DoProcessObject(session);
}
finally
using (Utils.StartEditingScope(true))
{
_processing = false;
AssetDatabase.SaveAssets();
try
{
_processing = true;
DoProcessObject(session);
}
finally
{
_processing = false;
session.MarkDirtyAll();
}
}
}

Expand Down Expand Up @@ -80,22 +82,23 @@ public void OnPostprocessAvatar()
public static void ProcessObject(OptimizerSession session)
{
if (_processing) return;
try
{
AssetDatabase.StartAssetEditing();
_processing = true;
DoProcessObject(session);
}
finally
using (Utils.StartEditingScope(true))
{
_processing = false;
AssetDatabase.StopAssetEditing();
foreach (var component in session.GetComponents<AvatarTagComponent>())
UnityEngine.Object.DestroyImmediate(component);
foreach (var activator in session.GetComponents<AvatarActivator>())
UnityEngine.Object.DestroyImmediate(activator);
try
{
_processing = true;
DoProcessObject(session);
}
finally
{
_processing = false;
foreach (var component in session.GetComponents<AvatarTagComponent>())
UnityEngine.Object.DestroyImmediate(component);
foreach (var activator in session.GetComponents<AvatarActivator>())
UnityEngine.Object.DestroyImmediate(activator);

AssetDatabase.SaveAssets();
session.MarkDirtyAll();
}
}
}

Expand Down
27 changes: 27 additions & 0 deletions Editor/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,33 @@ public static NativeArray<T> SliceNativeArray<T>(NativeArray<T> source, int leng
source.AsReadOnlySpan().Slice(0, length).CopyTo(res.AsSpan());
return res;
}

public static AssetEditingScope StartEditingScope(bool saveAssets) => AssetEditingScope.Start(saveAssets);

internal readonly struct AssetEditingScope : IDisposable
{
// 0: default: skip stop
// 1: stop asset editing
// 2: stop editing & save assets
private readonly int _flags;

private AssetEditingScope(int flags)
{
_flags = flags;
}

public static AssetEditingScope Start(bool saveAssets)
{
AssetDatabase.StartAssetEditing();
return new AssetEditingScope(1 | (saveAssets ? 2 : 0));
}

public void Dispose()
{
if ((_flags & 1) != 0) AssetDatabase.StopAssetEditing();
if ((_flags & 2) != 0) AssetDatabase.SaveAssets();
}
}
}

internal struct ArraySerializedPropertyEnumerable : IEnumerable<SerializedProperty>
Expand Down