mirror of
https://github.com/aharabada/glitchy-engine-beef.git
synced 2026-09-05 13:01:52 +00:00
Fixed ScriptClass leaking
This commit is contained in:
@@ -453,6 +453,7 @@ namespace GlitchyEditor.EditWindows
|
||||
className == scriptComponent.Instance?.ScriptClass.FullName))
|
||||
{
|
||||
scriptComponent.Instance = new ScriptInstance(script);
|
||||
scriptComponent.Instance.ReleaseRef();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@ using GlitchyEngine.Core;
|
||||
|
||||
namespace GlitchyEngine.Scripting;
|
||||
|
||||
class ScriptClass
|
||||
class ScriptClass : RefCounter
|
||||
{
|
||||
private String _namespace ~ delete _;
|
||||
private String _className ~ delete _;
|
||||
|
||||
@@ -18,13 +18,13 @@ static class ScriptEngine
|
||||
|
||||
private static Scene s_Context ~ _?.ReleaseRef();
|
||||
|
||||
private static ScriptClass s_EntityRoot ~ delete _;
|
||||
private static ScriptClass s_EngineObject ~ delete _;
|
||||
private static ScriptClass s_EntityRoot ~ _?.ReleaseRef();
|
||||
private static ScriptClass s_EngineObject ~ _?.ReleaseRef();
|
||||
|
||||
private static Dictionary<StringView, ScriptClass> _entityScripts = new .() ~ {
|
||||
for (var entry in _)
|
||||
{
|
||||
delete entry.value;
|
||||
entry.value.ReleaseRef();
|
||||
}
|
||||
delete _entityScripts;
|
||||
};
|
||||
@@ -62,12 +62,22 @@ static class ScriptEngine
|
||||
SetReference!(s_Context, scene);
|
||||
}
|
||||
|
||||
public static void OnRuntimeStop()
|
||||
{
|
||||
for (var entry in _entityScriptInstances)
|
||||
{
|
||||
entry.value.ReleaseRef();
|
||||
}
|
||||
_entityScriptInstances.Clear();
|
||||
|
||||
SetContext(null);
|
||||
}
|
||||
|
||||
public static void InitializeInstance(Entity entity, ScriptComponent* script)
|
||||
{
|
||||
_entityScriptInstances[entity.UUID] = script.Instance..AddRef();
|
||||
|
||||
script.Instance.Instantiate(entity.UUID);
|
||||
script.Instance.InvokeOnCreate();
|
||||
}
|
||||
|
||||
private static MonoAssembly* LoadCSharpAssembly(StringView assemblyPath)
|
||||
@@ -111,7 +121,7 @@ static class ScriptEngine
|
||||
{
|
||||
for (var entry in _entityScripts)
|
||||
{
|
||||
delete entry.value;
|
||||
entry.value.ReleaseRef();
|
||||
}
|
||||
_entityScripts.Clear();
|
||||
|
||||
|
||||
@@ -17,7 +17,7 @@ class ScriptInstance : RefCounter
|
||||
public this(ScriptClass scriptClass)
|
||||
{
|
||||
Log.EngineLogger.AssertDebug(scriptClass != null);
|
||||
_scriptClass = scriptClass;
|
||||
_scriptClass = scriptClass..AddRef();
|
||||
}
|
||||
|
||||
private ~this()
|
||||
@@ -27,6 +27,7 @@ class ScriptInstance : RefCounter
|
||||
_scriptClass.OnDestroy(_instance);
|
||||
Mono.mono_gchandle_free(_gcHandle);
|
||||
}
|
||||
_scriptClass?.ReleaseRef();
|
||||
}
|
||||
|
||||
public void Instantiate(UUID uuid)
|
||||
|
||||
@@ -462,8 +462,6 @@ namespace GlitchyEngine.World
|
||||
public void Dispose() mut
|
||||
{
|
||||
ReleaseRefAndNullify!(_instance);
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -76,7 +76,20 @@ namespace GlitchyEngine.World
|
||||
CopyComponents<Rigidbody2DComponent>(this, target);
|
||||
CopyComponents<BoxCollider2DComponent>(this, target);
|
||||
CopyComponents<CircleCollider2DComponent>(this, target);
|
||||
CopyComponents<ScriptComponent>(this, target);
|
||||
//CopyComponents<ScriptComponent>(this, target);
|
||||
|
||||
for (let (sourceHandle, sourceComponent) in _ecsWorld.Enumerate<ScriptComponent>())
|
||||
{
|
||||
Entity sourceEntity = .(sourceHandle, this);
|
||||
|
||||
Entity targetEntity = target.GetEntityByID(sourceEntity.UUID);
|
||||
ScriptComponent* targetComponent = targetEntity.AddComponent<ScriptComponent>();
|
||||
|
||||
// TODO: Do proper copy
|
||||
|
||||
targetComponent.Instance = new ScriptInstance(sourceComponent.Instance.ScriptClass);
|
||||
targetComponent.Instance..ReleaseRef();
|
||||
}
|
||||
|
||||
// Copy transforms
|
||||
for (let (sourceHandle, sourceTransform) in _ecsWorld.Enumerate<TransformComponent>())
|
||||
@@ -134,8 +147,8 @@ namespace GlitchyEngine.World
|
||||
|
||||
public void OnRuntimeStop()
|
||||
{
|
||||
ScriptEngine.SetContext(null);
|
||||
OnSimulationStop();
|
||||
ScriptEngine.OnRuntimeStop();
|
||||
}
|
||||
|
||||
public void OnSimulationStart()
|
||||
@@ -244,12 +257,28 @@ namespace GlitchyEngine.World
|
||||
if (!script.InInstantiated)
|
||||
{
|
||||
ScriptEngine.InitializeInstance(Entity(entity, this), script);
|
||||
script.Instance.InvokeOnCreate();
|
||||
}
|
||||
|
||||
script.Instance.InvokeOnUpdate(gameTime.DeltaTime);
|
||||
}
|
||||
}
|
||||
|
||||
/*if (mode.HasFlag(.Editor))
|
||||
{
|
||||
// Run editor scripts
|
||||
for (var (entity, script) in _ecsWorld.Enumerate<ScriptComponent>())
|
||||
{
|
||||
if (!script.InInstantiated)
|
||||
{
|
||||
ScriptEngine.InitializeInstance(Entity(entity, this), script);
|
||||
}
|
||||
|
||||
// TODO: Editor update
|
||||
//script.Instance.InvokeOnUpdate(gameTime.DeltaTime);
|
||||
}
|
||||
}*/
|
||||
|
||||
if (mode.HasFlag(.Physics))
|
||||
{
|
||||
// Update 2D physics
|
||||
|
||||
+16
-15
@@ -14,7 +14,8 @@ class MyTestEntity : Entity
|
||||
{
|
||||
Log.Info($"Create! {UUID}");
|
||||
|
||||
_rigidBody = GetComponent<RigidBody2D>();
|
||||
//_rigidBody = GetComponent<RigidBody2D>();
|
||||
RemoveComponent<RigidBody2D>();
|
||||
}
|
||||
|
||||
///// <summary>
|
||||
@@ -31,24 +32,24 @@ class MyTestEntity : Entity
|
||||
/// <param name="deltaTime"></param>
|
||||
void OnUpdate(float deltaTime)
|
||||
{
|
||||
Vector2 force = Vector2.Zero;
|
||||
//Vector2 force = Vector2.Zero;
|
||||
|
||||
if (Input.IsKeyPressed(Key.A))
|
||||
{
|
||||
force.X -= 1000 * deltaTime;
|
||||
}
|
||||
//if (Input.IsKeyPressed(Key.A))
|
||||
//{
|
||||
// force.X -= 1000 * deltaTime;
|
||||
//}
|
||||
|
||||
if (Input.IsKeyPressed(Key.D))
|
||||
{
|
||||
force.X += 1000 * deltaTime;
|
||||
}
|
||||
//if (Input.IsKeyPressed(Key.D))
|
||||
//{
|
||||
// force.X += 1000 * deltaTime;
|
||||
//}
|
||||
|
||||
if (Input.IsKeyPressing(Key.Space))
|
||||
{
|
||||
force.Y += 2000;
|
||||
}
|
||||
//if (Input.IsKeyPressing(Key.Space))
|
||||
//{
|
||||
// force.Y += 2000;
|
||||
//}
|
||||
|
||||
_rigidBody.ApplyForceToCenter(force);
|
||||
//_rigidBody.ApplyForceToCenter(force);
|
||||
|
||||
if (Input.IsMouseButtonReleasing(MouseButton.LeftButton))
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user