Created
January 4, 2016 10:15
-
-
Save McTwist/e32fc7642c99316d0f9d to your computer and use it in GitHub Desktop.
Compresses a TorqueScript file to a smaller size but can still run
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// One file only | |
// Does not optimize | |
function Compress(%from, %to) | |
{ | |
%obj = new FileObject(); | |
%obj.openForRead(findFirstFile(%from)); | |
%chars = "abcdefghijklmnopqrstuvwxyz"; | |
%current = 0; | |
deleteVariables("$va*"); | |
while (!%obj.isEOF()) | |
{ | |
%line = %obj.readLine(); | |
// Remove comments | |
%comment = 0; | |
while ((%comment = strpos(%line, "//", %comment)) >= 0) | |
{ | |
if (!withinString(%line, %comment)) | |
{ | |
%line = getSubStr(%line, 0, %comment); | |
continue; | |
} | |
%comment++; | |
} | |
%func = -1; | |
while (withinString(%line, %func = stripos(%line, "function", %func + 1))){} | |
%first = 0; | |
// Find and replace local variables | |
while ((%first = strpos(%line, "%", %first)) >= 0) | |
{ | |
// Inside a string | |
if (withinString(%line, %first)) | |
{ | |
%first++; | |
continue; | |
} | |
// Find end of valid variable | |
for (%last = %first+1; %last < strlen(%line) && stripos(%chars @ "0123456789", getSubStr(%line, %last, 1)) >= 0; %last++){} | |
// Operator | |
if (%first >= %last-2) | |
{ | |
%first++; | |
continue; | |
} | |
%name = getSubStr(%line, %first, %last - %first); | |
// Reset local variable span if function found | |
if (%func >= 0 && %func < %first) | |
{ | |
while (withinString(%line, %func = stripos(%line, "function", %func + 1))){} | |
%current = 0; | |
deleteVariables("$va*"); | |
} | |
// New | |
if (strlen($va[%name]) == 0) | |
{ | |
$va[%name] = getSubStr(%chars, %current, 1); | |
%current++; | |
} | |
//%len = strlen(%line); | |
//%first = %first++ - (%len - strlen(%line)); // Go back some steps to find new position | |
%first = 0; // Go back some steps to find new position | |
%line = strreplace(%line, %name, "%" @ $va[%name]); | |
} | |
%compressed = %compressed NL %line; | |
} | |
// Remove spaces | |
%compressed = removeWhitespace(" ", %compressed); | |
%compressed = removeWhitespace("\t", %compressed); | |
%compressed = removeWhitespace("\n", %compressed); | |
%obj.delete(); | |
%obj = new FileObject(); | |
%obj.openForWrite(%to); | |
%obj.writeLine(%compressed); | |
%obj.delete(); | |
deleteVariables("$va*"); | |
} | |
// Checks if a position is within a string | |
function withinString(%text, %pos) | |
{ | |
%length = strlen(%text); | |
// Begin | |
while (%first < %length && (%first = strpos(%text, "\"", %first)) >= 0 && %first < %pos) | |
{ | |
%second = %first+1; | |
while (%second < %length && (%second = strpos(%text, "\"", %second)) >= 0 && %first < %pos) | |
{ | |
%escaped = 0; | |
// Avoid escaped | |
for (%it = %second-1; getSubStr(%text, %it, 1) $= "\\"; %it--) | |
%escaped = !%escaped; | |
if (%escaped) | |
{ | |
%second++; | |
continue; | |
} | |
// Is inside | |
if (%first < %pos && %second > %pos) | |
{ | |
return true; | |
} | |
%first = %second+1; | |
break; | |
} | |
} | |
return false; | |
} | |
// Remove whitespace | |
function removeWhitespace(%type, %text) | |
{ | |
%length = strlen(%text); | |
%specials = "!^\"'$@,.(){}[];=?:-+*/%<>&|\t\r\n "; | |
for (%it = 0; %it < %length; %it++) | |
{ | |
%char = getSubStr(%text, %it, 1); | |
if (%inside || %char !$= %type || (%char $= %type && %it > 0 && (strpos(%specials, getSubStr(%text, %it-1, 1)) == -1 && strpos(%specials, getSubStr(%text, %it+1, 1)) == -1))) | |
%result = %result @ %char; | |
// String | |
if (%char $= "\"") | |
{ | |
if (%inside) | |
{ | |
%escaped = 0; | |
// Avoid escaped | |
for (%nt = %it-1; getSubStr(%text, %nt, 1) $= "\\"; %nt--) | |
%escaped = !%escaped; | |
if (%escaped) | |
continue; | |
} | |
%inside = !%inside; | |
} | |
} | |
return %result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment