Add Lua reserved name checking and exception hooks

This commit is contained in:
ScottNZ
2013-12-06 03:23:54 +13:00
parent c39bd53e2a
commit 452a0c0e25
2 changed files with 29 additions and 7 deletions

View File

@@ -13,6 +13,7 @@ using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using NLua;
using NLua.Event;
using OpenRA.FileFormats;
namespace OpenRA.Mods.RA.Scripting
@@ -26,6 +27,7 @@ namespace OpenRA.Mods.RA.Scripting
{
Log.Write("debug", "Creating Lua script context");
Lua = new Lua();
Lua.HookException += OnLuaException;
functionCache = new Cache<string, LuaFunction>(Lua.GetFunction);
}
@@ -79,11 +81,21 @@ namespace OpenRA.Mods.RA.Scripting
}
}
void LogException(Exception e)
void OnLuaException(object sender, HookExceptionEventArgs e)
{
Game.Debug("{0}", e.Message);
ShowException(e.Exception);
}
void ShowException(Exception e)
{
ShowErrorMessage(e.Message, e.ToString());
}
public void ShowErrorMessage(string shortMessage, string longMessage)
{
Game.Debug("{0}", shortMessage);
Game.Debug("See debug.log for details");
Log.Write("debug", "{0}", e);
Log.Write("debug", "{0}", longMessage ?? shortMessage);
}
public void LoadLuaScripts(Func<string, string> getFileContents, params string[] files)
@@ -98,7 +110,7 @@ namespace OpenRA.Mods.RA.Scripting
}
catch (Exception e)
{
LogException(e);
ShowException(e);
}
}
}
@@ -114,7 +126,7 @@ namespace OpenRA.Mods.RA.Scripting
}
catch (Exception e)
{
LogException(e);
ShowException(e);
return null;
}
}