Skip to content

Commit

Permalink
encrypt/decrypt screen
Browse files Browse the repository at this point in the history
  • Loading branch information
mattieFM committed Jan 25, 2025
1 parent f3e7887 commit 21124fd
Show file tree
Hide file tree
Showing 5 changed files with 213 additions and 20 deletions.
1 change: 1 addition & 0 deletions www/mods/commonLibs/_common/API_CORE/imageAPI.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ MATTIE.imageAPI.saveBitmapToFile = function saveBitmapToFile(bitmap, outputPath)
var path = require("path");
var fs = require('fs');

console.log("waiting for bitmap to load")
bitmap.addLoadListener(() => {
// Ensure the Bitmap is ready
if (!bitmap.isReady()) {
Expand Down
49 changes: 44 additions & 5 deletions www/mods/commonLibs/_common/compatibility.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,50 @@ MATTIE.compat.pauseDecrypt = false;
*/
MATTIE.compat.runtime_decrypt=false;

/**
* @description set the field hasEncryptedImages to true or false in memory and system.json
* @param {boolean} bool
*/
MATTIE.compat.setEncryptedImagesInSystemJson = function(bool){
const fs = require("fs")
sysJsonPath = `${MATTIE.DataManager.localGamePath()}/data/System.json`;
Decrypter.hasEncryptedImages = bool;
console.log("system.json loading")
if(fs.existsSync(sysJsonPath)){
console.log("system.json found")
contents = JSON.parse(fs.readFileSync(sysJsonPath))
contents.hasEncryptedImages = bool
contents = JSON.stringify(contents)
fs.writeFileSync(sysJsonPath,contents)
}
console.log("system.json updated")
}

Bitmap.prototype.decryptAndSave = function(url){
let pngUrl = `${MATTIE.DataManager.localGamePath()}/`;
let rpgMVPUrl = `${pngUrl}/`;
if (url.endsWith('.rpgmvp')) {
rpgMVPUrl += url;
pngUrl += url.replace('.rpgmvp', '.png');
} else if (url.endsWith('.png')) {
rpgMVPUrl += url.replace('.png', '.rpgmvp');
pngUrl += url;
}

pngUrl = decodeURIComponent(pngUrl);
rpgMVPUrl = decodeURIComponent(rpgMVPUrl);

this._loadingState = 'decrypting';
Decrypter.decryptImg(pngUrl, this);
//MATTIE.imageAPI.saveBitmapToFile(this,pngUrl)
console.log("image decrypted")
}

/**
* @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) {
Bitmap.prototype.compatabilityLoad = function (url,force=false) {
var fs = require('fs');

try {
Expand All @@ -38,20 +76,21 @@ Bitmap.prototype.compatabilityLoad = function (url) {
pngUrl += url;
}

pngUrl = decodeURIComponent(pngUrl);
rpgMVPUrl = decodeURIComponent(rpgMVPUrl);
// pngUrl = decodeURIComponent(pngUrl);
// rpgMVPUrl = decodeURIComponent(rpgMVPUrl);

console.log(rpgMVPUrl);

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 ((modmanagerWantsToDecrypt && rpgmakerWantsToDecrypt) || force || (fs.existsSync(rpgMVPUrl) && !fs.existsSync(pngUrl))) {
if (Utils.isNwjs()) {
this._loadingState = 'decrypting';
Decrypter.decryptImg(pngUrl, this);
MATTIE.imageAPI.saveBitmapToFile(this,pngUrl)
console.log(pngUrl)
if(MATTIE.compat.runtime_decrypt) MATTIE.imageAPI.saveBitmapToFile(this,pngUrl)
console.log("image decrypted")
}
} else if (pngUrl) {
Expand Down
181 changes: 166 additions & 15 deletions www/mods/commonLibs/_common/menus/scenes.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@ MATTIE.CmdManager = MATTIE.CmdManager || {};
MATTIE.modLoader = MATTIE.modLoader || {};


MATTIE.TextManager.decryptBtn="Decrypt Game"
MATTIE.TextManager.decryptBtn="Decrypt"
MATTIE.CmdManager.decryptBtn="CMD_Decrypt"
MATTIE.TextManager.encryptBtn = "Encrypt Game"
MATTIE.TextManager.encryptBtn = "Encrypt"
MATTIE.CmdManager.encryptBtn = "CMD_Encrypt"
MATTIE.TextManager.deleteEncryptedFiles = "Delete ENcrypted Files"
MATTIE.CmdManager.deleteEncryptedFiles = "CMD_DeleteEncrypted"
MATTIE.TextManager.deleteDecryptedFiles = "Delete DEcrypted Files"
MATTIE.CmdManager.deleteDecryptedFiles = "CMD_DeleteDecrypted"

/**
* @class
Expand Down Expand Up @@ -73,6 +77,8 @@ MATTIE.scenes.modLoader.prototype.addModsListWindow = function () {
MATTIE.scenes.decrypter = function () {
this.initialize.apply(this, arguments);
this.text3="test"
this.encryptedCount=0
this.totalCount=0
};

MATTIE.scenes.decrypter.prototype = Object.create(MATTIE.scenes.base.prototype);
Expand All @@ -95,26 +101,82 @@ MATTIE.scenes.decrypter.prototype.addWins = function(){
this.addTextWindow()
}

function findFilesInDir(directory) {
const fs = require('fs');
const path = require('path');
let results = [];

// Read all items in the current directory
const items = fs.readdirSync(directory);

items.forEach(item => {
const fullPath = path.join(directory, item);
const stat = fs.statSync(fullPath);

// If it's a directory, recurse into it
if (stat.isDirectory()) {
results = results.concat(findFilesInDir(fullPath));
} else {
// Otherwise, it's a file, add it to the results
results.push(fullPath);
}
});

return results;
}

MATTIE.scenes.decrypter.prototype.count = function(){
files=findFilesInDir(`${MATTIE.DataManager.localGamePath()}/img/`)
numberOfRPGMVPFiles=files.filter(name=>name.includes("rpgmvp")).length
this.encryptedCount = numberOfRPGMVPFiles
this.totalCount = files.length
this.updateText()
}

MATTIE.scenes.decrypter.prototype.updateText = function(){
const text2 = [
`RPGMaker Status: ${Decrypter.hasEncryptedImages?"Encrypted":"Decrypted"}`,
`Files Status: ${this.encryptedCount}/${this.totalCount} Encrypted`,
];
this._decryptStatusWin.updateText(text2);
}

/**add the window that shows information and text for this scene */
MATTIE.scenes.decrypter.prototype.addTextWindow = function () {
const text = [
'This is the decryption utility page It will let',
'you encrypt and decrypt all applicable files.',
'This page handles decryption and encryption. Do not use anything',
'here unless you need to, decryption is UNECCECARY now. no files',
'are deleted via encrypt/decrypt in this process as such an',
'decrypted game will have a .rpgmvp and a .png file for each image.',
'RPGMaker status determines which file will be used when playing',
'the game. You may toggle between these files by clicking encrypt',
'and decrypt. For most people it is not necessary to ever toggle',
'between the two but for overhaul mods that use encrypted files',
'this feature could be useful.',
'the delete buttons are largely not needed to be used ever. for',
'more info you can click one to read its confirmation prompt just',
'be sure to click cancel and not okay lest you delete things.'
];
this._textDisplayWin = new MATTIE.windows.TextDisplay(0,0,600,200, text);
this._textDisplayWin = new MATTIE.windows.TextDisplay(0,0,800,350, text);
this._textDisplayWin.updatePlacement()
this.addWindow(this._textDisplayWin);

const text2 = [
`RPGMaker Status: ${Decrypter.hasEncryptedImages?"Encrypted":"Decrypted"}`,
`Files Status: ${Decrypter.hasEncryptedImages?"Encrypted":"Decrypted"}`,
];
this._decryptStatusWin = new MATTIE.windows.TextDisplay(0,0,400,100, text2);
this._decryptStatusWin.updatePlacement(100,-200)

this._decryptStatusWin = new MATTIE.windows.TextDisplay(0,0,450,100, "");
this._decryptStatusWin.updatePlacement(75,-250)
this.addWindow(this._decryptStatusWin);
this.count()

// Create the tooltip window
this._tooltipWindow = new MATTIE.windows.Window_Tooltip(250,100,"hiya",300,undefined,300);
this._tooltipWindow = new MATTIE.windows.Window_Tooltip(225,35,[
`RPGMaker Status indicates wether.`,
`.rpgmvp or .png files will be preferred`,
`files status is the number of`,
`files in your copy of the game.`,
`By default we do not delete encrypted`,
`files if your game is decrypted it will be`,
`roughly half of the files being encrypted.`
],500,undefined,300);
this.addWindow(this._tooltipWindow);

}
Expand All @@ -127,25 +189,114 @@ MATTIE.scenes.decrypter.prototype.addDecryptBtn = function () {
const btns = {};
btns[MATTIE.TextManager.decryptBtn] = MATTIE.CmdManager.decryptBtn;
btns[MATTIE.TextManager.encryptBtn] = MATTIE.CmdManager.encryptBtn;
this._decryptBtn = new MATTIE.windows.HorizontalBtns(175 + 300 + 10, btns, 2);
btns[MATTIE.TextManager.deleteEncryptedFiles] = MATTIE.CmdManager.deleteEncryptedFiles;
btns[MATTIE.TextManager.deleteDecryptedFiles] = MATTIE.CmdManager.deleteDecryptedFiles;
this._decryptBtn = new MATTIE.windows.HorizontalBtns(175 + 300 + 10, btns, 4);
this._decryptBtn.updateWidth(600);
this._decryptBtn.updatePlacement(175 + 300 + 10);
this.addWindow(this._decryptBtn)
};



/**
* setup all handlers for this scene

Check failure on line 203 in www/mods/commonLibs/_common/menus/scenes.js

View workflow job for this annotation

GitHub Actions / build

This line has a length of 709. Maximum allowed is 160
*/
MATTIE.scenes.decrypter.prototype.setupHandlers = function() {
this._decryptBtn.setHandler(MATTIE.CmdManager.decryptBtn, (() => {
console.log("hiya")
this._decryptBtn.setHandler(MATTIE.CmdManager.decryptBtn, (async () => {
const fs = require("fs")

if(
confirm("This will freeze the game until it is finished. The first time this is run it will take 5-10 min for slower computers, it will not take much time to toggle between encrypted/decrypted files once the bulk of the work is done.\n This will create a decrypt all files, leaving the original files intact, simply creating a decrypted copy, as such you will have .rpgmvp files and .png files for every image.\n Additionally this will set system.json's field 'hasEncryptedImages' to false.\nIMPORTANT:this can cause the game to crash sometimes depending on how much memory your computer has, should it crash you may restart the game and click the button again, it will pickup where it left off.")
){
img_files=findFilesInDir(`${MATTIE.DataManager.localGamePath()}/img/`).filter(path=>path.includes(".rpgmvp"))

Check failure on line 212 in www/mods/commonLibs/_common/menus/scenes.js

View workflow job for this annotation

GitHub Actions / build

Unexpected `await` inside a loop
prev = MATTIE.compat.runtime_decrypt
MATTIE.compat.runtime_decrypt=true

for (let index = 0; index < img_files.length; index++) {
const img = img_files[index];
if(!fs.existsSync(img.replace(".rpgmvp",".png"))){
await new Promise(res=>{
bitmap=Bitmap.load(img.split("www")[1])
const that = this
bitmap.addLoadListener(function() {
that.totalCount++
that.updateText()
res()
});
})

}
}




MATTIE.compat.setEncryptedImagesInSystemJson(false)

MATTIE.compat.runtime_decrypt=prev
}

//call activate so we dont get locked out
this.count()
this._decryptBtn.activate();
}));

Check failure on line 244 in www/mods/commonLibs/_common/menus/scenes.js

View workflow job for this annotation

GitHub Actions / build

This line has a length of 255. Maximum allowed is 160
this._decryptBtn.setHandler(MATTIE.CmdManager.encryptBtn, (() => {
console.log("hiya")
//call activate so we dont get locked out
MATTIE.compat.setEncryptedImagesInSystemJson(true)
this.count()
this._decryptBtn.activate();
}));

Check failure on line 251 in www/mods/commonLibs/_common/menus/scenes.js

View workflow job for this annotation

GitHub Actions / build

Unexpected `await` inside a loop

this._decryptBtn.setHandler(MATTIE.CmdManager.deleteEncryptedFiles, (async () => {
const fs = require("fs")
if(confirm("This button will delete all encrypted files that have a matching .png file of the same name. This is largely uneccecary to ever do. The only main reason to do this is to save some disk space. But its not much that you would save.")){
img_files=findFilesInDir(`${MATTIE.DataManager.localGamePath()}/img/`).filter(path=>path.includes(".rpgmvp"))

for (let index = 0; index < img_files.length; index++) {
const img = img_files[index];
if(fs.existsSync(img.replace(".rpgmvp",".png"))){
console.log(`deleting:${img}`)
await new Promise(res=>{
fs.unlink(img, (err) => {
this.totalCount--
this.encryptedCount--
this.updateText()
res()
})})

Check failure on line 268 in www/mods/commonLibs/_common/menus/scenes.js

View workflow job for this annotation

GitHub Actions / build

This line has a length of 265. Maximum allowed is 160

}
}
}
this.count()
this._decryptBtn.activate();
}));

Check failure on line 275 in www/mods/commonLibs/_common/menus/scenes.js

View workflow job for this annotation

GitHub Actions / build

Unexpected `await` inside a loop

this._decryptBtn.setHandler(MATTIE.CmdManager.deleteDecryptedFiles, (async () => {
const fs = require("fs")
if(confirm("This button will delete all decrypted (.png) files that have a matching .rpgmvp file of the same name. This is largely uneccecary to ever do. The only main reason to do this is to save some disk space. But its not much that you would save.")){
img_files=findFilesInDir(`${MATTIE.DataManager.localGamePath()}/img/`).filter(path=>path.includes(".png"))
console.log(img_files)
for (let index = 0; index < img_files.length; index++) {
const img = img_files[index];
if(fs.existsSync(img.replace(".png",".rpgmvp"))){
console.log(`deleting:${img}`)
await new Promise(res=>{
fs.unlink(img, (err) => {
this.totalCount--
this.updateText()
res()
})})
}
}
}
MATTIE.compat.setEncryptedImagesInSystemJson(true)
this.count()
this._decryptBtn.activate();
}));

this._decryptBtn.setHandler('cancel', this.popScene.bind(this));
}

Expand Down
1 change: 1 addition & 0 deletions www/mods/commonLibs/_common/menus/windows.js
Original file line number Diff line number Diff line change
Expand Up @@ -781,6 +781,7 @@ MATTIE.windows.Window_Tooltip.prototype.initialize = function(x=0,y=0,infoText="
this.tolerance = 13

this._infoWin = new MATTIE.windows.TextDisplay(this.x,this.y,width,infoHeight,this._infoText)
this._infoWin.opacity=255
this._infoWin.hide()
this._iconWin = new MATTIE.windows.TooltipIcon(x,y,100,100," ?")
this._iconWin.opacity=0;
Expand Down
1 change: 1 addition & 0 deletions www/mods/commonLibs/_common/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ MATTIE.util.randSeededChance = function (chance) {
return (roll <= chance);
};


/**
* @description check if a number is within +/- a tolerance to a target
* @param {*} number
Expand Down

0 comments on commit 21124fd

Please sign in to comment.