Permalink
Cannot retrieve contributors at this time
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
145 lines (126 sloc)
5.9 KB
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
/* ------------------------------------------- | |
FMOD Studio Script by Tanz0rz: | |
Export event names as Haxe constants | |
------------------------------------------- | |
*/ | |
studio.menu.addMenuItem({ | |
name: "Export Haxe Constants and Build", | |
execute: function() {displayDirectoryPickerModal()}, | |
keySequence: "Ctrl+B", | |
}); | |
const constantsFileName = "FmodConstants.hx"; | |
const cacheFileName = "CachedHaxeConstantsOutputLocation"; | |
function displayDirectoryPickerModal() { | |
var outputPathDir = readOutputPathFromFile(); | |
studio.ui.showModalDialog({ | |
windowTitle: "Select the folder with your Haxe project's Main class", | |
windowWidth: 800, | |
windowHeight: 0, | |
widgetType: studio.ui.widgetType.Layout, | |
layout: studio.ui.layoutType.VBoxLayout, | |
items: [ | |
{ | |
widgetType: studio.ui.widgetType.Layout, | |
layout: studio.ui.layoutType.HBoxLayout, | |
contentsMargins: { left: 0, top: 0, right: 0, bottom: 0 }, | |
items: [ | |
{ widgetType: studio.ui.widgetType.Spacer, sizePolicy: { horizontalPolicy: studio.ui.sizePolicy.MinimumExpanding } }, | |
{ widgetType: studio.ui.widgetType.PathLineEdit, stretchFactor: 1, widgetId: "m_directoryPicker", text: outputPathDir, pathType: studio.ui.pathType.Directory}, | |
{ widgetType: studio.ui.widgetType.PushButton, text: "Save", onClicked: function() { createConstantsFile(this); this.closeDialog(); } }, | |
], | |
}, | |
], | |
}); | |
}; | |
function createConstantsFile(directoryPickerWidget) { | |
var outputPath = directoryPickerWidget.findWidget("m_directoryPicker").text(); | |
const fullOutputPath = "{0}/{1}".format(outputPath, constantsFileName);; | |
var constantsFile = studio.system.getFile(fullOutputPath); | |
if (!constantsFile.open(studio.system.openMode.WriteOnly)) { | |
alert("Failed to open constants file for writing: " + fullOutputPath + "\n\nCheck the file is not read-only."); | |
console.error("Failed to open constants file for writing: " + fullOutputPath); | |
return; | |
} | |
console.log("Opened file: " + fullOutputPath); | |
writeFileBody(constantsFile); | |
constantsFile.close(); | |
saveOutputPathToFile(outputPath); | |
alert("Haxe constants file successfully created in:\n\n" + outputPath); | |
console.log("Haxe constants file successfully created in: " + outputPath); | |
console.log("Building banks..."); | |
studio.project.build(); | |
} | |
function readOutputPathFromFile() { | |
const haxeSystemInformationFileLocation = studio.project.filePath.substr(0, studio.project.filePath.lastIndexOf("/") + 1) + cacheFileName; | |
haxeSystemInformationFile = studio.system.getFile(haxeSystemInformationFileLocation); | |
if (!haxeSystemInformationFile.open(studio.system.openMode.ReadOnly)) { | |
return ""; | |
} | |
const fileData = haxeSystemInformationFile.readText(10000); | |
haxeSystemInformationFile.close(); | |
return fileData; | |
} | |
function saveOutputPathToFile(outputDir) { | |
const haxeSystemInformationFileLocation = studio.project.filePath.substr(0, studio.project.filePath.lastIndexOf("/") + 1) + cacheFileName; | |
haxeSystemInformationFile = studio.system.getFile(haxeSystemInformationFileLocation); | |
if (!haxeSystemInformationFile.open(studio.system.openMode.WriteOnly)) { | |
alert("Failed to open file to cache selected directory: " + haxeSystemInformationFile); | |
console.error("Failed to open file to cache selected directory: " + haxeSystemInformationFile); | |
return; | |
} | |
haxeSystemInformationFile.writeText(outputDir); | |
haxeSystemInformationFile.close(); | |
} | |
function writeFileBody(constantsFile) { | |
// Write header for file | |
constantsFile.writeText("/*\r\n DO NOT EDIT THIS FILE DIRECTLY\r\n" | |
+ " This file was generated by a script in FMOD Studio \r\n*/\r\n\r\n"); | |
constantsFile.writeText("package;\r\n\r\n") | |
// Read all events in from FMOD Studio project | |
var allEvents = studio.project.model.Event.findInstances(); | |
if (allEvents.length === 0) { | |
return; | |
} | |
// Sort events alphabetically | |
allEvents.sort(function(a, b) { | |
var pathA = a.getPath().toUpperCase(); | |
var pathB = b.getPath().toUpperCase(); | |
if (pathA < pathB) { | |
return -1; | |
} | |
if (pathA > pathB) { | |
return 1; | |
} | |
return 0; | |
}); | |
// Generate constants for music events | |
console.log("Exporting Music events") | |
constantsFile.writeText("class FmodSongs {\r\n"); | |
allEvents.forEach(function(object) { | |
// If the event starts with a number, prefix an underscore | |
// Replace any whitespace in the event path with underscores | |
const path = object.getPath().replace(/(^[0-9])/g, "_$1").replace(/ /g,"_"); | |
if (path.split('/')[1] == "Music") { | |
console.log("Path: " + path); | |
var finalSlashIndex = path.lastIndexOf('/'); | |
var eventName = path.substring(finalSlashIndex + 1); | |
constantsFile.writeText(" public static inline var " + eventName + ":String = \"" + path + "\";\r\n"); | |
} | |
}); | |
constantsFile.writeText("}\r\n\r\n"); | |
// Generate constants for sfx events | |
console.log("Exporting SFX events") | |
constantsFile.writeText("class FmodSFX {\r\n"); | |
allEvents.forEach(function(object) { | |
// If the event starts with a number, prefix an underscore | |
// Replace any whitespace in the event path with underscores | |
const path = object.getPath().replace(/(^[0-9])/g, "_$1").replace(/ /g,"_"); | |
if (path.split('/')[1] == "SFX") { | |
console.log("Path: " + path); | |
var finalSlashIndex = path.lastIndexOf('/'); | |
var eventName = path.substring(finalSlashIndex + 1); | |
constantsFile.writeText(" public static inline var " + eventName + ":String = \"" + path + "\";\r\n"); | |
} | |
}); | |
constantsFile.writeText("}\r\n"); | |
} |