Client LuaCsForBarotrauma
LuaGame.cs
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Xml.Linq;
7 using FarseerPhysics.Dynamics;
8 using Microsoft.Xna.Framework;
9 using MoonSharp.Interpreter;
10 
11 namespace Barotrauma
12 {
13  partial class LuaGame
14  {
17  public string SaveFolder => string.IsNullOrEmpty(GameSettings.CurrentConfig.SavePath) ? SaveUtil.DefaultSaveFolder : GameSettings.CurrentConfig.SavePath;
18 
19 #if CLIENT
21  {
22  get
23  {
24  return GameMain.Client;
25  }
26  }
27 
28  public bool Paused => GameMain.Instance?.Paused == true;
30  public byte MyID => SessionId; // compatibility
31 
33 
35  {
36  get
37  {
40  else
41  return GameMain.Client.ChatBox;
42  }
43  }
44 
45  public Sounds.SoundManager SoundManager
46  {
47  get
48  {
49  return GameMain.SoundManager;
50  }
51  }
52 
53  public Lights.LightManager LightManager
54  {
55  get
56  {
57  return GameMain.LightManager;
58  }
59  }
60 
62  {
63  get
64  {
66  }
67  }
68 
70  {
71  get
72  {
73  return GameMain.MainMenuScreen;
74  }
75  }
76 
77  public Particles.ParticleManager ParticleManager
78  {
79  get
80  {
82  }
83  }
84 
85  public bool IsSubEditor
86  {
87  get
88  {
90  }
91  }
92 
93 #else
94  public GameServer Server
95  {
96  get
97  {
98  return GameMain.Server;
99  }
100  }
101 
102  public bool IsDedicated
103  {
104  get
105  {
106  return GameMain.Server.ServerPeer is LidgrenServerPeer;
107  }
108  }
109 
110  public bool Paused => false;
111 #endif
112 
114  {
115  get
116  {
117 #if SERVER
118  return GameMain.Server.ServerSettings;
119 #else
120  return GameMain.Client.ServerSettings;
121 #endif
122  }
123  }
124 
126  {
127  get
128  {
129 #if SERVER
130  return GameMain.Server.RespawnManager;
131 #else
132  return GameMain.Client.RespawnManager;
133 #endif
134  }
135  }
136 
137  public bool? ForceVoice = null;
138  public bool? ForceLocalVoice = null;
139 
140  public DynValue Settings;
141 
142  public bool allowWifiChat = false;
143  public bool overrideTraitors = false;
144  public bool overrideRespawnSub = false;
145  public bool overrideSignalRadio = false;
146  public bool disableSpamFilter = false;
147  public bool disableDisconnectCharacter = false;
148  public bool enableControlHusk = false;
149 
151  {
152  get { return MapEntity.MapEntityUpdateInterval; }
153  set { MapEntity.MapEntityUpdateInterval = value; }
154  }
155 
156  public int GapUpdateInterval
157  {
158  get { return 1; }
159  set { }
160  }
161 
163  {
164  get { return MapEntity.PoweredUpdateInterval; }
165  set { MapEntity.PoweredUpdateInterval = value; }
166  }
167 
169  {
170  get { return Character.CharacterUpdateInterval; }
171  set { Character.CharacterUpdateInterval = value; }
172  }
173 
174 
175  public HashSet<Item> UpdatePriorityItems = new HashSet<Item>();
176  public HashSet<Character> UpdatePriorityCharacters = new HashSet<Character>();
177 
178  public void AddPriorityItem(Item item)
179  {
180  UpdatePriorityItems.Add(item);
181  }
182 
183  public void RemovePriorityItem(Item item)
184  {
185  UpdatePriorityItems.Remove(item);
186  }
187 
188  public void ClearPriorityItem()
189  {
190  UpdatePriorityItems.Clear();
191  }
192 
193  public void AddPriorityCharacter(Character character)
194  {
195  UpdatePriorityCharacters.Add(character);
196  }
197 
198  public void RemovePriorityCharacter(Character character)
199  {
200  UpdatePriorityCharacters.Remove(character);
201  }
202 
204  {
205  UpdatePriorityCharacters.Clear();
206  }
207 
208  public bool RoundStarted
209  {
210 
211  get
212  {
214 #if SERVER
215  return GameMain.Server?.GameStarted == true;
216 #else
217  return GameMain.Client?.GameStarted == true;
218 #endif
219  }
220  }
221 
223  {
224  get
225  {
226  return GameMain.GameSession;
227  }
228  }
229 
231  {
232  get
233  {
234  return GameMain.NetLobbyScreen;
235  }
236  }
237 
239  {
240  get
241  {
242  return GameMain.GameScreen;
243  }
244  }
245 
246  public World World
247  {
248  get
249  {
250  return GameMain.World;
251  }
252  }
253 
254 #if SERVER
255  public ServerPeer Peer
256  {
257  get
258  {
259  return GameMain.Server.ServerPeer;
260  }
261  }
262 #else
263  public ClientPeer Peer
264  {
265  get
266  {
267  return GameMain.Client.ClientPeer;
268  }
269  }
270 #endif
271 
272  public LuaGame()
273  {
274  LuaUserData.MakeFieldAccessible(UserData.RegisterType(typeof(GameSettings)), "currentConfig");
275  Settings = UserData.CreateStatic(typeof(GameSettings));
276  }
277 
278  public void OverrideTraitors(bool o)
279  {
280  overrideTraitors = o;
281  }
282 
283  public void OverrideRespawnSub(bool o)
284  {
285  overrideRespawnSub = o;
286  }
287 
288  public void AllowWifiChat(bool o)
289  {
290  allowWifiChat = o;
291  }
292 
293  public void OverrideSignalRadio(bool o)
294  {
296  }
297 
298  public void DisableSpamFilter(bool o)
299  {
300  disableSpamFilter = o;
301  }
302 
303  public void DisableDisconnectCharacter(bool o)
304  {
306  }
307 
308 
309  public void EnableControlHusk(bool o)
310  {
311  enableControlHusk = o;
312  }
313 
314  public static void Explode(Vector2 pos, float range = 100, float force = 30, float damage = 30, float structureDamage = 30, float itemDamage = 30, float empStrength = 0, float ballastFloraStrength = 0)
315  {
316  new Explosion(range, force, damage, structureDamage, itemDamage, empStrength, ballastFloraStrength).Explode(pos, null);
317  }
318 
319  public static string SpawnItem(string name, Vector2 pos, bool inventory = false, Character character = null)
320  {
321  string error;
322  DebugConsole.SpawnItem(new string[] { name, inventory ? "inventory" : "cursor" }, pos, character, out error);
323  return error;
324  }
325 
327  {
328  return ContentPackageManager.EnabledPackages.All.ToArray();
329  }
330 
331  public static ItemPrefab GetItemPrefab(string itemNameOrId)
332  {
333  ItemPrefab itemPrefab =
334  (MapEntityPrefab.Find(itemNameOrId, identifier: null, showErrorMessages: false) ??
335  MapEntityPrefab.Find(null, identifier: itemNameOrId, showErrorMessages: false)) as ItemPrefab;
336 
337  return itemPrefab;
338  }
339 
340  public static Submarine GetRespawnSub()
341  {
342 #if SERVER
343  if (GameMain.Server.RespawnManager == null)
344  return null;
345  return GameMain.Server.RespawnManager.RespawnShuttle;
346 #else
347  if (GameMain.Client.RespawnManager == null)
348  return null;
349  return GameMain.Client.RespawnManager.RespawnShuttle;
350 #endif
351  }
352 
353  public static Items.Components.Steering GetSubmarineSteering(Submarine sub)
354  {
355  foreach (Item item in Item.ItemList)
356  {
357  if (item.Submarine != sub) continue;
358 
359  var steering = item.GetComponent<Items.Components.Steering>();
360  if (steering != null)
361  {
362  return steering;
363  }
364  }
365 
366  return null;
367  }
368 
369  public static WifiComponent GetWifiComponent(Item item)
370  {
371  if (item == null) return null;
372  return item.GetComponent<WifiComponent>();
373  }
374 
376  {
377  if (item == null) return null;
378  return item.GetComponent<LightComponent>();
379  }
380 
382  {
383  if (item == null) return null;
384  return item.GetComponent<CustomInterface>();
385  }
386 
388  {
389  if (item == null) return null;
390  return item.GetComponent<Fabricator>();
391  }
392 
393  public static Holdable GetHoldableComponent(Item item)
394  {
395  if (item == null) return null;
396  return item.GetComponent<Holdable>();
397  }
398 
399  public static void ExecuteCommand(string command)
400  {
401  DebugConsole.ExecuteCommand(command);
402  }
403 
404  public static Signal CreateSignal(string value, int stepsTaken = 1, Character sender = null, Item source = null, float power = 0, float strength = 1)
405  {
406  return new Signal(value, stepsTaken, sender, source, power, strength);
407  }
408 
409  private List<DebugConsole.Command> luaAddedCommand = new List<DebugConsole.Command>();
410  public IEnumerable<DebugConsole.Command> LuaAddedCommand { get { return luaAddedCommand; } }
411 
412  public bool IsCustomCommandPermitted(Identifier command)
413  {
414  DebugConsole.Command[] permitted = new DebugConsole.Command[]
415  {
416  DebugConsole.FindCommand("cl_reloadluacs"),
417  DebugConsole.FindCommand("cl_lua"),
418  DebugConsole.FindCommand("cl_toggleluadebug"),
419  };
420 
421  foreach (var consoleCommand in LuaAddedCommand.Concat(permitted.AsEnumerable()))
422  {
423  if (consoleCommand.Names.Contains(command))
424  {
425  return true;
426  }
427  }
428 
429  return false;
430  }
431 
432  public void RemoveCommand(string name)
433  {
434  for (var i = 0; i < DebugConsole.Commands.Count; i++)
435  {
436  foreach (var cmdname in DebugConsole.Commands[i].Names)
437  {
438  if (cmdname == name)
439  {
440  luaAddedCommand.Remove(DebugConsole.Commands[i]);
441  DebugConsole.Commands.RemoveAt(i);
442  continue;
443  }
444  }
445  }
446  }
447 
448  public void AddCommand(string name, string help, LuaCsAction onExecute, LuaCsFunc getValidArgs = null, bool isCheat = false)
449  {
450  var cmd = new DebugConsole.Command(name, help, (string[] arg1) => onExecute(new object[] { arg1 }),
451  () =>
452  {
453  if (getValidArgs == null) return null;
454  var validArgs = getValidArgs();
455  if (validArgs is DynValue luaValue)
456  {
457  return luaValue.ToObject<string[][]>();
458  }
459  return (string[][])validArgs;
460  }, isCheat);
461 
462  luaAddedCommand.Add(cmd);
463  DebugConsole.Commands.Add(cmd);
464  }
465 
466  public List<DebugConsole.Command> Commands => DebugConsole.Commands;
467 
468  public void AssignOnExecute(string names, object onExecute) => DebugConsole.AssignOnExecute(names, (string[] a) => { GameMain.LuaCs.CallLuaFunction(onExecute, new object[] { a }); });
469 
470  public void SaveGame(string path)
471  {
472  if (!LuaCsFile.CanWriteToPath(path)) { throw new ScriptRuntimeException($"Saving files to {path} is disallowed."); }
473  SaveUtil.SaveGame(path);
474  }
475 
476  public void LoadGame(string path)
477  {
478  SaveUtil.LoadGame(path);
479  }
480 
481 #if SERVER
482  public void LoadCampaign(string path, Client client = null)
483  {
484  MultiPlayerCampaign.LoadCampaign(path, client);
485  }
486 
487  public static void SendMessage(string msg, ChatMessageType? messageType = null, Client sender = null, Character character = null)
488  {
489  GameMain.Server.SendChatMessage(msg, messageType, sender, character);
490  }
491 
492  public static void SendTraitorMessage(WriteOnlyMessage message, Client client)
493  {
494  GameMain.Server.SendTraitorMessage(message, client);
495  }
496 
497  public static void SendDirectChatMessage(string sendername, string text, Character sender, ChatMessageType messageType = ChatMessageType.Private, Client client = null, string iconStyle = "")
498  {
499  ChatMessage cm = ChatMessage.Create(sendername, text, messageType, sender);
500  cm.IconStyle = iconStyle;
501  GameMain.Server.SendDirectChatMessage(cm, client);
502  }
503 
504  public static void SendDirectChatMessage(ChatMessage chatMessage, Client client)
505  {
506  GameMain.Server.SendDirectChatMessage(chatMessage, client);
507  }
508 
509  public static void Log(string message, ServerLog.MessageType type)
510  {
511  GameServer.Log(message, type);
512  }
513 
514  public static void DispatchRespawnSub()
515  {
516  GameMain.Server.RespawnManager.DispatchShuttle();
517  }
518 
519  public static bool StartGame()
520  {
521  return GameMain.Server.TryStartGame();
522  }
523 
524  public static void EndGame()
525  {
526  GameMain.Server.EndGame();
527  }
528 
529  public void AssignOnClientRequestExecute(string names, object onExecute) => DebugConsole.AssignOnClientRequestExecute(names, (Client a, Vector2 b, string[] c) => { GameMain.LuaCs.CallLuaFunction(onExecute, new object[] { a, b, c }); });
530 
531 #endif
532 
533  public void Stop()
534  {
537 
538  foreach (var cmd in luaAddedCommand)
539  {
540  DebugConsole.Commands.Remove(cmd);
541  }
542  }
543  }
544 
545 }
ChatBox ChatBox
Present only in single player games. In multiplayer. The chatbox is found from GameSession....
Submarine Submarine
Definition: Entity.cs:53
Explosions are area of effect attacks that can damage characters, items and structures.
void Explode(Vector2 worldPosition, Entity damageSource, Character attacker=null)
static GameSession?? GameSession
Definition: GameMain.cs:88
static NetLobbyScreen NetLobbyScreen
Definition: GameMain.cs:55
static SubEditorScreen SubEditorScreen
Definition: GameMain.cs:68
static Lights.LightManager LightManager
Definition: GameMain.cs:78
static World World
Definition: GameMain.cs:105
static ChatMode ActiveChatMode
Definition: GameMain.cs:218
static bool IsMultiplayer
Definition: GameMain.cs:35
static bool IsSingleplayer
Definition: GameMain.cs:34
static GameScreen GameScreen
Definition: GameMain.cs:52
static MainMenuScreen MainMenuScreen
Definition: GameMain.cs:53
static ParticleManager ParticleManager
Definition: GameMain.cs:101
static GameClient Client
Definition: GameMain.cs:188
static GameMain Instance
Definition: GameMain.cs:144
static LuaCsSetup LuaCs
Definition: GameMain.cs:26
static Sounds.SoundManager SoundManager
Definition: GameMain.cs:80
static readonly List< Item > ItemList
static bool CanWriteToPath(string path)
Definition: LuaCsUtility.cs:52
DynValue CallLuaFunction(object function, params object[] args)
static Items.Components.Steering GetSubmarineSteering(Submarine sub)
Definition: LuaGame.cs:353
bool IsMultiplayer
Definition: LuaGame.cs:16
DynValue Settings
Definition: LuaGame.cs:140
void RemovePriorityItem(Item item)
Definition: LuaGame.cs:183
bool disableDisconnectCharacter
Definition: LuaGame.cs:147
bool? RoundStarted
Definition: LuaGame.cs:209
int MapEntityUpdateInterval
Definition: LuaGame.cs:151
Lights.LightManager LightManager
Definition: LuaGame.cs:54
static string SpawnItem(string name, Vector2 pos, bool inventory=false, Character character=null)
Definition: LuaGame.cs:319
Sounds.SoundManager SoundManager
Definition: LuaGame.cs:46
void OverrideTraitors(bool o)
Definition: LuaGame.cs:278
Particles.ParticleManager ParticleManager
Definition: LuaGame.cs:78
ChatMode ActiveChatMode
Definition: LuaGame.cs:32
string SaveFolder
Definition: LuaGame.cs:17
void EnableControlHusk(bool o)
Definition: LuaGame.cs:309
void OverrideSignalRadio(bool o)
Definition: LuaGame.cs:293
SubEditorScreen SubEditorScreen
Definition: LuaGame.cs:62
void SaveGame(string path)
Definition: LuaGame.cs:470
void DisableSpamFilter(bool o)
Definition: LuaGame.cs:298
List< DebugConsole.Command > Commands
Definition: LuaGame.cs:466
int CharacterUpdateInterval
Definition: LuaGame.cs:169
void AddPriorityItem(Item item)
Definition: LuaGame.cs:178
int PoweredUpdateInterval
Definition: LuaGame.cs:163
bool IsSingleplayer
Definition: LuaGame.cs:15
void AllowWifiChat(bool o)
Definition: LuaGame.cs:288
bool overrideRespawnSub
Definition: LuaGame.cs:144
static void Explode(Vector2 pos, float range=100, float force=30, float damage=30, float structureDamage=30, float itemDamage=30, float empStrength=0, float ballastFloraStrength=0)
Definition: LuaGame.cs:314
void RemoveCommand(string name)
Definition: LuaGame.cs:432
void DisableDisconnectCharacter(bool o)
Definition: LuaGame.cs:303
void ClearPriorityCharacter()
Definition: LuaGame.cs:203
IEnumerable< DebugConsole.Command > LuaAddedCommand
Definition: LuaGame.cs:410
static Holdable GetHoldableComponent(Item item)
Definition: LuaGame.cs:393
bool IsCustomCommandPermitted(Identifier command)
Definition: LuaGame.cs:412
static LightComponent GetLightComponent(Item item)
Definition: LuaGame.cs:375
HashSet< Item > UpdatePriorityItems
Definition: LuaGame.cs:175
void AssignOnExecute(string names, object onExecute)
void OverrideRespawnSub(bool o)
Definition: LuaGame.cs:283
static ContentPackage[] GetEnabledContentPackages()
Definition: LuaGame.cs:326
ClientPeer Peer
Definition: LuaGame.cs:264
static Signal CreateSignal(string value, int stepsTaken=1, Character sender=null, Item source=null, float power=0, float strength=1)
Definition: LuaGame.cs:404
bool enableControlHusk
Definition: LuaGame.cs:148
static Submarine GetRespawnSub()
Definition: LuaGame.cs:340
static void ExecuteCommand(string command)
Definition: LuaGame.cs:399
void RemovePriorityCharacter(Character character)
Definition: LuaGame.cs:198
static CustomInterface GetCustomInterface(Item item)
Definition: LuaGame.cs:381
bool disableSpamFilter
Definition: LuaGame.cs:146
bool overrideSignalRadio
Definition: LuaGame.cs:145
static Fabricator GetFabricatorComponent(Item item)
Definition: LuaGame.cs:387
void ClearPriorityItem()
Definition: LuaGame.cs:188
static ItemPrefab GetItemPrefab(string itemNameOrId)
Definition: LuaGame.cs:331
static WifiComponent GetWifiComponent(Item item)
Definition: LuaGame.cs:369
bool? ForceLocalVoice
Definition: LuaGame.cs:138
void AddCommand(string name, string help, LuaCsAction onExecute, LuaCsFunc getValidArgs=null, bool isCheat=false)
Definition: LuaGame.cs:448
void AddPriorityCharacter(Character character)
Definition: LuaGame.cs:193
void LoadGame(string path)
Definition: LuaGame.cs:476
HashSet< Character > UpdatePriorityCharacters
Definition: LuaGame.cs:176
bool overrideTraitors
Definition: LuaGame.cs:143
static void MakeFieldAccessible(IUserDataDescriptor IUUD, string fieldName)
Definition: LuaUserData.cs:129
static MapEntityPrefab Find(string name, string identifier=null, bool showErrorMessages=true)
Find a matching map entity prefab
static ChatMessage Create(string senderName, string text, ChatMessageType type, Entity sender, Client client=null, PlayerConnectionChangeType changeType=PlayerConnectionChangeType.None, Color? textColor=null)
delegate void LuaCsAction(params object[] args)
delegate object LuaCsFunc(params object[] args)