How to write a PHP utility for separating the TXT file generated by ShowMyCode into individual class files.
Everybody makes mistakes. We certainly did when we forgot to commit our changes to SVN and lost about two days of work on a Flash project recently! We had an up-to-date published SWF of the document, but we needed to make additional changes and our ActionScript source code was two days behind.
One way to rectify the mistake was to manually redo all our recent work to bring our last saved copy up-to-date. But who wants to do that? Not me!
This is where ShowMyCode.com saved the day. Show My Code is a free web-based SWF decompiler. You upload a SWF, and voila, it spits out the ActionScript source. Show My Code did a great job of recovering the source code for this particular project and saved us from redoing a lot of work.
However if I can make one criticism of ShowMyCode it is that all of the ActionScript is exported into one TXT file instead of into the correct file structure of one class per AS file.
That might not be a problem with very small projects, but in this case there were 67 different classes / interfaces all listed in one file. I am not a fan of copy/paste work. I’m a developer not a machine. Maybe for two or three items, but 67… no way!
So I wrote a little PHP utility which will divide the TXT file generated by ShowMyCode into individual classes. It will create the appropriate file structure defined by the package of each class, and save each class as an AS file in the correct directory. It uses a regular expression to make sense of the TXT file.

Some of the 4700 lines of code exported by ShowMyCode into one file, next to the clean directory structure generated by our PHP utility.
Here’s the source code and of course its free and public and you can do whatever you want with it.
input = $input;
$this->output = $output;
}
public function decompile() {
if (!is_dir($this->output)) {
// Create the output directory
echo 'Creating output directory: '.$this->output."n";
if (!mkdir($this->output, 0777, true)) {
echo 'Error: could not create the output directory: '.$this->output;
exit();
}
chmod($this->output, 0777);
} else {
// Clear the output directory (if the tool has already run)
echo 'Clearing output directory: '.$this->output."n";
$this->clearDirectory($this->output);
}
// Read input text file
echo 'Reading input text file: '.$this->input."n";
$handle = fopen($this->input, 'r') or die('Could not open '.$this->input);
$contents = fread($handle, filesize($this->input));
fclose($handle);
// Split the text into an array of classes / interfaces by using a regular expression
echo 'Split the text into an array of classes / interfaces'."n";
$reg_exp = '|(packages){1}([^s]*)(.*?)(public){1}([a-zs]*)([^s]*)(.*?)(//package){1}|s';
preg_match_all($reg_exp, $contents, $matches, PREG_SET_ORDER);
echo 'Found '.count($matches).' classes / interfaces'."nn";
// Loop through the matches
foreach ($matches as $val) {
$package = $val[2];
$type = trim($val[5]); // class, dynamic class, interface, etc
$class = $val[6];
$filename = $this->output.'/'.str_replace('.', '/', $package).'/'.$class.'.as';
$source = $val[1].$val[2].$val[3].$val[4].$val[5].$val[6].$val[7];
// Ensure the right directory structure for this package exists
$this->createDirectoryForPackage($package, $this->output);
// Create the file
echo 'Create a file for the '.$type.': '.$filename."n";
$output_file_handle = fopen($filename, 'w');
fwrite($output_file_handle, $source);
fclose($output_file_handle);
}
}
/**
* Create a directory structure for a package, e.g "com.thesedays.project.ui" becomes com/thesedays/project/ui
* @param String $package
* @param String $base The inital directory
*/
private function createDirectoryForPackage($package, $base = '') {
$dirs = explode(".", $package);
for ($i=0; $iclearDirectory($path);
rmdir($path);
} else {
unlink($path);
}
}
}
closedir($handle);
}
}
}
// Example usage
header('Content-type: text/plain');
$decompiler = new SWFDecompiler('ShowMyCode.com.txt', 'output');
$decompiler->decompile();
?>
Haven’t used the site, but awesome tool to break it back up!
Hello,
It seems ShowMyCode uses ASV as the decompiler on server side. (While this currently does not violate license terms, we will most probably change it in the near future, and require at least a link).
For AS3 decompiling, you can check out AS3 Sorcerer, http://www.as3sorcerer.com , very affordable, and decompiling is fully enabled for evaluation.
Thanks.
Best regards,
Mehmet, Manitu Group