@@ -16,6 +16,8 @@ internal class AppPlayer
16
16
17
17
internal const bool printTypeCacheTypes = false ;
18
18
19
+ private bool initialized = false ;
20
+
19
21
public AppPlayer ( string [ ] args , bool shouldConsoleLog )
20
22
{
21
23
instance = this ;
@@ -86,157 +88,170 @@ public void Create()
86
88
87
89
renderWindow . OnInit = ( ) =>
88
90
{
89
- AssetDatabase . Reload ( ) ;
90
-
91
- try
91
+ AssetDatabase . Reload ( null , ( ) =>
92
92
{
93
- var data = ResourceManager . instance . LoadFile ( "StapleAppIcon.png" ) ;
94
-
95
- if ( data != null )
93
+ try
96
94
{
97
- var rawInfo = Texture . LoadStandard ( data , StandardTextureColorComponents . RGBA ) ;
95
+ var data = ResourceManager . instance . LoadFile ( "StapleAppIcon.png" ) ;
98
96
99
- if ( rawInfo != null )
97
+ if ( data != null )
100
98
{
101
- var width = 256 ;
102
- var height = ( int ) ( rawInfo . height / ( float ) rawInfo . width * 256 ) ;
99
+ var rawInfo = Texture . LoadStandard ( data , StandardTextureColorComponents . RGBA ) ;
103
100
104
- if ( rawInfo . Resize ( width , height ) )
101
+ if ( rawInfo != null )
105
102
{
106
- renderWindow . window . SetIcon ( rawInfo ) ;
103
+ var width = 256 ;
104
+ var height = ( int ) ( rawInfo . height / ( float ) rawInfo . width * 256 ) ;
105
+
106
+ if ( rawInfo . Resize ( width , height ) )
107
+ {
108
+ renderWindow . window . SetIcon ( rawInfo ) ;
109
+ }
107
110
}
108
111
}
109
112
}
110
- }
111
- catch ( Exception )
112
- {
113
- }
113
+ catch ( Exception )
114
+ {
115
+ }
114
116
115
- Time . fixedDeltaTime = 1 / ( float ) AppSettings . Current . fixedTimeFrameRate ;
116
- Physics3D . PhysicsDeltaTime = 1 / ( float ) AppSettings . Current . physicsFrameRate ;
117
+ Time . fixedDeltaTime = 1 / ( float ) AppSettings . Current . fixedTimeFrameRate ;
118
+ Physics3D . PhysicsDeltaTime = 1 / ( float ) AppSettings . Current . physicsFrameRate ;
117
119
118
- bool hasFocus = renderWindow . window . IsFocused ;
120
+ bool hasFocus = renderWindow . window . IsFocused ;
119
121
120
- if ( AppSettings . Current . runInBackground == false && hasFocus == false )
121
- {
122
- ResetRendering ( hasFocus ) ;
123
- }
122
+ if ( AppSettings . Current . runInBackground == false && hasFocus == false )
123
+ {
124
+ ResetRendering ( hasFocus ) ;
125
+ }
124
126
125
- Scene . sceneList = ResourceManager . instance . LoadSceneList ( ) ;
127
+ Scene . sceneList = ResourceManager . instance . LoadSceneList ( ) ;
126
128
127
- if ( Scene . sceneList == null || Scene . sceneList . Count == 0 )
128
- {
129
- Log . Error ( $ "Failed to load scene list") ;
129
+ if ( Scene . sceneList == null || Scene . sceneList . Count == 0 )
130
+ {
131
+ Log . Error ( $ "Failed to load scene list") ;
130
132
131
- renderWindow . shouldStop = true ;
133
+ renderWindow . shouldStop = true ;
132
134
133
- throw new Exception ( "Failed to load scene list" ) ;
134
- }
135
+ throw new Exception ( "Failed to load scene list" ) ;
136
+ }
135
137
136
- Log . Info ( "Loaded scene list" ) ;
138
+ Log . Info ( "Loaded scene list" ) ;
137
139
138
- if ( Physics3D . ImplType != null && Physics3D . ImplType . IsAssignableTo ( typeof ( IPhysics3D ) ) == false )
139
- {
140
- Log . Error ( $ "Failed to initialize physics: { Physics3D . ImplType . FullName } doesn't implement IPhysics3D") ;
140
+ if ( Physics3D . ImplType != null && Physics3D . ImplType . IsAssignableTo ( typeof ( IPhysics3D ) ) == false )
141
+ {
142
+ Log . Error ( $ "Failed to initialize physics: { Physics3D . ImplType . FullName } doesn't implement IPhysics3D") ;
141
143
142
- renderWindow . shouldStop = true ;
144
+ renderWindow . shouldStop = true ;
143
145
144
- throw new Exception ( "Failed to initialize physics" ) ;
145
- }
146
+ throw new Exception ( "Failed to initialize physics" ) ;
147
+ }
146
148
147
- var physicsInstance = Physics3D . ImplType != null ? ObjectCreation . CreateObject < IPhysics3D > ( Physics3D . ImplType ) : null ;
149
+ var physicsInstance = Physics3D . ImplType != null ? ObjectCreation . CreateObject < IPhysics3D > ( Physics3D . ImplType ) : null ;
148
150
149
- try
150
- {
151
- Physics3D . Instance = new Physics3D ( physicsInstance ) ;
152
- }
153
- catch ( Exception e )
154
- {
155
- Log . Error ( e . ToString ( ) ) ;
151
+ try
152
+ {
153
+ Physics3D . Instance = new Physics3D ( physicsInstance ) ;
154
+ }
155
+ catch ( Exception e )
156
+ {
157
+ Log . Error ( e . ToString ( ) ) ;
156
158
157
- renderWindow . shouldStop = true ;
159
+ renderWindow . shouldStop = true ;
158
160
159
- throw new Exception ( "Failed to initialize physics" ) ;
160
- }
161
+ throw new Exception ( "Failed to initialize physics" ) ;
162
+ }
161
163
162
- SubsystemManager . instance . RegisterSubsystem ( RenderSystem . Instance , RenderSystem . Priority ) ;
163
- SubsystemManager . instance . RegisterSubsystem ( EntitySystemManager . Instance , EntitySystemManager . Priority ) ;
164
- SubsystemManager . instance . RegisterSubsystem ( AudioSystem . Instance , AudioSystem . Priority ) ;
164
+ SubsystemManager . instance . RegisterSubsystem ( RenderSystem . Instance , RenderSystem . Priority ) ;
165
+ SubsystemManager . instance . RegisterSubsystem ( EntitySystemManager . Instance , EntitySystemManager . Priority ) ;
166
+ SubsystemManager . instance . RegisterSubsystem ( AudioSystem . Instance , AudioSystem . Priority ) ;
165
167
166
- if ( Physics3D . Instance != null )
167
- {
168
- SubsystemManager . instance . RegisterSubsystem ( Physics3D . Instance , Physics3D . Priority ) ;
169
- }
168
+ if ( Physics3D . Instance != null )
169
+ {
170
+ SubsystemManager . instance . RegisterSubsystem ( Physics3D . Instance , Physics3D . Priority ) ;
171
+ }
170
172
171
- void HandleTypes < T > ( string caption , Func < Type , bool > check , Action < T > callback )
172
- {
173
- var types = TypeCache . AllTypes ( )
174
- . Where ( x => check ( x ) )
175
- . ToArray ( ) ;
173
+ void HandleTypes < T > ( string caption , Func < Type , bool > check , Action < T > callback )
174
+ {
175
+ var types = TypeCache . AllTypes ( )
176
+ . Where ( x => check ( x ) )
177
+ . ToArray ( ) ;
176
178
177
- Log . Info ( $ "Loading { types . Length } { caption } s") ;
179
+ Log . Info ( $ "Loading { types . Length } { caption } s") ;
178
180
179
- foreach ( var type in types )
180
- {
181
- try
181
+ foreach ( var type in types )
182
182
{
183
- var instance = ( T ) Activator . CreateInstance ( type ) ;
184
-
185
- if ( instance != null )
183
+ try
186
184
{
187
- callback ( instance ) ;
188
-
189
- Log . Info ( $ "Created { caption } { type . FullName } ") ;
185
+ var instance = ( T ) Activator . CreateInstance ( type ) ;
186
+
187
+ if ( instance != null )
188
+ {
189
+ callback ( instance ) ;
190
+
191
+ Log . Info ( $ "Created { caption } { type . FullName } ") ;
192
+ }
193
+ else
194
+ {
195
+ Log . Info ( $ "Failed to create { caption } { type . FullName } ") ;
196
+ }
190
197
}
191
- else
198
+ catch ( Exception e )
192
199
{
193
- Log . Info ( $ "Failed to create { caption } { type . FullName } ") ;
200
+ Log . Warning ( $ "Player: Failed to load { caption } { type . FullName } : { e } ") ;
194
201
}
195
202
}
196
- catch ( Exception e )
197
- {
198
- Log . Warning ( $ "Player: Failed to load { caption } { type . FullName } : { e } ") ;
199
- }
200
203
}
201
- }
202
204
203
- HandleTypes < IRenderSystem > ( "render system" ,
204
- ( x => typeof ( IRenderSystem ) . IsAssignableFrom ( x ) && x != typeof ( IRenderSystem ) ) ,
205
- ( instance => RenderSystem . Instance . RegisterSystem ( instance ) ) ) ;
205
+ HandleTypes < IRenderSystem > ( "render system" ,
206
+ ( x => typeof ( IRenderSystem ) . IsAssignableFrom ( x ) && x != typeof ( IRenderSystem ) ) ,
207
+ ( instance => RenderSystem . Instance . RegisterSystem ( instance ) ) ) ;
206
208
207
- HandleTypes < object > ( "entity system" ,
208
- ( x => ( typeof ( IEntitySystemUpdate ) . IsAssignableFrom ( x ) && x != typeof ( IEntitySystemUpdate ) ) ||
209
- ( typeof ( IEntitySystemFixedUpdate ) . IsAssignableFrom ( x ) && x != typeof ( IEntitySystemFixedUpdate ) ) ) ,
210
- ( instance => EntitySystemManager . Instance . RegisterSystem ( instance ) ) ) ;
209
+ HandleTypes < object > ( "entity system" ,
210
+ ( x => ( typeof ( IEntitySystemUpdate ) . IsAssignableFrom ( x ) && x != typeof ( IEntitySystemUpdate ) ) ||
211
+ ( typeof ( IEntitySystemFixedUpdate ) . IsAssignableFrom ( x ) && x != typeof ( IEntitySystemFixedUpdate ) ) ) ,
212
+ ( instance => EntitySystemManager . Instance . RegisterSystem ( instance ) ) ) ;
211
213
212
- var scene = ResourceManager . instance . LoadScene ( Scene . sceneList [ 0 ] ) ;
214
+ var scene = ResourceManager . instance . LoadScene ( Scene . sceneList [ 0 ] ) ;
213
215
214
- if ( scene == null )
215
- {
216
- Log . Error ( $ "Failed to load main scene") ;
216
+ if ( scene == null )
217
+ {
218
+ Log . Error ( $ "Failed to load main scene") ;
217
219
218
- renderWindow . shouldStop = true ;
220
+ renderWindow . shouldStop = true ;
219
221
220
- throw new Exception ( "Failed to load main scene" ) ;
221
- }
222
+ throw new Exception ( "Failed to load main scene" ) ;
223
+ }
224
+
225
+ Scene . SetActiveScene ( scene ) ;
222
226
223
- Scene . SetActiveScene ( scene ) ;
227
+ Log . Info ( "Loaded first scene" ) ;
224
228
225
- Log . Info ( "Loaded first scene " ) ;
229
+ Log . Info ( "Finished initializing " ) ;
226
230
227
- Log . Info ( "Finished initializing" ) ;
231
+ initialized = true ;
232
+ } ) ;
228
233
} ;
229
234
230
235
renderWindow . OnFixedUpdate = ( ) =>
231
236
{
237
+ if ( initialized == false )
238
+ {
239
+ return ;
240
+ }
241
+
232
242
SubsystemManager . instance . Update ( SubsystemType . FixedUpdate ) ;
233
243
234
244
EntitySystemManager . Instance . UpdateFixed ( ) ;
235
245
} ;
236
246
237
247
renderWindow . OnUpdate = ( ) =>
238
248
{
239
- if ( ( AppSettings . Current ? . allowFullscreenSwitch ?? true ) && Input . GetKey ( KeyCode . LeftAlt ) && Input . GetKeyDown ( KeyCode . Enter ) )
249
+ if ( initialized == false )
250
+ {
251
+ return ;
252
+ }
253
+
254
+ if ( ( AppSettings . Current ? . allowFullscreenSwitch ?? true ) && Input . GetKey ( KeyCode . LeftAlt ) && Input . GetKeyDown ( KeyCode . Enter ) )
240
255
{
241
256
Screen . SetResolution ( Screen . Width , Screen . Height , Screen . WindowMode == WindowMode . Windowed ? WindowMode . BorderlessFullscreen : WindowMode . Windowed ) ;
242
257
}
0 commit comments