Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion brewlib
Submodule brewlib updated 1 files
+2 −2 Audio/AudioChannel.cs
22 changes: 19 additions & 3 deletions editor/Scripting/ScriptCompiler.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.Emit;
using Microsoft.VisualBasic.ApplicationServices;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System;
using System.Linq;
using System.Reflection;
using System.Reflection.Metadata;
using System.Text;

namespace StorybrewEditor.Scripting
Expand All @@ -15,23 +19,35 @@ public static void Compile(string[] sourcePaths, string outputPath, IEnumerable<
{
Debug.Print($"{nameof(Scripting)}: Compiling {string.Join(", ", sourcePaths)}");

var syntaxTrees = sourcePaths.Select(path => SyntaxFactory.ParseSyntaxTree(File.ReadAllText(path), path: path)).ToArray();
var syntaxTrees = sourcePaths.Select(path => SyntaxFactory.ParseSyntaxTree(File.ReadAllText(path), path: path));
var references = new List<MetadataReference>();

foreach (var assembly in referencedAssemblies)
if (File.Exists(assembly))
references.Add(MetadataReference.CreateFromFile(assembly));
{


//binarie: I have no idea why loading the assembly directly works
//i think maybe the editor needs to have the assembly stored in memory to correctly compile?
Assembly.LoadFrom(assembly);
PortableExecutableReference metaRef = MetadataReference.CreateFromFile(assembly);

references.Add(metaRef);
}

var compilation = CSharpCompilation.Create(
assemblyName: Path.GetFileNameWithoutExtension(outputPath),
syntaxTrees: syntaxTrees,
references: references,
syntaxTrees: syntaxTrees,
options: new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary));


EmitResult result;
using (var stream = new FileStream(outputPath, FileMode.Create, FileAccess.Write))
result = compilation.Emit(stream);



if (!result.Success)
{
var diagnostics = result.Diagnostics
Expand Down
9 changes: 7 additions & 2 deletions editor/Scripting/ScriptContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.Loader;

namespace StorybrewEditor.Scripting
Expand Down Expand Up @@ -94,7 +95,8 @@ public TScript CreateScript()
{
currentVersion = localTargetVersion;

if (disposedValue) throw new ObjectDisposedException(nameof(ScriptContainer<TScript>));
ObjectDisposedException.ThrowIf(disposedValue, this);
//if (disposedValue) throw new ObjectDisposedException(nameof(ScriptContainer<TScript>));

try
{
Expand All @@ -105,6 +107,7 @@ public TScript CreateScript()
}

var assemblyPath = Path.Combine(CompiledScriptsPath, $"{Guid.NewGuid()}.dll");

ScriptCompiler.Compile(SourcePaths, assemblyPath, ReferencedAssemblies);

var contextName = $"{Name} {Id}";
Expand All @@ -113,7 +116,9 @@ public TScript CreateScript()

try
{
scriptType = assemblyLoadContext.LoadFromAssemblyPath(assemblyPath).GetType(ScriptTypeName);
Assembly assembly = assemblyLoadContext.LoadFromAssemblyPath(assemblyPath);

scriptType = assembly.GetType(ScriptTypeName);
if (scriptType == null)
throw new TypeLoadException($"Type {ScriptTypeName} was not found in assembly");

Expand Down
62 changes: 61 additions & 1 deletion editor/Scripting/ScriptManager.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
using BrewLib.Data;
using BrewLib.Util;
using StorybrewCommon.Scripting;
using StorybrewEditor.Storyboarding;
using StorybrewEditor.Util;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Xml;

namespace StorybrewEditor.Scripting
{
Expand Down Expand Up @@ -176,7 +181,62 @@ private void updateSolutionFiles()
Directory.CreateDirectory(vsCodePath);

var csProjPath = Path.Combine(ScriptsPath, "scripts.csproj");
File.WriteAllBytes(csProjPath, resourceContainer.GetBytes("project/scripts.csproj", ResourceSource.Embedded | ResourceSource.Relative));
var document = new XmlDocument() { PreserveWhitespace = false, };
try
{
using (var stream = resourceContainer.GetStream("project/scripts.csproj", ResourceSource.Embedded | ResourceSource.Relative))
{

document.Load(stream);
}

var xmlns = document.DocumentElement.GetAttribute("xmlns");
//var compileGroup = document.CreateElement("ItemGroup", xmlns);
//document.DocumentElement.AppendChild(compileGroup);
//foreach (var path in Directory.EnumerateFiles(ScriptsPath, "*.cs", SearchOption.AllDirectories))
//{
// var relativePath = PathHelper.GetRelativePath(ScriptsPath, path);

// var compileNode = document.CreateElement("Compile", xmlns);
// compileNode.SetAttribute("Include", relativePath);
// compileGroup.AppendChild(compileNode);
//}

var referencedAssembliesGroup = document.CreateElement("ItemGroup", xmlns);
document.DocumentElement.AppendChild(referencedAssembliesGroup);
var importedAssemblies = referencedAssemblies.Where(e => !Project.DefaultAssemblies.Contains(e));
foreach (var path in importedAssemblies)
{
//Trace.WriteLine($"{path} is being imported");
var relativePath = PathHelper.GetRelativePath(ScriptsPath, path);

var compileNode = document.CreateElement("Reference", xmlns);

AssemblyName importedAssembly = AssemblyName.GetAssemblyName(path);
Trace.Assert(importedAssembly != null);

compileNode.SetAttribute("Include", importedAssembly.Name);
//var hintPath = document.CreateElement("HintPath", xmlns);
//hintPath.AppendChild(document.CreateTextNode(@$"..\..\{relativePath}"));
//compileNode.AppendChild(hintPath);

var hintPath = document.CreateElement("HintPath", xmlns);
hintPath.AppendChild(document.CreateTextNode(@$"{relativePath}"));
compileNode.AppendChild(hintPath);

//hintPath = document.CreateElement("HintPath", xmlns);
//hintPath.AppendChild(document.CreateTextNode(@$"{path}"));
//compileNode.AppendChild(hintPath);

referencedAssembliesGroup.AppendChild(compileNode);
}
document.Save(csProjPath);
}
catch (Exception e)
{
Trace.Fail($"Failed to update scripts.csproj: {e}");
//Trace.WriteLine($"Failed to update scripts.csproj: {e}");
}
}

#region IDisposable Support
Expand Down