Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend searching & transform functionality of TPJResourceFile #4

Open
delphidabbler opened this issue Nov 20, 2022 · 2 comments
Open
Assignees
Labels
accepted enhancement New feature or request

Comments

@delphidabbler
Copy link
Member

delphidabbler commented Nov 20, 2022

At present there is one method of TPJResourceFile that can be used to find resources:

function FindEntry(const ResType, ResName: PChar;
  const LangID: Word = $FFFF): TPJResourceEntry;

But this method only finds the first matching resource and the ability to specify search criteria is limited.

Searching would be improved if:

  1. All resources matching a criteria could be found.
  2. The user could specify the search criteria by means of a callback closure.
  3. Methods could check whether some or all entries match a condition.
  4. Methods could find sets of resource types & language ids matching certain criteria.

An iterator function and possibly a map function would also be useful.

Possible methods would be:

type
  TPJResourceFile = class
  public
    // ...
    function SelectEntries(Predicate: TPredicate<TPJResourceEntry>): TArray<TPJResourceEntry>;
    function SomeEntries(Predicate: TPredicate<TPJResourceEntry>): Boolean;
    function EveryEntry(Predicate: TPredicate<TPJResourceEntry>): Boolean;
    procedure ForEachEntry(Callback: TProc<TPJResourceEntry>); overload;
    procedure ForEachEntry(Predicate: TPredicate<TPJResourceEntry>; Callback: TProc<TPJResourceEntry>); overload;
    function MapEntries<T>(Callback: TFunc<TPJResourceEntry,T>): TArray<T>; overload;
    function MapEntries<T>(Predicate: TPredicate<TPJResourceEntry>; Callback: TFunc<TPJResourceEntry,T>): TArray<T>; overload;
    function FindEntry(Predicate: TPredicate<TPJResourceEntry>): TPJResourceEntry; overload;
    function FindEntryIndex(Predicate: TPredicate<TPJResourceEntry>): Boolean; overload;
    function FindUniqueResTypes(Predicate: TPredicate<TPJResourceEntry>): TArray<string>;
    function FindUniqueLanguageIDs(Predicate: TPredicate<TPJResourceEntry>): TArray<Word>;
    // ...
  end;

Descriptions:

  • SelectEntries - Selects the resource entries matching the given predicate and returns an array of selected entries.
  • SomeEntries - Returns true if one or more resource entries match the given predicate else false. This is a generalised version of the existing EntryExists method.
  • EveryEntry - Returns true if all resource entries match the given predicate else false.
  • ForEachEntry - Two overloaded methods. The first unconditionally calls the given callback for each resource entry. The 2nd only calls the callback if the given criteria are satisfied.
  • MapEntries<T> - Two overloaded methods that map resource entries onto some other type and returns an array of the new type. The first overload maps every resource while the second includes maps only entries that match the given criteria.
  • FindEntry - Overload of the existing method that finds and returns the first resource entry that matches the given predicate. Returns nil if there is no match.
  • FindEntryIndex - Overload of the existing method that finds the index of the first resource entry that matches the given predicate. Returns -1 if there is no match.
  • FindUniqueResTypes - Returns an array containing the set of string representations of the resource types of resource entries that match the given criteria.
  • FindUniqueLanguageIDs - Returns an array containing the set language IDs of resource entries that match the given criteria.
@delphidabbler
Copy link
Member Author

  • ForEachEntry - Two overloaded methods. The first unconditionally calls the given callback for each resource entry. The 2nd only calls the callback if the given criteria are satisfied.

A better name for the 2nd version of ForEachEntry (with the predicate) would be ForSomeEntries.

@delphidabbler delphidabbler self-assigned this Nov 20, 2022
@delphidabbler
Copy link
Member Author

function FindUniqueResTypes(Predicate: TPredicate<TPJResourceEntry>): TArray<string>;
function FindUniqueLanguageIDs(Predicate: TPredicate<TPJResourceEntry>): TArray<Word>;

Should there also be methods that get all resource types & all language IDs?

function UniqueResTypes: TArray<string>;
function UniqueLanguageIDs: TArray<Word>;

These are equivalent to calling the FindUniqueXXX methods with a predicate that always returns true.

And maybe FindUniqueXXX methods would be better named SelectUniqueXXX.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
accepted enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant