Exemplo n.º 1
0
 // GetConfig...returns the config instance
 internal static ModConfig GetConfig(ModNet.NetConfig netConfig) => ConfigManager.GetConfig(ModLoader.GetMod(netConfig.modname), netConfig.configname);
Exemplo n.º 2
0
        internal static void HandleInGameChangeConfigPacket(BinaryReader reader, int whoAmI)
        {
            if (Main.netMode == NetmodeID.MultiplayerClient)
            {
                bool   success = reader.ReadBoolean();
                string message = reader.ReadString();
                if (success)
                {
                    string    modname      = reader.ReadString();
                    string    configname   = reader.ReadString();
                    string    json         = reader.ReadString();
                    ModConfig activeConfig = GetConfig(ModLoader.GetMod(modname), configname);
                    JsonConvert.PopulateObject(json, activeConfig, serializerSettingsCompact);
                    activeConfig.OnChanged();

                    Main.NewText($"Shared config changed: Message: {message}, Mod: {modname}, Config: {configname}");
                    if (Main.InGameUI.CurrentState == Interface.modConfig)
                    {
                        Main.InGameUI.SetState(Interface.modConfig);
                        Interface.modConfig.SetMessage("Server response: " + message, Color.Green);
                    }
                }
                else
                {
                    // rejection only sent back to requester.
                    // Update UI with message

                    Main.NewText("Changes Rejected: " + message);
                    if (Main.InGameUI.CurrentState == Interface.modConfig)
                    {
                        Interface.modConfig.SetMessage("Server rejected changes: " + message, Color.Red);
                        //Main.InGameUI.SetState(Interface.modConfig);
                    }
                }
            }
            else
            {
                // no bool in request.
                string    modname        = reader.ReadString();
                string    configname     = reader.ReadString();
                string    json           = reader.ReadString();
                ModConfig config         = GetConfig(ModLoader.GetMod(modname), configname);
                ModConfig loadTimeConfig = GetLoadTimeConfig(ModLoader.GetMod(modname), configname);
                ModConfig pendingConfig  = GeneratePopulatedClone(config);
                JsonConvert.PopulateObject(json, pendingConfig, serializerSettingsCompact);
                bool   success = true;
                string message = "Accepted";
                if (loadTimeConfig.NeedsReload(pendingConfig))
                {
                    success = false;
                    message = "Can't save because changes would require a reload.";
                }
                if (!config.AcceptClientChanges(pendingConfig, whoAmI, ref message))
                {
                    success = false;
                }
                if (success)
                {
                    // Apply to Servers Config
                    ConfigManager.Save(pendingConfig);
                    JsonConvert.PopulateObject(json, config, ConfigManager.serializerSettingsCompact);
                    config.OnChanged();
                    // Send new config to all clients
                    var p = new ModPacket(MessageID.InGameChangeConfig);
                    p.Write(true);
                    p.Write(message);
                    p.Write(modname);
                    p.Write(configname);
                    p.Write(json);
                    p.Send();
                }
                else
                {
                    // Send rejections message back to client who requested change
                    var p = new ModPacket(MessageID.InGameChangeConfig);
                    p.Write(false);
                    p.Write(message);
                    p.Send(whoAmI);
                }
            }
            return;
        }