Permalink
Browse files

Added LICENSE

  • Loading branch information...
1 parent 9614ff0 commit f34d4a480a9ed941bd78b9d83e31c32f0c15c45d @fekberg committed Apr 26, 2012
View
21 LICENSE
@@ -0,0 +1,21 @@
+ The MIT License
@gparent
gparent Apr 26, 2012

I don't know if you noticed, but this commit fucks up the formatting pretty bad (see CodeExecuter.cs)

@fekberg
fekberg Apr 26, 2012

Thanks for spotting that gparent2. I've made a new commit with the formatting fixed.

@gparent
gparent Apr 26, 2012

No problem, it just happens that this project is in my long "list of things to eventually fork and do something slightly different with" so I spotted it :)

@fekberg
fekberg Apr 27, 2012

Cool, looking forward too that!

+
+Copyright (c) 2012 Filip Ekberg <mail@filipekberg.se>
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE
View
93 Rossie/Rossie.6.0.ReSharper.user
@@ -0,0 +1,93 @@
+<Configuration>
+ <RecentFiles>
+ <RecentFiles>
+ <File id="479AEF8F-3698-4096-99F5-E2C5B903CA34/f:RossieEngineServiceInstaller.cs" caret="0" fromTop="0" />
+ <File id="479AEF8F-3698-4096-99F5-E2C5B903CA34/f:RossieEngineServiceInstaller.Designer.cs" caret="0" fromTop="0" />
+ <File id="2B4B589C-B795-40BC-A105-585790922C31/f:MainWindow.xaml" caret="662" fromTop="11" />
+ <File id="2B4B589C-B795-40BC-A105-585790922C31/f:ICommandService.cs" caret="197" fromTop="9" />
+ <File id="1D5A2399-EBFF-41AD-B1A1-38B60FFCD767/f:Validator.cs" caret="880" fromTop="25" />
+ <File id="479AEF8F-3698-4096-99F5-E2C5B903CA34/f:RossieEngineService.Designer.cs" caret="0" fromTop="0" />
+ <File id="479AEF8F-3698-4096-99F5-E2C5B903CA34/f:CommandServer.cs" caret="622" fromTop="19" />
+ <File id="479AEF8F-3698-4096-99F5-E2C5B903CA34/f:CommandService.cs" caret="924" fromTop="32" />
+ <File id="479AEF8F-3698-4096-99F5-E2C5B903CA34/f:ICommandService.cs" caret="197" fromTop="9" />
+ <File id="479AEF8F-3698-4096-99F5-E2C5B903CA34/f:RossieEngineService.cs" caret="676" fromTop="22" />
+ <File id="2B4B589C-B795-40BC-A105-585790922C31/f:MainWindow.xaml.cs" caret="1617" fromTop="19" />
+ <File id="1D5A2399-EBFF-41AD-B1A1-38B60FFCD767/f:CodeExecuter.cs" caret="1306" fromTop="13" />
+ <File id="479AEF8F-3698-4096-99F5-E2C5B903CA34/f:Program.cs" caret="414" fromTop="12" />
+ </RecentFiles>
+ <RecentEdits>
+ <File id="479AEF8F-3698-4096-99F5-E2C5B903CA34/f:Program.cs" caret="28" fromTop="0" />
+ <File id="2B4B589C-B795-40BC-A105-585790922C31/f:MainWindow.xaml" caret="580" fromTop="4" />
+ <File id="2B4B589C-B795-40BC-A105-585790922C31/f:MainWindow.xaml.cs" caret="410" fromTop="17" />
+ <File id="2B4B589C-B795-40BC-A105-585790922C31/f:MainWindow.xaml.cs" caret="148" fromTop="0" />
+ <File id="2B4B589C-B795-40BC-A105-585790922C31/f:MainWindow.xaml.cs" caret="2475" fromTop="43" />
+ <File id="479AEF8F-3698-4096-99F5-E2C5B903CA34/f:RossieEngineService.cs" caret="864" fromTop="29" />
+ <File id="479AEF8F-3698-4096-99F5-E2C5B903CA34/f:CommandService.cs" caret="1312" fromTop="22" />
+ <File id="479AEF8F-3698-4096-99F5-E2C5B903CA34/f:CommandServer.cs" caret="804" fromTop="21" />
+ <File id="2B4B589C-B795-40BC-A105-585790922C31/f:MainWindow.xaml.cs" caret="6299" fromTop="46" />
+ <File id="2B4B589C-B795-40BC-A105-585790922C31/f:MainWindow.xaml.cs" caret="1294" fromTop="22" />
+ <File id="2B4B589C-B795-40BC-A105-585790922C31/f:MainWindow.xaml" caret="662" fromTop="11" />
+ <File id="1D5A2399-EBFF-41AD-B1A1-38B60FFCD767/f:CodeExecuter.cs" caret="2961" fromTop="27" />
+ <File id="1D5A2399-EBFF-41AD-B1A1-38B60FFCD767/f:CodeExecuter.cs" caret="762" fromTop="5" />
+ <File id="1D5A2399-EBFF-41AD-B1A1-38B60FFCD767/f:CodeExecuter.cs" caret="1888" fromTop="19" />
+ <File id="1D5A2399-EBFF-41AD-B1A1-38B60FFCD767/f:CodeExecuter.cs" caret="2968" fromTop="10" />
+ <File id="1D5A2399-EBFF-41AD-B1A1-38B60FFCD767/f:CodeExecuter.cs" caret="3507" fromTop="33" />
+ <File id="1D5A2399-EBFF-41AD-B1A1-38B60FFCD767/f:CodeExecuter.cs" caret="311" fromTop="11" />
+ <File id="1D5A2399-EBFF-41AD-B1A1-38B60FFCD767/f:CodeExecuter.cs" caret="3981" fromTop="18" />
+ <File id="1D5A2399-EBFF-41AD-B1A1-38B60FFCD767/f:CodeExecuter.cs" caret="3981" fromTop="30" />
+ <File id="1D5A2399-EBFF-41AD-B1A1-38B60FFCD767/f:CodeExecuter.cs" caret="3981" fromTop="34" />
+ <File id="1D5A2399-EBFF-41AD-B1A1-38B60FFCD767/f:CodeExecuter.cs" caret="3675" fromTop="17" />
+ <File id="479AEF8F-3698-4096-99F5-E2C5B903CA34/f:CommandServer.cs" caret="804" fromTop="7" />
+ <File id="479AEF8F-3698-4096-99F5-E2C5B903CA34/f:CommandServer.cs" caret="109" fromTop="6" />
+ <File id="479AEF8F-3698-4096-99F5-E2C5B903CA34/f:CommandServer.cs" caret="615" fromTop="18" />
+ <File id="479AEF8F-3698-4096-99F5-E2C5B903CA34/f:CommandService.cs" caret="68" fromTop="0" />
+ <File id="479AEF8F-3698-4096-99F5-E2C5B903CA34/f:CommandService.cs" caret="163" fromTop="6" />
+ <File id="479AEF8F-3698-4096-99F5-E2C5B903CA34/f:RossieEngineService.cs" caret="676" fromTop="22" />
+ <File id="2B4B589C-B795-40BC-A105-585790922C31/f:MainWindow.xaml.cs" caret="549" fromTop="9" />
+ <File id="2B4B589C-B795-40BC-A105-585790922C31/f:MainWindow.xaml.cs" caret="906" fromTop="14" />
+ <File id="1D5A2399-EBFF-41AD-B1A1-38B60FFCD767/f:CodeExecuter.cs" caret="1323" fromTop="14" />
+ </RecentEdits>
+ </RecentFiles>
+ <CompletionStatisticsManager>
+ <ItemStatistics item="CSharpCodeCompletionContext">
+ <Item value="Validator`0" priority="31" />
+ <Item value="public" priority="30" />
+ <Item value="ResultView" priority="31" />
+ <Item value="serviceResult" priority="31" />
+ <Item value="InputCode" priority="31" />
+ </ItemStatistics>
+ <ItemStatistics item="Qualified:@Rossie.Engine.Validator">
+ <Item value="Validate`0" priority="26" />
+ </ItemStatistics>
+ <ItemStatistics item="XamlCodeCompletionContext">
+ <Item value="SetColumn`0" priority="5" />
+ </ItemStatistics>
+ <ItemStatistics item="Qualified:System.Windows.Controls.TextBox">
+ <Item value="Text" priority="26" />
+ </ItemStatistics>
+ <ItemStatistics item="Qualified:System.Windows.Controls.Label">
+ <Item value="Content" priority="22" />
+ </ItemStatistics>
+ <ItemStatistics item="Qualified:System.Object">
+ <Item value="ToString`0" priority="26" />
+ </ItemStatistics>
+ </CompletionStatisticsManager>
+ <NAntValidationSettings>
+ <NAntPath value="" />
+ </NAntValidationSettings>
+ <UnitTestRunner>
+ <Providers />
+ </UnitTestRunner>
+ <UnitTestRunnerNUnit>
+ <NUnitInstallDir IsNull="False">
+ </NUnitInstallDir>
+ <UseAddins>Never</UseAddins>
+ </UnitTestRunnerNUnit>
+ <SettingsComponent>
+ <string />
+ <integer />
+ <boolean>
+ <setting name="SolutionAnalysisEnabled">False</setting>
+ </boolean>
+ </SettingsComponent>
+</Configuration>
View
157 Rossie/Rossie.Engine/CodeExecuter.cs
@@ -12,75 +12,85 @@
namespace Rossie.Engine
{
- public sealed class ByteCodeLoader : MarshalByRefObject
+public sealed class ByteCodeLoader : MarshalByRefObject
+{
+ public ByteCodeLoader()
{
- public ByteCodeLoader()
- {
- }
+ }
- public object Run(byte[] compiledAssembly)
- {
- var assembly = Assembly.Load(compiledAssembly);
- assembly.EntryPoint.Invoke(null, new object[] { });
- var result = assembly.GetType("EntryPoint").GetProperty("Result").GetValue(null, null);
+ public object Run(byte[] compiledAssembly)
+ {
+ var assembly = Assembly.Load(compiledAssembly);
+ assembly.EntryPoint.Invoke(null, new object[] { });
+ var result = assembly.GetType("EntryPoint").GetProperty("Result").GetValue(null, null);
- return result;
- }
+ return result;
}
+}
public class CodeExecuter
{
- private static AppDomain CreateSandbox()
- {
- var e = new Evidence();
- e.AddHostEvidence(new Zone(SecurityZone.Internet));
+private static AppDomain CreateSandbox()
+{
+ var e = new Evidence();
+ e.AddHostEvidence(new Zone(SecurityZone.Internet));
- var ps = SecurityManager.GetStandardSandbox(e);
- var security = new SecurityPermission(SecurityPermissionFlag.Execution);
+ var ps = SecurityManager.GetStandardSandbox(e);
+ var security = new SecurityPermission(SecurityPermissionFlag.Execution);
- ps.AddPermission(security);
+ ps.AddPermission(security);
- var setup = new AppDomainSetup { ApplicationBase = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) };
- return AppDomain.CreateDomain("Sandbox", null, setup, ps);
- }
+ var setup = new AppDomainSetup { ApplicationBase = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) };
+ return AppDomain.CreateDomain("Sandbox", null, setup, ps);
+}
public object Execute(string code)
{
- if (!Validator.Validate(code)) return "Not implemented";
-
- var sandbox = CreateSandbox();
-
- const string entryPoint = "using System.Reflection; public class EntryPoint { public static object Result {get;set;} public static void Main() { Result = Script.Eval(); } }"; // new StreamReader("EntryPoint.txt").ReadToEnd();
- var script = "public static object Eval() {" + code + "}";
-
- var core = sandbox.Load("System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089");
- var system = sandbox.Load("System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089");
-
- var compilation = Compilation.Create("foo", new CompilationOptions(assemblyKind: AssemblyKind.ConsoleApplication, usings: ReadOnlyArray<string>.CreateFrom(new[] { "System", "System.IO", "System.Net", "System.Linq", "System.Threading", "System.Threading.Tasks", "System.Text", "System.Text.RegularExpressions", "System.Collections.Generic" })),
- new[]
- {
- SyntaxTree.ParseCompilationUnit(entryPoint),
- SyntaxTree.ParseCompilationUnit(script, options: new ParseOptions(kind: SourceCodeKind.Interactive))
- },
- new MetadataReference[] {
- new AssemblyFileReference(typeof(object).Assembly.Location),
- new AssemblyFileReference(core.Location),
- new AssemblyFileReference(system.Location)
- });
-
- byte[] compiledAssembly;
- using (var output = new MemoryStream())
- {
- var emitResult = compilation.Emit(output);
+ // if (!Validator.Validate(code)) return "Not implemented";
+
+var sandbox = CreateSandbox();
+
+ const string entryPoint =
+ "using System.Reflection; public class EntryPoint { public static object Result {get;set;} public static void Main() { Result = Script.Eval(); } }";
+ var script = "public static object Eval() {" + code + "}";
+
+var core = sandbox.Load("System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089");
+var system = sandbox.Load("System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089");
+
+var compilation = Compilation.Create("foo", new CompilationOptions( assemblyKind: AssemblyKind.ConsoleApplication,
+ usings: ReadOnlyArray<string>.CreateFrom(
+ new[] {
+ "System",
+ "System.IO",
+ "System.Net",
+ "System.Linq",
+ "System.Text",
+ "System.Text.RegularExpressions",
+ "System.Collections.Generic" })),
+ new[]
+ {
+ SyntaxTree.ParseCompilationUnit(entryPoint),
+ SyntaxTree.ParseCompilationUnit(script, options: new ParseOptions(kind: SourceCodeKind.Interactive))
+ },
+ new MetadataReference[] {
+ new AssemblyFileReference(typeof(object).Assembly.Location),
+ new AssemblyFileReference(core.Location),
+ new AssemblyFileReference(system.Location)
+ });
+
+byte[] compiledAssembly;
+using (var output = new MemoryStream())
+{
+ var emitResult = compilation.Emit(output);
- if (!emitResult.Success)
- {
- var errors = emitResult.Diagnostics.Select(x => x.Info.GetMessage().Replace("Eval()", "<Factory>()").ToString()).ToArray();
+ if (!emitResult.Success)
+ {
+ var errors = emitResult.Diagnostics.Select(x => x.Info.GetMessage().Replace("Eval()", "<Factory>()").ToString()).ToArray();
- return string.Join(", ", errors);
- }
+ return string.Join(", ", errors);
+ }
- compiledAssembly = output.ToArray();
- }
+ compiledAssembly = output.ToArray();
+}
if (compiledAssembly.Length == 0) return "Incorrect data";
@@ -89,26 +99,25 @@ public object Execute(string code)
object result = null;
try
{
- var scriptThread = new Thread(() =>
- {
- Thread.CurrentThread.CurrentCulture = new CultureInfo("en");
- Thread.CurrentThread.CurrentUICulture = new CultureInfo("en");
- try
- {
- result = loader.Run(compiledAssembly);
- }
- catch (Exception ex)
- {
- result = ex.Message;
- }
- });
- scriptThread.Start();
- if (!scriptThread.Join(6000))
- {
- scriptThread.Abort();
- AppDomain.Unload(sandbox);
- return "Timeout";
- }
+var scriptThread = new Thread(() =>
+{
+ try
+ {
+ result = loader.Run(compiledAssembly);
+ }
+ catch (Exception ex)
+ {
+ result = ex.Message;
+ }
+});
+
+scriptThread.Start();
+
+if (!scriptThread.Join(6000))
+{
+ scriptThread.Abort();
+ AppDomain.Unload(sandbox);
+}
}
catch (Exception ex)
{
View
28 Rossie/Rossie.Windows.Demo/MainWindow.xaml.cs
@@ -21,22 +21,22 @@ private void Execute_Click(object sender, RoutedEventArgs e)
{
RunInSandbox(InputCode.Text);
}
- private static readonly Uri ServiceUri = new Uri("net.pipe://localhost/Pipe");
- private const string PipeName = "RossieEngineService";
- private static readonly EndpointAddress ServiceAddress = new EndpointAddress(string.Format(CultureInfo.InvariantCulture, "{0}/{1}", ServiceUri.OriginalString, PipeName));
- private static ICommandService _serviceProxy;
+private static readonly Uri ServiceUri = new Uri("net.pipe://localhost/Pipe");
+private const string PipeName = "RossieEngineService";
+private static readonly EndpointAddress ServiceAddress = new EndpointAddress(string.Format(CultureInfo.InvariantCulture, "{0}/{1}", ServiceUri.OriginalString, PipeName));
+private static ICommandService _serviceProxy;
- private static void StartCodeService()
- {
- var service = new ServiceController("Rossie Engine Service");
- if (service.Status != ServiceControllerStatus.Running)
- {
- service.Start();
+private static void StartCodeService()
+{
+ var service = new ServiceController("Rossie Engine Service");
+ if (service.Status != ServiceControllerStatus.Running)
+ {
+ service.Start();
- service.WaitForStatus(ServiceControllerStatus.Running);
- }
- _serviceProxy = ChannelFactory<ICommandService>.CreateChannel(new NetNamedPipeBinding(), ServiceAddress);
- }
+ service.WaitForStatus(ServiceControllerStatus.Running);
+ }
+ _serviceProxy = ChannelFactory<ICommandService>.CreateChannel(new NetNamedPipeBinding(), ServiceAddress);
+}
private void RunInSandbox(string code)
{
View
39 Rossie/Rossie.Windows.Service/CommandServer.cs
@@ -4,30 +4,27 @@
namespace Rossie.Windows.Service
{
- static class CommandServer
- {
-
- private static readonly Uri ServiceUri = new Uri("net.pipe://localhost/Pipe");
- private const string PipeName = "RossieEngineService";
-
- private static readonly CommandService Service = new CommandService();
- private static ServiceHost _host;
+static class CommandServer
+{
+ private static readonly Uri ServiceUri = new Uri("net.pipe://localhost/Pipe");
+ private const string PipeName = "RossieEngineService";
- public static void Start()
- {
- _host = new ServiceHost(Service, ServiceUri);
- _host.AddServiceEndpoint(typeof(ICommandService), new NetNamedPipeBinding(), PipeName);
- _host.Open();
+ private static readonly CommandService Service = new CommandService();
+ private static ServiceHost _host;
- EventLog.WriteEntry("RossieEngineService", "Command Server Started", EventLogEntryType.Information);
- }
+ public static void Start()
+ {
+ _host = new ServiceHost(Service, ServiceUri);
+ _host.AddServiceEndpoint(typeof(ICommandService), new NetNamedPipeBinding(), PipeName);
+ _host.Open();
+ }
- public static void Stop()
- {
- if ((_host == null) || (_host.State == CommunicationState.Closed)) return;
+ public static void Stop()
+ {
+ if ((_host == null) || (_host.State == CommunicationState.Closed)) return;
- _host.Close();
- _host = null;
- }
+ _host.Close();
+ _host = null;
}
}
+}
View
62 Rossie/Rossie.Windows.Service/CommandService.cs
@@ -1,53 +1,51 @@
using System;
using System.Diagnostics;
-using System.Globalization;
using System.ServiceModel;
-using System.Threading;
using Roslyn.Scripting.CSharp;
using Rossie.Engine;
namespace Rossie.Windows.Service
{
- [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
- internal class CommandService : ICommandService
- {
+[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
+internal class CommandService : ICommandService
+{
- public string Execute(string code)
+ public string Execute(string code)
+ {
+ var engine = new CodeExecuter();
+ try
{
- var engine = new CodeExecuter();
- try
- {
- var unformatted = engine.Execute(code);
+ var unformatted = engine.Execute(code);
- return FormatResult(unformatted);
- }
- catch (Exception ex)
- {
- EventLog.WriteEntry("RossieEngineService", ex.ToString(), EventLogEntryType.Error);
- }
-
- return string.Empty;
+ return FormatResult(unformatted);
+ }
+ catch (Exception ex)
+ {
+ EventLog.WriteEntry("RossieEngineService", ex.ToString(), EventLogEntryType.Error);
}
- private static string FormatResult(object input)
+ return string.Empty;
+ }
+
+ private static string FormatResult(object input)
+ {
+ try
{
- try
- {
- var formatter = new ObjectFormatter(maxLineLength: 350);
- var result = formatter.FormatObject(input);
+ var formatter = new ObjectFormatter(maxLineLength: 350);
+ var result = formatter.FormatObject(input);
- if (string.IsNullOrEmpty(result)) return "null";
+ if (string.IsNullOrEmpty(result)) return "null";
- result = result.Replace(Environment.NewLine, " ").Replace("\n", " ").Replace("\r", " ");
+ result = result.Replace(Environment.NewLine, " ").Replace("\n", " ").Replace("\r", " ");
- if (result.Length > 350) result = result.Substring(0, 350);
+ if (result.Length > 350) result = result.Substring(0, 350);
- return result;
- }
- catch (Exception ex)
- {
- return ex.ToString();
- }
+ return result;
+ }
+ catch (Exception ex)
+ {
+ return ex.ToString();
}
}
}
+}
View
4 Rossie/Rossie.Windows.Service/RossieEngineService.cs
@@ -21,9 +21,9 @@ protected override void OnStart(string[] args)
EventLog.WriteEntry("RossieEngineService", "Starting Command Server", EventLogEntryType.Information);
- var thread = new Thread(CommandServer.Start);
+var thread = new Thread(CommandServer.Start);
- thread.Start();
+thread.Start();
}
catch (Exception ex)
{
View
BIN Rossie/Rossie.sln.docstates.suo
Binary file not shown.
View
BIN Rossie/Rossie.suo
Binary file not shown.

0 comments on commit f34d4a4

Please sign in to comment.