generated from vrchat-community/template-package
-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve asset saving performance (#479)
* feat: new, more efficient serialization APIs * fix: unpacking generated assets breaks references Also implement support for subassets. * chore: remove dead code and fix tests
- Loading branch information
1 parent
2dcda1b
commit 8212db6
Showing
19 changed files
with
522 additions
and
112 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
#nullable enable | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Text.RegularExpressions; | ||
using nadena.dev.ndmf.runtime; | ||
using UnityEditor; | ||
using UnityEditor.VersionControl; | ||
using UnityEngine; | ||
using Object = UnityEngine.Object; | ||
|
||
namespace nadena.dev.ndmf | ||
{ | ||
internal class AssetSaver : IAssetSaver | ||
{ | ||
private readonly string subAssetPath, rootAssetPath; | ||
private readonly int assetsPerContainer; | ||
|
||
private GeneratedAssets _rootAsset; | ||
private SubAssetContainer _currentSubContainer; | ||
private int _assetCount; | ||
private HashSet<Object> _temporaryAssets = new HashSet<Object>(); | ||
private List<SubAssetContainer> _containers = new(); | ||
|
||
public Object CurrentContainer => _currentSubContainer; | ||
|
||
internal AssetSaver(string generatedAssetsRoot, string avatarName, int assetsPerContainer = 256) | ||
{ | ||
this.assetsPerContainer = assetsPerContainer; | ||
|
||
avatarName = FilterAssetName(avatarName, "avatar"); | ||
var rootPath = generatedAssetsRoot + "/" + avatarName; | ||
subAssetPath = rootPath + "/_assets"; | ||
|
||
this.rootAssetPath = rootPath + "/"; | ||
|
||
if (AssetDatabase.IsValidFolder(subAssetPath)) | ||
{ | ||
AssetDatabase.DeleteAsset(rootPath); | ||
} | ||
|
||
// Ensure directory exists recursively | ||
if (!AssetDatabase.IsValidFolder(subAssetPath)) | ||
{ | ||
Directory.CreateDirectory(subAssetPath); | ||
AssetDatabase.Refresh(); | ||
} | ||
|
||
var rootAssetPath = AssetDatabase.GenerateUniqueAssetPath(rootPath + "/" + avatarName + ".asset"); | ||
_rootAsset = ScriptableObject.CreateInstance<GeneratedAssets>(); | ||
AssetDatabase.CreateAsset(_rootAsset, rootAssetPath); | ||
_currentSubContainer = CreateAssetContainer(); | ||
|
||
_assetCount = 0; | ||
} | ||
|
||
public void SaveAsset(UnityEngine.Object? obj) | ||
{ | ||
if (obj == null || EditorUtility.IsPersistent(obj)) return; | ||
|
||
_temporaryAssets.Add(obj); | ||
|
||
if (obj is Texture) | ||
{ | ||
// Textures can be quite large, so push them off to their own files. | ||
// However, be sure to create them as a subasset, as this appears to be much faster, for some reason... | ||
var texName = FilterAssetName(obj.name, "texture"); | ||
var container = CreateAssetContainer(texName); | ||
|
||
AssetDatabase.AddObjectToAsset(obj, container); | ||
return; | ||
} | ||
|
||
if (_assetCount >= assetsPerContainer) | ||
{ | ||
_currentSubContainer = CreateAssetContainer(); | ||
_assetCount = 0; | ||
} | ||
|
||
AssetDatabase.AddObjectToAsset(obj, _currentSubContainer); | ||
_assetCount++; | ||
} | ||
|
||
private SubAssetContainer CreateAssetContainer(string name = "assets") | ||
{ | ||
var subContainerPath = AssetDatabase.GenerateUniqueAssetPath(subAssetPath + "/" + name + ".asset"); | ||
var subContainer = ScriptableObject.CreateInstance<SubAssetContainer>(); | ||
AssetDatabase.CreateAsset(subContainer, subContainerPath); | ||
|
||
_containers.Add(subContainer); | ||
|
||
return subContainer; | ||
} | ||
|
||
public bool IsTemporaryAsset(UnityEngine.Object? obj) | ||
{ | ||
if (obj == null) return true; | ||
|
||
if (_temporaryAssets.Contains(obj) || !EditorUtility.IsPersistent(obj)) | ||
{ | ||
return true; | ||
} | ||
|
||
var path = AssetDatabase.GetAssetPath(obj); | ||
if (path.StartsWith(rootAssetPath)) | ||
{ | ||
_temporaryAssets.Add(obj); | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
|
||
private static readonly Regex WindowsReservedFileNames = new Regex( | ||
"(CON|PRN|AUX|NUL|COM[0-9]|LPT[0-9])([.].*)?", | ||
RegexOptions.IgnoreCase | ||
); | ||
|
||
private static readonly Regex WindowsReservedFileCharacters = new Regex( | ||
"[<>:\"/\\\\|?*\x00-\x1f]", | ||
RegexOptions.IgnoreCase | ||
); | ||
|
||
private static readonly Regex StripLeadingTrailingWhitespace = new Regex( | ||
"^[\\s]*((?=\\S).*\\S)[\\s]*$" | ||
); | ||
|
||
internal static string FilterAssetName(string assetName, string? fallbackName = null) | ||
{ | ||
assetName = WindowsReservedFileCharacters.Replace(assetName, "_"); | ||
|
||
if (WindowsReservedFileNames.IsMatch(assetName)) | ||
{ | ||
assetName = "_" + assetName; | ||
} | ||
|
||
var match = StripLeadingTrailingWhitespace.Match(assetName); | ||
if (match.Success) | ||
{ | ||
assetName = match.Groups[1].Value; | ||
} else { | ||
assetName = fallbackName ?? Guid.NewGuid().ToString(); | ||
} | ||
|
||
return assetName; | ||
} | ||
|
||
|
||
public IEnumerable<Object> GetPersistedAssets() | ||
{ | ||
return _temporaryAssets.Where(o => o != null); | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
_rootAsset.SubAssets = _containers; | ||
EditorUtility.SetDirty(_rootAsset); | ||
AssetDatabase.SaveAssets(); | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.