Esempio n. 1
0
 protected virtual void ReplaceScript(ScriptComponent scriptComponent, ReloadedScriptEntry reloadedScript)
 {
     // TODO: Let PropertyGrid know that we updated the script
     scriptComponent.Scripts[reloadedScript.ScriptIndex] = DeserializeScript(reloadedScript);
 }
        protected override void TransformObjectAfterRead(ref ObjectContext objectContext)
        {
            if (isSerializingAsReference)
            {
                // Transform the deserialized reference into a fake Entity, EntityComponent, etc...
                // Fake objects will later be fixed later with EntityAnalysis.FixupEntityReferences()
                if (!objectContext.SerializerContext.IsSerializing)
                {
                    var entityComponentReference = objectContext.Instance as EntityComponentReference;
                    var entityScriptReference = objectContext.Instance as EntityScriptReference;
                    if (entityComponentReference != null)
                    {
                        var entityReference = new Entity { Id = entityComponentReference.Entity.Id };
                        var entityComponent = (EntityComponent)Activator.CreateInstance(entityComponentReference.ComponentType);
                        entityComponent.Entity = entityReference;

                        objectContext.Instance = entityComponent;
                    }
                    else if (entityScriptReference != null)
                    {
                        var entityReference = new Entity { Id = entityScriptReference.Entity.Id };
                        var scriptComponent = new ScriptComponent();
                        entityReference.Add(scriptComponent);

                        var entityScript = (Script)Activator.CreateInstance(entityScriptReference.ScriptType);
                        entityScript.Id = entityScriptReference.Id;
                        scriptComponent.Scripts.Add(entityScript);

                        objectContext.Instance = entityScript;
                    }
                    else if (objectContext.Instance is EntityReference)
                    {
                        objectContext.Instance = new Entity { Id = ((EntityReference)objectContext.Instance).Id };
                    }
                    else
                    {
                        base.TransformObjectAfterRead(ref objectContext);
                    }
                }
            }
        }
        protected override void ReplaceScript(ScriptComponent scriptComponent, ReloadedScriptEntry reloadedScript)
        {
            // Create new script instance
            var newScript = DeserializeScript(reloadedScript);

            // Dispose and unregister old script (and their MicroThread, if any)
            var oldScript = scriptComponent.Scripts[reloadedScript.ScriptIndex];

            // Flag scripts as being live reloaded
            if (game != null)
            {
                game.Script.LiveReload(oldScript, newScript);
            }

            // Replace with new script
            // TODO: Remove script before serializing it, so cancellation code can run
            scriptComponent.Scripts[reloadedScript.ScriptIndex] = newScript;

            oldScript.Dispose();
        }