Adds more script globals.

Also makes them all abstract for code reuse working around a Roslyn bug.
This commit is contained in:
Pieter-Jan Briers
2020-05-22 22:13:11 +02:00
parent 564c39b85a
commit 2c5536e982
3 changed files with 69 additions and 45 deletions

View File

@@ -10,7 +10,6 @@ using Microsoft.CodeAnalysis.Scripting;
using Microsoft.CodeAnalysis.Text;
using Robust.Client.UserInterface.CustomControls;
using Robust.Client.ViewVariables;
using Robust.Shared.Interfaces.GameObjects;
using Robust.Shared.Interfaces.Reflection;
using Robust.Shared.IoC;
using Robust.Shared.Localization;
@@ -42,7 +41,7 @@ namespace Robust.Client.Console
Title = Loc.GetString("Robust C# Interactive (CLIENT)");
ScriptInstanceShared.InitDummy();
_globals = new ScriptGlobals(this);
_globals = new ScriptGlobalsImpl(this);
IoCManager.InjectDependencies(this);
@@ -97,7 +96,7 @@ namespace Robust.Client.Console
else
{
var options = ScriptInstanceShared.GetScriptOptions(_reflectionManager);
newScript = CSharpScript.Create(code, options, typeof(IScriptGlobals));
newScript = CSharpScript.Create(code, options, typeof(ScriptGlobals));
}
// Compile ahead of time so that we can do syntax highlighting correctly for the echo.
@@ -156,37 +155,30 @@ namespace Robust.Client.Console
OutputPanel.AddText(">");
}
private sealed class ScriptGlobals : IScriptGlobals
private sealed class ScriptGlobalsImpl : ScriptGlobals
{
private readonly ScriptConsoleClient _owner;
[field: Dependency] public IEntityManager ent { get; } = default!;
[field: Dependency] public IComponentManager comp { get; } = default!;
[field: Dependency] public IViewVariablesManager vvm { get; } = default!;
[field: Dependency] public override IViewVariablesManager vvm { get; } = default!;
public ScriptGlobals(ScriptConsoleClient owner)
public ScriptGlobalsImpl(ScriptConsoleClient owner)
{
IoCManager.InjectDependencies(this);
_owner = owner;
}
public void vv(object a)
public override void vv(object a)
{
vvm.OpenVV(a);
}
public T res<T>()
{
return IoCManager.Resolve<T>();
}
public void write(object toString)
public override void write(object toString)
{
_owner.OutputPanel.AddText(toString?.ToString() ?? "");
}
public void show(object obj)
public override void show(object obj)
{
write(CSharpObjectFormatter.Instance.FormatObject(obj));
}
@@ -195,16 +187,11 @@ namespace Robust.Client.Console
[SuppressMessage("ReSharper", "InconsistentNaming")]
[PublicAPI]
public interface IScriptGlobals
public abstract class ScriptGlobals : ScriptGlobalsShared
{
public IEntityManager ent { get; }
public IComponentManager comp { get; }
public IViewVariablesManager vvm { get; }
public abstract IViewVariablesManager vvm { get; }
public void vv(object a);
public T res<T>();
public void write(object toString);
public void show(object obj);
public abstract void vv(object a);
}
}
#endif

View File

@@ -154,7 +154,7 @@ namespace Robust.Server.Scripting
else
{
var options = ScriptInstanceShared.GetScriptOptions(_reflectionManager);
newScript = CSharpScript.Create(code, options, typeof(IScriptGlobals));
newScript = CSharpScript.Create(code, options, typeof(ScriptGlobals));
}
// Compile ahead of time so that we can do syntax highlighting correctly for the echo.
@@ -231,29 +231,21 @@ namespace Robust.Server.Scripting
public ScriptInstance()
{
Globals = new ScriptGlobals(this);
Globals = new ScriptGlobalsImpl(this);
}
}
private sealed class ScriptGlobals : IScriptGlobals
private sealed class ScriptGlobalsImpl : ScriptGlobals
{
private readonly ScriptInstance _scriptInstance;
public ScriptGlobals(ScriptInstance scriptInstance)
public ScriptGlobalsImpl(ScriptInstance scriptInstance)
{
_scriptInstance = scriptInstance;
IoCManager.InjectDependencies(this);
}
[field: Dependency] public IEntityManager ent { get; } = default!;
[field: Dependency] public IComponentManager comp { get; } = default!;
public T res<T>()
{
return IoCManager.Resolve<T>();
}
public void write(object toString)
public override void write(object toString)
{
if (_scriptInstance.RunningScript)
{
@@ -261,7 +253,7 @@ namespace Robust.Server.Scripting
}
}
public void show(object obj)
public override void show(object obj)
{
write(CSharpObjectFormatter.Instance.FormatObject(obj));
}
@@ -270,13 +262,7 @@ namespace Robust.Server.Scripting
[SuppressMessage("ReSharper", "InconsistentNaming")]
[PublicAPI]
public interface IScriptGlobals
public abstract class ScriptGlobals : ScriptGlobalsShared
{
public IEntityManager ent { get; }
public IComponentManager comp { get; }
public T res<T>();
public void write(object toString);
public void show(object obj);
}
}

View File

@@ -0,0 +1,51 @@
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using JetBrains.Annotations;
using Robust.Shared.GameObjects;
using Robust.Shared.Interfaces.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Map;
using Robust.Shared.Prototypes;
namespace Robust.Shared.Scripting
{
[PublicAPI]
[SuppressMessage("ReSharper", "IdentifierTypo")]
[SuppressMessage("ReSharper", "InconsistentNaming")]
public abstract class ScriptGlobalsShared
{
[field: Dependency] public IEntityManager ent { get; } = default!;
[field: Dependency] public IComponentManager comp { get; } = default!;
[field: Dependency] public IPrototypeManager prot { get; } = default!;
public IEnumerable<T> protos<T>() where T : class, IPrototype
{
return prot.EnumeratePrototypes<T>();
}
public IEnumerable<EntityPrototype> eprotos => prot.EnumeratePrototypes<EntityPrototype>();
public GridCoordinates gpos(double x, double y, int gridId)
{
return new GridCoordinates((float)x, (float)y, new GridId(gridId));
}
public GridCoordinates gpos(double x, double y, GridId gridId)
{
return new GridCoordinates((float)x, (float)y, gridId);
}
public IEntity spawn(string prototype, GridCoordinates position)
{
return ent.SpawnEntity(prototype, position);
}
public T res<T>()
{
return IoCManager.Resolve<T>();
}
public abstract void write(object toString);
public abstract void show(object obj);
}
}