public static void Save(SyncModSettings settings, bool world)
 {
     if (world)
     {
         using (var writer = MyAPIGateway.Utilities.WriteFileInWorldStorage("ModSettings.xml", typeof(SyncModSettings)))
         {
             writer.Write(MyAPIGateway.Utilities.SerializeToXML(settings));
         }
     }
     else
     {
         using (var writer = MyAPIGateway.Utilities.WriteFileInLocalStorage("ModSettings.xml", typeof(SyncModSettings)))
         {
             writer.Write(MyAPIGateway.Utilities.SerializeToXML(settings));
         }
     }
 }
Exemplo n.º 2
0
 /// <summary>
 ///
 /// </summary>
 private void SyncModSettingsReceived(byte[] dataRcv)
 {
     try
     {
         var msgRcv = MyAPIGateway.Utilities.SerializeFromBinary <MsgModSettings>(dataRcv);
         Settings      = msgRcv.Settings;
         SettingsValid = true;
         if (Mod.Log.ShouldLog(Logging.Level.Info))
         {
             Mod.Log.Write(Logging.Level.Info, "BuildAndRepairSystemMod: SyncModSettingsReceived");
         }
         foreach (var buildAndRepairSystem in BuildAndRepairSystems.Values)
         {
             buildAndRepairSystem.SettingsChanged();
         }
     }
     catch (Exception ex)
     {
         Mod.Log.Write(Logging.Level.Error, "BuildAndRepairSystemMod: SyncModSettingsReceived Exception:{0}", ex);
     }
 }
Exemplo n.º 3
0
        /// <summary>
        ///
        /// </summary>
        public void Init()
        {
            Mod.Log.Write(Logging.Level.Info, "BuildAndRepairSystemMod: Initializing.");
            _Init = true;

            Settings      = SyncModSettings.Load();
            SettingsValid = MyAPIGateway.Session.IsServer;
            foreach (var entry in BuildAndRepairSystems)
            {
                entry.Value.SettingsChanged();
            }

            Mod.Log.LogLevel = Settings.LogLevel;

            MyAPIGateway.Session.DamageSystem.RegisterBeforeDamageHandler(0, BeforeDamageHandlerNoDamageByBuildAndRepairSystem);
            if (MyAPIGateway.Session.IsServer)
            {
                //Detect friendly damage onl needed on server
                MyAPIGateway.Session.DamageSystem.RegisterAfterDamageHandler(100, AfterDamageHandlerNoDamageByBuildAndRepairSystem);
            }

            if (MyAPIGateway.Utilities.IsDedicated)
            {
                MyAPIGateway.Multiplayer.RegisterMessageHandler(MSGID_MOD_DATAREQUEST, SyncModDataRequestReceived);
                MyAPIGateway.Multiplayer.RegisterMessageHandler(MSGID_BLOCK_DATAREQUEST, SyncBlockDataRequestReceived);
                //Same as MSGID_BLOCK_SETTINGS but SendMessageToOthers sends also to self, which will result in stack overflow
                MyAPIGateway.Multiplayer.RegisterMessageHandler(MSGID_BLOCK_SETTINGS_FROM_CLIENT, SyncBlockSettingsReceived);
            }
            else if (!MyAPIGateway.Session.IsServer)
            {
                MyAPIGateway.Multiplayer.RegisterMessageHandler(MSGID_MOD_SETTINGS, SyncModSettingsReceived);
                MyAPIGateway.Multiplayer.RegisterMessageHandler(MSGID_BLOCK_SETTINGS_FROM_SERVER, SyncBlockSettingsReceived);
                MyAPIGateway.Multiplayer.RegisterMessageHandler(MSGID_BLOCK_STATE_FROM_SERVER, SyncBlockStateReceived);

                SyncModDataRequestSend();
            }
            MyAPIGateway.Utilities.MessageEntered += Utilities_MessageEntered;

            Mod.Log.Write(Logging.Level.Info, "BuildAndRepairSystemMod: Initialized.");
        }
        public static SyncModSettings Load()
        {
            var             world    = false;
            SyncModSettings settings = null;

            try
            {
                if (MyAPIGateway.Utilities.FileExistsInWorldStorage("ModSettings.xml", typeof(SyncModSettings)))
                {
                    world = true;
                    using (var reader = MyAPIGateway.Utilities.ReadFileInWorldStorage("ModSettings.xml", typeof(SyncModSettings)))
                    {
                        settings = MyAPIGateway.Utilities.SerializeFromXML <SyncModSettings>(reader.ReadToEnd());
                        Mod.Log.Write(Logging.Level.Info, "NanobotBuildAndRepairSystemSettings: Loaded from world file.");
                    }
                }
                else if (MyAPIGateway.Utilities.FileExistsInLocalStorage("ModSettings.xml", typeof(SyncModSettings)))
                {
                    using (var reader = MyAPIGateway.Utilities.ReadFileInLocalStorage("ModSettings.xml", typeof(SyncModSettings)))
                    {
                        settings = MyAPIGateway.Utilities.SerializeFromXML <SyncModSettings>(reader.ReadToEnd());
                        Mod.Log.Write(Logging.Level.Info, "NanobotBuildAndRepairSystemSettings: Loaded from local storage.");

                        Save(settings, true);
                    }
                }

                if (settings != null)
                {
                    var adjusted = false;
                    if (settings.Version < CurrentSettingsVersion)
                    {
                        Mod.Log.Write(Logging.Level.Info, "NanobotBuildAndRepairSystemSettings: Settings have old version: {0} update to {1}", settings.Version, CurrentSettingsVersion);
                        switch (settings.Version)
                        {
                        case 0:
                            settings.LogLevel = Logging.Level.Error;
                            break;
                        }

                        if (settings.Welder.AllowedSearchModes == 0)
                        {
                            settings.Welder.AllowedSearchModes = SearchModes.Grids | SearchModes.BoundingBox;
                        }
                        if (settings.Welder.AllowedWorkModes == 0)
                        {
                            settings.Welder.AllowedWorkModes = WorkModes.WeldBeforeGrind | WorkModes.GrindBeforeWeld | WorkModes.GrindIfWeldGetStuck;
                        }
                        if (settings.Welder.WeldingMultiplier == 0)
                        {
                            settings.Welder.WeldingMultiplier = 1;
                        }
                        if (settings.Welder.GrindingMultiplier == 0)
                        {
                            settings.Welder.GrindingMultiplier = 1;
                        }

                        adjusted         = true;
                        settings.Version = CurrentSettingsVersion;
                    }
                    if (settings.Range > NanobotBuildAndRepairSystemBlock.WELDER_RANGE_MAX_IN_M)
                    {
                        settings.Range = NanobotBuildAndRepairSystemBlock.WELDER_RANGE_MAX_IN_M;
                        adjusted       = true;
                    }
                    else if (settings.Range < NanobotBuildAndRepairSystemBlock.WELDER_RANGE_MIN_IN_M)
                    {
                        settings.Range = NanobotBuildAndRepairSystemBlock.WELDER_RANGE_MIN_IN_M;
                        adjusted       = true;
                    }

                    if (settings.Welder.WeldingMultiplier < NanobotBuildAndRepairSystemBlock.WELDING_GRINDING_MULTIPLIER_MIN)
                    {
                        settings.Welder.WeldingMultiplier = NanobotBuildAndRepairSystemBlock.WELDING_GRINDING_MULTIPLIER_MIN;
                        adjusted = true;
                    }
                    else if (settings.Welder.WeldingMultiplier >= NanobotBuildAndRepairSystemBlock.WELDING_GRINDING_MULTIPLIER_MAX)
                    {
                        settings.Welder.WeldingMultiplier = NanobotBuildAndRepairSystemBlock.WELDING_GRINDING_MULTIPLIER_MAX;
                        adjusted = true;
                    }

                    if (settings.Welder.GrindingMultiplier < NanobotBuildAndRepairSystemBlock.WELDING_GRINDING_MULTIPLIER_MIN)
                    {
                        settings.Welder.GrindingMultiplier = NanobotBuildAndRepairSystemBlock.WELDING_GRINDING_MULTIPLIER_MIN;
                        adjusted = true;
                    }
                    else if (settings.Welder.GrindingMultiplier >= NanobotBuildAndRepairSystemBlock.WELDING_GRINDING_MULTIPLIER_MAX)
                    {
                        settings.Welder.GrindingMultiplier = NanobotBuildAndRepairSystemBlock.WELDING_GRINDING_MULTIPLIER_MAX;
                        adjusted = true;
                    }

                    Mod.Log.Write(Logging.Level.Info, "NanobotBuildAndRepairSystemSettings: Settings {0} {1} ", settings.Welder.GrindingMultiplier, settings);
                    if (adjusted)
                    {
                        Save(settings, world);
                    }
                }
                else
                {
                    settings = new SyncModSettings()
                    {
                        Version = CurrentSettingsVersion
                    };
                    Save(settings, world);
                }
            }
            catch (Exception ex)
            {
                Mod.Log.Write(Logging.Level.Error, "NanobotBuildAndRepairSystemSettings: Exception while loading: {0}", ex);
            }

            return(settings);
        }
Exemplo n.º 5
0
        private void Utilities_MessageEntered(string messageText, ref bool sendToOthers)
        {
            if (string.IsNullOrEmpty(messageText))
            {
                return;
            }
            var cmd = messageText.ToLower();

            if (cmd.StartsWith(CmdKey))
            {
                Mod.Log.Write(Logging.Level.Error, "BuildAndRepairSystemMod: Cmd: {0}", messageText);
                var args = cmd.Remove(0, CmdKey.Length).Trim().Split(' ');
                if (args.Length > 0)
                {
                    Mod.Log.Write(Logging.Level.Error, "BuildAndRepairSystemMod: Cmd args[0]: {0}", args[0]);
                    switch (args[0].Trim())
                    {
                    case CmdCwsf:
                        if (MyAPIGateway.Session.IsServer)
                        {
                            SyncModSettings.Save(Settings, true);
                            MyAPIGateway.Utilities.ShowMessage(CmdKey, "Settings file created inside world folder");
                        }
                        else
                        {
                            MyAPIGateway.Utilities.ShowMessage(CmdKey, "command not allowed on client");
                        }
                        break;

                    case CmdLogLevel:
                        if (args.Length > 1)
                        {
                            switch (args[1].Trim())
                            {
                            case CmdLogLevel_All:
                                Mod.Log.LogLevel = Logging.Level.All;
                                MyAPIGateway.Utilities.ShowMessage(CmdKey, string.Format("Logging level switched to All [{0:X}]", Mod.Log.LogLevel));
                                break;

                            case CmdLogLevel_Default:
                            default:
                                Mod.Log.LogLevel = Settings.LogLevel;
                                MyAPIGateway.Utilities.ShowMessage(CmdKey, string.Format("Logging level switched to Default [{0:X}]", Mod.Log.LogLevel));
                                break;
                            }
                        }
                        break;

                    case CmdHelp1:
                    case CmdHelp2:
                    default:
                        MyAPIGateway.Utilities.ShowMissionScreen("NanobotBuildAndRepairSystem", "Help", "", MyAPIGateway.Session.IsServer ? CmdHelpText_Server : CmdHelpText_Client);
                        break;
                    }
                }
                else
                {
                    MyAPIGateway.Utilities.ShowMissionScreen("NanobotBuildAndRepairSystem", "Help", "", MyAPIGateway.Session.IsServer ? CmdHelpText_Server : CmdHelpText_Client);
                }
                sendToOthers = false;
            }
        }