Browse Source

Merge pull request #445 from FungusGames/luautils-global-mode

Luautils global mode
master
Chris Gregan 9 years ago
parent
commit
9e7a62bbf2
  1. 34
      Assets/Fungus/Lua/Resources/Lua/fungus.txt
  2. 13
      Assets/Fungus/Lua/Scripts/LuaStore.cs
  3. 65
      Assets/Fungus/Lua/Scripts/LuaUtils.cs
  4. 21
      Assets/Tests/Lua/FungusTests.unity
  5. 18
      Assets/Tests/Lua/LuaEnvironmentTests.unity
  6. 6
      Assets/Tests/StringSubstitution/StringSubstitutionTests.unity

34
Assets/Fungus/Lua/Resources/Lua/fungus.txt

@ -18,17 +18,17 @@ end
-- Returns the absolute time
-- Use this timing function to work correctly with the Lua Environment's timeScale property
function M.gettime()
return fungus.luautils.getTime()
return M.luautils.getTime()
end
-- Returns the delta time this frame
-- Use this timing function to work correctly with the Lua Environment's timeScale property
function M.getdeltatime()
return fungus.luautils.getDeltaTime()
return M.luautils.getDeltaTime()
end
function M.settimescale(s)
fungus.luautils.timeScale = s
M.luautils.timeScale = s
end
-------------
@ -61,7 +61,7 @@ end
function M.run(enumerator)
-- If the parameter isn't an enumerator then CreateTask will fail
local status, err = pcall( function()
local task = fungus.luaenvironment.RunUnityCoroutine(enumerator)
local task = M.luaenvironment.RunUnityCoroutine(enumerator)
end)
if (not status) then
@ -74,7 +74,7 @@ end
function M.runwait(enumerator)
-- If the parameter isn't an enumerator then CreateTask will fail
local status, err = pcall( function()
local task = fungus.luaenvironment.RunUnityCoroutine(enumerator)
local task = M.luaenvironment.RunUnityCoroutine(enumerator)
while (task != nil and task.Running) do
coroutine.yield();
end
@ -92,18 +92,18 @@ end
-- Set active language for string table
function M.setlanguage(languagecode)
fungus.luautils.activeLanguage = languagecode
M.luautils.activeLanguage = languagecode
end
-- Get a named string from the string table
function M.getstring(key)
return fungus.luautils.GetString(key)
return M.luautils.GetString(key)
end
-- Substitutes variables and localisation strings into a piece of text
-- e.g. v = 10, "Subbed value is [$v]" => "Subbed value is 10"
function M.sub(text)
return fungus.luautils.substitute(text)
return M.luautils.substitute(text)
end
--------------------
@ -151,7 +151,7 @@ end
-- Gets the active say dialog, or creates one if none exists yet
function M.getsaydialog()
if (M.sayoptions.saydialog == nil) then
local sd = fungus.luautils.spawn("SayDialog").GetComponent("SayDialog")
local sd = M.luautils.spawn("SayDialog").GetComponent("SayDialog")
M.setsaydialog(sd)
end
return M.sayoptions.saydialog
@ -166,7 +166,7 @@ function M.setcharacter(character, portrait)
sd.SetCharacter(character)
-- Do substitution on character name
local subbed = fungus.sub(character.nameText)
local subbed = M.sub(character.nameText)
M.sayoptions.saydialog.SetCharacterName(subbed, character.nameColor)
-- Try to set the portrait sprite
@ -191,10 +191,10 @@ function M.say(text, voiceclip)
local sd = M.getsaydialog()
-- Do variable substitution before displaying text
local subbed = fungus.sub(text)
local subbed = M.sub(text)
local e = sd.SayInternal(subbed, M.sayoptions.clearprevious, M.sayoptions.waitforinput, M.sayoptions.fadewhendone, M.sayoptions.stopvoiceover, voiceclip)
fungus.runwait(e)
M.runwait(e)
end
--------------
@ -212,7 +212,7 @@ end
-- Gets the active menu dialog, or creates one if none exists yet
function M.getmenudialog()
if (M.menuoptions.menudialog == nil) then
local md = fungus.luautils.spawn("MenuDialog").GetComponent("MenuDialog")
local md = M.luautils.spawn("MenuDialog").GetComponent("MenuDialog")
M.setmenudialog(md)
end
return M.menuoptions.menudialog
@ -226,8 +226,8 @@ function M.menu(text, callback, interactive)
local md = M.getmenudialog()
-- Do variable substitution before displaying text
local subbed = fungus.sub(text)
md.AddOption(subbed, interactive or true, fungus.luaenvironment, callback)
local subbed = M.sub(text)
md.AddOption(subbed, interactive or true, M.luaenvironment, callback)
end
-- Display a timer during which the player has to choose an option.
@ -236,8 +236,8 @@ end
function M.menutimer(duration, callback)
local md = M.getmenudialog()
local e = md.ShowTimer(duration, fungus.luaenvironment, callback)
fungus.runwait(e)
local e = md.ShowTimer(duration, M.luaenvironment, callback)
M.runwait(e)
end
-- Clear all currently displayed menu options

13
Assets/Fungus/Lua/Scripts/LuaStore.cs

@ -82,18 +82,15 @@ namespace Fungus
return;
}
// If the fungus global table is defined then add the store to it
Table fungusTable = globals.Get("fungus").Table;
if (fungusTable == null)
if (fungusTable != null)
{
Debug.LogError("fungus table not found");
return;
fungusTable["store"] = primeTable;
}
fungusTable["store"] = primeTable;
// If we're using the fungus module in globals mode then add the store to globals as well.
if (globals.Get("luaenvironment") != DynValue.Nil)
else
{
// Add the store as a global
globals["store"] = primeTable;
}
}

65
Assets/Fungus/Lua/Scripts/LuaUtils.cs

@ -94,7 +94,6 @@ namespace Fungus
InitTypes();
InitFungusModule();
InitBindings();
InitStringTable();
}
/// <summary>
@ -185,6 +184,33 @@ namespace Fungus
fungusTable["test"] = UserData.CreateStatic(testType);
}
// Register the stringtable (if one is set)
if (stringTable != null)
{
try
{
DynValue stringTableRes = interpreter.DoString(stringTable.text);
if (stringTableRes.Type == DataType.Table)
{
stringTableCached = stringTableRes.Table;
if (fungusTable != null)
{
fungusTable["stringtable"] = stringTableCached;
}
}
}
catch (ScriptRuntimeException ex)
{
LuaEnvironment.LogException(ex.DecoratedMessage, stringTable.text);
}
catch (InterpreterException ex)
{
LuaEnvironment.LogException(ex.DecoratedMessage, stringTable.text);
}
}
stringSubstituter = new StringSubstituter();
if (fungusModule == FungusModuleOptions.UseGlobalVariables)
{
// Copy all items from the Fungus table to global variables
@ -200,45 +226,12 @@ namespace Fungus
}
}
interpreter.Globals["fungus"] = DynValue.Nil;
// Note: We can't remove the fungus table itself because of dependencies between functions
}
}
/// <summary>
/// Loads the global string table used for localisation.
/// </summary>
protected virtual void InitStringTable()
{
MoonSharp.Interpreter.Script interpreter = luaEnvironment.Interpreter;
if (stringTable != null)
{
try
{
DynValue stringTableRes = interpreter.DoString(stringTable.text);
if (stringTableRes.Type == DataType.Table)
{
stringTableCached = stringTableRes.Table;
Table fungusTable = interpreter.Globals.Get("fungus").Table;
if (fungusTable != null)
{
fungusTable["stringtable"] = stringTableCached;
}
}
}
catch (ScriptRuntimeException ex)
{
LuaEnvironment.LogException(ex.DecoratedMessage, stringTable.text);
}
catch (InterpreterException ex)
{
LuaEnvironment.LogException(ex.DecoratedMessage, stringTable.text);
}
}
stringSubstituter = new StringSubstituter();
}
/// <summary>
/// Returns a string from the string table for this key.
/// The string returned depends on the active language.

21
Assets/Tests/Lua/FungusTests.unity

@ -1218,7 +1218,7 @@ MonoBehaviour:
runwait( b.Execute() )
check(fungus.gettime() - t >= 1)
check(gettime() - t >= 1)
-- Check the new value of StringVar
@ -2748,7 +2748,7 @@ MonoBehaviour:
check(v3.value.z == 1)
fungus.pass()'
pass()'
runAsCoroutine: 1
--- !u!1 &465829169
GameObject:
@ -5397,11 +5397,11 @@ MonoBehaviour:
m_Name:
m_EditorClassIdentifier:
version: 1
scrollPos: {x: 32, y: -30}
scrollPos: {x: 163, y: 77}
variablesScrollPos: {x: 0, y: 0}
variablesExpanded: 0
blockViewHeight: 251
zoom: 0.9129947
zoom: 1
scrollViewRect:
serializedVersion: 2
x: -343
@ -5409,8 +5409,7 @@ MonoBehaviour:
width: 1114
height: 859
selectedBlock: {fileID: 942227027}
selectedCommands:
- {fileID: 942227053}
selectedCommands: []
variables:
- {fileID: 942227029}
- {fileID: 942227035}
@ -5574,7 +5573,7 @@ MonoBehaviour:
errorMessage:
indentLevel: 0
luaEnvironment: {fileID: 0}
luaScript: 'local c = fungus.factory.color(1, 0.5, 1, 1)
luaScript: 'local c = factory.color(1, 0.5, 1, 1)
return c'
@ -5742,7 +5741,7 @@ MonoBehaviour:
errorMessage:
indentLevel: 0
luaEnvironment: {fileID: 0}
luaScript: 'local v = fungus.factory.vector2(1,1)
luaScript: 'local v = factory.vector2(1,1)
return v'
@ -5811,7 +5810,7 @@ MonoBehaviour:
errorMessage:
indentLevel: 0
luaEnvironment: {fileID: 0}
luaScript: 'local v = fungus.factory.vector3(1,1,1)
luaScript: 'local v = factory.vector3(1,1,1)
return v'
runAsCoroutine: 0
@ -7621,7 +7620,7 @@ GameObject:
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 0
m_IsActive: 1
--- !u!114 &1641285457
MonoBehaviour:
m_ObjectHideFlags: 0
@ -8816,7 +8815,7 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 61dddfdc5e0e44ca298d8f46f7f5a915, type: 3}
m_Name:
m_EditorClassIdentifier:
selectedFlowchart: {fileID: 56584893}
selectedFlowchart: {fileID: 942227028}
--- !u!4 &1791050794
Transform:
m_ObjectHideFlags: 1

18
Assets/Tests/Lua/LuaEnvironmentTests.unity

@ -142,7 +142,7 @@ GameObject:
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
m_IsActive: 0
--- !u!114 &47556548
MonoBehaviour:
m_ObjectHideFlags: 0
@ -958,7 +958,7 @@ MonoBehaviour:
local t = 0
local timer = fungus.time.time
local timer = time.time
-- Wait with timeScale = -1
@ -1620,19 +1620,19 @@ MonoBehaviour:
luaFile: {fileID: 0}
luaScript: '-- Test string table localisation system
check(fungus.stringtable != nil)
check(stringtable != nil)
-- Check string table loaded ok
check(fungus.stringtable.hello.en == "Hi there")
check(stringtable.hello.en == "Hi there")
check(fungus.stringtable.hello.fr == "Bonjour")
check(stringtable.hello.fr == "Bonjour")
-- Check getstring()
local g = fungus.getstring("hello")
local g = getstring("hello")
check(g == "Hi there")
@ -1641,14 +1641,14 @@ MonoBehaviour:
t = 3
local s = fungus.sub("should be {$t}")
local s = sub("should be {$t}")
check(s == "should be 3")
-- Test substituting a string table value (en)
s = fungus.sub("say {$hello}")
s = sub("say {$hello}")
check(s == "say Hi there")
@ -1921,7 +1921,7 @@ GameObject:
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 0
m_IsActive: 1
--- !u!114 &1900871525
MonoBehaviour:
m_ObjectHideFlags: 0

6
Assets/Tests/StringSubstitution/StringSubstitutionTests.unity

@ -345,7 +345,7 @@ GameObject:
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
m_IsActive: 0
--- !u!114 &545923888
MonoBehaviour:
m_ObjectHideFlags: 0
@ -474,7 +474,7 @@ GameObject:
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 0
m_IsActive: 1
--- !u!114 &684307463
MonoBehaviour:
m_ObjectHideFlags: 0
@ -1185,7 +1185,7 @@ MonoBehaviour:
check (subbed == "Ok")
fungus.pass()'
pass()'
runAsCoroutine: 1
--- !u!1 &1532141878
GameObject:

Loading…
Cancel
Save