2017-10-27 01:00:52 +08:00
|
|
|
/*
|
|
|
|
Plugin Loader for Blockbench
|
|
|
|
By JannisX11
|
|
|
|
*/
|
|
|
|
var onUninstall, onInstall;
|
|
|
|
var Plugins = {
|
2018-04-15 22:06:43 +08:00
|
|
|
apipath: 'https://blockbench.net/api/plugins.json',
|
2017-10-27 01:00:52 +08:00
|
|
|
Vue: [], //Vue Object
|
|
|
|
installed: [], //Simple List of Names
|
|
|
|
json: undefined, //Json from website
|
|
|
|
data: [], //Vue Object Data
|
|
|
|
loadingStep: false,
|
|
|
|
updateSearch: function() {
|
|
|
|
Plugins.Vue._data.showAll = !Plugins.Vue._data.showAll
|
|
|
|
Plugins.Vue._data.showAll = !Plugins.Vue._data.showAll
|
|
|
|
},
|
|
|
|
devReload: function() {
|
2018-03-29 02:48:11 +08:00
|
|
|
var reloads = 0;
|
2017-10-27 01:00:52 +08:00
|
|
|
Plugins.data.forEach(function(pl) {
|
|
|
|
if (pl.fromFile) {
|
|
|
|
pl.reload()
|
2018-03-29 02:48:11 +08:00
|
|
|
reloads++;
|
2017-10-27 01:00:52 +08:00
|
|
|
}
|
|
|
|
})
|
2018-03-29 02:48:11 +08:00
|
|
|
console.log('Reloaded '+reloads+ ' plugin'+pluralS(reloads))
|
2017-10-27 01:00:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isApp) {
|
2018-03-29 02:48:11 +08:00
|
|
|
Plugins.path = app.app.getPath('userData')+osfs+'plugins'+osfs
|
2017-10-27 01:00:52 +08:00
|
|
|
fs.readdir(Plugins.path, function(err) {
|
|
|
|
if (err) {
|
|
|
|
fs.mkdir(Plugins.path, function(a) {})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
Plugins.apipath = '../api/plugins.json'
|
|
|
|
}
|
2018-04-15 22:06:43 +08:00
|
|
|
$.getJSON('https://blockbench.net/api/plugins.json', function(data) {
|
2017-10-27 01:00:52 +08:00
|
|
|
Plugins.json = data
|
|
|
|
if (Plugins.loadingStep === true) {
|
|
|
|
loadInstalledPlugins()
|
|
|
|
} else {
|
|
|
|
Plugins.loadingStep = true
|
|
|
|
}
|
|
|
|
}).fail(function() {
|
|
|
|
console.log('Could not connect to plugin server')
|
|
|
|
$('#plugin_available_empty').text('Could not connect to plugin server')
|
|
|
|
if (Plugins.loadingStep === true) {
|
|
|
|
loadInstalledPlugins()
|
|
|
|
} else {
|
|
|
|
Plugins.loadingStep = true
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
$(document).ready(function() {
|
|
|
|
if (Plugins.loadingStep === true) {
|
|
|
|
loadInstalledPlugins()
|
|
|
|
} else {
|
|
|
|
Plugins.loadingStep = true
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
function loadInstalledPlugins() {
|
|
|
|
var storage_data = localStorage.getItem('installed_plugins')
|
|
|
|
if (storage_data !== null) {
|
|
|
|
Plugins.installed = JSON.parse(storage_data)
|
|
|
|
}
|
|
|
|
if (Plugins.json !== undefined) {
|
|
|
|
for (var id in Plugins.json) {
|
|
|
|
var plugin = Plugins.json[id]
|
|
|
|
var obj = {
|
|
|
|
id: id,
|
|
|
|
title: plugin.title,
|
|
|
|
author: plugin.author,
|
|
|
|
description: plugin.description,
|
|
|
|
icon: plugin.icon,
|
|
|
|
variant: plugin.variant,
|
2018-03-29 02:48:11 +08:00
|
|
|
min_version: plugin.min_version,
|
2017-10-27 01:00:52 +08:00
|
|
|
installed: Plugins.installed.includes(id)
|
|
|
|
}
|
|
|
|
if (obj.installed) {
|
2018-03-29 02:48:11 +08:00
|
|
|
if (isApp) {
|
|
|
|
downloadPlugin(id)
|
|
|
|
} else {
|
|
|
|
loadPlugin(id)
|
|
|
|
}
|
2017-10-27 01:00:52 +08:00
|
|
|
}
|
|
|
|
Plugins.data.push(obj)
|
|
|
|
Plugins.data.sort(function(a,b) {
|
|
|
|
return sort_collator.compare(a.title, b.title)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
} else if (Plugins.installed.length > 0) {
|
2018-01-03 03:34:44 +08:00
|
|
|
//Only show downloaded plugins in the plugin window
|
2017-10-27 01:00:52 +08:00
|
|
|
Plugins.installed.forEach(function(id) {
|
|
|
|
loadPlugin(id, function() {
|
|
|
|
//Plugin Data Comes from the plugin file
|
|
|
|
if (plugin_data === undefined) return;
|
|
|
|
var obj = {
|
|
|
|
id: id,
|
|
|
|
title: plugin_data.title,
|
|
|
|
author: plugin_data.author,
|
|
|
|
description: plugin_data.description,
|
|
|
|
icon: plugin_data.icon,
|
|
|
|
variant: plugin_data.variant,
|
2018-03-29 02:48:11 +08:00
|
|
|
min_version: plugin_data.min_version,
|
2017-10-27 01:00:52 +08:00
|
|
|
installed: true
|
|
|
|
}
|
|
|
|
Plugins.data.push(obj)
|
|
|
|
Plugins.data.sort(function(a,b) {
|
|
|
|
return sort_collator.compare(a.title, b.title)
|
|
|
|
});
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
if (Plugins.installed.length > 0) {
|
|
|
|
console.log('Loaded '+Plugins.installed.length+' plugin'+pluralS(Plugins.installed.length))
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Plugins.Vue = new Vue({
|
|
|
|
el: '#plugin_list',
|
|
|
|
data: {
|
|
|
|
showAll: false,
|
|
|
|
items: Plugins.data
|
|
|
|
},
|
|
|
|
computed: {
|
|
|
|
installedPlugins() {
|
|
|
|
var name = $('#plugin_search_bar').val().toUpperCase()
|
|
|
|
return this.items.filter(item => {
|
2018-03-29 02:48:11 +08:00
|
|
|
if (this.showAll !== item.installed) {
|
2017-10-27 01:00:52 +08:00
|
|
|
if (name.length > 0) {
|
|
|
|
return (
|
|
|
|
item.id.toUpperCase().includes(name) ||
|
|
|
|
item.title.toUpperCase().includes(name) ||
|
|
|
|
item.description.toUpperCase().includes(name) ||
|
|
|
|
item.author.toUpperCase().includes(name)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
})
|
|
|
|
}
|
|
|
|
},
|
|
|
|
methods: {
|
|
|
|
install: function(event) {
|
|
|
|
var id = $(event.target.parentElement.parentElement).attr('plugin')
|
|
|
|
if (isApp) {
|
|
|
|
downloadPlugin(id)
|
|
|
|
} else {
|
|
|
|
loadPlugin(id)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
uninstall: function() {
|
|
|
|
var id = $(event.target.parentElement.parentElement).attr('plugin')
|
|
|
|
uninstallPlugin(id)
|
|
|
|
},
|
|
|
|
update: function() {
|
|
|
|
if (isApp) {
|
|
|
|
var id = $(event.target.parentElement.parentElement).attr('plugin')
|
|
|
|
downloadPlugin(id)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
checkIfInstallable: function(plugin) {
|
|
|
|
var result =
|
|
|
|
plugin.variant === 'both' ||
|
|
|
|
(
|
|
|
|
isApp === (plugin.variant === 'desktop') &&
|
|
|
|
isApp !== (plugin.variant === 'web')
|
|
|
|
);
|
2018-03-29 02:48:11 +08:00
|
|
|
if (result && plugin.min_version) {
|
|
|
|
result = compareVersions(plugin.min_version, appVersion) ? 'outdated_client' : true
|
|
|
|
}
|
|
|
|
return result;
|
2017-10-27 01:00:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
function saveInstalledPlugins() {
|
|
|
|
localStorage.setItem('installed_plugins', JSON.stringify(Plugins.installed))
|
|
|
|
hideDialog()
|
|
|
|
}
|
2018-01-03 03:34:44 +08:00
|
|
|
function loadPlugin(id, cb, install, allow_update) {
|
2017-10-27 01:00:52 +08:00
|
|
|
if (isApp === true) {
|
|
|
|
$.getScript(Plugins.path + id + '.js', function(a) {
|
|
|
|
if (onUninstall) {
|
|
|
|
Plugins.data.findInArray('id', id).uninstall = onUninstall
|
|
|
|
onUninstall = undefined
|
|
|
|
}
|
|
|
|
if (install && onInstall) {
|
|
|
|
onInstall()
|
|
|
|
}
|
|
|
|
onInstall = undefined
|
|
|
|
if (cb !== undefined) cb()
|
|
|
|
}).fail(function() {
|
|
|
|
console.log('Could not find file of plugin "'+id+'". Uninstalling it instead.')
|
2017-11-17 05:23:41 +08:00
|
|
|
uninstallPlugin(id)
|
|
|
|
saveInstalledPlugins()
|
2017-10-27 01:00:52 +08:00
|
|
|
})
|
|
|
|
} else {
|
2018-04-15 22:06:43 +08:00
|
|
|
$.getScript('https://blockbench.net/api/plugins/'+id+'.js', function() {
|
2017-10-27 01:00:52 +08:00
|
|
|
if (onUninstall) {
|
|
|
|
Plugins.data.findInArray('id', id).uninstall = onUninstall
|
|
|
|
onUninstall = undefined
|
|
|
|
}
|
|
|
|
if (install && onInstall) {
|
|
|
|
onInstall()
|
|
|
|
}
|
|
|
|
onInstall = undefined
|
|
|
|
if (cb) cb()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
if (Plugins.installed.includes(id) === false) {
|
|
|
|
Plugins.installed.push(id)
|
|
|
|
}
|
|
|
|
Plugins.data.findInArray('id', id).installed = true
|
|
|
|
}
|
|
|
|
function loadPluginFromFile() {
|
|
|
|
function readFromPluginFile(content, path, hideWarning) {
|
2018-03-29 02:48:11 +08:00
|
|
|
onInstall = undefined
|
2017-10-27 01:00:52 +08:00
|
|
|
if (!hideWarning) {
|
|
|
|
if (isApp) {
|
|
|
|
if (!confirm('Do you want to allow this plugin to make changes to your PC? Only load plugins from people you trust.')) return;
|
|
|
|
} else {
|
|
|
|
if (!confirm('Do you want to load this plugin? Only load plugins from people you trust.')) return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
eval(content)
|
|
|
|
} catch (err) {
|
|
|
|
showQuickMessage('Invalid Plugin File, See Console')
|
|
|
|
console.error(err)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var obj = {
|
2018-03-29 02:48:11 +08:00
|
|
|
author: 'unknown',
|
|
|
|
icon: 'extension',
|
2017-10-27 01:00:52 +08:00
|
|
|
installed: true,
|
|
|
|
id: 'test',
|
|
|
|
title: 'Plugin',
|
|
|
|
variant: 'both',
|
|
|
|
description: '',
|
|
|
|
fromFile: true,
|
|
|
|
filePath: path,
|
|
|
|
uninstall: function() {
|
|
|
|
var index = Plugins.data.indexOf(this)
|
|
|
|
if (index >= 0) Plugins.data.splice(index, 1)
|
|
|
|
if (this.uninstallMethod) {
|
|
|
|
this.uninstallMethod()
|
|
|
|
}
|
|
|
|
},
|
|
|
|
reload: function() {
|
|
|
|
if (isApp) {
|
|
|
|
obj.uninstall()
|
|
|
|
fs.readFile(path, 'utf-8', function (err, data) {
|
|
|
|
if (err) {
|
|
|
|
console.log(err)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
readFromPluginFile(data, path, true)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
},
|
|
|
|
uninstallMethod: false
|
|
|
|
}
|
|
|
|
$.extend(true, obj, plugin_data)
|
|
|
|
obj.uninstallMethod = onUninstall
|
|
|
|
onUninstall = undefined
|
2018-03-29 02:48:11 +08:00
|
|
|
if (onInstall) onInstall()
|
|
|
|
onInstall = undefined
|
2017-10-27 01:00:52 +08:00
|
|
|
Plugins.data.push(obj)
|
|
|
|
Plugins.data.sort(function(a,b) {
|
|
|
|
return sort_collator.compare(a.title, b.title)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
Blockbench.import('.js', readFromPluginFile, ['bbplugin', 'js'])
|
|
|
|
}
|
|
|
|
function downloadPlugin(id) {
|
|
|
|
//$('.uc_btn').attr('disabled', true)
|
|
|
|
|
|
|
|
var file = originalFs.createWriteStream(Plugins.path+id+'.js')
|
2018-04-15 22:06:43 +08:00
|
|
|
var request = https.get('https://blockbench.net/api/plugins/'+id+'.js', function(response) {
|
2017-10-27 01:00:52 +08:00
|
|
|
response.pipe(file);
|
|
|
|
response.on('end', function() {
|
|
|
|
setTimeout(function() {
|
|
|
|
loadPlugin(id, undefined, true)
|
|
|
|
}, 100)
|
|
|
|
})
|
|
|
|
});
|
|
|
|
}
|
|
|
|
function uninstallPlugin(id) {
|
|
|
|
if (isApp) {
|
|
|
|
var filepath = Plugins.path + id + '.js'
|
|
|
|
if (fs.existsSync(filepath)) {
|
|
|
|
fs.unlink(filepath, (err) => {
|
|
|
|
if (err) {
|
|
|
|
console.log(err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
//File does not exist
|
|
|
|
}
|
|
|
|
}
|
|
|
|
var index = Plugins.installed.indexOf(id)
|
|
|
|
if (index > -1) {
|
|
|
|
Plugins.installed.splice(index, 1)
|
|
|
|
}
|
|
|
|
var data_obj = Plugins.data.findInArray('id', id)
|
|
|
|
data_obj.installed = false
|
|
|
|
if (data_obj.uninstall) {
|
|
|
|
data_obj.uninstall()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
function switchPluginTabs(installed) {
|
|
|
|
$('#plugins .tab').removeClass('open')
|
|
|
|
if (installed) {
|
|
|
|
$('#installed_plugins').addClass('open')
|
|
|
|
Plugins.Vue._data.showAll = false
|
|
|
|
} else {
|
|
|
|
$('#all_plugins').addClass('open')
|
|
|
|
Plugins.Vue._data.showAll = true
|
|
|
|
}
|
|
|
|
}
|