Browse Source

Combine the unity table with the fungus table

master
chrisgregan 9 years ago
parent
commit
a7064c455e
  1. 53
      Assets/Fungus/Lua/Resources/Lua/fungus.txt
  2. 8
      Assets/Fungus/Lua/Scripts/LuaStore.cs
  3. 26
      Assets/Fungus/Lua/Scripts/LuaUtils.cs
  4. 21
      Assets/Tests/Lua/FungusTests.unity
  5. 78
      Assets/Tests/Lua/LuaEnvironmentTests.unity

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

@ -3,6 +3,15 @@ local inspect = require('inspect')
-- Utility functions for working with Lua in Fungus -- Utility functions for working with Lua in Fungus
local M = {} local M = {}
-- Merge everything in the temp _fungus table into this module
local function mergefungustable()
for k,v in pairs(_fungus) do
M[k] = v
end
_fungus = nil
end
mergefungustable()
------------ ------------
-- Debugging -- Debugging
------------ ------------
@ -18,17 +27,17 @@ end
-- Returns the absolute time -- Returns the absolute time
-- Use this timing function to work correctly with the Lua Environment's timeScale property -- Use this timing function to work correctly with the Lua Environment's timeScale property
function M.gettime() function M.gettime()
return unity.luautils.getTime() return fungus.luautils.getTime()
end end
-- Returns the delta time this frame -- Returns the delta time this frame
-- Use this timing function to work correctly with the Lua Environment's timeScale property -- Use this timing function to work correctly with the Lua Environment's timeScale property
function M.getdeltatime() function M.getdeltatime()
return unity.luautils.getDeltaTime() return fungus.luautils.getDeltaTime()
end end
function M.settimescale(s) function M.settimescale(s)
unity.luautils.timeScale = s fungus.luautils.timeScale = s
end end
------------- -------------
@ -61,7 +70,7 @@ end
function M.run(enumerator) function M.run(enumerator)
-- If the parameter isn't an enumerator then CreateTask will fail -- If the parameter isn't an enumerator then CreateTask will fail
local status, err = pcall( function() local status, err = pcall( function()
local task = unity.luaenvironment.RunUnityCoroutine(enumerator) local task = fungus.luaenvironment.RunUnityCoroutine(enumerator)
end) end)
if (not status) then if (not status) then
@ -74,7 +83,7 @@ end
function M.runwait(enumerator) function M.runwait(enumerator)
-- If the parameter isn't an enumerator then CreateTask will fail -- If the parameter isn't an enumerator then CreateTask will fail
local status, err = pcall( function() local status, err = pcall( function()
local task = unity.luaenvironment.RunUnityCoroutine(enumerator) local task = fungus.luaenvironment.RunUnityCoroutine(enumerator)
while (task != nil and task.Running) do while (task != nil and task.Running) do
coroutine.yield(); coroutine.yield();
end end
@ -92,18 +101,18 @@ end
-- Set active language for string table -- Set active language for string table
function M.setlanguage(languagecode) function M.setlanguage(languagecode)
unity.luautils.activeLanguage = languagecode fungus.luautils.activeLanguage = languagecode
end end
-- Get a named string from the string table -- Get a named string from the string table
function M.getstring(key) function M.getstring(key)
return unity.luautils.GetString(key) return fungus.luautils.GetString(key)
end end
-- Substitutes variables and localisation strings into a piece of text -- Substitutes variables and localisation strings into a piece of text
-- e.g. v = 10, "Subbed value is [$v]" => "Subbed value is 10" -- e.g. v = 10, "Subbed value is [$v]" => "Subbed value is 10"
function M.sub(text) function M.sub(text)
return unity.luautils.substitute(text) return fungus.luautils.substitute(text)
end end
-------------------- --------------------
@ -116,34 +125,20 @@ function M.assert(c, reason)
-- Output a traceback to help track down source -- Output a traceback to help track down source
error( debug.traceback("Assert failed", 2) ) error( debug.traceback("Assert failed", 2) )
end end
unity.test.assert(c, reason) fungus.test.assert(c, reason)
end end
-- Pass an integration test (requires Unity Test Tools) -- Pass an integration test (requires Unity Test Tools)
function M.pass() function M.pass()
unity.test.pass() fungus.test.pass()
end end
-- Fail an integration test (requires Unity Test Tools) -- Fail an integration test (requires Unity Test Tools)
function M.fail(reason) function M.fail(reason)
error( debug.traceback("Test failed", 2) ) error( debug.traceback("Test failed", 2) )
unity.test.fail(reason) fungus.test.fail(reason)
end end
---------------
-- Fungus Prefs
---------------
-- Handy alias to the real prefs object
M.prefs = unity.prefs
---------------
-- Lua Store
---------------
-- Handy alias to the real store object
M.store = unity.store
------------- -------------
-- Say Dialog -- Say Dialog
------------- -------------
@ -164,7 +159,7 @@ end
-- Gets the active say dialog, or creates one if none exists yet -- Gets the active say dialog, or creates one if none exists yet
function M.getsaydialog() function M.getsaydialog()
if (M.sayoptions.saydialog == nil) then if (M.sayoptions.saydialog == nil) then
local sd = unity.luautils.spawn("SayDialog").GetComponent("SayDialog") local sd = fungus.luautils.spawn("SayDialog").GetComponent("SayDialog")
M.setsaydialog(sd) M.setsaydialog(sd)
end end
return M.sayoptions.saydialog return M.sayoptions.saydialog
@ -225,7 +220,7 @@ end
-- Gets the active menu dialog, or creates one if none exists yet -- Gets the active menu dialog, or creates one if none exists yet
function M.getmenudialog() function M.getmenudialog()
if (M.menuoptions.menudialog == nil) then if (M.menuoptions.menudialog == nil) then
local md = unity.luautils.spawn("MenuDialog").GetComponent("MenuDialog") local md = fungus.luautils.spawn("MenuDialog").GetComponent("MenuDialog")
M.setmenudialog(md) M.setmenudialog(md)
end end
return M.menuoptions.menudialog return M.menuoptions.menudialog
@ -240,7 +235,7 @@ function M.menu(text, callback, interactive)
-- Do variable substitution before displaying text -- Do variable substitution before displaying text
local subbed = fungus.sub(text) local subbed = fungus.sub(text)
md.AddOption(subbed, interactive or true, unity.luaenvironment, callback) md.AddOption(subbed, interactive or true, fungus.luaenvironment, callback)
end end
-- Display a timer during which the player has to choose an option. -- Display a timer during which the player has to choose an option.
@ -249,7 +244,7 @@ end
function M.menutimer(duration, callback) function M.menutimer(duration, callback)
local md = M.getmenudialog() local md = M.getmenudialog()
local e = md.ShowTimer(duration, unity.luaenvironment, callback) local e = md.ShowTimer(duration, fungus.luaenvironment, callback)
fungus.runwait(e) fungus.runwait(e)
end end

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

@ -82,14 +82,14 @@ namespace Fungus
return; return;
} }
Table unityTable = globals.Get("unity").Table; Table fungusTable = globals.Get("_fungus").Table;
if (unityTable == null) if (fungusTable == null)
{ {
Debug.LogError("Unity table not found"); Debug.LogError("_fungus table not found");
return; return;
} }
unityTable["store"] = primeTable; fungusTable["store"] = primeTable;
} }
} }

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

@ -132,27 +132,29 @@ namespace Fungus
{ {
MoonSharp.Interpreter.Script interpreter = luaEnvironment.Interpreter; MoonSharp.Interpreter.Script interpreter = luaEnvironment.Interpreter;
// Add the CLR class objects to a global unity table // Add the CLR class objects to a temp unity table called _fungus.
Table unityTable = new Table(interpreter); // When the fungus module is required, all the entries from _fungus are copied over.
interpreter.Globals["unity"] = unityTable;
Table fungusTable = new Table(interpreter);
interpreter.Globals["_fungus"] = fungusTable;
// Static classes // Static classes
unityTable["time"] = UserData.CreateStatic(typeof(Time)); fungusTable["time"] = UserData.CreateStatic(typeof(Time));
unityTable["prefs"] = UserData.CreateStatic(typeof(FungusPrefs)); fungusTable["prefs"] = UserData.CreateStatic(typeof(FungusPrefs));
UserData.RegisterType(typeof(PODTypeFactory)); UserData.RegisterType(typeof(PODTypeFactory));
unityTable["factory"] = UserData.CreateStatic(typeof(PODTypeFactory)); fungusTable["factory"] = UserData.CreateStatic(typeof(PODTypeFactory));
// Lua Environment and Lua Utils components // Lua Environment and Lua Utils components
unityTable["luaenvironment"] = luaEnvironment; fungusTable["luaenvironment"] = luaEnvironment;
unityTable["luautils"] = this; fungusTable["luautils"] = this;
// Provide access to the Unity Test Tools (if available). // Provide access to the Unity Test Tools (if available).
Type testType = Type.GetType("IntegrationTest"); Type testType = Type.GetType("IntegrationTest");
if (testType != null) if (testType != null)
{ {
UserData.RegisterType(testType); UserData.RegisterType(testType);
unityTable["test"] = UserData.CreateStatic(testType); fungusTable["test"] = UserData.CreateStatic(testType);
} }
// Example of how to register an enum // Example of how to register an enum
@ -175,10 +177,10 @@ namespace Fungus
if (stringTableRes.Type == DataType.Table) if (stringTableRes.Type == DataType.Table)
{ {
stringTableCached = stringTableRes.Table; stringTableCached = stringTableRes.Table;
Table unityTable = interpreter.Globals.Get("unity").Table; Table fungusTable = interpreter.Globals.Get("_fungus").Table;
if (unityTable != null) if (fungusTable != null)
{ {
unityTable["stringtable"] = stringTableCached; fungusTable["stringtable"] = stringTableCached;
} }
} }
} }

21
Assets/Tests/Lua/FungusTests.unity

@ -4403,10 +4403,10 @@ MonoBehaviour:
luaEnvironment: {fileID: 0} luaEnvironment: {fileID: 0}
luaFile: {fileID: 0} luaFile: {fileID: 0}
luaScript: "-- Test Say creating a SayDialog when none already exists\n\nfungus.sayoptions.waitforinput luaScript: "-- Test Say creating a SayDialog when none already exists\n\nfungus.sayoptions.waitforinput
= false\nfungus.say(\"test\")\n\ngo = unity.luautils.find(\"SayDialog\")\nfungus.assert(go = false\nfungus.say(\"test\")\n\ngo = fungus.luautils.find(\"SayDialog\")\nfungus.assert(go
!= nil)\n\n-- Test Menu creating a MenuDialog when none already exists\nfunction != nil)\n\n-- Test Menu creating a MenuDialog when none already exists\nfunction
choose_a()\n fungus.pass()\nend\n\nfungus.menu(\"Option A\", choose_a)\n\nlocal choose_a()\n fungus.pass()\nend\n\nfungus.menu(\"Option A\", choose_a)\n\nlocal
go = unity.luautils.find(\"OptionButton0\")\nfungus.assert(go != nil)\n\nlocal go = fungus.luautils.find(\"OptionButton0\")\nfungus.assert(go != nil)\n\nlocal
button = go.GetComponent(\"Button\")\nfungus.assert(button != nil)\n\nbutton.onClick.Invoke()" button = go.GetComponent(\"Button\")\nfungus.assert(button != nil)\n\nbutton.onClick.Invoke()"
runAsCoroutine: 1 runAsCoroutine: 1
useFungusModule: 1 useFungusModule: 1
@ -5408,7 +5408,7 @@ MonoBehaviour:
height: 859 height: 859
selectedBlock: {fileID: 942227027} selectedBlock: {fileID: 942227027}
selectedCommands: selectedCommands:
- {fileID: 942227025} - {fileID: 942227053}
variables: variables:
- {fileID: 942227029} - {fileID: 942227029}
- {fileID: 942227035} - {fileID: 942227035}
@ -5575,7 +5575,7 @@ MonoBehaviour:
errorMessage: errorMessage:
indentLevel: 0 indentLevel: 0
luaEnvironment: {fileID: 0} luaEnvironment: {fileID: 0}
luaScript: 'local c = unity.factory.color(1, 0.5, 1, 1) luaScript: 'local c = fungus.factory.color(1, 0.5, 1, 1)
return c' return c'
@ -5748,7 +5748,7 @@ MonoBehaviour:
errorMessage: errorMessage:
indentLevel: 0 indentLevel: 0
luaEnvironment: {fileID: 0} luaEnvironment: {fileID: 0}
luaScript: 'local v = unity.factory.vector2(1,1) luaScript: 'local v = fungus.factory.vector2(1,1)
return v' return v'
@ -5819,7 +5819,7 @@ MonoBehaviour:
errorMessage: errorMessage:
indentLevel: 0 indentLevel: 0
luaEnvironment: {fileID: 0} luaEnvironment: {fileID: 0}
luaScript: 'local v = unity.factory.vector3(1,1,1) luaScript: 'local v = fungus.factory.vector3(1,1,1)
return v' return v'
useFungusModule: 1 useFungusModule: 1
@ -7630,7 +7630,7 @@ GameObject:
m_Icon: {fileID: 0} m_Icon: {fileID: 0}
m_NavMeshLayer: 0 m_NavMeshLayer: 0
m_StaticEditorFlags: 0 m_StaticEditorFlags: 0
m_IsActive: 0 m_IsActive: 1
--- !u!114 &1641285457 --- !u!114 &1641285457
MonoBehaviour: MonoBehaviour:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0
@ -8827,7 +8827,7 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 61dddfdc5e0e44ca298d8f46f7f5a915, type: 3} m_Script: {fileID: 11500000, guid: 61dddfdc5e0e44ca298d8f46f7f5a915, type: 3}
m_Name: m_Name:
m_EditorClassIdentifier: m_EditorClassIdentifier:
selectedFlowchart: {fileID: 56584893} selectedFlowchart: {fileID: 942227028}
--- !u!4 &1791050794 --- !u!4 &1791050794
Transform: Transform:
m_ObjectHideFlags: 1 m_ObjectHideFlags: 1
@ -9329,7 +9329,7 @@ MonoBehaviour:
variablesScrollPos: {x: 0, y: 0} variablesScrollPos: {x: 0, y: 0}
variablesExpanded: 1 variablesExpanded: 1
blockViewHeight: 400 blockViewHeight: 400
zoom: 0.9729984 zoom: 1
scrollViewRect: scrollViewRect:
serializedVersion: 2 serializedVersion: 2
x: -343 x: -343
@ -9337,8 +9337,7 @@ MonoBehaviour:
width: 1114 width: 1114
height: 859 height: 859
selectedBlock: {fileID: 1983492494} selectedBlock: {fileID: 1983492494}
selectedCommands: selectedCommands: []
- {fileID: 1983492498}
variables: variables:
- {fileID: 1983492492} - {fileID: 1983492492}
- {fileID: 1983492497} - {fileID: 1983492497}

78
Assets/Tests/Lua/LuaEnvironmentTests.unity

@ -961,7 +961,7 @@ MonoBehaviour:
local t = 0 local t = 0
local timer = unity.time.time local timer = fungus.time.time
-- Wait with timeScale = -1 -- Wait with timeScale = -1
@ -994,7 +994,7 @@ MonoBehaviour:
-- The real elapsed time should be 3 seconds -- The real elapsed time should be 3 seconds
elapsed = (unity.time.time - timer) elapsed = (fungus.time.time - timer)
assert(elapsed >= 3) assert(elapsed >= 3)
@ -1622,14 +1622,16 @@ MonoBehaviour:
luaFile: {fileID: 0} luaFile: {fileID: 0}
luaScript: '-- Test string table localisation system luaScript: '-- Test string table localisation system
fungus.assert(unity.stringtable != nil) print (fungus.stringtable)
fungus.assert(fungus.stringtable != nil)
-- Check string table loaded ok -- Check string table loaded ok
fungus.assert(unity.stringtable.hello.en == "Hi there") fungus.assert(fungus.stringtable.hello.en == "Hi there")
fungus.assert(unity.stringtable.hello.fr == "Bonjour") fungus.assert(fungus.stringtable.hello.fr == "Bonjour")
-- Check getstring() -- Check getstring()
@ -1718,9 +1720,9 @@ MonoBehaviour:
luaFile: {fileID: 0} luaFile: {fileID: 0}
luaScript: '-- Test if object binding works luaScript: '-- Test if object binding works
unity.test.assert (dummy != nil) fungus.assert (dummy != nil)
unity.test.pass() fungus.pass()
' '
runAsCoroutine: 1 runAsCoroutine: 1
@ -1769,7 +1771,8 @@ MonoBehaviour:
width: 1114 width: 1114
height: 859 height: 859
selectedBlock: {fileID: 1828947834} selectedBlock: {fileID: 1828947834}
selectedCommands: [] selectedCommands:
- {fileID: 1828947833}
variables: [] variables: []
description: "Check if the LuaStore prime table is created \nand registered as fungus.store, description: "Check if the LuaStore prime table is created \nand registered as fungus.store,
and if we can write and read values to it." and if we can write and read values to it."
@ -2014,12 +2017,12 @@ MonoBehaviour:
luaScript: '-- Test GameObject methods like find, instantiate, spawn, etc. luaScript: '-- Test GameObject methods like find, instantiate, spawn, etc.
testgo = unity.luautils.find("TestGameObject") testgo = fungus.luautils.find("TestGameObject")
fungus.assert(testgo != null) fungus.assert(testgo != null)
go = unity.luautils.instantiate(testgo) go = fungus.luautils.instantiate(testgo)
fungus.assert(go != null) fungus.assert(go != null)
@ -2029,17 +2032,17 @@ MonoBehaviour:
fungus.assert(go.name == "fred") fungus.assert(go.name == "fred")
unity.luautils.destroy(go) fungus.luautils.destroy(go)
-- Spawn an object from a prefab in a Resources folder -- Spawn an object from a prefab in a Resources folder
saydialog = unity.luautils.spawn("SayDialog") saydialog = fungus.luautils.spawn("SayDialog")
fungus.assert(saydialog != nil) fungus.assert(saydialog != nil)
unity.luautils.destroy(saydialog) fungus.luautils.destroy(saydialog)
fungus.pass()' fungus.pass()'
@ -2105,6 +2108,10 @@ Prefab:
m_Modification: m_Modification:
m_TransformParent: {fileID: 1960220032} m_TransformParent: {fileID: 1960220032}
m_Modifications: m_Modifications:
- target: {fileID: 11414792, guid: e0c2b90c058ff43f4a56a266d4fa721b, type: 2}
propertyPath: boundTypes.Array.size
value: 10
objectReference: {fileID: 0}
- target: {fileID: 11414792, guid: e0c2b90c058ff43f4a56a266d4fa721b, type: 2} - target: {fileID: 11414792, guid: e0c2b90c058ff43f4a56a266d4fa721b, type: 2}
propertyPath: boundObjects.Array.size propertyPath: boundObjects.Array.size
value: 1 value: 1
@ -2149,6 +2156,51 @@ Prefab:
propertyPath: boundObjects.Array.data[0].key propertyPath: boundObjects.Array.data[0].key
value: dummy value: dummy
objectReference: {fileID: 0} objectReference: {fileID: 0}
- target: {fileID: 11414792, guid: e0c2b90c058ff43f4a56a266d4fa721b, type: 2}
propertyPath: boundTypes.Array.data[0]
value: UnityEngine.GameObject, UnityEngine, Version=0.0.0.0, Culture=neutral,
PublicKeyToken=null
objectReference: {fileID: 0}
- target: {fileID: 11414792, guid: e0c2b90c058ff43f4a56a266d4fa721b, type: 2}
propertyPath: boundTypes.Array.data[1]
value: UnityEngine.PrimitiveType, UnityEngine, Version=0.0.0.0, Culture=neutral,
PublicKeyToken=null
objectReference: {fileID: 0}
- target: {fileID: 11414792, guid: e0c2b90c058ff43f4a56a266d4fa721b, type: 2}
propertyPath: boundTypes.Array.data[2]
value: UnityEngine.Component, UnityEngine, Version=0.0.0.0, Culture=neutral,
PublicKeyToken=null
objectReference: {fileID: 0}
- target: {fileID: 11414792, guid: e0c2b90c058ff43f4a56a266d4fa721b, type: 2}
propertyPath: boundTypes.Array.data[3]
value: System.Type, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
objectReference: {fileID: 0}
- target: {fileID: 11414792, guid: e0c2b90c058ff43f4a56a266d4fa721b, type: 2}
propertyPath: boundTypes.Array.data[4]
value: System.Boolean, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
objectReference: {fileID: 0}
- target: {fileID: 11414792, guid: e0c2b90c058ff43f4a56a266d4fa721b, type: 2}
propertyPath: boundTypes.Array.data[5]
value: System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
objectReference: {fileID: 0}
- target: {fileID: 11414792, guid: e0c2b90c058ff43f4a56a266d4fa721b, type: 2}
propertyPath: boundTypes.Array.data[6]
value: UnityEngine.SendMessageOptions, UnityEngine, Version=0.0.0.0, Culture=neutral,
PublicKeyToken=null
objectReference: {fileID: 0}
- target: {fileID: 11414792, guid: e0c2b90c058ff43f4a56a266d4fa721b, type: 2}
propertyPath: boundTypes.Array.data[7]
value: UnityEngine.SceneManagement.Scene, UnityEngine, Version=0.0.0.0, Culture=neutral,
PublicKeyToken=null
objectReference: {fileID: 0}
- target: {fileID: 11414792, guid: e0c2b90c058ff43f4a56a266d4fa721b, type: 2}
propertyPath: boundTypes.Array.data[8]
value: UnityEngine.Object, UnityEngine, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
objectReference: {fileID: 0}
- target: {fileID: 11414792, guid: e0c2b90c058ff43f4a56a266d4fa721b, type: 2}
propertyPath: boundTypes.Array.data[9]
value: System.Single, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
objectReference: {fileID: 0}
m_RemovedComponents: [] m_RemovedComponents: []
m_ParentPrefab: {fileID: 100100000, guid: e0c2b90c058ff43f4a56a266d4fa721b, type: 2} m_ParentPrefab: {fileID: 100100000, guid: e0c2b90c058ff43f4a56a266d4fa721b, type: 2}
m_IsPrefabParent: 0 m_IsPrefabParent: 0

Loading…
Cancel
Save