Skip to content

Commit d73af01

Browse files
committed
Add a new native-lua implementation.
1 parent f6efc9c commit d73af01

29 files changed

+1929
-21
lines changed

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
CSC = dmcs
3838
CSFLAGS = -nologo -warn:4 -debug:full -optimize- -codepage:utf8 -unsafe -warnaserror
3939
DEFINE = DEBUG;TRACE
40-
COMMON_LIBS = System.dll System.Core.dll System.Drawing.dll System.Xml.dll thirdparty/ICSharpCode.SharpZipLib.dll thirdparty/FuzzyLogicLibrary.dll thirdparty/Mono.Nat.dll thirdparty/MaxMind.Db.dll thirdparty/MaxMind.GeoIP2.dll
40+
COMMON_LIBS = System.dll System.Core.dll System.Drawing.dll System.Xml.dll thirdparty/ICSharpCode.SharpZipLib.dll thirdparty/FuzzyLogicLibrary.dll thirdparty/Mono.Nat.dll thirdparty/MaxMind.Db.dll thirdparty/MaxMind.GeoIP2.dll thirdparty/Eluant.dll
4141

4242

4343

@@ -175,7 +175,7 @@ editor_SRCS := $(shell find OpenRA.Editor/ -iname '*.cs')
175175
editor_TARGET = OpenRA.Editor.exe
176176
editor_KIND = winexe
177177
editor_DEPS = $(game_TARGET)
178-
editor_LIBS = System.Windows.Forms.dll System.Data.dll System.Drawing.dll $(editor_DEPS)
178+
editor_LIBS = System.Windows.Forms.dll System.Data.dll System.Drawing.dll $(editor_DEPS) thirdparty/Eluant.dll
179179
editor_EXTRA = -resource:OpenRA.Editor.Form1.resources -resource:OpenRA.Editor.MapSelect.resources
180180
editor_FLAGS = -win32icon:OpenRA.Editor/OpenRA.Editor.Icon.ico
181181

OpenRA.Editor/OpenRA.Editor.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@
7171
<Reference Include="System.Drawing" />
7272
<Reference Include="System.Windows.Forms" />
7373
<Reference Include="System.Xml" />
74+
<Reference Include="Eluant">
75+
<HintPath>..\thirdparty\Eluant.dll</HintPath>
76+
</Reference>
7477
</ItemGroup>
7578
<ItemGroup>
7679
<Compile Include="ActorPropertiesDialog.cs">

OpenRA.Game/Actor.cs

100755100644
Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,16 @@
1212
using System.Collections.Generic;
1313
using System.Drawing;
1414
using System.Linq;
15+
using Eluant;
16+
using Eluant.ObjectBinding;
1517
using OpenRA.Graphics;
1618
using OpenRA.Primitives;
19+
using OpenRA.Scripting;
1720
using OpenRA.Traits;
1821

1922
namespace OpenRA
2023
{
21-
public class Actor
24+
public class Actor : IScriptBindable, IScriptNotifyBind, ILuaTableBinding, ILuaEqualityBinding, ILuaToStringBinding
2225
{
2326
public readonly ActorInfo Info;
2427

@@ -240,5 +243,40 @@ public void Kill(Actor attacker)
240243

241244
health.Value.InflictDamage(this, attacker, health.Value.MaxHP, null, true);
242245
}
246+
247+
#region Scripting interface
248+
249+
Lazy<ScriptActorInterface> luaInterface;
250+
public void OnScriptBind(ScriptContext context)
251+
{
252+
luaInterface = Exts.Lazy(() => new ScriptActorInterface(context, this));
253+
}
254+
255+
public LuaValue this[LuaRuntime runtime, LuaValue keyValue]
256+
{
257+
get { return luaInterface.Value[runtime, keyValue]; }
258+
set { luaInterface.Value[runtime, keyValue] = value; }
259+
}
260+
261+
public LuaValue Equals(LuaRuntime runtime, LuaValue left, LuaValue right)
262+
{
263+
Actor a, b;
264+
if (!left.TryGetClrValue<Actor>(out a) || !right.TryGetClrValue<Actor>(out b))
265+
return false;
266+
267+
return a == b;
268+
}
269+
270+
public LuaValue ToString(LuaRuntime runtime)
271+
{
272+
return "Actor ({0})".F(this);
273+
}
274+
275+
public bool HasScriptProperty(string name)
276+
{
277+
return luaInterface.Value.ContainsKey(name);
278+
}
279+
280+
#endregion
243281
}
244282
}

OpenRA.Game/CPos.cs

Lines changed: 54 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@
1010

1111
using System;
1212
using System.Drawing;
13+
using Eluant;
14+
using Eluant.ObjectBinding;
15+
using OpenRA.Scripting;
1316

1417
namespace OpenRA
1518
{
16-
/// <summary>
17-
/// Cell coordinate position in the world (coarse).
18-
/// </summary>
19-
public struct CPos
19+
public struct CPos : IScriptBindable, ILuaAdditionBinding, ILuaSubtractionBinding, ILuaEqualityBinding, ILuaTableBinding
2020
{
2121
public readonly int X, Y;
2222

@@ -60,6 +60,56 @@ public override bool Equals(object obj)
6060

6161
public override string ToString() { return "{0},{1}".F(X, Y); }
6262

63+
#region Scripting interface
64+
65+
public LuaValue Add(LuaRuntime runtime, LuaValue left, LuaValue right)
66+
{
67+
CPos a;
68+
CVec b;
69+
if (!left.TryGetClrValue<CPos>(out a) || !right.TryGetClrValue<CVec>(out b))
70+
throw new LuaException("Attempted to call CPos.Add(CPos, CVec) with invalid arguments ({0}, {1})".F(left.WrappedClrType().Name, right.WrappedClrType().Name));
71+
72+
return new LuaCustomClrObject(a + b);
73+
}
74+
75+
public LuaValue Subtract(LuaRuntime runtime, LuaValue left, LuaValue right)
76+
{
77+
CPos a;
78+
CVec b;
79+
if (!left.TryGetClrValue<CPos>(out a) || !right.TryGetClrValue<CVec>(out b))
80+
throw new LuaException("Attempted to call CPos.Subtract(CPos, CVec) with invalid arguments ({0}, {1})".F(left.WrappedClrType().Name, right.WrappedClrType().Name));
81+
82+
return new LuaCustomClrObject(a - b);
83+
}
84+
85+
public LuaValue Equals(LuaRuntime runtime, LuaValue left, LuaValue right)
86+
{
87+
CPos a, b;
88+
if (!left.TryGetClrValue<CPos>(out a) || !right.TryGetClrValue<CPos>(out b))
89+
return false;
90+
91+
return a == b;
92+
}
93+
94+
public LuaValue this[LuaRuntime runtime, LuaValue key]
95+
{
96+
get
97+
{
98+
switch (key.ToString())
99+
{
100+
case "X": return X;
101+
case "Y": return Y;
102+
default: throw new LuaException("CPos does not define a member '{0}'".F(key));
103+
}
104+
}
105+
106+
set
107+
{
108+
throw new LuaException("CPos is read-only. Use CPos.New to create a new value");
109+
}
110+
}
111+
112+
#endregion
63113
}
64114

65115
public static class RectangleExtensions

OpenRA.Game/CVec.cs

Lines changed: 59 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@
1010

1111
using System;
1212
using System.Drawing;
13+
using Eluant;
14+
using Eluant.ObjectBinding;
15+
using OpenRA.Scripting;
1316

1417
namespace OpenRA
1518
{
16-
/// <summary>
17-
/// Cell coordinate vector (coarse).
18-
/// </summary>
19-
public struct CVec
19+
public struct CVec : IScriptBindable, ILuaAdditionBinding, ILuaSubtractionBinding, ILuaUnaryMinusBinding, ILuaEqualityBinding, ILuaTableBinding
2020
{
2121
public readonly int X, Y;
2222

@@ -82,5 +82,60 @@ public override bool Equals(object obj)
8282
new CVec(1, 0),
8383
new CVec(1, 1),
8484
};
85+
86+
#region Scripting interface
87+
88+
public LuaValue Add(LuaRuntime runtime, LuaValue left, LuaValue right)
89+
{
90+
CVec a, b;
91+
if (!left.TryGetClrValue<CVec>(out a) || !right.TryGetClrValue<CVec>(out b))
92+
throw new LuaException("Attempted to call CVec.Add(CVec, CVec) with invalid arguments ({0}, {1})".F(left.WrappedClrType().Name, right.WrappedClrType().Name));
93+
94+
return new LuaCustomClrObject(a + b);
95+
}
96+
97+
public LuaValue Subtract(LuaRuntime runtime, LuaValue left, LuaValue right)
98+
{
99+
CVec a, b;
100+
if (!left.TryGetClrValue<CVec>(out a) || !right.TryGetClrValue<CVec>(out b))
101+
throw new LuaException("Attempted to call CVec.Subtract(CVec, CVec) with invalid arguments ({0}, {1})".F(left.WrappedClrType().Name, right.WrappedClrType().Name));
102+
103+
return new LuaCustomClrObject(a - b);
104+
}
105+
106+
public LuaValue Minus(LuaRuntime runtime)
107+
{
108+
return new LuaCustomClrObject(-this);
109+
}
110+
111+
public LuaValue Equals(LuaRuntime runtime, LuaValue left, LuaValue right)
112+
{
113+
CVec a, b;
114+
if (!left.TryGetClrValue<CVec>(out a) || !right.TryGetClrValue<CVec>(out b))
115+
return false;
116+
117+
return a == b;
118+
}
119+
120+
public LuaValue this[LuaRuntime runtime, LuaValue key]
121+
{
122+
get
123+
{
124+
switch (key.ToString())
125+
{
126+
case "X": return X;
127+
case "Y": return Y;
128+
case "Facing": return Traits.Util.GetFacing(this, 0);
129+
default: throw new LuaException("CVec does not define a member '{0}'".F(key));
130+
}
131+
}
132+
133+
set
134+
{
135+
throw new LuaException("WVec is read-only. Use CVec.New to create a new value");
136+
}
137+
}
138+
139+
#endregion
85140
}
86141
}

OpenRA.Game/OpenRA.Game.csproj

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@
7474
<Package>mono.nat</Package>
7575
<HintPath>..\thirdparty\Mono.Nat.dll</HintPath>
7676
</Reference>
77+
<Reference Include="Eluant">
78+
<HintPath>..\thirdparty\Eluant.dll</HintPath>
79+
</Reference>
7780
<Reference Include="Tao.OpenAl, Version=1.1.0.1, Culture=neutral, PublicKeyToken=a7579dda88828311">
7881
<SpecificVersion>False</SpecificVersion>
7982
<HintPath>..\thirdparty\Tao\Tao.OpenAl.dll</HintPath>
@@ -236,6 +239,13 @@
236239
<Compile Include="Widgets\SpriteWidget.cs" />
237240
<Compile Include="Widgets\SpriteSequenceWidget.cs" />
238241
<Compile Include="Widgets\RGBASpriteWidget.cs" />
242+
<Compile Include="Scripting\ScriptContext.cs" />
243+
<Compile Include="Scripting\ScriptActorInterface.cs" />
244+
<Compile Include="Scripting\ScriptObjectWrapper.cs" />
245+
<Compile Include="Scripting\ScriptTypes.cs" />
246+
<Compile Include="Scripting\ScriptMemberWrapper.cs" />
247+
<Compile Include="Scripting\ScriptMemberExts.cs" />
248+
<Compile Include="Scripting\ScriptPlayerInterface.cs" />
239249
</ItemGroup>
240250
<ItemGroup>
241251
<Compile Include="FileSystem\D2kSoundResources.cs" />
@@ -359,4 +369,7 @@
359369
<Target Name="AfterBuild">
360370
</Target>
361371
-->
372+
<ItemGroup>
373+
<Folder Include="Scripting\" />
374+
</ItemGroup>
362375
</Project>

OpenRA.Game/Player.cs

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,31 @@
11
#region Copyright & License Information
22
/*
3-
* Copyright 2007-2011 The OpenRA Developers (see AUTHORS)
3+
* Copyright 2007-2014 The OpenRA Developers (see AUTHORS)
44
* This file is part of OpenRA, which is free software. It is made
55
* available to you under the terms of the GNU General Public License
66
* as published by the Free Software Foundation. For more information,
77
* see COPYING.
88
*/
99
#endregion
1010

11+
using System;
1112
using System.Collections.Generic;
1213
using System.Linq;
14+
using Eluant;
15+
using Eluant.ObjectBinding;
1316
using OpenRA.FileFormats;
1417
using OpenRA.Network;
1518
using OpenRA.Graphics;
1619
using OpenRA.Primitives;
20+
using OpenRA.Scripting;
1721
using OpenRA.Traits;
1822

1923
namespace OpenRA
2024
{
2125
public enum PowerState { Normal, Low, Critical };
2226
public enum WinState { Won, Lost, Undefined };
2327

24-
public class Player
28+
public class Player : IScriptBindable, IScriptNotifyBind, ILuaTableBinding, ILuaEqualityBinding, ILuaToStringBinding
2529
{
2630
public Actor PlayerActor;
2731
public WinState WinState = WinState.Undefined;
@@ -106,5 +110,35 @@ public bool IsAlliedWith(Player p)
106110
// Observers are considered as allies
107111
return p == null || Stances[p] == Stance.Ally;
108112
}
113+
114+
#region Scripting interface
115+
116+
Lazy<ScriptPlayerInterface> luaInterface;
117+
public void OnScriptBind(ScriptContext context)
118+
{
119+
luaInterface = Exts.Lazy(() => new ScriptPlayerInterface(context, this));
120+
}
121+
122+
public LuaValue this[LuaRuntime runtime, LuaValue keyValue]
123+
{
124+
get { return luaInterface.Value[runtime, keyValue]; }
125+
set { luaInterface.Value[runtime, keyValue] = value; }
126+
}
127+
128+
public LuaValue Equals (LuaRuntime runtime, LuaValue left, LuaValue right)
129+
{
130+
Player a, b;
131+
if (!left.TryGetClrValue<Player>(out a) || !right.TryGetClrValue<Player>(out b))
132+
return false;
133+
134+
return a == b;
135+
}
136+
137+
public LuaValue ToString(LuaRuntime runtime)
138+
{
139+
return "Player ({0})".F(PlayerName);
140+
}
141+
142+
#endregion
109143
}
110144
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#region Copyright & License Information
2+
/*
3+
* Copyright 2007-2014 The OpenRA Developers (see AUTHORS)
4+
* This file is part of OpenRA, which is free software. It is made
5+
* available to you under the terms of the GNU General Public License
6+
* as published by the Free Software Foundation. For more information,
7+
* see COPYING.
8+
*/
9+
#endregion
10+
11+
using System;
12+
using System.Collections;
13+
using System.Collections.Generic;
14+
using System.Linq;
15+
using System.Reflection;
16+
using Eluant;
17+
using Eluant.ObjectBinding;
18+
using OpenRA.FileFormats;
19+
using OpenRA.Traits;
20+
21+
namespace OpenRA.Scripting
22+
{
23+
public class ScriptActorInterface : ScriptObjectWrapper
24+
{
25+
readonly Actor actor;
26+
27+
protected override string DuplicateKeyError(string memberName) { return "Actor '{0}' defines the command '{1}' on multiple traits".F(actor.Info.Name, memberName); }
28+
protected override string MemberNotFoundError(string memberName) { return "Actor '{0}' does not define a property '{1}'".F(actor.Info.Name, memberName); }
29+
30+
public ScriptActorInterface(ScriptContext context, Actor actor)
31+
: base(context)
32+
{
33+
this.actor = actor;
34+
35+
var args = new [] { actor };
36+
var objects = context.ActorCommands[actor.Info].Select(cg =>
37+
{
38+
var groupCtor = cg.GetConstructor(new Type[] { typeof(Actor) });
39+
return groupCtor.Invoke(args);
40+
});
41+
42+
Bind(objects);
43+
}
44+
}
45+
}

0 commit comments

Comments
 (0)