Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[HELP] Language for sending event messages #317

Open
shabievvusal opened this issue Nov 24, 2024 · 8 comments
Open

[HELP] Language for sending event messages #317

shabievvusal opened this issue Nov 24, 2024 · 8 comments

Comments

@shabievvusal
Copy link

Hi, I'm a beginner in programming. I wanted to make changes to your project, but I can't compile it. Many errors are due to the fact that it is impossible to find something in the project. Although I forked it and everything should work.

I want to make changes in order to gain a little experience. I want to change the ability to receive a message about the start of a fight with a boss, to study a new area in your project. I receive messages about the start of an attack on the base in English, I wanted to change it to Russian.

I will at least try to implement all this in reality, although I am not confident in my abilities.
build_log.txt

@shabievvusal
Copy link
Author

I forked the project, but I don't know how to change the answers about the event name from English to Russian.

@shabievvusal
Copy link
Author

I understand that we need to work with Localization here. But in general, it seems to me that it would all be simplified if the dedicated server was launched in Russian.

using System;
using System.Collections.Generic;
using HarmonyLib;
using UnityEngine;

namespace DiscordConnector.Patches;
internal class RandEventPatches
{

    [HarmonyPatch(typeof(RandomEvent), nameof(RandomEvent.OnActivate))]
    internal class OnActivate
    {

        private static void Prefix(ref RandomEvent __instance)
        {
            bool active = __instance.m_active;
            float duration = __instance.m_duration;
            string name = __instance.m_name;
            float time = __instance.m_time;
            float remaining = duration - time;
            Vector3 pos = __instance.m_pos;
            Plugin.StaticLogger.LogDebug(
                $"Random event OnActivate {name}: {active} for {duration} at {pos}. (time: {time})"
            );



            List<String> involvedPlayers = new List<string>();
            foreach (ZNet.PlayerInfo playerInfo in ZNet.instance.GetPlayerList())
            {
                if (RandEventSystem.instance.IsInsideRandomEventArea(__instance, playerInfo.m_position))
                {
                    involvedPlayers.Add(playerInfo.m_name);
                }
            }
            Plugin.StaticLogger.LogDebug(
                $"Involved players in event: {(string.Join(",", involvedPlayers.ToArray()))}"
            );

            if (__instance.m_time > 0)
            {
                if (Plugin.StaticConfig.EventResumedMessageEnabled)
                {
                    string message = MessageTransformer.FormatEventMessage(
                        Plugin.StaticConfig.EventResumedMessage,
                        Localization.instance.Localize(__instance.m_endMessage),
                        Localization.instance.Localize(__instance.m_startMessage)
                    // string.Join(",", involvedPlayers.ToArray()) //! Removed with event changes 
                    );
                    if (!Plugin.StaticConfig.EventResumedPosEnabled)
                    {
                        DiscordApi.SendMessage(Webhook.Event.EventResumed, message);
                        return;
                    }
                    if (Plugin.StaticConfig.DiscordEmbedsEnabled || !message.Contains("%POS%"))
                    {
                        DiscordApi.SendMessage(Webhook.Event.EventResumed, message, pos);
                    }
                    else
                    {
                        message = MessageTransformer.FormatEventMessage(
                            Plugin.StaticConfig.EventResumedMessage,
                            Localization.instance.Localize(__instance.m_endMessage),
                            Localization.instance.Localize(__instance.m_startMessage),
                            // string.Join(",", involvedPlayers.ToArray()), //! Removed with event changes 
                            pos
                        );
                        DiscordApi.SendMessage(Webhook.Event.EventResumed, message);
                    }
                }
            }
            else
            {
                if (Plugin.StaticConfig.EventStartMessageEnabled)
                {
                    string message = MessageTransformer.FormatEventStartMessage(
                        Plugin.StaticConfig.EventResumedMessage,
                        Localization.instance.Localize(__instance.m_endMessage),
                        Localization.instance.Localize(__instance.m_startMessage)
                    // string.Join(",", involvedPlayers.ToArray()) //! Removed with event changes 
                    );
                    if (!Plugin.StaticConfig.EventStartPosEnabled)
                    {
                        DiscordApi.SendMessage(Webhook.Event.EventStart, message);
                        return;
                    }
                    if (Plugin.StaticConfig.DiscordEmbedsEnabled || !message.Contains("%POS%"))
                    {
                        DiscordApi.SendMessage(Webhook.Event.EventStart, message, pos);
                    }
                    else
                    {
                        message = MessageTransformer.FormatEventStartMessage(
                            Plugin.StaticConfig.EventResumedMessage,
                            Localization.instance.Localize(__instance.m_endMessage),
                            Localization.instance.Localize(__instance.m_startMessage),
                            // string.Join(",", involvedPlayers.ToArray()), //! Removed with event changes 
                            pos
                        );
                        DiscordApi.SendMessage(Webhook.Event.EventStart, message);
                    }
                }
            }
        }
    }

    [HarmonyPatch(typeof(RandomEvent), nameof(RandomEvent.OnDeactivate))]
    internal class OnDeactivate
    {

        private static void Prefix(ref RandomEvent __instance, ref bool end)
        {
            bool active = __instance.m_active;
            float duration = __instance.m_duration;
            string name = __instance.m_name;
            float time = __instance.m_time;
            Vector3 pos = __instance.m_pos;
            Plugin.StaticLogger.LogDebug(
                $"Random event OnDeactivate {name}: End?{active} for {duration} at {pos}. (time: {time})"
            );

            List<String> involvedPlayers = new List<string>();
            foreach (ZNet.PlayerInfo playerInfo in ZNet.instance.GetPlayerList())
            {
                if (RandEventSystem.instance.IsInsideRandomEventArea(__instance, playerInfo.m_position))
                {
                    involvedPlayers.Add(playerInfo.m_name);
                }
            }
            Plugin.StaticLogger.LogDebug(
                $"Involved players in event: {(string.Join(",", involvedPlayers.ToArray()))}"
            );

            if (!end)
            {
                if (Plugin.StaticConfig.EventPausedMessageEnabled)
                {
                    string message = MessageTransformer.FormatEventMessage(
                        Plugin.StaticConfig.EventPausedMessage,
                        Localization.instance.Localize(__instance.m_endMessage),
                        Localization.instance.Localize(__instance.m_startMessage)
                    // string.Join(",", involvedPlayers.ToArray()) //! Removed with event changes 
                    );
                    if (!Plugin.StaticConfig.EventPausedPosEnabled)
                    {
                        DiscordApi.SendMessage(Webhook.Event.EventPaused, message);
                        return;
                    }
                    if (Plugin.StaticConfig.DiscordEmbedsEnabled || !message.Contains("%POS%"))
                    {
                        DiscordApi.SendMessage(Webhook.Event.EventPaused, message, pos);
                    }
                    else
                    {
                        message = MessageTransformer.FormatEventMessage(
                            Plugin.StaticConfig.EventPausedMessage,
                            Localization.instance.Localize(__instance.m_endMessage),
                            Localization.instance.Localize(__instance.m_startMessage),
                            // string.Join(",", involvedPlayers.ToArray()), //! Removed with event changes 
                            pos
                        );
                        DiscordApi.SendMessage(Webhook.Event.EventPaused, message);
                    }
                }
            }
            else
            {
                if (Plugin.StaticConfig.EventStopMessageEnabled)
                {
                    string message = MessageTransformer.FormatEventEndMessage(
                        Plugin.StaticConfig.EventStopMessage,
                        Localization.instance.Localize(__instance.m_endMessage),
                        Localization.instance.Localize(__instance.m_startMessage)
                    // string.Join(",", involvedPlayers.ToArray()) //! Removed with event changes 
                    );
                    if (!Plugin.StaticConfig.EventStopPosEnabled)
                    {
                        DiscordApi.SendMessage(Webhook.Event.EventStop, message);
                        return;
                    }
                    if (Plugin.StaticConfig.DiscordEmbedsEnabled || !message.Contains("%POS%"))
                    {
                        DiscordApi.SendMessage(Webhook.Event.EventStop, message, pos);
                    }
                    else
                    {
                        message = MessageTransformer.FormatEventEndMessage(
                            Plugin.StaticConfig.EventStopMessage,
                            Localization.instance.Localize(__instance.m_endMessage),
                            Localization.instance.Localize(__instance.m_startMessage),
                            // string.Join(",", involvedPlayers.ToArray()), //! Removed with event changes 
                            pos
                        );
                        DiscordApi.SendMessage(Webhook.Event.EventStop, message);
                    }
                }
            }
        }
    }

}

@shabievvusal shabievvusal changed the title [HELP] I can't fork the project [HELP] Language for sending event messages Nov 24, 2024
@nwesterhausen
Copy link
Owner

Yeah I haven't looked into the localization part at all. But all the existing code does is ask the server for the strings to use, I would expect launched it with Russian as the language would fix. But not sure how that would work, and perhaps the localization is all client-side.

If that's the case, there likely is some localization class that can provide the event strings for the specified language?

@shabievvusal
Copy link
Author

Yeah I haven't looked into the localization part at all. But all the existing code does is ask the server for the strings to use, I would expect launched it with Russian as the language would fix. But not sure how that would work, and perhaps the localization is all client-side.

If that's the case, there likely is some localization class that can provide the event strings for the specified language?

It seems to me that nothing needs to be changed in the code, and it is enough to change the launch of the dedicated server valheim in Russian. Such information is missing. On the other hand, I'm not sure that this is enough. Maybe on a dedicated server everything will be in Russian, but the answers will still be in English. How complicated it all is...

@shabievvusal
Copy link
Author

What package did you connect for Localization?

image

@shabievvusal
Copy link
Author

here you can see that the server is running with English localization. how do i fix this?

image

@nwesterhausen
Copy link
Owner

Re: server localization -- I have no idea. Never tried it.

Re: Which localization package -- It's one of the classes from Valheim itself. If you want to take a look at all the classes, an easy tool to do this with is ILSpy which can decompile C# programs. You can use it to poke around in the Valheim code and see the classes and stuff.

@shabievvusal
Copy link
Author

shabievvusal commented Nov 25, 2024

I did it. True, it didn't take me much trouble, but it would be more interesting to do it with code. At least change the configuration file to start the server by choosing the language. I opened the file assembly_guitils. All that's left is to wait for the event to start and get a message in the discord, I'll write later whether it helped or not.

image
image

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants