1
0
mirror of https://github.com/chylex/TweetDuck.git synced 2025-05-03 23:34:09 +02:00

Rewrite Plugin default config handling and config error reporting

This commit is contained in:
chylex 2016-09-18 20:29:31 +02:00
parent ba6ce072ac
commit 34955b7083
3 changed files with 67 additions and 56 deletions
Plugins
Resources/Scripts

View File

@ -13,6 +13,7 @@ class Plugin{
public string Version { get { return metadata["VERSION"]; } } public string Version { get { return metadata["VERSION"]; } }
public string Website { get { return metadata["WEBSITE"]; } } public string Website { get { return metadata["WEBSITE"]; } }
public string ConfigFile { get { return metadata["CONFIGFILE"]; } } public string ConfigFile { get { return metadata["CONFIGFILE"]; } }
public string ConfigDefault { get { return metadata["CONFIGDEFAULT"]; } }
public string RequiredVersion { get { return metadata["REQUIRES"]; } } public string RequiredVersion { get { return metadata["REQUIRES"]; } }
public PluginGroup Group { get; private set; } public PluginGroup Group { get; private set; }
public PluginEnvironment Environments { get; private set; } public PluginEnvironment Environments { get; private set; }
@ -31,13 +32,25 @@ public string FolderPath{
public bool HasConfig{ public bool HasConfig{
get{ get{
return ConfigFile.Length > 0; return ConfigFile.Length > 0 && GetFullPathIfSafe(ConfigFile).Length > 0;
} }
} }
public string ConfigPath{ public string ConfigPath{
get{ get{
return HasConfig ? Path.Combine(path, ConfigFile) : ""; return HasConfig ? Path.Combine(path, ConfigFile) : string.Empty;
}
}
public bool HasDefaultConfig{
get{
return ConfigDefault.Length > 0 && GetFullPathIfSafe(ConfigDefault).Length > 0;
}
}
public string DefaultConfigPath{
get{
return HasDefaultConfig ? Path.Combine(path, ConfigDefault) : string.Empty;
} }
} }
@ -50,6 +63,7 @@ public string ConfigPath{
{ "VERSION", "(unknown)" }, { "VERSION", "(unknown)" },
{ "WEBSITE", "" }, { "WEBSITE", "" },
{ "CONFIGFILE", "" }, { "CONFIGFILE", "" },
{ "CONFIGDEFAULT", "" },
{ "REQUIRES", "*" } { "REQUIRES", "*" }
}; };
@ -62,6 +76,18 @@ private Plugin(string path, PluginGroup group){
this.Environments = PluginEnvironment.None; this.Environments = PluginEnvironment.None;
} }
private void OnMetadataLoaded(){
string configPath = ConfigPath, defaultConfigPath = DefaultConfigPath;
if (configPath.Length > 0 && defaultConfigPath.Length > 0 && !File.Exists(configPath) && File.Exists(defaultConfigPath)){
try{
File.Copy(defaultConfigPath, configPath, false);
}catch(Exception e){
Program.HandleException("Could not generate a configuration file for '"+identifier+"' plugin.", e);
}
}
}
public string GetScriptPath(PluginEnvironment environment){ public string GetScriptPath(PluginEnvironment environment){
if (Environments.HasFlag(environment)){ if (Environments.HasFlag(environment)){
string file = environment.GetScriptFile(); string file = environment.GetScriptFile();
@ -72,6 +98,28 @@ public string GetScriptPath(PluginEnvironment environment){
} }
} }
public string GetFullPathIfSafe(string relativePath){
string fullPath = Path.Combine(path, relativePath);
try{
string folderPathName = new DirectoryInfo(path).FullName;
DirectoryInfo currentInfo = new DirectoryInfo(fullPath);
while(currentInfo.Parent != null){
if (currentInfo.Parent.FullName == folderPathName){
return fullPath;
}
currentInfo = currentInfo.Parent;
}
}
catch{
// ignore
}
return string.Empty;
}
public override string ToString(){ public override string ToString(){
return Identifier; return Identifier;
} }
@ -166,6 +214,8 @@ private static bool LoadMetadata(string path, Plugin plugin, out string error){
return false; return false;
} }
plugin.OnMetadataLoaded();
error = string.Empty; error = string.Empty;
return true; return true;
} }

View File

@ -20,30 +20,7 @@ private void manager_Reloaded(object sender, PluginLoadEventArgs e){
private string GetFullPathIfSafe(int token, string path){ private string GetFullPathIfSafe(int token, string path){
Plugin plugin = manager.GetPluginFromToken(token); Plugin plugin = manager.GetPluginFromToken(token);
return plugin == null ? string.Empty : plugin.GetFullPathIfSafe(path);
if (plugin == null){
return string.Empty;
}
string fullPath = Path.Combine(plugin.FolderPath, path);
try{
string folderPathName = new DirectoryInfo(plugin.FolderPath).FullName;
DirectoryInfo currentInfo = new DirectoryInfo(fullPath);
while(currentInfo.Parent != null){
if (currentInfo.Parent.FullName == folderPathName){
return fullPath;
}
currentInfo = currentInfo.Parent;
}
}
catch{
// ignore
}
return string.Empty;
} }
public void WriteFile(int token, string path, string contents){ public void WriteFile(int token, string path, string contents){

View File

@ -2,8 +2,13 @@
// //
// Block: Setup a simple JavaScript object configuration loader. // Block: Setup a simple JavaScript object configuration loader.
// //
(function(){ window.TDPF_loadConfigurationFile = function(pluginObject, fileNameUser, fileNameDefault, onSuccess, onFailure){
var continueLoading = function(token, identifier, fileName, onSuccess, onFailure){ var identifier = pluginObject.$id;
var token = pluginObject.$token;
$TDP.checkFileExists(token, fileNameUser).then(exists => {
var fileName = exists ? fileNameUser : fileNameDefault;
$TDP.readFile(token, fileName, true).then(contents => { $TDP.readFile(token, fileName, true).then(contents => {
var obj; var obj;
@ -20,34 +25,13 @@
onSuccess && onSuccess(obj); onSuccess && onSuccess(obj);
}).catch(err => { }).catch(err => {
if (!(onFailure && onFailure(err))){ if (!(onFailure && onFailure(err))){
$TD.alert("warning", "Problem loading '"+fileName+"' file for '"+identifier+"' plugin: "+err); $TD.alert("warning", "Problem loading '"+fileName+"' file for '"+identifier+"' plugin: "+err.message);
} }
}); });
}; }).catch(err => {
if (!(onFailure && onFailure(err))){
window.TDPF_loadConfigurationFile = function(pluginObject, fileNameUser, fileNameDefault, onSuccess, onFailure){ $TD.alert("warning", "Problem checking '"+fileNameUser+"' file for '"+identifier+"' plugin: "+err.message);
var identifier = pluginObject.$id; }
var token = pluginObject.$token; });
};
$TDP.checkFileExists(token, fileNameUser).then(exists => {
if (!exists){
$TDP.readFile(token, fileNameDefault, true).then(contents => {
$TDP.writeFile(token, fileNameUser, contents);
continueLoading(token, identifier, fileNameUser, onSuccess, onFailure);
}).catch(err => {
if (!(onFailure && onFailure(err))){
$TD.alert("warning", "Problem generating '"+fileNameUser+"' file for '"+identifier+"' plugin: "+err);
}
});
}
else{
continueLoading(token, identifier, fileNameUser, onSuccess, onFailure);
}
}).catch(err => {
if (!(onFailure && onFailure(err))){
$TD.alert("warning", "Problem checking '"+fileNameUser+"' file for '"+identifier+"' plugin: "+err);
}
});
};
})();
})($TDP); })($TDP);