Last active
June 24, 2020 07:32
-
-
Save itssimple/01baae84a5c10bb56f6a46a7eed66dfa to your computer and use it in GitHub Desktop.
Overwolf Plugin, Disposable
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
/* Only including the plugin-part here */ | |
"extra-objects": { | |
"OverwolfSamplePlugin": { | |
"file": "overwolf-plugin-sample.dll", | |
"class": "overwolf.samples.plugins.SamplePlugin" | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 */ | |
}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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