Skip to content

Commit

Permalink
Improved project reference adding.
Browse files Browse the repository at this point in the history
Fixed defaults on objects added through Gum to respect recursive values.
  • Loading branch information
vchelaru committed Jan 21, 2024
1 parent f635218 commit ce0d908
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 13 deletions.
32 changes: 32 additions & 0 deletions FRBDK/Glue/Glue/VSHelpers/VSSolution.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,38 @@ public string FullFileName
set;
}

public static bool RemoveProjectReference(FilePath solution, string project, out string output, out string error)
{
output = string.Empty;
error = string.Empty;

if(solution.Exists())
{
var contents = System.IO.File.ReadAllText(solution.FullPath);

if(contents?.Contains(project) == true)
{
var indexOfProjectReference = contents.IndexOf(project);
const string startOfTextToRemove = "Project(\"";
const string endOfTextToRemove = "EndProject";

var indexOfStart = contents.LastIndexOf(startOfTextToRemove, indexOfProjectReference);
var indexOfEnd = contents.IndexOf(endOfTextToRemove, indexOfProjectReference + project.Length);

if(indexOfStart > -1 && indexOfEnd > -1)
{
var endWithLenth = indexOfEnd + endOfTextToRemove.Length;
// Remove between indexOfStart and indexOfEnd
contents = contents.Remove(indexOfStart, endWithLenth - indexOfStart);

System.IO.File.WriteAllText(solution.FullPath, contents);
return true;
}
}
}
return false;
}

public static bool AddExistingProjectWithDotNet(FilePath solution, FilePath project, out string output, out string error)
{
output = null;
Expand Down
15 changes: 13 additions & 2 deletions FRBDK/Glue/GumPlugin/GumPlugin/Managers/AssetTypeInfoManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using Xceed.Wpf.Toolkit.PropertyGrid.Attributes;
using static FlatRedBall.Glue.SaveClasses.GlueProjectSave;
using FlatRedBall.Glue.Parsing;
using Gum.DataTypes.Variables;

namespace GumPlugin.Managers
{
Expand Down Expand Up @@ -770,14 +771,19 @@ public void UpdateAtiForElement(AssetTypeInfo newAti, ElementSave element)
variableName = "Current" + variableName;
}

var hasAlreadyBeenAdded = newAti.VariableDefinitions.Any(item => item.Name == variableName);
var existing = newAti.VariableDefinitions.FirstOrDefault(item => item.Name == variableName);
var hasAlreadyBeenAdded = existing != null;

if (!hasAlreadyBeenAdded)
{

var variableDefinition = new VariableDefinition();
variableDefinition.Category = variable.Category;
variableDefinition.DefaultValue = variable.Value?.ToString();

// We should bubble up the recursive value so that it can show here:
//variableDefinition.DefaultValue = variable.Value?.ToString();
variableDefinition.DefaultValue = state.GetValueRecursive(variableName)?.ToString();

variableDefinition.Name = variableName; // gum variables can have spaces, but Glue variables can't

variableDefinition.Type = QualifyGumVariableType(variable, element);
Expand All @@ -787,6 +793,11 @@ public void UpdateAtiForElement(AssetTypeInfo newAti, ElementSave element)
newAti.VariableDefinitions.Add(variableDefinition);

}
else
{
// This could be different per type so let's do this:
existing.DefaultValue = state.GetValueRecursive(variableName)?.ToString();
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,8 @@ private static async Task LinkToSourceInternal(string frbRootFolder, string gumR
{
await TaskManager.Self.AddAsync(() =>
{
var projectReferences = GetProjectReferencesForCurrentProject();
var projectReferences = GetNecessaryProjectReferencesForCurrentProject();
var necessaryReferencesStripped = projectReferences.Select(item => FileManager.RemovePath(item.RelativeProjectFilePath)).ToArray();

string outerError = null;
string innerError;
Expand Down Expand Up @@ -211,31 +212,53 @@ await TaskManager.Self.AddAsync(() =>

if (!string.IsNullOrEmpty(frbRootFolder) && !string.IsNullOrEmpty(gumRootFolder))
{
//Update project references
var slnFilePath = GlueState.Self.CurrentSlnFileName;
var sln = VSSolution.FromFile(slnFilePath);

var referencedProjects = sln.ReferencedProjects;

//Update project references
var sln = VSSolution.FromFile(GlueState.Self.CurrentSlnFileName);
List<string> slnProjectReferencesToRemove = new List<string>();
// remove any old references:
foreach(var existingReference in referencedProjects)
{
var strippedExisting = FileManager.RemovePath(existingReference);

var willBeReplaced = necessaryReferencesStripped.Contains(strippedExisting);

if(willBeReplaced)
{
slnProjectReferencesToRemove.Add(existingReference);
}
}

foreach(var referenceToRemove in slnProjectReferencesToRemove)
{
VSSolution.RemoveProjectReference(slnFilePath, referenceToRemove, out string _, out string _);
}

var referencedProject = sln.ReferencedProjects;
// references may have been removed so re-load it:
sln = VSSolution.FromFile(slnFilePath);
referencedProjects = sln.ReferencedProjects;

var proj = GlueState.Self.CurrentMainProject;

GeneralResponse addGeneralResponse = GeneralResponse.SuccessfulResponse;

foreach (var projectReference in projectReferences)
{
AddProjectReference(sln, referencedProject, proj, addGeneralResponse, projectReference, frbRootFolder, gumRootFolder);
AddProjectReference(sln, referencedProjects, proj, addGeneralResponse, projectReference, frbRootFolder, gumRootFolder);
}

var isFNA = GlueState.Self.CurrentMainProject is FnaDesktopProject;
if (includeGumSkia)
{
AddProjectReference(sln, referencedProject, proj, addGeneralResponse, isFNA ? GumSkiaFNA : GumSkia, frbRootFolder, gumRootFolder);
AddProjectReference(sln, referencedProjects, proj, addGeneralResponse, isFNA ? GumSkiaFNA : GumSkia, frbRootFolder, gumRootFolder);
}

if (isFNA)
{
AddProjectReference(sln, referencedProject, proj, addGeneralResponse, FNA, frbRootFolder, gumRootFolder);
AddProjectReference(sln, referencedProjects, proj, addGeneralResponse, FNA, frbRootFolder, gumRootFolder);
}

if (addGeneralResponse.Succeeded)
Expand Down Expand Up @@ -270,7 +293,7 @@ await TaskManager.Self.AddAsync(() =>
}, "Linking game to FRB Source");
}

private static List<ProjectReference> GetProjectReferencesForCurrentProject()
private static List<ProjectReference> GetNecessaryProjectReferencesForCurrentProject()
{
if (GlueState.Self.CurrentMainProject.DotNetVersion.Major >= 6) {
// When we support Android/iOS .NET 6, we need to handle those here:
Expand Down Expand Up @@ -345,7 +368,7 @@ private static bool ValidateSourceGum(string path, List<ProjectReference> projec
return true;
}

private static void AddProjectReference(VSSolution sln, List<string> referencedProject, VisualStudioProject proj,
private static void AddProjectReference(VSSolution sln, List<string> existingProjectReferences, VisualStudioProject proj,
GeneralResponse addGeneralResponse, ProjectReference reference,
string frbRootFolder, string gumRootFolder
)
Expand All @@ -360,7 +383,7 @@ private static void AddProjectReference(VSSolution sln, List<string> referencedP

if (extension == "csproj")
{
if (!AddProject(referencedProject, sln.FullFileName, fullPath))
if (!AddProject(existingProjectReferences, sln.FullFileName, fullPath))
{
addGeneralResponse.Succeeded = false;
addGeneralResponse.Message = $"Failed to add {fullPath}";
Expand All @@ -370,7 +393,7 @@ private static void AddProjectReference(VSSolution sln, List<string> referencedP
}
else if (extension == "shproj")
{
if (!AddSharedProject(referencedProject, sln.FullFileName, fullPath, reference.ProjectTypeId, reference.ProjectId, fullPath.NoPathNoExtension))
if (!AddSharedProject(existingProjectReferences, sln.FullFileName, fullPath, reference.ProjectTypeId, reference.ProjectId, fullPath.NoPathNoExtension))
{
addGeneralResponse.Succeeded = false;
addGeneralResponse.Message = $"Failed to add {fullPath}";
Expand Down

0 comments on commit ce0d908

Please sign in to comment.