├── luablock
├── Test
│ ├── obj
│ │ └── Debug
│ │ │ ├── TemporaryGeneratedFile_036C0B5B-1481-4323-8D20-8F5ADCB23D92.cs
│ │ │ ├── TemporaryGeneratedFile_5937a670-0e60-4077-877b-f7221da3dda1.cs
│ │ │ ├── TemporaryGeneratedFile_E7A71F73-0F8D-4B9B-B56E-8E70B10BC5D3.cs
│ │ │ └── Test.csproj.FileListAbsolute.txt
│ ├── bin
│ │ └── Debug
│ │ │ ├── tt.schematic
│ │ │ ├── Tree.schematic
│ │ │ ├── test.schematic
│ │ │ ├── cmd
│ │ │ ├── tp.lua
│ │ │ ├── xp.lua
│ │ │ ├── clear.lua
│ │ │ ├── clone.lua
│ │ │ ├── debug.lua
│ │ │ ├── fill.lua
│ │ │ ├── give.lua
│ │ │ ├── kill.lua
│ │ │ ├── seed.lua
│ │ │ ├── time.lua
│ │ │ ├── effect.lua
│ │ │ ├── enchant.lua
│ │ │ ├── execute.lua
│ │ │ ├── gamemode.lua
│ │ │ ├── gamerule.lua
│ │ │ ├── particle.lua
│ │ │ ├── publish.lua
│ │ │ ├── setblock.lua
│ │ │ ├── summon.lua
│ │ │ ├── tellraw.lua
│ │ │ ├── testfor.lua
│ │ │ ├── trigger.lua
│ │ │ ├── weather.lua
│ │ │ ├── blockdata.lua
│ │ │ ├── difficulty.lua
│ │ │ ├── playsound.lua
│ │ │ ├── scoreboard.lua
│ │ │ ├── spawnpoint.lua
│ │ │ ├── achievement.lua
│ │ │ ├── setworldspawn.lua
│ │ │ ├── spreadplayers.lua
│ │ │ ├── testforblock.lua
│ │ │ ├── testforblocks.lua
│ │ │ ├── worldborder.lua
│ │ │ ├── defaultgamemode.lua
│ │ │ ├── toggledownfall.lua
│ │ │ └── print.lua
│ │ │ ├── Test.exe.config
│ │ │ ├── Test.vshost.exe.config
│ │ │ ├── tes1t.lua
│ │ │ ├── test.lua
│ │ │ └── Test.vshost.exe.manifest
│ ├── App.config
│ ├── Program.cs
│ ├── Properties
│ │ └── AssemblyInfo.cs
│ └── Test.csproj
├── luablock
│ ├── obj
│ │ └── Debug
│ │ │ ├── TemporaryGeneratedFile_036C0B5B-1481-4323-8D20-8F5ADCB23D92.cs
│ │ │ ├── TemporaryGeneratedFile_5937a670-0e60-4077-877b-f7221da3dda1.cs
│ │ │ ├── TemporaryGeneratedFile_E7A71F73-0F8D-4B9B-B56E-8E70B10BC5D3.cs
│ │ │ └── luablock.csproj.FileListAbsolute.txt
│ ├── CmdJson.cs
│ ├── packages.config
│ ├── Command.cs
│ ├── Commands
│ │ ├── Say.cs
│ │ └── LuaCmd.cs
│ ├── CommandManager.cs
│ ├── Properties
│ │ └── AssemblyInfo.cs
│ ├── NetLua
│ │ ├── LuaContext.cs
│ │ ├── Libraries
│ │ │ ├── MathLibrary.cs
│ │ │ └── IoLibrary.cs
│ │ ├── Ast
│ │ │ └── Ast.cs
│ │ ├── LuaGrammar.cs
│ │ ├── Lua.cs
│ │ ├── LuaEvents.cs
│ │ ├── LuaObject.cs
│ │ ├── Parser.cs
│ │ └── LuaCompiler.cs
│ ├── luablock.csproj
│ ├── Schematic.cs
│ └── Compiler.cs
├── packages
│ ├── fNbt.0.6.3
│ │ └── fNbt.0.6.3.nupkg
│ ├── Irony.0.9.0
│ │ ├── Irony.0.9.0.nupkg
│ │ └── tools
│ │ │ └── Irony.GrammarExplorer.exe.config
│ └── MoonSharp.0.9.8.0
│ │ └── MoonSharp.0.9.8.0.nupkg
└── luablock.sln
└── README.md
/luablock/Test/obj/Debug/TemporaryGeneratedFile_036C0B5B-1481-4323-8D20-8F5ADCB23D92.cs:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/luablock/Test/obj/Debug/TemporaryGeneratedFile_5937a670-0e60-4077-877b-f7221da3dda1.cs:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/luablock/Test/obj/Debug/TemporaryGeneratedFile_E7A71F73-0F8D-4B9B-B56E-8E70B10BC5D3.cs:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/luablock/luablock/obj/Debug/TemporaryGeneratedFile_036C0B5B-1481-4323-8D20-8F5ADCB23D92.cs:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/luablock/luablock/obj/Debug/TemporaryGeneratedFile_5937a670-0e60-4077-877b-f7221da3dda1.cs:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/luablock/luablock/obj/Debug/TemporaryGeneratedFile_E7A71F73-0F8D-4B9B-B56E-8E70B10BC5D3.cs:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/luablock/Test/bin/Debug/tt.schematic:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Myvar/LuaBlock/HEAD/luablock/Test/bin/Debug/tt.schematic
--------------------------------------------------------------------------------
/luablock/Test/bin/Debug/Tree.schematic:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Myvar/LuaBlock/HEAD/luablock/Test/bin/Debug/Tree.schematic
--------------------------------------------------------------------------------
/luablock/Test/bin/Debug/test.schematic:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Myvar/LuaBlock/HEAD/luablock/Test/bin/Debug/test.schematic
--------------------------------------------------------------------------------
/luablock/Test/bin/Debug/cmd/tp.lua:
--------------------------------------------------------------------------------
1 | ret = "/tp"
2 |
3 | for i=0,argsl,1
4 | do
5 | ret = ret .. " " .. args[i]
6 | end
7 |
8 | return ret
--------------------------------------------------------------------------------
/luablock/Test/bin/Debug/cmd/xp.lua:
--------------------------------------------------------------------------------
1 | ret = "/xp"
2 |
3 | for i=0,argsl,1
4 | do
5 | ret = ret .. " " .. args[i]
6 | end
7 |
8 | return ret
--------------------------------------------------------------------------------
/luablock/packages/fNbt.0.6.3/fNbt.0.6.3.nupkg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Myvar/LuaBlock/HEAD/luablock/packages/fNbt.0.6.3/fNbt.0.6.3.nupkg
--------------------------------------------------------------------------------
/luablock/Test/bin/Debug/cmd/clear.lua:
--------------------------------------------------------------------------------
1 | ret = "/clear"
2 |
3 | for i=0,argsl,1
4 | do
5 | ret = ret .. " " .. args[i]
6 | end
7 |
8 | return ret
--------------------------------------------------------------------------------
/luablock/Test/bin/Debug/cmd/clone.lua:
--------------------------------------------------------------------------------
1 | ret = "/clone"
2 |
3 | for i=0,argsl,1
4 | do
5 | ret = ret .. " " .. args[i]
6 | end
7 |
8 | return ret
--------------------------------------------------------------------------------
/luablock/Test/bin/Debug/cmd/debug.lua:
--------------------------------------------------------------------------------
1 | ret = "/debug"
2 |
3 | for i=0,argsl,1
4 | do
5 | ret = ret .. " " .. args[i]
6 | end
7 |
8 | return ret
--------------------------------------------------------------------------------
/luablock/Test/bin/Debug/cmd/fill.lua:
--------------------------------------------------------------------------------
1 | ret = "/fill"
2 |
3 | for i=0,argsl,1
4 | do
5 | ret = ret .. " " .. args[i]
6 | end
7 |
8 | return ret
--------------------------------------------------------------------------------
/luablock/Test/bin/Debug/cmd/give.lua:
--------------------------------------------------------------------------------
1 | ret = "/give"
2 |
3 | for i=0,argsl,1
4 | do
5 | ret = ret .. " " .. args[i]
6 | end
7 |
8 | return ret
--------------------------------------------------------------------------------
/luablock/Test/bin/Debug/cmd/kill.lua:
--------------------------------------------------------------------------------
1 | ret = "/kill"
2 |
3 | for i=0,argsl,1
4 | do
5 | ret = ret .. " " .. args[i]
6 | end
7 |
8 | return ret
--------------------------------------------------------------------------------
/luablock/Test/bin/Debug/cmd/seed.lua:
--------------------------------------------------------------------------------
1 | ret = "/seed"
2 |
3 | for i=0,argsl,1
4 | do
5 | ret = ret .. " " .. args[i]
6 | end
7 |
8 | return ret
--------------------------------------------------------------------------------
/luablock/Test/bin/Debug/cmd/time.lua:
--------------------------------------------------------------------------------
1 | ret = "/time"
2 |
3 | for i=0,argsl,1
4 | do
5 | ret = ret .. " " .. args[i]
6 | end
7 |
8 | return ret
--------------------------------------------------------------------------------
/luablock/packages/Irony.0.9.0/Irony.0.9.0.nupkg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Myvar/LuaBlock/HEAD/luablock/packages/Irony.0.9.0/Irony.0.9.0.nupkg
--------------------------------------------------------------------------------
/luablock/Test/bin/Debug/cmd/effect.lua:
--------------------------------------------------------------------------------
1 | ret = "/effect"
2 |
3 | for i=0,argsl,1
4 | do
5 | ret = ret .. " " .. args[i]
6 | end
7 |
8 | return ret
--------------------------------------------------------------------------------
/luablock/Test/bin/Debug/cmd/enchant.lua:
--------------------------------------------------------------------------------
1 | ret = "/enchant"
2 |
3 | for i=0,argsl,1
4 | do
5 | ret = ret .. " " .. args[i]
6 | end
7 |
8 | return ret
--------------------------------------------------------------------------------
/luablock/Test/bin/Debug/cmd/execute.lua:
--------------------------------------------------------------------------------
1 | ret = "/execute"
2 |
3 | for i=0,argsl,1
4 | do
5 | ret = ret .. " " .. args[i]
6 | end
7 |
8 | return ret
--------------------------------------------------------------------------------
/luablock/Test/bin/Debug/cmd/gamemode.lua:
--------------------------------------------------------------------------------
1 | ret = "/gamemode"
2 |
3 | for i=0,argsl,1
4 | do
5 | ret = ret .. " " .. args[i]
6 | end
7 |
8 | return ret
--------------------------------------------------------------------------------
/luablock/Test/bin/Debug/cmd/gamerule.lua:
--------------------------------------------------------------------------------
1 | ret = "/gamerule"
2 |
3 | for i=0,argsl,1
4 | do
5 | ret = ret .. " " .. args[i]
6 | end
7 |
8 | return ret
--------------------------------------------------------------------------------
/luablock/Test/bin/Debug/cmd/particle.lua:
--------------------------------------------------------------------------------
1 | ret = "/particle"
2 |
3 | for i=0,argsl,1
4 | do
5 | ret = ret .. " " .. args[i]
6 | end
7 |
8 | return ret
--------------------------------------------------------------------------------
/luablock/Test/bin/Debug/cmd/publish.lua:
--------------------------------------------------------------------------------
1 | ret = "/publish"
2 |
3 | for i=0,argsl,1
4 | do
5 | ret = ret .. " " .. args[i]
6 | end
7 |
8 | return ret
--------------------------------------------------------------------------------
/luablock/Test/bin/Debug/cmd/setblock.lua:
--------------------------------------------------------------------------------
1 | ret = "/setblock"
2 |
3 | for i=0,argsl,1
4 | do
5 | ret = ret .. " " .. args[i]
6 | end
7 |
8 | return ret
--------------------------------------------------------------------------------
/luablock/Test/bin/Debug/cmd/summon.lua:
--------------------------------------------------------------------------------
1 | ret = "/summon"
2 |
3 | for i=0,argsl,1
4 | do
5 | ret = ret .. " " .. args[i]
6 | end
7 |
8 | return ret
--------------------------------------------------------------------------------
/luablock/Test/bin/Debug/cmd/tellraw.lua:
--------------------------------------------------------------------------------
1 | ret = "/tellraw"
2 |
3 | for i=0,argsl,1
4 | do
5 | ret = ret .. " " .. args[i]
6 | end
7 |
8 | return ret
--------------------------------------------------------------------------------
/luablock/Test/bin/Debug/cmd/testfor.lua:
--------------------------------------------------------------------------------
1 | ret = "#/testfor"
2 |
3 | for i=0,argsl,1
4 | do
5 | ret = ret .. " " .. args[i]
6 | end
7 |
8 | return ret
--------------------------------------------------------------------------------
/luablock/Test/bin/Debug/cmd/trigger.lua:
--------------------------------------------------------------------------------
1 | ret = "/trigger"
2 |
3 | for i=0,argsl,1
4 | do
5 | ret = ret .. " " .. args[i]
6 | end
7 |
8 | return ret
--------------------------------------------------------------------------------
/luablock/Test/bin/Debug/cmd/weather.lua:
--------------------------------------------------------------------------------
1 | ret = "/weather"
2 |
3 | for i=0,argsl,1
4 | do
5 | ret = ret .. " " .. args[i]
6 | end
7 |
8 | return ret
--------------------------------------------------------------------------------
/luablock/Test/bin/Debug/cmd/blockdata.lua:
--------------------------------------------------------------------------------
1 | ret = "/blockdata"
2 |
3 | for i=0,argsl,1
4 | do
5 | ret = ret .. " " .. args[i]
6 | end
7 |
8 | return ret
--------------------------------------------------------------------------------
/luablock/Test/bin/Debug/cmd/difficulty.lua:
--------------------------------------------------------------------------------
1 | ret = "/difficulty"
2 |
3 | for i=0,argsl,1
4 | do
5 | ret = ret .. " " .. args[i]
6 | end
7 |
8 | return ret
--------------------------------------------------------------------------------
/luablock/Test/bin/Debug/cmd/playsound.lua:
--------------------------------------------------------------------------------
1 | ret = "/playsound"
2 |
3 | for i=0,argsl,1
4 | do
5 | ret = ret .. " " .. args[i]
6 | end
7 |
8 | return ret
--------------------------------------------------------------------------------
/luablock/Test/bin/Debug/cmd/scoreboard.lua:
--------------------------------------------------------------------------------
1 | ret = "/scoreboard"
2 |
3 | for i=0,argsl,1
4 | do
5 | ret = ret .. " " .. args[i]
6 | end
7 |
8 | return ret
--------------------------------------------------------------------------------
/luablock/Test/bin/Debug/cmd/spawnpoint.lua:
--------------------------------------------------------------------------------
1 | ret = "/spawnpoint"
2 |
3 | for i=0,argsl,1
4 | do
5 | ret = ret .. " " .. args[i]
6 | end
7 |
8 | return ret
--------------------------------------------------------------------------------
/luablock/Test/bin/Debug/cmd/achievement.lua:
--------------------------------------------------------------------------------
1 | ret = "/achievement"
2 |
3 | for i=0,argsl,1
4 | do
5 | ret = ret .. " " .. args[i]
6 | end
7 |
8 | return ret
--------------------------------------------------------------------------------
/luablock/Test/bin/Debug/cmd/setworldspawn.lua:
--------------------------------------------------------------------------------
1 | ret = "/setworldspawn"
2 |
3 | for i=0,argsl,1
4 | do
5 | ret = ret .. " " .. args[i]
6 | end
7 |
8 | return ret
--------------------------------------------------------------------------------
/luablock/Test/bin/Debug/cmd/spreadplayers.lua:
--------------------------------------------------------------------------------
1 | ret = "/spreadplayers"
2 |
3 | for i=0,argsl,1
4 | do
5 | ret = ret .. " " .. args[i]
6 | end
7 |
8 | return ret
--------------------------------------------------------------------------------
/luablock/Test/bin/Debug/cmd/testforblock.lua:
--------------------------------------------------------------------------------
1 | ret = "#/testforblock"
2 |
3 | for i=0,argsl,1
4 | do
5 | ret = ret .. " " .. args[i]
6 | end
7 |
8 | return ret
--------------------------------------------------------------------------------
/luablock/Test/bin/Debug/cmd/testforblocks.lua:
--------------------------------------------------------------------------------
1 | ret = "/testforblocks"
2 |
3 | for i=0,argsl,1
4 | do
5 | ret = ret .. " " .. args[i]
6 | end
7 |
8 | return ret
--------------------------------------------------------------------------------
/luablock/Test/bin/Debug/cmd/worldborder.lua:
--------------------------------------------------------------------------------
1 | ret = "/worldborder"
2 |
3 | for i=0,argsl,1
4 | do
5 | ret = ret .. " " .. args[i]
6 | end
7 |
8 | return ret
--------------------------------------------------------------------------------
/luablock/Test/bin/Debug/cmd/defaultgamemode.lua:
--------------------------------------------------------------------------------
1 | ret = "/defaultgamemode"
2 |
3 | for i=0,argsl,1
4 | do
5 | ret = ret .. " " .. args[i]
6 | end
7 |
8 | return ret
--------------------------------------------------------------------------------
/luablock/Test/bin/Debug/cmd/toggledownfall.lua:
--------------------------------------------------------------------------------
1 | ret = "/toggledownfall"
2 |
3 | for i=0,argsl,1
4 | do
5 | ret = ret .. " " .. args[i]
6 | end
7 |
8 | return ret
--------------------------------------------------------------------------------
/luablock/packages/MoonSharp.0.9.8.0/MoonSharp.0.9.8.0.nupkg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Myvar/LuaBlock/HEAD/luablock/packages/MoonSharp.0.9.8.0/MoonSharp.0.9.8.0.nupkg
--------------------------------------------------------------------------------
/luablock/Test/App.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/luablock/Test/bin/Debug/Test.exe.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/luablock/Test/bin/Debug/Test.vshost.exe.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/luablock/luablock/CmdJson.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Text;
5 | using System.Threading.Tasks;
6 |
7 | namespace luablock
8 | {
9 | public class CmdJson
10 | {
11 |
12 | }
13 | }
14 |
--------------------------------------------------------------------------------
/luablock/luablock/packages.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/luablock/Test/bin/Debug/cmd/print.lua:
--------------------------------------------------------------------------------
1 | ret = "/tellraw " .. args[0] .. " [ "
2 | --/tellraw @a [ {text:"Your score is "} , {score:{name:lua,objective:lua_if_1}} , {text:"."} ]
3 | for i=1,argsl - 1,1
4 | do
5 | ret = ret .. "{text:\" " .. args[i] .. "\"},"
6 | end
7 |
8 | return ret .. "{score:{name:lua,objective:" .. args[argsl] .."}}]"
--------------------------------------------------------------------------------
/luablock/Test/bin/Debug/tes1t.lua:
--------------------------------------------------------------------------------
1 |
2 | function war()
3 | if item == 0 then
4 | give("@a","minecraft:diamond_sword", 1, 0,"{Unbreakable:1}")
5 | end
6 | end
7 |
8 | function hunger()
9 | if item == 1 then
10 | give("@a","minecraft:beef", 1, 0)
11 | end
12 | end
13 |
14 | function redstone()
15 | if item == 2 then
16 | give("@a","minecraft:redstone_block", 64, 0)
17 | end
18 | end
19 |
20 | war()
21 | hunger()
22 | redstone()
--------------------------------------------------------------------------------
/luablock/luablock/Command.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Text;
5 | using System.Threading.Tasks;
6 |
7 | namespace luablock
8 | {
9 | public class Command
10 | {
11 | public string Name { get; set; }
12 |
13 | public virtual string Parse(string Name, List args)
14 | {
15 | return "/say [ §4Error ] Identifier " + Name + " Does not exzist";
16 | }
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/luablock/Test/bin/Debug/test.lua:
--------------------------------------------------------------------------------
1 |
2 | function isten()
3 | if vara == 10 then
4 |
5 | print("@a", "§aVara is: ", vara)
6 |
7 | end
8 | end
9 |
10 | function main()
11 |
12 | vara = 10
13 |
14 | print("@a", "The vlaue is: ", vara)
15 |
16 | vara = vara + 50 + 15
17 |
18 | print("@a", "The vlaue is: ", vara)
19 |
20 | vara = vara - 70
21 |
22 | print("@a", "The vlaue is: ", vara)
23 |
24 | vara = vara * 2
25 |
26 | print("@a", "The vlaue is: ", vara)
27 |
28 | isten()
29 | end
30 |
31 | main()
32 |
--------------------------------------------------------------------------------
/luablock/Test/bin/Debug/Test.vshost.exe.manifest:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/luablock/Test/Program.cs:
--------------------------------------------------------------------------------
1 | using luablock;
2 | using System;
3 | using System.Collections.Generic;
4 | using System.IO;
5 | using System.Linq;
6 | using System.Text;
7 | using System.Threading.Tasks;
8 |
9 | namespace Test
10 | {
11 | class Program
12 | {
13 | static void Main(string[] args)
14 | {
15 | // Schematic s1 = new Schematic();
16 | // s1.Load("tt.schematic");
17 |
18 |
19 |
20 |
21 |
22 | Compiler c = new Compiler();
23 | c.Convert(File.ReadAllText(args[0]), args[1]);
24 | }
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/luablock/luablock/Commands/Say.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Text;
5 | using System.Threading.Tasks;
6 |
7 | namespace luablock.Commands
8 | {
9 | public class Say : Command
10 | {
11 | public Say()
12 | {
13 | Name = "say";
14 | }
15 |
16 | public override string Parse(string Name, List args)
17 | {
18 | string re = "/say";
19 |
20 | foreach(var i in args)
21 | {
22 | re += " " + i;
23 | }
24 |
25 | return re;
26 | }
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/luablock/luablock/CommandManager.cs:
--------------------------------------------------------------------------------
1 | using luablock.Commands;
2 | using System;
3 | using System.Collections.Generic;
4 | using System.Linq;
5 | using System.Text;
6 | using System.Threading.Tasks;
7 |
8 | namespace luablock
9 | {
10 | public static class CommandManager
11 | {
12 | public static List Cmds = new List()
13 | {
14 | new Say()
15 | };
16 |
17 | public static Command GetCommand(string name)
18 | {
19 | foreach(var i in Cmds)
20 | {
21 | if(i.Name == name)
22 | {
23 | return i;
24 | }
25 | }
26 |
27 | return new Command();
28 | }
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # LuaBlock
2 | A compiler that compiles Lua to Command blocks in the form of a schematic, for minecraft
3 |
4 | **Please Feel free to contribute**
5 |
6 | [](https://gitter.im/Myvar/LuaBlock?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge)
7 |
8 | #Usage
9 | 1. Download the latest archive, and unzip it.
10 | 2. Open command promt where you unzipped LuaBlock (Tip: Make sure you don't have anything selected, hold shift and right click in the folder. Now click "Open command prompt here")
11 | 3. You can now run the compiler with the following command "LBC [input lua file path] [output schematic file path]"
12 | 4. You can now import the schematic file with your favourite editor(mcedit, worldedit etc)
13 |
--------------------------------------------------------------------------------
/luablock/luablock/Commands/LuaCmd.cs:
--------------------------------------------------------------------------------
1 | using MoonSharp.Interpreter;
2 | using NetLua;
3 | using NetLua.Ast;
4 | using System;
5 | using System.Collections.Generic;
6 | using System.Linq;
7 | using System.Text;
8 | using System.Threading.Tasks;
9 |
10 | namespace luablock.Commands
11 | {
12 | public class LuaCmd : Command
13 | {
14 | public string Lua { get; set; }
15 |
16 | public override string Parse(string Name, List args)
17 | {
18 | Script script = new Script();
19 | UserData.RegisterType>();
20 |
21 | script.Globals.SetAsObject("args", args);
22 | script.Globals.SetAsObject("argsl", args.Count - 1);
23 | return script.DoString(Lua).String;
24 | }
25 |
26 | private string TypeToString(IExpression e)
27 | {
28 | //this feels so cheety(whatt ever you do dont show this too kenny! xD)
29 | dynamic z = e;
30 | try
31 | {
32 | try
33 | {
34 | return z.Value;
35 | }
36 | catch
37 | {
38 | return z.Name;
39 | }
40 | }
41 | catch
42 | {
43 | return "";
44 | }
45 | }
46 | }
47 | }
48 |
--------------------------------------------------------------------------------
/luablock/luablock.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio 14
4 | VisualStudioVersion = 14.0.23107.0
5 | MinimumVisualStudioVersion = 10.0.40219.1
6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "luablock", "luablock\luablock.csproj", "{B4FC2990-A026-47D6-930D-2724FF54C308}"
7 | EndProject
8 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Test", "Test\Test.csproj", "{99857049-CF9A-4791-90DD-8C14B0EF4BBE}"
9 | EndProject
10 | Global
11 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
12 | Debug|Any CPU = Debug|Any CPU
13 | Release|Any CPU = Release|Any CPU
14 | EndGlobalSection
15 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
16 | {B4FC2990-A026-47D6-930D-2724FF54C308}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
17 | {B4FC2990-A026-47D6-930D-2724FF54C308}.Debug|Any CPU.Build.0 = Debug|Any CPU
18 | {B4FC2990-A026-47D6-930D-2724FF54C308}.Release|Any CPU.ActiveCfg = Release|Any CPU
19 | {B4FC2990-A026-47D6-930D-2724FF54C308}.Release|Any CPU.Build.0 = Release|Any CPU
20 | {99857049-CF9A-4791-90DD-8C14B0EF4BBE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
21 | {99857049-CF9A-4791-90DD-8C14B0EF4BBE}.Debug|Any CPU.Build.0 = Debug|Any CPU
22 | {99857049-CF9A-4791-90DD-8C14B0EF4BBE}.Release|Any CPU.ActiveCfg = Release|Any CPU
23 | {99857049-CF9A-4791-90DD-8C14B0EF4BBE}.Release|Any CPU.Build.0 = Release|Any CPU
24 | EndGlobalSection
25 | GlobalSection(SolutionProperties) = preSolution
26 | HideSolutionNode = FALSE
27 | EndGlobalSection
28 | EndGlobal
29 |
--------------------------------------------------------------------------------
/luablock/Test/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 |
5 | // General Information about an assembly is controlled through the following
6 | // set of attributes. Change these attribute values to modify the information
7 | // associated with an assembly.
8 | [assembly: AssemblyTitle("Test")]
9 | [assembly: AssemblyDescription("")]
10 | [assembly: AssemblyConfiguration("")]
11 | [assembly: AssemblyCompany("")]
12 | [assembly: AssemblyProduct("Test")]
13 | [assembly: AssemblyCopyright("Copyright © 2015")]
14 | [assembly: AssemblyTrademark("")]
15 | [assembly: AssemblyCulture("")]
16 |
17 | // Setting ComVisible to false makes the types in this assembly not visible
18 | // to COM components. If you need to access a type in this assembly from
19 | // COM, set the ComVisible attribute to true on that type.
20 | [assembly: ComVisible(false)]
21 |
22 | // The following GUID is for the ID of the typelib if this project is exposed to COM
23 | [assembly: Guid("99857049-cf9a-4791-90dd-8c14b0ef4bbe")]
24 |
25 | // Version information for an assembly consists of the following four values:
26 | //
27 | // Major Version
28 | // Minor Version
29 | // Build Number
30 | // Revision
31 | //
32 | // You can specify all the values or you can default the Build and Revision Numbers
33 | // by using the '*' as shown below:
34 | // [assembly: AssemblyVersion("1.0.*")]
35 | [assembly: AssemblyVersion("1.0.0.0")]
36 | [assembly: AssemblyFileVersion("1.0.0.0")]
37 |
--------------------------------------------------------------------------------
/luablock/luablock/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 |
5 | // General Information about an assembly is controlled through the following
6 | // set of attributes. Change these attribute values to modify the information
7 | // associated with an assembly.
8 | [assembly: AssemblyTitle("luablock")]
9 | [assembly: AssemblyDescription("")]
10 | [assembly: AssemblyConfiguration("")]
11 | [assembly: AssemblyCompany("")]
12 | [assembly: AssemblyProduct("luablock")]
13 | [assembly: AssemblyCopyright("Copyright © 2015")]
14 | [assembly: AssemblyTrademark("")]
15 | [assembly: AssemblyCulture("")]
16 |
17 | // Setting ComVisible to false makes the types in this assembly not visible
18 | // to COM components. If you need to access a type in this assembly from
19 | // COM, set the ComVisible attribute to true on that type.
20 | [assembly: ComVisible(false)]
21 |
22 | // The following GUID is for the ID of the typelib if this project is exposed to COM
23 | [assembly: Guid("b4fc2990-a026-47d6-930d-2724ff54c308")]
24 |
25 | // Version information for an assembly consists of the following four values:
26 | //
27 | // Major Version
28 | // Minor Version
29 | // Build Number
30 | // Revision
31 | //
32 | // You can specify all the values or you can default the Build and Revision Numbers
33 | // by using the '*' as shown below:
34 | // [assembly: AssemblyVersion("1.0.*")]
35 | [assembly: AssemblyVersion("1.0.0.0")]
36 | [assembly: AssemblyFileVersion("1.0.0.0")]
37 |
--------------------------------------------------------------------------------
/luablock/packages/Irony.0.9.0/tools/Irony.GrammarExplorer.exe.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 | 0
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 | False
24 |
25 |
26 | False
27 |
28 |
29 | True
30 |
31 |
32 |
33 |
34 |
--------------------------------------------------------------------------------
/luablock/luablock/obj/Debug/luablock.csproj.FileListAbsolute.txt:
--------------------------------------------------------------------------------
1 | C:\Users\Myvar\Desktop\luablock\luablock\bin\Debug\luablock.dll
2 | C:\Users\Myvar\Desktop\luablock\luablock\bin\Debug\luablock.pdb
3 | C:\Users\Myvar\Desktop\luablock\luablock\obj\Debug\luablock.csprojResolveAssemblyReference.cache
4 | C:\Users\Myvar\Desktop\luablock\luablock\obj\Debug\luablock.dll
5 | C:\Users\Myvar\Desktop\luablock\luablock\obj\Debug\luablock.pdb
6 | C:\Users\Myvar\Desktop\luablock\luablock\bin\Debug\Irony.dll
7 | C:\Users\Myvar\Desktop\luablock\luablock\bin\Debug\Irony.pdb
8 | C:\Users\Myvar\Desktop\luablock\luablock\bin\Debug\Irony.xml
9 | C:\Users\Myvar\Desktop\luablock\luablock\bin\Debug\fNbt.dll
10 | C:\Users\Myvar\Desktop\luablock\luablock\bin\Debug\fNbt.pdb
11 | C:\Users\Myvar\Desktop\luablock\luablock\bin\Debug\MoonSharp.Interpreter.dll
12 | C:\Users\Myvar\Desktop\luablock\luablock\bin\Debug\MoonSharp.Interpreter.pdb
13 | C:\Users\Myvar\Desktop\luablock\luablock\bin\Debug\MoonSharp.Interpreter.xml
14 | C:\Users\Myvar\Documents\GitHub\LuaBlock\luablock\luablock\bin\Debug\luablock.dll
15 | C:\Users\Myvar\Documents\GitHub\LuaBlock\luablock\luablock\bin\Debug\luablock.pdb
16 | C:\Users\Myvar\Documents\GitHub\LuaBlock\luablock\luablock\bin\Debug\fNbt.dll
17 | C:\Users\Myvar\Documents\GitHub\LuaBlock\luablock\luablock\bin\Debug\Irony.dll
18 | C:\Users\Myvar\Documents\GitHub\LuaBlock\luablock\luablock\bin\Debug\MoonSharp.Interpreter.dll
19 | C:\Users\Myvar\Documents\GitHub\LuaBlock\luablock\luablock\bin\Debug\fNbt.pdb
20 | C:\Users\Myvar\Documents\GitHub\LuaBlock\luablock\luablock\bin\Debug\Irony.pdb
21 | C:\Users\Myvar\Documents\GitHub\LuaBlock\luablock\luablock\bin\Debug\Irony.xml
22 | C:\Users\Myvar\Documents\GitHub\LuaBlock\luablock\luablock\bin\Debug\MoonSharp.Interpreter.pdb
23 | C:\Users\Myvar\Documents\GitHub\LuaBlock\luablock\luablock\bin\Debug\MoonSharp.Interpreter.xml
24 | C:\Users\Myvar\Documents\GitHub\LuaBlock\luablock\luablock\obj\Debug\luablock.csprojResolveAssemblyReference.cache
25 | C:\Users\Myvar\Documents\GitHub\LuaBlock\luablock\luablock\obj\Debug\luablock.dll
26 | C:\Users\Myvar\Documents\GitHub\LuaBlock\luablock\luablock\obj\Debug\luablock.pdb
27 |
--------------------------------------------------------------------------------
/luablock/Test/obj/Debug/Test.csproj.FileListAbsolute.txt:
--------------------------------------------------------------------------------
1 | C:\Users\Myvar\Desktop\luablock\Test\bin\Debug\Test.exe.config
2 | C:\Users\Myvar\Desktop\luablock\Test\bin\Debug\Test.exe
3 | C:\Users\Myvar\Desktop\luablock\Test\bin\Debug\Test.pdb
4 | C:\Users\Myvar\Desktop\luablock\Test\bin\Debug\luablock.dll
5 | C:\Users\Myvar\Desktop\luablock\Test\bin\Debug\luablock.pdb
6 | C:\Users\Myvar\Desktop\luablock\Test\obj\Debug\Test.csprojResolveAssemblyReference.cache
7 | C:\Users\Myvar\Desktop\luablock\Test\obj\Debug\Test.exe
8 | C:\Users\Myvar\Desktop\luablock\Test\obj\Debug\Test.pdb
9 | C:\Users\Myvar\Desktop\luablock\Test\bin\Debug\Irony.dll
10 | C:\Users\Myvar\Desktop\luablock\Test\bin\Debug\Irony.pdb
11 | C:\Users\Myvar\Desktop\luablock\Test\bin\Debug\Irony.xml
12 | C:\Users\Myvar\Desktop\luablock\Test\bin\Debug\fNbt.dll
13 | C:\Users\Myvar\Desktop\luablock\Test\bin\Debug\fNbt.pdb
14 | C:\Users\Myvar\Desktop\luablock\Test\bin\Debug\MoonSharp.Interpreter.dll
15 | C:\Users\Myvar\Desktop\luablock\Test\bin\Debug\MoonSharp.Interpreter.pdb
16 | C:\Users\Myvar\Desktop\luablock\Test\bin\Debug\MoonSharp.Interpreter.xml
17 | C:\Users\Myvar\Documents\GitHub\LuaBlock\luablock\Test\bin\Debug\Test.exe.config
18 | C:\Users\Myvar\Documents\GitHub\LuaBlock\luablock\Test\obj\Debug\Test.exe
19 | C:\Users\Myvar\Documents\GitHub\LuaBlock\luablock\Test\obj\Debug\Test.pdb
20 | C:\Users\Myvar\Documents\GitHub\LuaBlock\luablock\Test\bin\Debug\Test.exe
21 | C:\Users\Myvar\Documents\GitHub\LuaBlock\luablock\Test\bin\Debug\Test.pdb
22 | C:\Users\Myvar\Documents\GitHub\LuaBlock\luablock\Test\bin\Debug\luablock.dll
23 | C:\Users\Myvar\Documents\GitHub\LuaBlock\luablock\Test\bin\Debug\Irony.dll
24 | C:\Users\Myvar\Documents\GitHub\LuaBlock\luablock\Test\bin\Debug\fNbt.dll
25 | C:\Users\Myvar\Documents\GitHub\LuaBlock\luablock\Test\bin\Debug\MoonSharp.Interpreter.dll
26 | C:\Users\Myvar\Documents\GitHub\LuaBlock\luablock\Test\bin\Debug\luablock.pdb
27 | C:\Users\Myvar\Documents\GitHub\LuaBlock\luablock\Test\bin\Debug\Irony.pdb
28 | C:\Users\Myvar\Documents\GitHub\LuaBlock\luablock\Test\bin\Debug\Irony.xml
29 | C:\Users\Myvar\Documents\GitHub\LuaBlock\luablock\Test\bin\Debug\fNbt.pdb
30 | C:\Users\Myvar\Documents\GitHub\LuaBlock\luablock\Test\bin\Debug\MoonSharp.Interpreter.pdb
31 | C:\Users\Myvar\Documents\GitHub\LuaBlock\luablock\Test\bin\Debug\MoonSharp.Interpreter.xml
32 | C:\Users\Myvar\Documents\GitHub\LuaBlock\luablock\Test\obj\Debug\Test.csprojResolveAssemblyReference.cache
33 |
--------------------------------------------------------------------------------
/luablock/Test/Test.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Debug
6 | AnyCPU
7 | {99857049-CF9A-4791-90DD-8C14B0EF4BBE}
8 | Exe
9 | Properties
10 | Test
11 | Test
12 | v4.5.2
13 | 512
14 | true
15 |
16 |
17 | AnyCPU
18 | true
19 | full
20 | false
21 | bin\Debug\
22 | DEBUG;TRACE
23 | prompt
24 | 4
25 |
26 |
27 | AnyCPU
28 | pdbonly
29 | true
30 | bin\Release\
31 | TRACE
32 | prompt
33 | 4
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 | {b4fc2990-a026-47d6-930d-2724ff54c308}
55 | luablock
56 |
57 |
58 |
59 |
66 |
--------------------------------------------------------------------------------
/luablock/luablock/NetLua/LuaContext.cs:
--------------------------------------------------------------------------------
1 | /*
2 | * See LICENSE file
3 | */
4 |
5 | using System;
6 | using System.Collections.Generic;
7 | using System.Linq;
8 | using System.Text;
9 |
10 | using System.Dynamic;
11 |
12 | namespace NetLua
13 | {
14 | ///
15 | /// Holds a scope and its variables
16 | ///
17 | public class LuaContext : DynamicObject
18 | {
19 | LuaContext parent;
20 | Dictionary variables;
21 | LuaArguments varargs;
22 |
23 | ///
24 | /// Used to create scopes
25 | ///
26 | public LuaContext(LuaContext Parent)
27 | {
28 | parent = Parent;
29 | variables = new Dictionary();
30 | varargs = new LuaArguments(new LuaObject[] { });
31 | }
32 |
33 | ///
34 | /// Creates a base context
35 | ///
36 | public LuaContext() : this(null) { }
37 |
38 | ///
39 | /// Sets or creates a variable in the local scope
40 | ///
41 | public void SetLocal(string Name, LuaObject Value)
42 | {
43 | variables[Name] = Value;
44 | }
45 |
46 | ///
47 | /// Sets or creates a variable in the global scope
48 | ///
49 | public void SetGlobal(string Name, LuaObject Value)
50 | {
51 | if (parent == null)
52 | variables[Name] = Value;
53 | else
54 | parent.SetGlobal(Name, Value);
55 | }
56 |
57 | ///
58 | /// Returns the nearest declared variable value or nil
59 | ///
60 | public LuaObject Get(string Name)
61 | {
62 | var obj = LuaObject.Nil;
63 | if (variables.TryGetValue(Name, out obj) || parent == null)
64 | {
65 | if (obj == null)
66 | return LuaObject.Nil;
67 | else
68 | return obj;
69 | }
70 | else
71 | {
72 | return parent.Get(Name);
73 | }
74 | }
75 |
76 | ///
77 | /// Sets the nearest declared variable or creates a new one
78 | ///
79 | public void Set(string Name, LuaObject Value)
80 | {
81 | var obj = LuaObject.Nil;
82 | if (parent == null || variables.TryGetValue(Name, out obj))
83 | variables[Name] = Value;
84 | else
85 | parent.Set(Name, Value);
86 | }
87 |
88 | internal LuaArguments Varargs
89 | {
90 | get
91 | {
92 | return varargs;
93 | }
94 |
95 | set
96 | {
97 | varargs = value;
98 | }
99 | }
100 |
101 | #region DynamicObject
102 | public override bool TryGetMember(GetMemberBinder binder, out object result)
103 | {
104 | result = Get(binder.Name);
105 | if (result == LuaObject.Nil)
106 | return false;
107 | return true;
108 | }
109 |
110 | public override bool TrySetMember(SetMemberBinder binder, object value)
111 | {
112 | Set(binder.Name, LuaObject.FromObject(value));
113 | return true;
114 | }
115 | #endregion
116 | }
117 | }
118 |
--------------------------------------------------------------------------------
/luablock/luablock/luablock.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Debug
6 | AnyCPU
7 | {B4FC2990-A026-47D6-930D-2724FF54C308}
8 | Library
9 | Properties
10 | luablock
11 | luablock
12 | v4.5.2
13 | 512
14 |
15 |
16 | true
17 | full
18 | false
19 | bin\Debug\
20 | DEBUG;TRACE
21 | prompt
22 | 4
23 |
24 |
25 | pdbonly
26 | true
27 | bin\Release\
28 | TRACE
29 | prompt
30 | 4
31 |
32 |
33 |
34 | ..\packages\fNbt.0.6.3\lib\net35-client\fNbt.dll
35 | True
36 |
37 |
38 | ..\packages\Irony.0.9.0\lib\net45\Irony.dll
39 | True
40 |
41 |
42 | ..\packages\MoonSharp.0.9.8.0\lib\net40-client\MoonSharp.Interpreter.dll
43 | True
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 |
76 |
77 |
78 |
85 |
--------------------------------------------------------------------------------
/luablock/luablock/NetLua/Libraries/MathLibrary.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Text;
5 |
6 | namespace NetLua
7 | {
8 | public static class MathLibrary
9 | {
10 | static LuaArguments math_abs(LuaArguments args)
11 | {
12 | return Lua.Return(Math.Abs(args[0]));
13 | }
14 |
15 | static LuaArguments math_acos(LuaArguments args)
16 | {
17 | return Lua.Return(Math.Acos(args[0]));
18 | }
19 |
20 | static LuaArguments math_asin(LuaArguments args)
21 | {
22 | return Lua.Return(Math.Asin(args[0]));
23 | }
24 |
25 | static LuaArguments math_atan(LuaArguments args)
26 | {
27 | return Lua.Return(Math.Atan(args[0]));
28 | }
29 |
30 | static LuaArguments math_atan2(LuaArguments args)
31 | {
32 | return Lua.Return(Math.Atan2(args[0], args[1]));
33 | }
34 |
35 | static LuaArguments math_ceil(LuaArguments args)
36 | {
37 | return Lua.Return(Math.Ceiling(args[0]));
38 | }
39 |
40 | static LuaArguments math_cos(LuaArguments args)
41 | {
42 | return Lua.Return(Math.Cos(args[0]));
43 | }
44 |
45 | static LuaArguments math_cosh(LuaArguments args)
46 | {
47 | return Lua.Return(Math.Cosh(args[0]));
48 | }
49 |
50 | static LuaArguments math_exp(LuaArguments args)
51 | {
52 | return Lua.Return(Math.Exp(args[0]));
53 | }
54 |
55 | static LuaArguments math_floor(LuaArguments args)
56 | {
57 | return Lua.Return(Math.Floor(args[0]));
58 | }
59 |
60 | static LuaArguments math_log(LuaArguments args)
61 | {
62 | return Lua.Return(Math.Log(args[0], args[1] | Math.E));
63 | }
64 |
65 | static LuaArguments math_max(LuaArguments args)
66 | {
67 | var max = args[0];
68 | foreach (LuaObject o in args)
69 | {
70 | max = Math.Max(max, o);
71 | }
72 | return Lua.Return(max);
73 | }
74 |
75 | static LuaArguments math_min(LuaArguments args)
76 | {
77 | var min = args[0];
78 | foreach (LuaObject o in args)
79 | {
80 | min = Math.Min(min, o);
81 | }
82 | return Lua.Return(min);
83 | }
84 |
85 | static LuaArguments math_pow(LuaArguments args)
86 | {
87 | return Lua.Return(Math.Pow(args[0], args[1]));
88 | }
89 |
90 | static LuaArguments math_sin(LuaArguments args)
91 | {
92 | return Lua.Return(Math.Sin(args[0]));
93 | }
94 |
95 | static LuaArguments math_sinh(LuaArguments args)
96 | {
97 | return Lua.Return(Math.Sinh(args[0]));
98 | }
99 |
100 | static LuaArguments math_sqrt(LuaArguments args)
101 | {
102 | return Lua.Return(Math.Sqrt(args[0]));
103 | }
104 |
105 | static LuaArguments math_tan(LuaArguments args)
106 | {
107 | return Lua.Return(Math.Tan(args[0]));
108 | }
109 |
110 | static LuaArguments math_tanh(LuaArguments args)
111 | {
112 | return Lua.Return(Math.Tanh(args[0]));
113 | }
114 |
115 | public static void AddMathLibrary(LuaContext Context)
116 | {
117 | dynamic math = LuaObject.NewTable();
118 | math.abs = (LuaFunction)math_abs;
119 | math.acos = (LuaFunction)math_acos;
120 | math.asin = (LuaFunction)math_asin;
121 | math.atan = (LuaFunction)math_atan;
122 | math.atan2 = (LuaFunction)math_atan2;
123 | math.ceil = (LuaFunction)math_ceil;
124 | math.cos = (LuaFunction)math_cos;
125 | math.cosh = (LuaFunction)math_cosh;
126 | math.exp = (LuaFunction)math_exp;
127 | math.floor = (LuaFunction)math_floor;
128 | math.log = (LuaFunction)math_log;
129 | math.max = (LuaFunction)math_max;
130 | math.min = (LuaFunction)math_min;
131 | math.pow = (LuaFunction)math_pow;
132 | math.sin = (LuaFunction)math_sin;
133 | math.sinh = (LuaFunction)math_sinh;
134 | math.sqrt = (LuaFunction)math_sqrt;
135 | math.tan = (LuaFunction)math_tan;
136 | math.tanh = (LuaFunction)math_tanh;
137 |
138 | math.pi = Math.PI;
139 |
140 | Context.Set("math", math);
141 | }
142 | }
143 | }
144 |
--------------------------------------------------------------------------------
/luablock/luablock/Schematic.cs:
--------------------------------------------------------------------------------
1 | using fNbt;
2 | using System;
3 | using System.Collections.Generic;
4 | using System.Linq;
5 | using System.Text;
6 | using System.Threading.Tasks;
7 |
8 | namespace luablock
9 | {
10 | public class Schematic
11 | {
12 |
13 |
14 | public short Width { get; set; }
15 | public short Height { get; set; }
16 | public short Length { get; set; }
17 |
18 | private byte[] _data { get; set; }
19 | private byte[] _biomes { get; set; }
20 | private byte[] _blocks { get; set; }
21 |
22 | public List Blocks { get; set; } = new List();
23 |
24 |
25 | public void Fill()
26 | {
27 | var size = Width * Height * Length;
28 |
29 | for (int i = 0; i < size; i++)
30 | {
31 | Blocks.Add(new Block());
32 | }
33 |
34 | }
35 |
36 |
37 | public void SetBlock(int x, int y, int z, Block b)
38 | {
39 | //(y * Length + z)* Width + x
40 | Blocks[(y * Length + z) * Width + x] = b;
41 | }
42 |
43 | public Block GetBlock(int x, int y, int z)
44 | {
45 | //(y * Length + z)* Width + x
46 | return Blocks[(y * Length + z) * Width + x];
47 | }
48 |
49 | public void Load(string p)
50 | {
51 | var myFile = new NbtFile();
52 | myFile.LoadFromFile(p);
53 | var root = myFile.RootTag;
54 |
55 | Width = root.Get("Width").ShortValue;
56 | Height = root.Get("Height").ShortValue;
57 | Length = root.Get("Length").ShortValue;
58 |
59 | _data = root.Get("Data").ByteArrayValue;
60 | _biomes = root.Get("Biomes").ByteArrayValue;
61 | _blocks = root.Get("Blocks").ByteArrayValue;
62 |
63 | Fill();
64 |
65 |
66 |
67 | for (int y = 0; y < Height; y++)
68 | {
69 | for (int z = 0; z < Length; z++)
70 | {
71 | for (int x = 0; x < Width; x++)
72 | {
73 | SetBlock(x, y, z, new Block() { ID = _blocks[(y * Length + z) * Width + x], Metta = _data[(y * Length + z) * Width + x] & 0x0F });
74 | }
75 | }
76 | }
77 |
78 |
79 | }
80 |
81 | public void Save(string p)
82 | {
83 | var myFile = new NbtFile();
84 |
85 | var root = myFile.RootTag;
86 | root.Name = "Schematic";
87 |
88 | root.Add(new NbtShort("Height", Height));
89 | root.Add(new NbtShort("Length", Length));
90 | root.Add(new NbtShort("Width", Width));
91 |
92 |
93 | var nbttile = new NbtList("TileEntities", NbtTagType.Compound);
94 | var size = Width * Height * Length;
95 | _data = new byte[size];
96 | _blocks = new byte[size];
97 |
98 | for (int y = 0; y < Height; y++)
99 | {
100 | for (int z = 0; z < Length; z++)
101 | {
102 | for (int x = 0; x < Width; x++)
103 | {
104 | var d = GetBlock(x, y, z);
105 | _blocks[(y * Length + z) * Width + x] = (byte)d.ID;
106 | _data[(y * Length + z) * Width + x] = (byte)(d.Metta & 0x0F);
107 |
108 | if (d.Command != null)
109 | {
110 | var nbtc = new NbtCompound();
111 | nbtc.Add(new NbtString("CustomName", "LuaBlock"));
112 | nbtc.Add(new NbtString("Command", d.Command));
113 | nbtc.Add(new NbtString("id", "Control"));
114 | nbtc.Add(new NbtInt("x", x));
115 | nbtc.Add(new NbtInt("y", y));
116 | nbtc.Add(new NbtInt("z", z));
117 |
118 | nbttile.Add(nbtc);
119 | }
120 | }
121 | }
122 | }
123 | root.Add(new NbtList("Entities", NbtTagType.Byte));
124 | root.Add(nbttile);
125 | root.Add(new NbtList("TileTicks", NbtTagType.Byte));
126 |
127 | root.Add(new NbtString("Materials", "Alpha"));
128 |
129 |
130 |
131 | root.Add(new NbtByteArray("Data", _data));
132 | // root.Add(new NbtByteArray("Biomes", new byte[Width * Length]));
133 | root.Add(new NbtByteArray("Blocks", _blocks));
134 |
135 |
136 | myFile.SaveToFile(p, NbtCompression.None);
137 | }
138 | }
139 |
140 | public class Block
141 | {
142 | public int ID { get; set; } = 0;
143 | public int Metta { get; set; } = 0;
144 | public string Command { get; set; } = null;
145 | }
146 | }
147 |
--------------------------------------------------------------------------------
/luablock/luablock/NetLua/Ast/Ast.cs:
--------------------------------------------------------------------------------
1 | /*
2 | * The MIT License (MIT)
3 | *
4 | * Copyright (c) 2013 Francesco Bertolaccini
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy of
7 | * this software and associated documentation files (the "Software"), to deal in
8 | * the Software without restriction, including without limitation the rights to
9 | * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
10 | * the Software, and to permit persons to whom the Software is furnished to do so,
11 | * subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in all
14 | * copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
18 | * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
19 | * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
20 | * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 | */
23 |
24 | using System;
25 | using System.Collections.Generic;
26 | using System.Linq;
27 | using System.Text;
28 |
29 | namespace NetLua.Ast
30 | {
31 | public enum BinaryOp
32 | {
33 | Addition,
34 | Subtraction,
35 | Multiplication,
36 | Division,
37 | Power,
38 | Modulo,
39 | Concat,
40 | GreaterThan,
41 | GreaterOrEqual,
42 | LessThan,
43 | LessOrEqual,
44 | Equal,
45 | Different,
46 | And,
47 | Or
48 | }
49 |
50 | public enum UnaryOp
51 | {
52 | Negate,
53 | Invert,
54 | Length
55 | }
56 |
57 | public interface IStatement { }
58 |
59 | public interface IExpression { }
60 |
61 | public interface IAssignable : IExpression
62 | { }
63 |
64 | public class Variable : IExpression, IAssignable
65 | {
66 | // Prefix.Name
67 | public IExpression Prefix;
68 | public string Name;
69 | }
70 |
71 | public class Argument
72 | {
73 | public string Name;
74 | }
75 |
76 | public class StringLiteral : IExpression
77 | {
78 | public string Value;
79 | }
80 |
81 | public class NumberLiteral : IExpression
82 | {
83 | public double Value;
84 | }
85 |
86 | public class NilLiteral : IExpression
87 | { }
88 |
89 | public class BoolLiteral : IExpression
90 | {
91 | public bool Value;
92 | }
93 |
94 | public class VarargsLiteral : IExpression
95 | { }
96 |
97 | public class FunctionCall : IStatement, IExpression
98 | {
99 | public IExpression Function;
100 | public List Arguments = new List();
101 | }
102 |
103 | public class TableAccess : IExpression, IAssignable
104 | {
105 | // Expression[Index]
106 | public IExpression Expression;
107 | public IExpression Index;
108 | }
109 |
110 | public class FunctionDefinition : IExpression
111 | {
112 | // function(Arguments) Body end
113 | public List Arguments = new List();
114 | public Block Body;
115 | }
116 |
117 | public class BinaryExpression : IExpression
118 | {
119 | public IExpression Left, Right;
120 | public BinaryOp Operation;
121 | }
122 |
123 | public class UnaryExpression : IExpression
124 | {
125 | public IExpression Expression;
126 | public UnaryOp Operation;
127 | }
128 |
129 | public class TableConstructor : IExpression
130 | {
131 | public Dictionary Values = new Dictionary();
132 | }
133 |
134 | public class Assignment : IStatement
135 | {
136 | // Var1, Var2, Var3 = Exp1, Exp2, Exp3
137 | //public Variable[] Variables;
138 | //public IExpression[] Expressions;
139 |
140 | public List Variables = new List();
141 | public List Expressions = new List();
142 | }
143 |
144 | public class ReturnStat : IStatement
145 | {
146 | public List Expressions = new List();
147 | }
148 |
149 | public class BreakStat : IStatement { }
150 |
151 | public class LocalAssignment : IStatement
152 | {
153 | public List Names = new List();
154 | public List Values = new List();
155 | }
156 |
157 | public class Block : IStatement
158 | {
159 | public List Statements = new List();
160 | }
161 |
162 | public class WhileStat : IStatement
163 | {
164 | public IExpression Condition;
165 | public Block Block;
166 | }
167 |
168 | public class RepeatStat : IStatement
169 | {
170 | public Block Block;
171 | public IExpression Condition;
172 | }
173 |
174 | public class NumericFor : IStatement
175 | {
176 | public IExpression Var, Limit, Step;
177 | public string Variable;
178 | public Block Block;
179 | }
180 |
181 | public class GenericFor : IStatement
182 | {
183 | public List Variables = new List();
184 | public List Expressions = new List();
185 | public Block Block;
186 | }
187 |
188 | public class IfStat : IStatement
189 | {
190 | public IExpression Condition;
191 | public Block Block;
192 | public List ElseIfs = new List();
193 | public Block ElseBlock;
194 | }
195 | }
196 |
--------------------------------------------------------------------------------
/luablock/luablock/NetLua/LuaGrammar.cs:
--------------------------------------------------------------------------------
1 | /*
2 | * See LICENSE file
3 | */
4 |
5 | using System;
6 | using System.Collections.Generic;
7 | using System.Linq;
8 | using System.Text;
9 |
10 | using Irony.Parsing;
11 | using Irony.Ast;
12 |
13 | namespace NetLua
14 | {
15 | class LuaGrammar : Grammar
16 | {
17 | public LuaGrammar()
18 | : base(true)
19 | {
20 | #region Terminals
21 | Terminal Identifier = new IdentifierTerminal("identifier");
22 | Terminal SingleString = new StringLiteral("string", "'", StringOptions.AllowsAllEscapes);
23 | Terminal DoubleString = new StringLiteral("string", "\"", StringOptions.AllowsAllEscapes);
24 | Terminal Number = new NumberLiteral("number", NumberOptions.AllowSign);
25 |
26 | Terminal LineComment = new CommentTerminal("Comment", "--", "\n", "\r");
27 | Terminal LongComment = new CommentTerminal("LongComment", "--[[", "]]");
28 |
29 | base.NonGrammarTerminals.Add(LineComment);
30 | base.NonGrammarTerminals.Add(LongComment);
31 | #endregion
32 |
33 | #region Nonterminals
34 | NonTerminal Chunk = new NonTerminal("Chunk");
35 |
36 | NonTerminal Prefix = new NonTerminal("Prefix");
37 | NonTerminal Variable = new NonTerminal("Variable");
38 | NonTerminal FunctionCall = new NonTerminal("FunctionCall");
39 | NonTerminal CallArguments = new NonTerminal("FunctionCallArguments");
40 | NonTerminal OopCall = new NonTerminal("OopCall");
41 | NonTerminal CallArgumentsFragment = new NonTerminal("");
42 | NonTerminal Expression = new NonTerminal("Expression");
43 | NonTerminal FunctionDef = new NonTerminal("FunctionDef");
44 | NonTerminal DefArguments = new NonTerminal("FunctionDefArguments");
45 | NonTerminal DefArgumentsFragment = new NonTerminal("");
46 |
47 | NonTerminal Statement = new NonTerminal("Statement");
48 | NonTerminal ReturnStatement = new NonTerminal("ReturnStat");
49 | NonTerminal BreakStatement = new NonTerminal("BreakStat");
50 | NonTerminal Assignment = new NonTerminal("Assignment");
51 | NonTerminal LocalAssignment = new NonTerminal("LocalAssignment");
52 | NonTerminal FunctionDecl = new NonTerminal("FunctionDecl");
53 | NonTerminal DoBlock = new NonTerminal("DoBlock");
54 | NonTerminal If = new NonTerminal("If");
55 | NonTerminal Elseif = new NonTerminal("Elseif");
56 | NonTerminal Else = new NonTerminal("Else");
57 | NonTerminal While = new NonTerminal("While");
58 | NonTerminal Repeat = new NonTerminal("Repeat");
59 | NonTerminal For = new NonTerminal("For");
60 |
61 | NonTerminal PowerOp = new NonTerminal("PowerOp");
62 | NonTerminal MulOp = new NonTerminal("MulOp");
63 | NonTerminal AddOp = new NonTerminal("AddOp");
64 | NonTerminal ConcatOp = new NonTerminal("ConcatOp");
65 | NonTerminal RelOp = new NonTerminal("RelOp");
66 | NonTerminal AndOp = new NonTerminal("AndOp");
67 | NonTerminal OrOp = new NonTerminal("OrOp");
68 | NonTerminal UnaryExpr = new NonTerminal("UnaryExpr");
69 |
70 | NonTerminal TableConstruct = new NonTerminal("TableConstruct");
71 | NonTerminal TableConstructFragment = new NonTerminal("TableConstructFragment");
72 | #endregion
73 |
74 | #region Fragments
75 | CallArgumentsFragment.Rule = Expression | Expression + "," + CallArgumentsFragment;
76 |
77 | CallArguments.Rule = "(" + (CallArgumentsFragment | Empty) + ")";
78 |
79 | DefArgumentsFragment.Rule = Identifier | Identifier + "," + DefArgumentsFragment;
80 |
81 | DefArguments.Rule = "(" + (DefArgumentsFragment | Empty) + ")";
82 |
83 | Chunk.Rule = MakeStarRule(Chunk, Statement);
84 | #endregion
85 |
86 | #region Expressions
87 | PowerOp.Rule = Expression + ("^" + Expression | Empty);
88 | MulOp.Rule = PowerOp + ((ToTerm("*") | "/" | "%") + PowerOp | Empty);
89 | AddOp.Rule = MulOp + ((ToTerm("+") | "-") + MulOp | Empty);
90 | ConcatOp.Rule = AddOp + (".." + AddOp | Empty);
91 | RelOp.Rule = ConcatOp + ((ToTerm(">") | ">=" | "<" | "<=" | "==" | "~=") + ConcatOp | Empty);
92 | AndOp.Rule = RelOp + ("and" + RelOp | Empty);
93 | OrOp.Rule = AndOp + ("or" + AndOp | Empty);
94 |
95 | UnaryExpr.Rule = (ToTerm("not") | "-" | "#") + Expression;
96 |
97 | Prefix.Rule =
98 | OopCall
99 | | FunctionCall
100 | | Variable
101 | | "(" + Expression + ")";
102 |
103 | Variable.Rule =
104 | Prefix + "." + Identifier
105 | | Prefix + "[" + Expression + "]"
106 | | Identifier;
107 |
108 | FunctionCall.Rule = Prefix + CallArguments;
109 | OopCall.Rule = Prefix + ":" + Identifier + CallArguments;
110 |
111 | FunctionDef.Rule =
112 | ToTerm("function") + DefArguments
113 | + Chunk + ToTerm("end");
114 |
115 | var tableSep = new NonTerminal("");
116 | tableSep.Rule = ToTerm(";") | ",";
117 | TableConstructFragment.Rule =
118 | (
119 | (
120 | (
121 | (Identifier | "[" + Expression + "]") + "="
122 | )
123 | + Expression
124 | | Expression
125 | )
126 | + (";" + TableConstructFragment | "," + TableConstructFragment | Empty)
127 | ) | Empty;
128 | TableConstruct.Rule = "{" + TableConstructFragment + "}";
129 |
130 | var varargs = new NonTerminal("Varargs");
131 | varargs.Rule = "...";
132 |
133 | Expression.Rule =
134 | varargs
135 | | Prefix
136 | | OrOp
137 | | UnaryExpr
138 | | ToTerm("true")
139 | | "false"
140 | | "nil"
141 | | SingleString
142 | | DoubleString
143 | | Number
144 | | FunctionDef
145 | | TableConstruct;
146 | #endregion
147 |
148 | #region Statements
149 | FunctionDecl.Rule = "function" + Variable + (":" + Identifier | Empty) + DefArguments + Chunk + "end";
150 |
151 |
152 | var RetChunk = new NonTerminal("RetChunk");
153 | RetChunk.Rule = Expression + (("," + RetChunk) | Empty);
154 |
155 | ReturnStatement.Rule =
156 | "return" + (RetChunk | Empty);
157 |
158 | var AssignExpChunk = new NonTerminal("AssignExpChunk");
159 | AssignExpChunk.Rule = Expression + (("," + AssignExpChunk) | Empty);
160 | var AssignVarChunk = new NonTerminal("AssignVarChunk");
161 | AssignVarChunk.Rule = Variable + (("," + AssignVarChunk) | Empty);
162 |
163 | Assignment.Rule =
164 | AssignVarChunk + "=" + AssignExpChunk;
165 |
166 | var LocalAssignNameChunk = new NonTerminal("AssignNameChunk");
167 | var LocalFunction = new NonTerminal("LocalFunction");
168 | LocalAssignNameChunk.Rule = Identifier + (("," + LocalAssignNameChunk) | Empty);
169 | LocalFunction.Rule = "function" + Identifier + DefArguments + Chunk + "end";
170 | LocalAssignment.Rule = "local" + (LocalAssignNameChunk + ("=" + AssignExpChunk | Empty) | LocalFunction);
171 |
172 | Elseif.Rule = "elseif" + Expression + "then" + Chunk + (Elseif | Empty);
173 | Else.Rule = "else" + Chunk;
174 |
175 | If.Rule = "if" + Expression + "then" + Chunk + (Elseif | Empty) + (Else | Empty) + "end";
176 |
177 | While.Rule = "while" + Expression + DoBlock;
178 |
179 | DoBlock.Rule = "do" + Chunk + "end";
180 |
181 | Repeat.Rule = "repeat" + Chunk + "until" + Expression;
182 |
183 | BreakStatement.Rule = "break";
184 |
185 | var NumericFor = new NonTerminal("NumericFor");
186 | NumericFor.Rule = Identifier + "=" + Expression + "," + Expression + ("," + Expression | Empty);
187 | var GenericFor = new NonTerminal("GenericFor");
188 | var NameList = new NonTerminal("NameList");
189 | var ExpList = new NonTerminal("ExpList");
190 | NameList.Rule = Identifier + (("," + NameList) | Empty);
191 | ExpList.Rule = Expression + (("," + ExpList) | Empty);
192 | GenericFor.Rule = NameList + "in" + ExpList;
193 |
194 | For.Rule = "for" + (GenericFor | NumericFor) + DoBlock;
195 |
196 | Statement.Rule =
197 | ";"
198 | | ReturnStatement
199 | | BreakStatement
200 | | Assignment
201 | | LocalAssignment
202 | | FunctionCall
203 | | OopCall
204 | | FunctionDecl
205 | | For
206 | | If
207 | | While
208 | | DoBlock
209 | | Repeat;
210 | #endregion
211 |
212 | this.Root = Chunk;
213 | this.MarkReservedWords(
214 | "true", "false",
215 | "nil", "local",
216 | "function", "while",
217 | "if", "for", "repeat", "until",
218 | "end", "do", "return", "break");
219 |
220 | this.MarkPunctuation(".", ",", ";", "(", ")", "[", "]", "{", "}", "=", ":");
221 | this.MarkTransient(Statement);
222 | }
223 | }
224 | }
225 |
--------------------------------------------------------------------------------
/luablock/luablock/NetLua/Lua.cs:
--------------------------------------------------------------------------------
1 | /*
2 | * See LICENSE file
3 | */
4 |
5 | using System;
6 | using System.Collections.Generic;
7 | using System.Linq.Expressions;
8 | using System.Text;
9 | using System.Dynamic;
10 |
11 | using NetLua.Ast;
12 |
13 | namespace NetLua
14 | {
15 | public class Lua : DynamicObject
16 | {
17 | LuaContext ctx = new LuaContext();
18 | Parser p = new Parser();
19 |
20 | ///
21 | /// Helper function for returning Nil from a function
22 | ///
23 | /// Nil
24 | public static LuaArguments Return()
25 | {
26 | return new LuaObject[] { LuaObject.Nil };
27 | }
28 |
29 | ///
30 | /// Helper function for returning objects from a function
31 | ///
32 | /// The objects to return
33 | public static LuaArguments Return(params LuaObject[] values)
34 | {
35 | return values;
36 | }
37 |
38 | ///
39 | /// Creates a new Lua context with the base functions already set
40 | ///
41 | public Lua()
42 | {
43 | ctx.Set("assert", (LuaFunction)assert);
44 | ctx.Set("dofile", (LuaFunction)dofile);
45 | ctx.Set("error", (LuaFunction)error);
46 | ctx.Set("getmetatable", (LuaFunction)getmetatable);
47 | ctx.Set("setmetatable", (LuaFunction)setmetatable);
48 | ctx.Set("rawequal", (LuaFunction)rawequal);
49 | ctx.Set("rawget", (LuaFunction)rawget);
50 | ctx.Set("rawset", (LuaFunction)rawset);
51 | ctx.Set("rawlen", (LuaFunction)rawlen);
52 | ctx.Set("tonumber", (LuaFunction)tonumber);
53 | ctx.Set("tostring", (LuaFunction)tostring);
54 | ctx.Set("type", (LuaFunction)type);
55 | ctx.Set("ipairs", (LuaFunction)ipairs);
56 | ctx.Set("next", (LuaFunction)next);
57 | ctx.Set("pairs", (LuaFunction)pairs);
58 | }
59 |
60 | ///
61 | /// Parses and executes the specified file
62 | ///
63 | /// The file to execute
64 | public LuaArguments DoFile(string Filename)
65 | {
66 | FunctionDefinition def = new FunctionDefinition();
67 | def.Arguments = new List();
68 | def.Body = p.ParseFile(Filename);
69 | var function = LuaCompiler.CompileFunction(def, Expression.Constant(ctx)).Compile();
70 | return function().Call(Lua.Return());
71 | }
72 |
73 | ///
74 | /// Parses and executes the specified string
75 | ///
76 | public LuaArguments DoString(string Chunk)
77 | {
78 | FunctionDefinition def = new FunctionDefinition();
79 | def.Arguments = new List();
80 | def.Body = p.ParseString(Chunk);
81 | var function = LuaCompiler.CompileFunction(def, Expression.Constant(ctx)).Compile();
82 | return function().Call(Lua.Return());
83 | }
84 |
85 | ///
86 | /// The base context
87 | ///
88 | public LuaContext Context
89 | {
90 | get
91 | {
92 | return ctx;
93 | }
94 | }
95 |
96 | ///
97 | /// The base context
98 | ///
99 | public dynamic DynamicContext
100 | {
101 | get
102 | {
103 | return ctx;
104 | }
105 | }
106 |
107 | #region Dynamic methods
108 |
109 | public override bool TryGetMember(GetMemberBinder binder, out object result)
110 | {
111 | result = null;
112 | LuaObject obj = ctx.Get(binder.Name);
113 | if (obj.IsNil)
114 | return false;
115 | else
116 | {
117 | result = LuaObject.getObject(obj);
118 | return true;
119 | }
120 | }
121 |
122 | public override bool TrySetMember(SetMemberBinder binder, object value)
123 | {
124 | if (value is LuaObject)
125 | ctx.Set(binder.Name, value as LuaObject);
126 | else
127 | ctx.Set(binder.Name, LuaObject.FromObject(value));
128 | return true;
129 | }
130 |
131 | #endregion
132 |
133 | #region Basic functions
134 |
135 | LuaArguments assert(LuaArguments args)
136 | {
137 | if (args.Length > 0)
138 | {
139 | if (args[0].AsBool() == false)
140 | {
141 | if (args.Length == 1)
142 | throw new LuaException("Assertion failed");
143 | else
144 | throw new LuaException(args[1].ToString());
145 | }
146 | }
147 | return Return();
148 | }
149 |
150 | LuaArguments dofile(LuaArguments args)
151 | {
152 | return DoFile(args[0].ToString());
153 | }
154 |
155 | LuaArguments error(LuaArguments args)
156 | {
157 | throw new LuaException(args[0].ToString());
158 | }
159 |
160 | LuaArguments getmetatable(LuaArguments args)
161 | {
162 | return Return(args[0].Metatable);
163 | }
164 |
165 | LuaArguments setmetatable(LuaArguments args)
166 | {
167 | args[0].Metatable = args[1];
168 | return Return();
169 | }
170 |
171 | LuaArguments rawequal(LuaArguments args)
172 | {
173 | return Return(args[0] == args[1]);
174 | }
175 |
176 | LuaArguments rawget(LuaArguments args)
177 | {
178 | return Return(LuaEvents.rawget(args[0], args[1]));
179 | }
180 |
181 | LuaArguments rawset(LuaArguments args)
182 | {
183 | LuaEvents.rawset(args[0], args[1], args[2]);
184 | return Return(args[0]);
185 | }
186 |
187 | LuaArguments rawlen(LuaArguments args)
188 | {
189 | LuaObject obj = args[0];
190 | if (obj.IsString)
191 | return Return(obj.AsString().Length);
192 | else if (obj.IsTable)
193 | return Return(obj.AsTable().Count);
194 | else
195 | throw new LuaException("invalid argument");
196 | }
197 |
198 | LuaArguments tonumber(LuaArguments args)
199 | {
200 | LuaObject obj = args[0];
201 | if (args.Length == 1)
202 | {
203 | double d = 0;
204 | if (obj.IsString)
205 | {
206 | if (double.TryParse(obj.AsString(), out d))
207 | return Return(d);
208 | else
209 | return Return();
210 | }
211 | else if (obj.IsNumber)
212 | {
213 | return Return(obj.AsNumber());
214 | }
215 | else
216 | {
217 | return Return();
218 | }
219 | }
220 | else
221 | {
222 | //TODO: Implement tonumber for bases different from 10
223 | throw new NotImplementedException();
224 | }
225 | }
226 |
227 | LuaArguments tostring(LuaArguments args)
228 | {
229 | return Return(LuaEvents.tostring_event(args[0]));
230 | }
231 |
232 | LuaArguments type(LuaArguments args)
233 | {
234 | switch (args[0].Type)
235 | {
236 | case LuaType.boolean:
237 | return Return("boolean");
238 | case LuaType.function:
239 | return Return("function");
240 | case LuaType.nil:
241 | return Return("nil");
242 | case LuaType.number:
243 | return Return("number");
244 | case LuaType.@string:
245 | return Return("string");
246 | case LuaType.table:
247 | return Return("table");
248 | case LuaType.thread:
249 | return Return("thread");
250 | case LuaType.userdata:
251 | return Return("userdata");
252 | }
253 | return Return();
254 | }
255 |
256 | LuaArguments ipairs(LuaArguments args)
257 | {
258 | LuaObject handler = LuaEvents.getMetamethod(args[0], "__ipairs");
259 | if (!handler.IsNil)
260 | {
261 | return handler.Call(args);
262 | }
263 | else
264 | {
265 | if (args[0].IsTable)
266 | {
267 | LuaFunction f = delegate(LuaArguments x)
268 | {
269 | var s = x[0];
270 | var var = x[1].AsNumber() + 1;
271 |
272 | var val = s[var];
273 | if (val == LuaObject.Nil)
274 | return Return(LuaObject.Nil);
275 | else
276 | return Return(var, val);
277 | };
278 | return Return(f, args[0], 0);
279 | }
280 | else
281 | {
282 | throw new LuaException("t must be a table");
283 | }
284 | }
285 | }
286 |
287 | LuaArguments next(LuaArguments args)
288 | {
289 | var table = args[0];
290 | var index = args[1];
291 | if (!table.IsTable)
292 | {
293 | throw new LuaException("t must be a table");
294 | }
295 | List keys = new List(table.AsTable().Keys);
296 | if (index.IsNil)
297 | {
298 | if (keys.Count == 0)
299 | return Return();
300 | return Return(keys[0], table[keys[0]]);
301 | }
302 | else
303 | {
304 | int pos = keys.IndexOf(index);
305 | if (pos == keys.Count - 1)
306 | {
307 | return Return();
308 | }
309 | else
310 | {
311 | return Return(keys[pos + 1], table[keys[pos + 1]]);
312 | }
313 | }
314 | }
315 |
316 | LuaArguments pairs(LuaArguments args)
317 | {
318 | LuaObject handler = LuaEvents.getMetamethod(args[0], "__pairs");
319 | if (!handler.IsNil)
320 | {
321 | return handler.Call(args);
322 | }
323 | else
324 | {
325 | return Return((LuaFunction)next, args[0], LuaObject.Nil);
326 | }
327 | }
328 |
329 | #endregion
330 | }
331 | }
332 |
--------------------------------------------------------------------------------
/luablock/luablock/NetLua/Libraries/IoLibrary.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Text;
5 | using System.IO;
6 |
7 | namespace NetLua
8 | {
9 | public static class IoLibrary
10 | {
11 | // TODO: implment lines, read
12 | class FileObject
13 | {
14 | public FileObject(Stream s)
15 | {
16 | stream = s;
17 | if (s.CanRead)
18 | reader = new StreamReader(s);
19 | if (s.CanWrite)
20 | writer = new StreamWriter(s);
21 | }
22 |
23 | public Stream stream;
24 | public StreamReader reader;
25 | public StreamWriter writer;
26 | }
27 |
28 | static dynamic FileMetatable = LuaObject.NewTable();
29 |
30 | static LuaObject currentInput = LuaObject.Nil, currentOutput = LuaObject.Nil;
31 |
32 | static bool isStream(LuaObject obj)
33 | {
34 | return (obj.IsUserData && obj.luaobj is FileObject);
35 | }
36 |
37 | static LuaObject CreateFileObject(Stream stream)
38 | {
39 | LuaObject obj = LuaObject.FromObject(new FileObject(stream));
40 | obj.Metatable = FileMetatable;
41 |
42 | return obj;
43 | }
44 |
45 | static LuaObject CreateFileObject(Stream stream, bool autoflush)
46 | {
47 | FileObject fobj = new FileObject(stream);
48 | fobj.writer.AutoFlush = autoflush;
49 | LuaObject obj = LuaObject.FromObject(fobj);
50 | obj.Metatable = FileMetatable;
51 |
52 | return obj;
53 | }
54 |
55 | public static void AddIoLibrary(LuaContext Context)
56 | {
57 | dynamic io = LuaObject.NewTable();
58 |
59 | FileMetatable.__index = LuaObject.NewTable();
60 | FileMetatable.__index.write = (LuaFunction)write;
61 | FileMetatable.__index.close = (LuaFunction)close;
62 | FileMetatable.__index.flush = (LuaFunction)flush;
63 | FileMetatable.__index.seek = (LuaFunction)seek;
64 | FileMetatable.__index.read = (LuaFunction)read;
65 |
66 | io.open = (LuaFunction)io_open;
67 | io.type = (LuaFunction)io_type;
68 | io.input = (LuaFunction)io_input;
69 | io.output = (LuaFunction)io_output;
70 | io.temp = (LuaFunction)io_temp;
71 | io.flush = (LuaFunction)io_flush;
72 | io.write = (LuaFunction)io_write;
73 | io.read = (LuaFunction)io_read;
74 |
75 | currentInput = CreateFileObject(Console.OpenStandardInput());
76 | currentOutput = CreateFileObject(Console.OpenStandardOutput(), true);
77 | io.stdin = currentInput;
78 | io.stdout = currentOutput;
79 | io.stderr = CreateFileObject(Console.OpenStandardError(), true);
80 |
81 | Context.Set("io", io);
82 | }
83 |
84 | static LuaArguments io_open(LuaArguments args)
85 | {
86 | var file = args[0];
87 | var mode = args[1];
88 |
89 | if (file.IsString)
90 | {
91 | FileMode fmode = FileMode.Open;
92 | FileAccess faccess = FileAccess.Read;
93 | if (mode.IsString)
94 | {
95 | switch (mode.ToString())
96 | {
97 | case "r":
98 | faccess = FileAccess.Read; break;
99 | case "w":
100 | fmode = FileMode.Create; faccess = FileAccess.ReadWrite; break;
101 | case "a":
102 | fmode = FileMode.Append; faccess = FileAccess.Write; break;
103 | case "r+":
104 | case "w+":
105 | case "a+":
106 | // TODO: Implement rwa+
107 | throw new NotImplementedException();
108 | }
109 | }
110 | FileStream stream = new FileStream(file.ToString(), fmode, faccess);
111 |
112 | return Lua.Return(CreateFileObject(stream));
113 | }
114 | else
115 | return Lua.Return();
116 | }
117 |
118 | static LuaArguments io_type(LuaArguments args)
119 | {
120 | var obj = args[0];
121 | if (isStream(obj))
122 | {
123 | FileObject fobj = obj.luaobj as FileObject;
124 | if (!fobj.stream.CanWrite && !fobj.stream.CanRead)
125 | return Lua.Return("closed file");
126 | else
127 | return Lua.Return("file");
128 | }
129 | return Lua.Return();
130 | }
131 |
132 | static LuaArguments io_input(LuaArguments args)
133 | {
134 | var obj = args[0];
135 | if (isStream(obj))
136 | {
137 | currentInput = obj;
138 | return Lua.Return(currentInput);
139 | }
140 | else if (obj.IsString)
141 | {
142 | currentInput = io_open(args)[0];
143 | return Lua.Return(currentInput);
144 | }
145 | else if (args.Length == 0)
146 | return Lua.Return(currentInput);
147 | else
148 | throw new LuaException("Invalid argument");
149 | }
150 |
151 | static LuaArguments io_output(LuaArguments args)
152 | {
153 | var obj = args[0];
154 | if (isStream(obj))
155 | {
156 | currentOutput = obj;
157 | return Lua.Return(currentOutput);
158 | }
159 | else if (obj.IsString)
160 | {
161 | FileStream stream = new FileStream(obj.ToString(), FileMode.OpenOrCreate);
162 | currentOutput = CreateFileObject(stream);
163 | return Lua.Return(currentOutput);
164 | }
165 | else if (args.Length == 0)
166 | return Lua.Return(currentOutput);
167 | else
168 | throw new LuaException("Invalid argument");
169 | }
170 |
171 | static LuaArguments io_temp(LuaArguments args)
172 | {
173 | string path = Path.GetTempFileName();
174 | Stream s = new FileStream(path, FileMode.Append, FileAccess.Write, FileShare.Write, Int16.MaxValue, FileOptions.DeleteOnClose);
175 |
176 | return Lua.Return(CreateFileObject(s));
177 | }
178 |
179 | static LuaArguments io_write(LuaArguments args)
180 | {
181 | var obj = args[0];
182 | if (!obj.IsNil)
183 | return currentOutput["write"].MethodCall(currentOutput, args);
184 | else
185 | return Lua.Return();
186 | }
187 |
188 | static LuaArguments io_flush(LuaArguments args)
189 | {
190 | var obj = args[0];
191 | if (obj.IsNil)
192 | return currentOutput["flush"].MethodCall(currentOutput, args);
193 | else
194 | return obj["flush"].MethodCall(obj, args);
195 | }
196 |
197 | static LuaArguments io_close(LuaArguments args)
198 | {
199 | var obj = args[0];
200 | if (obj.IsNil)
201 | return currentOutput["close"].MethodCall(currentOutput, args);
202 | else
203 | return obj["close"].MethodCall(obj, args);
204 | }
205 |
206 | static LuaArguments io_read(LuaArguments args)
207 | {
208 | return currentInput["read"].MethodCall(currentInput, args);
209 | }
210 |
211 | static LuaArguments write(LuaArguments args)
212 | {
213 | var self = args[0];
214 | if (isStream(self))
215 | {
216 | FileObject fobj = self.luaobj as FileObject;
217 | foreach (var arg in args)
218 | {
219 | if (arg == self)
220 | continue;
221 |
222 | if (!(arg.IsString || arg.IsNumber))
223 | Lua.Return();
224 |
225 | if (fobj.stream.CanWrite)
226 | fobj.writer.Write(arg.ToString());
227 | else
228 | Lua.Return();
229 | }
230 | return Lua.Return(self);
231 | }
232 | else
233 | return Lua.Return();
234 | }
235 |
236 | static LuaArguments close(LuaArguments args)
237 | {
238 | var obj = args[0];
239 | if (isStream(obj))
240 | {
241 | FileObject fobj = obj.luaobj as FileObject;
242 | fobj.stream.Close();
243 | }
244 | return Lua.Return();
245 | }
246 |
247 | static LuaArguments flush(LuaArguments args)
248 | {
249 | var obj = args[0];
250 | if (isStream(obj))
251 | {
252 | FileObject fobj = obj.luaobj as FileObject;
253 | fobj.writer.Flush();
254 | }
255 | return Lua.Return();
256 | }
257 |
258 | static LuaArguments seek(LuaArguments args)
259 | {
260 | var obj = args[0];
261 | var whence = args[1] | "cur";
262 | var offset = args[2] | 0;
263 |
264 | if (isStream(obj))
265 | {
266 | var fobj = obj.luaobj as FileObject;
267 | switch (whence.ToString())
268 | {
269 | case "cur":
270 | fobj.stream.Position += (long)offset; break;
271 | case "set":
272 | fobj.stream.Position = (long)offset; break;
273 | case "end":
274 | fobj.stream.Position = fobj.stream.Length + (long)offset; break;
275 | }
276 | return Lua.Return(fobj.stream.Position);
277 | }
278 | return Lua.Return();
279 | }
280 |
281 | static LuaArguments read(LuaArguments args)
282 | {
283 | var self = args[0];
284 | if (isStream(self))
285 | {
286 | var fobj = self.luaobj as FileObject;
287 | if (args.Length == 1)
288 | {
289 | var line = fobj.reader.ReadLine();
290 |
291 | return Lua.Return(line);
292 | }
293 | else
294 | {
295 | List ret = new List();
296 | foreach (var arg in args)
297 | {
298 | if (arg == self)
299 | continue;
300 | if (arg.IsNumber)
301 | {
302 | StringBuilder bld = new StringBuilder();
303 | for (int i = 0; i < arg; i++)
304 | {
305 | bld.Append((char)fobj.reader.Read());
306 | }
307 | ret.Add(bld.ToString());
308 | }
309 | else if (arg == "*a")
310 | ret.Add(fobj.reader.ReadToEnd());
311 | else if (arg == "*l")
312 | ret.Add(fobj.reader.ReadLine());
313 | else if (arg == "*n")
314 | {
315 | //TODO: Implement io.read("*n")
316 | throw new NotImplementedException();
317 | }
318 | }
319 | return Lua.Return(ret.ToArray());
320 | }
321 | }
322 | else
323 | return Lua.Return();
324 | }
325 | }
326 | }
327 |
--------------------------------------------------------------------------------
/luablock/luablock/Compiler.cs:
--------------------------------------------------------------------------------
1 | using luablock.Commands;
2 | using NetLua;
3 | using NetLua.Ast;
4 | using System;
5 | using System.Collections;
6 | using System.Collections.Generic;
7 | using System.IO;
8 | using System.Linq;
9 | using System.Text;
10 | using System.Threading.Tasks;
11 |
12 | namespace luablock
13 | {
14 | public class Compiler
15 | {
16 | public Dictionary> Methods = new Dictionary>();
17 |
18 | public int ConstantCount = 0;
19 |
20 | public Compiler()
21 | {
22 | if(Directory.Exists("cmd"))
23 | {
24 | foreach(var i in Directory.GetFiles("cmd"))
25 | {
26 | if (i.EndsWith(".lua"))
27 | {
28 | var fl = new FileInfo(i);
29 | CommandManager.Cmds.Add(new LuaCmd() { Lua = File.ReadAllText(i), Name = fl.Name.Split('.')[0] });
30 | }
31 | }
32 | }
33 | else
34 | {
35 | Directory.CreateDirectory("cmd");
36 | }
37 | }
38 |
39 | public void Convert(string script, string schmaticpath)
40 | {
41 | Parser p = new Parser();
42 | var z = p.ParseString(script);
43 |
44 | Methods.Add("classlevel", BuildBody(z.Statements));
45 |
46 | //build clean up code
47 | List CleanUp = new List();
48 | int total = 0;
49 | foreach(var i in Methods)
50 | {
51 | foreach(var x in i.Value)
52 | {
53 | total++;
54 | if (x.TrimStart('/').StartsWith("scoreboard objectives add"))
55 | {
56 | CleanUp.Add("/scoreboard objectives remove " + x.TrimStart('/').Split(' ')[3]);
57 | }
58 | }
59 | }
60 | Methods["classlevel"].Add("|" + (total / 4));//indicated we need more repeaters
61 | Methods["classlevel"].AddRange(CleanUp);
62 | BuildSchematic(schmaticpath);
63 |
64 | }
65 |
66 | public int IFsfound = 0;
67 |
68 | public void iterateLogic(List Lines, IfStat ifs)
69 | {
70 |
71 | var a = TypeToString((ifs.Condition as BinaryExpression).Left);
72 | var b = TypeToString((ifs.Condition as BinaryExpression).Right);
73 |
74 | Lines.Add("#/scoreboard players test lua " + a + " " + b + " " + b);
75 | }
76 |
77 |
78 | public List BuildBody(List Statments, List Linesa = null)
79 | {
80 | if(Linesa == null)
81 | {
82 | Linesa = new List();
83 | }
84 | List Lines = Linesa;
85 | foreach (var i in Statments)
86 | {
87 | if(i is IfStat)
88 | {
89 | var x = i as IfStat;
90 | iterateLogic(Lines, x);
91 | Methods.Add("lua_if_" + IFsfound, BuildBody(x.Block.Statements));
92 | var tmplines = Lines.ToArray().ToList();
93 | //tmplines.Add("");
94 | var bd = BuildBody(new List() { new FunctionCall() { Arguments = new List(), Function = new StringLiteral() { Value = "lua_if_" + IFsfound } } }, tmplines);
95 | bd[bd.Count - 1] = bd[bd.Count - 1].TrimStart('/');// 1 tick repeater
96 | Lines.Add(bd[bd.Count - 1]);
97 | IFsfound++;
98 | }
99 |
100 | if (i is FunctionCall)
101 | {
102 | // /setblock ~ ~ ~ minecraft:redstone_block
103 | var x = i as FunctionCall;
104 | if (Methods.ContainsKey(TypeToString(x.Function)))
105 | {
106 | // note: how there is no /, this is to say we need a delay on the repleater
107 | var ins = 0;
108 | if ((Methods.Keys.ToList().IndexOf(TypeToString(x.Function)) == 0))
109 | {
110 | ins = Methods.Count * 2 + (Methods.Keys.ToList().IndexOf(TypeToString(x.Function))) * 2;
111 | }
112 | else
113 | {
114 |
115 | var index = (Methods.Keys.ToList().IndexOf(TypeToString(x.Function)));
116 | ins = (Methods.Count - index) * 2;
117 |
118 |
119 | }
120 | Lines.Add("setblock ~-" + (ins) + " ~ ~-" + ((Lines.Count * 2) + 2) + " minecraft:redstone_block");
121 | }
122 | else
123 | {
124 |
125 | var y = CommandManager.GetCommand(TypeToString(x.Function));
126 | Lines.Add(y.Parse(TypeToString(x.Function), ArgBuilder(x.Arguments)));
127 | }
128 | }
129 | if (i is Assignment)
130 | {
131 | var x = i as Assignment;
132 |
133 | if (x.Expressions[0] is FunctionDefinition)
134 | {
135 | var fnc = x.Expressions[0] as FunctionDefinition;
136 | Methods.Add(TypeToString((x.Variables[0] as Variable)), BuildBody(fnc.Body.Statements));
137 | }
138 | else
139 | {
140 | if (x.Expressions[0] is BinaryExpression)
141 | {
142 | var name = TypeToString((x.Variables[0] as Variable));
143 | var j = x.Expressions[0] as BinaryExpression;
144 | iterateBinOp(j, name , Lines);
145 |
146 | }
147 | else
148 | {
149 | var name = TypeToString((x.Variables[0] as Variable));
150 | //Lines.Add("/scoreboard objectives remove " + name);
151 | Lines.Add("/scoreboard objectives add " + name + " dummy");
152 | Lines.Add("/scoreboard players set lua " + name + " " + TypeToString(x.Expressions[0]));
153 | }
154 | }
155 | }
156 | }
157 |
158 | return Lines;
159 | }
160 |
161 |
162 | public int optc = 0;
163 |
164 | public string iterateBinOp(BinaryExpression bb, string name, List Lines)
165 | {
166 | var line = "";
167 | string a = "";
168 | string b = "";
169 |
170 | if (bb.Left is BinaryExpression)
171 | {
172 | a = name + "_opt_" + optc;
173 | optc++;
174 | //Lines.Add("/scoreboard objectives remove " + a);
175 | Lines.Add("/scoreboard objectives add " + a + " dummy");
176 | var ln = iterateBinOp(bb.Left as BinaryExpression, a , Lines);
177 | Lines.Add("/scoreboard players operation lua " + a + " " + "+=" + " lua " + ln);
178 | }
179 | else
180 | {
181 | if(char.IsDigit(TypeToString(bb.Left)[0]))
182 | {
183 | //and new var
184 | //Lines.Add("/scoreboard objectives remove con_" + ConstantCount);
185 | Lines.Add("/scoreboard objectives add con_" + ConstantCount + " dummy");
186 | Lines.Add("/scoreboard players set lua con_" + ConstantCount + " " + TypeToString(bb.Left));
187 | a = "con_" + ConstantCount;
188 | ConstantCount++;
189 | }
190 | else
191 | {
192 | a = TypeToString(bb.Left);
193 | }
194 | }
195 |
196 | if (bb.Right is BinaryExpression)
197 | {
198 | b = name + "_opt_" + optc;
199 | optc++;
200 | // Lines.Add("/scoreboard objectives remove " + b);
201 | Lines.Add("/scoreboard objectives add " + b + " dummy");
202 | var ln = iterateBinOp(bb.Right as BinaryExpression, b, Lines);
203 | Lines.Add("/scoreboard players operation lua " + b + " " + "+=" + " lua " + ln);
204 | }
205 | else
206 | {
207 | if (char.IsDigit(TypeToString(bb.Right)[0]))
208 | {
209 | //and new var
210 | // Lines.Add("/scoreboard objectives remove con_" + ConstantCount);
211 | Lines.Add("/scoreboard objectives add con_" + ConstantCount + " dummy");
212 | Lines.Add("/scoreboard players set lua con_" + ConstantCount + " " + TypeToString(bb.Right));
213 | b = "con_" + ConstantCount;
214 | ConstantCount++;
215 | }
216 | else
217 | {
218 | b = TypeToString(bb.Right);
219 | }
220 | }
221 |
222 |
223 | Lines.Add("/scoreboard players operation lua " + a + " " + BuildBOp(bb.Operation) + " lua " + b );
224 | line = a;
225 | return line;
226 | }
227 |
228 | public string BuildBOp(BinaryOp bo)
229 | {
230 | switch(bo)
231 | {
232 | case BinaryOp.Addition:
233 | return "+=";
234 | case BinaryOp.Subtraction:
235 | return "-=";
236 | case BinaryOp.Division:
237 | return "/=";
238 | case BinaryOp.Multiplication:
239 | return "*=";
240 | }
241 | return "+=";
242 | }
243 |
244 | public List ArgBuilder(List perams)
245 | {
246 | var re = new List();
247 | foreach(var i in perams)
248 | {
249 | re.Add(TypeToString(i));
250 | }
251 | return re;
252 | }
253 |
254 | private int getIntFromBitArray(BitArray bitArray)
255 | {
256 |
257 | if (bitArray.Length > 32)
258 | throw new ArgumentException("Argument length shall be at most 32 bits.");
259 |
260 | int[] array = new int[1];
261 | bitArray.CopyTo(array, 0);
262 | return array[0];
263 |
264 | }
265 |
266 | public void BuildSchematic(string schmaticpath)
267 | {
268 | int maxl = 0;
269 | int maxlw = 0;
270 | foreach (var i in Methods)
271 | {
272 | if (i.Value.Count > maxl)
273 | {
274 | maxl = i.Value.Count;
275 | }
276 | foreach (var iyy in i.Value)
277 | {
278 | var iz = iyy;
279 | if (iz.StartsWith("|"))
280 | {
281 | iz = iz.TrimStart('|');
282 | maxlw += int.Parse(iz + 2); // 2 exstra repeaters to give script time
283 |
284 | }
285 | }
286 | }
287 |
288 |
289 |
290 |
291 | Schematic s = new Schematic();
292 | s.Height = 1;
293 | s.Width = (short)((Methods.Count * 2) + maxlw + 5) ;
294 | s.Length = (short)(maxl * 2 + 2 + 5);
295 | s.Fill();
296 | int x = 0;
297 | foreach (var ii in Methods)
298 | {
299 | int z = 1;
300 | s.SetBlock(x, 0, 0, new Block() { ID = 93, Metta = 2 });
301 | foreach (var iyy in ii.Value)
302 | {
303 | var i = iyy;
304 |
305 | if (i.StartsWith("|"))
306 | {
307 | i = i.TrimStart('|');
308 | for (int iu = 0; iu < int.Parse(i); iu++)
309 | {
310 | s.SetBlock(x, 0, z, new Block() { ID = 93, Metta = 14 }); // full tic repeater
311 | z++;
312 | }
313 |
314 | }
315 | else
316 | {
317 | int b = 2;
318 | if (!i.StartsWith("/"))
319 | {
320 | b = 6;
321 | }
322 | if (i.StartsWith("#"))
323 | {
324 | i = i.TrimStart('#');
325 | s.SetBlock(x, 0, z + 1, new Block() { ID = 149, Metta = 2 });
326 | }
327 | else
328 | {
329 | s.SetBlock(x, 0, z + 1, new Block() { ID = 93, Metta = b });
330 | }
331 | s.SetBlock(x, 0, z, new Block() { ID = 137, Metta = 0, Command = i });
332 | z += 2;
333 | }
334 | }
335 | s.SetBlock(x, 0, z, new Block() { ID = 137, Metta = 0, Command = "/setblock ~ ~ ~-" + (z + 1) + " minecraft:air" });
336 | x += 2;
337 | }
338 | s.Save(schmaticpath);
339 | }
340 |
341 |
342 |
343 |
344 | private string TypeToString(IExpression e)
345 | {
346 | //this feels so cheety(whatt ever you do dont show this too kenny! xD)
347 |
348 | dynamic z = e;
349 | try
350 | {
351 | try
352 | {
353 | return z.Value.ToString();
354 | }
355 | catch
356 | {
357 | return z.Name.ToString();
358 | }
359 | }
360 | catch
361 | {
362 | return "";
363 | }
364 | }
365 | }
366 | }
367 |
--------------------------------------------------------------------------------
/luablock/luablock/NetLua/LuaEvents.cs:
--------------------------------------------------------------------------------
1 | /*
2 | * See LICENSE file
3 | */
4 |
5 | using System;
6 | using System.Collections.Generic;
7 | using System.Linq;
8 | using System.Text;
9 |
10 | namespace NetLua
11 | {
12 | static class LuaEvents
13 | {
14 | // This is all based on the Lua 5.2 manual
15 | // http://www.lua.org/manual/5.2/manual.html#2.4
16 |
17 | public static LuaObject rawget(LuaObject table, LuaObject index)
18 | {
19 | if (table.IsTable)
20 | {
21 | LuaObject obj;
22 | if (table.AsTable().TryGetValue(index, out obj))
23 | return obj;
24 | else
25 | return LuaObject.Nil;
26 | }
27 | else
28 | throw new LuaException("Invalid operation");
29 | }
30 |
31 | public static void rawset(LuaObject table, LuaObject key, LuaObject value)
32 | {
33 | if (table.IsTable)
34 | {
35 | var t = table.AsTable();
36 | if (t.ContainsKey(key))
37 | t[key] = value;
38 | else
39 | t.Add(key, value);
40 | }
41 | else
42 | throw new LuaException("Invalid operation");
43 | }
44 |
45 | static LuaObject getBinhandler(LuaObject a, LuaObject b, string f)
46 | {
47 | var f1 = getMetamethod(a, f);
48 | var f2 = getMetamethod(b, f);
49 |
50 | return f1 | f2;
51 | }
52 |
53 | static LuaObject getEqualhandler(LuaObject a, LuaObject b)
54 | {
55 | if ((a.Type != b.Type) || (a.IsTable && b.IsUserData))
56 | return LuaObject.Nil;
57 | var mm1 = getMetamethod(a, "__eq");
58 | var mm2 = getMetamethod(b, "__eq");
59 | if (mm1 == mm2)
60 | return mm1;
61 | else
62 | return LuaObject.Nil;
63 | }
64 |
65 | internal static LuaObject toNumber(LuaObject obj)
66 | {
67 | if (obj.IsNumber)
68 | return LuaObject.FromNumber(obj.AsNumber());
69 | else if (obj.IsString)
70 | {
71 | double d;
72 | if (double.TryParse(obj.AsString(), out d))
73 | return LuaObject.FromNumber(d);
74 | else
75 | return LuaObject.Nil;
76 | }
77 | else
78 | return LuaObject.Nil;
79 | }
80 |
81 | internal static bool ConvertToNumber(LuaObject obj, out double value)
82 | {
83 | if (obj.IsNumber)
84 | {
85 | value = (double)obj.luaobj;
86 | return true;
87 | }
88 | else if (obj.IsString)
89 | {
90 | if (double.TryParse(obj.AsString(), out value))
91 | return true;
92 | else
93 | return false;
94 | }
95 | else
96 | {
97 | value = 0d;
98 | return false;
99 | }
100 | }
101 |
102 | internal static LuaObject getMetamethod(LuaObject obj, string e)
103 | {
104 | if (obj.Metatable == null || obj.Metatable.IsNil)
105 | return LuaObject.Nil;
106 | else
107 | return obj.Metatable[e];
108 | }
109 |
110 | internal static LuaObject add_event(LuaObject op1, LuaObject op2)
111 | {
112 | double a, b;
113 |
114 | if (ConvertToNumber(op1, out a) && ConvertToNumber(op2, out b))
115 | return LuaObject.FromNumber(a + b);
116 | else
117 | {
118 | var handler = getBinhandler(op1, op2, "__add");
119 | if (!handler.IsNil)
120 | return handler.Call(op1, op2)[0];
121 | }
122 |
123 | throw new LuaException("Invalid arithmetic operation");
124 | }
125 |
126 | internal static LuaObject sub_event(LuaObject op1, LuaObject op2)
127 | {
128 | double a, b;
129 |
130 | if (ConvertToNumber(op1, out a) && ConvertToNumber(op2, out b))
131 | return LuaObject.FromNumber(a - b);
132 | else
133 | {
134 | var handler = getBinhandler(op1, op2, "__sub");
135 | if (!handler.IsNil)
136 | return handler.Call(op1, op2)[0];
137 | }
138 |
139 | throw new LuaException("Invalid arithmetic operation");
140 | }
141 |
142 | internal static LuaObject mul_event(LuaObject op1, LuaObject op2)
143 | {
144 | double a, b;
145 |
146 | if (ConvertToNumber(op1, out a) && ConvertToNumber(op2, out b))
147 | return LuaObject.FromNumber(a * b);
148 | else
149 | {
150 | var handler = getBinhandler(op1, op2, "__mul");
151 | if (!handler.IsNil)
152 | return handler.Call(op1, op2)[0];
153 | }
154 |
155 | throw new LuaException("Invalid arithmetic operation");
156 | }
157 |
158 | internal static LuaObject div_event(LuaObject op1, LuaObject op2)
159 | {
160 | double a, b;
161 |
162 | if (ConvertToNumber(op1, out a) && ConvertToNumber(op2, out b))
163 | return LuaObject.FromNumber(a / b);
164 | else
165 | {
166 | var handler = getBinhandler(op1, op2, "__div");
167 | if (!handler.IsNil)
168 | return handler.Call(op1, op2)[0];
169 | }
170 |
171 | throw new LuaException("Invalid arithmetic operation");
172 | }
173 |
174 | internal static LuaObject mod_event(LuaObject op1, LuaObject op2)
175 | {
176 | double a, b;
177 |
178 | if (ConvertToNumber(op1, out a) && ConvertToNumber(op2, out b))
179 | return LuaObject.FromNumber(a - Math.Floor(a / b) * b);
180 | else
181 | {
182 | var handler = getBinhandler(op1, op2, "__mod");
183 | if (!handler.IsNil)
184 | return handler.Call(op1, op2)[0];
185 | }
186 |
187 | throw new LuaException("Invalid arithmetic operation");
188 | }
189 |
190 | internal static LuaObject pow_event(LuaObject op1, LuaObject op2)
191 | {
192 | double a, b;
193 |
194 | if (ConvertToNumber(op1, out a) && ConvertToNumber(op2, out b))
195 | return LuaObject.FromNumber(Math.Pow(a, b));
196 | else
197 | {
198 | var handler = getBinhandler(op1, op2, "__pow");
199 | if (!handler.IsNil)
200 | return handler.Call(op1, op2)[0];
201 | }
202 |
203 | throw new LuaException("Invalid arithmetic operation");
204 | }
205 |
206 | internal static LuaObject unm_event(LuaObject op)
207 | {
208 | double a;
209 |
210 | if (ConvertToNumber(op, out a))
211 | {
212 | return LuaObject.FromNumber(-a);
213 | }
214 | else
215 | {
216 | var handler = getMetamethod(op, "__unm");
217 | if (!handler.IsNil)
218 | return handler.Call(op)[0];
219 | }
220 |
221 | throw new LuaException("Invalid arithmetic operation");
222 | }
223 |
224 | internal static LuaObject index_event(LuaObject table, LuaObject key)
225 | {
226 | LuaObject handler;
227 |
228 | if (table.IsTable)
229 | {
230 | var v = rawget(table, key);
231 | if (!v.IsNil)
232 | return v;
233 | else
234 | {
235 | handler = getMetamethod(table, "__index");
236 | if (handler.IsNil)
237 | return LuaObject.Nil;
238 | }
239 | }
240 | else
241 | {
242 | handler = getMetamethod(table, "__index");
243 | if (handler.IsNil)
244 | throw new LuaException("Invalid argument");
245 | }
246 |
247 | if (handler.IsFunction)
248 | return handler.AsFunction()(new LuaObject[]{table, key})[0];
249 | else if (!handler.IsNil)
250 | return handler[key];
251 | else
252 | return LuaObject.Nil;
253 | }
254 |
255 | internal static LuaObject newindex_event(LuaObject table, LuaObject key, LuaObject value)
256 | {
257 | LuaObject handler;
258 | if (table.IsTable)
259 | {
260 | var v = rawget(table, key);
261 | if (!v.IsNil)
262 | {
263 | rawset(table, key, value);
264 | return LuaObject.Nil;
265 | }
266 | handler = getMetamethod(table, "__newindex");
267 | if (handler.IsNil)
268 | {
269 | rawset(table, key, value);
270 | return LuaObject.Nil;
271 | }
272 | }
273 | else
274 | {
275 | handler = getMetamethod(table, "__newindex");
276 | if (handler.IsNil)
277 | throw new LuaException("Invalid op");
278 | }
279 |
280 | if (handler.IsFunction)
281 | handler.AsFunction()(new LuaObject[]{table, key, value});
282 | else
283 | handler[key] = value;
284 | return LuaObject.Nil;
285 | }
286 |
287 | internal static LuaArguments call_event(LuaObject func, LuaArguments args)
288 | {
289 | if (func.IsFunction)
290 | return func.AsFunction()(args);
291 | else
292 | {
293 | var handler = getMetamethod(func, "__call");
294 | if (handler.IsFunction)
295 | {
296 | var argslist = new List();
297 | argslist.Add(func); argslist.AddRange(args);
298 | return handler.AsFunction()(argslist.ToArray());
299 | }
300 | else
301 | throw new LuaException("Cannot call nonfunction");
302 | }
303 | }
304 |
305 | internal static LuaObject len_event(LuaObject op)
306 | {
307 | if (op.IsString)
308 | return op.AsString().Length;
309 | else
310 | {
311 | var handler = getMetamethod(op, "__len");
312 | if (!handler.IsNil)
313 | return handler.Call(op)[0];
314 | else if (op.IsTable)
315 | return op.AsTable().Count;
316 | else
317 | throw new LuaException("Invalid op");
318 | }
319 | }
320 |
321 | internal static LuaObject concat_event(LuaObject op1, LuaObject op2)
322 | {
323 | if ((op1.IsString || op1.IsNumber) && (op2.IsString || op2.IsNumber))
324 | return string.Concat(op1, op2);
325 | else
326 | {
327 | var handler = getBinhandler(op1, op2, "__concat");
328 | if (!handler.IsNil)
329 | return handler.Call(op1, op2)[0];
330 | else
331 | throw new LuaException("Invalid op");
332 | }
333 | }
334 |
335 | internal static LuaObject eq_event(LuaObject op1, LuaObject op2)
336 | {
337 | if (op1 == op2)
338 | return true;
339 | var handler = getEqualhandler(op1, op2);
340 | if (!handler.IsNil)
341 | return !(!(handler.Call(op1, op2)[0].AsBool()));
342 | else
343 | return false;
344 | }
345 |
346 | internal static LuaObject lt_event(LuaObject op1, LuaObject op2)
347 | {
348 | if (op1.IsNumber && op2.IsNumber)
349 | return op1.AsNumber() < op2.AsNumber();
350 | else if (op1.IsString && op2.IsString)
351 | {
352 | int n = StringComparer.CurrentCulture.Compare(op1.AsString(), op2.AsString());
353 | return (n < 0);
354 | }
355 | else
356 | {
357 | var handler = getBinhandler(op1, op2, "__lt");
358 | if (!handler.IsNil)
359 | return !(!(handler.Call(op1, op2)[0].AsBool()));
360 | else
361 | throw new ArgumentException("attempt to compare " + op1.type.ToString() + " with " + op2.type.ToString());
362 | }
363 | }
364 |
365 | internal static LuaObject le_event(LuaObject op1, LuaObject op2)
366 | {
367 | if (op1.IsNumber && op2.IsNumber)
368 | return op1.AsNumber() <= op2.AsNumber();
369 | else if (op1.IsString && op2.IsString)
370 | {
371 | int n = StringComparer.CurrentCulture.Compare(op1.AsString(), op2.AsString());
372 | return (n <= 0);
373 | }
374 | else
375 | {
376 | var handler = getBinhandler(op1, op2, "__le");
377 | if (!handler.IsNil)
378 | return !(!(handler.Call(op1, op2)[0].AsBool()));
379 | else
380 | throw new ArgumentException("attempt to compare " + op1.type.ToString() + " with " + op2.type.ToString());
381 | }
382 | }
383 |
384 | internal static LuaObject gc_event(LuaObject op)
385 | {
386 | var handler = getMetamethod(op, "__gc");
387 | if (handler.IsFunction)
388 | handler.AsFunction()(new LuaObject[] {op});
389 | return LuaObject.Nil;
390 | }
391 |
392 | internal static LuaObject tostring_event(LuaObject op)
393 | {
394 | var handler = getMetamethod(op, "__tostring");
395 | if (!handler.IsNil)
396 | return handler.Call(op)[0];
397 | else
398 | return op.ToString();
399 | }
400 | }
401 | }
402 |
--------------------------------------------------------------------------------
/luablock/luablock/NetLua/LuaObject.cs:
--------------------------------------------------------------------------------
1 | /*
2 | * See LICENSE file
3 | */
4 |
5 | using System;
6 | using System.Linq;
7 | using System.Collections;
8 | using System.Collections.Generic;
9 | using System.Threading;
10 |
11 | using System.Dynamic;
12 |
13 | namespace NetLua
14 | {
15 | using LuaTable = IDictionary;
16 | using LuaTableImpl = Dictionary;
17 | using LuaTableItem = KeyValuePair;
18 |
19 | ///
20 | /// An exception thrown by the Lua interpreter
21 | ///
22 | public class LuaException : Exception
23 | {
24 | ///
25 | /// An exception thrown by a syntactical error
26 | ///
27 | /// The file wich contains the error
28 | /// The row of the error
29 | /// The column of the error
30 | /// The kind of the error
31 | public LuaException(string file, int row, int col, string message)
32 | : base(string.Format("Error in {0}({1},{2}): {3}", file, row, col, message))
33 | { }
34 |
35 | public LuaException(string message)
36 | : base("Error (unknown context): " + message)
37 | { }
38 | }
39 |
40 | ///
41 | /// A Lua function
42 | ///
43 | public delegate LuaArguments LuaFunction(LuaArguments args);
44 |
45 | public class LuaArguments : IEnumerable
46 | {
47 | List list;
48 |
49 | public LuaArguments()
50 | {
51 | list = new List();
52 | }
53 |
54 | public LuaArguments(List List)
55 | {
56 | list = List;
57 | }
58 |
59 | public LuaArguments(params LuaObject[] Objects)
60 | {
61 | list = new List(Objects);
62 | }
63 |
64 | public LuaArguments(params LuaArguments[] Objects)
65 | {
66 | foreach (LuaArguments arg in Objects)
67 | {
68 | if (list == null)
69 | list = new List(arg.list);
70 | else
71 | list.AddRange(arg.list);
72 | }
73 | }
74 |
75 | public int Length
76 | {
77 | get
78 | {
79 | return list.Count;
80 | }
81 | }
82 |
83 | public LuaObject this[int Index]
84 | {
85 | get
86 | {
87 | if (Index < list.Count)
88 | return list[Index];
89 | else
90 | return LuaObject.Nil;
91 | }
92 | set
93 | {
94 | if (Index < list.Count)
95 | list[Index] = value;
96 | }
97 | }
98 |
99 | public void Add(LuaObject obj)
100 | {
101 | list.Add(obj);
102 | }
103 |
104 | public LuaArguments Concat(LuaArguments args)
105 | {
106 | list.AddRange(args.list);
107 | return this;
108 | }
109 |
110 | public IEnumerator GetEnumerator()
111 | {
112 | return list.GetEnumerator();
113 | }
114 |
115 | IEnumerator IEnumerable.GetEnumerator()
116 | {
117 | return this.GetEnumerator();
118 | }
119 |
120 | public static implicit operator LuaArguments(LuaObject[] array)
121 | {
122 | return new LuaArguments(array);
123 | }
124 |
125 | public static implicit operator LuaObject[](LuaArguments args)
126 | {
127 | return args.list.ToArray();
128 | }
129 |
130 | public LuaArguments GetSubset(int startIndex)
131 | {
132 | if (startIndex >= list.Count)
133 | {
134 | return new LuaArguments(new LuaObject[] { });
135 | }
136 | else
137 | {
138 | return new LuaArguments(list.GetRange(startIndex, list.Count - startIndex));
139 | }
140 | }
141 | }
142 |
143 | // http://www.lua.org/pil/2.html
144 | ///
145 | /// A Lua type
146 | ///
147 | public enum LuaType : byte
148 | {
149 | nil = 0,
150 | boolean,
151 | number,
152 | @string,
153 | userdata,
154 | function,
155 | thread,
156 | table
157 | }
158 |
159 | ///
160 | /// A Lua object. Can be any of the standard Lua objects
161 | ///
162 | public class LuaObject : DynamicObject, IEnumerable>
163 | {
164 | internal object luaobj;
165 | internal LuaType type;
166 | private LuaObject metatable = Nil;
167 |
168 | public LuaObject()
169 | {
170 | this.metatable = Nil;
171 | }
172 |
173 | private LuaObject(object Obj, LuaType Type)
174 | {
175 | this.metatable = Nil;
176 | this.luaobj = Obj;
177 | this.type = Type;
178 | }
179 |
180 | ~LuaObject()
181 | {
182 | LuaEvents.gc_event(this);
183 | }
184 |
185 | ///
186 | /// Gets or sets the object's metatable
187 | ///
188 | public LuaObject Metatable
189 | {
190 | get
191 | {
192 | return metatable;
193 | }
194 | set
195 | {
196 | metatable = value;
197 | }
198 | }
199 |
200 | #region Common objects
201 | ///
202 | /// An empty/unset value
203 | ///
204 | public static readonly LuaObject Nil = new LuaObject() { luaobj = null, type = LuaType.nil };
205 |
206 | ///
207 | /// A standard true boolean value
208 | ///
209 | public static readonly LuaObject True = new LuaObject { luaobj = true, type = LuaType.boolean, Metatable = Nil };
210 |
211 | ///
212 | /// A standard false boolean value
213 | ///
214 | public static readonly LuaObject False = new LuaObject { luaobj = false, type = LuaType.boolean, Metatable = Nil };
215 |
216 | ///
217 | /// Zero (number)
218 | ///
219 | public static readonly LuaObject Zero = new LuaObject { luaobj = 0d, type = LuaType.number, Metatable = Nil };
220 |
221 | ///
222 | /// And empty string
223 | ///
224 | public static readonly LuaObject EmptyString = new LuaObject { luaobj = "", type = LuaType.@string, Metatable = Nil };
225 | #endregion
226 |
227 | ///
228 | /// Gets the underlying Lua type
229 | ///
230 | public LuaType Type { get { return type; } internal set { type = value; } }
231 |
232 | ///
233 | /// Checks whether the type matches or not
234 | ///
235 | public bool Is(LuaType type)
236 | {
237 | return this.type == type;
238 | }
239 |
240 | ///
241 | /// Creates a Lua object from a .NET object
242 | /// Automatically checks if there is a matching Lua type.
243 | /// If not, creates a userdata value
244 | ///
245 | public static LuaObject FromObject(object obj)
246 | {
247 | if (obj == null)
248 | return Nil;
249 |
250 | if (obj is LuaObject)
251 | return (LuaObject)obj;
252 |
253 | if (obj is bool)
254 | return FromBool((bool)obj);
255 |
256 | {
257 | var str = obj as string;
258 | if (str != null)
259 | return FromString(str);
260 | }
261 |
262 | {
263 | var @delegate = obj as LuaFunction;
264 | if (@delegate != null)
265 | return FromFunction(@delegate);
266 | }
267 |
268 | {
269 | var dictionary = obj as LuaTable;
270 | if (dictionary != null)
271 | return FromTable(dictionary);
272 | }
273 |
274 | if (obj is double) return FromNumber((double)obj);
275 | if (obj is float) return FromNumber((float)obj);
276 | if (obj is int) return FromNumber((int)obj);
277 | if (obj is uint) return FromNumber((uint)obj);
278 | if (obj is short) return FromNumber((short)obj);
279 | if (obj is ushort) return FromNumber((ushort)obj);
280 | if (obj is long) return FromNumber((long)obj);
281 | if (obj is ulong) return FromNumber((ulong)obj);
282 | if (obj is byte) return FromNumber((byte)obj);
283 | if (obj is sbyte) return FromNumber((sbyte)obj);
284 | if (obj is Thread) return new LuaObject { luaobj = obj, type = LuaType.thread };
285 | return FromUserData(obj);
286 | }
287 |
288 | #region Boolean
289 | ///
290 | /// Creates a Lua object from a boolean value
291 | ///
292 | public static LuaObject FromBool(bool bln)
293 | {
294 | if (bln)
295 | return True;
296 |
297 | return False;
298 | }
299 |
300 | public static implicit operator LuaObject(bool bln)
301 | {
302 | return FromBool(bln);
303 | }
304 |
305 | public static implicit operator bool(LuaObject obj)
306 | {
307 | return obj.AsBool();
308 | }
309 |
310 | ///
311 | /// Gets whether this is a boolean object
312 | ///
313 | public bool IsBool { get { return type == LuaType.boolean; } }
314 |
315 | ///
316 | /// Converts this Lua object into a boolean
317 | ///
318 | ///
319 | public bool AsBool()
320 | {
321 | if (luaobj == null)
322 | return false;
323 |
324 | if (luaobj is bool && ((bool)luaobj) == false)
325 | return false;
326 |
327 | return true;
328 | }
329 | #endregion
330 |
331 | #region Number
332 | ///
333 | /// Creates a Lua object from a double
334 | ///
335 | public static LuaObject FromNumber(double number)
336 | {
337 | if (number == 0d)
338 | return Zero;
339 |
340 | return new LuaObject(number, LuaType.number);
341 | }
342 |
343 | public static implicit operator LuaObject(double number)
344 | {
345 | return FromNumber(number);
346 | }
347 |
348 | public static implicit operator double(LuaObject obj)
349 | {
350 | return obj.AsNumber();
351 | }
352 |
353 | ///
354 | /// Gets whether this is a number object
355 | ///
356 | public bool IsNumber { get { return type == LuaType.number; } }
357 |
358 | ///
359 | /// Converts this object into a number
360 | ///
361 | public double AsNumber()
362 | {
363 | return (double)luaobj;
364 | }
365 | #endregion
366 |
367 | #region String
368 | ///
369 | /// Creates a Lua object from a string
370 | ///
371 | public static LuaObject FromString(string str)
372 | {
373 | if (str == null)
374 | return Nil;
375 |
376 | if (str.Length == 0)
377 | return EmptyString;
378 |
379 | return new LuaObject(str, LuaType.@string);
380 | }
381 |
382 | public static implicit operator LuaObject(string str)
383 | {
384 | return FromString(str);
385 | }
386 |
387 | public static implicit operator string(LuaObject obj)
388 | {
389 | return obj.AsString();
390 | }
391 |
392 | public bool IsString { get { return type == LuaType.@string; } }
393 |
394 | public string AsString()
395 | {
396 | return luaobj.ToString();
397 | }
398 | #endregion
399 |
400 | #region Function
401 | ///
402 | /// Creates a Lua object from a Lua function
403 | ///
404 | public static LuaObject FromFunction(LuaFunction fn)
405 | {
406 | if (fn == null)
407 | return Nil;
408 |
409 | return new LuaObject(fn, LuaType.function);
410 | }
411 |
412 | public static implicit operator LuaObject(LuaFunction fn)
413 | {
414 | return FromFunction(fn);
415 | }
416 |
417 | public bool IsFunction { get { return type == LuaType.function; } }
418 |
419 | public LuaFunction AsFunction()
420 | {
421 | var fn = luaobj as LuaFunction;
422 | if (fn == null)
423 | throw new LuaException("cannot call non-function");
424 |
425 | return fn;
426 | }
427 | #endregion
428 |
429 | #region Table
430 | ///
431 | /// Creates a Lua object from a Lua table
432 | ///
433 | public static LuaObject FromTable(LuaTable table)
434 | {
435 | if (table == null)
436 | return Nil;
437 |
438 | return new LuaObject(table, LuaType.table);
439 | }
440 |
441 | ///
442 | /// Creates and initializes a Lua object with a table value
443 | ///
444 | /// The initial items of the table to create
445 | public static LuaObject NewTable(params LuaTableItem[] initItems)
446 | {
447 | var table = FromTable(new LuaTableImpl());
448 |
449 | foreach (var item in initItems)
450 | table[item.Key] = item.Value;
451 |
452 | return table;
453 | }
454 |
455 | public bool IsTable { get { return type == LuaType.table; } }
456 |
457 | public LuaTable AsTable()
458 | {
459 | return luaobj as LuaTable;
460 | }
461 | #endregion
462 |
463 | #region Userdata
464 | ///
465 | /// Creates a Lua object from a .NET object
466 | ///
467 | public static LuaObject FromUserData(object userdata)
468 | {
469 | if (userdata == null)
470 | return Nil;
471 |
472 | //return new LuaObject { luaobj = userdata, type = LuaType.userdata };
473 | return new LuaObject(userdata, LuaType.userdata);
474 | }
475 |
476 | ///
477 | /// Gets whether this object is nil
478 | ///
479 | public bool IsNil { get { return type == LuaType.nil; } }
480 |
481 | ///
482 | /// Gets whether this object is userdata
483 | ///
484 | public bool IsUserData { get { return type == LuaType.userdata; } }
485 |
486 | ///
487 | /// Returns the CLI object underneath the wrapper
488 | ///
489 | public object AsUserData()
490 | {
491 | return luaobj;
492 | }
493 | #endregion
494 |
495 | public static LuaObject operator +(LuaObject a, LuaObject b)
496 | {
497 | return LuaEvents.add_event(a, b);
498 | }
499 |
500 | public static LuaObject operator -(LuaObject a, LuaObject b)
501 | {
502 | return LuaEvents.sub_event(a, b);
503 | }
504 |
505 | public static LuaObject operator *(LuaObject a, LuaObject b)
506 | {
507 | return LuaEvents.mul_event(a, b);
508 | }
509 |
510 | public static LuaObject operator /(LuaObject a, LuaObject b)
511 | {
512 | return LuaEvents.div_event(a, b);
513 | }
514 |
515 | public static LuaObject operator %(LuaObject a, LuaObject b)
516 | {
517 | return LuaEvents.mod_event(a, b);
518 | }
519 |
520 | public static LuaObject operator ^(LuaObject a, LuaObject b)
521 | {
522 | return LuaEvents.pow_event(a, b);
523 | }
524 |
525 | public static LuaObject operator <(LuaObject a, LuaObject b)
526 | {
527 | return LuaEvents.lt_event(a, b);
528 | }
529 |
530 | public static LuaObject operator >(LuaObject a, LuaObject b)
531 | {
532 | return LuaEvents.lt_event(b, a);
533 | }
534 |
535 | public static LuaObject operator <=(LuaObject a, LuaObject b)
536 | {
537 | return LuaEvents.le_event(a, b);
538 | }
539 |
540 | public static LuaObject operator >=(LuaObject a, LuaObject b)
541 | {
542 | return LuaEvents.le_event(b, a);
543 | }
544 |
545 | public static bool operator ==(LuaObject a, object b)
546 | {
547 | if (b == null)
548 | return LuaObject.FromObject(a).IsNil;
549 |
550 | if (a.IsNil)
551 | {
552 | if (b == null)
553 | return true;
554 | else
555 | {
556 | if (b is LuaObject)
557 | return (b as LuaObject).IsNil;
558 | else
559 | return false;
560 | }
561 | }
562 | else
563 | {
564 | if (b == null)
565 | return false;
566 | else
567 | {
568 | if (b is LuaObject)
569 | {
570 | if ((b as LuaObject).IsNil)
571 | return a.IsNil;
572 |
573 | return (b as LuaObject).luaobj.Equals(a.luaobj);
574 | }
575 | else
576 | return a.luaobj.Equals(b);
577 | }
578 | }
579 | }
580 |
581 | public static bool operator !=(LuaObject a, object b)
582 | {
583 | return !(a == b);
584 | }
585 |
586 | public static LuaObject operator |(LuaObject a, object b)
587 | {
588 | if (a.IsNil || !a.AsBool())
589 | {
590 | if (b is LuaObject)
591 | return b as LuaObject;
592 | else
593 | return LuaObject.FromObject(b);
594 | }
595 | else
596 | return a;
597 | }
598 |
599 | public static LuaObject operator &(LuaObject a, object b)
600 | {
601 | if (a.IsNil || !a.AsBool())
602 | return a;
603 | else
604 | {
605 | if (b is LuaObject)
606 | return b as LuaObject;
607 | else
608 | return LuaObject.FromObject(b);
609 | }
610 | }
611 |
612 | public IEnumerator> GetEnumerator()
613 | {
614 | var table = luaobj as IEnumerable>;
615 | if (table == null)
616 | return null;
617 |
618 | return table.GetEnumerator();
619 | }
620 |
621 | IEnumerator IEnumerable.GetEnumerator()
622 | {
623 | return GetEnumerator();
624 | }
625 |
626 | public LuaObject this[LuaObject key]
627 | {
628 | get
629 | {
630 | if (IsTable)
631 | {
632 | var table = AsTable();
633 | LuaObject obj;
634 | if (table.TryGetValue(key, out obj))
635 | return obj;
636 | else
637 | return LuaEvents.index_event(this, key);
638 | }
639 | else
640 | return LuaEvents.index_event(this, key);
641 | }
642 | set
643 | {
644 | if (IsTable)
645 | {
646 | var table = AsTable();
647 | LuaObject obj;
648 | if (table.TryGetValue(key, out obj))
649 | table[key] = value;
650 | else
651 | LuaEvents.newindex_event(this, key, value);
652 | }
653 | else
654 | LuaEvents.newindex_event(this, key, value);
655 | }
656 | }
657 |
658 | // Unlike AsString, this will return string representations of nil, tables, and functions
659 | public override string ToString()
660 | {
661 | if (IsNil)
662 | return "nil";
663 |
664 | if (IsTable)
665 | return "table";
666 |
667 | if (IsFunction)
668 | return "function";
669 |
670 | if (IsBool)
671 | return luaobj.ToString().ToLower();
672 |
673 | return luaobj.ToString();
674 | }
675 |
676 | public override bool Equals(object obj)
677 | {
678 | if (obj is LuaObject)
679 | return luaobj.Equals((obj as LuaObject).luaobj);
680 | else
681 | return luaobj.Equals(obj);
682 | }
683 |
684 | public override int GetHashCode()
685 | {
686 | unchecked
687 | {
688 | return (luaobj != null ? luaobj.GetHashCode() : 0) ^ (byte)type;
689 | }
690 | }
691 |
692 | ///
693 | /// Calls the object passing the instance as first argument. Uses the metafield __call
694 | ///
695 | /// The object to be passed as first argument
696 | /// Arguments to be passed after the object
697 | public LuaArguments MethodCall(LuaObject instance, LuaArguments args)
698 | {
699 | var objs = new LuaObject[args.Length + 1];
700 | objs[0] = instance;
701 | for (int i = 0; i < args.Length; i++)
702 | {
703 | objs[i + 1] = args[i];
704 | }
705 |
706 | return this.Call(objs);
707 | }
708 |
709 | ///
710 | /// Calls the object. If this is not a function, it calls the metatable field __call
711 | ///
712 | /// The arguments to pass
713 | public LuaArguments Call(params LuaObject[] args)
714 | {
715 | return this.Call(new LuaArguments(args));
716 | }
717 |
718 | ///
719 | /// Calls the object. If this is not a function, it calls the metatable field __call
720 | ///
721 | /// The arguments to pass
722 | public LuaArguments Call(LuaArguments args)
723 | {
724 | return LuaEvents.call_event(this, args);
725 | }
726 |
727 | #region DynamicObject
728 |
729 | ///
730 | /// Gets a standard .NET value froma LuaObject
731 | ///
732 | /// The LuaObject is is a function or a table, its underlying luaobj if not
733 | internal static object getObject(LuaObject a)
734 | {
735 | if (a.Type != LuaType.table && a.Type != LuaType.function)
736 | return a.luaobj;
737 | else
738 | return a;
739 | }
740 |
741 | public override bool TryGetMember(GetMemberBinder binder, out object result)
742 | {
743 | result = null;
744 | LuaObject obj = this[binder.Name];
745 | if (obj.IsNil)
746 | return false;
747 | else
748 | {
749 | result = getObject(obj);
750 | return true;
751 | }
752 | }
753 |
754 | public override bool TrySetMember(SetMemberBinder binder, object value)
755 | {
756 | if (value is LuaObject)
757 | this[binder.Name] = (LuaObject)value;
758 | else
759 | this[binder.Name] = LuaObject.FromObject(value);
760 | return true;
761 | }
762 |
763 | public override bool TryInvoke(InvokeBinder binder, object[] args, out object result)
764 | {
765 | LuaObject[] passingArgs = Array.ConvertAll