public static void CollectFiles(string resolverPath, string folderPath) { if (string.IsNullOrEmpty(resolverPath)) { throw new ArgumentException("Value cannot be null or empty.", nameof(resolverPath)); } if (string.IsNullOrEmpty(folderPath)) { throw new ArgumentException("Value cannot be null or empty.", nameof(folderPath)); } var resolver = AssetInfoEditorUtility.LoadInfo <Utf8JsonResolverAssetInfo>(resolverPath); string[] paths = Directory.GetFiles(folderPath, $"*.{Utf8JsonExternalTypeEditorUtility.EXTERNAL_TYPE_ASSET_EXTENSION_NAME}", SearchOption.AllDirectories); resolver.Sources.Clear(); for (int i = 0; i < paths.Length; i++) { string path = paths[i]; var asset = AssetDatabase.LoadAssetAtPath <TextAsset>(path); if (asset != null) { resolver.Sources.Add(asset); } } AssetInfoEditorUtility.SaveInfo(resolverPath, resolver, false); }
public static void ClearResolver(string assetPath) { if (string.IsNullOrEmpty(assetPath)) { throw new ArgumentException("Value cannot be null or empty.", nameof(assetPath)); } var info = AssetInfoEditorUtility.LoadInfo <Utf8JsonResolverAssetInfo>(assetPath); string path = GetDestinationSourcePath(assetPath, info.ResolverName, info.DestinationSource); if (!string.IsNullOrEmpty(path)) { info.DestinationSource = null; AssetInfoEditorUtility.SaveInfo(assetPath, info); AssetDatabase.MoveAssetToTrash(path); } }
private static void CollectSources(IDictionary <string, string> cache, IReadOnlyList <string> paths) { for (int i = 0; i < paths.Count; i++) { string path = paths[i]; var info = AssetInfoEditorUtility.LoadInfo <Utf8JsonResolverAssetInfo>(path); if (info.AutoGenerate) { foreach (TextAsset asset in info.Sources) { string sourcePath = AssetDatabase.GetAssetPath(asset); cache[sourcePath] = path; } } } }
public static void GenerateResolver(string assetPath, ICodeGenerateContainerValidation validation = null, Compilation compilation = null, SyntaxGenerator generator = null) { if (string.IsNullOrEmpty(assetPath)) { throw new ArgumentException("Value cannot be null or empty.", nameof(assetPath)); } var info = AssetInfoEditorUtility.LoadInfo <Utf8JsonResolverAssetInfo>(assetPath); string source = GenerateResolver(info, validation, compilation, generator); string path = GetDestinationSourcePath(assetPath, info.ResolverName, info.DestinationSource); File.WriteAllText(path, source); AssetDatabase.ImportAsset(path); if (info.DestinationSource == null) { info.DestinationSource = AssetDatabase.LoadAssetAtPath <TextAsset>(path); AssetInfoEditorUtility.SaveInfo(assetPath, info); } }
public static string GenerateExternalSources(IReadOnlyList <string> externalPaths, ICollection <string> sourcePaths, Type attributeType = null, ICodeGenerateContainerValidation validation = null, Compilation compilation = null, SyntaxGenerator generator = null) { if (externalPaths == null) { throw new ArgumentNullException(nameof(externalPaths)); } if (sourcePaths == null) { throw new ArgumentNullException(nameof(sourcePaths)); } if (validation == null) { validation = CodeGenerateContainerAssetEditorUtility.DefaultValidation; } if (compilation == null) { compilation = CodeAnalysisEditorUtility.ProjectCompilation; } if (generator == null) { generator = CodeAnalysisEditorUtility.Generator; } string externalsTempPath = FileUtil.GetUniqueTempPathInProject(); var types = new HashSet <Type>(); CSharpSyntaxRewriter rewriterAddAttribute = null; if (attributeType != null && compilation.TryConstructTypeSymbol(attributeType, out ITypeSymbol typeSymbol)) { rewriterAddAttribute = GetAttributeRewriter(compilation, generator, typeSymbol); } Directory.CreateDirectory(externalsTempPath); for (int i = 0; i < externalPaths.Count; i++) { string externalPath = externalPaths[i]; var info = AssetInfoEditorUtility.LoadInfo <CodeGenerateContainerInfo>(externalPath); if (info.TryGetTargetType(out Type type)) { if (types.Add(type)) { SyntaxNode unit = CodeGenerateContainerInfoEditorUtility.CreateUnit(info, validation, compilation, generator); if (rewriterAddAttribute != null) { unit = rewriterAddAttribute.Visit(unit); } string sourcePath = $"{externalsTempPath}/{Guid.NewGuid():N}.cs"; string source = unit.NormalizeWhitespace().ToFullString(); File.WriteAllText(sourcePath, source); sourcePaths.Add(sourcePath); } else { Debug.LogWarning($"The specified type already generated: '{type}'."); } } } return(externalsTempPath); }