mirror of
https://github.com/BlackMATov/unity-flash-tools.git
synced 2025-12-16 14:11:19 +07:00
1 line
6.2 KiB
JavaScript
1 line
6.2 KiB
JavaScript
// ------------------------------------
|
|
// JS functions
|
|
// ------------------------------------
|
|
|
|
if ( !String.prototype.format ) {
|
|
String.prototype.format = function() {
|
|
var args = arguments;
|
|
return this.replace(/{(\d+)}/g, function(match, number) {
|
|
return typeof args[number] != 'undefined' ? args[number] : match;
|
|
});
|
|
};
|
|
}
|
|
|
|
// ------------------------------------
|
|
// FlashTools
|
|
// ------------------------------------
|
|
|
|
var FlashTools = function() {
|
|
};
|
|
|
|
// ------------------------------------
|
|
// Common functions
|
|
// ------------------------------------
|
|
|
|
FlashTools.prototype.Trace = function(text) {
|
|
fl.outputPanel.trace(text);
|
|
};
|
|
|
|
FlashTools.prototype.TraceError = function(text) {
|
|
this.Trace("!!!Error!!!: " + text);
|
|
};
|
|
|
|
FlashTools.prototype.Assert = function(expr, msg) {
|
|
if ( !expr ) {
|
|
throw msg != undefined ? "Assert! " + msg : "Assert!";
|
|
}
|
|
};
|
|
|
|
FlashTools.prototype.ClearOutput = function() {
|
|
fl.outputPanel.clear();
|
|
};
|
|
|
|
FlashTools.prototype.EscapePath = function(path) {
|
|
return path.replace(/ /g, '%20');
|
|
};
|
|
|
|
FlashTools.prototype.CombinePath = function(lhs, rhs) {
|
|
return this.EscapePath(lhs) + this.EscapePath(rhs);
|
|
};
|
|
|
|
// ------------------------------------
|
|
// Clip item functions
|
|
// ------------------------------------
|
|
|
|
FlashTools.prototype.ClipItem_TraceInfo = function(item) {
|
|
this.Trace("\tName : " + item.name);
|
|
this.Trace("\tExportFilename : " + this.ClipItem_GetExportFilename(item));
|
|
};
|
|
|
|
FlashTools.prototype.ClipItem_GetExportFilename = function(item) {
|
|
return item.name;
|
|
};
|
|
|
|
// ------------------------------------
|
|
// Bitmap item functions
|
|
// ------------------------------------
|
|
|
|
FlashTools.prototype.BitmapItem_TraceInfo = function(item) {
|
|
this.Trace("\tName : " + item.name);
|
|
this.Trace("\tExportFilename : " + this.BitmapItem_GetExportFilename(item));
|
|
};
|
|
|
|
FlashTools.prototype.BitmapItem_GetExportFilename = function(item) {
|
|
var export_filename = item.name.replace(/\//g, '_');
|
|
var regex_has_png_ext = /\.png$/i;
|
|
return regex_has_png_ext.test(export_filename)
|
|
? export_filename
|
|
: export_filename + ".png";
|
|
};
|
|
|
|
// ------------------------------------
|
|
// Document functions
|
|
// ------------------------------------
|
|
|
|
FlashTools.prototype.Document_TraceInfo = function(document) {
|
|
this.Trace("\tName : " + document.name);
|
|
this.Trace("\tPath : " + this.Document_GetPath(document));
|
|
this.Trace("\tExportFolder : " + this.Document_GetExportFolder(document));
|
|
};
|
|
|
|
FlashTools.prototype.Document_GetPath = function(document) {
|
|
return this.EscapePath(document.pathURI);
|
|
};
|
|
|
|
FlashTools.prototype.Document_GetExportFolder = function(document) {
|
|
return this.Document_GetPath(document) + "_export/";
|
|
};
|
|
|
|
FlashTools.prototype.Document_GetBitmapExportFolder = function(document) {
|
|
return this.Document_GetExportFolder(document) + "bitmaps/";
|
|
};
|
|
|
|
FlashTools.prototype.Document_ExitEditMode = function(document) {
|
|
for ( var i = 0; i < 100; ++i ) {
|
|
document.exitEditMode();
|
|
}
|
|
};
|
|
|
|
FlashTools.prototype.Document_PrepareExportFolder = function(document) {
|
|
var export_folder = this.Document_GetExportFolder(document);
|
|
if ( FLfile.exists(export_folder) ) {
|
|
if ( !FLfile.remove(export_folder) ) {
|
|
throw "Can't remove document export folder ({0})!"
|
|
.format(export_folder);
|
|
}
|
|
}
|
|
if ( !FLfile.createFolder(export_folder) ) {
|
|
throw "Can't create document export folder ({0})!"
|
|
.format(export_folder);
|
|
}
|
|
var bitmap_export_folder = this.Document_GetBitmapExportFolder(document);
|
|
if ( !FLfile.createFolder(bitmap_export_folder) ) {
|
|
throw "Can't create bitmap export folder ({0})!"
|
|
.format(bitmap_export_folder);
|
|
}
|
|
};
|
|
|
|
FlashTools.prototype.Document_ExportLibrary = function(document) {
|
|
this.Document_ExitEditMode(document);
|
|
var export_folder = this.Document_GetExportFolder(document);
|
|
var bitmap_export_folder = this.Document_GetBitmapExportFolder(document);
|
|
|
|
var xml_content = "<Library>\n";
|
|
|
|
for ( var i = 0; i < document.library.items.length; ++i ) {
|
|
var item = document.library.items[i];
|
|
if ( item.itemType == "folder" ) {
|
|
// nothing
|
|
} else if ( item.itemType == "bitmap" ) {
|
|
var item_filename = this.BitmapItem_GetExportFilename(item);
|
|
var item_export_path = this.CombinePath(bitmap_export_folder, item_filename);
|
|
if ( !item.exportToFile(item_export_path) ) {
|
|
throw "Can't export bitmap asset ({0})!"
|
|
.format(item_export_path);
|
|
}
|
|
xml_content +=
|
|
"\t<Asset name='{0}' type='{1}' filename='{2}'/>\n"
|
|
.format(item.name, item.itemType, item_filename);
|
|
} else if ( item.itemType == "graphic" || item.itemType == "component" || item.itemType == "movie clip" ) {
|
|
var item_filename = this.ClipItem_GetExportFilename(item);
|
|
var item_export_path = this.CombinePath(export_folder, item_filename);
|
|
xml_content +=
|
|
"\t<Asset name='{0}' type='{1}' filename='{2}'/>\n"
|
|
.format(item.name, item.itemType, item_filename);
|
|
} else {
|
|
throw "Unsupported library item type ({0})!"
|
|
.format(item.itemType);
|
|
}
|
|
}
|
|
|
|
xml_content += "</Library>";
|
|
var library_path = this.CombinePath(export_folder, "library.xml");
|
|
if ( !FLfile.write(library_path, xml_content) ) {
|
|
throw "Can't create library xml ({0})!"
|
|
.format(library_path);
|
|
}
|
|
};
|
|
|
|
// ------------------------------------
|
|
// Convert functions
|
|
// ------------------------------------
|
|
|
|
FlashTools.prototype.ConvertAll = function() {
|
|
var documents = fl.documents;
|
|
for ( var i = 0; i < documents.length; ++i ) {
|
|
this.ConvertOne(documents[i]);
|
|
}
|
|
};
|
|
|
|
FlashTools.prototype.ConvertOne = function(document) {
|
|
this.Trace("-= Convert document start =-");
|
|
try {
|
|
this.Document_TraceInfo(document);
|
|
this.Document_PrepareExportFolder(document);
|
|
this.Document_ExportLibrary(document);
|
|
this.Trace("-= Convert document finish =-");
|
|
} catch ( e ) {
|
|
this.Trace("-= Convert document error =- : " + e);
|
|
}
|
|
};
|
|
|
|
// ------------------------------------
|
|
// Test functions
|
|
// ------------------------------------
|
|
|
|
FlashTools.prototype.CommonTest = function(func, func_name) {
|
|
if ( !func() ) {
|
|
this.Trace("Test error: " + func_name);
|
|
}
|
|
};
|
|
|
|
FlashTools.prototype.Test0 = function() {
|
|
return true;
|
|
};
|
|
|
|
FlashTools.prototype.RunTests = function() {
|
|
this.CommonTest(this.Test0, "Test0");
|
|
};
|
|
|
|
// ------------------------------------
|
|
// Run
|
|
// ------------------------------------
|
|
|
|
var ft = new FlashTools();
|
|
ft.ClearOutput();
|
|
ft.RunTests();
|
|
ft.ConvertAll(); |