@@ -23,6 +23,11 @@ public partial class LDtkWorld
2323 /// </summary>
2424 public string RootFolder { get ; set ; }
2525
26+ /// <summary>
27+ /// The content manager used if you are using the contentpipeline
28+ /// </summary>
29+ public ContentManager Content { get ; set ; }
30+
2631 /// <summary>
2732 /// Loads the ldtk world file from disk directly
2833 /// </summary>
@@ -44,16 +49,12 @@ public static LDtkWorld LoadWorld(string filePath)
4449 /// <returns>LDtkWorld</returns>
4550 public static LDtkWorld LoadWorld ( string filePath , ContentManager content )
4651 {
47- if ( content != null )
48- {
49- LDtkWorld world ;
50- world = content . Load < LDtkWorld > ( filePath ) ;
51- world . RootFolder = Path . GetDirectoryName ( filePath ) ;
52+ LDtkWorld world ;
53+ world = content . Load < LDtkWorld > ( filePath ) ;
54+ world . Content = content ;
55+ world . RootFolder = Path . GetDirectoryName ( filePath ) ;
5256
53- return world ;
54- }
55-
56- throw new ContentLoadException ( $ "Could not load ldtk world at { filePath } .") ;
57+ return world ;
5758 }
5859
5960 /// <summary>
@@ -81,7 +82,7 @@ public LDtkLevel LoadLevel(string identifier)
8182
8283 string path = Path . Join ( RootFolder , Levels [ i ] . ExternalRelPath ) ;
8384
84- level = JsonSerializer . Deserialize < LDtkLevel > ( File . ReadAllText ( path ) , SerializeOptions ) ;
85+ level = Content . Load < LDtkLevel > ( path . Replace ( ".ldtkl" , "" ) ) ?? JsonSerializer . Deserialize < LDtkLevel > ( File . ReadAllText ( path ) , SerializeOptions ) ;
8586 break ;
8687 }
8788
@@ -94,29 +95,6 @@ public LDtkLevel LoadLevel(string identifier)
9495 throw new LevelNotFoundException ( $ "Could not find { identifier } Level in { this } .") ;
9596 }
9697
97- /// <summary>
98- /// Gets a collection of entities of type <typeparamref name="T"/> in the current level
99- /// </summary>
100- /// <returns></returns>
101- /// <exception cref="EntityNotFoundException"></exception>
102- public T GetEntity < T > ( ) where T : new ( )
103- {
104- List < T > entities = new List < T > ( ) ;
105- for ( int i = 0 ; i < Levels . Length ; i ++ )
106- {
107- entities . AddRange ( Levels [ i ] . GetEntities < T > ( ) ) ;
108- }
109-
110- if ( entities . Count == 1 )
111- {
112- return entities [ 0 ] ;
113- }
114- else
115- {
116- throw new EntityNotFoundException ( $ "Could not find entity with identifier { typeof ( T ) . Name } ") ;
117- }
118- }
119-
12098 /// <summary>
12199 /// Loads the ldtkl world file from disk directly or from the embeded one depending on if externalLevels is set
122100 /// </summary>
@@ -142,45 +120,7 @@ public LDtkLevel LoadLevel(int uid)
142120
143121 string path = Path . Join ( RootFolder , Levels [ i ] . ExternalRelPath ) ;
144122
145- level = JsonSerializer . Deserialize < LDtkLevel > ( File . ReadAllText ( path ) , SerializeOptions ) ;
146- break ;
147- }
148-
149- if ( level != null )
150- {
151- level . Parent = this ;
152- return level ;
153- }
154-
155- throw new LevelNotFoundException ( $ "Could not find { uid } Level in { this } .") ;
156- }
157-
158- /// <summary>
159- /// Loads the ldtkl world file from disk directly or from the embeded one depending on if externalLevels is set
160- /// </summary>
161- /// <param name="uid">The Levels uid</param>
162- /// <param name="content">Content Pipeline</param>
163- /// <returns><see cref="LDtkLevel"/></returns>
164- /// <exception cref="LevelNotFoundException"></exception>
165- public LDtkLevel LoadLevel ( int uid , ContentManager content )
166- {
167- LDtkLevel level = null ;
168-
169- for ( int i = 0 ; i < Levels . Length ; i ++ )
170- {
171- if ( Levels [ i ] . Uid != uid )
172- {
173- continue ;
174- }
175-
176- if ( ExternalLevels == false )
177- {
178- level = Levels [ i ] ;
179- break ;
180- }
181-
182- string path = Path . Join ( RootFolder , Levels [ i ] . ExternalRelPath ) ;
183- level = content . Load < LDtkLevel > ( path . Replace ( ".ldtkl" , "" ) ) ;
123+ level = Content . Load < LDtkLevel > ( path . Replace ( ".ldtkl" , "" ) ) ?? JsonSerializer . Deserialize < LDtkLevel > ( File . ReadAllText ( path ) , SerializeOptions ) ;
184124 break ;
185125 }
186126
@@ -212,43 +152,6 @@ public EntityDefinition GetEntityDefinitionFromUid(int uid)
212152 return null ;
213153 }
214154
215- /// <summary>
216- /// Loads the ldtkl world file from disk directly or from the embeded one depending on if externalLevels is set
217- /// </summary>
218- /// <param name="identifier">The Level identifier</param>
219- /// <param name="content">The optional XNA content manager if you are using the content pipeline</param>
220- /// <returns>LDtkWorld</returns>
221- public LDtkLevel LoadLevel ( string identifier , ContentManager content )
222- {
223- LDtkLevel level = null ;
224-
225- for ( int i = 0 ; i < Levels . Length ; i ++ )
226- {
227- if ( Levels [ i ] . Identifier != identifier )
228- {
229- continue ;
230- }
231-
232- if ( ExternalLevels == false )
233- {
234- level = Levels [ i ] ;
235- break ;
236- }
237-
238- string path = Path . Join ( RootFolder , Levels [ i ] . ExternalRelPath ) ;
239- level = content . Load < LDtkLevel > ( path . Replace ( ".ldtkl" , "" ) ) ;
240- break ;
241- }
242-
243- if ( level != null )
244- {
245- level . Parent = this ;
246- return level ;
247- }
248-
249- throw new LevelNotFoundException ( $ "Could not Content.Load `{ identifier } ` in { this } .") ;
250- }
251-
252155 /// <summary>
253156 /// Gets the intgrid value definitions
254157 /// </summary>
@@ -272,4 +175,27 @@ public IntGridValueDefinition[] GetIntgridValueDefinitions(string identifier)
272175
273176 throw new FieldNotFoundException ( ) ;
274177 }
178+
179+ /// <summary>
180+ /// Gets a collection of entities of type <typeparamref name="T"/> in the current level
181+ /// </summary>
182+ /// <returns></returns>
183+ /// <exception cref="EntityNotFoundException"></exception>
184+ public T GetEntity < T > ( ) where T : new ( )
185+ {
186+ List < T > entities = new List < T > ( ) ;
187+ for ( int i = 0 ; i < Levels . Length ; i ++ )
188+ {
189+ entities . AddRange ( Levels [ i ] . GetEntities < T > ( ) ) ;
190+ }
191+
192+ if ( entities . Count == 1 )
193+ {
194+ return entities [ 0 ] ;
195+ }
196+ else
197+ {
198+ throw new EntityNotFoundException ( $ "Could not find entity with identifier { typeof ( T ) . Name } ") ;
199+ }
200+ }
275201}
0 commit comments