Deployment Protection in RPGMV

Holy hell! - That's the words I needed to hear! - I have no idea why it didn't click in my head this whole time thinking it was literally talking about a Plugin I need to configure...
I'm commenting on the "exclude unused resources" part here, I'll leave the encryption/obfuscation part to the others.

Inside the plugin code itself, where the author defines the parameters, they need to add along with the parameter name and type:
Code:
@require 1
This will then be included even when you select "Exclude unused files". This is in the help file or if you want the full information on the 1.5.0+ plugin manager here.

If you wrote the plugin, easy-peasy you can solve the problem. If someone else wrote the plugin, you may or may not be allowed to edit it based on their terms of service.

A workaround is to create a map that the player will never visit. In this map, you can add music, images, sprites, etc. whatever needs to be in the deployed version that's missing now, and the engine will add it because now it thinks it's being "used".
I've made a plugin. What it does is, base64 encoding of every dataFile. And if you're playtesting, it will take data from the original files (because the encoded ones don't get created until you launch the game for the first time), but in the deployed game it will take data from the encoded ones. So after deployment you just delete the unencoded ones and you're good to go.
EDIT: I just realized that since MV first loads all plugins, then it launches the game, I can create the files even before the game gets launched. So I removed one if and one alias, meaning you can see any performance hits this plugin is bound to inflict by decompression even on playtest.

Also, the reason why I call it compression is simple - the LZString function is "compressToBase64" and "decompressFromBase64". 😛

EDIT: I uploaded it to my repository. https://github.com/Poryg1/RPG-maker-MV-small-plugins

Oh, that was unexpected - I'm not so accustomed to having things done for me but I shall have a look at what you've put together 🙂
And fair enough on the Compression statement - I'm just anal about definitions & terminologies, apologies. 😛


@MusicalSamurai you probably don't know it yet, but we have rules against double posting (posting right underneath yourself). You can just edit your previous post if you have more to add 🙂

Apologies, I was trying not to but the response timings have been strange and I've caught myself just finishing a response and seeing another post pop up which I feel to immediately respond to also, resulting in the doubles...
To add insult to injury, I cannot delete my duplicates even though I'm capable of copying the text from one and pasting it into a single message... This place has some strange restrictions... (I'm used to being a dev/admin on so many systems, it feels strange to be a pawn without even the ability to remove his own messages)

But I shall look out for posts before sending, and then try to include it all into a single post 🙂
(Also didn't realise this bit stays open at the bottom while switching between pages - quite handy!)


A workaround is to create a map that the player will never visit. In this map, you can add music, images, sprites, etc. whatever needs to be in the deployed version that's missing now, and the engine will add it because now it thinks it's being "used".

I have thought of this briefly, but I'm a bit OCD and the thought of an unused map with random stuff loaded on it makes me shudder; even my Debug Room is like a fully working mini-game with its own little secrets in it 😛
But yes... It is a very sensible suggestion and perhaps I will resort to your workaround if I'm somehow at a loss with defining my assets programmatically - many thanks 🙂


I uploaded it to my repository
I just took a look at your code now; I'd sniff at your naming conventions if it weren't for the amazingly concise code! - I don't think I could have written better than that, other than having slightly different variable names and I don't tend to use var but the scope you're using it in is just right! - I don't like using other people's stuff but you've definitely given me an amazing bit of insight into how this could be practically pulled off! Thank You!
(If I come up with anything different myself, I will certainly share it with you)
I could see one form of JSON protection to have a build step that converts the JSON to JS and then pre-compile to binary code to ship with the NW.js binary in the deployed version.
Also, the reason why I call it compression is simple - the LZString function is "compressToBase64" and "decompressFromBase64". 😛

EDIT: I uploaded it to my repository. https://github.com/Poryg1/RPG-maker-MV-small-plugins
@MusicalSamurai

Poryg is correct, The compressToBase64() function first encodes to Base64 then applies a RLE (run-length encoding) which isn't great compression, but at least the function name isn't misleading like XMLHttpRequest() is.

Anyways, after I got back home I basically wrote the same type of plug-in that Poryg did. To me both the similarities and differences between the two Plug-in is very interesting.

However, mine is a little more friendly/automated.

Run the Exported-game-executable one time after Deployment (RPG Maker MV -> File->Deployment) which will encode all the map and database files in the data directory. The plug-in won't touch the files during normal RPG Maker MV play testing.

However, make sure you make a backup (and examine the code before running it). I did test it with a new project, but it does delete files.

-- To error is human, to make software go off the rails requires only a typo.
EDIT:
I looked at Poryg's code again and it does indeed encode the maps as well.
However, mine is a little more friendly/automated and also encodes the map files.
Um, my plugin encodes all JSON files inside of the data folder, so that includes the map files too. What it does not include are files outside of the data folder or inside of the subfolders if you have any, because not too many plugins use these kinds of structures anyway.
Essentially the only difference between the two is, my plugin won't delete files and you have to do it manually after deployment. My first version did that, but I encountered crashes after deployment, so I had to scrap that version.
Um, my plugin encodes all JSON files inside of the data folder, so that includes the map files too.
Yeah, I didn't catch that until I looked at your for loop and saw it was iterating over the files from the directory and not the _databaseFiles[].
Sorry about that! :oops:

Had I realized fs.readdirSync() existed I could have significantly simplified my code without any drawbacks.
Well, the readdirSync apparently leads to a crash in the deployed version, so you didn't miss anything.
Hey Guys - seems the conversation has continued 😛

@MusicalSamurai
Poryg is correct, The compressToBase64() function first encodes to Base64 then applies a RLE (run-length encoding) which isn't great compression, but at least the function name isn't misleading like XMLHttpRequest()

You do have a point - although I was merely picking on the Base 64 encoding on its own, to me; isn't considered as a compression.

Good ol' misleading functions names...

Well, so far I haven't had time to implement any of the ideas here yet - but I'll be cooking up something similar to Poryg - I'm quite happy with any manual task of stripping out source files afterwards, like a 'final manual checkover' can't hurt. I'll also fully playtest it before giving it out anyway 🙂

I'm really excited about this new information, for the first time I feel there's some potential protection I can apply to my game files and my original thoguhts weren't too crazy - never expected anyone to actually respond in Code though - I am very happy - Thank you all!

Hopefully one day I can prove useful to someone else here to pass on the love! 🙂
Well, so far I haven't had time to implement any of the ideas here yet - but I'll be cooking up something similar to Poryg - I'm quite happy with any manual task of stripping out source files afterwards, like a 'final manual checkover' can't hurt. I'll also fully playtest it before giving it out anyway 🙂

I'm really excited about this new information, for the first time I feel there's some potential protection I can apply to my game files and my original thoguhts weren't too crazy - never expected anyone to actually respond in Code though - I am very happy - Thank you all!

Hopefully one day I can prove useful to someone else here to pass on the love! 🙂

I'm still thinking about it as well, it's an interesting problem. While you have a more simplistic implementation goal I'll continue with my own version with a the trade-off of more code and fewer steps for the user that can go wrong.

I've been looking through the Node.js documentation and a few functions caught my attention. I don't know how far back in RPG Maker MV support goes back for which APIs, but if it knows how to make a Promise. . .

https://nodejs.org/api/fs.html#fs_fspromises_writefile_file_data_options

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Using_promises


You might also want to make your own Developer's Plug-in.
Here are 3 functions that I'd expect to see most people use (or wish they knew about).
Code:
PIXI.utils.skipHello(); // Stops the PIXI banner from being displayed in the Console window. Documented in js/libs/pixi.js

require('nw.gui').Window.get().showDevTools(); // Displays the console window. The console grabs the focus of user input. Good if you want to use the console right away.

let win = require('nw.gui').Window.get(); // The user input focus stays with the game client. Good if you want to test the game.
win.showDevTools(
    '',
    function() {
        win.focus();
    }
);

The explanation for the last function is that if you give showDevTools a callback, it will be called after the Console has created the window and stolen input focus. You can then tell the game window to then take it back and keep input focus on the game window.
@MrNybbles All good things to know about as a developer, for sure, although I really could care less about the pixi hello message but I also generally only code and never released a game yet for me to care about it enough yet lol.

Another good one I tend to use, although not always necessary in some cases, is pausing the game when out of focus and resuming when back in focus.
PHP:
window.addEventListener('blur', () => {
  SceneManager.stop()
})

window.addEventListener('focus', () => {
  SceneManager.resume()
})

// If you want to include nw minimize and maximize listeners, this would also be possible

const win =require('nw.gui').Window.get();
win.on('maximize', function() {
  SceneManager.resume()
})

win.on('minimize', function() {
  SceneManager.stop()
})
Starting to tangent away from the original concern here lads 😛
- But I appreciate the thoughts!

I won't require any of that fancy testing stuff, I think editing and playing the good ol' way works fine for me, I rarely make any changes to my script anyway as it's more for initialisation and compacting certain things to make it more streamlined for my uses in events.

I'm just interested in obfuscating my Data/*.json files, all else is fairly trivial.

Although I do have a quick question, I am curious why I cannot use something like string.equalsIgnoreCase("something")... I may be a bit rusty but I was under the presumption that equalsIgnoreCase was under the standard library... I have to use string === "Blah" || "blah" which feels a bit clunky to me, alongside using string.toUpperCase() which also feels strange to use but doesn't throw any errors...

Thoughts on the IgnoreCase function anyone?

And...
I don't know how far back in RPG Maker MV support goes back for which APIs, but if it knows how to make a Promise. . ..
I've had very little experience with Node I'm afraid, I've heard of a Promise but I don't think I've ever needed it for anything... Gently scraped on it when trying to make a Discord Bot but other than that I'm a noob at that 'higher level' stuff, I'm very much a native Programmer kinda guy 😛 (I rarely pull in external libraries unless absolutely necessary) - I know I'm probably 'missing out' on something but Life is too busy to worry about what I might be missing out on 😀
Unfortunately there is no function equalsIgnoreCase() in Javascript( only in Java), but that don't mean you can't make it yourself.

You could put this in rpg_core.js or as a seperate plugin file and then you can use it anywhere in your game.
PHP:
String.prototype.equalsIgnoreCase = function (string) {
  return this.toLowerCase() === string.toLowerCase()
}

'myString'.equalsIgnoreCase('mystring') // returns true
@MusicalSamurai I think LTN Games' suggestion will work best for you, but I do want to mention one thing.
As long as no other code (usually plug-ins) adds a method with the exact same method that works differently than expected it would work.
In general such things are discouraged for plug-ins other people use because then the plug-ins would be altering the expected behavior of each other.
To avoid this people usually just declare a local function in an iife to protect themselves from outside interference.

Really though, it should be on the makers of the plug-ins to not step on other people's toes.
Code:
(function() {
  const function_name(lhs, rhs) {
    // Insert code here.
  }
})();

Regarding the FS Promise API, as soon as I tried 'require('fs').promises' I got back 'undefined'.
The good news is that after rewriting my code it resolved the problem of having not written the encrypted files yet, probably due to using Sync functions.

Regarding why I use PIXI.utils.skipHello(), it is because I use the console quite a bit to understand the current state of the game and the PIXI banner is eye-catching to say the least.

For example, typing '$dataMap' in gives me the state of the current map (which can be read or changed). Typing 'nw.Screen.screens' gets me an array with my actual screen information. If you look in rpg_Managers.js you will see the global names for all the databases.
Unfortunately there is no function equalsIgnoreCase() in Javascript( only in Java)

Ah.. Of course - I bounce between so many languages I get confused what can do what... See a lesson to learn from my stupidity - pay attention to what language you're using! 😀

@MusicalSamuraiAs long as no other code (usually plug-ins) adds a method with the exact same method that works differently than expected it would work.
In general such things are discouraged for plug-ins other people use because then the plug-ins would be altering the expected behavior of each other.
To avoid this people usually just declare a local function in an iife to protect themselves from outside interference.

Ah yes, I am familiar with IIFE's and will be considering that format if ever to make a plugin that's to be flexible or perhaps to benefit other people - in this sense though, my 'plugin' is mainly for defining new functions or overrides and isn't something that's built to benefit anything else other than the game it was crafted for...
Anyhow, 'nw.Screen.screens' sounds useful and of course checking out the core files is always a good shout too 🙂
@MusicalSamurai check out this program that I made in Node js:

It doesn't make your game hack proof, but actually protects your JS and JSON files from being stolen or modified, this is because they're obfuscated and not encrypted.
Obfuscation makes a Javascript code unreadable to humans, while encryption can easily be decrypted and you get the original code back.

The program might not work 100% as I've noticed there are some strange behaviors with folders, but any problem you get just post in that thread.

Original thread: https://forums.rpgmakerweb.com/threads/deployment-protection-in-rpgmv.115027/