Tanoda
LUAScriptingManager.cs
Go to the documentation of this file.
1using GILES;
2using MoonSharp.Interpreter;
3using NaughtyAttributes;
4using System;
5using System.Collections;
6using System.Collections.Generic;
7using System.IO;
8using System.Linq;
9using System.Threading;
10using UnityEngine;
11using UnityEngine.UI;
12
13public class LUAScriptingManager : MonoBehaviour
14{
15#if DANA
16 public static LUAScriptingManager instance;
17
18 public InputField ScriptField;
19 public Text DebugOutputText;
20
21 private static readonly object LockObject = new object();
22 private static readonly Queue<Action> Actions = new Queue<Action>();
23
24 string scriptCode = @"
25 speak('teszt')
26 findGlobalGO('kocsi')
27 findGlobalGO('kocsi', true)
28 debugLog('szia')
29 -- sleep(2500)
30 debugLog('mizu')
31 -- speak('szia, mizu?', 'hu-HU')
32 -- cube = findGO('CUBE')
33 -- cube.transform.position = Vector3(0,2,0)
34 -- blokk = findGO(""C197496.fbx"")
35 -- alatetArray = findAllGO(""1x1_trilib.FBX"")
36 -- for _, alatet in ipairs(alatetArray) do
37 -- setParent(alatet, blokk.transform)
38 -- end
39 -- teszt = findGlobalGO(""kocsi"")
40 -- teszt.transform.LookAt(Vector3(0,0,0))
41 -- dkey = findGO(""desoutter_key.FBX"")
42 -- setStatic(dkey, true)
43 -- return 'ok'";
44
45 void Start()
46 {
47 instance = this;
48 if (File.Exists(Path.Combine(Application.streamingAssetsPath, "startup.lua")))
49 {
50 var startup = File.ReadAllText(Path.Combine(Application.streamingAssetsPath, "startup.lua"));
51
52 Script script = NewScript();
53 script.DoString(startup);
54 }
55 }
56
57 public void RunScriptFromFile(string file)
58 {
59 if (File.Exists(Path.Combine(Application.streamingAssetsPath, "scripts", file)))
60 {
61 var run = File.ReadAllText(Path.Combine(Application.streamingAssetsPath, "scripts", file));
62
63 Script script = NewScript();
64 script.DoString(run);
65 }
66 else
67 {
68 Debug.LogWarning("RunScriptFromFile: '" + file + "' does not exists!");
69 }
70 }
71
72 public void RunScriptFromInput()
73 {
74 try
75 {
76 var script = NewScript();
77 var retval = script.DoString(ScriptField.text);
78 if (retval.IsNilOrNan())
79 {
80 DebugOutputText.text = "";
81 }
82 else
83 switch (retval.Type)
84 {
85 case DataType.Boolean:
86 DebugOutputText.text = retval.Boolean.ToString();
87 break;
88 case DataType.Number:
89 DebugOutputText.text = retval.Number.ToString();
90 break;
91 case DataType.String:
92 DebugOutputText.text = retval.String;
93 break;
94 case DataType.Void:
95 DebugOutputText.text = "";
96 break;
97 }
98
99 }
100 catch (Exception e)
101 {
102 if (!DebugOutputText)
103 return;
104
105 DebugOutputText.text = e.Message;
106 }
107 }
108
109 [Button]
110 void Teszt()
111 {
112 Script script = NewScript();
113 script.DoString(scriptCode);
114 //DynValue res = script.DoString(scriptCode);
115 //Debug.Log(res.String);
116 }
117 [Button]
118 void Teszt2()
119 {
120 //new Thread(() =>
121 //{
122 // Script script = NewScript();
123 // script.DoString(scriptCode);
124 //
125 //}).Start();
126
127 Script script = NewScript();
128 script.DoString("delayAction('debugLog(\"hello, ez egy delay után volt\")', 3)");
129 }
130
131 private void SetPos(GameObject o, float x, float y, float z)
132 {
133 SetPos(o, new Vector3(x, y, z));
134 }
135
136 private void SetPos(GameObject o, Vector3 p)
137 {
138 o.transform.position = p;
139 }
140 private void SetLocalPos(GameObject o, Vector3 p)
141 {
142 o.transform.localPosition = p;
143 }
144
145 private void SetRot(GameObject o, Vector3 p)
146 {
147 o.transform.eulerAngles = p;
148 }
149 private void SetLocalRot(GameObject o, Vector3 p)
150 {
151 o.transform.localEulerAngles = p;
152 }
153
154 private void SetEnabled(GameObject o, bool v)
155 {
156 o?.SetActive(v);
157 }
158
159 private Vector3 NewVector3(float x, float y, float z)
160 {
161 return new Vector3(x, y, z);
162 }
163
164 public Script NewScript()
165 {
166 Script script = new Script();
167
168 UserData.RegisterType<GameObject>();
169 UserData.RegisterType<Vector3>();
170 UserData.RegisterType<Transform>();
171 UserData.RegisterType<Text>();
172 UserData.RegisterType<ThrowableCanDisable>();
173 UserData.RegisterType<pb_Selection>();
174 script.Globals["findGO"] = (Func<string, GameObject>)FindGO;
175 script.Globals["findGlobalGO"] = (Func<string, bool, GameObject>)findGlobal;
176 //script.Globals["findGlobalGO"] = (Func<string, GameObject>)findGlobal;
177 script.Globals["findAllGO"] = (Func<string, List<GameObject>>)FindAllGO;
178 script.Globals["spawn"] = (Func<GameObject, GameObject>)Instantiate;
179 script.Globals["destroy"] = (Action<UnityEngine.Object>)Destroy;
180 //script.Globals["setPos"] = (Action<GameObject, float, float, float>) SetPos;
181 script.Globals["Vector3"] = (Func<float, float, float, Vector3>)NewVector3;
182 script.Globals["setPos"] = (Action<GameObject, Vector3>)SetPos;
183 script.Globals["setLocalPos"] = (Action<GameObject, Vector3>)SetLocalPos;
184 script.Globals["setRot"] = (Action<GameObject, Vector3>)SetRot;
185 script.Globals["setLocalRot"] = (Action<GameObject, Vector3>)SetLocalRot;
186 script.Globals["debugLog"] = (Action<string>)Debug.Log;
187 script.Globals["speak"] = (Action<string, string>)Speak;
188 script.Globals["sleep"] = (Action<int>)Thread.Sleep;
189 script.Globals["setEnabled"] = (Action<GameObject, bool>)SetEnabled;
190 script.Globals["setStatic"] = (Action<GameObject, bool>)SetStatic;
191 script.Globals["lerp"] = (Action<GameObject, Vector3, Vector3, float>)LerpPos;
192 script.Globals["setParent"] = (Action<GameObject, Transform>)SetParent;
193 script.Globals["getInteractive"] = (Func<GameObject, ThrowableCanDisable>)GetTCD;
194 script.Globals["selectPlayer"] = (Action)SelectPlayer;
195 script.Globals["SetSelection"] = (Action<GameObject>)pb_Selection.SetSelection;
196 script.Globals["resetCleco"] = (Action<bool>)ManipulatorArm.instance.ResetCleco;
197 script.Globals["setComponent"] = (Action<GameObject, string, bool>)ActivateComponent;
198 script.Globals["delayAction"] = (Action<string, float>)DelayAction;
199 script.Globals["replay"] = (Action)ReplayLogic.instance.ReplayLevel;
200 script.Globals["start"] = (Action)LevelEditorOnLoaded.instance.StartStages;
201
202 return script;
203 }
204
205 private GameObject findGlobal(string goName, bool includeInactive = false)
206 {
207 GameObject go = null;
208 try
209 {
210 go = pb_Scene.instance.transform.GetComponentsInChildren<Transform>(includeInactive).Where(obj => obj.name == goName).Select(x => x.gameObject).ToList()[0].gameObject;
211 }
212 catch (Exception)
213 {
214 // ignored
215 }
216 if (go)
217 return go;
218
219 try
220 {
221 go = GameObject.Find("Environment").GetComponentsInChildren<Transform>(includeInactive).Where(obj => obj.name == goName).Select(x => x.gameObject).ToList()[0].gameObject;
222 }
223 catch (Exception)
224 {
225 // ignored
226 }
227
228 if (go)
229 return go;
230
231 foreach (var tool in ToolPositioner.instance.tools)
232 {
233 if (!tool) continue;
234 try
235 {
236 go = tool.GetComponentsInChildren<Transform>(includeInactive).Where(obj => obj.name == goName).Select(x => x.gameObject).ToList()[0].gameObject;
237 }
238 catch (Exception)
239 {
240 // ignored
241 }
242
243 if (go)
244 return go;
245 }
246
247 if (includeInactive)
248 {
249 try
250 {
251 go = GameObject.FindObjectsOfType<GameObject>(includeInactive).Where(obj => obj.name == goName).FirstOrDefault();
252 }
253 catch (Exception)
254 {
255 // ignored
256 }
257 return go;
258 }
259 else
260 return GameObject.Find(goName);
261 }
262
263 private void Speak(string text, string lang = "hu-HU")
264 {
265 VoiceTTS.Speak(text, lang);
266 }
267
268 private void DelayAction(string action, float delay)
269 {
270 StartCoroutine(this.delay(action, delay));
271 }
272
273 private IEnumerator delay(string a, float t)
274 {
275 yield return new WaitForSeconds(t);
276
277 Script script = NewScript();
278 script.DoString(a);
279 }
280
281 private void ActivateComponent(GameObject go, string s, bool b)
282 {
283 try
284 {
285 var cmp = go.GetComponent(s) as MonoBehaviour;
286 if (cmp)
287 {
288 cmp.enabled = b;
289 }
290 }
291 catch (Exception)
292 {
293 // ignored
294 }
295 }
296
297 private void SelectPlayer()
298 {
299 pb_Selection.SetSelection(GameObject.Find("Player"));
300 }
301
302 private ThrowableCanDisable GetTCD(GameObject go)
303 {
304 if (go && go.GetComponentInChildren<ThrowableCanDisable>())
305 {
306 return go.GetComponentInChildren<ThrowableCanDisable>();
307 }
308 else
309 {
310 return null;
311 }
312 }
313
314 private List<GameObject> FindAllGO(string name)
315 {
316 return pb_Scene.instance.transform.GetComponentsInChildren<Transform>().Where(obj => obj.name == name).Select(x => x.gameObject).ToList();
317 }
318
319
320 private GameObject FindGO(string name)
321 {
322 try
323 {
324 return pb_Scene.instance.transform.GetComponentsInChildren<Transform>().Where(obj => obj.name == name).Select(x => x.gameObject).ToList()[0].gameObject;
325 }
326 catch (Exception)
327 {
328 return null;
329 }
330 }
331
332 private void SetStatic(GameObject go, bool value)
333 {
334 if (value)
335 Macro.SetStatic(go);
336 else
338 }
339
340 private void SetParent(GameObject go, Transform p)
341 {
342 go.transform.SetParent(p, true);
343 }
344
345
346 private void LerpPos(GameObject o, Vector3 p, Vector3 r, float t)
347 {
348 StartCoroutine(LerpTransform(o, p, r, t));
349 }
350
351 IEnumerator LerpTransform(GameObject o, Vector3 p, Vector3 r, float t)
352 {
353
354 for (float i = 0; i <= t; i += 0.01f)
355 {
356
357 o.transform.position = Vector3.Lerp(o.transform.position, p, i / t);
358 o.transform.localRotation = Quaternion.Lerp(o.transform.localRotation, Quaternion.Euler(r), i / t);
359 yield return new WaitForSeconds(0.01f);
360 }
361 }
362
367 public static void InvokeAsync(Action action)
368 {
369 lock (LockObject)
370 {
371 Actions.Enqueue(action);
372 }
373 }
374
375 void Update()
376 {
377 lock (LockObject)
378 {
379 while (Actions.Count > 0)
380 {
381 Actions.Dequeue()();
382 }
383 }
384 }
385#endif
386}
UnityEngine.UI.Button Button
Definition: Pointer.cs:7
UnityEngine.Debug Debug
Definition: TanodaServer.cs:19
static void SetSelection(IEnumerable< GameObject > selection)
Definition: pb_Selection.cs:92
static LevelEditorOnLoaded instance
Definition: Macro.cs:12
static void SetStatic(GameObject go)
Definition: Macro.cs:388
static void SetInteractable(GameObject go, bool isKinematic=true)
Definition: Macro.cs:439
void ResetCleco(bool disable=false)
static ManipulatorArm instance
GameObject[] tools
static ToolPositioner instance