Skip to content
This repository has been archived by the owner on Jan 18, 2022. It is now read-only.

UTY-1970: invalid enum definition #1412

Merged
merged 10 commits into from
Jul 3, 2020
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@
- Added support for flag arguments in `CommandLineParser`. [#1409](https://github.com/spatialos/gdk-for-unity/pull/1409)
- Added `Scripting Backend` option dropdown to the Build Configuration UI. [#1411](https://github.com/spatialos/gdk-for-unity/pull/1411)

### Breaking Changes

- All generated C# enums will now start from 0, being shifted to schema values on serialization and shifted back to C# values on deserialization. [#1412](https://github.com/spatialos/gdk-for-unity/pull/1412)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typically we put 'Breaking Changes' at the top as that will be most important to a developer when upgrading

- A warning will be generated when enums defined in schema does not start from 0.

BryanJY-Wong marked this conversation as resolved.
Show resolved Hide resolved
### Changed

- Moved Gdk Tools Configuration to the Unity "Project Settings" window under `Spatial OS`. [#1408](https://github.com/spatialos/gdk-for-unity/pull/1408)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ public string GetSerializationStatement(string instance, string schemaObject, ui
case ValueType.Primitive:
return $"{schemaObject}.{SchemaFunctionMappings.AddSchemaFunctionFromType(PrimitiveType.Value)}({fieldNumber}, {instance});";
case ValueType.Enum:
return $"{schemaObject}.AddEnum({fieldNumber}, (uint) {instance});";
var shifter = UnityEnumDetails.GetEnumMinimum(FqnType);
return $"{schemaObject}.AddEnum({fieldNumber}, (uint) ({instance}) + {shifter});";
case ValueType.Type:
return $"{FqnType}.Serialization.Serialize({instance}, {schemaObject}.AddObject({fieldNumber}));";
default:
Expand All @@ -60,7 +61,8 @@ public string GetDeserializationExpression(string schemaObject, uint fieldNumber
return
$"{schemaObject}.{SchemaFunctionMappings.GetSchemaFunctionFromType(PrimitiveType.Value)}({fieldNumber})";
case ValueType.Enum:
return $"({FqnType}) {schemaObject}.GetEnum({fieldNumber})";
var shifter = UnityEnumDetails.GetEnumMinimum(FqnType);
return $"({FqnType}) ({schemaObject}.GetEnum({fieldNumber}) - {shifter})";
Copy link
Contributor Author

@BryanJY-Wong BryanJY-Wong Jul 1, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tried a val + 0 on https://sharplab.io/. Compiler ignores +- 0.

case ValueType.Type:
return $"{FqnType}.Serialization.Deserialize({schemaObject}.GetObject({fieldNumber}))";
default:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,41 @@
using System;
using System.Collections.Generic;
using System.Linq;
using NLog;

namespace Improbable.Gdk.CodeGeneration.Model.Details
{
public class UnityEnumDetails : GeneratorInputDetails
{
public readonly IReadOnlyList<(uint, string)> Values;

private static readonly Dictionary<string, uint> EnumMinimums = new Dictionary<string, uint>();

private static readonly Logger Logger = LogManager.GetCurrentClassLogger();

public UnityEnumDetails(string package, EnumDefinition rawEnumDefinition)
: base(package, rawEnumDefinition)
{
Values = rawEnumDefinition.Values.Select(value => (value.Value, value.Name)).ToList();
var min = rawEnumDefinition.Values.Select(list => list.Value).Min();
BryanJY-Wong marked this conversation as resolved.
Show resolved Hide resolved
EnumMinimums[FullyQualifiedName] = min;
if (min != 0)
{
Logger.Warn($"The enum, {Name}, is defined with a minimum value of {min}, which is greater than 0. Shifting the enum values to start from 0. " +
"This will lead to inconsistencies in the values used in Unity and the values captured in snapshots. " +
$"This inconsistency is handled in the serialization/deserialization process but please consider redefining the values in {Name} to start from 0");
}

Values = rawEnumDefinition.Values.Select(value => (value.Value - min, value.Name)).ToList();
}

public static uint GetEnumMinimum(string fullQualifiedName)
{
if (!EnumMinimums.TryGetValue(fullQualifiedName, out var output))
{
throw new ArgumentException($"Could not find the following enum: {fullQualifiedName}");
}

return output;
}
}
}