1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Threading;
|
---|
4 | using UnityEngine;
|
---|
5 |
|
---|
6 | namespace AllocsFixes.NetConnections
|
---|
7 | {
|
---|
8 | public class ConsoleOutputSeparator
|
---|
9 | {
|
---|
10 | public struct NetCommand
|
---|
11 | {
|
---|
12 | public string command;
|
---|
13 | public IConnection client;
|
---|
14 |
|
---|
15 | public NetCommand (string _cmd, IConnection _client)
|
---|
16 | {
|
---|
17 | command = _cmd;
|
---|
18 | client = _client;
|
---|
19 | }
|
---|
20 | }
|
---|
21 |
|
---|
22 | private static List<NetCommand> netCommandQueue = new List<NetCommand> ();
|
---|
23 | private static bool isCurrentCommandFromClient = false;
|
---|
24 | private static int issuerOfCurrentClientCommand = -1;
|
---|
25 | private static IConnection issuerOfCurrentCommand;
|
---|
26 |
|
---|
27 | public static void C_ExecuteCmdFromClient (ConsoleSdtd console, int _entityId, string _playerName, string _playerID, string _command)
|
---|
28 | {
|
---|
29 | Log.Out ("Executed command \"" + _command + "\" from player \"" + _playerName + "\"");
|
---|
30 |
|
---|
31 | lock (netCommandQueue) {
|
---|
32 | isCurrentCommandFromClient = true;
|
---|
33 | issuerOfCurrentClientCommand = _entityId;
|
---|
34 |
|
---|
35 | string[] array = _command.Split (' ');
|
---|
36 | if (array.Length == 0) {
|
---|
37 | C_SendResult (console, "*** ERROR: empty command '" + _command + "'");
|
---|
38 | } else {
|
---|
39 | ConsoleCommand cmd = console.getCommand (_command);
|
---|
40 | if (cmd != null) {
|
---|
41 | string[] array2 = new string[array.Length - 1];
|
---|
42 | for (int i = 1; i < array.Length; i++) {
|
---|
43 | array2 [i - 1] = array [i];
|
---|
44 | }
|
---|
45 | cmd.ExecuteRemote (_playerID, array2);
|
---|
46 | } else {
|
---|
47 | C_SendResult (console, "*** ERROR: unknown command '" + array [0] + "'");
|
---|
48 | }
|
---|
49 | }
|
---|
50 |
|
---|
51 | isCurrentCommandFromClient = false;
|
---|
52 | }
|
---|
53 |
|
---|
54 | }
|
---|
55 |
|
---|
56 | public static void C_SendResult (ConsoleSdtd console, string _line)
|
---|
57 | {
|
---|
58 | if (isCurrentCommandFromClient) {
|
---|
59 | CommonMappingFunctions.GetConnectionManager ().SendPackage (new NetPackage_ConsoleCmdClient (_line, false), new PackageDestinationSingleEntityID (issuerOfCurrentClientCommand));
|
---|
60 | } else {
|
---|
61 | if (console.telnetServer != null && issuerOfCurrentCommand != null)
|
---|
62 | issuerOfCurrentCommand.SendLine (_line);
|
---|
63 | else if (ControlPanel.IsStarted ())
|
---|
64 | ControlPanel.AddTextToOutputBuffer (_line);
|
---|
65 | }
|
---|
66 | }
|
---|
67 |
|
---|
68 | public static void C_Run (ConsoleSdtd console)
|
---|
69 | {
|
---|
70 | if (netCommandQueue.Count > 0) {
|
---|
71 | lock (netCommandQueue) {
|
---|
72 | issuerOfCurrentCommand = netCommandQueue [0].client;
|
---|
73 | try {
|
---|
74 | console.ExecuteRemoteCmdInternal (netCommandQueue [0].command, false);
|
---|
75 | } catch (Exception e) {
|
---|
76 | Log.Out("Exception while executing command: " + e);
|
---|
77 | }
|
---|
78 | netCommandQueue.RemoveAt (0);
|
---|
79 | issuerOfCurrentCommand = null;
|
---|
80 | }
|
---|
81 | }
|
---|
82 | }
|
---|
83 |
|
---|
84 | public static void QueueNetCommand (string _line, IConnection _con)
|
---|
85 | {
|
---|
86 | lock (netCommandQueue) {
|
---|
87 | netCommandQueue.Add (new NetCommand (_line, _con));
|
---|
88 | }
|
---|
89 | }
|
---|
90 | }
|
---|
91 | }
|
---|