****************************************
*                                      *
*  Wc3 Text File Optimizer 2.0         *
*                                      *
****************************************

 The usual 3rd party program warning:
 
  THIS PROGRAM IS DISTRIBUTED "AS IS".  NO WARRANTY OF ANY KIND IS EXPRESSED
  OR IMPLIED. YOU USE AT YOUR OWN RISK. THE AUTHOR WILL NOT BE LIABLE FOR 
  DATA LOSS, DAMAGES, LOSS OF PROFITS OR ANY OTHER KIND OF LOSS WHILE USING
  OR MISSUSING THIS SOFTWARE.

 What it does?
 
   Is able to optimize the strings file, the map script and/or the map mpq itself to save space. Includes plenty of Script Obfuscation methods that can be really useful to save space, improve execution and may work as an effective anti unprotector method.

   Note that this program wasn't really designed for map protection, it has the option to remove the files used only by the world editor, but that's only to save space, it is not the author's fault that world editor isn't able to open maps that warcraft iii can without problems, in case you don't want to "protect" the map, don't use the Remove Useless files option or use the "Allow map preview" option.

   It is true that the script obfuscation options included in this program, proved to be really effective against wtg reconstructors, but that isn't their main objective, their objective is to improve performance and save space, It is not the author's fault if map unprotectors are unable to beat them. Anyone with a basic and kind of cheap programming knowledge and plenty of time to waste, may be able to make a program that "unprotect" maps with the obfuscation methods included, though that program doesn't exist at this moment.

 How to use it?
 
   Open the exe file, then click the Open File... button and find your map, then use Save Optimized as... to save the optimized version of the map, before doing that you can choose what extra obfuscation methods you'd like.

   For Security reasons, it is impossible to save it as the same file you gave the program, this is to ensure you have a backup of the file you optimized.

 Map Optimization Options:
 
   * Strong Zlib Deflate Compression, will recompress the files listed in the mpq using the ZLIB deflate compression, should save some Kbytes.

   * Buffer Size: Will REBUILD the entire map mpq using a new buffer size, the biggest the buffer size is the more ram the game will require to decompress it and the least space the map will need, Blizzard's favorite buffer size is 4096 bytes, and the recomended one is 65536 bytes, bigger buffer sizes may work, though they might be risky but they are worth a shot none the less. 

   NOTE: if you choose this option, the new Map file will MISS any archive that wasn't listed in the listfile.

   NOTE 2: I found that small maps may actually grow in size with this option and some high buffer sizes.

   * Look for other scripts to obfuscate: Will check all of the files listed in the listfile that have the .j or .ai extensions, then will remove comments and useless spaces from them, This is useful when you have a custom blizzard.j or an ai script in your map.

   * Remove Useless Files : Some people call this map protection, will remove all the files that are only needed by the world excepting war3mapunits.doo to save space, war3mapunits.doo is not deleted because if that file is missing the map will be unopenable by the world editor.

   * Also Remove Unit Data : Will Remove war3mapunits.doo (the file that includes the positions and options of the units in the map for world editor) , leaving the map unopeneable by world editor, and saving space, if 'allow map preview' is enabled, it will create a fake war3mapunits.doo and replace the one in your map with it.

   * Allow Map Preview : Will Replace the map's trigger data with a custom one that just has a comment with the info you write in the text boxes, this will allow you to include contact information, the url of the open source version / downloadable files that include your stuff, or whatever you want a guy that opens the world editor read. If "Also Remove Unit Data" is enabled, this will also create a fake war3mapunits.doo to leave the map openeable.

    If you are the kind of person that updates its map very often, you can use the [...] button that should be found in bottom right of the textbox to load the comment contents from a text file. (The first 2 lines of the text file would go to category and name.

   If Category or Name are blank, this will remove the wtg and wct files instead of making a new one, this is because otherwise it would corrupt the file.

 Extra Obfuscating Methods
 
   * Remove Useless code: The way world editor converts the GUI "triggers" into JASS is kind of bad, and would leave some unnecessary code like else statements just before an endif , or ==true which is completelly worthless or calling the DoNothing function which does nothing, this will just remove that stuff from the script.

   * Anti BJ : Will replace most of Blizzard.j functions and constants with native / number counterparts, this process has various intentions:
       - It improves map speed, cause the triggers will use less calling times because of the replacement of the functions / constants (don't expect a huge improvement though)
       - It saves up some space (not too much, but always helps).

    NOTE: You shouldn't use this if you are using a custom blizzard.j file or you are going to obfuscate a custom blizzard.j file.

   * Merge Initialization functions: Will merge all the functions called by main and config:
       - The functions that get mixed are the global initializations, the sound creation, the rect creation, the unit creation and the trigger setup.
       - It saves some space and loading time (not too much, but always helps)

      NOTE: This function is completelly designed for maps.

    It will even work with functions that have local declarations, excepting some few cases , where two of the functions have 2 local variables with the same name but with different type (it will still work in case both have the same name and same type).

   * Merge Global declarations with initializations: When world editor saves the map, it declares the global variables in a place, and makes a separated function to initialize them (giving them initial values), but in JASS it is possible to initialize non-arrays at the moment of declaring the variable, this obfuscation will move the initializations of non-array global variables, rects and cameras to the globals section, would save some map space.

   * Merge Array Initializations: World Edit makes it so arrays are initialized with some loops, one loop per array, but it is normal that most of the arrays in a map will have the same starting index, this will put all the arrays that use the same starting index into the same loop, saving some worthless space and little loading time.

   * Conditions: World Editor Loves to create a lot of code for trigger conditions, it makes the script easier to read, but in game they are completelly worthless, this method will reconfigure the map's conditions to use the least space possible.

   * Purge Constant functions : This is not as important as the other ones, unless you have quite a good quantity of spells like mine that overuse constant functions. This will replace the calls to them with their values, currently it is only for 'takes nothing' functions, but sooner will do with every function.

 What to do if the optimized map doesn't work?
 
In the weird case the map doesn't work, I suggest these debugging steps:

 1) Weird Buffer size value? If you were using a buffersize different than 4096 or 65536, try disabling the buffer size option, and see if the map works now, in that case it was caused by the buffer size, please notiffy me about it.

 2) Try Optimizing the map without using any extra map script obfuscation. If the new map works, then the problem was caused by one of the obfuscation methods. In that case, Try with different combinations of obfuscation options, I recomend trying with all the obfuscation methods enabled excepting the last three which have the most chances of malfunctioning, the merge and anti bj methods are often enough. Now that we know it was caused by the obfuscation methods, I will really appreciate if you send me your map's map script file (I only need the map script).

 3) In case it doesn't work even without the extra obfuscation methods, try disabling all of the extra map optimization methods, in case the map works now report it to me, and If possible send me your map so I can debug this program.

 Error Messages:
 
 - I/O Exception, unable to open file, and similars, The file you want to replace is currently opened by another program, or your disk is damaged.

 - Too Much lines on file : Certain obfuscation methods have a limit of JASS lines in the file, the actual value is 30000 lines, I don't think a map script with more lines than 30000 will ever appear, but in case your map script has more than 30000 lines, tell me so I can increase the value (thus making the program use more memory)

 - Merge Error: Function to merge never ends! : the map script, either because of the original file or may be the obfuscation methods made before, doesn't have its correspondant endfunction, If you are using a map script this shouldn't happen, in that case I'll ask you to send me your map script (just the script not the map) , to see what is happening.

 - Other error messages: Please report them.

 Final Notes:
 
 - The program will need disk write access on the directory it is placed, in other words don't execute this from a Write Protected Disk.

 - You can associate map, and map script files with this program now, in case you want to associate string files with it too use the -wts argument before the %1

 - This is not a 'map protection' program, its objective is to decrease map downloading time and to improve the script's execution. Even though some of its obfuscation methods proved to beat automatic map unprotectors, the point of this program is to optimize the map, and if it leaves the map unopeneable by World Editor while the game can play it correctly, it is actually World Editor's fault, The Allow map preview option was added to prevent the map to be unopenable, and you are even able to put an url to the source there, but still delete the stuff that is only needed by the world editor.

 - The program comes with the source code, for learning purposes, you should use Delphi to open it, you may update the program as long as you share the source of the updated stuff to me, and don't forget my name on the credits of your new tool.

 Credits and special thanks
 
 Program Author: Vexorian
 Uses the wonderful SFMPQ.DLL Library by ShadowFlare

 Special thanks to:
 - AIAndy for keeping updating pjass which helps testing and finding bugs with the obfuscation methods.
 - Pitzermike and StarcraftFreak for the huge help they gave me about mpq manipulation.
 - Zoar for telling me about the fact that blizzard uses UTF8
 - Karukef for sending me his map's script allowing me to find a bug in the conditions obfuscation.
 - ArcanisteR for helping me find and fix a bug with anti BJ
 
 The Ultimate Packer for eXecutables :
          Copyright (c) 1996-2002 Markus Oberhumer & Laszlo Molnar
                       http://upx.sourceforge.net
 In the future :
 
   - Fix the language issues after all, (I need help of people with maps that use foreign characters)
   - More Obfuscation methods will keep getting released.
   - Make Anti BJ 2, that will also replace the functions that just swap the arguments' positions.
   - A strings file optimization option will be added (will delete editor suffixes and manage duplicated strings)

   - Detect buffer size

 Version History
 
 2.0 :
 - My logic was flawed, again, figured that a function that has a return statement shouldn't be merged, fixed that now.
 - Another Flawed logic issue, it can't just add a -1 to the arguments because it would screw becase * has priority in the evaluation order, if you were having problems in certain triggers and you love using Convert Player to index and the convert index to player, then the cause of that issue was fixed.
 - Fixed another bug with AntiBJ, there was a slight chance that it wouldn't change certain functions.
 - Fixed another problem, the merge array initializations method wasn't actually working if the merge functions option was enabled, now its fixed.

 - Added the Purge constant functions method.

 1.9 :
 - My logic was flawed, I found a big problem with the way I was merging functions with locals, now the initial value of the local variable would be set in the right place (putting it in the declaration would cause problems in certain situations)
 - Fixed a rather small frozen time before starting the global merge method.
 - The merge initialization functions method got a major speed boost.
 - Other methods excepting the Merge Array Initializations and the conditions obfuscation were improved speed wise too.

 1.8 :
 - Fixed a bug that was causing the merge Initialization functions option to make the map unplayable under certain circunstances.
 - Fixed various bugs in the merge Initialization functions options when strings had carriage returns.
 - The "merge global initializations with declarations" options got a major speed boost, used to be the slowest method and now is the fastest, similar improvements will be done with the other methods.
 - Fixed some stuff in the readme file (this file)

 1.7 :
 - The function merge methods now are able to merge functions that use local variables, excepting some weird cases.
 - The function merging methods were all merged into one option, this is because they all use the same function afterall, so if one is bugged all the other ones will too, and it will save you 2 clicking times.
 - More functions are merged, at this time it will end with a gigantic main function that contains everything from global initializations to triggers, including unit creation.
 - Maps that use the Remove weather effect action, can now use Anti BJ without problems, in other words I fixed the bug with Anti BJ.

 1.6 :
 - Fixed a bug with the conditions obfuscation method, thanks Karukef for the feedback that helped finding it.
 - Fixed problems with "foreign" characters , thanks a lot to Zoar for this.
 - Reorganized the form so the status label has more space to show information.
 - Hopefully no longer leaves any temp file.

 1.5 :
 - Now you can open map files directly, and the program will automatically fix the strings and obfuscate the script of the map.
 - Also added some map optimization options like changing the buffer size, forcing zlib deflate, and removing useless files, you can get really incredibly file size compression now.
 - The Interface was changed somehow now that it can open maps too.
 - Now the program is called wc3 map optimizer.
 - Script and Wts file fixing without changing the map are still possible when you open that kind of files.
 - Global initialization merge was fixed due to a lot of glitches that certain types cause, even if the map compiled well, now it would only merge certain types or any variable that is initialized with null.
 - Global initialization merge also works with sounds now.
 - Updated AntiBJ, does its work with more functions, and the new BJ dummies that came with 1.17
 - Added The Conditions Obfuscation Method.
 - You can now associate script files or maps with this program.
 - Temporal files are now created on the same directory of this program.

 1.1 :
 - Added the "Merge Global Declarations with initializations" obfuscation method.
 - Added the "Merge Array Initializations" obfuscation method.
 - Added the "Remove useless code" optimization method.

 - Added a check/uncheck all check box, since the options are already too many.

 0.9 :
 - JASS script obfuscating no longer ignores tabs.
 - Added 3 new obfuscation methods based in merging the initialization functions into a giant one.
 - Array brackets are now considered operators as they are supposed to be, so the problem with stuff inside array brackets not being fixed is solved.
 - Fixed a bug in the Anti BJ, was causing it to fix strings instead of non strings after certain circunstances.
 - Anti BJ no longer leaves a temp file.
 - Anti BJ now also replaces the GUI get player number with the native one +1 (so it takes less space)
 - String File Fixer now makes sure it is not removing lines inside the strings.

 - The program now tells you how much the file size was reduced.
 - Added a progress bar, new obfuscating methods were too time consuming for me.

 0.3 :
 - Added the Anti BJ obfuscation method, still at beta stage so it can ruin your map, my tests of the method didn't show problems though.

 0.2 :
 - I recently discovered that strings might have a weird behaviur in JASS syntax, and this program wasn't aware of that either, just fixed it, so there aren't bugs with strings that have carriage returns inside.          

 0.1 :  (first version)