Skip to content

Commit

Permalink
Added support for variants instead of types.
Browse files Browse the repository at this point in the history
  • Loading branch information
vchelaru committed Feb 10, 2024
1 parent e782664 commit b1f3fdc
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 19 deletions.
2 changes: 1 addition & 1 deletion Engines/FlatRedBallXNA/FlatRedBall/FlatRedBallServices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public class SyntaxVersionAttribute : Attribute
public int Version;
}

[SyntaxVersion(Version=52)]
[SyntaxVersion(Version=53)]
public static partial class FlatRedBallServices
{
internal static SingleThreadSynchronizationContext singleThreadSynchronizationContext;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ private void UpdateIncludedAndExcluded(CustomVariable instance)
}


HandleTypeProperty(instance);
IncludeOrExcludeTypeProperty(instance);

if (instance.DefinedByBase)
{
Expand Down Expand Up @@ -116,7 +116,7 @@ private void UpdateIncludedAndExcluded(CustomVariable instance)

}

private void HandleTypeProperty(CustomVariable variable)
private void IncludeOrExcludeTypeProperty(CustomVariable variable)
{

if (variable != null &&
Expand Down
18 changes: 13 additions & 5 deletions FRBDK/Glue/Glue/Reflection/ExposedVariableManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
using FlatRedBall.Utilities;
using FlatRedBall.Instructions.Reflection;
using FlatRedBall.Math.Geometry;
using static FlatRedBall.Glue.SaveClasses.GlueProjectSave;


namespace FlatRedBall.Glue.Reflection
Expand Down Expand Up @@ -1023,6 +1024,8 @@ public static List<string> GetAvailableNewVariableTypes(bool allowNone = true, b
{
List<string> toReturn = new List<string>();

var glueProject = ObjectFinder.Self.GlueProject;

toReturn.AddRange(mAvailablePrimitives);

if (allowNone == false)
Expand All @@ -1042,7 +1045,7 @@ void TryAddRfsType(ReferencedFileSave rfs)
}
}
}
foreach (ReferencedFileSave rfs in ObjectFinder.Self.GlueProject.GlobalFiles)
foreach (ReferencedFileSave rfs in glueProject.GlobalFiles)
{
TryAddRfsType(rfs);
}
Expand All @@ -1051,7 +1054,7 @@ void TryAddRfsType(ReferencedFileSave rfs)
// element, but any CSV in the project will make a class
// that is accessible from outside of that class, and we want
// to make sure that we can do the same thing
foreach (var element in ObjectFinder.Self.GlueProject.AllElements())
foreach (var element in glueProject.AllElements())
{
foreach(var rfs in element.ReferencedFiles)
{
Expand All @@ -1066,18 +1069,23 @@ void TryAddRfsType(ReferencedFileSave rfs)
// if screens are needed...if so
// add them later.



if(!GlueState.Self.CurrentGlueProject.SuppressBaseTypeGeneration)
{
foreach(var entity in ObjectFinder.Self.GlueProject.Entities)
string typeOrVariant = glueProject.FileVersion >= (int)GluxVersions.VariantsInsteadOfTypes
? "Variant"
: "Type";
foreach (var entity in glueProject.Entities)
{
var isBaseEntity = string.IsNullOrEmpty(entity.BaseElement);

if(isBaseEntity)
{
var hasDerived = ObjectFinder.Self.GlueProject.Entities.Any(item => item.BaseElement == entity.Name);
var hasDerived = glueProject.Entities.Any(item => item.BaseElement == entity.Name);
if(hasDerived)
{
var typeName = entity.Name.Replace("\\", ".") + "Type";
var typeName = entity.Name.Replace("\\", ".") + typeOrVariant;
toReturn.Add(typeName);
}
}
Expand Down
12 changes: 9 additions & 3 deletions FRBDK/Glue/Glue/SaveClasses/CustomVariableExtensionMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using Microsoft.Xna.Framework;
using WpfDataUi.Controls;
using FlatRedBall.Glue.Plugins;
using static FlatRedBall.Glue.SaveClasses.GlueProjectSave;

namespace FlatRedBall.Glue.SaveClasses
{
Expand Down Expand Up @@ -84,12 +85,17 @@ public static bool GetIsBaseElementType(this CustomVariable customVariable, out
public static bool GetIsBaseElementType(string type, out GlueElement element)
{
element = null;
if (!GlueState.CurrentGlueProject.SuppressBaseTypeGeneration && type.Contains("."))
var glueProject = GlueState.CurrentGlueProject;
if (!glueProject.SuppressBaseTypeGeneration && type.Contains("."))
{
if (type.StartsWith("Entities.") && type.EndsWith("Type"))
string typeOrVariant = glueProject.FileVersion >= (int)GluxVersions.VariantsInsteadOfTypes
? "Variant"
: "Type";

if (type.StartsWith("Entities.") && type.EndsWith(typeOrVariant))
{
// strip of Entities. and Type and see if there's an entity with a matching name:
var entityName = type.Substring(0, type.Length - "Type".Length).Replace(".", "\\");
var entityName = type.Substring(0, type.Length - typeOrVariant.Length).Replace(".", "\\");

element = GlueState.CurrentGlueProject.Entities.FirstOrDefault(item => item.Name == entityName);

Expand Down
8 changes: 5 additions & 3 deletions FRBDK/Glue/Glue/SaveClasses/GlueProjectSave.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,16 +135,18 @@ public enum GluxVersions
// Technically this isn't referencing GumCommon project, but it's referencing code from GumCommon
GumCommonCodeReferencing = 50,
GumTextSupportsBbCode = 51,
DamageDealingToggles = 52
DamageDealingToggles = 52,

// Stop! If adding an entry here, modify SyntaxVersionAttribute.Version
VariantsInsteadOfTypes = 53,

// Stop! If adding an entry here, modify SyntaxVersionAttribute on FlatRedBallServices
}

#endregion

#region Versions

public const int LatestVersion = (int)GluxVersions.DamageDealingToggles;
public const int LatestVersion = (int)GluxVersions.VariantsInsteadOfTypes;

public int FileVersion { get; set; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1026,7 +1026,13 @@ private static object ConvertVariableValueToCurrentType(string newType, object c

}
}
else if(oldType?.EndsWith("Type") == true || newType?.EndsWith("Variant") == true)
{
// This is converting form a type to something else like a variant. Let's leave it the same

newValue = currentValue?.ToString();
wasAbleToConvert = true;
}
if (wasAbleToConvert == false)
{
newValue = CustomVariableExtensionMethods.GetDefaultValueAccordingToType(newType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
using static FlatRedBall.Glue.SaveClasses.GlueProjectSave;

namespace OfficialPlugins.ElementInheritanceTypePlugin.CodeGenerators
{
Expand All @@ -19,8 +20,10 @@ public override void GenerateAdditionalClasses(ICodeBlock codeBlock, IElement el
{
var derivedElements = ObjectFinder.Self.GetAllDerivedElementsRecursive(element as GlueElement);

var glueProject = GlueState.Self.CurrentGlueProject;

var shouldGenerate = derivedElements.Count > 0 &&
GlueState.Self.CurrentGlueProject.SuppressBaseTypeGeneration == false;
glueProject.SuppressBaseTypeGeneration == false;


/////////////////////Early Out////////////////////////////////
Expand All @@ -30,7 +33,11 @@ public override void GenerateAdditionalClasses(ICodeBlock codeBlock, IElement el
}
///////////////////End Early Out//////////////////////////////

var classBlock = codeBlock.Class("public partial", $"{element.ClassName}Type");
string typeOrVariant = glueProject.FileVersion >= (int)GluxVersions.VariantsInsteadOfTypes
? "Variant"
: "Type";

var classBlock = codeBlock.Class("public partial", $"{element.ClassName}{typeOrVariant}");

classBlock.Line("public string Name { get; set; }");
classBlock.Line("public override string ToString() {return Name; }");
Expand All @@ -50,7 +57,7 @@ public override void GenerateAdditionalClasses(ICodeBlock codeBlock, IElement el

foreach (var derivedElement in derivedElements)
{
classBlock.Line($"public static {element.ClassName}Type {derivedElement.ClassName} {{ get; private set; }} = new {element.ClassName}Type");
classBlock.Line($"public static {element.ClassName}{typeOrVariant} {derivedElement.ClassName} {{ get; private set; }} = new {element.ClassName}{typeOrVariant}");
var block = classBlock.Block();
block.Line($"Name = \"{derivedElement.ClassName}\",");
block.Line($"Type = typeof({derivedElement.Name.Replace("/", ".").Replace("\\", ".")}),");
Expand Down Expand Up @@ -89,7 +96,7 @@ public override void GenerateAdditionalClasses(ICodeBlock codeBlock, IElement el
classBlock.Line(";");
}

classBlock.Line($"public static List<{element.ClassName}Type> All = new List<{element.ClassName}Type>{{");
classBlock.Line($"public static List<{element.ClassName}{typeOrVariant}> All = new List<{element.ClassName}{typeOrVariant}>{{");
var innerList = classBlock.CodeBlockIndented();
foreach (var derivedElement in derivedElements)
{
Expand All @@ -102,7 +109,12 @@ public override void GenerateAdditionalClasses(ICodeBlock codeBlock, IElement el

private static void CreateFromNameMethod(IElement element, List<GlueElement> derivedElements, ICodeBlock classBlock)
{
var fromName = classBlock.Function($"public static {element.ClassName}Type", "FromName", "string name");
var glueProject = GlueState.Self.CurrentGlueProject;

string typeOrVariant = glueProject.FileVersion >= (int)GluxVersions.VariantsInsteadOfTypes
? "Variant"
: "Type";
var fromName = classBlock.Function($"public static {element.ClassName}{typeOrVariant}", "FromName", "string name");
var switchBlock = fromName.Switch("name");
foreach (var derivedElement in derivedElements)
{
Expand Down

0 comments on commit b1f3fdc

Please sign in to comment.