Skip to content

Commit

Permalink
Merge pull request #30 from MythicalLTD/20-add-plugins-so-users-can-m…
Browse files Browse the repository at this point in the history
…ake-plugins-for-the-application

Add plugins
  • Loading branch information
NaysKutzu authored Jun 2, 2023
2 parents ab59812 + 3cf6da6 commit 652c40d
Show file tree
Hide file tree
Showing 8 changed files with 198 additions and 2 deletions.
1 change: 1 addition & 0 deletions FrmServerController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
using System.Threading.Tasks;
using System.Windows.Forms;


namespace PteroController
{
public partial class FrmServerController : Form
Expand Down
34 changes: 32 additions & 2 deletions Program.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Windows.Forms;

Expand All @@ -20,7 +21,8 @@ static void Main(string[] args)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
//EXAMPLE OF A CLI COMMAND

// EXAMPLE OF A CLI COMMAND
if (args.Contains("-version"))
{
AttachConsole(ATTACH_PARENT_PROCESS);
Expand All @@ -32,7 +34,35 @@ static void Main(string[] args)
Console.WriteLine("Press any key to exit");
}

Application.Run(new FrmLogin());
if(args.Contains("-debug"))
{
AttachConsole(ATTACH_PARENT_PROCESS);
Console.Title = "PteroController | CLI";
Console.WriteLine("@echo off");
Console.Clear();
Console.WriteLine("PteroController version " + appversion + " by MythicalSystems");
Console.WriteLine("");
Console.WriteLine("DEBUG MODE ACTIVE");
Console.WriteLine("Please do not use this mode if you are not a developer");
try
{
PteroControllerLoader pluginLoader = new PteroControllerLoader();
pluginLoader.LoadPlugins();
Application.Run(new FrmLogin());
}
catch (Exception ex)
{
Console.WriteLine("An error occurred: " + ex.Message);
}
return;
}
else
{
PteroControllerLoader pluginLoader = new PteroControllerLoader();
pluginLoader.LoadPlugins();
Application.Run(new FrmLogin());
}

}
}
}
1 change: 1 addition & 0 deletions PteroController.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,7 @@
</Compile>
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="PteroControllerLoader.cs" />
<EmbeddedResource Include="FrmAlert.resx">
<DependentUpon>FrmAlert.cs</DependentUpon>
</EmbeddedResource>
Expand Down
124 changes: 124 additions & 0 deletions PteroControllerLoader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
//
// PteroController | PluginLoader
//
// HI THERE THANKS FOR CHECKING THIS FILE OUT!
// IF YOU ARE LOOKING FOR THE DEAFULT CODE TO MAKE A PLUGIN HERE YOU GO:
/*
using System;
namespace PteroControllerPlugin
{
public class PluginInit
{
private string _name = "ExamplePlugin";
private string _version = "1.0.0";
private string _author = "NaysKutzu";
public string Name
{
get { return _name; }
}
public string Version
{
get { return _version; }
}
public string Author
{
get { return _author; }
}
public PluginInit()
{
sayhello();
}
private void sayhello()
{
Console.WriteLine("Hello, World from plugin!");
}
}
}
*/
// WARNING: DO NOT EDIT THE NAMESPACE OR THE PluginInit name this will not load on another pc!
using System;
using System.CodeDom.Compiler;
using System.IO;
using System.Reflection;
using Microsoft.CSharp;

namespace PteroController
{
public class PluginInfo
{
public string Name { get; set; }
public string Version { get; set; }
public string Author { get; set; }
}

public class PteroControllerLoader
{
private const string FolderPath = "plugins";

public void CheckAndCreatePluginFolder()
{
if (!Directory.Exists(FolderPath))
{
Directory.CreateDirectory(FolderPath);
Console.WriteLine("Plugin folder created successfully.");
}
}

public void LoadPlugins()
{
CheckAndCreatePluginFolder();

string[] files = Directory.GetFiles(FolderPath, "*.cs");
foreach (string file in files)
{
string fileContent = File.ReadAllText(file);

CSharpCodeProvider codeProvider = new CSharpCodeProvider();
CompilerParameters compilerParameters = new CompilerParameters();
compilerParameters.GenerateInMemory = true;

CompilerResults compilerResults = codeProvider.CompileAssemblyFromSource(compilerParameters, fileContent);
if (compilerResults.Errors.HasErrors)
{
foreach (CompilerError error in compilerResults.Errors)
{
Console.WriteLine($"Error in file {Path.GetFileName(file)} at line {error.Line}: {error.ErrorText}");
}
}
else
{
Console.WriteLine($"File {Path.GetFileName(file)} compiled successfully.");
Assembly compiledAssembly = compilerResults.CompiledAssembly;
Type pluginType = compiledAssembly.GetType("PteroControllerPlugin.PluginInit");
object pluginInstance = Activator.CreateInstance(pluginType);
PluginInfo pluginInfo = GetPluginInfo(pluginInstance);
Console.WriteLine($"Loaded plugin: {pluginInfo.Name} (Version: {pluginInfo.Version}, Author: {pluginInfo.Author})");
}
}
}

private PluginInfo GetPluginInfo(object pluginInstance)
{
Type pluginType = pluginInstance.GetType();

PropertyInfo nameProperty = pluginType.GetProperty("Name");
PropertyInfo versionProperty = pluginType.GetProperty("Version");
PropertyInfo authorProperty = pluginType.GetProperty("Author");

PluginInfo pluginInfo = new PluginInfo
{
Name = nameProperty?.GetValue(pluginInstance)?.ToString(),
Version = versionProperty?.GetValue(pluginInstance)?.ToString(),
Author = authorProperty?.GetValue(pluginInstance)?.ToString()
};

return pluginInfo;
}
}
}
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ No currently the app is still in development, and we don't provide themes yet bu
### Im a developer where can i find the BunifUI files?
Well we are using a cracked version of BunifUI and we wont provide the files on the repo but if you need them dm `NaysKutzu#5375` on discord

### How can i develop my own plugin?
Well to make your own plugin for PteroController you will have to have some knowledge of c# and .net and if you alertly know all of that here you can find a the [documentation](https://docs.mythicalsystems.tech/PteroController/HowToMakeAPlugin)

## Authors

- [@NaysKutzu](https://github.com/NaysKutzu)
Expand Down
Binary file modified bin/Release/PteroController.exe
Binary file not shown.
Binary file modified bin/Release/PteroController.pdb
Binary file not shown.
37 changes: 37 additions & 0 deletions bin/Release/plugins/ExamplePlugin.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// WARNING: DO NOT EDIT THE NAMESPACE OR THE PluginInit name this will not load on another pc!
using System;

namespace PteroControllerPlugin
{
public class PluginInit
{
private string _name = "ExamplePlugin";
private string _version = "1.0.0";
private string _author = "NaysKutzu";

public string Name
{
get { return _name; }
}

public string Version
{
get { return _version; }
}

public string Author
{
get { return _author; }
}

public PluginInit()
{
sayhello();
}

private void sayhello()
{
Console.WriteLine("Hello, World from plugin!");
}
}
}

0 comments on commit 652c40d

Please sign in to comment.