2022-03-08 03:13:53 -05:00
|
|
|
package funkin.modding;
|
|
|
|
|
2024-03-23 15:34:37 -04:00
|
|
|
import funkin.data.dialogue.conversation.ConversationRegistry;
|
|
|
|
import funkin.data.dialogue.dialoguebox.DialogueBoxRegistry;
|
|
|
|
import funkin.data.dialogue.speaker.SpeakerRegistry;
|
2024-03-05 02:29:44 -05:00
|
|
|
import funkin.data.event.SongEventRegistry;
|
2024-03-23 15:34:37 -04:00
|
|
|
import funkin.data.story.level.LevelRegistry;
|
2024-03-05 02:29:44 -05:00
|
|
|
import funkin.data.notestyle.NoteStyleRegistry;
|
|
|
|
import funkin.data.song.SongRegistry;
|
|
|
|
import funkin.data.stage.StageRegistry;
|
2024-03-23 15:34:37 -04:00
|
|
|
import funkin.data.freeplay.album.AlbumRegistry;
|
2024-03-05 02:29:44 -05:00
|
|
|
import funkin.modding.module.ModuleHandler;
|
2024-02-07 18:45:13 -05:00
|
|
|
import funkin.play.character.CharacterData.CharacterDataParser;
|
2023-10-17 00:38:28 -04:00
|
|
|
import funkin.save.Save;
|
2024-03-05 02:29:44 -05:00
|
|
|
import funkin.util.FileUtil;
|
|
|
|
import funkin.util.macro.ClassMacro;
|
|
|
|
import polymod.backends.PolymodAssets.PolymodAssetType;
|
|
|
|
import polymod.format.ParseRules.TextFileFormat;
|
|
|
|
import polymod.Polymod;
|
2022-03-08 03:13:53 -05:00
|
|
|
|
2024-03-05 02:29:44 -05:00
|
|
|
/**
|
|
|
|
* A class for interacting with Polymod, the atomic modding framework for Haxe.
|
|
|
|
*/
|
2022-03-08 03:13:53 -05:00
|
|
|
class PolymodHandler
|
|
|
|
{
|
2023-01-22 19:55:30 -05:00
|
|
|
/**
|
|
|
|
* The API version that mods should comply with.
|
2023-07-17 21:54:58 -04:00
|
|
|
* Format this with Semantic Versioning; <MAJOR>.<MINOR>.<PATCH>.
|
2023-01-22 19:55:30 -05:00
|
|
|
* Bug fixes increment the patch version, new features increment the minor version.
|
|
|
|
* Changes that break old mods increment the major version.
|
|
|
|
*/
|
2024-03-05 02:29:44 -05:00
|
|
|
static final API_VERSION:String = '0.1.0';
|
2023-01-22 19:55:30 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Where relative to the executable that mods are located.
|
|
|
|
*/
|
2024-03-05 02:29:44 -05:00
|
|
|
static final MOD_FOLDER:String =
|
|
|
|
#if (REDIRECT_ASSETS_FOLDER && macos)
|
|
|
|
'../../../../../../../example_mods'
|
|
|
|
#elseif REDIRECT_ASSETS_FOLDER
|
|
|
|
'../../../../example_mods'
|
|
|
|
#else
|
|
|
|
'mods'
|
|
|
|
#end;
|
|
|
|
|
|
|
|
static final CORE_FOLDER:Null<String> =
|
|
|
|
#if (REDIRECT_ASSETS_FOLDER && macos)
|
|
|
|
'../../../../../../../assets'
|
|
|
|
#elseif REDIRECT_ASSETS_FOLDER
|
|
|
|
'../../../../assets'
|
|
|
|
#else
|
|
|
|
null
|
|
|
|
#end;
|
2023-01-22 19:55:30 -05:00
|
|
|
|
2024-03-05 02:29:44 -05:00
|
|
|
/**
|
|
|
|
* If the mods folder doesn't exist, create it.
|
|
|
|
*/
|
|
|
|
public static function createModRoot():Void
|
2023-01-22 19:55:30 -05:00
|
|
|
{
|
|
|
|
FileUtil.createDirIfNotExists(MOD_FOLDER);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Loads the game with ALL mods enabled with Polymod.
|
|
|
|
*/
|
2024-03-05 02:29:44 -05:00
|
|
|
public static function loadAllMods():Void
|
2023-01-22 19:55:30 -05:00
|
|
|
{
|
|
|
|
// Create the mod root if it doesn't exist.
|
|
|
|
createModRoot();
|
2024-03-05 02:29:44 -05:00
|
|
|
trace('Initializing Polymod (using all mods)...');
|
2023-01-22 19:55:30 -05:00
|
|
|
loadModsById(getAllModIds());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Loads the game with configured mods enabled with Polymod.
|
|
|
|
*/
|
2024-03-05 02:29:44 -05:00
|
|
|
public static function loadEnabledMods():Void
|
2023-01-22 19:55:30 -05:00
|
|
|
{
|
|
|
|
// Create the mod root if it doesn't exist.
|
|
|
|
createModRoot();
|
|
|
|
|
2024-03-05 02:29:44 -05:00
|
|
|
trace('Initializing Polymod (using configured mods)...');
|
2024-03-04 16:37:42 -05:00
|
|
|
loadModsById(Save.instance.enabledModIds);
|
2023-01-22 19:55:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Loads the game without any mods enabled with Polymod.
|
|
|
|
*/
|
2024-03-05 02:29:44 -05:00
|
|
|
public static function loadNoMods():Void
|
2023-01-22 19:55:30 -05:00
|
|
|
{
|
|
|
|
// Create the mod root if it doesn't exist.
|
|
|
|
createModRoot();
|
|
|
|
|
|
|
|
// We still need to configure the debug print calls etc.
|
2024-03-05 02:29:44 -05:00
|
|
|
trace('Initializing Polymod (using no mods)...');
|
2023-01-22 19:55:30 -05:00
|
|
|
loadModsById([]);
|
|
|
|
}
|
|
|
|
|
2024-03-05 02:29:44 -05:00
|
|
|
/**
|
|
|
|
* Load all the mods with the given ids.
|
|
|
|
* @param ids The ORDERED list of mod ids to load.
|
|
|
|
*/
|
|
|
|
public static function loadModsById(ids:Array<String>):Void
|
2023-01-22 19:55:30 -05:00
|
|
|
{
|
|
|
|
if (ids.length == 0)
|
|
|
|
{
|
|
|
|
trace('You attempted to load zero mods.');
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
trace('Attempting to load ${ids.length} mods...');
|
|
|
|
}
|
|
|
|
|
|
|
|
buildImports();
|
|
|
|
|
2024-03-05 02:29:44 -05:00
|
|
|
var loadedModList:Array<ModMetadata> = polymod.Polymod.init(
|
2023-01-22 22:25:45 -05:00
|
|
|
{
|
|
|
|
// Root directory for all mods.
|
|
|
|
modRoot: MOD_FOLDER,
|
|
|
|
// The directories for one or more mods to load.
|
|
|
|
dirs: ids,
|
|
|
|
// Framework being used to load assets.
|
|
|
|
framework: OPENFL,
|
|
|
|
// The current version of our API.
|
|
|
|
apiVersionRule: API_VERSION,
|
|
|
|
// Call this function any time an error occurs.
|
|
|
|
errorCallback: PolymodErrorHandler.onPolymodError,
|
|
|
|
// Enforce semantic version patterns for each mod.
|
|
|
|
// modVersions: null,
|
|
|
|
// A map telling Polymod what the asset type is for unfamiliar file extensions.
|
|
|
|
// extensionMap: [],
|
|
|
|
|
|
|
|
frameworkParams: buildFrameworkParams(),
|
|
|
|
|
|
|
|
// List of filenames to ignore in mods. Use the default list to ignore the metadata file, etc.
|
|
|
|
ignoredFiles: Polymod.getDefaultIgnoreList(),
|
|
|
|
|
|
|
|
// Parsing rules for various data formats.
|
|
|
|
parseRules: buildParseRules(),
|
|
|
|
|
|
|
|
// Parse hxc files and register the scripted classes in them.
|
|
|
|
useScriptedClasses: true,
|
2023-09-04 22:18:45 -04:00
|
|
|
loadScriptsAsync: #if html5 true #else false #end,
|
2023-01-22 22:25:45 -05:00
|
|
|
});
|
2023-01-22 19:55:30 -05:00
|
|
|
|
|
|
|
if (loadedModList == null)
|
|
|
|
{
|
|
|
|
trace('An error occurred! Failed when loading mods!');
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (loadedModList.length == 0)
|
|
|
|
{
|
|
|
|
trace('Mod loading complete. We loaded no mods / ${ids.length} mods.');
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
trace('Mod loading complete. We loaded ${loadedModList.length} / ${ids.length} mods.');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (mod in loadedModList)
|
|
|
|
{
|
|
|
|
trace(' * ${mod.title} v${mod.modVersion} [${mod.id}]');
|
|
|
|
}
|
|
|
|
|
|
|
|
#if debug
|
2024-03-05 02:29:44 -05:00
|
|
|
var fileList:Array<String> = Polymod.listModFiles(PolymodAssetType.IMAGE);
|
2023-01-22 19:55:30 -05:00
|
|
|
trace('Installed mods have replaced ${fileList.length} images.');
|
|
|
|
for (item in fileList)
|
2024-03-05 02:29:44 -05:00
|
|
|
{
|
2023-01-22 19:55:30 -05:00
|
|
|
trace(' * $item');
|
2024-03-05 02:29:44 -05:00
|
|
|
}
|
2023-01-22 19:55:30 -05:00
|
|
|
|
|
|
|
fileList = Polymod.listModFiles(PolymodAssetType.TEXT);
|
|
|
|
trace('Installed mods have added/replaced ${fileList.length} text files.');
|
|
|
|
for (item in fileList)
|
2024-03-05 02:29:44 -05:00
|
|
|
{
|
2023-01-22 19:55:30 -05:00
|
|
|
trace(' * $item');
|
2024-03-05 02:29:44 -05:00
|
|
|
}
|
2023-01-22 19:55:30 -05:00
|
|
|
|
|
|
|
fileList = Polymod.listModFiles(PolymodAssetType.AUDIO_MUSIC);
|
|
|
|
trace('Installed mods have replaced ${fileList.length} music files.');
|
|
|
|
for (item in fileList)
|
2024-03-05 02:29:44 -05:00
|
|
|
{
|
2023-01-22 19:55:30 -05:00
|
|
|
trace(' * $item');
|
2024-03-05 02:29:44 -05:00
|
|
|
}
|
2023-01-22 19:55:30 -05:00
|
|
|
|
|
|
|
fileList = Polymod.listModFiles(PolymodAssetType.AUDIO_SOUND);
|
|
|
|
trace('Installed mods have replaced ${fileList.length} sound files.');
|
|
|
|
for (item in fileList)
|
2024-03-05 02:29:44 -05:00
|
|
|
{
|
2023-01-22 19:55:30 -05:00
|
|
|
trace(' * $item');
|
2024-03-05 02:29:44 -05:00
|
|
|
}
|
2023-01-22 19:55:30 -05:00
|
|
|
|
|
|
|
fileList = Polymod.listModFiles(PolymodAssetType.AUDIO_GENERIC);
|
|
|
|
trace('Installed mods have replaced ${fileList.length} generic audio files.');
|
|
|
|
for (item in fileList)
|
2024-03-05 02:29:44 -05:00
|
|
|
{
|
2023-01-22 19:55:30 -05:00
|
|
|
trace(' * $item');
|
2024-03-05 02:29:44 -05:00
|
|
|
}
|
2023-01-22 19:55:30 -05:00
|
|
|
#end
|
|
|
|
}
|
|
|
|
|
|
|
|
static function buildImports():Void
|
|
|
|
{
|
|
|
|
// Add default imports for common classes.
|
|
|
|
|
|
|
|
// Add import aliases for certain classes.
|
|
|
|
// NOTE: Scripted classes are automatically aliased to their parent class.
|
|
|
|
Polymod.addImportAlias('flixel.math.FlxPoint', flixel.math.FlxPoint.FlxBasePoint);
|
|
|
|
|
|
|
|
// Add blacklisting for prohibited classes and packages.
|
2024-05-02 01:52:14 -04:00
|
|
|
|
|
|
|
// `Sys`
|
2024-05-02 02:49:55 -04:00
|
|
|
// Sys.command() can run malicious processes
|
2024-05-02 01:52:14 -04:00
|
|
|
Polymod.blacklistImport('Sys');
|
|
|
|
|
2024-05-02 02:49:55 -04:00
|
|
|
// `Reflect`
|
|
|
|
// Reflect.callMethod() can access blacklisted packages
|
|
|
|
Polymod.blacklistImport('Reflect');
|
|
|
|
|
|
|
|
// `Type`
|
|
|
|
// Type.createInstance(Type.resolveClass()) can access blacklisted packages
|
|
|
|
Polymod.blacklistImport('Type');
|
|
|
|
|
|
|
|
// `cpp.Lib`
|
|
|
|
// Lib.load() can load malicious DLLs
|
|
|
|
Polymod.blacklistImport('cpp.Lib');
|
|
|
|
|
2023-01-22 19:55:30 -05:00
|
|
|
// `polymod.*`
|
2024-05-02 02:49:55 -04:00
|
|
|
// You can probably unblacklist a module
|
2023-01-22 19:55:30 -05:00
|
|
|
for (cls in ClassMacro.listClassesInPackage('polymod'))
|
|
|
|
{
|
2023-03-01 18:22:28 -05:00
|
|
|
if (cls == null) continue;
|
2024-03-05 02:29:44 -05:00
|
|
|
var className:String = Type.getClassName(cls);
|
2023-01-22 19:55:30 -05:00
|
|
|
Polymod.blacklistImport(className);
|
|
|
|
}
|
2024-05-02 01:52:14 -04:00
|
|
|
|
|
|
|
// `sys.*`
|
|
|
|
for (cls in ClassMacro.listClassesInPackage('sys'))
|
|
|
|
{
|
|
|
|
if (cls == null) continue;
|
|
|
|
var className:String = Type.getClassName(cls);
|
|
|
|
Polymod.blacklistImport(className);
|
|
|
|
}
|
2023-01-22 19:55:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static function buildParseRules():polymod.format.ParseRules
|
|
|
|
{
|
2024-03-05 02:29:44 -05:00
|
|
|
var output:polymod.format.ParseRules = polymod.format.ParseRules.getDefault();
|
2023-01-22 19:55:30 -05:00
|
|
|
// Ensure TXT files have merge support.
|
2024-03-05 02:29:44 -05:00
|
|
|
output.addType('txt', TextFileFormat.LINES);
|
2023-01-22 19:55:30 -05:00
|
|
|
// Ensure script files have merge support.
|
2024-03-05 02:29:44 -05:00
|
|
|
output.addType('hscript', TextFileFormat.PLAINTEXT);
|
|
|
|
output.addType('hxs', TextFileFormat.PLAINTEXT);
|
|
|
|
output.addType('hxc', TextFileFormat.PLAINTEXT);
|
|
|
|
output.addType('hx', TextFileFormat.PLAINTEXT);
|
2023-01-22 19:55:30 -05:00
|
|
|
|
|
|
|
// You can specify the format of a specific file, with file extension.
|
|
|
|
// output.addFile("data/introText.txt", TextFileFormat.LINES)
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline function buildFrameworkParams():polymod.Polymod.FrameworkParams
|
|
|
|
{
|
|
|
|
return {
|
|
|
|
assetLibraryPaths: [
|
2024-04-05 01:24:03 -04:00
|
|
|
'default' => 'preload', 'shared' => 'shared', 'songs' => 'songs', 'videos' => 'videos', 'tutorial' => 'tutorial', 'week1' => 'week1',
|
|
|
|
'week2' => 'week2', 'week3' => 'week3', 'week4' => 'week4', 'week5' => 'week5', 'week6' => 'week6', 'week7' => 'week7', 'weekend1' => 'weekend1',
|
2023-07-17 21:54:58 -04:00
|
|
|
],
|
|
|
|
coreAssetRedirect: CORE_FOLDER,
|
2023-01-22 19:55:30 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-05 02:29:44 -05:00
|
|
|
/**
|
|
|
|
* Retrieve a list of metadata for ALL installed mods, including disabled mods.
|
|
|
|
* @return An array of mod metadata
|
|
|
|
*/
|
2023-01-22 19:55:30 -05:00
|
|
|
public static function getAllMods():Array<ModMetadata>
|
|
|
|
{
|
|
|
|
trace('Scanning the mods folder...');
|
2024-03-05 02:29:44 -05:00
|
|
|
var modMetadata:Array<ModMetadata> = Polymod.scan(
|
2023-01-22 22:25:45 -05:00
|
|
|
{
|
|
|
|
modRoot: MOD_FOLDER,
|
|
|
|
apiVersionRule: API_VERSION,
|
|
|
|
errorCallback: PolymodErrorHandler.onPolymodError
|
|
|
|
});
|
2023-01-22 19:55:30 -05:00
|
|
|
trace('Found ${modMetadata.length} mods when scanning.');
|
|
|
|
return modMetadata;
|
|
|
|
}
|
|
|
|
|
2024-03-05 02:29:44 -05:00
|
|
|
/**
|
|
|
|
* Retrieve a list of ALL mod IDs, including disabled mods.
|
|
|
|
* @return An array of mod IDs
|
|
|
|
*/
|
2023-01-22 19:55:30 -05:00
|
|
|
public static function getAllModIds():Array<String>
|
|
|
|
{
|
2024-03-05 02:29:44 -05:00
|
|
|
var modIds:Array<String> = [for (i in getAllMods()) i.id];
|
2023-01-22 19:55:30 -05:00
|
|
|
return modIds;
|
|
|
|
}
|
|
|
|
|
2024-03-05 02:29:44 -05:00
|
|
|
/**
|
|
|
|
* Retrieve a list of metadata for all enabled mods.
|
|
|
|
* @return An array of mod metadata
|
|
|
|
*/
|
2023-01-22 19:55:30 -05:00
|
|
|
public static function getEnabledMods():Array<ModMetadata>
|
|
|
|
{
|
2024-03-05 02:29:44 -05:00
|
|
|
var modIds:Array<String> = Save.instance.enabledModIds;
|
|
|
|
var modMetadata:Array<ModMetadata> = getAllMods();
|
|
|
|
var enabledMods:Array<ModMetadata> = [];
|
2023-01-22 19:55:30 -05:00
|
|
|
for (item in modMetadata)
|
|
|
|
{
|
|
|
|
if (modIds.indexOf(item.id) != -1)
|
|
|
|
{
|
|
|
|
enabledMods.push(item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return enabledMods;
|
|
|
|
}
|
|
|
|
|
2024-03-05 02:29:44 -05:00
|
|
|
/**
|
|
|
|
* Clear and reload from disk all data assets.
|
|
|
|
* Useful for "hot reloading" for fast iteration!
|
|
|
|
*/
|
|
|
|
public static function forceReloadAssets():Void
|
2023-01-22 19:55:30 -05:00
|
|
|
{
|
|
|
|
// Forcibly clear scripts so that scripts can be edited.
|
|
|
|
ModuleHandler.clearModuleCache();
|
|
|
|
Polymod.clearScripts();
|
|
|
|
|
|
|
|
// Forcibly reload Polymod so it finds any new files.
|
|
|
|
// TODO: Replace this with loadEnabledMods().
|
|
|
|
funkin.modding.PolymodHandler.loadAllMods();
|
|
|
|
|
|
|
|
// Reload scripted classes so stages and modules will update.
|
|
|
|
Polymod.registerAllScriptClasses();
|
|
|
|
|
|
|
|
// Reload everything that is cached.
|
|
|
|
// Currently this freezes the game for a second but I guess that's tolerable?
|
|
|
|
|
|
|
|
// TODO: Reload event callbacks
|
|
|
|
|
2023-07-13 20:26:56 -04:00
|
|
|
// These MUST be imported at the top of the file and not referred to by fully qualified name,
|
|
|
|
// to ensure build macros work properly.
|
2023-09-08 17:46:44 -04:00
|
|
|
SongRegistry.instance.loadEntries();
|
2023-07-13 20:26:56 -04:00
|
|
|
LevelRegistry.instance.loadEntries();
|
|
|
|
NoteStyleRegistry.instance.loadEntries();
|
2024-01-03 21:10:14 -05:00
|
|
|
SongEventRegistry.loadEventCache();
|
2024-02-07 09:21:44 -05:00
|
|
|
ConversationRegistry.instance.loadEntries();
|
|
|
|
DialogueBoxRegistry.instance.loadEntries();
|
|
|
|
SpeakerRegistry.instance.loadEntries();
|
2024-03-20 14:37:24 -04:00
|
|
|
AlbumRegistry.instance.loadEntries();
|
2024-01-16 16:49:15 -05:00
|
|
|
StageRegistry.instance.loadEntries();
|
2024-02-07 09:21:44 -05:00
|
|
|
CharacterDataParser.loadCharacterCache(); // TODO: Migrate characters to BaseRegistry.
|
2023-01-22 19:55:30 -05:00
|
|
|
ModuleHandler.loadModuleCache();
|
|
|
|
}
|
2022-03-08 03:13:53 -05:00
|
|
|
}
|