Last active
August 29, 2015 14:21
-
-
Save thebeardphantom/a07475a7e15ba46e6ead to your computer and use it in GitHub Desktop.
RewiredStaticClassGenerator
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
using System.Collections.Generic; | |
using System.IO; | |
using System.Text; | |
using UnityEditor; | |
using UnityEngine; | |
public class RewiredStaticClassGenerator : ScriptableWizard { | |
//Templates for generation | |
const string OC_TEMPLATE = "\n\npublic static class {0} {{\n{1}}}"; | |
const string IC_TEMPLATE = "\t/// <summary>\n\t/// {0}\n\t/// </summary>\n\tpublic static class {1} {{\n{2}\t}}\n"; | |
const string ACTION_TEMPLATE = "\t\t/// <summary>\n\t\t/// {0}\n\t\t/// </summary>\n\t\tpublic const string {2} = {3};\n"; | |
static RewiredStaticClassGenerator instance; | |
InputManager im; | |
string finalOutput = string.Empty; | |
//To watch field changes | |
string oldNamespace = string.Empty; | |
string oldClassName = "RWSL"; | |
public string className = "RWSL"; | |
[TextArea(0, 50)] | |
public string output = ""; | |
[MenuItem("Tools/Generate Rewired Statics")] | |
static void GenerateList() { | |
instance = ScriptableWizard.DisplayWizard<RewiredStaticClassGenerator>("Rewired Static List Generator", "Create", "Cancel"); | |
//Try to find InputManager in currently loaded scene | |
instance.im = FindObjectOfType<InputManager>(); | |
if(instance.im == null) { | |
EditorUtility.DisplayDialog("InputManager Not Found", "Must load scene with a Rewired InputManager!", "OK"); | |
instance.Close(); | |
} | |
} | |
private void GenerateFinalOutput() { | |
if(im == null) { | |
return; | |
} | |
//Create dictionary Action Categories -> Actions | |
var actions = im.userData.GetActions_Copy(); | |
var dict = new Dictionary<InputCategory, List<InputAction>>(); | |
foreach(var a in actions) { | |
var category = im.userData.GetActionCategoryById(a.categoryId); | |
if(!dict.ContainsKey(category)) { | |
dict[category] = new List<InputAction>(); | |
} | |
dict[category].Add(a); | |
} | |
//Will hold all subclasses (for category separation) | |
var innerClasses = new List<string>(); | |
StringBuilder sb; | |
foreach(var e in dict) { | |
sb = new StringBuilder(); | |
//Create individual fields for final inner class | |
foreach(var a in e.Value) { | |
var docStr = GetActionDocStr(a); | |
sb.AppendLine(string.Format(ACTION_TEMPLATE, docStr, a.name, "\"" + a.name + "\"")); | |
} | |
//Remove final trailing newline | |
sb.Remove(sb.Length - 1, 1); | |
var icDocString = GetCategoryDocStr(e.Key); | |
//Add list of fields to inner class (category) string | |
var innerClass = string.Format(IC_TEMPLATE, icDocString, e.Key.name, sb.ToString()); | |
innerClasses.Add(innerClass); | |
} | |
sb = new StringBuilder(); | |
//Create single string block of all inner classes | |
foreach(var ic in innerClasses) { | |
sb.AppendLine(ic); | |
} | |
//Add inner class string block to the containing class template | |
finalOutput = string.Format(OC_TEMPLATE, className, sb.ToString()).Trim(); | |
} | |
string GetCategoryDocStr(InputCategory ic) { | |
return string.Format("DescriptiveName: {0}\n\t/// ID: {1}", ic.descriptiveName, ic.id); | |
} | |
string GetActionDocStr(InputAction a) { | |
var aDocNLFormat = isNamespaced ? "\n\t\t\t/// " : "\n\t\t/// "; | |
return a.type == InputActionType.Axis ? string.Format("{1}{0}Type: {2}{0}PosAxis: {3}{0}NegAxis: {4}", aDocNLFormat, a.descriptiveName, a.type, a.positiveDescriptiveName, a.negativeDescriptiveName) : | |
string.Format("{1}{0}Type: {2}", aDocNLFormat, a.descriptiveName, a.type); | |
} | |
void OnWizardCreate() { | |
var saveLocation = EditorUtility.SaveFilePanel("Save Static List", Application.dataPath, className, "cs"); | |
if(saveLocation == null) { | |
return; | |
} | |
File.WriteAllText(saveLocation, finalOutput); | |
AssetDatabase.Refresh(); | |
} | |
void OnWizardUpdate() { | |
//If fields changed, regenerate output | |
className = className.Trim(); | |
if(string.IsNullOrEmpty(finalOutput) || oldClassName != className) { | |
GenerateFinalOutput(); | |
oldClassName = className; | |
} | |
output = "===BEGIN OUTPUT PREVIEW===\n\n" + finalOutput + "\n\n===END OUTPUT PREVIEW==="; | |
//Check for invalid values | |
isValid = false; | |
if(string.IsNullOrEmpty(className)) { | |
errorString = "MUST SPECIFY CLASS NAME"; | |
} | |
else { | |
errorString = ""; | |
isValid = true; | |
} | |
} | |
void OnWizardOtherButton() { | |
instance.Close(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment