2019-05-29 05:48:41 +02:00
import { SettingsCookie , PluginCookie , Plugins } from "data" ;
import ContentManager from "./contentmanager" ;
import Utilities from "./utilities" ;
import Core from "./core" ;
import BdApi from "./pluginapi" ;
import Emitter from "./emitter" ;
import DataStore from "./datastore" ;
2019-05-28 20:19:48 +02:00
function PluginModule ( ) {
}
PluginModule . prototype . loadPlugins = function ( ) {
this . loadPluginData ( ) ;
2019-05-28 23:27:25 +02:00
const errors = ContentManager . loadPlugins ( ) ;
2019-05-29 06:29:15 +02:00
const plugins = Object . keys ( Plugins ) ;
for ( let i = 0 ; i < plugins . length ; i ++ ) {
let plugin , name ;
2019-05-28 20:19:48 +02:00
try {
2019-05-29 05:48:41 +02:00
plugin = Plugins [ plugins [ i ] ] . plugin ;
2019-05-28 20:19:48 +02:00
name = plugin . getName ( ) ;
if ( plugin . load && typeof ( plugin . load ) == "function" ) plugin . load ( ) ;
}
catch ( err ) {
2019-05-29 05:48:41 +02:00
PluginCookie [ name ] = false ;
Utilities . err ( "Plugins" , name + " could not be loaded." , err ) ;
errors . push ( { name : name , file : Plugins [ plugins [ i ] ] . filename , message : "load() could not be fired." , error : { message : err . message , stack : err . stack } } ) ;
2019-05-28 20:19:48 +02:00
continue ;
}
2019-05-29 05:48:41 +02:00
if ( ! PluginCookie [ name ] ) PluginCookie [ name ] = false ;
2019-05-28 20:19:48 +02:00
2019-05-29 05:48:41 +02:00
if ( PluginCookie [ name ] ) {
2019-05-28 20:19:48 +02:00
try {
plugin . start ( ) ;
2019-05-29 05:48:41 +02:00
if ( SettingsCookie [ "fork-ps-2" ] ) Core . showToast ( ` ${ plugin . getName ( ) } v ${ plugin . getVersion ( ) } has started. ` ) ;
2019-05-28 20:19:48 +02:00
}
catch ( err ) {
2019-05-29 05:48:41 +02:00
PluginCookie [ name ] = false ;
Utilities . err ( "Plugins" , name + " could not be started." , err ) ;
errors . push ( { name : name , file : Plugins [ plugins [ i ] ] . filename , message : "start() could not be fired." , error : { message : err . message , stack : err . stack } } ) ;
2019-05-28 20:19:48 +02:00
}
}
}
this . savePluginData ( ) ;
require ( "electron" ) . remote . getCurrentWebContents ( ) . on ( "did-navigate-in-page" , this . channelSwitch . bind ( this ) ) ;
2019-05-29 05:48:41 +02:00
// if (SettingsCookie["fork-ps-5"]) ContentManager.watchContent("plugin");
2019-05-28 23:27:25 +02:00
return errors ;
2019-05-28 20:19:48 +02:00
} ;
PluginModule . prototype . startPlugin = function ( plugin , reload = false ) {
try {
2019-05-29 05:48:41 +02:00
Plugins [ plugin ] . plugin . start ( ) ;
if ( SettingsCookie [ "fork-ps-2" ] && ! reload ) Core . showToast ( ` ${ Plugins [ plugin ] . plugin . getName ( ) } v ${ Plugins [ plugin ] . plugin . getVersion ( ) } has started. ` ) ;
2019-05-28 20:19:48 +02:00
}
catch ( err ) {
2019-05-29 05:48:41 +02:00
if ( SettingsCookie [ "fork-ps-2" ] && ! reload ) Core . showToast ( ` ${ Plugins [ plugin ] . plugin . getName ( ) } v ${ Plugins [ plugin ] . plugin . getVersion ( ) } could not be started. ` , { type : "error" } ) ;
PluginCookie [ plugin ] = false ;
2019-05-28 20:19:48 +02:00
this . savePluginData ( ) ;
2019-05-29 05:48:41 +02:00
Utilities . err ( "Plugins" , name + " could not be started." , err ) ;
2019-05-28 20:19:48 +02:00
}
} ;
PluginModule . prototype . stopPlugin = function ( plugin , reload = false ) {
try {
2019-05-29 05:48:41 +02:00
Plugins [ plugin ] . plugin . stop ( ) ;
if ( SettingsCookie [ "fork-ps-2" ] && ! reload ) Core . showToast ( ` ${ Plugins [ plugin ] . plugin . getName ( ) } v ${ Plugins [ plugin ] . plugin . getVersion ( ) } has stopped. ` ) ;
2019-05-28 20:19:48 +02:00
}
catch ( err ) {
2019-05-29 05:48:41 +02:00
if ( SettingsCookie [ "fork-ps-2" ] && ! reload ) Core . showToast ( ` ${ Plugins [ plugin ] . plugin . getName ( ) } v ${ Plugins [ plugin ] . plugin . getVersion ( ) } could not be stopped. ` , { type : "error" } ) ;
Utilities . err ( "Plugins" , Plugins [ plugin ] . plugin . getName ( ) + " could not be stopped." , err ) ;
2019-05-28 20:19:48 +02:00
}
} ;
PluginModule . prototype . enablePlugin = function ( plugin , reload = false ) {
2019-05-29 05:48:41 +02:00
if ( PluginCookie [ plugin ] ) return ;
PluginCookie [ plugin ] = true ;
2019-05-28 20:19:48 +02:00
this . savePluginData ( ) ;
this . startPlugin ( plugin , reload ) ;
} ;
PluginModule . prototype . disablePlugin = function ( plugin , reload = false ) {
2019-05-29 05:48:41 +02:00
if ( ! PluginCookie [ plugin ] ) return ;
PluginCookie [ plugin ] = false ;
2019-05-28 20:19:48 +02:00
this . savePluginData ( ) ;
this . stopPlugin ( plugin , reload ) ;
} ;
PluginModule . prototype . togglePlugin = function ( plugin ) {
2019-05-29 05:48:41 +02:00
if ( PluginCookie [ plugin ] ) this . disablePlugin ( plugin ) ;
2019-05-28 20:19:48 +02:00
else this . enablePlugin ( plugin ) ;
} ;
PluginModule . prototype . loadPlugin = function ( filename ) {
const error = ContentManager . loadContent ( filename , "plugin" ) ;
if ( error ) {
2019-05-29 05:48:41 +02:00
if ( SettingsCookie [ "fork-ps-1" ] ) Core . showContentErrors ( { plugins : [ error ] } ) ;
if ( SettingsCookie [ "fork-ps-2" ] ) BdApi . showToast ( ` ${ filename } could not be loaded. ` , { type : "error" } ) ;
return Utilities . err ( "ContentManager" , ` ${ filename } could not be loaded. ` , error ) ;
2019-05-28 20:19:48 +02:00
}
2019-05-29 05:48:41 +02:00
const plugin = Object . values ( Plugins ) . find ( p => p . filename == filename ) . plugin ;
2019-05-28 20:19:48 +02:00
try { if ( plugin . load && typeof ( plugin . load ) == "function" ) plugin . load ( ) ; }
2019-05-29 05:48:41 +02:00
catch ( err ) { if ( SettingsCookie [ "fork-ps-1" ] ) Core . showContentErrors ( { plugins : [ err ] } ) ; }
Utilities . log ( "ContentManager" , ` ${ plugin . getName ( ) } v ${ plugin . getVersion ( ) } was loaded. ` ) ;
if ( SettingsCookie [ "fork-ps-2" ] ) BdApi . showToast ( ` ${ plugin . getName ( ) } v ${ plugin . getVersion ( ) } was loaded. ` , { type : "success" } ) ;
Emitter . dispatch ( "plugin-loaded" , plugin . getName ( ) ) ;
2019-05-28 20:19:48 +02:00
} ;
PluginModule . prototype . unloadPlugin = function ( filenameOrName ) {
2019-05-29 05:48:41 +02:00
const bdplugin = Object . values ( Plugins ) . find ( p => p . filename == filenameOrName ) || Plugins [ filenameOrName ] ;
2019-05-28 20:19:48 +02:00
if ( ! bdplugin ) return ;
const plugin = bdplugin . plugin . getName ( ) ;
2019-05-29 05:48:41 +02:00
if ( PluginCookie [ plugin ] ) this . disablePlugin ( plugin , true ) ;
const error = ContentManager . unloadContent ( Plugins [ plugin ] . filename , "plugin" ) ;
delete Plugins [ plugin ] ;
2019-05-28 20:19:48 +02:00
if ( error ) {
2019-05-29 05:48:41 +02:00
if ( SettingsCookie [ "fork-ps-1" ] ) Core . showContentErrors ( { plugins : [ error ] } ) ;
if ( SettingsCookie [ "fork-ps-2" ] ) BdApi . showToast ( ` ${ plugin } could not be unloaded. It may have not been loaded yet. ` , { type : "error" } ) ;
return Utilities . err ( "ContentManager" , ` ${ plugin } could not be unloaded. It may have not been loaded yet. ` , error ) ;
2019-05-28 20:19:48 +02:00
}
2019-05-29 05:48:41 +02:00
Utilities . log ( "ContentManager" , ` ${ plugin } was unloaded. ` ) ;
if ( SettingsCookie [ "fork-ps-2" ] ) BdApi . showToast ( ` ${ plugin } was unloaded. ` , { type : "success" } ) ;
Emitter . dispatch ( "plugin-unloaded" , plugin ) ;
2019-05-28 20:19:48 +02:00
} ;
PluginModule . prototype . reloadPlugin = function ( filenameOrName ) {
2019-05-29 05:48:41 +02:00
const bdplugin = Object . values ( Plugins ) . find ( p => p . filename == filenameOrName ) || Plugins [ filenameOrName ] ;
2019-05-28 20:19:48 +02:00
if ( ! bdplugin ) return this . loadPlugin ( filenameOrName ) ;
const plugin = bdplugin . plugin . getName ( ) ;
2019-05-29 05:48:41 +02:00
const enabled = PluginCookie [ plugin ] ;
2019-05-28 20:19:48 +02:00
if ( enabled ) this . stopPlugin ( plugin , true ) ;
2019-05-29 05:48:41 +02:00
const error = ContentManager . reloadContent ( Plugins [ plugin ] . filename , "plugin" ) ;
2019-05-28 20:19:48 +02:00
if ( error ) {
2019-05-29 05:48:41 +02:00
if ( SettingsCookie [ "fork-ps-1" ] ) Core . showContentErrors ( { plugins : [ error ] } ) ;
if ( SettingsCookie [ "fork-ps-2" ] ) BdApi . showToast ( ` ${ plugin } could not be reloaded. ` , { type : "error" } ) ;
return Utilities . err ( "ContentManager" , ` ${ plugin } could not be reloaded. ` , error ) ;
2019-05-28 20:19:48 +02:00
}
2019-05-29 05:48:41 +02:00
if ( Plugins [ plugin ] . plugin . load && typeof ( Plugins [ plugin ] . plugin . load ) == "function" ) Plugins [ plugin ] . plugin . load ( ) ;
2019-05-28 20:19:48 +02:00
if ( enabled ) this . startPlugin ( plugin , true ) ;
2019-05-29 05:48:41 +02:00
Utilities . log ( "ContentManager" , ` ${ plugin } v ${ Plugins [ plugin ] . plugin . getVersion ( ) } was reloaded. ` ) ;
if ( SettingsCookie [ "fork-ps-2" ] ) BdApi . showToast ( ` ${ plugin } v ${ Plugins [ plugin ] . plugin . getVersion ( ) } was reloaded. ` , { type : "success" } ) ;
Emitter . dispatch ( "plugin-reloaded" , plugin ) ;
2019-05-28 20:19:48 +02:00
} ;
PluginModule . prototype . updatePluginList = function ( ) {
const results = ContentManager . loadNewContent ( "plugin" ) ;
for ( const filename of results . added ) this . loadPlugin ( filename ) ;
for ( const name of results . removed ) this . unloadPlugin ( name ) ;
} ;
PluginModule . prototype . loadPluginData = function ( ) {
2019-05-29 05:48:41 +02:00
const saved = DataStore . getSettingGroup ( "plugins" ) ;
if ( ! saved ) return ;
Object . assign ( PluginCookie , saved ) ;
2019-05-28 20:19:48 +02:00
} ;
PluginModule . prototype . savePluginData = function ( ) {
2019-05-29 05:48:41 +02:00
DataStore . setSettingGroup ( "plugins" , PluginCookie ) ;
2019-05-28 20:19:48 +02:00
} ;
PluginModule . prototype . newMessage = function ( ) {
2019-05-29 06:29:15 +02:00
const plugins = Object . keys ( Plugins ) ;
for ( let i = 0 ; i < plugins . length ; i ++ ) {
const plugin = Plugins [ plugins [ i ] ] . plugin ;
2019-05-29 05:48:41 +02:00
if ( ! PluginCookie [ plugin . getName ( ) ] ) continue ;
2019-05-28 20:19:48 +02:00
if ( typeof plugin . onMessage === "function" ) {
try { plugin . onMessage ( ) ; }
2019-05-29 05:48:41 +02:00
catch ( err ) { Utilities . err ( "Plugins" , "Unable to fire onMessage for " + plugin . getName ( ) + "." , err ) ; }
2019-05-28 20:19:48 +02:00
}
}
} ;
PluginModule . prototype . channelSwitch = function ( ) {
2019-05-29 06:29:15 +02:00
const plugins = Object . keys ( Plugins ) ;
for ( let i = 0 ; i < plugins . length ; i ++ ) {
const plugin = Plugins [ plugins [ i ] ] . plugin ;
2019-05-29 05:48:41 +02:00
if ( ! PluginCookie [ plugin . getName ( ) ] ) continue ;
2019-05-28 20:19:48 +02:00
if ( typeof plugin . onSwitch === "function" ) {
try { plugin . onSwitch ( ) ; }
2019-05-29 05:48:41 +02:00
catch ( err ) { Utilities . err ( "Plugins" , "Unable to fire onSwitch for " + plugin . getName ( ) + "." , err ) ; }
2019-05-28 20:19:48 +02:00
}
}
} ;
PluginModule . prototype . rawObserver = function ( e ) {
2019-05-29 06:29:15 +02:00
const plugins = Object . keys ( Plugins ) ;
for ( let i = 0 ; i < plugins . length ; i ++ ) {
const plugin = Plugins [ plugins [ i ] ] . plugin ;
2019-05-29 05:48:41 +02:00
if ( ! PluginCookie [ plugin . getName ( ) ] ) continue ;
2019-05-28 20:19:48 +02:00
if ( typeof plugin . observer === "function" ) {
try { plugin . observer ( e ) ; }
2019-05-29 05:48:41 +02:00
catch ( err ) { Utilities . err ( "Plugins" , "Unable to fire observer for " + plugin . getName ( ) + "." , err ) ; }
2019-05-28 20:19:48 +02:00
}
}
2019-05-28 23:27:25 +02:00
} ;
2019-05-29 05:48:41 +02:00
export default new PluginModule ( ) ;