Beispiel #1
0
 public void Test_CallFunction_InputInts_ReturnSum()
 {
     // Test passing more than one input
     LuaUtilities.LoadScriptFromFile(testCode1Path);
     MoonSharp.Interpreter.DynValue value = LuaUtilities.CallFunction("test_func3", 4, 7);
     Assert.AreEqual(11, (int)value.CastToNumber());
 }
        public static void LoadScripts()
        {
            string luaFilePath = Path.Combine(Application.streamingAssetsPath, "LUA");

            luaFilePath = Path.Combine(luaFilePath, "Events.lua");
            LuaUtilities.LoadScriptFromFile(luaFilePath);
        }
Beispiel #3
0
 public void Test_CallFunction()
 {
     // Test a function that dosent return anything (void c# , nil/nan Lua)
     LuaUtilities.LoadScriptFromFile(testCode1Path);
     MoonSharp.Interpreter.DynValue value = LuaUtilities.CallFunction("test_func0");
     Assert.AreEqual(true, value.IsNilOrNan());
 }
Beispiel #4
0
 public void Test_CallFunction_InputString_ReturnInput()
 {
     // Test a function that returns the String passed to it
     LuaUtilities.LoadScriptFromFile(testCode1Path);
     MoonSharp.Interpreter.DynValue value = LuaUtilities.CallFunction("test_func2", "inputted value");
     Assert.AreEqual("inputted value", value.CastToString());
 }
Beispiel #5
0
 public void Test_CallFunction_ReturnString()
 {
     // Test a function that returns a string
     LuaUtilities.LoadScriptFromFile(testCode1Path);
     MoonSharp.Interpreter.DynValue value = LuaUtilities.CallFunction("test_func1");
     Assert.AreEqual("test_func1_returns", value.CastToString());
 }
    private void LoadLuaScript()
    {
        string filePath = System.IO.Path.Combine(Application.streamingAssetsPath, "LUA");

        filePath = System.IO.Path.Combine(filePath, "GameEvent.lua");

        LuaUtilities.LoadScriptFromFile(filePath);
    }
Beispiel #7
0
    void LoadFurnitureLua(string filePath)
    {
        string luaCode = System.IO.File.ReadAllText(filePath);

        // Instantiate the singleton

        LuaUtilities.LoadScript(luaCode);
    }
Beispiel #8
0
 public FurnitureActions()
 {
     // TODO: This should be moved to a more logical location
     LuaUtilities.RegisterGlobal(typeof(Inventory));
     LuaUtilities.RegisterGlobal(typeof(Job));
     LuaUtilities.RegisterGlobal(typeof(ModUtils));
     LuaUtilities.RegisterGlobal(typeof(World));
 }
Beispiel #9
0
    public void DoWork(float workTime)
    {
        // We don't know if the Job can actually be worked, but still call the callbacks
        // so that animations and whatnot can be updated.
        if (cbJobWorked != null)
        {
            cbJobWorked(this);
        }

        if (cbJobWorkedLua != null)
        {
            foreach (string luaFunction in cbJobWorkedLua.ToList())
            {
                LuaUtilities.CallFunction(luaFunction, this);
            }
        }

        // Check to make sure we actually have everything we need.
        // If not, don't register the work time.
        if (MaterialNeedsMet() == false)
        {
            ////Debug.LogError("Tried to do work on a job that doesn't have all the material.");
            return;
        }

        jobTime -= workTime;

        if (jobTime <= 0)
        {
            // Do whatever is supposed to happen with a job cycle completes.
            if (cbJobCompleted != null)
            {
                cbJobCompleted(this);
            }

            foreach (string luaFunction in cbJobCompletedLua.ToList())
            {
                LuaUtilities.CallFunction(luaFunction, this);
            }

            if (jobRepeats == false)
            {
                // Let everyone know that the job is officially concluded
                if (cbJobStopped != null)
                {
                    cbJobStopped(this);
                }
            }
            else
            {
                // This is a repeating job and must be reset.
                jobTime += jobTimeRequired;
            }
        }
    }
Beispiel #10
0
    public string GetSpriteName()
    {
        if (getSpriteNameAction == null || getSpriteNameAction.Length == 0)
        {
            return(objectType);
        }

        DynValue ret = LuaUtilities.CallFunction(getSpriteNameAction, this);

        return(ret.String);
    }
Beispiel #11
0
 public static void LoadModsScripts(DirectoryInfo[] mods)
 {
     foreach (DirectoryInfo mod in mods)
     {
         string luaModFile = Path.Combine(mod.FullName, "Events.lua");
         if (File.Exists(luaModFile))
         {
             LuaUtilities.LoadScriptFromFile(luaModFile);
         }
     }
 }
Beispiel #12
0
    public string GetSpriteName()
    {
        if (string.IsNullOrEmpty(getSpriteNameAction))
        {
            return(ObjectType);
        }

        DynValue ret = LuaUtilities.CallFunction(getSpriteNameAction, this);

        return(ret.String);
    }
Beispiel #13
0
    public static void CallFunctionsWithEvent(string[] functionNames, GameEvent gameEvent)
    {
        foreach (string fn in functionNames)
        {
            DynValue result = LuaUtilities.CallFunction(fn, gameEvent);

            if (result.Type == DataType.String)
            {
                Debug.ULogErrorChannel("Lua", result.String);
            }
        }
    }
Beispiel #14
0
    public FurnitureActions()
    {
        // TODO: This should be moved to a more logical location
        LuaUtilities.RegisterGlobal(typeof(Inventory));
        LuaUtilities.RegisterGlobal(typeof(Job));
        LuaUtilities.RegisterGlobal(typeof(ModUtils));
        LuaUtilities.RegisterGlobal(typeof(World));
        LuaUtilities.RegisterGlobal(typeof(WorldController));
        LuaUtilities.RegisterGlobal(typeof(Power.Connection));

        LoadScripts();
    }
    public static void CallFunctionsWithFurniture(string[] functionNames, Furniture furn, float deltaTime)
    {
        foreach (string fn in functionNames)
        {
            DynValue result = LuaUtilities.CallFunction(fn, furn, deltaTime);

            if (result.Type == DataType.String)
            {
                Debug.Log(result.String);
            }
        }
    }
Beispiel #16
0
    public static void CallFunctionsWithNeed(string[] functionNames, Need need, float deltaTime)
    {
        foreach (string fn in functionNames)
        {
            DynValue result = LuaUtilities.CallFunction(fn, need, deltaTime);

            if (result.Type == DataType.String)
            {
                Debug.ULogChannel("NeedActions", "Lua response:", result.String);
            }
        }
    }
Beispiel #17
0
    public Enterability IsEnterable()
    {
        if (string.IsNullOrEmpty(isEnterableAction))
        {
            return(Enterability.Yes);
        }

        //// FurnitureActions.CallFunctionsWithFurniture( isEnterableActions.ToArray(), this );

        DynValue ret = LuaUtilities.CallFunction(isEnterableAction, this);

        return((Enterability)ret.Number);
    }
Beispiel #18
0
    public ENTERABILITY IsEnterable()
    {
        if (isEnterableAction == null || isEnterableAction.Length == 0)
        {
            return(ENTERABILITY.Yes);
        }

        //// FurnitureActions.CallFunctionsWithFurniture( isEnterableActions.ToArray(), this );

        DynValue ret = LuaUtilities.CallFunction(isEnterableAction, this);

        return((ENTERABILITY)ret.Number);
    }
Beispiel #19
0
    public static void LoadScriptFromFile(string filePath)
    {
        string luaCode = System.IO.File.ReadAllText(filePath);

        try
        {
            LuaUtilities.LoadScript(luaCode);
        }
        catch (MoonSharp.Interpreter.SyntaxErrorException e)
        {
            Debug.LogError("[" + Path.GetFileName(filePath) + "] LUA Parse error: " + e.DecoratedMessage);
        }
    }
Beispiel #20
0
        public Scheduler()
        {
            this.events = new List <ScheduledEvent>();
            this.eventsToAddNextTick = new List <ScheduledEvent>();

            Debug.ULogChannel("Scheduler", "Loading Lua stripts");

            // FIXME: Are these actually needed here?
            LuaUtilities.RegisterGlobal(typeof(Inventory));
            LuaUtilities.RegisterGlobal(typeof(Job));
            LuaUtilities.RegisterGlobal(typeof(ModUtils));
            LuaUtilities.RegisterGlobal(typeof(World));
            LoadScripts();
        }
Beispiel #21
0
    // Checks whether the given floor type is allowed to be built on the tile.
    // TODO Export this kind of check to an XML/LUA file for easier modding of floor types.
    private bool CanBuildTileTypeHere(Tile t, TileType tileType)
    {
        DynValue value = LuaUtilities.CallFunction(tileType.CanBuildHereLua, t);

        if (value != null)
        {
            return(value.Boolean);
        }
        else
        {
            Debug.ULogChannel("Lua", "Found no lua function " + tileType.CanBuildHereLua);

            return(false);
        }
    }
Beispiel #22
0
    public void Update(float deltaTime)
    {
        int conditionsMet = 0;

        foreach (string precondition in preconditions)
        {
            // Call lua precondition it should return 1 if met otherwise 0
            conditionsMet += (int)LuaUtilities.CallFunction(precondition, this, deltaTime).Number;
        }

        if (conditionsMet >= preconditions.Count && executed == false && (MaxRepeats <= 0 || repeats < MaxRepeats))
        {
            repeats++;
            Execute();
        }
    }
        public ScheduledEvent(ScheduledEvent eventPrototype, float cooldown, float timeToWait, bool repeatsForever = false, int repeats = 1)
        {
            this.Name = eventPrototype.Name;
            if (eventPrototype.EventType == EventType.CSharp)
            {
                this.OnFire = eventPrototype.OnFire;
            }
            else
            {
                this.OnFire = (evt) => LuaUtilities.CallFunction(eventPrototype.LuaFunctionName, evt);
            }

            this.Cooldown       = cooldown;
            this.TimeToWait     = timeToWait;
            this.RepeatsForever = repeatsForever;
            this.RepeatsLeft    = repeats;
            this.EventType      = eventPrototype.EventType;
        }
Beispiel #24
0
    /// <summary>
    /// Loads all TileType definitions in Data\ and Data\Mods
    /// </summary>
    public static void LoadTileTypes()
    {
        // Load lua code
        string luaPath     = System.IO.Path.Combine(Application.streamingAssetsPath, "LUA");
        string luaFilePath = System.IO.Path.Combine(luaPath, "Tile.lua");
        string luaCode     = System.IO.File.ReadAllText(luaFilePath);

        LuaUtilities.LoadScript(luaCode);

        // Load all mod defined lua code
        foreach (DirectoryInfo mod in WorldController.Instance.modsManager.GetMods())
        {
            foreach (FileInfo file in mod.GetFiles("Tiles.lua"))
            {
                Debug.ULogChannel("TileType", "Loading mod " + mod.Name + " TileType definitions!");

                luaCode = System.IO.File.ReadAllText(file.FullName);

                LuaUtilities.LoadScript(luaCode);
            }
        }

        // Load TileType xml definitions
        string dataPath = System.IO.Path.Combine(Application.streamingAssetsPath, "Data");
        string xmlPath  = System.IO.Path.Combine(dataPath, "Tiles.xml");
        string xmlText  = System.IO.File.ReadAllText(xmlPath);

        readTileTypesFromXml(xmlText);

        // Load all mod defined TileType definitions
        foreach (DirectoryInfo mod in WorldController.Instance.modsManager.GetMods())
        {
            foreach (FileInfo file in mod.GetFiles("Tiles.xml"))
            {
                Debug.ULogChannel("TileType", "Loading mod " + mod.Name + " TileType definitions!");

                xmlText = System.IO.File.ReadAllText(file.FullName);

                readTileTypesFromXml(xmlText);
            }
        }
    }
Beispiel #25
0
    void CreateFurniturePrototypes()
    {
        string luaFilePath = System.IO.Path.Combine(Application.streamingAssetsPath, "LUA");

        luaFilePath = System.IO.Path.Combine(luaFilePath, "Furniture.lua");
        LuaUtilities.LoadScriptFromFile(luaFilePath);


        furniturePrototypes    = new Dictionary <string, Furniture>();
        furnitureJobPrototypes = new Dictionary <string, Job>();

        // READ FURNITURE PROTOTYPE XML FILE HERE
        // TODO:  Probably we should be getting past a StreamIO handle or the raw
        // text here, rather than opening the file ourselves.

        string dataPath         = System.IO.Path.Combine(Application.streamingAssetsPath, "Data");
        string filePath         = System.IO.Path.Combine(dataPath, "Furniture.xml");
        string furnitureXmlText = System.IO.File.ReadAllText(filePath);

        LoadFurniturePrototypesFromFile(furnitureXmlText);


        DirectoryInfo[] mods = WorldController.Instance.modsManager.GetMods();
        foreach (DirectoryInfo mod in mods)
        {
            string furnitureLuaModFile = System.IO.Path.Combine(mod.FullName, "Furniture.lua");
            if (File.Exists(furnitureLuaModFile))
            {
                LuaUtilities.LoadScriptFromFile(furnitureLuaModFile);
            }

            string furnitureXmlModFile = System.IO.Path.Combine(mod.FullName, "Furniture.xml");
            if (File.Exists(furnitureXmlModFile))
            {
                string furnitureXmlModText = System.IO.File.ReadAllText(furnitureXmlModFile);
                LoadFurniturePrototypesFromFile(furnitureXmlModText);
            }
        }
    }
Beispiel #26
0
        private void Awake()
        {
            animator = GetComponent <Animator> ();
            motor    = GetComponent <PlayerMotor> ();
            Entity   = new Entity(gameObject);
            Entity.SetId("000000");
            player   = new Player(gameObject);
            AssetId  = "00000000";
            UniqueId = "00000000";

            GameObject = gameObject;
            Transform  = transform;

            var script = new Script();

            LuaUtilities.ApplyDefaultValues(script);
            Script = new LuaScript {
                code   = string.Empty,
                script = script
            };

            ActiveComponents = GetComponents <NativeComponent> ();
        }
Beispiel #27
0
    public static void CallFunctionsWithFurniture <T>(string[] functionNames, T furn, float deltaTime)
    {
        if (furn == null)
        {
            Debug.LogError("Furn is null, cannot call LUA function (something is fishy).");
        }

        foreach (string fn in functionNames)
        {
            if (fn == null)
            {
                Debug.LogError("'" + fn + "' is not a LUA function.");
                return;
            }

            DynValue result = LuaUtilities.CallFunction(fn, furn, deltaTime);

            if (result.Type == DataType.String)
            {
                Debug.Log(result.String);
            }
        }
    }
Beispiel #28
0
    /// <summary>
    /// Loads all TileType definitions in Data\ and Data\Mods.
    /// </summary>
    public static void LoadTileTypes()
    {
        // Load lua code
        string luaPath     = Path.Combine(Application.streamingAssetsPath, "LUA");
        string luaFilePath = Path.Combine(luaPath, TilesScriptFileName);

        LuaUtilities.LoadScriptFromFile(luaFilePath);

        // Load all mod defined lua code
        foreach (DirectoryInfo mod in WorldController.Instance.modsManager.GetMods())
        {
            foreach (FileInfo file in mod.GetFiles(TilesScriptFileName))
            {
                Debug.ULogChannel(ULogChanelName, "Loading Tile LUA scripts from mod: {0}", mod.Name);
                LuaUtilities.LoadScriptFromFile(file.FullName);
            }
        }

        // Load TileType xml definitions
        string dataPath = Path.Combine(Application.streamingAssetsPath, "Data");
        string xmlPath  = Path.Combine(dataPath, TilesDescriptionFileName);
        string xmlText  = File.ReadAllText(xmlPath);

        ReadTileTypesFromXml(xmlText);

        // Load all mod defined TileType definitions
        foreach (DirectoryInfo mod in WorldController.Instance.modsManager.GetMods())
        {
            foreach (FileInfo file in mod.GetFiles(TilesDescriptionFileName))
            {
                Debug.ULogChannel(ULogChanelName, "Loading TileType definitions from mod: {0}", mod.Name);
                xmlText = File.ReadAllText(file.FullName);
                ReadTileTypesFromXml(xmlText);
            }
        }
    }
Beispiel #29
0
    public static void CallFunctionsWithFurniture <T>(string[] functionNames, T furn, float deltaTime)
    {
        if (furn == null)
        {
            //These errors are about the lua code so putting themin the lua channel
            Debug.ULogErrorChannel("Lua", "Furn is null, cannot call LUA function (something is fishy).");
        }

        foreach (string fn in functionNames)
        {
            if (fn == null)
            {
                Debug.ULogErrorChannel("Lua", "'" + fn + "' is not a LUA function.");
                return;
            }

            DynValue result = LuaUtilities.CallFunction(fn, furn, deltaTime);

            if (result.Type == DataType.String)
            {
                Debug.ULogErrorChannel("Lua", result.String);
            }
        }
    }
Beispiel #30
0
 private void InvokeContextMenuLuaAction(string luaFunction, Character character)
 {
     LuaUtilities.CallFunction(luaFunction, this, character);
 }