浏览代码

mojo3d jsonifier WIP.

Mark Sibly 7 年之前
父节点
当前提交
90a944adf0

+ 16 - 6
modules/mojo3d/scene/entities/model.monkey2

@@ -121,19 +121,29 @@ Class Model Extends Renderable
 	
 	#end
 	Function Load:Model( path:String )
-	
-		For Local loader:=Eachin Mojo3dLoader.Instances
 		
-			Local model:=loader.LoadModel( path )
+		Local scene:=mojo3d.Scene.GetCurrent()
+		
+		Local editing:=scene.Editing
+		
+		scene.Editing=False
+		
+		Local model:Model
+		
+		For Local loader:=Eachin Mojo3dLoader.Instances
+			
+			model=loader.LoadModel( path )
 			
 			If Not model Continue
 			
-			If model.Scene.Editing model.Scene.Jsonifier.AddInstance( model,"mojo3d.Model.Load",New Variant[]( path )  )
+			If editing scene.Jsonifier.AddInstance( model,"mojo3d.Model.Load",New Variant[]( path )  )
 				
-			Return model
+			Exit
 		Next
+
+		scene.Editing=editing
 		
-		Return Null
+		Return model
 	End
 	
 	#rem monkeydoc Loads a boned model from a file path.

+ 32 - 19
modules/mojo3d/scene/jsonifier/jsonifier.monkey2

@@ -1,7 +1,7 @@
 Namespace mojo3d.jsonifier
 
 Class Jsonifier
-	
+
 	Method AddInstance( obj:Object,ctor:Invocation )
 		
 		Assert( Not _instsByObj.Contains( obj ) )
@@ -36,28 +36,22 @@ Class Jsonifier
 		AddInstance( obj,New Invocation( decl,Null,args ) )
 	End
 
-	#rem
-	'function/method call	
-	Method AddInstance( obj:Object,name:String,args:Variant[] )
-		
-		AddInstance( obj,New Invocation( name,args ) )
-	End
-	
-	#end
-	
 	Method JsonifyInstances:JsonObject()
 		
 		Local jobj:=New JsonObject
 		
+		jobj["assetsDir"]=New JsonString( AssetsDir() )
+		
 		Local jinsts:=New JsonArray( _insts.Length )
 		
-		For Local i:=0 Until jinsts.Length
+		For Local i:=0 Until _insts.Length
 			
 			Local inst:=_insts[i]
 			Local jobj:=New JsonObject
 			
 			jobj["id"]=New JsonString( inst.id )
 			jobj["type"]=New JsonString( inst.obj.DynamicType.Name )
+			
 			jobj["ctor"]=Jsonify( inst.ctor )
 			
 			Local state:=JsonifyState( inst.obj ),dstate:=New JsonObject
@@ -71,7 +65,7 @@ Class Jsonifier
 			Next
 			
 			jobj["state"]=dstate
-			
+				
 			jinsts[i]=jobj
 		Next
 
@@ -82,6 +76,10 @@ Class Jsonifier
 	
 	Method DejsonifyInstances( jobj:JsonObject )
 		
+		Local assetsDir:=AssetsDir()
+		
+		If jobj.Contains( "assetsDir" ) SetAssetsDir( jobj.GetString( "assetsDir" ) )
+		
 		Local jinsts:=jobj.GetArray( "instances" )
 		
 		For Local i:=0 Until jinsts.Length
@@ -95,15 +93,17 @@ Class Jsonifier
 				obj=_insts[i].obj
 				
 			Else
+				
 				Local ctor:=Cast<Invocation>( Dejsonify( jobj["ctor"],Typeof<Invocation> ) )
 			
 				obj=Cast<Object>( ctor.Execute() )
+				
 			Endif
 			
 			_dejsonified.Add( obj )
 			
 			'set value type state only on this pass.
-			DejsonifyState( obj,jobj.GetObject( "state" ),obj.DynamicType,False )
+			If jobj.Contains( "state" ) DejsonifyState( obj,jobj.GetObject( "state" ),obj.DynamicType,False )
 		Next
 
 		'set reference type state - do this on a second pass 'coz of forward refs. Probably wont always work?
@@ -113,8 +113,10 @@ Class Jsonifier
 			
 			Local obj:=_dejsonified[i]
 			
-			DejsonifyState( obj,jobj.GetObject( "state" ),obj.DynamicType,True )
+			If jobj.Contains( "state" ) DejsonifyState( obj,jobj.GetObject( "state" ),obj.DynamicType,True )
 		Next
+		
+		SetAssetsDir( assetsDir )
 	End
 	
 	Method Jsonify:JsonValue( value:Variant )
@@ -162,6 +164,11 @@ Class Jsonifier
 			Local jvalue:=jext.Jsonify( value,Self )
 			If jvalue Return jvalue
 		Next
+
+		Select type.Kind
+		Case "Class"
+			Return JsonValue.NullValue
+		End
 		
 		RuntimeError( "TODO: No jsonifier found for type '"+type+"'" )
 		Return Null
@@ -211,8 +218,14 @@ Class Jsonifier
 			Local value:=jext.Dejsonify( jvalue,type,Self )
 			If value Return value
 		Next
+
+		Select type.Kind
+		Case "Class"
+			Return type.NullValue
+		End
 		
 		RuntimeError( "No dejsonifier found for type '"+type+"'" )
+		
 		Return Null
 	End
 	
@@ -283,11 +296,11 @@ Class Jsonifier
 		
 	End
 
-	Method DejsonifyState( obj:Object,jobj:JsonObject,type:TypeInfo,refs:Bool )
+	Method DejsonifyState( obj:Object,jobj:JsonObject,type:TypeInfo,insts:Bool )
 		
 		If type.Kind<>"Class" Return
 		
-		If type.SuperType DejsonifyState( obj,jobj,type.SuperType,refs )
+		If type.SuperType DejsonifyState( obj,jobj,type.SuperType,insts )
 
 		For Local d:=Eachin type.GetDecls()
 			
@@ -299,11 +312,11 @@ Class Jsonifier
 			
 			Local type:=d.Type
 			
-			Local isref:=type.Kind="Class"
+			Local isinst:=type.Kind="Class"
 			
-			If Not isref And type.Kind="Array" And type.ElementType.Kind="Class" isref=True
+			If Not isinst And type.Kind="Array" And type.ElementType.Kind="Class" isinst=True
 				
-			If isref<>refs Continue
+			If isinst<>insts Continue
 			
 			d.Set( obj,Dejsonify( jobj.GetValue( d.Name ),d.Type ) )
 		Next

+ 0 - 11
modules/mojo3d/scene/jsonifier/jsonifierexts.monkey2

@@ -112,9 +112,6 @@ Class InvocationJsonifierExt Extends JsonifierExt
 			
 			Local jobj:=Cast<JsonObject>( jvalue )
 			
-'			Local scope:=TypeInfo.GetType( jobj.GetString( "scope" ) )
-'			Local dname:=jobj.GetString( "decl" )
-
 			Local dname:=jobj.GetString( "decl" )
 			Local dtype:=jobj.GetString( "type" )
 			Local jinst:=jobj.Contains( "inst" ) ? jobj.GetValue( "inst" ) Else JsonValue.NullValue
@@ -166,7 +163,6 @@ Class InvocationJsonifierExt Extends JsonifierExt
 End
 
 'need to fix array handling!
- 
 Class Mojo3dJsonifierExt Extends JsonifierExt
 	
 	Const instance:=New Mojo3dJsonifierExt
@@ -192,10 +188,3 @@ Class Mojo3dJsonifierExt Extends JsonifierExt
 	End
 
 End	
-	
-			
-			
-	
-	
-
-