-
Notifications
You must be signed in to change notification settings - Fork 7.1k
/
PluginCache.js
203 lines (186 loc) · 5.51 KB
/
PluginCache.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/**
* @author Richard Davey <rich@phaser.io>
* @copyright 2013-2024 Phaser Studio Inc.
* @license {@link https://opensource.org/licenses/MIT|MIT License}
*/
// Contains the plugins that Phaser uses globally and locally.
// These are the source objects, not instantiated.
var corePlugins = {};
// Contains the plugins that the dev has loaded into their game
// These are the source objects, not instantiated.
var customPlugins = {};
var PluginCache = {};
/**
* @namespace Phaser.Plugins.PluginCache
*/
/**
* Static method called directly by the Core internal Plugins.
* Key is a reference used to get the plugin from the plugins object (i.e. InputPlugin)
* Plugin is the object to instantiate to create the plugin
* Mapping is what the plugin is injected into the Scene.Systems as (i.e. input)
*
* @method Phaser.Plugins.PluginCache.register
* @since 3.8.0
*
* @param {string} key - A reference used to get this plugin from the plugin cache.
* @param {function} plugin - The plugin to be stored. Should be the core object, not instantiated.
* @param {string} mapping - If this plugin is to be injected into the Scene Systems, this is the property key map used.
* @param {boolean} [custom=false] - Core Scene plugin or a Custom Scene plugin?
*/
PluginCache.register = function (key, plugin, mapping, custom)
{
if (custom === undefined) { custom = false; }
corePlugins[key] = { plugin: plugin, mapping: mapping, custom: custom };
};
/**
* Stores a custom plugin in the global plugin cache.
* The key must be unique, within the scope of the cache.
*
* @method Phaser.Plugins.PluginCache.registerCustom
* @since 3.8.0
*
* @param {string} key - A reference used to get this plugin from the plugin cache.
* @param {function} plugin - The plugin to be stored. Should be the core object, not instantiated.
* @param {string} mapping - If this plugin is to be injected into the Scene Systems, this is the property key map used.
* @param {?any} data - A value to be passed to the plugin's `init` method.
*/
PluginCache.registerCustom = function (key, plugin, mapping, data)
{
customPlugins[key] = { plugin: plugin, mapping: mapping, data: data };
};
/**
* Checks if the given key is already being used in the core plugin cache.
*
* @method Phaser.Plugins.PluginCache.hasCore
* @since 3.8.0
*
* @param {string} key - The key to check for.
*
* @return {boolean} `true` if the key is already in use in the core cache, otherwise `false`.
*/
PluginCache.hasCore = function (key)
{
return corePlugins.hasOwnProperty(key);
};
/**
* Checks if the given key is already being used in the custom plugin cache.
*
* @method Phaser.Plugins.PluginCache.hasCustom
* @since 3.8.0
*
* @param {string} key - The key to check for.
*
* @return {boolean} `true` if the key is already in use in the custom cache, otherwise `false`.
*/
PluginCache.hasCustom = function (key)
{
return customPlugins.hasOwnProperty(key);
};
/**
* Returns the core plugin object from the cache based on the given key.
*
* @method Phaser.Plugins.PluginCache.getCore
* @since 3.8.0
*
* @param {string} key - The key of the core plugin to get.
*
* @return {Phaser.Types.Plugins.CorePluginContainer} The core plugin object.
*/
PluginCache.getCore = function (key)
{
return corePlugins[key];
};
/**
* Returns the custom plugin object from the cache based on the given key.
*
* @method Phaser.Plugins.PluginCache.getCustom
* @since 3.8.0
*
* @param {string} key - The key of the custom plugin to get.
*
* @return {Phaser.Types.Plugins.CustomPluginContainer} The custom plugin object.
*/
PluginCache.getCustom = function (key)
{
return customPlugins[key];
};
/**
* Returns an object from the custom cache based on the given key that can be instantiated.
*
* @method Phaser.Plugins.PluginCache.getCustomClass
* @since 3.8.0
*
* @param {string} key - The key of the custom plugin to get.
*
* @return {function} The custom plugin object.
*/
PluginCache.getCustomClass = function (key)
{
return (customPlugins.hasOwnProperty(key)) ? customPlugins[key].plugin : null;
};
/**
* Removes a core plugin based on the given key.
*
* @method Phaser.Plugins.PluginCache.remove
* @since 3.8.0
*
* @param {string} key - The key of the core plugin to remove.
*/
PluginCache.remove = function (key)
{
if (corePlugins.hasOwnProperty(key))
{
delete corePlugins[key];
}
};
/**
* Removes a custom plugin based on the given key.
*
* @method Phaser.Plugins.PluginCache.removeCustom
* @since 3.8.0
*
* @param {string} key - The key of the custom plugin to remove.
*/
PluginCache.removeCustom = function (key)
{
if (customPlugins.hasOwnProperty(key))
{
delete customPlugins[key];
}
};
/**
* Removes all Core Plugins.
*
* This includes all of the internal system plugins that Phaser needs, like the Input Plugin and Loader Plugin.
* So be sure you only call this if you do not wish to run Phaser again.
*
* @method Phaser.Plugins.PluginCache.destroyCorePlugins
* @since 3.12.0
*/
PluginCache.destroyCorePlugins = function ()
{
for (var key in corePlugins)
{
if (corePlugins.hasOwnProperty(key))
{
delete corePlugins[key];
}
}
};
/**
* Removes all Custom Plugins.
*
* @method Phaser.Plugins.PluginCache.destroyCustomPlugins
* @since 3.12.0
*/
PluginCache.destroyCustomPlugins = function ()
{
for (var key in customPlugins)
{
if (customPlugins.hasOwnProperty(key))
{
delete customPlugins[key];
}
}
};
module.exports = PluginCache;