Skip to content

Commit

Permalink
Convert file components into ECS resources
Browse files Browse the repository at this point in the history
  • Loading branch information
chances committed Apr 9, 2024
1 parent 0b83ed1 commit 94566c1
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 14 deletions.
1 change: 1 addition & 0 deletions dub.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"systemDependencies": "glfw >= 3.2, ",
"dependencies": {
"bindbc-glfw": "~>1.0.1",
"concepts": "~>0.1.0",
"eventcore": "~>0.9.20",
"gfm:math": "~>8.0",
"wgpu-d": {
Expand Down
49 changes: 35 additions & 14 deletions source/ecs/components.d
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
/// License: 3-Clause BSD License
module teraflop.ecs.components;

import concepts : implements;
import wgpu.api : Adapter, Device;

///
interface Resource {
import wgpu.api : Adapter, Device;

/// Whether *all* of an `teraflop.ecs.Entity`'s GPU Resources have been initialized.
/// See_Also: `teraflop.ecs.Entity.tag`
static const Initialized = "Initialized";
Expand All @@ -26,8 +27,10 @@ interface Asset : Resource {

/// A file either read from the user's disk or from an in-memory buffer.
/// See_Also: `ObservableFile`
@implements!(File, Resource)
struct File {
static import std.file;
import std.string : format;

package const string componentName;
private bool _exists = false;
Expand All @@ -38,16 +41,21 @@ struct File {
/// Contents of this file.
const(ubyte)[] contents;

/// Remarks: Allocates a buffer equivalent to the file's size in bytes, if it exists.
/// Params:
/// filePath = Path to a file.
this(string filePath) {
this.filePath = componentName = filePath;
import std.path : baseName;

this.filePath = filePath;
this.componentName = format!"File:%s"(filePath.baseName);
this._exists = std.file.exists(filePath);
if (this.exists) contents = new ubyte[std.file.getSize(filePath)];
else contents = new ubyte[0];
}
/// Instantiate a static file given its in-memory `contents`.
package (teraflop) this(const(ubyte)[] contents) {
import std.digest.sha : toHexString, sha1Of;
import std.string : format;

this.filePath = null;
this._exists = true;
Expand Down Expand Up @@ -77,10 +85,30 @@ struct File {
import teraflop.ecs : named;
return (cast(File) this).named(componentName);
}

/// Reads this file's contents into memory.
/// Remarks: This is a no-op if this file exists purely in-memory.
/// See_Also: `inMemory`
void initialize(Adapter adapter, Device device) @trusted {
import std.algorithm : copy;
import std.conv : castFrom;
import std.stdio : File;

if (this.inMemory) return;

auto file = File(this.filePath, "rb");
auto contents = castFrom!(const(ubyte)[]).to!(ubyte[])(this.contents);
auto buffer = file.rawRead(contents);
file.close();
assert(buffer.copy(contents).length == 0, "File buffer is not entirely filled.");
}
}

// TODO: Read and write memory mapped files. Memory mapping can increase I/O performance of large files.

/// A file that may be watched for changes at runtime.
/// See_Also: `File`
@implements!(ObservableFile, Resource)
struct ObservableFile {
static import std.file;
import std.typecons : Flag, No, Yes;
Expand All @@ -102,7 +130,6 @@ struct ObservableFile {
this(string filePath, Flag!"hotReload" hotReload = No.hotReload) {
_file = new File(filePath);
this.hotReload = hotReload;
this.readFile();
}
/// Instantiate a static file given its in-memory `contents`.
package (teraflop) this(string contents) {
Expand Down Expand Up @@ -133,14 +160,8 @@ struct ObservableFile {
return (cast(ObservableFile) this).named(_file.componentName);
}

package (teraflop) void readFile() {
import std.stdio : File;

_file.exists = std.file.exists(_file.filePath);
if (!exists) return;

auto file = File(_file.filePath, "rb");
_file.contents = file.rawRead(new ubyte[file.size()]);
file.close();
/// See_Also: `File.initialize`
void initialize(Adapter adapter, Device device) {
this._file.initialize(adapter, device);
}
}

0 comments on commit 94566c1

Please sign in to comment.