Skip to content

Commit 5fb69ad

Browse files
authored
Merge pull request #177 from Nfactor26/prefab-in-sequence-entity
Prefab should always be added to a SequenceEntity when editing a Prefab
2 parents c191aae + 36a3f88 commit 5fb69ad

File tree

1 file changed

+64
-44
lines changed

1 file changed

+64
-44
lines changed

src/Pixel.Automation.Designer.ViewModels/AutomationBuilder/PrefabProjectManager.cs

Lines changed: 64 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@
55
using Pixel.Automation.Core.Components.Entities;
66
using Pixel.Automation.Core.Components.Processors;
77
using Pixel.Automation.Core.Components.Sequences;
8-
using Pixel.Automation.Core.Enums;
98
using Pixel.Automation.Core.Interfaces;
109
using Pixel.Automation.Core.Models;
1110
using Pixel.Automation.Editor.Core.Interfaces;
1211
using Pixel.Persistence.Services.Client;
1312
using Pixel.Persistence.Services.Client.Interfaces;
1413
using Pixel.Scripting.Editor.Core.Contracts;
1514
using Pixel.Scripting.Reference.Manager.Contracts;
15+
using System;
1616
using System.IO;
1717

1818
namespace Pixel.Automation.Designer.ViewModels.AutomationBuilder
@@ -28,8 +28,24 @@ public class PrefabProjectManager : ProjectManager, IPrefabProjectManager
2828
private readonly ApplicationSettings applicationSettings;
2929
private PrefabProject prefabProject;
3030
private PrefabVersion loadedVersion;
31-
private Entity prefabbedEntity;
31+
private Entity prefabEntity;
3232

33+
/// <summary>
34+
/// constructor
35+
/// </summary>
36+
/// <param name="serializer">Serializer</param>
37+
/// <param name="entityManager">EntityManager</param>
38+
/// <param name="prefabFileSystem">Prefab file system</param>
39+
/// <param name="typeProvider">Known Type provider</param>
40+
/// <param name="argumentTypeProvider">Argument type provider</param>
41+
/// <param name="codeEditorFactory">Factory for creading code editorss</param>
42+
/// <param name="scriptEditorFactory">Factory for creating script editors</param>
43+
/// <param name="scriptEngineFactory">Factory for creating script enginess</param>
44+
/// <param name="codeGenerator">Code generator for generating initial data model code</param>
45+
/// <param name="applicationDataManager">Data manager for application data</param>
46+
/// <param name="prefabDataManager">Data manager for prefab data</param>
47+
/// <param name="applicationSettings">Application settings</param>
48+
/// <param name="referenceManagerFactory">Factory for creating Reference Manager</param>
3349
public PrefabProjectManager(ISerializer serializer, IEntityManager entityManager, IPrefabFileSystem prefabFileSystem, ITypeProvider typeProvider, IArgumentTypeProvider argumentTypeProvider,
3450
ICodeEditorFactory codeEditorFactory, IScriptEditorFactory scriptEditorFactory, IScriptEngineFactory scriptEngineFactory,
3551
ICodeGenerator codeGenerator, IApplicationDataManager applicationDataManager, IPrefabDataManager prefabDataManager, ApplicationSettings applicationSettings,
@@ -46,6 +62,9 @@ public PrefabProjectManager(ISerializer serializer, IEntityManager entityManager
4662

4763
public async Task<Entity> Load(PrefabProject prefabProject, VersionInfo versionToLoad)
4864
{
65+
Guard.Argument(prefabProject, nameof(prefabProject)).NotNull();
66+
Guard.Argument(versionToLoad, nameof(versionToLoad)).NotNull();
67+
4968
this.prefabProject = prefabProject;
5069
this.loadedVersion = versionToLoad as PrefabVersion;
5170
this.prefabFileSystem.Initialize(prefabProject, versionToLoad);
@@ -65,7 +84,8 @@ public async Task<Entity> Load(PrefabProject prefabProject, VersionInfo versionT
6584
this.entityManager.Arguments = dataModel;
6685
ConfigureArgumentTypeProvider(this.entityManager.Arguments.GetType().Assembly);
6786
Initialize();
68-
return await Task.FromResult(this.RootEntity);
87+
88+
return this.RootEntity;
6989
}
7090

7191

@@ -81,43 +101,28 @@ private void Initialize()
81101

82102
private Entity AddEntitiesAndPrefab()
83103
{
84-
this.prefabbedEntity = this.Load<Entity>(this.prefabFileSystem.PrefabFile);
85-
Entity templateRoot = default;
86-
87-
if(File.Exists(this.prefabFileSystem.TemplateFile))
88-
{
89-
templateRoot = this.Load<Entity>(this.prefabFileSystem.TemplateFile); ;
90-
}
104+
this.prefabEntity = this.Load<Entity>(this.prefabFileSystem.PrefabFile);
105+
var targetApplication = applicationDataManager.GetAllApplications().FirstOrDefault(a => a.ApplicationId.Equals(prefabProject.ApplicationId));
91106

92-
if (templateRoot != null)
107+
if (!File.Exists(this.prefabFileSystem.TemplateFile))
93108
{
94-
templateRoot.EntityManager = this.entityManager;
95-
RestoreParentChildRelation(templateRoot);
96-
Entity prefabPlaceHolder = templateRoot.GetComponentsOfType<SequentialEntityProcessor>(SearchScope.Children).Last();
97-
prefabPlaceHolder.AddComponent(this.prefabbedEntity);
98-
RestoreParentChildRelation(prefabPlaceHolder);
99-
}
100-
else
101-
{
102-
templateRoot = new ProcessRootEntity();
109+
var templateRoot = new ProcessRootEntity();
103110
templateRoot.EntityManager = entityManager;
104111

105-
var targetApplication = applicationDataManager.GetAllApplications().FirstOrDefault(a => a.ApplicationId.Equals(prefabProject.ApplicationId));
106-
107-
ApplicationPoolEntity appPoolEntity = new ApplicationPoolEntity();
112+
ApplicationPoolEntity appPoolEntity = new ApplicationPoolEntity();
108113
templateRoot.AddComponent(appPoolEntity);
109114

110115
var applicationEntity = CreateApplicationEntity(targetApplication);
111116
appPoolEntity.AddComponent(applicationEntity);
112117

113118
SequentialEntityProcessor launchProcessor = new SequentialEntityProcessor() { Name = "Launch Applications" };
114119
templateRoot.AddComponent(launchProcessor);
115-
launchProcessor.AddComponent(new SequenceEntity()
120+
launchProcessor.AddComponent(new SequenceEntity()
116121
{
117122
Name = $"Sequence : {targetApplication.ApplicationName}",
118-
TargetAppId = applicationEntity.ApplicationId
123+
TargetAppId = applicationEntity.ApplicationId
119124
});
120-
125+
121126

122127
SequentialEntityProcessor shutDownProcessor = new SequentialEntityProcessor() { Name = "Shutdown Applications" };
123128
templateRoot.AddComponent(shutDownProcessor);
@@ -127,24 +132,39 @@ private Entity AddEntitiesAndPrefab()
127132
TargetAppId = applicationEntity.ApplicationId
128133
});
129134

130-
SequentialEntityProcessor prefabProcessor = new SequentialEntityProcessor() { Name = "Run Prefab" };
135+
SequentialEntityProcessor prefabProcessor = new SequentialEntityProcessor() { Name = "Run Prefab", Tag = "RunPrefab" };
131136
templateRoot.AddComponent(prefabProcessor);
132137
prefabProcessor.AddComponent(new SequenceEntity()
133138
{
134-
Name = $"Sequence : {targetApplication.ApplicationName}",
139+
Name = $"Initialize Data",
135140
TargetAppId = applicationEntity.ApplicationId
136-
});
137-
138-
var applicationSequence = prefabProcessor.Components.First() as Entity;
139-
applicationSequence.AddComponent(new SequenceEntity() { Name = "Initialize Data" });
140-
141+
});
141142
this.prefabFileSystem.CreateOrReplaceTemplate(templateRoot);
143+
}
142144

143-
applicationSequence.AddComponent(prefabbedEntity);
144-
RestoreParentChildRelation(templateRoot);
145-
}
145+
var root = this.Load<Entity>(this.prefabFileSystem.TemplateFile);
146+
root.EntityManager = this.entityManager;
147+
RestoreParentChildRelation(root);
148+
var runPrefabProcessor = root.GetComponentsByTag("RunPrefab").Single() as Entity;
146149

147-
return templateRoot;
150+
//In order to run prefab, it must belong to a SequenceEntity. We can create a prefab from any entity.
151+
//Hence, we check if the prefab root is not a SequenceEntity, we create a placeholder SequenceEntity first and add prefab entity to it.
152+
if(this.prefabEntity is SequenceEntity)
153+
{
154+
runPrefabProcessor.AddComponent(this.prefabEntity);
155+
}
156+
else
157+
{
158+
var applicationSequence = new SequenceEntity()
159+
{
160+
Name = $"Initialize Data",
161+
TargetAppId = targetApplication.ApplicationId
162+
};
163+
runPrefabProcessor.AddComponent(applicationSequence);
164+
applicationSequence.AddComponent(this.prefabEntity);
165+
}
166+
RestoreParentChildRelation(runPrefabProcessor);
167+
return root;
148168
}
149169

150170
private ApplicationEntity CreateApplicationEntity(ApplicationDescription targetApplication) {
@@ -199,14 +219,13 @@ public override async Task Save()
199219
try
200220
{
201221
this.RootEntity.ResetHierarchy();
202-
serializer.Serialize(this.prefabFileSystem.PrefabFile, this.prefabbedEntity, typeProvider.GetKnownTypes());
222+
serializer.Serialize(this.prefabFileSystem.PrefabFile, this.prefabEntity, typeProvider.GetKnownTypes());
203223

204-
var prefabParent = this.prefabbedEntity.Parent;
205-
prefabParent.RemoveComponent(this.prefabbedEntity);
224+
var prefabParent = this.prefabEntity.Parent;
225+
prefabParent.RemoveComponent(this.prefabEntity);
206226
this.prefabFileSystem.CreateOrReplaceTemplate(this.RootEntity);
207-
prefabParent.AddComponent(this.prefabbedEntity);
208-
209-
//push the changes to database
227+
prefabParent.AddComponent(this.prefabEntity);
228+
210229
await this.prefabDataManager.SavePrefabDataAsync(this.prefabProject, this.loadedVersion);
211230
}
212231
catch (Exception ex)
@@ -215,12 +234,13 @@ public override async Task Save()
215234
}
216235
}
217236

218-
237+
/// <inheritdoc/>
219238
protected override string GetProjectName()
220239
{
221240
return this.prefabProject.PrefabName;
222241
}
223242

243+
/// <inheritdoc/>
224244
protected override string GetProjectNamespace()
225245
{
226246
return this.prefabProject.Namespace;

0 commit comments

Comments
 (0)