5
5
using Pixel . Automation . Core . Components . Entities ;
6
6
using Pixel . Automation . Core . Components . Processors ;
7
7
using Pixel . Automation . Core . Components . Sequences ;
8
- using Pixel . Automation . Core . Enums ;
9
8
using Pixel . Automation . Core . Interfaces ;
10
9
using Pixel . Automation . Core . Models ;
11
10
using Pixel . Automation . Editor . Core . Interfaces ;
12
11
using Pixel . Persistence . Services . Client ;
13
12
using Pixel . Persistence . Services . Client . Interfaces ;
14
13
using Pixel . Scripting . Editor . Core . Contracts ;
15
14
using Pixel . Scripting . Reference . Manager . Contracts ;
15
+ using System ;
16
16
using System . IO ;
17
17
18
18
namespace Pixel . Automation . Designer . ViewModels . AutomationBuilder
@@ -28,8 +28,24 @@ public class PrefabProjectManager : ProjectManager, IPrefabProjectManager
28
28
private readonly ApplicationSettings applicationSettings ;
29
29
private PrefabProject prefabProject ;
30
30
private PrefabVersion loadedVersion ;
31
- private Entity prefabbedEntity ;
31
+ private Entity prefabEntity ;
32
32
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>
33
49
public PrefabProjectManager ( ISerializer serializer , IEntityManager entityManager , IPrefabFileSystem prefabFileSystem , ITypeProvider typeProvider , IArgumentTypeProvider argumentTypeProvider ,
34
50
ICodeEditorFactory codeEditorFactory , IScriptEditorFactory scriptEditorFactory , IScriptEngineFactory scriptEngineFactory ,
35
51
ICodeGenerator codeGenerator , IApplicationDataManager applicationDataManager , IPrefabDataManager prefabDataManager , ApplicationSettings applicationSettings ,
@@ -46,6 +62,9 @@ public PrefabProjectManager(ISerializer serializer, IEntityManager entityManager
46
62
47
63
public async Task < Entity > Load ( PrefabProject prefabProject , VersionInfo versionToLoad )
48
64
{
65
+ Guard . Argument ( prefabProject , nameof ( prefabProject ) ) . NotNull ( ) ;
66
+ Guard . Argument ( versionToLoad , nameof ( versionToLoad ) ) . NotNull ( ) ;
67
+
49
68
this . prefabProject = prefabProject ;
50
69
this . loadedVersion = versionToLoad as PrefabVersion ;
51
70
this . prefabFileSystem . Initialize ( prefabProject , versionToLoad ) ;
@@ -65,7 +84,8 @@ public async Task<Entity> Load(PrefabProject prefabProject, VersionInfo versionT
65
84
this . entityManager . Arguments = dataModel ;
66
85
ConfigureArgumentTypeProvider ( this . entityManager . Arguments . GetType ( ) . Assembly ) ;
67
86
Initialize ( ) ;
68
- return await Task . FromResult ( this . RootEntity ) ;
87
+
88
+ return this . RootEntity ;
69
89
}
70
90
71
91
@@ -81,43 +101,28 @@ private void Initialize()
81
101
82
102
private Entity AddEntitiesAndPrefab ( )
83
103
{
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 ) ) ;
91
106
92
- if ( templateRoot != null )
107
+ if ( ! File . Exists ( this . prefabFileSystem . TemplateFile ) )
93
108
{
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 ( ) ;
103
110
templateRoot . EntityManager = entityManager ;
104
111
105
- var targetApplication = applicationDataManager . GetAllApplications ( ) . FirstOrDefault ( a => a . ApplicationId . Equals ( prefabProject . ApplicationId ) ) ;
106
-
107
- ApplicationPoolEntity appPoolEntity = new ApplicationPoolEntity ( ) ;
112
+ ApplicationPoolEntity appPoolEntity = new ApplicationPoolEntity ( ) ;
108
113
templateRoot . AddComponent ( appPoolEntity ) ;
109
114
110
115
var applicationEntity = CreateApplicationEntity ( targetApplication ) ;
111
116
appPoolEntity . AddComponent ( applicationEntity ) ;
112
117
113
118
SequentialEntityProcessor launchProcessor = new SequentialEntityProcessor ( ) { Name = "Launch Applications" } ;
114
119
templateRoot . AddComponent ( launchProcessor ) ;
115
- launchProcessor . AddComponent ( new SequenceEntity ( )
120
+ launchProcessor . AddComponent ( new SequenceEntity ( )
116
121
{
117
122
Name = $ "Sequence : { targetApplication . ApplicationName } ",
118
- TargetAppId = applicationEntity . ApplicationId
123
+ TargetAppId = applicationEntity . ApplicationId
119
124
} ) ;
120
-
125
+
121
126
122
127
SequentialEntityProcessor shutDownProcessor = new SequentialEntityProcessor ( ) { Name = "Shutdown Applications" } ;
123
128
templateRoot . AddComponent ( shutDownProcessor ) ;
@@ -127,24 +132,39 @@ private Entity AddEntitiesAndPrefab()
127
132
TargetAppId = applicationEntity . ApplicationId
128
133
} ) ;
129
134
130
- SequentialEntityProcessor prefabProcessor = new SequentialEntityProcessor ( ) { Name = "Run Prefab" } ;
135
+ SequentialEntityProcessor prefabProcessor = new SequentialEntityProcessor ( ) { Name = "Run Prefab" , Tag = "RunPrefab" } ;
131
136
templateRoot . AddComponent ( prefabProcessor ) ;
132
137
prefabProcessor . AddComponent ( new SequenceEntity ( )
133
138
{
134
- Name = $ "Sequence : { targetApplication . ApplicationName } ",
139
+ Name = $ "Initialize Data ",
135
140
TargetAppId = applicationEntity . ApplicationId
136
- } ) ;
137
-
138
- var applicationSequence = prefabProcessor . Components . First ( ) as Entity ;
139
- applicationSequence . AddComponent ( new SequenceEntity ( ) { Name = "Initialize Data" } ) ;
140
-
141
+ } ) ;
141
142
this . prefabFileSystem . CreateOrReplaceTemplate ( templateRoot ) ;
143
+ }
142
144
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 ;
146
149
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 ;
148
168
}
149
169
150
170
private ApplicationEntity CreateApplicationEntity ( ApplicationDescription targetApplication ) {
@@ -199,14 +219,13 @@ public override async Task Save()
199
219
try
200
220
{
201
221
this . RootEntity . ResetHierarchy ( ) ;
202
- serializer . Serialize ( this . prefabFileSystem . PrefabFile , this . prefabbedEntity , typeProvider . GetKnownTypes ( ) ) ;
222
+ serializer . Serialize ( this . prefabFileSystem . PrefabFile , this . prefabEntity , typeProvider . GetKnownTypes ( ) ) ;
203
223
204
- var prefabParent = this . prefabbedEntity . Parent ;
205
- prefabParent . RemoveComponent ( this . prefabbedEntity ) ;
224
+ var prefabParent = this . prefabEntity . Parent ;
225
+ prefabParent . RemoveComponent ( this . prefabEntity ) ;
206
226
this . prefabFileSystem . CreateOrReplaceTemplate ( this . RootEntity ) ;
207
- prefabParent . AddComponent ( this . prefabbedEntity ) ;
208
-
209
- //push the changes to database
227
+ prefabParent . AddComponent ( this . prefabEntity ) ;
228
+
210
229
await this . prefabDataManager . SavePrefabDataAsync ( this . prefabProject , this . loadedVersion ) ;
211
230
}
212
231
catch ( Exception ex )
@@ -215,12 +234,13 @@ public override async Task Save()
215
234
}
216
235
}
217
236
218
-
237
+ /// <inheritdoc/>
219
238
protected override string GetProjectName ( )
220
239
{
221
240
return this . prefabProject . PrefabName ;
222
241
}
223
242
243
+ /// <inheritdoc/>
224
244
protected override string GetProjectNamespace ( )
225
245
{
226
246
return this . prefabProject . Namespace ;
0 commit comments