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

Rewrite PluginBridge to accommodate the functions to the new plugin folder handling

This commit is contained in:
chylex 2016-11-14 06:14:14 +01:00
parent 84b7078873
commit bf224408a3
2 changed files with 39 additions and 37 deletions

View File

@ -107,10 +107,6 @@ public string GetPluginFolder(PluginFolder folder){
} }
} }
public string GetFullPathIfSafe(string relativePath){
return GetFullPathIfSafe(PluginFolder.Root, relativePath);
}
public string GetFullPathIfSafe(PluginFolder folder, string relativePath){ public string GetFullPathIfSafe(PluginFolder folder, string relativePath){
string rootFolder = GetPluginFolder(folder); string rootFolder = GetPluginFolder(folder);
string fullPath = Path.Combine(rootFolder, relativePath); string fullPath = Path.Combine(rootFolder, relativePath);

View File

@ -2,6 +2,7 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Text; using System.Text;
using TweetDck.Plugins.Enums;
using TweetDck.Plugins.Events; using TweetDck.Plugins.Events;
namespace TweetDck.Plugins{ namespace TweetDck.Plugins{
@ -18,35 +19,26 @@ private void manager_Reloaded(object sender, PluginLoadEventArgs e){
fileCache.Clear(); fileCache.Clear();
} }
private string GetFullPathIfSafe(int token, string path){ private string GetFullPathOrThrow(int token, PluginFolder folder, string path){
Plugin plugin = manager.GetPluginFromToken(token); Plugin plugin = manager.GetPluginFromToken(token);
return plugin == null ? string.Empty : plugin.GetFullPathIfSafe(path); string fullPath = plugin == null ? string.Empty : plugin.GetFullPathIfSafe(folder, path);
}
public void WriteFile(int token, string path, string contents){
string fullPath = GetFullPathIfSafe(token, path);
if (fullPath == string.Empty){
throw new Exception("File path has to be relative to the plugin folder.");
}
// ReSharper disable once AssignNullToNotNullAttribute
Directory.CreateDirectory(Path.GetDirectoryName(fullPath));
File.WriteAllText(fullPath, contents, Encoding.UTF8);
fileCache[fullPath] = contents;
}
public string ReadFile(int token, string path, bool cache){
string fullPath = GetFullPathIfSafe(token, path);
if (fullPath.Length == 0){ if (fullPath.Length == 0){
throw new Exception("File path has to be relative to the plugin folder."); switch(folder){
case PluginFolder.Data: throw new Exception("File path has to be relative to the plugin data folder.");
case PluginFolder.Root: throw new Exception("File path has to be relative to the plugin root folder.");
default: throw new Exception("Invalid folder type "+folder+", this is a "+Program.BrandName+" error.");
}
} }
else{
return fullPath;
}
}
private string ReadFileUnsafe(string fullPath, bool readCached){
string cachedContents; string cachedContents;
if (cache && fileCache.TryGetValue(fullPath, out cachedContents)){ if (readCached && fileCache.TryGetValue(fullPath, out cachedContents)){
return cachedContents; return cachedContents;
} }
@ -59,25 +51,39 @@ public string ReadFile(int token, string path, bool cache){
} }
} }
public void DeleteFile(int token, string path){ // Public methods
string fullPath = GetFullPathIfSafe(token, path);
public void WriteFile(int token, string path, string contents){
string fullPath = GetFullPathOrThrow(token, PluginFolder.Data, path);
// ReSharper disable once AssignNullToNotNullAttribute
Directory.CreateDirectory(Path.GetDirectoryName(fullPath));
File.WriteAllText(fullPath, contents, Encoding.UTF8);
fileCache[fullPath] = contents;
}
public string ReadFile(int token, string path, bool cache){
return ReadFileUnsafe(GetFullPathOrThrow(token, PluginFolder.Data, path), cache);
}
public void DeleteFile(int token, string path){
string fullPath = GetFullPathOrThrow(token, PluginFolder.Data, path);
if (fullPath.Length == 0){
throw new Exception("File path has to be relative to the plugin folder.");
}
fileCache.Remove(fullPath); fileCache.Remove(fullPath);
File.Delete(fullPath); File.Delete(fullPath);
} }
public bool CheckFileExists(int token, string path){ public bool CheckFileExists(int token, string path){
string fullPath = GetFullPathIfSafe(token, path); return File.Exists(GetFullPathOrThrow(token, PluginFolder.Data, path));
}
if (fullPath.Length == 0){ public string ReadFileRoot(int token, string path){
throw new Exception("File path has to be relative to the plugin folder."); return ReadFileUnsafe(GetFullPathOrThrow(token, PluginFolder.Root, path), true);
} }
return File.Exists(fullPath); public bool CheckFileExistsRoot(int token, string path){
return File.Exists(GetFullPathOrThrow(token, PluginFolder.Root, path));
} }
} }
} }