-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Re-added entity state configuration window to the project.
- Loading branch information
Showing
3 changed files
with
125 additions
and
1 deletion.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
113 changes: 113 additions & 0 deletions
113
...oR2EditorKit/Editor/ScriptsForRoR2/Windows/AssetCreators/CreateEntityStateConfigWindow.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,113 @@ | ||
using HG.GeneralSerializer; | ||
using RoR2; | ||
using RoR2EditorKit.Common; | ||
using RoR2EditorKit.Core; | ||
using RoR2EditorKit.Core.Windows; | ||
using System; | ||
using System.Linq; | ||
using System.Reflection; | ||
using UnityEditor; | ||
using UnityEngine; | ||
|
||
namespace RoR2EditorKit.RoR2.EditorWindows | ||
{ | ||
public class CreateEntityStateConfigWindow : CreateRoR2ScriptableObjectWindow<EntityStateConfiguration> | ||
{ | ||
public EntityStateConfiguration entityStateConfiguration; | ||
private bool deriveNameFromType = true; | ||
|
||
[MenuItem(Constants.RoR2EditorKitContextRoot + "EntityStateConfiguration", false, Constants.RoR2EditorKitContextPriority)] | ||
public static void Open() | ||
{ | ||
OpenEditorWindow<CreateEntityStateConfigWindow>(null, "Create Entity State Configuration"); | ||
} | ||
|
||
protected override void OnWindowOpened() | ||
{ | ||
base.OnWindowOpened(); | ||
|
||
entityStateConfiguration = (EntityStateConfiguration)scriptableObject; | ||
mainSerializedObject = new SerializedObject(entityStateConfiguration); | ||
} | ||
|
||
private void OnGUI() | ||
{ | ||
EditorGUILayout.BeginHorizontal(); | ||
EditorGUILayout.BeginVertical("box"); | ||
|
||
deriveNameFromType = EditorGUILayout.Toggle("Derive AssetName from Type", true); | ||
|
||
if (!deriveNameFromType) | ||
{ | ||
nameField = EditorGUILayout.TextField("Asset Name", nameField); | ||
} | ||
|
||
DrawField("targetType", true); | ||
|
||
if (SimpleButton("Create EntityStateConfiguration")) | ||
{ | ||
var result = CreateEntityStateConfiguration(); | ||
if (result) | ||
{ | ||
Debug.Log($"Succesfully Created EntityStateConfiguration {entityStateConfiguration.name}"); | ||
TryToClose(); | ||
} | ||
} | ||
|
||
EditorGUILayout.EndVertical(); | ||
EditorGUILayout.EndHorizontal(); | ||
} | ||
|
||
private bool CreateEntityStateConfiguration() | ||
{ | ||
try | ||
{ | ||
var targetType = mainSerializedObject.FindProperty("targetType"); | ||
var assemblyQualifiedName = targetType.FindPropertyRelative("assemblyQualifiedName").stringValue; | ||
|
||
Type stateType = Type.GetType(assemblyQualifiedName); | ||
if (stateType != null) | ||
{ | ||
var allFieldsInType = stateType.GetFields(BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic); | ||
var filteredFields = allFieldsInType.Where(fieldInfo => | ||
{ | ||
bool canSerialize = SerializedValue.CanSerializeField(fieldInfo); | ||
bool shouldSerialize = !fieldInfo.IsStatic || (fieldInfo.DeclaringType == stateType); | ||
return canSerialize && shouldSerialize; | ||
}); | ||
|
||
var staticFields = filteredFields.Where(fieldInfo => fieldInfo.IsStatic); | ||
var instanceFields = filteredFields.Where(fieldInfo => !fieldInfo.IsStatic); | ||
|
||
|
||
FieldInfo[] fieldsToSerialize = staticFields.Union(instanceFields).ToArray(); | ||
|
||
Debug.LogWarning(fieldsToSerialize.Length); | ||
|
||
for (int i = 0; i < fieldsToSerialize.Length; i++) | ||
{ | ||
entityStateConfiguration.serializedFieldsCollection.GetOrCreateField(fieldsToSerialize[i].Name); | ||
} | ||
|
||
if (deriveNameFromType) | ||
entityStateConfiguration.SetNameFromTargetType(); | ||
else | ||
entityStateConfiguration.name = nameField; | ||
|
||
Util.CreateAssetAtSelectionPath(entityStateConfiguration); | ||
|
||
return true; | ||
} | ||
else | ||
{ | ||
throw new NullReferenceException($"could not find type {targetType}"); | ||
} | ||
} | ||
catch (Exception e) | ||
{ | ||
Debug.LogError($"Error while creating Entity State Configuration: {e}"); | ||
return false; | ||
} | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
...itorKit/Editor/ScriptsForRoR2/Windows/AssetCreators/CreateEntityStateConfigWindow.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.