From 6064d12de09ac6baa9df88a30722b212b942d0e1 Mon Sep 17 00:00:00 2001 From: Kalle Jillheden Date: Sat, 5 Feb 2022 15:35:24 +0000 Subject: [PATCH] Json.NET Unity Converters 1.4.0 Based on commit c4ce7e5668039daa603ef24ba8d917bb722ecefc Created by CircleCI job Build #1218 https://circleci.com/gh/jilleJr/Newtonsoft.Json-for-Unity.Converters/1218 --- CHANGELOG.md | 13 ++++++ Editor/UnityConvertersConfigEditor.cs | 3 +- Newtonsoft.Json.UnityConverters.asmdef | 2 +- Properties/AssemblyInfo.cs | 3 ++ .../{Properties.meta => Addressables.meta} | 2 +- .../Addressables/AssetReferenceConverter.cs | 43 +++++++++++++++++++ .../AssetReferenceConverter.cs.meta} | 2 +- ...t.Json.UnityConverters.Addressables.asmdef | 24 +++++++++++ ...n.UnityConverters.Addressables.asmdef.meta | 7 +++ UnityConverters/Properties/AssemblyInfo.cs | 4 -- UnityConverters/UnityConverterInitializer.cs | 10 ++++- package.json | 2 +- 12 files changed, 104 insertions(+), 11 deletions(-) rename UnityConverters/{Properties.meta => Addressables.meta} (77%) create mode 100644 UnityConverters/Addressables/AssetReferenceConverter.cs rename UnityConverters/{Properties/AssemblyInfo.cs.meta => Addressables/AssetReferenceConverter.cs.meta} (83%) create mode 100644 UnityConverters/Addressables/Newtonsoft.Json.UnityConverters.Addressables.asmdef create mode 100644 UnityConverters/Addressables/Newtonsoft.Json.UnityConverters.Addressables.asmdef.meta delete mode 100644 UnityConverters/Properties/AssemblyInfo.cs diff --git a/CHANGELOG.md b/CHANGELOG.md index cb21ae0..7a88bd8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,18 @@ # Unity Converters for Newtonsoft.Json changelog +## 1.4.0 (2022-02-05) + +- Added support for `UnityEngine.AddressableAssets.AssetReference`. + The new `AssetReferenceConverter` is only included in the build if your + project contains the `com.unity.addressables` package. + ([#67](https://github.com/jilleJr/Newtonsoft.Json-for-Unity.Converters/pull/67)) + + This automatic inclusion relies on AssemblyDefinition version defines, which + was introduced in Unity 2019.1.x. To enable the `AssetReferenceConverter` in + earlier versions of Unity, please add `HAVE_MODULE_ADDRESSABLES` to your + project's "Scripting Define Symbols" found in the + "Project Settings" -> "Player" -> "Other Settings" panel. + ## 1.3.0 (2021-10-21) - Changed the following modules to be automatically excluded from compilation diff --git a/Editor/UnityConvertersConfigEditor.cs b/Editor/UnityConvertersConfigEditor.cs index 7061774..5fdea36 100644 --- a/Editor/UnityConvertersConfigEditor.cs +++ b/Editor/UnityConvertersConfigEditor.cs @@ -197,7 +197,8 @@ private void FoldoutConvertersList(SerializedProperty property, AnimBool fadedAn .Select(o => ( serializedProperty: o, type: TypeCache.FindType(o.FindPropertyRelative(nameof(ConverterConfig.converterName)).stringValue) - )); + )) + .OrderBy(o => o.type.FullName); foreach (var namespaceGroup in allConfigsWithType.GroupBy(o => GetTypeNamespace(o.type))) { diff --git a/Newtonsoft.Json.UnityConverters.asmdef b/Newtonsoft.Json.UnityConverters.asmdef index 71f6225..6730f4f 100644 --- a/Newtonsoft.Json.UnityConverters.asmdef +++ b/Newtonsoft.Json.UnityConverters.asmdef @@ -27,4 +27,4 @@ "define": "HAVE_MODULE_PHYSICS2D" } ] -} \ No newline at end of file +} diff --git a/Properties/AssemblyInfo.cs b/Properties/AssemblyInfo.cs index f7d76ff..0573696 100644 --- a/Properties/AssemblyInfo.cs +++ b/Properties/AssemblyInfo.cs @@ -1,4 +1,7 @@ using System.Runtime.CompilerServices; +[assembly: InternalsVisibleTo("Newtonsoft.Json.UnityConverters.Addressables")] [assembly: InternalsVisibleTo("Newtonsoft.Json.UnityConverters.Editor")] +[assembly: InternalsVisibleTo("Newtonsoft.Json.UnityConverters.Tests")] +[assembly: InternalsVisibleTo("Newtonsoft.Json.UnityConverters.Tests.Addressables")] diff --git a/UnityConverters/Properties.meta b/UnityConverters/Addressables.meta similarity index 77% rename from UnityConverters/Properties.meta rename to UnityConverters/Addressables.meta index 4e36e5d..9f4a2b9 100644 --- a/UnityConverters/Properties.meta +++ b/UnityConverters/Addressables.meta @@ -1,5 +1,5 @@ fileFormatVersion: 2 -guid: 62fc377cb3b792843b5dba8775afd7bc +guid: 1d3d6db2520f0b14da096b76a6cd8827 folderAsset: yes DefaultImporter: externalObjects: {} diff --git a/UnityConverters/Addressables/AssetReferenceConverter.cs b/UnityConverters/Addressables/AssetReferenceConverter.cs new file mode 100644 index 0000000..d917a84 --- /dev/null +++ b/UnityConverters/Addressables/AssetReferenceConverter.cs @@ -0,0 +1,43 @@ +using System; +using Newtonsoft.Json.UnityConverters.Helpers; +using UnityEngine.AddressableAssets; + +namespace Newtonsoft.Json.UnityConverters.Addressables +{ + public class AssetReferenceConverter : JsonConverter + { + public override bool CanConvert(Type objectType) + { + return objectType == typeof(AssetReference); + } + + public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) + { + if (reader.TokenType == JsonToken.Null) + { + return null; + } + + if (reader.TokenType == JsonToken.String && reader.Value is string stringValue) + { + return new AssetReference(stringValue); + } + else + { + throw reader.CreateSerializationException($"Expected string when reading UnityEngine.Addressables.AssetReference type, got '{reader.TokenType}' <{reader.Value}>."); + } + } + + public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) + { + if (value is null || string.IsNullOrEmpty(((AssetReference)value).AssetGUID)) + { + writer.WriteNull(); + } + else + { + writer.WriteValue(((AssetReference)value).AssetGUID); + } + } + } +} diff --git a/UnityConverters/Properties/AssemblyInfo.cs.meta b/UnityConverters/Addressables/AssetReferenceConverter.cs.meta similarity index 83% rename from UnityConverters/Properties/AssemblyInfo.cs.meta rename to UnityConverters/Addressables/AssetReferenceConverter.cs.meta index ce56268..c80b953 100644 --- a/UnityConverters/Properties/AssemblyInfo.cs.meta +++ b/UnityConverters/Addressables/AssetReferenceConverter.cs.meta @@ -1,5 +1,5 @@ fileFormatVersion: 2 -guid: 000ecf2d50e027c4d98cc27e0079e451 +guid: 599e3e9b08862c44db7fb4ba60611b38 MonoImporter: externalObjects: {} serializedVersion: 2 diff --git a/UnityConverters/Addressables/Newtonsoft.Json.UnityConverters.Addressables.asmdef b/UnityConverters/Addressables/Newtonsoft.Json.UnityConverters.Addressables.asmdef new file mode 100644 index 0000000..0d21212 --- /dev/null +++ b/UnityConverters/Addressables/Newtonsoft.Json.UnityConverters.Addressables.asmdef @@ -0,0 +1,24 @@ +{ + "name": "Newtonsoft.Json.UnityConverters.Addressables", + "references": [ + "Unity.Addressables", + "Newtonsoft.Json.UnityConverters" + ], + "optionalUnityReferences": [], + "includePlatforms": [], + "excludePlatforms": [], + "allowUnsafeCode": false, + "overrideReferences": false, + "precompiledReferences": [], + "autoReferenced": true, + "defineConstraints": [ + "HAVE_MODULE_ADDRESSABLES" + ], + "versionDefines": [ + { + "name": "com.unity.addressables", + "expression": "", + "define": "HAVE_MODULE_ADDRESSABLES" + } + ] +} diff --git a/UnityConverters/Addressables/Newtonsoft.Json.UnityConverters.Addressables.asmdef.meta b/UnityConverters/Addressables/Newtonsoft.Json.UnityConverters.Addressables.asmdef.meta new file mode 100644 index 0000000..57c49fd --- /dev/null +++ b/UnityConverters/Addressables/Newtonsoft.Json.UnityConverters.Addressables.asmdef.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: 54b32bbb6d015da4881a30377f540d42 +AssemblyDefinitionImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/UnityConverters/Properties/AssemblyInfo.cs b/UnityConverters/Properties/AssemblyInfo.cs deleted file mode 100644 index 155c420..0000000 --- a/UnityConverters/Properties/AssemblyInfo.cs +++ /dev/null @@ -1,4 +0,0 @@ -using System.Runtime.CompilerServices; - -[assembly: InternalsVisibleTo("Newtonsoft.Json.UnityConverters.Tests")] -[assembly: InternalsVisibleTo("Newtonsoft.Json.UnityConverters.Editor")] diff --git a/UnityConverters/UnityConverterInitializer.cs b/UnityConverters/UnityConverterInitializer.cs index 13ed656..423c280 100644 --- a/UnityConverters/UnityConverterInitializer.cs +++ b/UnityConverters/UnityConverterInitializer.cs @@ -41,7 +41,7 @@ public static class UnityConverterInitializer /// /// The default given by Newtonsoft.Json-for-Unity.Converters /// - /// + /// /// /// All its properties stay default, but the Converters includes below: /// 1. Any custom has constructor without parameters. @@ -178,7 +178,13 @@ private static IEnumerable FindFilteredUnityConverters(UnityConvertersConf /// The types. internal static IEnumerable FindUnityConverters() { - return FilterToJsonConvertersAndOrder(typeof(UnityConverterInitializer).Assembly.GetTypes()); + var typesFromPackageDomains = AppDomain.CurrentDomain.GetAssemblies() + .Select(dll => dll.GetLoadableTypes() + .Where(type => type.Namespace?.StartsWith("Newtonsoft.Json.UnityConverters") == true) + ) + .SelectMany(types => types) + .OrderBy(type => type.FullName); + return FilterToJsonConvertersAndOrder(typesFromPackageDomains); } private static IEnumerable FindFilteredJsonNetConverters(UnityConvertersConfig config) diff --git a/package.json b/package.json index 252358c..9abda21 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "jillejr.newtonsoft.json-for-unity.converters", "displayName": "Json.NET Converters of Unity types", - "version": "1.3.0", + "version": "1.4.0", "unity": "2018.1", "description": "This package contains converters to and from common Unity types for Newtonsoft.Json. Types such as Vector2, Vector3, Matrix4x4, Quaternions, Color, even ScriptableObject, and more.\n\nGoes hand in hand with the jillejr.newtonsoft.json-for-unity package.\n\nThis package is licensed under The MIT License (MIT)\n\nCopyright © 2019 Kalle Jillheden (jilleJr)\nhttps://github.com/jilleJr/Newtonsoft.Json-for-Unity.Converters\n\nCopyright © 2020 Wanzyee Studio\nhttp://wanzyeestudio.blogspot.com/2017/03/jsonnet-converters.html\n\nCopyright © 2007 ParentElement\nhttps://github.com/ianmacgillivray/Json-NET-for-Unity\n\nCopyright © 2017 .NET Foundation and Contributors\nhttps://github.com/dotnet/runtime\n\nSee full copyrights in LICENSE.md inside package", "main": "index.js",