Skip to content

Instantly share code, notes, and snippets.

@itssimple
Last active June 24, 2020 07:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save itssimple/01baae84a5c10bb56f6a46a7eed66dfa to your computer and use it in GitHub Desktop.
Save itssimple/01baae84a5c10bb56f6a46a7eed66dfa to your computer and use it in GitHub Desktop.
Overwolf Plugin, Disposable
let plugin = new OverwolfPlugin('OverwolfSamplePlugin', true);
plugin.initialize(status => {
if(!status) {
console.error('Could not load plugin');
return;
}
});
/* Do all the work that is needed by the plugin */
plugin.dispose();
plugin = null;
{
/* Only including the plugin-part here */
"extra-objects": {
"OverwolfSamplePlugin": {
"file": "overwolf-plugin-sample.dll",
"class": "overwolf.samples.plugins.SamplePlugin"
}
}
}
namespace overwolf.sample.plugins
{
public class SamplePlugin : IDisposable
{
internal bool _isDisposed = false;
public void DoSomeFunStuff(Action<object> callback)
{
callback("Hello World!");
}
public void Dispose()
{
// No need to dispose, if we already disposed everything
if(_isDisposed) return;
// Do cleanup of resources, like you would in C# normally
_isDisposed = true;
}
}
}
function OverwolfPluginUsing(plugin, actionBlock) {
actionBlock(plugin);
plugin.dispose();
plugin = null;
};
/* Usage */
OverwolfPluginUsing(new OverwolfPlugin('OverwolfSamplePlugin', true), plugin => {
/* Do the plugin work */
plugin.initialize(status => {
if(!status) {
console.error('Could not load plugin');
return;
}
/* Rest of the code */
});
});
/**
* Taken from https://github.com/overwolf/overwolf-plugin-sample/blob/master/sampleapp/overwolfplugin.js
* Modified by adding a dispose-method, to call, if the plugin supports it, for cleanup
*/
function OverwolfPlugin(extraObjectNameInManifest, addNameToObject) {
var _pluginInstance = null;
var _extraObjectName = extraObjectNameInManifest;
var _addNameToObject = addNameToObject;
// public
this.initialize = function(callback) {
return _initialize(callback);
}
this.initialized = function() {
return _pluginInstance != null;
};
this.get = function() {
return _pluginInstance;
};
/**
* Method that disposes the current plugin instance to release resources
* @returns bool
*/
this.dispose = function() {
// If we don't have an instance of the plugin, or the plugin does not have a Dispose-method defined, we won't do anything
if (_pluginInstance === null || typeof(_pluginInstance.Dispose) === 'undefined') return true;
// Execute the Dispose-method on the plugin.
try {
_pluginInstance.Dispose();
_pluginInstance = null;
return true;
} catch(e) {
console.error('Error disposing plugin', e);
return false;
}
}
// privates
function _initialize(callback) {
var proxy = null;
try {
proxy = overwolf.extensions.current.getExtraObject;
} catch(e) {
console.error(
"overwolf.extensions.current.getExtraObject doesn't exist!");
return callback(false);
}
proxy(_extraObjectName, function(result) {
if (result.status != "success") {
console.error(
"failed to create " + _extraObjectName + " object: " + result);
return callback(false);
}
_pluginInstance = result.object;
if (_addNameToObject) {
_pluginInstance._PluginName_ = _extraObjectName;
}
return callback(true);
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment