static class · System

Resources

public static class Resources
Kind
static class
Namespace
System
Assembly
System.Core
Access
public, static, abstract
Source file
Resources.ant

Inheritance

Base classes
Any Resources

Methods

Install(str name, u8[] bytes)
#

static

Adds a resource at run time (a later one with the same name replaces it, and hides a built-in one).

Has(str name) : bool
#

static

Whether the program has resource name.

Read(str name) : u8[]?
#

static

The bytes of resource name (a copy), or null when there is none.

ReadText(str name) : str?
#

static

The text (UTF-8) of resource name, or null when there is none.

View(str name) : ResourceData?
#

static

Resource name read in place (no copy), or null when there is none.

Names() : str[]
#

static

The names of every resource: the built-in ones in build order, then the installed ones.

Private and internal members (9)

Dictionary<str, i32> builtIn
#

private static

Dictionary<str, u8[]> installed
#

private static

List<str> installedNames
#

private static

extern("$resources.count") Count() : i32
#

private static

extern("$resources.name") NameOf(i32 index) : u8[]
#

private static

extern("$resources.bytes") BytesOf(i32 index) : u8[]
#

private static

extern("$resources.length") LengthOf(i32 index) : i32
#

private static

unsafe extern("$resources.data") DataOf(i32 index) : u8*
#

private static

BuiltIn() : Dictionary<str, i32>
#

private static

Inherited members (5)

Source

Resources as Antril, decompiled from its Larva HIR.

System.Resources.ant
// System.Resources — System.Core (decompiled from Larva HIR)

into namespace System
{
	public static class Resources
	{
		Dictionary<str, i32> builtIn;
		Dictionary<str, u8[]> installed;
		List<str> installedNames;

		extern("$resources.count") Count() : i32;

		extern("$resources.name") NameOf(i32 index) : u8[];

		extern("$resources.bytes") BytesOf(i32 index) : u8[];

		extern("$resources.length") LengthOf(i32 index) : i32;

		unsafe extern("$resources.data") DataOf(i32 index) : u8*;

		BuiltIn() : Dictionary<str, i32>
		{
			Dictionary<str, i32> map = new Dictionary<str, i32>();
			loop (i32 i = 0..Count())
				map[new str(NameOf(i))] = i;
			return map;
		}

		public:
			/// Adds a resource at run time (a later one with the same name replaces it, and hides a built-in one).
			Install(str name, u8[] bytes)
			{
				if (!installed.ContainsKey(name))
					installedNames.Add(name);
				installed[name] = bytes;
			}

			/// Whether the program has resource `name`.
			Has(str name) : bool => installed.ContainsKey(name) || builtIn.ContainsKey(name);

			/// The bytes of resource `name` (a copy), or null when there is none.
			Read(str name) : u8[]?
			{
				if (installed.ContainsKey(name))
					return installed[name];
				if (builtIn.ContainsKey(name))
					return BytesOf(builtIn[name]);
				return null;
			}

			/// The text (UTF-8) of resource `name`, or null when there is none.
			ReadText(str name) : str?
			{
				if (Read(name) is u8[] bytes)
					return new str(bytes);
				return null;
			}

			/// Resource `name` read in place (no copy), or null when there is none.
			View(str name) : ResourceData?
			{
				if (installed.ContainsKey(name))
				{
					u8[] bytes = installed[name];
					i32 length = bytes.Length;
					unsafe
					{
						u8* first = length == 0 ? DataOf(-1) : &bytes[0];
						return new ResourceData(first, length);
					}
				}
				if (builtIn.ContainsKey(name))
				{
					i32 index = builtIn[name];
					unsafe
					{
						return new ResourceData(DataOf(index), LengthOf(index));
					}
				}
				return null;
			}

			/// The names of every resource: the built-in ones in build order, then the installed ones.
			Names() : str[]
			{
				List<str> names = new List<str>();
				loop (i32 i = 0..Count())
					names.Add(new str(NameOf(i)));
				loop (i32 i = 0..installedNames.Count)
				{
					if (!builtIn.ContainsKey(installedNames[i]))
						names.Add(installedNames[i]);
				}
				return names.ToArray();
			}
	}
}
Esc