SteamVR_Input_Generator.cs 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using UnityEditor;
  6. using UnityEngine;
  7. using System.CodeDom;
  8. using Microsoft.CSharp;
  9. using System.IO;
  10. using System.CodeDom.Compiler;
  11. using System.Reflection;
  12. using System.Linq.Expressions;
  13. using UnityEditor.SceneManagement;
  14. using UnityEditor.Callbacks;
  15. using Valve.Newtonsoft.Json;
  16. namespace Valve.VR
  17. {
  18. #pragma warning disable 0219 // variable assigned but not used.
  19. public static class SteamVR_Input_Generator
  20. {
  21. public const string steamVRInputOverwriteBuildKey = "SteamVR_Input_OverwriteBuild";
  22. public const string steamVRInputDeleteUnusedKey = "SteamVR_Input_DeleteUnused";
  23. private const string actionSetClassNamePrefix = "SteamVR_Input_ActionSet_";
  24. public const string generationNeedsReloadKey = "SteamVR_Input_GenerationNeedsReload";
  25. private const string progressBarTitle = "SteamVR Input Generation";
  26. public const string steamVRInputActionSetClassesFolder = "ActionSetClasses";
  27. public const string steamVRInputActionsClass = "SteamVR_Input_Actions";
  28. public const string steamVRInputActionSetsClass = "SteamVR_Input_ActionSets";
  29. public const string steamVRInputInitializationClass = "SteamVR_Input_Initialization";
  30. public const string steamVRActionsAssemblyDefinition = "SteamVR_Actions";
  31. private static bool generating = false;
  32. public static void BeginGeneration()
  33. {
  34. generating = true;
  35. fileChanged = false;
  36. SteamVR_Input_EditorWindow.SetProgressBarText("Beginning generation...", 0);
  37. GenerationStep_CreateActionSetClasses();
  38. GenerationStep_CreateHelperClasses();
  39. GenerationStep_CreateInitClass();
  40. GenerationStep_CreateAssemblyDefinition();
  41. DeleteUnusedScripts();
  42. if (fileChanged)
  43. EditorPrefs.SetBool(generationNeedsReloadKey, true);
  44. AssetDatabase.Refresh();
  45. SteamVR_Input_EditorWindow.ClearProgressBar();
  46. generating = false;
  47. }
  48. [DidReloadScripts]
  49. private static void OnReload()
  50. {
  51. bool didGenerate = EditorPrefs.GetBool(generationNeedsReloadKey);
  52. if (didGenerate)
  53. {
  54. EditorPrefs.SetBool(generationNeedsReloadKey, false);
  55. if (string.IsNullOrEmpty(EditorSceneManager.GetActiveScene().path) == false)
  56. EditorApplication.delayCall += ReloadScene;
  57. }
  58. }
  59. public static void ReloadScene()
  60. {
  61. EditorPrefs.SetBool(generationNeedsReloadKey, false);
  62. if (string.IsNullOrEmpty(EditorSceneManager.GetActiveScene().path) == false)
  63. {
  64. if (EditorSceneManager.GetActiveScene().isDirty)
  65. {
  66. EditorSceneManager.SaveCurrentModifiedScenesIfUserWantsTo();
  67. }
  68. string previousPath = EditorSceneManager.GetActiveScene().path;
  69. EditorSceneManager.NewScene(NewSceneSetup.EmptyScene);
  70. EditorSceneManager.OpenScene(previousPath); //reload open scene to avoid any weird serialization
  71. }
  72. }
  73. public static bool IsGenerating()
  74. {
  75. return generating;
  76. }
  77. public static void CancelGeneration()
  78. {
  79. generating = false;
  80. }
  81. private static List<CodeTypeDeclaration> setClasses = new List<CodeTypeDeclaration>();
  82. private static void GenerationStep_CreateInitClass()
  83. {
  84. CodeCompileUnit compileUnit = new CodeCompileUnit();
  85. CodeTypeDeclaration inputClass = CreatePartialInputClass(compileUnit);
  86. CodeMemberMethod preinitMethod = CreateStaticMethod(inputClass, SteamVR_Input_Generator_Names.preinitializeMethodName, true);
  87. string steamVRInputClassName = typeof(SteamVR_Input).Name;
  88. AddStaticInvokeToMethod(preinitMethod, SteamVR_Input_Generator_Names.actionsClassName, startPreInitActionSetsMethodName);
  89. AddStaticInvokeToMethod(preinitMethod, steamVRInputClassName, initializeActionSetDictionariesMethodName);
  90. AddStaticInvokeToMethod(preinitMethod, SteamVR_Input_Generator_Names.actionsClassName, preInitActionsMethodName);
  91. AddStaticInvokeToMethod(preinitMethod, SteamVR_Input_Generator_Names.actionsClassName, initializeActionsArraysMethodName);
  92. AddStaticInvokeToMethod(preinitMethod, steamVRInputClassName, initializeActionDictionariesMethodName);
  93. AddStaticInvokeToMethod(preinitMethod, steamVRInputClassName, finishPreInitActionSetsMethodName);
  94. // Build the output file name.
  95. string fullSourceFilePath = GetSourceFilePath(steamVRInputInitializationClass);
  96. CreateFile(fullSourceFilePath, compileUnit);
  97. }
  98. private static void GenerationStep_CreateAssemblyDefinition()
  99. {
  100. string fullSourceFilePath = GetSourceFilePath(steamVRActionsAssemblyDefinition, ".asmdef");
  101. if (File.Exists(fullSourceFilePath) == false)
  102. {
  103. SteamVR_Input_Unity_AssemblyFile_Definition actionsAssemblyDefinitionData = new SteamVR_Input_Unity_AssemblyFile_Definition();
  104. actionsAssemblyDefinitionData.autoReferenced = true;
  105. string jsonText = JsonConvert.SerializeObject(actionsAssemblyDefinitionData, Formatting.Indented, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Include });
  106. File.WriteAllText(fullSourceFilePath, jsonText);
  107. }
  108. }
  109. private static void GenerationStep_CreateActionSetClasses()
  110. {
  111. SteamVR_Input_EditorWindow.SetProgressBarText("Generating action set classes...", 0.25f);
  112. SteamVR_Input.InitializeFile();
  113. CreateActionsSubFolder();
  114. setClasses = GenerateActionSetClasses();
  115. Debug.LogFormat("<b>[SteamVR Input]</b> Created input script set classes: {0}", setClasses.Count);
  116. }
  117. private static void GenerationStep_CreateHelperClasses()
  118. {
  119. SteamVR_Input_EditorWindow.SetProgressBarText("Generating actions and actionsets classes...", 0.5f);
  120. GenerateActionHelpers(steamVRInputActionsClass);
  121. GenerateActionSetsHelpers(steamVRInputActionSetsClass);
  122. string actionsFullpath = Path.Combine(GetClassPath(), steamVRInputActionsClass + ".cs");
  123. string actionSetsFullpath = Path.Combine(GetClassPath(), steamVRInputActionSetsClass + ".cs");
  124. Debug.LogFormat("<b>[SteamVR Input]</b> Created input script main classes: {0} and {1}", actionsFullpath, actionSetsFullpath);
  125. }
  126. private static void DeleteUnusedScripts()
  127. {
  128. string folderPath = GetSubFolderPath();
  129. string[] files = Directory.GetFiles(folderPath);
  130. List<string> toDelete = new List<string>();
  131. for (int fileIndex = 0; fileIndex < files.Length; fileIndex++)
  132. {
  133. FileInfo file = new FileInfo(files[fileIndex]);
  134. if (file.Name.EndsWith(".cs") || file.Name.EndsWith(".cs.meta"))
  135. {
  136. bool isSet = false;
  137. if (SteamVR_Input.actionFile.action_sets.Any(set => string.Equals(GetSetClassName(set) + ".cs", file.Name, StringComparison.CurrentCultureIgnoreCase) ||
  138. string.Equals(GetSetClassName(set) + ".cs.meta", file.Name, StringComparison.CurrentCultureIgnoreCase)))
  139. {
  140. isSet = true;
  141. }
  142. bool isAction = false;
  143. if (SteamVR_Input.actionFile.actions.Any(action => string.Equals(action.codeFriendlyName + ".cs", file.Name, StringComparison.CurrentCultureIgnoreCase) ||
  144. string.Equals(action.codeFriendlyName + ".cs.meta", file.Name, StringComparison.CurrentCultureIgnoreCase)))
  145. {
  146. isAction = true;
  147. }
  148. if (isSet == false && isAction == false)
  149. {
  150. toDelete.Add(files[fileIndex]);
  151. }
  152. }
  153. }
  154. if (toDelete.Count > 0)
  155. {
  156. string filesToDelete = "";
  157. foreach (string file in toDelete)
  158. filesToDelete += file + "\n";
  159. bool confirm = EditorUtility.DisplayDialog("SteamVR Input", "Would you like to delete the following unused input files:\n" + filesToDelete, "Delete", "No");
  160. if (confirm)
  161. {
  162. foreach (string fileName in toDelete)
  163. {
  164. FileInfo file = new FileInfo(fileName);
  165. file.IsReadOnly = false;
  166. file.Delete();
  167. }
  168. }
  169. }
  170. }
  171. private static void CreateActionsSubFolder()
  172. {
  173. string folderPath = GetSubFolderPath();
  174. if (Directory.Exists(folderPath) == false)
  175. {
  176. Directory.CreateDirectory(folderPath);
  177. }
  178. }
  179. public static void DeleteActionClassFiles()
  180. {
  181. DeleteActionClass(steamVRInputActionsClass);
  182. DeleteActionClass(steamVRInputActionSetsClass);
  183. string folderPath = GetSubFolderPath();
  184. bool confirm = EditorUtility.DisplayDialog("Confirmation", "Are you absolutely sure you want to delete all code files in " + folderPath + "?", "Delete", "Cancel");
  185. if (confirm)
  186. {
  187. DeleteActionObjects("*.cs*");
  188. }
  189. }
  190. public static void DeleteGeneratedFolder()
  191. {
  192. string generatedFolderPath = GetClassPath();
  193. string subFolderPath = GetSubFolderPath();
  194. bool confirm = EditorUtility.DisplayDialog("Confirmation", "Are you absolutely sure you want to delete all code files in " + generatedFolderPath + "?", "Delete", "Cancel");
  195. if (confirm)
  196. {
  197. DeleteActionObjects("*.cs*", generatedFolderPath);
  198. DeleteActionObjects("*.cs*", subFolderPath);
  199. }
  200. }
  201. public static void DeleteActionObjects(string filter, string folderPath = null)
  202. {
  203. if (folderPath == null)
  204. folderPath = GetSubFolderPath();
  205. string[] assets = Directory.GetFiles(folderPath, filter);
  206. for (int assetIndex = 0; assetIndex < assets.Length; assetIndex++)
  207. {
  208. AssetDatabase.DeleteAsset(assets[assetIndex]);
  209. }
  210. Debug.LogFormat("<b>[SteamVR Input]</b> Deleted {0} files at path: {1}", assets.Length, folderPath);
  211. }
  212. private static void DeleteActionClass(string className)
  213. {
  214. string filePath = GetSourceFilePath(className);
  215. if (File.Exists(filePath) == true)
  216. {
  217. AssetDatabase.DeleteAsset(filePath);
  218. Debug.Log("<b>[SteamVR Input]</b> Deleted: " + filePath);
  219. }
  220. else
  221. {
  222. Debug.Log("<b>[SteamVR Input]</b> No file found at: " + filePath);
  223. }
  224. }
  225. private static string GetTypeStringForAction(SteamVR_Input_ActionFile_Action action)
  226. {
  227. return GetTypeForAction(action).Name;
  228. }
  229. private static Type GetTypeForAction(SteamVR_Input_ActionFile_Action action)
  230. {
  231. string actionType = action.type.ToLower();
  232. if (SteamVR_Input_ActionFile_ActionTypes.boolean == actionType)
  233. {
  234. return typeof(SteamVR_Action_Boolean);
  235. }
  236. if (SteamVR_Input_ActionFile_ActionTypes.vector1 == actionType)
  237. {
  238. return typeof(SteamVR_Action_Single);
  239. }
  240. if (SteamVR_Input_ActionFile_ActionTypes.vector2 == actionType)
  241. {
  242. return typeof(SteamVR_Action_Vector2);
  243. }
  244. if (SteamVR_Input_ActionFile_ActionTypes.vector3 == actionType)
  245. {
  246. return typeof(SteamVR_Action_Vector3);
  247. }
  248. if (SteamVR_Input_ActionFile_ActionTypes.pose == actionType)
  249. {
  250. return typeof(SteamVR_Action_Pose);
  251. }
  252. if (SteamVR_Input_ActionFile_ActionTypes.skeleton == actionType)
  253. {
  254. return typeof(SteamVR_Action_Skeleton);
  255. }
  256. if (SteamVR_Input_ActionFile_ActionTypes.vibration == actionType)
  257. {
  258. return typeof(SteamVR_Action_Vibration);
  259. }
  260. throw new System.Exception("unknown type (" + action.type + ") in actions file for action: " + action.name);
  261. }
  262. private static string GetClassPath()
  263. {
  264. string path = Path.Combine(SteamVR.GetSteamVRFolderParentPath(), SteamVR_Settings.instance.steamVRInputPath);
  265. if (Directory.Exists(path) == false)
  266. Directory.CreateDirectory(path);
  267. return path;
  268. }
  269. private static string GetSubFolderPath()
  270. {
  271. return Path.Combine(GetClassPath(), steamVRInputActionSetClassesFolder);
  272. }
  273. private static string GetSourceFilePath(string classname, string suffix = ".cs")
  274. {
  275. string sourceFileName = string.Format("{0}{1}", classname, suffix);
  276. return Path.Combine(GetClassPath(), sourceFileName);
  277. }
  278. private static bool fileChanged = false;
  279. private static void CreateFile(string fullPath, CodeCompileUnit compileUnit)
  280. {
  281. // Generate the code with the C# code provider.
  282. CSharpCodeProvider provider = new CSharpCodeProvider();
  283. // Build the output file name.
  284. string fullSourceFilePath = fullPath;
  285. //Debug.Log("[SteamVR] Writing class to: " + fullSourceFilePath);
  286. string path = GetClassPath();
  287. string priorMD5 = null;
  288. FileInfo file = new FileInfo(fullSourceFilePath);
  289. if (file.Exists)
  290. {
  291. file.IsReadOnly = false;
  292. priorMD5 = SteamVR_Utils.GetBadMD5HashFromFile(fullSourceFilePath);
  293. }
  294. // Create a TextWriter to a StreamWriter to the output file.
  295. using (StreamWriter sw = new StreamWriter(fullSourceFilePath, false))
  296. {
  297. IndentedTextWriter tw = new IndentedTextWriter(sw, " ");
  298. // Generate source code using the code provider.
  299. provider.GenerateCodeFromCompileUnit(compileUnit, tw,
  300. new CodeGeneratorOptions() { BracingStyle = "C" });
  301. // Close the output file.
  302. tw.Close();
  303. string newMD5 = SteamVR_Utils.GetBadMD5HashFromFile(fullSourceFilePath);
  304. if (priorMD5 != newMD5)
  305. fileChanged = true;
  306. }
  307. //Debug.Log("[SteamVR] Complete! Input class at: " + fullSourceFilePath);
  308. }
  309. private const string getActionMethodParamName = "path";
  310. private const string skipStateUpdatesParamName = "skipStateAndEventUpdates";
  311. private static List<CodeTypeDeclaration> GenerateActionSetClasses()
  312. {
  313. List<CodeTypeDeclaration> setClasses = new List<CodeTypeDeclaration>();
  314. for (int actionSetIndex = 0; actionSetIndex < SteamVR_Input.actionFile.action_sets.Count; actionSetIndex++)
  315. {
  316. SteamVR_Input_ActionFile_ActionSet actionSet = SteamVR_Input.actionFile.action_sets[actionSetIndex];
  317. CodeTypeDeclaration setClass = CreateActionSetClass(actionSet);
  318. setClasses.Add(setClass);
  319. }
  320. return setClasses;
  321. }
  322. private const string initializeActionDictionariesMethodName = "PreinitializeActionDictionaries";
  323. private const string initializeActionSetDictionariesMethodName = "PreinitializeActionSetDictionaries";
  324. private const string preInitActionsMethodName = "PreInitActions";
  325. private const string initializeActionsArraysMethodName = "InitializeActionArrays";
  326. private static void GenerateActionHelpers(string actionsClassFileName)
  327. {
  328. CodeCompileUnit compileUnit = new CodeCompileUnit();
  329. CodeTypeDeclaration inputClass = CreatePartialInputClass(compileUnit);
  330. CodeArrayCreateExpression actionsArray = new CodeArrayCreateExpression(new CodeTypeReference(typeof(SteamVR_Action)));
  331. CodeArrayCreateExpression actionsInArray = new CodeArrayCreateExpression(new CodeTypeReference(typeof(ISteamVR_Action_In)));
  332. CodeArrayCreateExpression actionsOutArray = new CodeArrayCreateExpression(new CodeTypeReference(typeof(ISteamVR_Action_Out)));
  333. CodeArrayCreateExpression actionsVibrationArray = new CodeArrayCreateExpression(new CodeTypeReference(typeof(SteamVR_Action_Vibration)));
  334. CodeArrayCreateExpression actionsPoseArray = new CodeArrayCreateExpression(new CodeTypeReference(typeof(SteamVR_Action_Pose)));
  335. CodeArrayCreateExpression actionsSkeletonArray = new CodeArrayCreateExpression(new CodeTypeReference(typeof(SteamVR_Action_Skeleton)));
  336. CodeArrayCreateExpression actionsBooleanArray = new CodeArrayCreateExpression(new CodeTypeReference(typeof(SteamVR_Action_Boolean)));
  337. CodeArrayCreateExpression actionsSingleArray = new CodeArrayCreateExpression(new CodeTypeReference(typeof(SteamVR_Action_Single)));
  338. CodeArrayCreateExpression actionsVector2Array = new CodeArrayCreateExpression(new CodeTypeReference(typeof(SteamVR_Action_Vector2)));
  339. CodeArrayCreateExpression actionsVector3Array = new CodeArrayCreateExpression(new CodeTypeReference(typeof(SteamVR_Action_Vector3)));
  340. CodeArrayCreateExpression actionsNonPoseNonSkeletonArray = new CodeArrayCreateExpression(new CodeTypeReference(typeof(ISteamVR_Action_In)));
  341. //add the getaction method to
  342. CodeMemberMethod actionsArraysInitMethod = CreateStaticMethod(inputClass, initializeActionsArraysMethodName, false);
  343. CodeMemberMethod actionsPreInitMethod = CreateStaticMethod(inputClass, preInitActionsMethodName, false);
  344. for (int actionSetIndex = 0; actionSetIndex < SteamVR_Input.actionFile.action_sets.Count; actionSetIndex++)
  345. {
  346. SteamVR_Input_ActionFile_ActionSet actionSet = SteamVR_Input.actionFile.action_sets[actionSetIndex];
  347. string actionSetShortName = actionSet.shortName;
  348. actionSetShortName = actionSetShortName.Substring(0, 1).ToLower() + actionSetShortName.Substring(1);
  349. for (int actionIndex = 0; actionIndex < actionSet.actionsList.Count; actionIndex++)
  350. {
  351. SteamVR_Input_ActionFile_Action action = actionSet.actionsList[actionIndex];
  352. string actionShortName = action.shortName;
  353. string typeName = GetTypeStringForAction(action);
  354. string codeFriendlyInstanceName;
  355. if (actionSet.actionsList.Count(findAction => findAction.shortName == actionShortName) >= 2)
  356. codeFriendlyInstanceName = string.Format("{0}_{1}_{2}", actionSetShortName, action.direction.ToString().ToLower(), actionShortName);
  357. else
  358. codeFriendlyInstanceName = string.Format("{0}_{1}", actionSetShortName, actionShortName);
  359. CodeMemberField actionField = CreateFieldAndPropertyWrapper(inputClass, codeFriendlyInstanceName, typeName);
  360. AddAssignActionStatement(actionsPreInitMethod, inputClass.Name, actionField.Name, action.name, typeName);
  361. actionsArray.Initializers.Add(new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(inputClass.Name), codeFriendlyInstanceName));
  362. if (action.direction == SteamVR_ActionDirections.In)
  363. {
  364. actionsInArray.Initializers.Add(new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(inputClass.Name), codeFriendlyInstanceName));
  365. if (typeName == typeof(SteamVR_Action_Pose).Name)
  366. {
  367. actionsPoseArray.Initializers.Add(new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(inputClass.Name), codeFriendlyInstanceName));
  368. }
  369. else if (typeName == typeof(SteamVR_Action_Skeleton).Name)
  370. {
  371. actionsSkeletonArray.Initializers.Add(new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(inputClass.Name), codeFriendlyInstanceName));
  372. }
  373. else if (typeName == typeof(SteamVR_Action_Boolean).Name)
  374. {
  375. actionsBooleanArray.Initializers.Add(new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(inputClass.Name), codeFriendlyInstanceName));
  376. }
  377. else if (typeName == typeof(SteamVR_Action_Single).Name)
  378. {
  379. actionsSingleArray.Initializers.Add(new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(inputClass.Name), codeFriendlyInstanceName));
  380. }
  381. else if (typeName == typeof(SteamVR_Action_Vector2).Name)
  382. {
  383. actionsVector2Array.Initializers.Add(new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(inputClass.Name), codeFriendlyInstanceName));
  384. }
  385. else if (typeName == typeof(SteamVR_Action_Vector3).Name)
  386. {
  387. actionsVector3Array.Initializers.Add(new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(inputClass.Name), codeFriendlyInstanceName));
  388. }
  389. if ((typeName == typeof(SteamVR_Action_Skeleton).Name) == false && (typeName == typeof(SteamVR_Action_Pose).Name) == false)
  390. {
  391. actionsNonPoseNonSkeletonArray.Initializers.Add(new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(inputClass.Name), codeFriendlyInstanceName));
  392. }
  393. }
  394. else
  395. {
  396. actionsVibrationArray.Initializers.Add(new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(inputClass.Name), codeFriendlyInstanceName));
  397. actionsOutArray.Initializers.Add(new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(inputClass.Name), codeFriendlyInstanceName));
  398. }
  399. }
  400. }
  401. AddAssignStatement(actionsArraysInitMethod, SteamVR_Input_Generator_Names.actionsFieldName, actionsArray);
  402. AddAssignStatement(actionsArraysInitMethod, SteamVR_Input_Generator_Names.actionsInFieldName, actionsInArray);
  403. AddAssignStatement(actionsArraysInitMethod, SteamVR_Input_Generator_Names.actionsOutFieldName, actionsOutArray);
  404. AddAssignStatement(actionsArraysInitMethod, SteamVR_Input_Generator_Names.actionsVibrationFieldName, actionsVibrationArray);
  405. AddAssignStatement(actionsArraysInitMethod, SteamVR_Input_Generator_Names.actionsPoseFieldName, actionsPoseArray);
  406. AddAssignStatement(actionsArraysInitMethod, SteamVR_Input_Generator_Names.actionsBooleanFieldName, actionsBooleanArray);
  407. AddAssignStatement(actionsArraysInitMethod, SteamVR_Input_Generator_Names.actionsSingleFieldName, actionsSingleArray);
  408. AddAssignStatement(actionsArraysInitMethod, SteamVR_Input_Generator_Names.actionsVector2FieldName, actionsVector2Array);
  409. AddAssignStatement(actionsArraysInitMethod, SteamVR_Input_Generator_Names.actionsVector3FieldName, actionsVector3Array);
  410. AddAssignStatement(actionsArraysInitMethod, SteamVR_Input_Generator_Names.actionsSkeletonFieldName, actionsSkeletonArray);
  411. AddAssignStatement(actionsArraysInitMethod, SteamVR_Input_Generator_Names.actionsNonPoseNonSkeletonIn, actionsNonPoseNonSkeletonArray);
  412. // Build the output file name.
  413. string fullSourceFilePath = GetSourceFilePath(actionsClassFileName);
  414. CreateFile(fullSourceFilePath, compileUnit);
  415. }
  416. private const string startPreInitActionSetsMethodName = "StartPreInitActionSets";
  417. private const string finishPreInitActionSetsMethodName = "PreinitializeFinishActionSets";
  418. private static void GenerateActionSetsHelpers(string actionSetsClassFileName)
  419. {
  420. CodeCompileUnit compileUnit = new CodeCompileUnit();
  421. CodeTypeDeclaration inputClass = CreatePartialInputClass(compileUnit);
  422. CodeMemberMethod startPreInitActionSetsMethod = CreateStaticMethod(inputClass, startPreInitActionSetsMethodName, false);
  423. CodeArrayCreateExpression actionSetsArray = new CodeArrayCreateExpression(new CodeTypeReference(typeof(SteamVR_ActionSet)));
  424. for (int actionSetIndex = 0; actionSetIndex < SteamVR_Input.actionFile.action_sets.Count; actionSetIndex++)
  425. {
  426. SteamVR_Input_ActionFile_ActionSet actionSet = SteamVR_Input.actionFile.action_sets[actionSetIndex];
  427. string shortName = GetValidIdentifier(actionSet.shortName);
  428. string codeFriendlyInstanceName = shortName;
  429. string setTypeName = GetSetClassName(actionSet);
  430. CodeMemberField actionSetField = CreateFieldAndPropertyWrapper(inputClass, shortName, setTypeName);
  431. AddAssignActionSetStatement(startPreInitActionSetsMethod, inputClass.Name, actionSetField.Name, actionSet.name, setTypeName);
  432. actionSetsArray.Initializers.Add(new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(inputClass.Name), codeFriendlyInstanceName));
  433. }
  434. AddAssignStatement(startPreInitActionSetsMethod, SteamVR_Input_Generator_Names.actionSetsFieldName, actionSetsArray);
  435. // Build the output file name.
  436. string fullSourceFilePath = GetSourceFilePath(actionSetsClassFileName);
  437. CreateFile(fullSourceFilePath, compileUnit);
  438. }
  439. private static CSharpCodeProvider provider = new CSharpCodeProvider();
  440. private static string GetValidIdentifier(string name)
  441. {
  442. string newName = name.Replace("-", "_");
  443. newName = provider.CreateValidIdentifier(newName);
  444. return newName;
  445. }
  446. public static MethodInfo GetMethodInfo<T>(Expression<Action<T>> expression)
  447. {
  448. var member = expression.Body as MethodCallExpression;
  449. if (member != null)
  450. return member.Method;
  451. throw new ArgumentException("Expression is not a method", "expression");
  452. }
  453. private static CodeTypeDeclaration CreatePartialInputClass(CodeCompileUnit compileUnit)
  454. {
  455. CodeNamespace codeNamespace = new CodeNamespace(typeof(SteamVR_Input).Namespace);
  456. codeNamespace.Imports.Add(new CodeNamespaceImport("System"));
  457. codeNamespace.Imports.Add(new CodeNamespaceImport("UnityEngine"));
  458. compileUnit.Namespaces.Add(codeNamespace);
  459. CodeTypeDeclaration inputClass = new CodeTypeDeclaration(SteamVR_Input_Generator_Names.actionsClassName);
  460. inputClass.IsPartial = true;
  461. codeNamespace.Types.Add(inputClass);
  462. return inputClass;
  463. }
  464. private static string GetSetClassName(SteamVR_Input_ActionFile_ActionSet set)
  465. {
  466. return actionSetClassNamePrefix + set.shortName;
  467. }
  468. private const string inActionFieldPrefix = "in_";
  469. private const string outActionFieldPrefix = "out_";
  470. private const string setFinishPreInitializeMethodName = "FinishPreInitialize";
  471. private static CodeTypeDeclaration CreateActionSetClass(SteamVR_Input_ActionFile_ActionSet set)
  472. {
  473. CodeCompileUnit compileUnit = new CodeCompileUnit();
  474. CodeNamespace codeNamespace = new CodeNamespace(typeof(SteamVR_Input).Namespace);
  475. codeNamespace.Imports.Add(new CodeNamespaceImport("System"));
  476. codeNamespace.Imports.Add(new CodeNamespaceImport("UnityEngine"));
  477. compileUnit.Namespaces.Add(codeNamespace);
  478. CodeTypeDeclaration setClass = new CodeTypeDeclaration(GetSetClassName(set));
  479. setClass.BaseTypes.Add(typeof(SteamVR_ActionSet));
  480. setClass.Attributes = MemberAttributes.Public;
  481. codeNamespace.Types.Add(setClass);
  482. string actionSetShortName = set.shortName;
  483. actionSetShortName = actionSetShortName.Substring(0, 1).ToLower() + actionSetShortName.Substring(1);
  484. foreach (var inAction in set.actionsInList)
  485. {
  486. string inActionName = inAction.shortName;
  487. if (set.actionsOutList.Any(outAction => inAction.shortName == outAction.shortName))
  488. inActionName = inActionFieldPrefix + inActionName;
  489. string actionClassPropertyName = string.Format("{0}_{1}", actionSetShortName, inActionName);
  490. CreateActionPropertyWrapper(setClass, SteamVR_Input_Generator_Names.actionsClassName, inActionName, actionClassPropertyName, inAction);
  491. }
  492. foreach (var outAction in set.actionsOutList)
  493. {
  494. string outActionName = outAction.shortName;
  495. if (set.actionsInList.Any(inAction => inAction.shortName == outAction.shortName))
  496. outActionName = outActionFieldPrefix + outActionName;
  497. string actionClassPropertyName = string.Format("{0}_{1}", actionSetShortName, outActionName);
  498. CreateActionPropertyWrapper(setClass, SteamVR_Input_Generator_Names.actionsClassName, outActionName, actionClassPropertyName, outAction);
  499. }
  500. // Build the output file name.
  501. string folderPath = GetSubFolderPath();
  502. string fullSourceFilePath = Path.Combine(folderPath, setClass.Name + ".cs");
  503. CreateFile(fullSourceFilePath, compileUnit);
  504. return setClass;
  505. }
  506. private static CodeMemberMethod CreateStaticMethod(CodeTypeDeclaration inputClass, string methodName, bool isPublic)
  507. {
  508. CodeMemberMethod method = new CodeMemberMethod();
  509. method.Name = methodName;
  510. if (isPublic)
  511. method.Attributes = MemberAttributes.Public | MemberAttributes.Static;
  512. else
  513. method.Attributes = MemberAttributes.Private | MemberAttributes.Static;
  514. inputClass.Members.Add(method);
  515. return method;
  516. }
  517. private static CodeMemberMethod CreateStaticConstructorMethod(CodeTypeDeclaration inputClass)
  518. {
  519. CodeTypeConstructor method = new CodeTypeConstructor();
  520. method.Attributes = MemberAttributes.Static;
  521. inputClass.Members.Add(method);
  522. return method;
  523. }
  524. private static CodeMemberField CreateField(CodeTypeDeclaration inputClass, string fieldName, Type fieldType, bool isStatic)
  525. {
  526. if (fieldType == null)
  527. Debug.Log("null fieldType");
  528. CodeMemberField field = new CodeMemberField();
  529. field.Name = fieldName;
  530. field.Type = new CodeTypeReference(fieldType);
  531. field.Attributes = MemberAttributes.Public;
  532. if (isStatic)
  533. field.Attributes |= MemberAttributes.Static;
  534. inputClass.Members.Add(field);
  535. return field;
  536. }
  537. private static CodeMemberField CreateFieldAndPropertyWrapper(CodeTypeDeclaration inputClass, string name, string type)
  538. {
  539. CodeMemberField actionField = CreatePrivateField(inputClass, name, type, true);
  540. CodeMemberProperty actionProperty = CreateStaticProperty(inputClass, name, type, actionField);
  541. return actionField;
  542. }
  543. private static CodeMemberProperty CreateStaticProperty(CodeTypeDeclaration inputClass, string propertyName, string propertyType, CodeMemberField privateField)
  544. {
  545. CodeMemberProperty property = new CodeMemberProperty();
  546. property.Name = propertyName;
  547. property.Type = new CodeTypeReference(propertyType);
  548. property.Attributes = MemberAttributes.Public | MemberAttributes.Static;
  549. CodeFieldReferenceExpression fieldReference = new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(inputClass.Name), privateField.Name);
  550. CodeMethodInvokeExpression invokeExpression = new CodeMethodInvokeExpression(fieldReference, "GetCopy");
  551. invokeExpression.Method.TypeArguments.Add(property.Type);
  552. CodeMethodReturnStatement returnStatement = new CodeMethodReturnStatement(invokeExpression);
  553. property.GetStatements.Add(returnStatement);
  554. inputClass.Members.Add(property);
  555. return property;
  556. }
  557. private static CodeMemberProperty CreateActionPropertyWrapper(CodeTypeDeclaration addToClass, string actionClass, string propertyName, string actionClassFieldName, SteamVR_Input_ActionFile_Action action)
  558. {
  559. string propertyType = GetTypeStringForAction(action);
  560. CodeMemberProperty property = new CodeMemberProperty();
  561. property.Name = propertyName;
  562. property.Type = new CodeTypeReference(propertyType);
  563. property.Attributes = MemberAttributes.Public;
  564. CodeFieldReferenceExpression fieldReference = new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(actionClass), actionClassFieldName);
  565. CodeMethodReturnStatement returnStatement = new CodeMethodReturnStatement(fieldReference);
  566. property.GetStatements.Add(returnStatement);
  567. addToClass.Members.Add(property);
  568. return property;
  569. }
  570. private const string privateFieldPrefix = "p_";
  571. private static CodeMemberField CreatePrivateField(CodeTypeDeclaration inputClass, string fieldName, string fieldType, bool isStatic)
  572. {
  573. return CreateField(inputClass, privateFieldPrefix + fieldName, fieldType, isStatic, false);
  574. }
  575. private static CodeMemberField CreateField(CodeTypeDeclaration inputClass, string fieldName, string fieldType, bool isStatic, bool isPublic = true)
  576. {
  577. CodeMemberField field = new CodeMemberField();
  578. field.Name = fieldName;
  579. field.Type = new CodeTypeReference(fieldType);
  580. if (isPublic)
  581. field.Attributes = MemberAttributes.Public;
  582. else
  583. field.Attributes = MemberAttributes.Private;
  584. if (isStatic)
  585. field.Attributes |= MemberAttributes.Static;
  586. inputClass.Members.Add(field);
  587. return field;
  588. }
  589. private static CodeMethodInvokeExpression AddStaticInvokeToMethod(CodeMemberMethod methodToAddTo, string classToInvoke, string invokeMethodName)
  590. {
  591. CodeMethodInvokeExpression invokeMethod = new CodeMethodInvokeExpression(new CodeMethodReferenceExpression(
  592. new CodeTypeReferenceExpression(classToInvoke), invokeMethodName));
  593. methodToAddTo.Statements.Add(invokeMethod);
  594. return invokeMethod;
  595. }
  596. private static void AddAssignStatement(CodeMemberMethod methodToAddTo, string fieldToAssign, CodeArrayCreateExpression array)
  597. {
  598. methodToAddTo.Statements.Add(new CodeAssignStatement(new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(typeof(SteamVR_Input)), fieldToAssign), array));
  599. }
  600. private const string createActionMethodName = "Create";
  601. private const string createActionSetMethodName = "Create";
  602. private const string getActionFromPathMethodName = "GetActionFromPath";
  603. //grab = SteamVR_Action.Create<SteamVR_Action_Boolean>("path");
  604. private static void AddAssignActionStatement(CodeMemberMethod methodToAddTo, string actionClassName, string fieldToAssign, string actionPath, string actionType)
  605. {
  606. CodeMethodInvokeExpression invokeMethod = new CodeMethodInvokeExpression(new CodeMethodReferenceExpression(new CodeTypeReferenceExpression(typeof(SteamVR_Action).Name), createActionMethodName));
  607. invokeMethod.Method.TypeArguments.Add(actionType);
  608. invokeMethod.Parameters.Add(new CodePrimitiveExpression(actionPath));
  609. methodToAddTo.Statements.Add(new CodeAssignStatement(new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(actionClassName), fieldToAssign), new CodeCastExpression(new CodeTypeReference(actionType), invokeMethod)));
  610. }
  611. private static void AddAssignActionSetStatement(CodeMemberMethod methodToAddTo, string actionClassName, string fieldToAssign, string actionSetName, string actionSetType)
  612. {
  613. CodeMethodInvokeExpression invokeMethod = new CodeMethodInvokeExpression(new CodeMethodReferenceExpression(new CodeTypeReferenceExpression(typeof(SteamVR_ActionSet).Name), createActionSetMethodName));
  614. invokeMethod.Method.TypeArguments.Add(actionSetType);
  615. invokeMethod.Parameters.Add(new CodePrimitiveExpression(actionSetName));
  616. methodToAddTo.Statements.Add(new CodeAssignStatement(new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(actionClassName), fieldToAssign), new CodeCastExpression(new CodeTypeReference(actionSetType), invokeMethod)));
  617. }
  618. private static void AddAssignLocalActionStatement(CodeMemberMethod methodToAddTo, string fieldToAssign, string actionPath, string actionType, bool create)
  619. {
  620. CodeMethodInvokeExpression invokeMethod;
  621. if (create)
  622. invokeMethod = new CodeMethodInvokeExpression(new CodeMethodReferenceExpression(new CodeTypeReferenceExpression(typeof(SteamVR_Action).Name), createActionMethodName));
  623. else
  624. invokeMethod = new CodeMethodInvokeExpression(new CodeMethodReferenceExpression(new CodeTypeReferenceExpression(typeof(SteamVR_Input).Name), getActionFromPathMethodName));
  625. invokeMethod.Method.TypeArguments.Add(actionType);
  626. invokeMethod.Parameters.Add(new CodePrimitiveExpression(actionPath));
  627. methodToAddTo.Statements.Add(new CodeAssignStatement(new CodeFieldReferenceExpression(new CodeThisReferenceExpression(), fieldToAssign), new CodeCastExpression(new CodeTypeReference(actionType), invokeMethod)));
  628. }
  629. private static void AddAssignNewInstanceStatement(CodeMemberMethod methodToAddTo, string fieldToAssign, string fieldType)
  630. {
  631. CodeObjectCreateExpression createExpression = new CodeObjectCreateExpression(new CodeTypeReference(fieldType));
  632. methodToAddTo.Statements.Add(new CodeAssignStatement(new CodeFieldReferenceExpression(new CodeThisReferenceExpression(), fieldToAssign), createExpression));
  633. }
  634. private static CodeConditionStatement CreateStringCompareStatement(CodeMemberMethod methodToAddTo, string action, string paramName, string returnActionName)
  635. {
  636. MethodInfo stringEqualsMethodInfo = GetMethodInfo<string>(set => string.Equals(null, null, StringComparison.CurrentCultureIgnoreCase));
  637. CodeTypeReferenceExpression stringType = new CodeTypeReferenceExpression(typeof(string));
  638. CodePrimitiveExpression actionName = new CodePrimitiveExpression(action);
  639. CodeVariableReferenceExpression pathName = new CodeVariableReferenceExpression(paramName);
  640. CodeVariableReferenceExpression caseInvariantName = new CodeVariableReferenceExpression("StringComparison.CurrentCultureIgnoreCase");
  641. CodeMethodInvokeExpression stringCompare = new CodeMethodInvokeExpression(stringType, stringEqualsMethodInfo.Name, pathName, actionName, caseInvariantName);
  642. CodeMethodReturnStatement returnAction = new CodeMethodReturnStatement(new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(typeof(SteamVR_Input)), returnActionName));
  643. CodeConditionStatement condition = new CodeConditionStatement(stringCompare, returnAction);
  644. methodToAddTo.Statements.Add(condition);
  645. return condition;
  646. }
  647. }
  648. }