public EvHolder(int typesLength) { FolderPath = string.Empty; EditorFolderPath = string.Empty; ListenerMode = ListenerGenMode.AwakeDestroy; EventGenMode = EventGenerationMode.BasicEvent | EventGenerationMode.UnityEvent; Types = new string[typesLength]; for (int i = 0; i < Types.Length; i++) { Types[i] = string.Empty; } }
void GenerateEventCode(ListenerGenMode listenerMode, EventGenerationMode eventGenMode, string[] types, string nameSpace, string fullTargetFolderPath, string fullTargetEditorFolderPath) { if (eventGenMode == EventGenerationMode.None) { return; } if (types.Length != EventTypesNumber) { throw new ArgumentException("Internal error. Event generator requires " + EventTypesNumber + " number of types as input."); } string allTypes = "Void"; string registerMethod = string.Empty; string unregisterMethod = string.Empty; if (listenerMode == ListenerGenMode.AwakeDestroy) { registerMethod = "protected virtual void Awake()"; unregisterMethod = "protected virtual void OnDestroy()"; } else if (listenerMode == ListenerGenMode.OnEnableOnDisable) { registerMethod = "protected virtual void OnEnable()"; unregisterMethod = "protected virtual void OnDisable()"; } for (int i = 0; i < types.Length; i++) { string read = types[i]; if (read != null && read.Length > 0) { if (i == 0) { allTypes = string.Empty; } if (read.Length > 1) { allTypes = allTypes + char.ToUpper(read[0]) + read.Substring(1); } else { allTypes = allTypes + read.ToUpper(); } } } //Iterate through all recovered types and create usefull strings string genericTypes = string.Empty; string argumentsWithTypes = string.Empty; string arguments = string.Empty; if (types[0] != null && types[0] != string.Empty) { genericTypes = "<"; for (int j = 0; j < types.Length; j++) { if (types[j] != null && types[j] != string.Empty) { genericTypes = genericTypes + types[j]; arguments = arguments + "Value" + j; argumentsWithTypes = argumentsWithTypes + types[j] + " Value" + j; if (j != 3 && types[j + 1] != null && types[j + 1] != string.Empty) { genericTypes = genericTypes + ", "; arguments = arguments + ", "; argumentsWithTypes = argumentsWithTypes + ", "; } } } genericTypes = genericTypes + ">"; } string baseEventClassName = "BaseSOEv" + allTypes; string eventAssetFileName = "\"Event\""; string basEventAssetFileName = "\"BasicEvent\""; string eventAssetMenuName = "\"" + "SOPRO/Events/" + allTypes + "\""; string basEventAssetMenuName = "\"" + "SOPRO/BasicEvents/" + allTypes + "\""; string eventClassName = "SOEv" + allTypes; string basicEventClassName = "SOBasicEv" + allTypes; string wrapperClassName = "UnEv" + allTypes; string editorClassName = eventClassName + "Drawer"; string basEditorClassName = basicEventClassName + "Drawer"; string listenerClassName = eventClassName + "Listener"; string unityEventClassName = "UnityEvent" + genericTypes; SOEventGenerator eventGenerator = new SOEventGenerator(); SOEventListenerGenerator listenerGenerator = new SOEventListenerGenerator(); UnityEventWrapperGenerator wrapperGenerator = new UnityEventWrapperGenerator(); SOEventEditorGenerator editorGenerator = new SOEventEditorGenerator(); SOEventEditorGenerator basicEditorGenerator = new SOEventEditorGenerator(); SOBasicEventGenerator basEventGenerator = new SOBasicEventGenerator(); BaseSOEventGenerator baseEvClassGenerator = new BaseSOEventGenerator(); List <string> validTypes = new List <string>(); for (int i = 0; i < types.Length; i++) { string res = types[i]; if (res != null && res.Length > 0) { validTypes.Add(res); } } wrapperGenerator.ClassName = wrapperClassName; wrapperGenerator.Namespace = nameSpace; wrapperGenerator.UnityEventTypeName = unityEventClassName; listenerGenerator.ClassName = listenerClassName; listenerGenerator.GenericArguments = arguments; listenerGenerator.GenericArgumentsWithTypes = argumentsWithTypes; listenerGenerator.Namespace = nameSpace; listenerGenerator.RegisterMethodSignature = registerMethod; listenerGenerator.UnregisterMethodSignature = unregisterMethod; listenerGenerator.SOEventTypeName = eventClassName; listenerGenerator.UnityEventWrapperTypeName = wrapperClassName; eventGenerator.AssetFileName = eventAssetFileName; eventGenerator.AssetMenuName = eventAssetMenuName; eventGenerator.ClassName = eventClassName; eventGenerator.GenericArguments = arguments; eventGenerator.GenericArgumentsWithTypes = argumentsWithTypes; eventGenerator.Namespace = nameSpace; eventGenerator.SOEventListenerTypeName = listenerClassName; eventGenerator.ValidTypes = validTypes.ToArray(); eventGenerator.BaseClassName = baseEventClassName; basEventGenerator.AssetFileName = basEventAssetFileName; basEventGenerator.AssetMenuName = basEventAssetMenuName; basEventGenerator.ClassName = basicEventClassName; basEventGenerator.GenericArguments = arguments; basEventGenerator.GenericArgumentsWithTypes = argumentsWithTypes; basEventGenerator.Namespace = nameSpace; basEventGenerator.ValidTypes = validTypes.ToArray(); basEventGenerator.BaseClassName = baseEventClassName; editorGenerator.ClassName = editorClassName; editorGenerator.Namespace = (nameSpace == null || nameSpace.Length == 0) ? nameSpace : nameSpace + ".Editor"; editorGenerator.SOEventTypeName = eventClassName; editorGenerator.AllValidTypes = validTypes.ToArray(); basicEditorGenerator.ClassName = basEditorClassName; basicEditorGenerator.Namespace = (nameSpace == null || nameSpace.Length == 0) ? nameSpace : nameSpace + ".Editor"; basicEditorGenerator.SOEventTypeName = basicEventClassName; basicEditorGenerator.AllValidTypes = validTypes.ToArray(); baseEvClassGenerator.Namespace = nameSpace; baseEvClassGenerator.ClassName = baseEventClassName; baseEvClassGenerator.GenericArgumentsWithTypes = argumentsWithTypes; string eventCode = eventGenerator.TransformText(); string basEventCode = basEventGenerator.TransformText(); string listenerCode = listenerGenerator.TransformText(); string wrapperCode = wrapperGenerator.TransformText(); string editorCode = editorGenerator.TransformText(); string basEditorCode = basicEditorGenerator.TransformText(); string baseEventCode = baseEvClassGenerator.TransformText(); if (!Directory.Exists(fullTargetFolderPath)) { Directory.CreateDirectory(fullTargetFolderPath); } if (!Directory.Exists(fullTargetEditorFolderPath)) { Directory.CreateDirectory(fullTargetEditorFolderPath); } //generate base class for events string fileName = Path.Combine(fullTargetFolderPath, Path.ChangeExtension(baseEventClassName, ".cs")); if (!File.Exists(fileName)) { File.WriteAllText(fileName, baseEventCode); } else { Debug.LogWarning("Error occurred while attempting code generation from " + this + " , file " + fileName + " already exists"); } //generate basic event code if ((eventGenMode & EventGenerationMode.BasicEvent) != 0) { fileName = Path.Combine(fullTargetFolderPath, Path.ChangeExtension(basicEventClassName, ".cs")); if (!File.Exists(fileName)) { File.WriteAllText(fileName, basEventCode); } else { Debug.LogWarning("Error occurred while attempting code generation from " + this + " , file " + fileName + " already exists"); } fileName = Path.Combine(fullTargetEditorFolderPath, Path.ChangeExtension(basEditorClassName, ".cs")); if (!File.Exists(fileName)) { File.WriteAllText(fileName, basEditorCode); } else { Debug.LogWarning("Error occurred while attempting code generation from " + this + " , file " + fileName + " already exists"); } } //generate unity event code if ((eventGenMode & EventGenerationMode.UnityEvent) != 0) { fileName = Path.Combine(fullTargetFolderPath, Path.ChangeExtension(eventClassName, ".cs")); if (!File.Exists(fileName)) { File.WriteAllText(fileName, eventCode); } else { Debug.LogWarning("Error occurred while attempting code generation from " + this + " , file " + fileName + " already exists"); } fileName = Path.Combine(fullTargetFolderPath, Path.ChangeExtension(listenerClassName, ".cs")); if (!File.Exists(fileName)) { File.WriteAllText(fileName, listenerCode); } else { Debug.LogWarning("Error occurred while attempting code generation from " + this + " , file " + fileName + " already exists"); } fileName = Path.Combine(fullTargetFolderPath, Path.ChangeExtension(wrapperClassName, ".cs")); if (!File.Exists(fileName)) { File.WriteAllText(fileName, wrapperCode); } else { Debug.LogWarning("Error occurred while attempting code generation from " + this + " , file " + fileName + " already exists"); } fileName = Path.Combine(fullTargetEditorFolderPath, Path.ChangeExtension(editorClassName, ".cs")); if (!File.Exists(fileName)) { File.WriteAllText(fileName, editorCode); } else { Debug.LogWarning("Error occurred while attempting code generation from " + this + " , file " + fileName + " already exists"); } } }