import { IPlugin } from "../IModLoaderAPI" ; import path from 'path' ; export const enum ProxySide { CLIENT , SERVER , UNIVERSAL } function custom_dummy ( ) { return true ; } export class ProxySideContainer { side : ProxySide ; backing : ( new ( ) => any ) | string ; core ? : string ; custom ? : ( ) => boolean ; constructor ( side , backing , core ? , custom ? ) { this . side = side ; this . backing = backing ; this . core = core ; this . custom = custom ; if ( this . custom === undefined ) { this . custom = custom_dummy ; } } } export function SidedProxy ( side , inst , core ? , custom ? ) { return function ( target , propertyKey ) { if ( target . ModLoader === undefined ) { target [ 'ModLoader' ] = { } ; } if ( target . ModLoader . sidedproxies === undefined ) { target . ModLoader [ 'sidedproxies' ] = new Map < string , string > ( ) ; } target . ModLoader . sidedproxies . set ( new ProxySideContainer ( side , inst , core , custom ) , propertyKey ) ; } ; } export function setupSidedProxy ( instance , isClient , isServer , core ) { let p = Object . getPrototypeOf ( instance ) ; let arr = [ ] ; if ( p . hasOwnProperty ( 'ModLoader' ) ) { if ( p . ModLoader . hasOwnProperty ( "hasBeenProcessed" ) ) { return arr ; } if ( p . ModLoader . hasOwnProperty ( 'sidedproxies' ) ) { p . ModLoader . sidedproxies . forEach ( function ( value , key ) { if ( key . core !== undefined ) { if ( key . core !== '*' ) { if ( key . core !== core ) return ; } } if ( ! key . custom ! ( ) ) return ; if ( ( isClient && key . side === ProxySide . CLIENT ) || key . side === ProxySide . UNIVERSAL ) { if ( typeof ( key . backing ) === 'string' ) { let c ; try { if ( c === undefined ) { c = require ( path . resolve ( path . parse ( key . backing ) . dir , path . parse ( key . backing ) . name + ".js" ) ) . default ; } } catch ( err ) { } try { if ( c === undefined ) { c = require ( path . resolve ( path . parse ( key . backing ) . dir , path . parse ( key . backing ) . name + ".mls" ) ) . default ; } } catch ( err ) { } try { if ( c === undefined ) { c = require ( path . resolve ( path . parse ( key . backing ) . dir , path . parse ( key . backing ) . name + ".mlz" ) ) . default ; } } catch ( err ) { } instance [ value ] = new c ( ) ; } else { instance [ value ] = new key . backing ( ) ; } arr . push ( instance [ value ] ) ; } else if ( isServer && key . side === ProxySide . SERVER ) { if ( typeof ( key . backing ) === 'string' ) { let c ; try { if ( c === undefined ) { c = require ( path . resolve ( path . parse ( key . backing ) . dir , path . parse ( key . backing ) . name + ".js" ) ) . default ; } } catch ( err ) { } try { if ( c === undefined ) { c = require ( path . resolve ( path . parse ( key . backing ) . dir , path . parse ( key . backing ) . name + ".mls" ) ) . default ; } } catch ( err ) { } try { if ( c === undefined ) { c = require ( path . resolve ( path . parse ( key . backing ) . dir , path . parse ( key . backing ) . name + ".mlz" ) ) . default ; } } catch ( err ) { } instance [ value ] = new c ( ) ; } else { instance [ value ] = new key . backing ( ) ; } arr . push ( instance [ value ] ) ; } } ) ; } } return arr ; } export function ParentReference ( ) { return function ( target , propertyKey ) { if ( target . ModLoader === undefined ) { target [ 'ModLoader' ] = { } ; } if ( target . ModLoader . ParentReference === undefined ) { target . ModLoader [ 'ParentReference' ] = new Map < string , string > ( ) ; } target . ModLoader . ParentReference . set ( propertyKey , propertyKey ) ; } ; } export function setupParentReference ( instance , parent ) { let p = Object . getPrototypeOf ( instance ) ; if ( p . hasOwnProperty ( 'ModLoader' ) ) { if ( p . ModLoader . hasOwnProperty ( "hasBeenProcessed" ) ) { return ; } if ( p . ModLoader . hasOwnProperty ( 'ParentReference' ) ) { p . ModLoader . ParentReference . forEach ( function ( value , key ) { instance [ key ] = parent ; } ) ; } } }