Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Config/eslint.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,14 @@
"C_WebView": "writable",
"BinaryReader": "writable",
"Bitmap": "writable",
"BuiltinAudioDecoder": "writable",
"Button": "writable",
"Canvas": "writable",
"Channels": "writable",
"CheckButton": "writable",
"Color": "writable",
"CRC32": "writable",
"Decoder": "writable",
"EditBox": "writable",
"Enum": "writable",
"FieldSet": "writable",
Expand Down
12 changes: 12 additions & 0 deletions Core/API/WebAudio/BuiltinAudioDecoder.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// This doesn't do any actual decoding; it merely serves to utilize the Resource cache for audio playback
// The audio engine supports no caching mechanism of its own, so we wrap any calls to disk and serve Resources instead
class BuiltinAudioDecoder extends Decoder {
fileTypes = { mp3: true, ogg: true, wav: true };
// No need to do any decoding as these audio formats are supported natively
decode(resource) {
return resource;
}
getSupportedFileTypes() {
return this.fileTypes;
}
}
16 changes: 9 additions & 7 deletions Core/APIs/C_Decoding.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,17 @@ const C_Decoding = {
return true;
},
addDecoder(decoder) {
const fileType = decoder.getSupportedFileType();
const fileTypes = decoder.getSupportedFileTypes();

if (this.registeredDecoders[fileType]) {
NOTICE(format("Failed to register decoder for file type %s (already registered)", fileType));
return;
}
for (const fileType of Object.keys(fileTypes)) {
if (this.registeredDecoders[fileType]) {
NOTICE(format("Failed to register decoder for file type %s (already registered)", fileType));
return;
}

this.registeredDecoders[fileType] = decoder;
DEBUG(format("Registered new decoder for file type *.%s", fileType.toUpperCase()));
this.registeredDecoders[fileType] = decoder;
DEBUG(format("Registered new decoder for file type *.%s", fileType.toUpperCase()));
}
},
decodeFile(filePath) {
const resource = C_Resources.load(filePath);
Expand Down
12 changes: 12 additions & 0 deletions Core/Builtins/Decoder.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class Decoder {
fileTypes = {};
constructor(decodingFunction) {
this.decode = decodingFunction || this.decode; // NO-OP as a safe default
}
getSupportedFileTypes() {
return this.fileTypes;
}
decode(resource) {
return resource;
}
}
12 changes: 0 additions & 12 deletions Core/Classes/Decoder.js

This file was deleted.

27 changes: 27 additions & 0 deletions Tests/API/C_WebAudio/BuiltinAudioDecoder.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
describe("BuiltinAudioDecoder", () => {
const decoder = new BuiltinAudioDecoder();
it("should be exported into the global environment", () => {
assertEquals(decoder.constructor.name, "BuiltinAudioDecoder");
});

let exportedApiSurface = ["getSupportedFileTypes", "decode"];

exportedApiSurface.forEach((namedExport) => {
it("should export function " + namedExport, () => {
assertEquals(typeof decoder[namedExport], "function");
});
});

const supportedFormats = decoder.getSupportedFileTypes();
it("should indicate that MP3 files can be decoded", () => {
assertTrue(supportedFormats["mp3"]);
});

it("should indicate that OGG files can be decoded", () => {
assertTrue(supportedFormats["ogg"]);
});

it("should indicate that WAV files can be decoded", () => {
assertTrue(supportedFormats["wav"]);
});
});
16 changes: 16 additions & 0 deletions Tests/Builtins/Decoder.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
describe("Decoder", () => {
const decoder = new Decoder();
it("should be exported into the global environment", () => {
assertEquals(decoder.constructor.name, "Decoder");
});

let exportedApiSurface = ["getSupportedFileTypes", "decode"];

exportedApiSurface.forEach((namedExport) => {
it("should export function " + namedExport, () => {
assertEquals(typeof decoder[namedExport], "function");
});
});

// Since this is just an interface that needs to be implemented, there's no implementation to test
});
9 changes: 8 additions & 1 deletion Tests/run-renderer-tests.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
const testSuites = {
SharedConstants: ["SharedConstants/Aliases.js", "SharedConstants/Paths.js"],
Builtins: ["Builtins/Assertions.js", "Builtins/LocalCacheTests.js", "Builtins/UniqueID.js", "Builtins/Validators.js"],
Builtins: [
"Builtins/Assertions.js",
"Builtins/LocalCacheTests.js",
"Builtins/Decoder.js",
"Builtins/UniqueID.js",
"Builtins/Validators.js",
],
C_Settings: [
"API/C_Settings/validate.js",
"API/C_Settings/validateDefaultSettings.js",
"API/C_Settings/validateUserSettings.js",
],
C_WebAudio: ["API/C_WebAudio/BuiltinAudioDecoder.js"],
};

for (const namespace in testSuites) {
Expand Down
4 changes: 2 additions & 2 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,6 @@
<!-- The core/engine must be loaded before the WebClient can be started -->
<script src="Core/Classes/AddOn.js"></script>

<script src="Core/Classes/Decoder.js"></script>

<script src="Core/Classes/HeightMap.js"></script>
<script src="Core/Classes/NavigationMap.js"></script>
<script src="Core/Classes/TerrainMap.js"></script>
Expand Down Expand Up @@ -140,6 +138,8 @@
StartWebClient();
</script>
</body>
<script src="Core/Builtins/Decoder.js"></script>
<script src="Core/Builtins/Validation.js"></script>
<script src="Core/Builtins/UniqueID.js"></script>
<script src="Core/API/WebAudio/BuiltinAudioDecoder.js"></script>
</html>