]> Dogcows Code - chaz/carfire/blob - CarFire/CarFire/CarFire/Script.cs
146bb22685249cf7db7449c521a1cbe644aef819
[chaz/carfire] / CarFire / CarFire / CarFire / Script.cs
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Reflection;
6 using System.Diagnostics;
7
8 namespace CarFire
9 {
10 public class Script
11 {
12 #region Public Properties
13
14 public bool IsRunning { get { return mIsRunning; } }
15
16 #endregion
17
18
19 #region Public Methods
20
21 public Script(string code, Game game)
22 {
23 mGame = game;
24
25 string[] functions = Parse.List(code);
26 if (functions != null)
27 {
28 foreach (string function in functions)
29 {
30 string[] parts = Parse.Function(function);
31 if (parts != null)
32 {
33 string[] args = Parse.List(parts[1]);
34 if (args != null)
35 {
36 Function func = new Function(parts[0], args);
37 mFunctions.Add(func);
38 }
39 else throw new Exception("Arguments could not be parsed: " + parts[1]);
40 }
41 else throw new Exception("Function could not be parsed: " + function);
42 }
43 }
44 else throw new Exception("Script could not be parsed: " + code);
45 }
46
47 public bool Run(Player player)
48 {
49 bool result = false;
50
51 if (!mIsRunning)
52 {
53 mIsRunning = true;
54 mRunningIndex = 0;
55 }
56
57 for (; mRunningIndex < mFunctions.Count; mRunningIndex++)
58 {
59 result = Call(mRunningIndex, player);
60 if (!result) break;
61 }
62
63 if (mRunningIndex >= mFunctions.Count - 1) mIsRunning = false;
64 return result;
65 }
66
67 #endregion
68
69
70 #region Private Methods
71
72 bool Call(int index, Player player)
73 {
74 Debug.Assert(0 <= index && index < mFunctions.Count);
75 try
76 {
77 object[] args = new object[2];
78 args[0] = player;
79 args[1] = mFunctions[index].Arguments;
80 return (bool)typeof(Impl).InvokeMember(mFunctions[index].Name, BindingFlags.InvokeMethod, null, null, args);
81 }
82 #pragma warning disable 0168
83 catch (System.MissingMethodException ex)
84 #pragma warning restore 0168
85 {
86 throw new Exception("Function could not be found: " + mFunctions[index].Name);
87 }
88 }
89
90 #endregion
91
92
93 #region Private Types
94
95 class Impl
96 {
97 public static bool True(Player player, string[] args)
98 {
99 return true;
100 }
101
102 public static bool False(Player player, string[] args)
103 {
104 return false;
105 }
106
107 public static bool Has(Player player, string[] args)
108 {
109 return false;
110 }
111
112 public static bool Print(Player player, string[] args)
113 {
114 foreach (string arg in args)
115 {
116 string line = Parse.String(arg);
117 if (line != null) Console.WriteLine(line);
118 }
119 return true;
120 }
121 }
122
123 class Function
124 {
125 public string Name { get { return mName; } }
126 public string[] Arguments { get { return mArgs; } }
127
128 public Function(string name, string[] args)
129 {
130 mName = name;
131 mArgs = args;
132 }
133
134 string mName;
135 string[] mArgs;
136 }
137
138 #endregion
139
140
141 #region Private Variables
142
143 Game mGame;
144 List<Function> mFunctions = new List<Function>();
145 bool mIsRunning;
146 int mRunningIndex;
147 Impl mImpl = new Impl();
148
149 #endregion
150 }
151 }
This page took 0.037025 seconds and 3 git commands to generate.