Skip to content

Commit

Permalink
Storages can reference (#3782)
Browse files Browse the repository at this point in the history
* Add assembly references to storages

* auto gen code
  • Loading branch information
gabrielbeamable authored Nov 14, 2024
1 parent 0877781 commit ed28f07
Show file tree
Hide file tree
Showing 416 changed files with 1,115 additions and 845 deletions.
4 changes: 3 additions & 1 deletion cli/cli/Commands/UnityCommands/ShowManifestCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public class ManifestStorageEntry
public string beamoId;
public string csprojPath;
public bool shouldBeEnabledOnRemote;
public List<UnityAssemblyReferenceData> unityReferences;
}

public class ShowManifestCommand : AtomicCommand<ShowManifestCommandArgs, ShowManifestCommandOutput>
Expand Down Expand Up @@ -110,7 +111,8 @@ public override Task<ShowManifestCommandOutput> GetResult(ShowManifestCommandArg
{
beamoId = beamoId,
csprojPath = definition.ProjectPath,
shouldBeEnabledOnRemote = definition.ShouldBeEnabledOnRemote
shouldBeEnabledOnRemote = definition.ShouldBeEnabledOnRemote,
unityReferences = db.UnityAssemblyDefinitionProjectReferences
};
storages.Add(storage);
}
Expand Down
7 changes: 7 additions & 0 deletions cli/cli/Services/BeamoLocalSystem_EmbeddedMongoDb.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,13 @@ public class EmbeddedMongoDbLocalProtocol : IBeamoLocalProtocol
/// </summary>
public List<string> GeneralDependencyProjectPaths = new List<string> { };

/// <summary>
/// A list of paths to Unity Assembly Definition project paths.
/// These projects are auto generated by the Beamable Unity SDK when an assembly
/// definition is referenced through Project Settings.
/// </summary>
public List<UnityAssemblyReferenceData> UnityAssemblyDefinitionProjectReferences = new List<UnityAssemblyReferenceData>();


public bool VerifyCanBeBuiltLocally(ConfigService _)
{
Expand Down
18 changes: 18 additions & 0 deletions cli/cli/Services/ProjectContextUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -538,13 +538,31 @@ public static EmbeddedMongoDbLocalProtocol ConvertProjectToLocalMongoProtocol(Cs
{
if (!absPathToProject.TryGetValue(referencedProject.FullPath, out var knownProject))
{
// Check if this is a Unity Assembly reference that does not have it's csproj generated yet
if (!string.IsNullOrEmpty(referencedProject.BeamUnityAssemblyName))
{
protocol.UnityAssemblyDefinitionProjectReferences.Add(new UnityAssemblyReferenceData()
{
Path = referencedProject.RelativePath,
AssemblyName = referencedProject.BeamUnityAssemblyName
});
continue;
}

Log.Warning($"Project=[{project.relativePath}] references project=[${referencedProject.FullPath}] but that project was not detected in the beamable folder context. ");
continue;
}

var referenceType = knownProject.properties.ProjectType;
switch (referenceType)
{
case "unity":
protocol.UnityAssemblyDefinitionProjectReferences.Add(new UnityAssemblyReferenceData()
{
Path = referencedProject.RelativePath,
AssemblyName = referencedProject.BeamUnityAssemblyName
});
break;
default:
protocol.GeneralDependencyProjectPaths.Add(knownProject.relativePath);
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public bool CheckAllValidFederations()
return allValid;
}

public bool CheckAllValidAssemblies(out string validationMessage)
public static bool CheckAllValidAssemblies(List<AssemblyDefinitionAsset> assemblyReferences, out string validationMessage)
{
//Check if there is any null reference in the array
foreach (AssemblyDefinitionAsset assembly in assemblyReferences)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
using Beamable.Common;
using Beamable.Editor.BeamCli.Commands;
using Beamable.Server.Editor.Usam;
using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEditorInternal;
using UnityEngine;

namespace Beamable.Editor.Microservice.UI2.Configs
{
public class BeamableStorageSettings : ScriptableObject
{
public List<AssemblyDefinitionAsset> assemblyReferences;
private List<AssemblyDefinitionAsset> originalAssemblyReferences = new List<AssemblyDefinitionAsset>();

public BeamManifestStorageEntry storage;
public string storageName => storage.beamoId;


public static SerializedObject GetSerializedSettings(BeamManifestStorageEntry storage)
{
var instance = CreateInstance<BeamableStorageSettings>();
instance.storage = storage;

{ // update the assembly references
instance.assemblyReferences = new List<AssemblyDefinitionAsset>();
foreach (var name in storage.unityReferences)
{
var guids = AssetDatabase.FindAssets($"{name.AssemblyName} t:{nameof(AssemblyDefinitionAsset)}");
AssemblyDefinitionAsset asset = null;
foreach (var id in guids)
{
var assetPath = AssetDatabase.GUIDToAssetPath(id);
var nameQuery = $"{Path.DirectorySeparatorChar}{name.AssemblyName}.asmdef";
if (!assetPath.Contains(nameQuery))
{
continue;
}

asset = AssetDatabase.LoadAssetAtPath<AssemblyDefinitionAsset>(assetPath);
}

instance.assemblyReferences.Add(asset);
}
}
instance.UpdateOriginalData();

return new SerializedObject(instance);
}

public Promise SaveChanges(UsamService usam)
{
UpdateOriginalData();
return usam.SetStorageChanges(storageName, assemblyReferences);
}

public bool HasChanges()
{
if (originalAssemblyReferences == null || assemblyReferences == null)
{
return false;
}

var nonEmptyAssemblies = assemblyReferences;
nonEmptyAssemblies.RemoveAll(x => x==null);

if (!BeamableMicroservicesSettings.ScrambledEquals(originalAssemblyReferences, nonEmptyAssemblies))
return true;

return false;
}

private void UpdateOriginalData()
{
originalAssemblyReferences.Clear();

assemblyReferences.ForEach(asmdef => originalAssemblyReferences.Add(asmdef));

originalAssemblyReferences.RemoveAll(x => x == null);
}
}
}

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

Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ void DrawNewStorage()
{
CheckDocker("create a Storage Object", () =>
{
usam.CreateStorage(name, deps);
var _ = usam.CreateStorage(name, deps);
}, out var cancelled);
return !cancelled;
});
Expand All @@ -93,7 +93,6 @@ void DrawNewService()
var _ = usam.CreateService(name, deps);
return true;
});

}

private void DrawNewFederationId()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ namespace Beamable.Editor.Microservice.UI2
public partial class UsamWindow
{
public List<SerializedObject> serviceSettings = new List<SerializedObject>();
public List<SerializedObject> storageSettings = new List<SerializedObject>();
public Vector2 settingsScrollPosition;
[NonSerialized]
// this is a flag that we'll use to re-serialize the settings when Unity domain-reloads
Expand All @@ -37,6 +38,15 @@ void ReserializeSettings()
var settings = BeamableMicroservicesSettings.GetSerializedSettings(service);
serviceSettings.Add(settings);
}

storageSettings.Clear();
for (var i = 0; i < usam.latestManifest.storages.Count; i++)
{
var storage = usam.latestManifest.storages[i];
var settings = BeamableStorageSettings.GetSerializedSettings(storage);
storageSettings.Add(settings);
}

hasSerializedSettingsYet = true;
}

Expand Down Expand Up @@ -70,19 +80,55 @@ void DrawSettings()
}
break;
}

SerializedObject foundStorage = null;
if (foundService == null)
{
for (var i = 0; i < storageSettings.Count; i++)
{
foundStorage = storageSettings[i];
var settings = (BeamableStorageSettings)foundStorage.targetObject;
if (settings == null) continue;

if (settings.storageName != selectedBeamoId)
{
foundStorage = null;
continue;
}
break;
}
}

{ // draw a little explanation...
const string settingHelp =
const string serviceSettingHelp =
"Configure the settings for the service. ";

const string noServiceFound =
"Storage objects have no configurable settings. Please select a Microservice instead. ";
const string storageSettingHelp =
"Configure the settings for the storage. ";

const string notFoundText =
"This should never happens. Something went terrible wrong!";

string textToShow;

if (foundService != null)
{
textToShow = serviceSettingHelp;
}else if (foundStorage != null)
{
textToShow = storageSettingHelp;
}
else
{
textToShow = notFoundText;
}

EditorGUILayout.BeginVertical(new GUIStyle(EditorStyles.helpBox)
{
padding = new RectOffset(12, 12, 8, 8),
margin = new RectOffset(12, 12, 12, 12),
});
EditorGUILayout.LabelField(foundService == null ? noServiceFound : settingHelp, new GUIStyle(EditorStyles.label)
EditorGUILayout.LabelField(textToShow, new GUIStyle(EditorStyles.label)
{
padding = new RectOffset(0,0,0,0),
margin = new RectOffset(0,0,0,0),
Expand All @@ -101,6 +147,16 @@ void DrawSettings()
EditorGUILayout.EndVertical();
EditorGUILayout.EndScrollView();
}

if (foundStorage != null)
{// draw the selected storage settings

settingsScrollPosition = EditorGUILayout.BeginScrollView(settingsScrollPosition);
EditorGUILayout.BeginVertical();
DrawStorageSettings(foundStorage);
EditorGUILayout.EndVertical();
EditorGUILayout.EndScrollView();
}
}


Expand Down Expand Up @@ -215,7 +271,7 @@ void DrawServiceSettings(SerializedObject serializedObj)
{
EditorDebouncer.Debounce("save-beam-settings", () =>
{
if (!settings.CheckAllValidAssemblies(out var errorMessage))
if (!BeamableMicroservicesSettings.CheckAllValidAssemblies(settings.assemblyReferences, out var errorMessage))
{
Debug.LogError("error " + errorMessage);
}
Expand Down Expand Up @@ -245,5 +301,63 @@ void DrawServiceSettings(SerializedObject serializedObj)
}

}

void DrawStorageSettings(SerializedObject serializedObj)
{
EditorGUILayout.BeginVertical(new GUIStyle()
{
padding = new RectOffset(12, 12, 12, 12)
});


var settings = (BeamableStorageSettings)serializedObj.targetObject;
if (settings == null)
{
EditorGUILayout.LabelField("Please refresh this page.");
return;
}

{
// reference settings
EditorGUILayout.LabelField($"{settings.storageName} Settings",
new GUIStyle(EditorStyles.largeLabel));
EditorGUI.indentLevel++;
EditorGUILayout.Separator();
EditorGUILayout.Separator();

EditorGUILayout.PropertyField(
serializedObj.FindProperty(nameof(BeamableMicroservicesSettings.assemblyReferences)),
new GUIContent("Assembly Definitions"));
EditorGUILayout.Separator();
}

if (settings.HasChanges())
{
EditorDebouncer.Debounce("save-beam-settings", () =>
{
if (!BeamableMicroservicesSettings.CheckAllValidAssemblies(settings.assemblyReferences, out var errorMessage))
{
Debug.LogError("error " + errorMessage);
}
else
{
Debug.Log("auto saving settings...");
settings.SaveChanges(usam).Then(_ =>
{
Debug.Log("Saved!");
usam.WaitReload().Then(_ =>
{
// once the manifest is re-read, reserialize our own date!
hasSerializedSettingsYet = false;
});
});
}
}, .1f);
}


EditorGUILayout.EndVertical();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Beamable.Common;
using Beamable.Editor.BeamCli.Commands;
using System;
using System.Collections.Generic;
using System.IO;
Expand Down Expand Up @@ -57,15 +58,28 @@ public void Reload()
}
}

foreach (var reference in definition.unityReferences)
UpdateReferencedAssemblies(definition.unityReferences);
}

if (_usam.latestManifest.storages != null)
{
foreach (var storage in _usam.latestManifest.storages)
{
if (!_nameToAssembly.TryGetValue(reference.AssemblyName, out var assembly)) continue;
if (!CsharpProjectUtil.IsValidReference(assembly.name)) continue;
_referencedAssemblies.Add(assembly);
foreach (var subReference in GetDeeplyReferencedAssemblies(assembly))
{
_referencedAssemblies.Add(subReference);
}
UpdateReferencedAssemblies(storage.unityReferences);
}
}
}

private void UpdateReferencedAssemblies(List<BeamUnityAssemblyReferenceData> references)
{
foreach (var reference in references)
{
if (!_nameToAssembly.TryGetValue(reference.AssemblyName, out var assembly)) continue;
if (!CsharpProjectUtil.IsValidReference(assembly.name)) continue;
_referencedAssemblies.Add(assembly);
foreach (var subReference in GetDeeplyReferencedAssemblies(assembly))
{
_referencedAssemblies.Add(subReference);
}
}
}
Expand Down
Loading

0 comments on commit ed28f07

Please # to comment.