Skip to content

Commit

Permalink
Merge branch 'feature/new-type-system' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
desplesda committed Jul 22, 2021
2 parents e78578f + 269ddde commit cae06e7
Show file tree
Hide file tree
Showing 8 changed files with 112 additions and 101 deletions.
32 changes: 14 additions & 18 deletions Editor/Editors/YarnProjectImporterEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -249,29 +249,25 @@ protected override void Apply()

var name = decl.FindPropertyRelative("name").stringValue;

SerializedProperty typeProperty = decl.FindPropertyRelative("type");
SerializedProperty typeProperty = decl.FindPropertyRelative("typeName");

Type type = (Yarn.Type)typeProperty.enumValueIndex;
Yarn.IType type = YarnProjectImporter.SerializedDeclaration.BuiltInTypesList.FirstOrDefault(t => t.Name == typeProperty.stringValue);

var description = decl.FindPropertyRelative("description").stringValue;

object defaultValue;
switch (type)
{
case Yarn.Type.Number:
defaultValue = decl.FindPropertyRelative("defaultValueNumber").floatValue;
break;
case Yarn.Type.String:
defaultValue = decl.FindPropertyRelative("defaultValueString").stringValue;
break;
case Yarn.Type.Bool:
defaultValue = decl.FindPropertyRelative("defaultValueBool").boolValue;
break;
default:
throw new System.ArgumentOutOfRangeException($"Invalid declaration type {type}");
}
System.IConvertible defaultValue;

var declaration = Declaration.CreateVariable(name, defaultValue, description);
if (type == Yarn.BuiltinTypes.Number) {
defaultValue = decl.FindPropertyRelative("defaultValueNumber").floatValue;
} else if (type == Yarn.BuiltinTypes.String) {
defaultValue = decl.FindPropertyRelative("defaultValueString").stringValue;
} else if (type == Yarn.BuiltinTypes.Boolean) {
defaultValue = decl.FindPropertyRelative("defaultValueBool").boolValue;
} else {
throw new System.ArgumentOutOfRangeException($"Invalid declaration type {type.Name}");
}

var declaration = Declaration.CreateVariable(name, type, defaultValue, description);

thisProgramDeclarations.Add(declaration);
}
Expand Down
85 changes: 49 additions & 36 deletions Editor/Importers/YarnProjectImporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,17 @@ public class YarnProjectImporter : ScriptedImporter
[System.Serializable]
public class SerializedDeclaration
{
internal static List<Yarn.IType> BuiltInTypesList = new List<Yarn.IType> {
Yarn.BuiltinTypes.String,
Yarn.BuiltinTypes.Boolean,
Yarn.BuiltinTypes.Number,
};

public string name = "$variable";
public Yarn.Type type = Yarn.Type.String;

[UnityEngine.Serialization.FormerlySerializedAs("type")]
public string typeName = Yarn.BuiltinTypes.String.Name;

public bool defaultValueBool;
public float defaultValueNumber;
public string defaultValueString;
Expand All @@ -36,25 +45,20 @@ public class SerializedDeclaration
public SerializedDeclaration(Declaration decl)
{
this.name = decl.Name;
this.type = decl.ReturnType;
this.typeName = decl.Type.Name;
this.description = decl.Description;
this.isImplicit = decl.IsImplicit;

sourceYarnAsset = AssetDatabase.LoadAssetAtPath<TextAsset>(decl.SourceFileName);

switch (this.type)
{
case Type.Number:
this.defaultValueNumber = System.Convert.ToSingle(decl.DefaultValue);
break;
case Type.String:
this.defaultValueString = System.Convert.ToString(decl.DefaultValue);
break;
case Type.Bool:
this.defaultValueBool = System.Convert.ToBoolean(decl.DefaultValue);
break;
default:
throw new System.InvalidOperationException($"Invalid declaration type {decl.ReturnType}");
if (this.typeName == BuiltinTypes.String.Name) {
this.defaultValueString = System.Convert.ToString(decl.DefaultValue);
} else if (this.typeName == BuiltinTypes.Boolean.Name) {
this.defaultValueBool = System.Convert.ToBoolean(decl.DefaultValue);
} else if (this.typeName == BuiltinTypes.Number.Name) {
this.defaultValueNumber = System.Convert.ToSingle(decl.DefaultValue);
} else {
throw new System.InvalidOperationException($"Invalid declaration type {decl.Type.Name}");
}
}
}
Expand Down Expand Up @@ -160,7 +164,7 @@ public override void OnImportAsset(AssetImportContext ctx)
// We'll replace this with a more complete list later if
// compilation succeeds.
serializedDeclarations = localDeclarations
.Where(decl => decl.DeclarationType == Declaration.Type.Variable)
.Where(decl => !(decl.Type is FunctionType))
.Select(decl => new SerializedDeclaration(decl)).ToList();

// We're done processing this file - we've parsed it, and
Expand Down Expand Up @@ -233,7 +237,7 @@ public override void OnImportAsset(AssetImportContext ctx)
// .yarnproject file, and the ones inside the .yarn files
serializedDeclarations = localDeclarations
.Concat(compilationResult.Declarations)
.Where(decl => decl.DeclarationType == Declaration.Type.Variable)
.Where(decl => !(decl.Type is FunctionType))
.Select(decl => new SerializedDeclaration(decl)).ToList();

// Clear error messages from all scripts - they've all passed
Expand Down Expand Up @@ -575,13 +579,13 @@ private void OnAdd(ReorderableList list)

// Clear necessary properties to something useful
var nameProp = entry.FindPropertyRelative("name");
var typeProp = entry.FindPropertyRelative("type");
var typeProp = entry.FindPropertyRelative("typeName");
var defaultValueStringProp = entry.FindPropertyRelative("defaultValueString");
var sourceYarnAssetProp = entry.FindPropertyRelative("sourceYarnAsset");
var descriptionProp = entry.FindPropertyRelative("description");

nameProp.stringValue = "$variable";
typeProp.enumValueIndex = (int)Yarn.Type.String;
typeProp.enumValueIndex = YarnProjectImporter.SerializedDeclaration.BuiltInTypesList.IndexOf(Yarn.BuiltinTypes.String);
defaultValueStringProp.stringValue = string.Empty;
sourceYarnAssetProp.objectReferenceValue = null;
descriptionProp.stringValue = string.Empty;
Expand Down Expand Up @@ -877,32 +881,41 @@ Rect RectForFieldIndex(int index, int lineCount = 1)

DrawPropertyField(namePosition, nameProperty, propertyIsReadOnly);

SerializedProperty typeProperty = property.FindPropertyRelative("type");
SerializedProperty typeProperty = property.FindPropertyRelative("typeName");

if (propertyIsReadOnly) {
DrawPropertyField(typePosition, typeProperty, true);
} else {
var popupElements = YarnProjectImporter.SerializedDeclaration.BuiltInTypesList;
var popupElementNames = popupElements.Select(t => t.Name).ToList();
var selectedIndex = popupElementNames.IndexOf(typeProperty.stringValue);

DrawPropertyField(typePosition, typeProperty, propertyIsReadOnly);
var prefixPosition = EditorGUI.PrefixLabel(typePosition, new GUIContent("Type"));

selectedIndex = EditorGUI.Popup(prefixPosition, selectedIndex, popupElementNames.ToArray());
if (selectedIndex >= 0 && selectedIndex <= popupElementNames.Count) {
typeProperty.stringValue = popupElementNames[selectedIndex];
}
}

SerializedProperty defaultValueProperty;

switch ((Yarn.Type)typeProperty.enumValueIndex)
{
case Yarn.Type.Number:
defaultValueProperty = property.FindPropertyRelative("defaultValueNumber");
break;
case Yarn.Type.String:
defaultValueProperty = property.FindPropertyRelative("defaultValueString");
break;
case Yarn.Type.Bool:
defaultValueProperty = property.FindPropertyRelative("defaultValueBool");
break;
default:
defaultValueProperty = null;
break;
var type = YarnProjectImporter.SerializedDeclaration.BuiltInTypesList.FirstOrDefault(t => t.Name == typeProperty.stringValue);

if (type == BuiltinTypes.Number) {
defaultValueProperty = property.FindPropertyRelative("defaultValueNumber");
} else if (type == BuiltinTypes.String) {
defaultValueProperty = property.FindPropertyRelative("defaultValueString");
} else if (type == BuiltinTypes.Boolean) {
defaultValueProperty = property.FindPropertyRelative("defaultValueBool");
} else {
defaultValueProperty = null;
}


if (defaultValueProperty == null)
{
EditorGUI.LabelField(defaultValuePosition, "Default Value", $"Variable type {(Yarn.Type)typeProperty.enumValueIndex} is not allowed");
EditorGUI.LabelField(defaultValuePosition, "Default Value", $"Variable type {typeProperty.stringValue} is not allowed");
}
else
{
Expand Down
2 changes: 1 addition & 1 deletion Editor/Utility/YarnProjectUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -544,7 +544,7 @@ internal static void ConvertImplicitVariableDeclarationsToExplicit(YarnProjectIm
return;
}

var implicitDeclarations = implicitResult.Declarations.Where(d => d.DeclarationType == Compiler.Declaration.Type.Variable && d.IsImplicit);
var implicitDeclarations = implicitResult.Declarations.Where(d => !(d.Type is Yarn.FunctionType) && d.IsImplicit);

var output = Yarn.Compiler.Utility.GenerateYarnFileWithDeclarations(explicitResult.Declarations.Concat(implicitDeclarations), "Program");

Expand Down
Binary file modified Runtime/DLLs/YarnSpinner.Compiler.dll
Binary file not shown.
Binary file modified Runtime/DLLs/YarnSpinner.dll
Binary file not shown.
88 changes: 45 additions & 43 deletions Runtime/InMemoryVariableStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,37 +94,39 @@ public string GetDebugList()
/// Used internally by serialization functions to wrap around the
/// SetValue() methods.
/// </summary>
void SetVariable(string name, Yarn.Type type, string value)
void SetVariable(string name, Yarn.IType type, string value)
{
switch (type)
if (type == Yarn.BuiltinTypes.Boolean)
{
case Type.Bool:
bool newBool;
if (bool.TryParse(value, out newBool))
{
SetValue(name, newBool);
}
else
{
throw new System.InvalidCastException($"Couldn't initialize default variable {name} with value {value} as Bool");
}
break;
case Type.Number:
float newNumber;
if (float.TryParse(value, out newNumber))
{ // TODO: this won't work for different cultures (e.g. French write "0.53" as "0,53")
SetValue(name, newNumber);
}
else
{
throw new System.InvalidCastException($"Couldn't initialize default variable {name} with value {value} as Number (Float)");
}
break;
case Type.String:
case Type.Undefined:
default:
SetValue(name, value); // no special type conversion required
break;
bool newBool;
if (bool.TryParse(value, out newBool))
{
SetValue(name, newBool);
}
else
{
throw new System.InvalidCastException($"Couldn't initialize default variable {name} with value {value} as Bool");
}
}
else if (type == Yarn.BuiltinTypes.Number)
{
float newNumber;
if (float.TryParse(value, out newNumber))
{ // TODO: this won't work for different cultures (e.g. French write "0.53" as "0,53")
SetValue(name, newNumber);
}
else
{
throw new System.InvalidCastException($"Couldn't initialize default variable {name} with value {value} as Number (Float)");
}
}
else if (type == Yarn.BuiltinTypes.String)
{
SetValue(name, value); // no special type conversion required
}
else
{
throw new System.ArgumentOutOfRangeException($"Unsupported type {type.Name}");
}
}

Expand Down Expand Up @@ -332,21 +334,21 @@ public void LoadFromFile(string filepath)
Debug.Log($"Variables loaded from file {filepath}");
}

public static readonly Dictionary<System.Type, Yarn.Type> TypeMappings = new Dictionary<System.Type, Yarn.Type>
public static readonly Dictionary<System.Type, Yarn.IType> TypeMappings = new Dictionary<System.Type, Yarn.IType>
{
{ typeof(string), Yarn.Type.String },
{ typeof(bool), Yarn.Type.Bool },
{ typeof(int), Yarn.Type.Number },
{ typeof(float), Yarn.Type.Number },
{ typeof(double), Yarn.Type.Number },
{ typeof(sbyte), Yarn.Type.Number },
{ typeof(byte), Yarn.Type.Number },
{ typeof(short), Yarn.Type.Number },
{ typeof(ushort), Yarn.Type.Number },
{ typeof(uint), Yarn.Type.Number },
{ typeof(long), Yarn.Type.Number },
{ typeof(ulong), Yarn.Type.Number },
{ typeof(decimal), Yarn.Type.Number },
{ typeof(string), Yarn.BuiltinTypes.String },
{ typeof(bool), Yarn.BuiltinTypes.Boolean },
{ typeof(int), Yarn.BuiltinTypes.Number },
{ typeof(float), Yarn.BuiltinTypes.Number },
{ typeof(double), Yarn.BuiltinTypes.Number },
{ typeof(sbyte), Yarn.BuiltinTypes.Number },
{ typeof(byte), Yarn.BuiltinTypes.Number },
{ typeof(short), Yarn.BuiltinTypes.Number },
{ typeof(ushort), Yarn.BuiltinTypes.Number },
{ typeof(uint), Yarn.BuiltinTypes.Number },
{ typeof(long), Yarn.BuiltinTypes.Number },
{ typeof(ulong), Yarn.BuiltinTypes.Number },
{ typeof(decimal), Yarn.BuiltinTypes.Number },
};

#endregion
Expand Down
4 changes: 2 additions & 2 deletions Samples~/Space/Dialogue/Space.yarnproject.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Tests/Runtime/Tests.yarnproject.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit cae06e7

Please sign in to comment.