Compare commits

...

3 Commits

Author SHA1 Message Date
morkt
d33d84ecf9 (CompilePlugins): add referenced assemblies. 2016-03-22 02:41:12 +04:00
morkt
cb32888ffd display plugins compilation errors. 2016-03-22 00:14:51 +04:00
morkt
2224d25063 runtime modules compilation implementation. 2016-03-22 00:14:17 +04:00
2 changed files with 58 additions and 2 deletions

View File

@@ -90,6 +90,7 @@ namespace GARbro.GUI
void WindowRendered ()
{
var compilation_error = FormatCatalog.Instance.LastError;
DirectoryViewModel vm = null;
try
{
@@ -105,6 +106,10 @@ namespace GARbro.GUI
}
ViewModel = vm;
lv_SelectItem (0);
if (compilation_error != null)
{
PopupError (compilation_error.Message, "GameRes library initialization error");
}
if (!vm.IsArchive)
SetStatusText (guiStrings.MsgReady);
}

View File

@@ -29,8 +29,11 @@ using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;
using System.IO;
using System.Linq;
using GameRes.Collections;
using System.CodeDom.Compiler;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using Microsoft.CSharp;
using GameRes.Collections;
namespace GameRes
{
@@ -77,12 +80,16 @@ namespace GameRes
private FormatCatalog ()
{
var assembly_location = Path.GetDirectoryName (System.Reflection.Assembly.GetExecutingAssembly().Location);
var plugins_dll = Path.Combine (assembly_location, "ArcPlugins.dll");
CompilePlugins (Path.Combine (assembly_location, "Plugins"), plugins_dll);
//An aggregate catalog that combines multiple catalogs
var catalog = new AggregateCatalog();
//Adds all the parts found in the same assembly as the Program class
catalog.Catalogs.Add (new AssemblyCatalog (typeof(FormatCatalog).Assembly));
//Adds parts matching pattern found in the directory of the assembly
catalog.Catalogs.Add (new DirectoryCatalog (Path.GetDirectoryName (System.Reflection.Assembly.GetExecutingAssembly().Location), "Arc*.dll"));
catalog.Catalogs.Add (new DirectoryCatalog (assembly_location, "Arc*.dll"));
//Create the CompositionContainer with the parts in the catalog
var container = new CompositionContainer (catalog);
@@ -218,6 +225,50 @@ namespace GameRes
var bin = new BinaryFormatter();
bin.Serialize (output, db);
}
private void CompilePlugins (string source_dir, string target)
{
try
{
if (!Directory.Exists (source_dir))
return;
var dir = new DirectoryInfo (source_dir);
var files = dir.EnumerateFiles ("*.cs");
if (!files.Any())
return;
if (File.Exists (target))
{
var target_info = new FileInfo (target);
var recent_plugin = files.OrderByDescending (f => f.LastWriteTime).First();
if (recent_plugin.LastWriteTime < target_info.LastWriteTime)
return;
}
var provider = new CSharpCodeProvider();
var parameters = new CompilerParameters { OutputAssembly = target };
#if DEBUG
parameters.IncludeDebugInformation = true;
#endif
parameters.ReferencedAssemblies.Add ("System.dll");
parameters.ReferencedAssemblies.Add ("System.ComponentModel.Composition.dll");
parameters.ReferencedAssemblies.Add (System.Reflection.Assembly.GetExecutingAssembly().Location);
var source = files.Select (f => f.FullName).ToArray();
var results = provider.CompileAssemblyFromFile (parameters, source);
if (results.Errors.HasErrors)
{
var error_text = new StringBuilder ("Plugins compilation failed due to errors.\r\n");
foreach (CompilerError error in results.Errors)
{
var filename = Path.GetFileName (error.FileName);
error_text.AppendFormat ("{0}:{1}: [{2}] {3}\r\n", filename, error.Line, error.ErrorNumber, error.ErrorText);
}
throw new ApplicationException (error_text.ToString());
}
}
catch (Exception X)
{
LastError = X;
}
}
}
[Serializable]