Skip to content

Commit 09ea4b6

Browse files
committed
Add a button to load mod files
1 parent 414cc0d commit 09ea4b6

File tree

3 files changed

+43
-2
lines changed

3 files changed

+43
-2
lines changed

Loader/gdmodext/html/menu.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ <h2 class="uk-heading-small">Overview</h2>
2525
</li>
2626
<li id="mods">
2727
<h2 class="uk-heading-small">Mod Loader</h2>
28-
<p class="uk-text-lead">Nothing here yet ;)</p>
28+
<button class="uk-button-primary" id="selectMod">Load Mod</button>
29+
<input type="file" id="fileInput" hidden></input>
2930
<a href="#" uk-switcher-item="next">Go to Scene Switcher</a>
3031
</li>
3132
<li id="scenes">

Loader/gdmodext/js/injected.js

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const debug = false;
1111
/**
1212
* The CDN to fetch the GDAPI files from
1313
*/
14-
const CDN = "https://cdn.jsdelivr.net/gh/arthuro555/gdmod@latest/API/";
14+
const CDN = "https://cdn.jsdelivr.net/gh/arthuro555/gdmod/API/";
1515

1616
/**
1717
* Flag telling if that page got patched already.
@@ -88,6 +88,30 @@ function patchSceneCode() {
8888
}
8989
}
9090

91+
/** From https://stackoverflow.com/a/12300351/10994662 */
92+
function dataURItoBlob(dataURI) {
93+
var byteString = atob(dataURI.split(',')[1]);
94+
95+
// separate out the mime component
96+
var mimeString = dataURI.split(',')[0].split(':')[1].split(';')[0]
97+
98+
// write the bytes of the string to an ArrayBuffer
99+
var ab = new ArrayBuffer(byteString.length);
100+
101+
// create a view into the buffer
102+
var ia = new Uint8Array(ab);
103+
104+
// set the bytes of the buffer to the correct values
105+
for (var i = 0; i < byteString.length; i++) {
106+
ia[i] = byteString.charCodeAt(i);
107+
}
108+
109+
// write the ArrayBuffer to a blob, and you're done
110+
var blob = new Blob([ab], {type: mimeString});
111+
return blob;
112+
113+
}
114+
91115

92116
// First we verify if the game is a GDevelop game
93117
if(window.gdjs !== undefined) {
@@ -115,6 +139,9 @@ if(window.gdjs !== undefined) {
115139
} else if(event.data["message"] === "changeScene") {
116140
if(typeof GDAPI === "undefined") return;
117141
GDAPI.currentScene.getGame()._sceneStack.replace(event.data.scene, true);
142+
} else if(event.data["message"] === "loadMod") {
143+
const mod = dataURItoBlob(event.data["mod"]);
144+
GDAPI.loadZipMod(mod);
118145
}
119146
}
120147
});

Loader/gdmodext/js/menu.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,23 @@ chrome.tabs.query({active: true, currentWindow: false}, function(tabs) {
5252
}
5353
});
5454

55+
const fileElement = document.getElementById("fileInput");
56+
fileElement.addEventListener('change', function () {
57+
var reader = new FileReader();
58+
reader.onloadend = function() {
59+
chrome.tabs.sendMessage(tabs[0].id, {message: "loadMod", mod: reader.result});
60+
}
61+
reader.readAsDataURL(fileElement.files[0]);
62+
});
63+
5564
document.getElementById('mods').addEventListener('show', function () {
5665
chrome.tabs.sendMessage(tabs[0].id, {message: "installAPI"});
5766
});
5867

68+
document.getElementById('selectMod').addEventListener('click', function() {
69+
fileElement.click();
70+
});
71+
5972
// Now that we opened the mod loader install the API into the game (the API is not crucial and is therefore only loaded when needed)
6073
chrome.tabs.sendMessage(tabs[0].id, {message: "connect"});
6174
});

0 commit comments

Comments
 (0)