Merge remote-tracking branch 'remotes/origin/lua' into general-devel

Conflicts:
	TShockAPI/Properties/AssemblyInfo.cs
This commit is contained in:
Lucas Nicodemus 2012-01-06 17:26:58 -07:00
commit 04b68eeb94
11 changed files with 267 additions and 16 deletions

BIN
LuaBins/LuaInterface.dll Normal file

Binary file not shown.

BIN
LuaBins/lua51.dll Normal file

Binary file not shown.

BIN
LuaBins/luanet.dll Normal file

Binary file not shown.

View file

@ -208,6 +208,10 @@ namespace TShockAPI
add(Permissions.cfg, ServerInfo, "stats"); add(Permissions.cfg, ServerInfo, "stats");
add(Permissions.converthardmode, ConvertCorruption, "convertcorruption"); add(Permissions.converthardmode, ConvertCorruption, "convertcorruption");
add(Permissions.converthardmode, ConvertHallow, "converthallow"); add(Permissions.converthardmode, ConvertHallow, "converthallow");
add(Permissions.runlua, RunLuaFile, "luarun");
add(Permissions.runlua, RunLuaString, "lua");
add(Permissions.runlua, ReloadLua, "luareload");
add(Permissions.runlua, TestLuaHook, "testhook");
} }
public static bool HandleCommand(TSPlayer player, string text) public static bool HandleCommand(TSPlayer player, string text)
@ -325,6 +329,43 @@ namespace TShockAPI
return c == ' ' || c == '\t' || c == '\n'; return c == ' ' || c == '\t' || c == '\n';
} }
#region Lua Commands
public static void TestLuaHook(CommandArgs args)
{
TShock.LuaLoader.HookCalls.OnHookTest();
}
public static void ReloadLua(CommandArgs args)
{
TShock.LuaLoader.LoadServerAutoruns();
args.Player.SendMessage("Lua reloaded.");
}
public static void RunLuaString(CommandArgs args)
{
if (args.Parameters.Count < 1)
{
args.Player.SendMessage("Syntax: /lua <lua>");
return;
}
TShock.LuaLoader.RunLuaString(args.Parameters[0]);
args.Player.SendMessage("Lua run: " + args.Parameters[0]);
}
public static void RunLuaFile(CommandArgs args)
{
if (args.Parameters.Count != 1)
{
args.Player.SendMessage("Syntax: /luarun <file>");
return;
}
TShock.LuaLoader.RunLuaFile(args.Parameters[0]);
}
#endregion
#region Account commands #region Account commands
public static void AttemptLogin(CommandArgs args) public static void AttemptLogin(CommandArgs args)
@ -1246,19 +1287,19 @@ namespace TShockAPI
} }
} }
private static void StartHardMode(CommandArgs args) private static void StartHardMode(CommandArgs args)
{ {
if (!TShock.Config.DisableHardmode) if (!TShock.Config.DisableHardmode)
WorldGen.StartHardmode(); WorldGen.StartHardmode();
else else
args.Player.SendMessage("Hardmode is disabled via config", Color.Red); args.Player.SendMessage("Hardmode is disabled via config", Color.Red);
} }
private static void DisableHardMode(CommandArgs args) private static void DisableHardMode(CommandArgs args)
{ {
Main.hardMode = false; Main.hardMode = false;
args.Player.SendMessage("Hardmode is now disabled", Color.Green); args.Player.SendMessage("Hardmode is now disabled", Color.Green);
} }
private static void ConvertCorruption(CommandArgs args) private static void ConvertCorruption(CommandArgs args)
{ {

View file

@ -0,0 +1,191 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.ComponentModel;
using LuaInterface;
namespace TShockAPI.LuaSystem
{
public class LuaLoader
{
private readonly Lua _lua = null;
public string LuaPath = "";
public string LuaAutorunPath = "";
public LuaHookBackend HookBackend = new LuaHookBackend();
public LuaHooks HookCalls = new LuaHooks();
public Dictionary<string, KeyValuePair<string, LuaFunction>> Hooks = new Dictionary
<string, KeyValuePair<string, LuaFunction>>();
public LuaLoader(string path)
{
_lua = new Lua();
LuaPath = path;
LuaAutorunPath = Path.Combine(LuaPath, "autorun");
SendLuaDebugMsg("Lua 5.1 (serverside) initialized.");
if (!string.IsNullOrEmpty(LuaPath) && !Directory.Exists(LuaPath))
{
Directory.CreateDirectory(LuaPath);
}
if (!string.IsNullOrEmpty(LuaAutorunPath) && !Directory.Exists(LuaAutorunPath))
{
Directory.CreateDirectory(LuaAutorunPath);
}
RegisterLuaFunctions();
LoadServerAutoruns();
}
public void LoadServerAutoruns()
{
try
{
foreach (string s in Directory.GetFiles(LuaAutorunPath))
{
SendLuaDebugMsg("Loading: " + s);
RunLuaFile(s);
}
}
catch (Exception e)
{
SendLuaDebugMsg(e.Message);
SendLuaDebugMsg(e.StackTrace);
}
}
public void RunLuaString(string s)
{
try
{
_lua.DoString(s);
}
catch (LuaException e)
{
SendLuaDebugMsg(e.Message);
}
}
public void RunLuaFile(string s)
{
try
{
_lua.DoFile(s);
}
catch (LuaException e)
{
SendLuaDebugMsg(e.Message);
}
}
public void SendLuaDebugMsg(string s)
{
ConsoleColor previousColor = Console.ForegroundColor;
Console.ForegroundColor = ConsoleColor.Cyan;
Console.WriteLine("Lua: " + s);
Console.ForegroundColor = previousColor;
}
public void RegisterLuaFunctions()
{
//I just added all the managers for Plugin Development. :)
//Feel free to remove any if you dont feel they are necessay
_lua["Players"] = TShock.Players;
_lua["Bans"] = TShock.Bans;
_lua["Warps"] = TShock.Warps;
_lua["Regions"] = TShock.Regions;
_lua["Backups"] = TShock.Backups;
_lua["Groups"] = TShock.Groups;
_lua["Users"] = TShock.Users;
_lua["Itembans"] = TShock.Users;
LuaFunctions LuaFuncs = new LuaFunctions(this);
var LuaFuncMethods = LuaFuncs.GetType().GetMethods();
foreach (System.Reflection.MethodInfo method in LuaFuncMethods)
{
_lua.RegisterFunction(method.Name, LuaFuncs, method);
}
//Utils
Utils LuaUtils = new Utils(this);
var LuaUtilMethods = LuaUtils.GetType().GetMethods();
foreach (System.Reflection.MethodInfo method in LuaUtilMethods)
{
_lua.RegisterFunction(method.Name, LuaUtils, method);
}
}
}
public class LuaFunctions
{
LuaLoader Parent;
public LuaFunctions(LuaLoader parent)
{
Parent = parent;
}
[Description("Prints a message to the console from the Lua debugger.")]
public void Print(string s)
{
ConsoleColor previousColor = Console.ForegroundColor;
Console.ForegroundColor = ConsoleColor.Cyan;
Console.WriteLine(s);
Console.ForegroundColor = previousColor;
}
[Description("Adds a hook that will trigger a function callback when activated.")]
public void HookAdd(string hook, string key, LuaFunction callback)
{
KeyValuePair<string, LuaFunction> internalhook = new KeyValuePair<string, LuaFunction>(hook, callback);
Parent.Hooks.Add(key, internalhook);
}
[Description("Removes a hook from the hook table. Good for reloading stuff.")]
public void HookRemove(string key)
{
Parent.Hooks.Remove(key);
}
}
public class LuaHookBackend
{
public void HookRun(string call, object parameters)
{
foreach (KeyValuePair<string, KeyValuePair<string, LuaFunction>> kv in TShock.LuaLoader.Hooks)
{
KeyValuePair<string, LuaFunction> hook = kv.Value;
if (call == hook.Key)
{
LuaFunction lf = hook.Value;
if (lf != null)
{
try
{
lf.Call(parameters);
}
catch (LuaException e)
{
TShock.LuaLoader.SendLuaDebugMsg(e.Message);
}
}
}
}
}
}
public class LuaHooks
{
[Description("Called on debug hook test.")]
public void OnHookTest()
{
object[] response = new object[2];
response[0] = true;
response[1] = "Hook win!";
TShock.LuaLoader.HookBackend.HookRun("HookTest", response);
}
}
}

View file

@ -149,6 +149,8 @@ namespace TShockAPI
[Description("Allow unrestricted Send Tile Square usage, for client side world editing")] public static readonly [Description("Allow unrestricted Send Tile Square usage, for client side world editing")] public static readonly
string allowclientsideworldedit; string allowclientsideworldedit;
[Description("User can execute Lua files from the hard disk and run Lua strings.")] public static readonly string
runlua;
static Permissions() static Permissions()
{ {

View file

@ -49,4 +49,4 @@ using System.Runtime.InteropServices;
// MMdd of the build // MMdd of the build
[assembly: AssemblyVersion("3.4.5.0106")] [assembly: AssemblyVersion("3.4.5.0106")]
[assembly: AssemblyFileVersion("3.4.5.0106")] [assembly: AssemblyFileVersion("3.4.5.0106")]

View file

@ -86,7 +86,7 @@ namespace TShockAPI
Environment.OSVersion + "&mono=" + Main.runningMono + "&port=" + Netplay.serverPort + Environment.OSVersion + "&mono=" + Main.runningMono + "&port=" + Netplay.serverPort +
"&plcount=" + TShock.Utils.ActivePlayers()); "&plcount=" + TShock.Utils.ActivePlayers());
} }
Log.ConsoleInfo("Stat Tracker: " + response + "\n"); Log.ConsoleInfo("Stat Tracker: " + response);
} }
catch (Exception e) catch (Exception e)
{ {

View file

@ -31,6 +31,7 @@ using MaxMind;
using Mono.Data.Sqlite; using Mono.Data.Sqlite;
using MySql.Data.MySqlClient; using MySql.Data.MySqlClient;
using Rests; using Rests;
using TShockAPI.LuaSystem;
using Terraria; using Terraria;
using TShockAPI.DB; using TShockAPI.DB;
using TShockAPI.Net; using TShockAPI.Net;
@ -64,6 +65,7 @@ namespace TShockAPI
public static RestManager RestManager; public static RestManager RestManager;
public static Utils Utils = new Utils(); public static Utils Utils = new Utils();
public static StatTracker StatTracker = new StatTracker(); public static StatTracker StatTracker = new StatTracker();
public static LuaLoader LuaLoader;
/// <summary> /// <summary>
/// Called after TShock is initialized. Useful for plugins that needs hooks before tshock but also depend on tshock being loaded. /// Called after TShock is initialized. Useful for plugins that needs hooks before tshock but also depend on tshock being loaded.
@ -418,8 +420,8 @@ namespace TShockAPI
RestApi.Start(); RestApi.Start();
StatTracker.CheckIn(); StatTracker.CheckIn();
FixChestStacks(); FixChestStacks();
LuaLoader = new LuaLoader(Path.Combine(".", "lua"));
} }
private void FixChestStacks() private void FixChestStacks()

View file

@ -53,6 +53,9 @@
<Reference Include="HttpServer"> <Reference Include="HttpServer">
<HintPath>..\HttpBins\HttpServer.dll</HintPath> <HintPath>..\HttpBins\HttpServer.dll</HintPath>
</Reference> </Reference>
<Reference Include="LuaInterface">
<HintPath>..\LuaBins\LuaInterface.dll</HintPath>
</Reference>
<Reference Include="Mono.Data.Sqlite"> <Reference Include="Mono.Data.Sqlite">
<HintPath>..\SqlBins\Mono.Data.Sqlite.dll</HintPath> <HintPath>..\SqlBins\Mono.Data.Sqlite.dll</HintPath>
</Reference> </Reference>
@ -107,6 +110,7 @@
<Compile Include="Group.cs" /> <Compile Include="Group.cs" />
<Compile Include="Extensions\LinqExt.cs" /> <Compile Include="Extensions\LinqExt.cs" />
<Compile Include="Log.cs" /> <Compile Include="Log.cs" />
<Compile Include="LuaSystem\LuaLoader.cs" />
<Compile Include="Net\BaseMsg.cs" /> <Compile Include="Net\BaseMsg.cs" />
<Compile Include="Net\DisconnectMsg.cs" /> <Compile Include="Net\DisconnectMsg.cs" />
<Compile Include="Net\NetTile.cs" /> <Compile Include="Net\NetTile.cs" />
@ -187,7 +191,7 @@
</PropertyGroup> </PropertyGroup>
<ProjectExtensions> <ProjectExtensions>
<VisualStudio> <VisualStudio>
<UserProperties BuildVersion_IncrementBeforeBuild="False" BuildVersion_StartDate="2011/6/17" BuildVersion_BuildVersioningStyle="None.None.None.MonthAndDayStamp" BuildVersion_BuildAction="Both" BuildVersion_UpdateFileVersion="True" BuildVersion_UpdateAssemblyVersion="True" /> <UserProperties BuildVersion_UpdateAssemblyVersion="True" BuildVersion_UpdateFileVersion="True" BuildVersion_BuildAction="Both" BuildVersion_BuildVersioningStyle="None.None.None.MonthAndDayStamp" BuildVersion_StartDate="2011/6/17" BuildVersion_IncrementBeforeBuild="False" />
</VisualStudio> </VisualStudio>
</ProjectExtensions> </ProjectExtensions>
<!-- To modify your build process, add your task inside one of the targets below and uncomment it. <!-- To modify your build process, add your task inside one of the targets below and uncomment it.

View file

@ -24,11 +24,22 @@ using System.Net.Sockets;
using System.Security.Cryptography; using System.Security.Cryptography;
using System.Text; using System.Text;
using Terraria; using Terraria;
using TShockAPI.LuaSystem;
namespace TShockAPI namespace TShockAPI
{ {
public class Utils public class Utils
{ {
LuaLoader Parent; //For Lua Functions that require the LuaLoader
public Utils()
{
}
public Utils(LuaLoader parent)
{
Parent = parent;
}
public Random Random = new Random(); public Random Random = new Random();
//private static List<Group> groups = new List<Group>(); //private static List<Group> groups = new List<Group>();