Skip to content

Commit

Permalink
fucking finally decryption should not be needed
Browse files Browse the repository at this point in the history
  • Loading branch information
mattieFM committed Jan 24, 2025
1 parent 496eaad commit 40d3241
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 4 deletions.
1 change: 1 addition & 0 deletions www/mods/commonLibs/_common.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"_common/API_CORE/miscAPI",
"_common/API_CORE/infoAPI",
"_common/API_CORE/simpleBattleAPI",
"_common/API_CORE/imageAPI",
"_common/supporters",
"_common/seedRandom",
"_common/util",
Expand Down
47 changes: 47 additions & 0 deletions www/mods/commonLibs/_common/API_CORE/imageAPI.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/**
* @namespace MATTIE.imageAPI
* @description the api for handling image related things, currently only saves bitmap to file.
*/
MATTIE.imageAPI = MATTIE.imageAPI || {};


MATTIE.imageAPI.saveBitmapToFile = function saveBitmapToFile(bitmap, outputPath) {
var path = require("path");
var fs = require('fs');

bitmap.addLoadListener(() => {
// Ensure the Bitmap is ready
if (!bitmap.isReady()) {
console.error("Bitmap is not ready to be saved.");
return;
}

try {
// Get the underlying canvas
const canvas = bitmap._canvas;

// Convert the canvas to a Base64 data URL
const dataUrl = canvas.toDataURL("image/png"); // PNG format

// Extract the Base64 data portion (strip off the 'data:image/png;base64,' part)
const base64Data = dataUrl.replace(/^data:image\/\w+;base64,/, "");

// Decode the Base64 string into binary data
const buffer = Buffer.from(base64Data, "base64");

// Ensure the output directory exists
const outputDir = path.dirname(outputPath);
if (!fs.existsSync(outputDir)) {
fs.mkdirSync(outputDir, { recursive: true });
}

// Write the binary data to the file
fs.writeFileSync(outputPath, buffer);
console.log(`Image saved to: ${outputPath}`);
} catch (error) {
console.error("Failed to save the bitmap:", error);
}
});


}
24 changes: 20 additions & 4 deletions www/mods/commonLibs/_common/compatibility.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,21 @@ MATTIE.compat = MATTIE.compat || {};
/** @description whether decryption of images should be forcibly stopped or not */
MATTIE.compat.pauseDecrypt = false;

/**
* @description weather or not files should be decrypted and saved to their unencrypted sate while playing the game
* this has some benefits and some drawbacks. This should not be any better than leaving the files encrypted as it decrypts them first anyways
* this might be useful for some edge cases though.
* @default false
*/
MATTIE.compat.runtime_decrypt=false;

/**
* @description a loader for assets that can load encrypted and unencrypted files
* slightly heavier as it is using fs.existssync
*/
Bitmap.prototype.compatabilityLoad = function (url) {
var fs = require('fs');

try {
let pngUrl = `${MATTIE.DataManager.localGamePath()}/`;
let rpgMVPUrl = `${pngUrl}/`;
Expand All @@ -28,14 +37,21 @@ Bitmap.prototype.compatabilityLoad = function (url) {
pngUrl += url;
}

pngUrl = pngUrl.replace('%24', '$');
rpgMVPUrl = rpgMVPUrl.replace('%24', '$');
pngUrl = decodeURIComponent(pngUrl);
rpgMVPUrl = decodeURIComponent(rpgMVPUrl);

console.log(rpgMVPUrl);
if (fs.existsSync(rpgMVPUrl) && Decrypter.hasEncryptedImages && !MATTIE.compat.pauseDecrypt) {

rpgmakerWantsToDecrypt = !Decrypter.checkImgIgnore(url) && Decrypter.hasEncryptedImages
modmanagerWantsToDecrypt = fs.existsSync(rpgMVPUrl) && !MATTIE.compat.pauseDecrypt

console.log(`modmanger want to decrypt:${modmanagerWantsToDecrypt}\nrpgmakerwantstodecrypt${rpgmakerWantsToDecrypt}`);
if (modmanagerWantsToDecrypt && rpgmakerWantsToDecrypt) {
if (Utils.isNwjs()) {
this._loadingState = 'decrypting';
Decrypter.decryptImg(url, this);
Decrypter.decryptImg(pngUrl, this);
MATTIE.imageAPI.saveBitmapToFile(this,pngUrl)
console.log("image decrypted")
}
} else if (pngUrl) {
// this line will fix the issue with caching images and let us update files during runtime.
Expand Down

0 comments on commit 40d3241

Please sign in to comment.