Skip to content

Commit

Permalink
feat: add default scope and keywords
Browse files Browse the repository at this point in the history
  • Loading branch information
caneva20 committed Apr 13, 2021
1 parent 4d04c1c commit 8325ebc
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 10 deletions.
3 changes: 3 additions & 0 deletions ConfigAttribute.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
using System;
using UnityEditor;

namespace me.caneva20.ConfigAssets {
[AttributeUsage(AttributeTargets.Class)]
public class ConfigAttribute : Attribute {
public string FileName { get; set; }
public string DisplayName { get; set; }
public bool EnableProvider { get; set; } = true;
public SettingsScope Scope { get; set; } = SettingsScope.Project;
public string[] Keywords { get; set; } = { };

public static ConfigAttribute Find<T>() where T : Config {
return Find(typeof(T));
Expand Down
34 changes: 24 additions & 10 deletions Editor/ConfigIndexer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,37 +55,51 @@ private static void CreateProviders(IEnumerable<Type> configs) {
builder.Append("////////////////////////////////////////////////////////////////////////////////\n");
builder.Append("\n");
builder.Append("using me.caneva20.ConfigAssets;\n");
builder.Append("using System.Collections.Generic;\n");
builder.Append("using UnityEditor;\n");
builder.Append("\n");
builder.Append("public static class ConfigAssetsSettingsProvider {\n");
builder.Append("\tprivate static SettingsProvider CreateProvider<T>(string name) where T : Config<T> {\n");
builder.Append("\t\treturn new SettingsProvider($\"Config assets/{name}\") {\n");
builder.Append("\t\t\tguiHandler = _ => Editor.CreateEditor(Config<T>.Instance).OnInspectorGUI()\n");
builder.Append(
"\tprivate static SettingsProvider CreateProvider<T>(string name, SettingsScope scope, IEnumerable<string> keywords) where T : Config<T> {\n");
builder.Append(
"\t\treturn new SettingsProvider($\"Config assets/{name}\", scope, keywords) {\n");
builder.Append(
"\t\t\tguiHandler = _ => Editor.CreateEditor(Config<T>.Instance).OnInspectorGUI()\n");
builder.Append("\t\t};\n");
builder.Append("\t}\n");
builder.Append("\n");

foreach (var configType in configs) {
var attribute = ConfigAttribute.Find(configType);

if (attribute?.EnableProvider == false) {
continue;
}

var name = configType.FullName?.Replace(".", "") ?? $"A{Guid.NewGuid()}";
var displayName = attribute?.DisplayName ?? configType.Name;
var scope = $"{nameof(SettingsScope)}.{attribute?.Scope ?? SettingsScope.Project}";
var keywords =
$"new string[] {{{string.Join(", ", (attribute?.Keywords ?? new string[] { }).Select(x => $"\"{x}\""))}}}";

if (string.IsNullOrEmpty(keywords)) {
keywords = "null";
}

builder.Append("\t[SettingsProvider]\n");
builder.Append($"\tpublic static SettingsProvider Create{name}Provider() {{\n");
builder.Append($"\t\treturn CreateProvider<{configType.FullName}>(\"{displayName}\");\n");
builder.Append(
$"\t\treturn CreateProvider<{configType.FullName}>(\"{displayName}\", {scope}, {keywords});\n");
builder.Append("\t}\n");
builder.Append("\n");
}

builder.Append("}\n");

File.WriteAllText($@"Assets/{Defaults.Instance.CodeGenDirectory}/ConfigAssetsSettingsProvider.cs", builder.ToString());


File.WriteAllText(
$@"Assets/{Defaults.Instance.CodeGenDirectory}/ConfigAssetsSettingsProvider.cs",
builder.ToString());

AssetDatabase.Refresh();
}
}
Expand Down

0 comments on commit 8325ebc

Please sign in to comment.