2022-01-20 17:47:42 -05:00
package modding ;
2022-02-22 13:57:29 -05:00
#if polymod
2022-01-28 20:03:38 -05:00
import polymod . Polymod . ModMetadata ;
2022-01-20 17:47:42 -05:00
import polymod . Polymod ;
import polymod . backends . OpenFLBackend ;
import polymod . backends . PolymodAssets . PolymodAssetType ;
import polymod . format . ParseRules . LinesParseFormat ;
import polymod . format . ParseRules . TextFileFormat ;
#end
class PolymodHandler
{
/ * *
* The API version that mods should comply with .
* Format this with Semantic Versioning ; < MAJOR > . < MINOR > . < PATCH > .
* Bug fixes increment the patch version , new features i n c r e m e n t t h e m i n o r v e r s i o n .
* Changes that break old mods increment the major version .
* /
static final API_VERSION = " 0 . 1 . 0 " ;
/ * *
* Where relative to the executable that mods are located .
* /
static final MOD_FOLDER = " m o d s " ;
/ * *
* Loads the game with ALL mods enabled with Polymod .
* /
public static function loadAllMods ( )
{
2022-02-22 13:57:29 -05:00
#if polymod
2022-01-20 17:47:42 -05:00
trace ( " I n i t i a l i z i n g P o l y m o d ( u s i n g a l l m o d s ) . . . " ) ;
loadModsById ( getAllModIds ( ) ) ;
#else
trace ( " P o l y m o d n o t i n i t i a l i z e d ; n o t s u p p o r t e d o n t h i s p l a t f o r m . " ) ;
#end
}
/ * *
* Loads the game without any mods enabled with Polymod .
* /
public static function loadNoMods ( )
{
// We still need to configure the debug print calls etc.
2022-02-22 13:57:29 -05:00
#if polymod
2022-01-20 17:47:42 -05:00
trace ( " I n i t i a l i z i n g P o l y m o d ( u s i n g n o m o d s ) . . . " ) ;
loadModsById ( [ ] ) ;
#else
trace ( " P o l y m o d n o t i n i t i a l i z e d ; n o t s u p p o r t e d o n t h i s p l a t f o r m . " ) ;
#end
}
public static function loadModsById ( ids : Array < String > )
{
2022-02-22 13:57:29 -05:00
#if polymod
2022-01-20 17:47:42 -05:00
if ( ids . length == 0 )
{
trace ( ' Y o u a t t e m p t e d t o l o a d z e r o m o d s . ' ) ;
}
e lse
{
trace ( ' A t t e m p t i n g t o l o a d ${ ids . length } m o d s . . . ' ) ;
}
var loadedModList = polymod . Polymod . init ( {
// 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.
apiVersion : API_VERSION ,
// Call this function any time an error occurs.
errorCallback : 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 ( ) ,
} ) ;
if ( loadedModList == null )
{
trace ( ' [ P O L Y M O D ] A n e r r o r o c c u r r e d ! F a i l e d w h e n l o a d i n g m o d s ! ' ) ;
}
e lse
{
if ( loadedModList . length == 0 )
{
trace ( ' [ P O L Y M O D ] M o d l o a d i n g c o m p l e t e . W e l o a d e d n o m o d s / ${ ids . length } m o d s . ' ) ;
}
e lse
{
trace ( ' [ P O L Y M O D ] M o d l o a d i n g c o m p l e t e . W e l o a d e d ${ loadedModList . length } / ${ ids . length } m o d s . ' ) ;
}
}
for ( mod in loadedModList )
trace ( ' * ${ mod . title } v ${ mod . modVersion } [ ${ mod . id } ] ' ) ;
#if debug
var fileList = Polymod . listModFiles ( " I M A G E " ) ;
trace ( ' [ P O L Y M O D ] I n s t a l l e d m o d s h a v e r e p l a c e d ${ fileList . length } i m a g e s . ' ) ;
for ( item in fileList )
trace ( ' * $ item ' ) ;
fileList = Polymod . listModFiles ( " T E X T " ) ;
trace ( ' [ P O L Y M O D ] I n s t a l l e d m o d s h a v e r e p l a c e d ${ fileList . length } t e x t f i l e s . ' ) ;
for ( item in fileList )
trace ( ' * $ item ' ) ;
fileList = Polymod . listModFiles ( " M U S I C " ) ;
trace ( ' [ P O L Y M O D ] I n s t a l l e d m o d s h a v e r e p l a c e d ${ fileList . length } m u s i c f i l e s . ' ) ;
for ( item in fileList )
trace ( ' * $ item ' ) ;
fileList = Polymod . listModFiles ( " S O U N D " ) ;
trace ( ' [ P O L Y M O D ] I n s t a l l e d m o d s h a v e r e p l a c e d ${ fileList . length } s o u n d f i l e s . ' ) ;
for ( item in fileList )
trace ( ' * $ item ' ) ;
#end
#else
trace ( " [ P O L Y M O D ] M o d s a r e n o t s u p p o r t e d o n t h i s p l a t f o r m . " ) ;
#end
}
2022-02-22 13:57:29 -05:00
#if polymod
2022-01-20 17:47:42 -05:00
static function buildParseRules ( ) : polymod . format . ParseRules
{
var output = polymod . format . ParseRules . getDefault ( ) ;
// Ensure TXT files have merge support.
output . addType ( " t x t " , TextFileFormat . LINES ) ;
// Ensure script files have merge support.
output . addType ( " h s c r i p t " , TextFileFormat . PLAINTEXT ) ;
// 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 : [
" s o n g s " => " . / s o n g s " , " s h a r e d " => " . / " , " t u t o r i a l " => " . / t u t o r i a l " , " s c r i p t s " => " . / s c r i p t s " , " w e e k 1 " => " . / w e e k 1 " , " w e e k 2 " => " . / w e e k 2 " ,
" w e e k 3 " => " . / w e e k 3 " , " w e e k 4 " => " . / w e e k 4 " , " w e e k 5 " => " . / w e e k 5 " , " w e e k 6 " => " . / w e e k 6 " , " w e e k 7 " => " . / w e e k 7 " , " w e e k 8 " => " . / w e e k 8 " ,
]
}
}
static function onPolymodError ( error : PolymodError ) : Void
{
// Perform an action based on the error code.
switch ( error . code )
{
c ase MOD_LOAD_PREPARE :
trace ( ' [ P O L Y M O D ] ${ error . message } ' ) ;
c ase MOD_LOAD_DONE :
trace ( ' [ P O L Y M O D ] ${ error . message } ' ) ;
c ase MISSING_ICON :
trace ( ' [ P O L Y M O D ] A m o d i s m i s s i n g a n i c o n . P l e a s e a d d o n e . ' ) ;
d efault :
// Log the message based on its severity.
switch ( error . severity )
{
c ase NOTICE :
trace ( ' [ P O L Y M O D ] ${ error . message } ' ) ;
c ase WARNING :
trace ( ' [ P O L Y M O D ] ${ error . message } ' ) ;
c ase ERROR :
trace ( ' [ P O L Y M O D ] ${ error . message } ' ) ;
}
}
}
#end
2022-02-22 13:57:29 -05:00
public static function getAllMods ( ) : Array < #if polymod ModMetadata #else D y n a m i c #end > // this is shitty conditional but ModMetadata isn't imported on HTML5! And I'm too lazy to actually do it properly!
2022-01-20 17:47:42 -05:00
{
2022-02-22 13:57:29 -05:00
#if polymod
2022-01-20 17:47:42 -05:00
trace ( ' S c a n n i n g t h e m o d s f o l d e r . . . ' ) ;
var modMetadata = Polymod . scan ( MOD_FOLDER ) ;
trace ( ' F o u n d ${ modMetadata . length } m o d s w h e n s c a n n i n g . ' ) ;
return modMetadata ;
#else
return [ ] ;
#end
}
public static function getAllModIds ( ) : Array < String >
{
var modIds = [ for ( i in getAllMods ( ) ) i . id ] ;
return modIds ;
}
}