Exemplo n.º 1
0
        public static void Action(IList args, IMethodContext context)
        {
            ImplLogger.LogImpl("action", args);

            MatchingType type   = (MatchingType)args[0];
            string       filter = (string)args[1];
            string       action = (string)args[2];

            List <IMyTerminalBlock> blocks = new List <IMyTerminalBlock>();

            BlockSelector.GetBlocksOfTypeWithQuery(type, filter, blocks);
            ImplLogger.LogBlocks(blocks);


            foreach (var block in blocks)
            {
                if (block.HasAction(action))
                {
                    block.ApplyAction(action);
                }
                else
                {
                    Log.WriteFormat(ImplLogger.LOG_CAT, LogLevel.Warning, "block \"{0}\" does not support action \"{1}\", ignoring", block.CustomName, action);
                }
            }
        }
Exemplo n.º 2
0
        public static void Data(IList args, IMethodContext context)
        {
            ImplLogger.LogImpl("customdata", args);

            var type      = (MatchingType)args[0];
            var filter    = (string)args[1];
            var append    = (bool)args[2];
            var permision = (DataPermision)args[3];
            var value     = (string)args[4];

            List <IMyTerminalBlock> blocks = new List <IMyTerminalBlock>();

            BlockSelector.GetBlocksOfTypeWithQuery(type, filter, blocks);
            ImplLogger.LogBlocks(blocks);


            foreach (var block in blocks)
            {
                if (block is IMyProgrammableBlock && permision < DataPermision.AllowPB)
                {
                    Log.Write(ImplLogger.LOG_CAT, LogLevel.Error, "permition AllowPB required to overwrite programmable block's data");
                    Log.Write(ImplLogger.LOG_CAT, LogLevel.Error, $"skipping \"{block.CustomName}\"");
                    continue;
                }
                if (block == Program.Current.Me && permision < DataPermision.AllowSelf)
                {
                    Log.Write(ImplLogger.LOG_CAT, LogLevel.Error, "permition AllowSelf required to overwrite user script");
                    Log.Write(ImplLogger.LOG_CAT, LogLevel.Error, $"skipping \"{block.CustomName}\" (this is me)");
                    continue;
                }

                block.CustomData = append ? (block.CustomData + value) : value;
            }
        }
Exemplo n.º 3
0
        private static void ListBlocks(IList args, IMethodContext context)
        {
            ImplLogger.LogImpl("listblocks", args);

            MatchingType type   = (MatchingType)args[0];
            string       filter = (string)args[1];

            List <IMyTerminalBlock> blocks = new List <IMyTerminalBlock>();

            BlockSelector.GetBlocksOfTypeWithQuery(type, filter, blocks);
            ImplLogger.LogBlocks(blocks);


            Log.Write("TypeName/SubtypeName \"Name\" [IntityId]");
            Log.WriteLine();
            foreach (var block in blocks)
            {
                Log.WriteFormat("{0}/{1} \"{2}\" [{3}]", new object[] {
                    block.GetType().Name,
                    block.BlockDefinition.SubtypeName,
                    block.CustomName,
                    block.EntityId
                });
            }
        }
Exemplo n.º 4
0
        internal static void ListActions(IList args, IMethodContext context)
        {
            ImplLogger.LogImpl("listactions", args);

            MatchingType type   = (MatchingType)args[0];
            string       filter = (string)args[1];

            List <IMyTerminalBlock> blocks = new List <IMyTerminalBlock>();

            BlockSelector.GetBlocksOfTypeWithQuery(type, filter, blocks);
            ImplLogger.LogBlocks(blocks);

            List <ITerminalAction> actions = new List <ITerminalAction>();

            foreach (var block in blocks)
            {
                Log.WriteFormat("block \"{0}\" of type \"{1}\" have actions:", new object[] { block.CustomName, block.GetType().Name });
                actions.Clear();
                block.GetActions(actions);

                foreach (var action in actions)
                {
                    Log.WriteFormat("\"{0}\": {1}", new object[] { action.Id, action.Name });
                }
                Log.WriteLine();
            }
        }
Exemplo n.º 5
0
        internal static void Test3(IList args, IMethodContext context)
        {
            ImplLogger.LogImpl("test3", args);

            MatchingType type   = (MatchingType)args[0];
            string       filter = (string)args[1];
            //           string prop = (string)args[2];

            var blocks = new List <IMyTerminalBlock>();

            BlockSelector.GetBlocksOfTypeWithQuery(type, filter, blocks);
            ImplLogger.LogBlocks(blocks);

            Log.Write("TypeName/SubtypeName \"Name\" [IntityId]");
            Log.WriteLine();
            foreach (var block in blocks)
            {
                var propDef = block.GetProperty("FontColor");

                Log.WriteFormat("name as sb {0}", propDef.As <Color>());
                Log.WriteFormat("name as int {0}", propDef.As <int>());
                Log.WriteFormat("existing prop {0}", block.GetProperty("FontColor"));
                Log.WriteFormat("unexisting prop {0}", block.GetProperty("someunexistingproperty"));
            }
        }
Exemplo n.º 6
0
        public static void RemoveInput(IList args, IMethodContext context)
        {
            ImplLogger.LogImpl("cm_removeInput", args);
            var action = (string)args[0];
            var _event = (InputEvent?)args[1];
            var method = (string)args[2];

            CMMapper.Shared.Remove(action, _event, method);
        }
Exemplo n.º 7
0
        public static void Switch(IList args, IMethodContext context)
        {
            ImplLogger.LogImpl("switch", args);
            int   var   = (int)(context.Get((string)args[0]));
            IList cases = (IList)args[1];

            if (var < 0 || var >= cases.Count)
            {
                Log.WriteFormat(ImplLogger.LOG_CAT, LogLevel.Warning, "Value {0} is out of bounds (0..{1})", var, cases.Count);
            }
            else
            {
                context.Runtime.StartProgram((string)cases[var]);
            }
        }
Exemplo n.º 8
0
        internal static void Load(IList args, IMethodContext context)
        {
            ImplLogger.LogImpl("load", args);
            Parser parser = new Parser((string)args[0]);

            parser.Parse(() => false);

            if (parser.Finalize())
            {
                context.Runtime.RegisterPrograms(parser.Programs);
            }
            else
            {
                Log.WriteFormat(ImplLogger.LOG_CAT, LogLevel.Warning, "exception during parsing: {0}", parser.ErrorMessage);
            }
        }
Exemplo n.º 9
0
        public static void Run(IList args, IMethodContext context)
        {
            ImplLogger.LogImpl("run", args);

            MatchingType type     = (MatchingType)args[0];
            string       filter   = (string)args[1];
            string       argument = (string)args[2];

            List <IMyProgrammableBlock> blocks = new List <IMyProgrammableBlock>();

            BlockSelector.GetBlocksOfTypeWithQuery(type, filter, blocks);
            ImplLogger.LogBlocks(blocks);

            foreach (var block in blocks)
            {
                block.TryRun(argument);
            }
        }
Exemplo n.º 10
0
        //        static blocks

        internal static void Test1(IList args, IMethodContext context)
        {
            ImplLogger.LogImpl("test1", args);

            MatchingType type   = (MatchingType)args[0];
            string       filter = (string)args[1];

            List <IMyTextSurfaceProvider> blocks = new List <IMyTextSurfaceProvider>();

            BlockSelector.GetBlocksOfTypeWithQuery(type, filter, blocks);
            ImplLogger.LogBlocks(blocks);


            Log.Write("TypeName/SubtypeName \"Name\" [IntityId]");
            Log.WriteLine();
            foreach (var lcd in blocks)
            {
                var block = lcd as IMyTerminalBlock;
                Log.Write($"{block.GetType().Name}/{block.BlockDefinition.SubtypeName} \"{block.CustomName}\" [{block.EntityId}]");
                Log.WriteFormat("surfaces count: {0}", lcd.SurfaceCount);
            }
        }
Exemplo n.º 11
0
        internal static void LogLevel_(IList args, IMethodContext context)
        {
            ImplLogger.LogImpl("loglevel", args);

            string cat   = (string)args[0];
            double level = (double)args[1];

            if (string.IsNullOrEmpty(cat))
            {
                Log.Write("LogLevels:");
                foreach (var kvp in Log.LogLevels)
                {
                    Log.WriteFormat("{0} : {1}", kvp.Key, kvp.Value);
                }
            }
            else
            {
                if (level < 0)
                {
                    Log.WriteFormat("LogLevel for \"{0}\": {1}", cat, Log.LogLevels.ContainsKey(cat) ? Log.LogLevels[cat] : LogLevel.None);
                }
                else
                {
                    if (cat.Equals("all", StringComparison.InvariantCultureIgnoreCase))
                    {
                        foreach (var key in new List <string>(Log.LogLevels.Keys))
                        {
                            Log.LogLevels[key] = (LogLevel)level;
                        }
                    }
                    else
                    {
                        Log.LogLevels[cat] = (LogLevel)level;
                    }
                }
            }
        }
Exemplo n.º 12
0
        public static void Text(IList args, IMethodContext context)
        {
            ImplLogger.LogImpl("text", args);

            MatchingType type   = (MatchingType)args[0];
            string       filter = (string)args[1];
            int          index  = (int)(double)args[2];
            bool         append = (bool)args[3];
            string       text   = (string)args[4];

            var blocks = new List <IMyTextSurfaceProvider>();

            BlockSelector.GetBlocksOfTypeWithQuery(type, filter, blocks);
            ImplLogger.LogBlocks(blocks);

            foreach (var block in blocks)
            {
                IMyTextSurface surface;
                if (block is IMyTextPanel && index == 0)
                {
                    surface = block as IMyTextSurface;
                }
                else
                {
                    surface = block.GetSurface(index);
                }

                if (surface != null)
                {
                    surface?.WriteText(text, append);
                }
                else
                {
                    Log.Write(ImplLogger.LOG_CAT, LogLevel.Verbose, "surface index out of range");
                }
            }
        }
Exemplo n.º 13
0
 public static void SetVar(IList args, IMethodContext context)
 {
     ImplLogger.LogImpl("setvar", args);
     context.Set((string)args[0], (double)args[1]);
 }
Exemplo n.º 14
0
 public static void Stop(IList args, IMethodContext context)
 {
     ImplLogger.LogImpl("stop", args);
     context.Runtime.StopProgram((string)args[0]);
 }
Exemplo n.º 15
0
 public static void Repeat(IList args, IMethodContext context)
 {
     ImplLogger.LogImpl("repeat", args);
     context.Goto(0);
 }
Exemplo n.º 16
0
 public static void WaitTicks(IList args, IMethodContext context)
 {
     ImplLogger.LogImpl("waitticks", args);
     context.Wait(((double)args[0]) / 60);
 }
Exemplo n.º 17
0
 internal static void Unload(IList args, IMethodContext context)
 {
     ImplLogger.LogImpl("unload", args);
     context.Runtime.UnloadProgram((string)args[0]);
 }
Exemplo n.º 18
0
 internal static void Echo(IList args, IMethodContext context)
 {
     ImplLogger.LogImpl("echo", args);
     Log.Write((string)args[0]);
 }
Exemplo n.º 19
0
        internal static void ListProps(IList args, IMethodContext context)
        {
            ImplLogger.LogImpl("listprops", args);

            MatchingType type   = (MatchingType)args[0];
            string       filter = (string)args[1];

            List <IMyTerminalBlock> blocks = new List <IMyTerminalBlock>();

            BlockSelector.GetBlocksOfTypeWithQuery(type, filter, blocks);
            ImplLogger.LogBlocks(blocks);

            List <ITerminalProperty> props = new List <ITerminalProperty>();

            foreach (var block in blocks)
            {
                Log.WriteFormat("Block \"{0}\" of type \"{1}\" contains properties:", new object[] { block.CustomName, block.GetType().Name });
                props.Clear();
                block.GetProperties(props);


                var allProps = new HashSet <string>();
                var badProps = new HashSet <string>(); // Termimal Properties can have same ids, which makes them unaccessible

                foreach (var prop in props)
                {
                    if (allProps.Contains(prop.Id))
                    {
                        badProps.Add(prop.Id);
                    }
                    else
                    {
                        allProps.Add(prop.Id);
                    }
                }

                foreach (var prop in props)
                {
                    // block.GetValue<object>(prop.Id) - Property is not of Type object <...>
                    object value = null;
                    try
                    {
                        PropType propType;
                        if (!badProps.Contains(prop.Id) && Enum.TryParse(prop.TypeName, out propType))
                        {
                            switch (propType)
                            {
                            case PropType.Boolean:
                                value = block.GetValueBool(prop.Id);
                                break;

                            case PropType.Single:
                                value = block.GetValueFloat(prop.Id);
                                break;

                            case PropType.Color:
                                value = block.GetValueColor(prop.Id);
                                break;

                            case PropType.StringBuilder:
                                value = block.GetValue <StringBuilder>(prop.Id);
                                break;

                            case PropType.String:
                                value = block.GetValue <string>(prop.Id);
                                break;

                            case PropType.Int64:
                                value = block.GetValue <long>(prop.Id);
                                break;
                            }
                        }
                    }
                    catch
                    {
                        // Looks like some game mod is broken, which is bad. Game breaking bad.
                        Log.WriteFormat(ImplLogger.LOG_CAT, LogLevel.Warning, $"Error reading property \"{prop.Id}\"");
                    }
                    Log.WriteFormat("\"{0}\" ({1}) = \"{2}\"", new object[] { prop.Id, prop.TypeName, value });
                }
                Log.WriteLine();
            }
        }
Exemplo n.º 20
0
        public static void ClearInputs(IList args, IMethodContext context)
        {
            ImplLogger.LogImpl("cm_clearinputs", args);

            CMMapper.Shared.Clear();
        }
Exemplo n.º 21
0
        public static void Transmit(IList args, IMethodContext context)
        {
            ImplLogger.LogImpl("transmit", args);

            MatchingType matchingType = (MatchingType)args[0];
            string       filter       = (string)args[1];
            string       targetString = (string)args[2];
            string       message      = (string)args[3];

            List <IMyTerminalBlock> antennas = new List <IMyTerminalBlock>();

            List <IMyRadioAntenna> radioAntennas = new List <IMyRadioAntenna>();

            BlockSelector.GetBlocksOfTypeWithQuery(matchingType, filter, radioAntennas);
            ImplLogger.LogBlocks(antennas);

            //get most powerful radio antenna
            IMyRadioAntenna mostPowerfulAntenna = null;

            //get radio antenna with longest radius that's enabled and broadcasting
            foreach (IMyRadioAntenna antenna in radioAntennas)
            {
                if (antenna.Enabled && antenna.GetValueBool("EnableBroadCast") &&
                    (mostPowerfulAntenna == null || antenna.Radius > mostPowerfulAntenna.Radius))
                {
                    mostPowerfulAntenna = antenna;
                }
            }

            if (mostPowerfulAntenna != null)
            {
                antennas.Add(mostPowerfulAntenna);
            }

            //--------get all laser antennas
            List <IMyLaserAntenna> laserAntennas = new List <IMyLaserAntenna>();

            BlockSelector.GetBlocksOfTypeWithQuery(matchingType, filter, laserAntennas);
            Log.WriteFormat(ImplLogger.LOG_CAT, LogLevel.Verbose, "{0} block(s) found", laserAntennas.Count);

            foreach (IMyLaserAntenna antenna in laserAntennas)
            {
                if (antenna.Status == MyLaserAntennaStatus.Connected)
                {
                    antennas.Add(antenna);
                }
            }

            //-----check whether at least one valid antenna was found
            if (antennas.Count != 0)
            {
                var transmitter = new Transmitter(antennas);
                transmitter.Transmit(message, targetString);
            }
            else
            {
                string warning;
                switch (matchingType)
                {
                default:
                case MatchingType.Match:
                    warning = string.Format("No antennas called \"{0}\" are currently able to transmit.", filter);
                    break;

                case MatchingType.Contains:
                    warning = string.Format("No antennas containing \"{0}\" are currently able to transmit.", filter);
                    break;

                case MatchingType.Head:
                    warning = string.Format("No antennas starting with \"{0}\" are currently able to transmit.", filter);
                    break;

                case MatchingType.Group:
                    warning = string.Format("No antennas in group \"{0}\" are currently able to transmit.", filter);
                    break;

                case MatchingType.Type:
                    warning = string.Format("No antennas of type \"{0}\" are currently able to transmit.", filter);
                    break;
                }

                Log.WriteFormat(ImplLogger.LOG_CAT, LogLevel.Warning, warning);
            }
        }
Exemplo n.º 22
0
 public static void SetFilter(IList args, IMethodContext context)
 {
     ImplLogger.LogImpl("cm_setFilter", args);
     Program.Current.Me.SetValue("ControlModule.CockpitFilter", (string)args[0]);
 }
Exemplo n.º 23
0
        public static void Set(IList args, IMethodContext context)
        {
            ImplLogger.LogImpl("set", args);

            MatchingType type   = (MatchingType)args[0];
            string       filter = (string)args[1];
            string       prop   = (string)args[2];
            string       value  = (string)args[3];

            List <IMyTerminalBlock> blocks = new List <IMyTerminalBlock>();

            BlockSelector.GetBlocksOfTypeWithQuery(type, filter, blocks);
            ImplLogger.LogBlocks(blocks);


            // Boolean
            // StringBuilder
            // Single
            // Int64
            // Color

            foreach (var block in blocks)
            {
                // todo: redo
                var propDef = block.GetProperty(prop);

                PropType propType;

                if (propDef != null && Enum.TryParse(propDef.TypeName, out propType))
                {
                    switch (propType)
                    {
                    case PropType.Boolean:
                    {
                        bool b;
                        if (bool.TryParse(value, out b))
                        {
                            block.SetValue(prop, b);
                        }
                        break;
                    }

                    case PropType.StringBuilder:
                    {
                        block.SetValue(prop, new StringBuilder(value));
                        break;
                    }

                    case PropType.String:
                    {
                        block.SetValue(prop, value);
                        break;
                    }

                    case PropType.Single:
                    {
                        float s;
                        if (float.TryParse(value, System.Globalization.NumberStyles.Number, C.I, out s))
                        {
                            block.SetValue(prop, s);
                        }
                    }
                    break;

                    case PropType.Int64:
                    {
                        long i;

                        if (ListConverter.ResolveListProperty(prop, value, out i))
                        {
                            block.SetValue(prop, i);
                        }
                    }
                    break;

                    case PropType.Color:
                    {
                        Color c;

                        if (ColorConverter.TryParseColor(value, out c))
                        {
                            block.SetValueColor(prop, c);
                        }
                        else
                        {
                            Log.WriteFormat(ImplLogger.LOG_CAT, LogLevel.Warning, "\"{0}\" is not a valid color", value);
                        }
                    }
                    break;
                    }
                }
                else
                {
                    Log.WriteFormat(ImplLogger.LOG_CAT, LogLevel.Warning, "block \"{0}\" does not have property \"{1}\", ignoring", block.CustomName, prop);
                }
            }
        }