public static void GenerateScript(Type instanceType, Type serializerType) { // The name of the class, ie, SharedInstance_SystemInt32 string className = instanceType.CSharpName(/*includeNamespace:*/ true, /*ensureSafeDeclarationName:*/ true); if (instanceType.Namespace != null && instanceType.Namespace != "System") { className = RemoveAll(instanceType.Namespace, '.') + className; } className = "SharedInstance_" + className; // The value we will use inside of the generic parameter, ie, // System.Int32 string genericType = instanceType.CSharpName(); if (instanceType.Namespace != null && instanceType.Namespace != "System") { genericType = instanceType.Namespace + "." + genericType; } // The name of the serializer. If null, then no serializer will be // emitted. This is used inside the angle brackets. string serializerName = null; if (serializerType != null) { serializerName = serializerType.CSharpName(includeNamespace: true); } Emit(className, genericType, serializerName); }
/// <summary> /// AOT compiles the object (in C#). /// </summary> private static string GenerateDirectConverterForTypeInCSharp(Type type, fsMetaProperty[] members, bool isConstructorPublic) { var sb = new StringBuilder(); string typeName = type.CSharpName(/*includeNamespace:*/ true); string typeNameSafeDecl = type.CSharpName(true, true); sb.AppendLine("using System;"); sb.AppendLine("using System.Collections.Generic;"); sb.AppendLine(); sb.AppendLine("namespace FullSerializer {"); sb.AppendLine(" partial class fsConverterRegistrar {"); sb.AppendLine(" public static Speedup." + typeNameSafeDecl + "_DirectConverter " + "Register_" + typeNameSafeDecl + ";"); sb.AppendLine(" }"); sb.AppendLine("}"); sb.AppendLine(); sb.AppendLine("namespace FullSerializer.Speedup {"); sb.AppendLine(" public class " + typeNameSafeDecl + "_DirectConverter : fsDirectConverter<" + typeName + "> {"); sb.AppendLine(" protected override fsResult DoSerialize(" + typeName + " model, Dictionary<string, fsData> serialized) {"); sb.AppendLine(" var result = fsResult.Success;"); sb.AppendLine(); foreach (var member in members) { sb.AppendLine(" result += SerializeMember(serialized, " + GetConverterString(member) + ", \"" + member.JsonName + "\", model." + member.MemberName + ");"); } sb.AppendLine(); sb.AppendLine(" return result;"); sb.AppendLine(" }"); sb.AppendLine(); sb.AppendLine(" protected override fsResult DoDeserialize(Dictionary<string, fsData> data, ref " + typeName + " model) {"); sb.AppendLine(" var result = fsResult.Success;"); sb.AppendLine(); for (int i = 0; i < members.Length; ++i) { var member = members[i]; sb.AppendLine(" var t" + i + " = model." + member.MemberName + ";"); sb.AppendLine(" result += DeserializeMember(data, " + GetConverterString(member) + ", \"" + member.JsonName + "\", out t" + i + ");"); sb.AppendLine(" model." + member.MemberName + " = t" + i + ";"); sb.AppendLine(); } sb.AppendLine(" return result;"); sb.AppendLine(" }"); sb.AppendLine(); sb.AppendLine(" public override object CreateInstance(fsData data, Type storageType) {"); if (isConstructorPublic) { sb.AppendLine(" return new " + typeName + "();"); } else { sb.AppendLine(" return Activator.CreateInstance(typeof(" + typeName + "), /*nonPublic:*/true);"); } sb.AppendLine(" }"); sb.AppendLine(" }"); sb.AppendLine("}"); return sb.ToString(); }
/// <summary> /// Setup the instance option manager for the given type. /// </summary> public AbstractTypeInstanceOptionManager(Type baseType) { _options = GetDisplayedTypes(baseType); _displayedOptions = new List<GUIContent>(); _displayedOptions.Add(new GUIContent("null (" + baseType.CSharpName() + ")")); _displayedOptions.AddRange(from option in _options select new GUIContent(option.TypeName)); }
/// <summary> /// Shows a new selection window for a SharedInstance type. /// </summary> /// <param name="instanceType">The generic SharedInstance parameter; that actual instance type.</param> /// <param name="sharedInstanceType">The generic SharedInstance type itself.</param> /// <param name="onSelected">Method to invoke when a new SharedInstance has been selected.</param> public static void Show(Type instanceType, Type sharedInstanceType, Action<UnityObject> onSelected) { var window = fiEditorWindowUtility.ShowUtility<fiSharedInstanceSelectorWindow>("Shared Instance Selector (" + instanceType.CSharpName() + ")"); window._instanceType = instanceType; window._sharedInstanceType = sharedInstanceType; window._onSelected = onSelected; window.Focus(); }
/// <summary> /// Returns the serializer type that the given behavior type uses. /// </summary> public static Type GetSerializerType(Type behaviorType) { for (int i = 0; i < _mappings.Count; ++i) { var mapping = _mappings[i]; if (mapping.BehaviorType.Resolve().IsAssignableFrom(behaviorType.Resolve())) { return mapping.SerializerType; } } throw new InvalidOperationException("Unable to determine serializer for " + behaviorType.CSharpName()); }
private static string GetOptionName(Type type) { string baseName = type.CSharpName(); if (type.IsValueType == false && type.GetConstructor(fsPortableReflection.EmptyTypes) == null) { baseName += " (skips ctor)"; } return baseName; }
private static void CreateNewScriptableObject(Type instanceType) { string assetPath = EditorUtility.SaveFilePanelInProject("Select Path (" + instanceType.CSharpName() + ")", Guid.NewGuid().ToString(), "asset", "", EditorPrefs.GetString(PathPreferencesKey, "Assets")); if (string.IsNullOrEmpty(assetPath) == false) { EditorPrefs.SetString(PathPreferencesKey, Path.GetDirectoryName(assetPath)); ScriptableObject asset = ScriptableObject.CreateInstance(instanceType); AssetDatabase.CreateAsset(asset, assetPath); } }
/// <summary> /// Setup the instance option manager for the given type. /// </summary> public TypeDropdownOptionsManager(Type baseType, bool allowUncreatableTypes) { if (allowUncreatableTypes) _options = fiReflectionUtility.GetTypesDeriving(baseType); else _options = fiReflectionUtility.GetCreatableTypesDeriving(baseType); _displayedOptions = new List<GUIContent>(); _displayedOptions.Add(new GUIContent("null (" + baseType.CSharpName() + ")")); _displayedOptions.AddRange(from option in _options select GetOptionName(option, !allowUncreatableTypes)); }
public static void GenerateScript(Type instanceType, Type serializerType) { // The name of the class, ie, SharedInstance_SystemInt32 string className = instanceType.CSharpName(); if (instanceType.Namespace != null && instanceType.Namespace != "System") { className = RemoveAll(instanceType.Namespace, '.') + className; } className = "SharedInstance_" + className; // The name of the type, ie, System.Int32 // We do a number of replacements so that it will be a valid filename string typeName = instanceType.CSharpName().Replace(" ", "_").Replace("<", "_").Replace(">", "_").Replace(",", "_"); if (instanceType.Namespace != null && instanceType.Namespace != "System") { typeName = instanceType.Namespace + "." + typeName; } // The name of the serializer string serializerName = null; if (serializerType != null) { serializerName = serializerType.CSharpName(); } Emit(className, typeName, serializerName); }
private static string GetOptionName(Type type) { string text = type.CSharpName(); if (!type.IsValueType && type.GetConstructor(Type.EmptyTypes) == null) { text += " (skips ctor)"; } return text; }
private static void CheckForNewBaseBehaviorType(Type type) { // If this is a new BaseBehavior type, it might have been converted // from a MonoBehavior. Additionally, there might have been prefab // instances. The prefab instances can lose their state given some // serialization race conditions. This code removes the race // condition by explicitly serializing all of the instances. // // Here's a description of the race which occurs after changing the // MonoBehaviour to BaseBehavior: // // 1. Unity deserializes the FI objects. FI does an early return here // because there is no data to deserialize. // 2. When you click on a prefab instance in the inspector, that item // will get serialized. Unity will then go to the prefab and // serialize it as well, invoking the serialization callbacks for // those two objects. // 3. The current update loop finishes. // 4. Unity pushes out the prefab serialization changes to the rest // of the prefab instances and calls their deserialization // callbacks. Because these objects have never had Serialize // called on them, they will blindly accept the prefab changes. // 5. When these objects are next serialized, they will use the // incorrect prefab data. Null check, sometimes there is no prefab // state. if (fiPrefabManager.Storage == null || fiPrefabManager.Storage.SeenBaseBehaviors == null) return; if (fiPrefabManager.Storage.SeenBaseBehaviors.Contains(type.CSharpName()) == false) { fiLog.Log(typeof(fiCommonSerializedObjectEditor), "Saving all BaseBehaviors of type " + type.CSharpName()); fiPrefabManager.Storage.SeenBaseBehaviors.Add(type.CSharpName()); EditorUtility.SetDirty(fiPrefabManager.Storage); fiSaveManager.SaveAll(type); } }
/// <summary> /// Returns true if the given RuntimeTypeModel contains a definition for the given type. /// </summary> private static bool ContainsType(RuntimeTypeModel model, Type type) { bool emit = !type.IsNullableType(); bool fastCheck = model.IsDefined(type); // DEBUGGING: comment this if statement out if there are protobuf-net related issues if (emit) { return fastCheck; } foreach (MetaType metaType in model.GetTypes()) { if (metaType.Type == type) { if (emit && fastCheck != true) UnityEngine.Debug.Log("Fast check failed for " + type.CSharpName() + " (nullabe? " + type.IsNullableType() + ")!"); return true; } } if (emit && fastCheck != false) UnityEngine.Debug.Log("Fast check failed for " + type.CSharpName() + " (nullabe? " + type.IsNullableType() + ")!"); return false; }
public DisplayedType(Type type) { Type = type; DisplayName = string.Empty; var attr = fsPortableReflection.GetAttribute<InspectorDropdownNameAttribute>(type, /*shouldCache:*/false); if (attr != null) DisplayName = attr.DisplayName; if (string.IsNullOrEmpty(DisplayName)) DisplayName = type.CSharpName(); }
private void DrawType(fsAotConfiguration.Entry entry, Type resolvedType) { var target = (fsAotConfiguration)this.target; EditorGUILayout.BeginHorizontal(); int currentIndex = GetIndexForState(entry.State); int newIndex = GUILayout.Toolbar(currentIndex, options, GUILayout.ExpandWidth(false)); if (currentIndex != newIndex) { entry.State = GetStateForIndex(newIndex); target.UpdateOrAddEntry(entry); EditorUtility.SetDirty(target); } string displayName = entry.FullTypeName; string tooltip = ""; if (resolvedType != null) { displayName = resolvedType.CSharpName(); tooltip = resolvedType.CSharpName(true); } GUIContent label = new GUIContent(displayName, tooltip); GUILayout.Label(label); GUILayout.FlexibleSpace(); GUIStyle messageStyle = new GUIStyle(EditorStyles.label); string message; if (resolvedType != null) { message = GetAotCompilationMessage(resolvedType); if (string.IsNullOrEmpty(message) == false) { messageStyle.normal.textColor = Color.red; } else { switch (IsOutOfDate(resolvedType)) { case OutOfDateResult.NoAot: message = "No AOT model found"; break; case OutOfDateResult.Stale: message = "Stale"; break; case OutOfDateResult.Current: message = "\u2713"; break; } } } else { message = "Cannot load type"; } GUILayout.Label(message, messageStyle); EditorGUILayout.EndHorizontal(); }