This repository has been archived by the owner on Oct 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
26ec073
commit 1fa01a4
Showing
16 changed files
with
1,314 additions
and
2,238 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
62 changes: 62 additions & 0 deletions
62
Packages/com.lurking-ninja.input-codegen/Editor/AssetPostProcessorHelper.cs
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,62 @@ | ||
/*** | ||
* Input System Codegen | ||
* Copyright (c) 2022-2024 Lurking Ninja. | ||
* | ||
* MIT License | ||
* https://github.com/LurkingNinja/com.lurking-ninja.input-codegen | ||
*/ | ||
namespace LurkingNinja.Input.Editor | ||
{ | ||
using System; | ||
using System.IO; | ||
using System.Linq; | ||
using UnityEditor; | ||
using UnityEngine; | ||
|
||
public static class AssetPostProcessorHelper | ||
{ | ||
private static string GetFullPath(string fileName, string path) => | ||
$"{Application.dataPath}/../{path}{fileName}.cs"; | ||
|
||
private static string GetPath(string fileName, string path) => | ||
$"{path}{fileName}.cs"; | ||
|
||
public static string KeyToCSharpWithoutAt(string key) => KeyToCSharp(key, false); | ||
|
||
public static string KeyToCSharp(string key, bool addAt = true) | ||
{ | ||
if(string.IsNullOrEmpty(key)) | ||
throw new ArgumentOutOfRangeException(nameof(key), "Key cannot be empty or null."); | ||
|
||
var outKey = ""; | ||
if (!char.IsLetter(key[0]) && key[0] != '_') | ||
outKey = $"_{key}"; | ||
outKey = key.Where(t1 => char.IsLetterOrDigit(t1) || t1 != '_').Aggregate(outKey, (current, t1) => current + t1); | ||
outKey = addAt ? $"@{outKey}" : outKey; | ||
var isValidIdentifier = new Microsoft.CSharp.CSharpCodeProvider().IsValidIdentifier(outKey); | ||
|
||
return !isValidIdentifier | ||
? throw new ArgumentOutOfRangeException(nameof(key), | ||
"Key should be resolvable into a valid C# identifier.") | ||
: outKey; | ||
} | ||
|
||
private static string GetFileName(string fileName) => | ||
Path.GetFileNameWithoutExtension(fileName).Replace(" ", "_"); | ||
|
||
public static void DeleteFile(string fileName, string path) => | ||
AssetDatabase.DeleteAsset(GetPath(GetFileName(fileName), path)); | ||
|
||
public static void WriteFile(string fileName, string path, string content) | ||
{ | ||
fileName = GetFileName(fileName); | ||
var genPath = GetFullPath(fileName, path); | ||
var folderOnly = Path.GetDirectoryName(genPath); | ||
if (!Directory.Exists(folderOnly) && folderOnly != null) | ||
Directory.CreateDirectory(folderOnly); | ||
using var writer = new StreamWriter(genPath, false); | ||
writer.WriteLine(content); | ||
AssetDatabase.ImportAsset($"{path}{fileName}.cs"); | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
Packages/com.lurking-ninja.input-codegen/Editor/AssetPostProcessorHelper.cs.meta
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
96 changes: 96 additions & 0 deletions
96
Packages/com.lurking-ninja.input-codegen/Editor/OnAssetPostProcessor.cs
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,96 @@ | ||
/*** | ||
* Input System Codegen | ||
* Copyright (c) 2022-2024 Lurking Ninja. | ||
* | ||
* MIT License | ||
* https://github.com/LurkingNinja/com.lurking-ninja.input-codegen | ||
*/ | ||
namespace LurkingNinja.Input.Editor | ||
{ | ||
using System; | ||
using System.Collections.Generic; | ||
using UnityEditor; | ||
using Object = UnityEngine.Object; | ||
|
||
// To detect creation and saving an asset. We do not care about moving. | ||
public class OnAssetPostProcessor : AssetPostprocessor | ||
{ | ||
private static readonly Dictionary<Type, List<Action<Object, string>>> _CHANGE_CALLBACKS = new(); | ||
internal static readonly Dictionary<Type, List<Action<Object, string>>> DELETE_CALLBACKS = new(); | ||
|
||
private static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets, | ||
string[] movedAssets, string[] movedFromAssetPaths) | ||
{ | ||
foreach (var path in importedAssets) | ||
{ | ||
foreach (var keyValue in _CHANGE_CALLBACKS) | ||
{ | ||
var asset = AssetDatabase.LoadAssetAtPath(path, keyValue.Key); | ||
if (asset is null) continue; | ||
foreach (var action in keyValue.Value) | ||
{ | ||
action?.Invoke(asset, path); | ||
} | ||
} | ||
} | ||
} | ||
|
||
public static void AddListener(Type key, | ||
Action<Object, string> changeCallback, Action<Object, string> deleteCallback) | ||
{ | ||
AddChangeListener(key, changeCallback); | ||
AddDeletionListener(key, deleteCallback); | ||
} | ||
|
||
public static void RemoveListener(Type key, | ||
Action<Object, string> changeCallback, Action<Object, string> deleteCallback) | ||
{ | ||
RemoveChangeListener(key, changeCallback); | ||
RemoveDeletionListener(key, deleteCallback); | ||
} | ||
|
||
private static void AddChangeListener(Type key, Action<Object, string> callback) | ||
{ | ||
if (!_CHANGE_CALLBACKS.ContainsKey(key)) _CHANGE_CALLBACKS[key] = new List<Action<Object, string>>(); | ||
if (_CHANGE_CALLBACKS[key].Contains(callback)) return; | ||
_CHANGE_CALLBACKS[key].Add(callback); | ||
} | ||
|
||
private static void RemoveChangeListener(Type key, Action<Object, string> callback) | ||
{ | ||
if (!_CHANGE_CALLBACKS.TryGetValue(key, out var changeCallback)) return; | ||
changeCallback.Remove(callback); | ||
} | ||
|
||
private static void AddDeletionListener(Type key, Action<Object, string> callback) | ||
{ | ||
if (!DELETE_CALLBACKS.ContainsKey(key)) DELETE_CALLBACKS[key] = new List<Action<Object, string>>(); | ||
if (DELETE_CALLBACKS[key].Contains(callback)) return; | ||
DELETE_CALLBACKS[key].Add(callback); | ||
} | ||
|
||
private static void RemoveDeletionListener(Type key, Action<Object, string> callback) | ||
{ | ||
if (!DELETE_CALLBACKS.TryGetValue(key, out var deleteCallback)) return; | ||
deleteCallback.Remove(callback); | ||
} | ||
} | ||
|
||
// To detect asset removal. | ||
public class CustomAssetModificationProcessor : AssetModificationProcessor | ||
{ | ||
private static AssetDeleteResult OnWillDeleteAsset(string path, RemoveAssetOptions rao) | ||
{ | ||
foreach (var keyValue in OnAssetPostProcessor.DELETE_CALLBACKS) | ||
{ | ||
var asset = AssetDatabase.LoadAssetAtPath(path, keyValue.Key); | ||
if (asset is null) continue; | ||
foreach (var action in keyValue.Value) | ||
{ | ||
action?.Invoke(asset, path); | ||
} | ||
} | ||
return AssetDeleteResult.DidNotDelete; | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
Packages/com.lurking-ninja.input-codegen/Editor/OnAssetPostProcessor.cs.meta
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
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
Oops, something went wrong.