Skip to content

We need to consider a unified API for validation #37

@marchc1

Description

@marchc1

Right now, a lot of things in the engine (Materials, Textures, Panels, etc) do not have a better way to determine if they're valid or not. While we won't get UAF's for having invalid pointers or anything, we will keep around resources that dont need to be kept, which also can lead to unintended behavior.

My previous engines have had something along the lines of this:

public interface IValidatable : IDisposable 
{
    bool Valid();
}
public static class IValidatableExts
{
    public static bool IsValid<T>(this T? object) where T : IValidatable => object == null ? false : object.Valid();
}

This would allow us to have Panel for example implement it as such:

public class Panel() {
    bool valid = true;
    ...
    void Remove() {
        ...
        Dispose();
        ...
    }
    void Dispose() {
        valid = false;
    }
    bool Valid() => valid;
    ...
}

The nice thing about the extension-based API here is that T can be nullable, which allows us to run this even on a null object - meaning we don't have to do something like obj != null && obj.IsValid() - this is all valid:

Panel? panel = null;
Assert(!panel.IsValid())
panel = new Button(...);
Assert(panel.IsValid())
panel.Remove();
Assert(!panel.IsValid())

It just deviates from what Source does (ie. Source has PHandles, etc). I just think this is a little nicer given we're working with C#.

Metadata

Metadata

Assignees

Labels

EnhancementNew feature or requestNeeds More InfoFurther information is requested

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions