Example #1
0
        private static void Prefix(ref ZNet __instance)
        {
            if (!__instance.IsServer())
            {
                ValheimPlusPlugin.harmony.UnpatchSelf();

                // Load the client config file on server ZNet instance exit (server disconnect)
                if (ConfigurationExtra.LoadSettings() != true)
                {
                    Debug.LogError("Error while loading configuration file.");
                }

                ValheimPlusPlugin.harmony.PatchAll();

                //We left the server, so reset our map sync check.
                if (Configuration.Current.Map.IsEnabled && Configuration.Current.Map.shareMapProgression)
                {
                    VPlusMapSync.ShouldSyncOnSpawn = true;
                }
            }
            else
            {
                //Save map data to disk
                if (Configuration.Current.Map.IsEnabled && Configuration.Current.Map.shareMapProgression)
                {
                    VPlusMapSync.SaveMapDataToDisk();
                }
            }
        }
Example #2
0
        // Awake is called once when both the game and the plug-in are loaded
        void Awake()
        {
            Logger.LogInfo("Trying to load the configuration file");


            if (ConfigurationExtra.LoadSettings() != true)
            {
                Logger.LogError("Error while loading configuration file.");
            }
            else
            {
                Logger.LogInfo("Configuration file loaded succesfully.");

                var harmony = new Harmony("mod.valheim_plus");
                harmony.PatchAll();

                isUpToDate = !Settings.isNewVersionAvailable();
                if (!isUpToDate)
                {
                    Logger.LogError("There is a newer version available of ValheimPlus.");
                    Logger.LogWarning("Please visit " + ValheimPlusPlugin.Repository + ".");
                }
                else
                {
                    Logger.LogInfo("ValheimPlus [" + version + "] is up to date.");
                }

                //Logo
                VPlusMainMenu.Load();
            }
        }
Example #3
0
 private bool LoadSettings()
 {
     try
     {
         if (File.Exists(ConfigYamlPath))
         {
             Conf = ConfigurationExtra.LoadFromYaml(ConfigYamlPath);
         }
         else if (File.Exists(ConfigIniPath))
         {
             Conf = ConfigurationExtra.LoadFromIni(ConfigIniPath);
         }
         else
         {
             Logger.LogError("Error: Configuration not found. Plugin not loaded.");
             return(false);
         }
         //var parser = new FileIniDataParser();
         //Config = parser.ReadFile(ConfigPath);
     }
     catch (Exception ex)
     {
         Debug.LogError($"Could not load config file: {ex}");
         return(false);
     }
     return(true);
 }
Example #4
0
 private static void Prefix(ref ZNet __instance)
 {
     if (!__instance.IsServer())
     {
         // Load the client config file on server ZNet instance exit (server disconnect)
         if (ConfigurationExtra.LoadSettings() != true)
         {
             Debug.LogError("Error while loading configuration file.");
         }
     }
 }
Example #5
0
        // Awake is called once when both the game and the plug-in are loaded
        void Awake()
        {
            Logger.LogInfo("Trying to load the configuration file");

            if (ConfigurationExtra.LoadSettings() != true)
            {
                Logger.LogError("Error while loading configuration file.");
            }
            else
            {
                Logger.LogInfo("Configuration file loaded succesfully.");


                harmony.PatchAll();

                isUpToDate = !IsNewVersionAvailable();
                if (!isUpToDate)
                {
                    Logger.LogError("There is a newer version available of ValheimPlus.");
                    Logger.LogWarning("Please visit " + ValheimPlusPlugin.Repository + ".");
                }
                else
                {
                    Logger.LogInfo("ValheimPlus [" + version + "] is up to date.");
                }

                //Create VPlus dir if it does not exist.
                if (!Directory.Exists(VPlusDataDirectoryPath))
                {
                    Directory.CreateDirectory(VPlusDataDirectoryPath);
                }

                //Logo
                //if (Configuration.Current.ValheimPlus.IsEnabled && Configuration.Current.ValheimPlus.mainMenuLogo)
                // No need to exclude with IF, this only loads the images, causes issues if this config setting is changed
                VPlusMainMenu.Load();

                VPlusSettings.Load();

                //Map Sync Save Timer
                if (ZNet.m_isServer && Configuration.Current.Map.IsEnabled && Configuration.Current.Map.shareMapProgression)
                {
                    mapSyncSaveTimer.AutoReset = true;
                    mapSyncSaveTimer.Elapsed  += (sender, args) => VPlusMapSync.SaveMapDataToDisk();
                }
            }
        }
Example #6
0
            static bool Prefix(ref string __result)
            {
                string gameVersion = Version.CombineVersion(global::Version.m_major, global::Version.m_minor, global::Version.m_patch);

                __result = gameVersion;

                if (Configuration.Current.Server.EnforceConfiguration && Configuration.Current.Server.EnforceMod)
                {
                    __result = gameVersion + "@" + ValheimPlusPlugin.version + "@" + ConfigurationExtra.GetServerHashFor(Configuration.Current);
                    Debug.Log($"Version generated with enforced mod and config hash : {__result}");
                    return(false);
                }

                if (Configuration.Current.Server.EnforceMod)
                {
                    Debug.Log($"Version generated with enforced mod : {__result}");
                    __result = gameVersion + "@" + ValheimPlusPlugin.version;
                    return(false);
                }
                Debug.Log($"Version generated : {__result}");
                return(false);
            }
Example #7
0
        public static void RPC_VPlusConfigSync(long sender, ZPackage configPkg)
        {
            if (ZNet.m_isServer) //Server
            {
                if (!Configuration.Current.Server.serverSyncsConfig)
                {
                    return;
                }

                ZPackage      pkg             = new ZPackage();
                List <string> cleanConfigData = new List <string>();

                IniData configdata = Configuration.Current.ConfigData;


                foreach (var prop in typeof(Configuration).GetProperties())
                {
                    var keyName = prop.Name;
                    var method  = prop.PropertyType.GetMethod("HasNeedsServerSync", BindingFlags.Public | BindingFlags.FlattenHierarchy | BindingFlags.Instance);

                    if (method != null)
                    {
                        var  instance           = prop.GetValue(Configuration.Current, null);
                        bool HasNeedsServerSync = (bool)method.Invoke(instance, new object[] { });
                        if (HasNeedsServerSync)
                        {
                            cleanConfigData.Add($"[{keyName}]");
                            bool hasEnableProperty = false;
                            if (configdata[keyName] != null)
                            {
                                configdata[keyName].ClearComments();
                                IEnumerator <KeyData> iterator = configdata[keyName].GetEnumerator();
                                while (iterator.MoveNext())
                                {
                                    KeyData keyData = iterator.Current;
                                    if (keyData.KeyName.Equals("enabled"))
                                    {
                                        hasEnableProperty = true;
                                    }
                                    cleanConfigData.Add($"{keyData.KeyName}={keyData.Value}");
                                }
                            }
                            if (!hasEnableProperty)
                            {
                                cleanConfigData.Add("enabled = false");
                            }
                        }
                    }
                }

                //Add number of clean lines to package
                pkg.Write(cleanConfigData.Count);

                //Add each line to the package
                foreach (string line in cleanConfigData)
                {
                    pkg.Write(line);

                    ZLog.Log("SENTCONFIG: " + line);
                }

                ZRoutedRpc.instance.InvokeRoutedRPC(sender, "VPlusConfigSync", new object[]
                {
                    pkg
                });

                ZLog.Log("VPlus configuration synced to peer #" + sender);
            }
            else //Client
            {
                if (configPkg != null &&
                    configPkg.Size() > 0 &&
                    sender == ZRoutedRpc.instance.GetServerPeerID()) //Validate the message is from the server and not another client.
                {
                    int numLines = configPkg.ReadInt();

                    if (numLines == 0)
                    {
                        ZLog.LogWarning("Got zero line config file from server. Cannot load.");
                        return;
                    }

                    using (MemoryStream memStream = new MemoryStream())
                    {
                        using (StreamWriter tmpWriter = new StreamWriter(memStream))
                        {
                            for (int i = 0; i < numLines; i++)
                            {
                                string line = configPkg.ReadString();

                                tmpWriter.WriteLine(line);

                                ZLog.Log("CONFIGDATA: " + line);
                            }

                            tmpWriter.Flush();      //Flush to memStream
                            memStream.Position = 0; //Rewind stream

                            ConfigurationExtra.LoadConfigurationFromStream(memStream);

                            // Needed to make sure client is using server configuration as dayLength is setup before
                            // TimeManipulation.SetupDayLength(); DEACTIVATED

                            ZLog.Log("Successfully synced VPlus configuration from server.");
                        }
                    }
                }
            }
        }
Example #8
0
        public static void RPC_VPlusConfigSync(long sender, ZPackage configPkg)
        {
            if (ZNet.m_isServer) //Server
            {
                if (!Configuration.Current.Server.serverSyncsConfig)
                {
                    return;
                }

                ZPackage pkg = new ZPackage();

                string[]      rawConfigData   = File.ReadAllLines(ConfigurationExtra.ConfigIniPath);
                List <string> cleanConfigData = new List <string>();

                for (int i = 0; i < rawConfigData.Length; i++)
                {
                    if (rawConfigData[i].Trim().StartsWith(";") ||
                        rawConfigData[i].Trim().StartsWith("#"))
                    {
                        continue;                                          //Skip comments
                    }
                    if (rawConfigData[i].Trim().IsNullOrWhiteSpace())
                    {
                        continue;                                               //Skip blank lines
                    }
                    //Add to clean data
                    cleanConfigData.Add(rawConfigData[i]);
                }

                //Add number of clean lines to package
                pkg.Write(cleanConfigData.Count);

                //Add each line to the package
                foreach (string line in cleanConfigData)
                {
                    pkg.Write(line);

                    ZLog.Log("SENTCONFIG: " + line);
                }

                ZRoutedRpc.instance.InvokeRoutedRPC(sender, "VPlusConfigSync", new object[]
                {
                    pkg
                });

                ZLog.Log("VPlus configuration synced to peer #" + sender);
            }
            else //Client
            {
                if (configPkg != null &&
                    configPkg.Size() > 0 &&
                    sender == ZRoutedRpc.instance.GetServerPeerID()) //Validate the message is from the server and not another client.
                {
                    int numLines = configPkg.ReadInt();

                    if (numLines == 0)
                    {
                        ZLog.LogWarning("Got zero line config file from server. Cannot load.");
                        return;
                    }

                    using (MemoryStream memStream = new MemoryStream())
                    {
                        using (StreamWriter tmpWriter = new StreamWriter(memStream))
                        {
                            for (int i = 0; i < numLines; i++)
                            {
                                string line = configPkg.ReadString();

                                tmpWriter.WriteLine(line);

                                ZLog.Log("CONFIGDATA: " + line);
                            }

                            tmpWriter.Flush();      //Flush to memStream
                            memStream.Position = 0; //Rewind stream

                            Configuration.Current = ConfigurationExtra.LoadFromIni(memStream);

                            // Needed to make sure client is using server configuration as dayLength is setup before
                            TimeManipulation.SetupDayLength();

                            ZLog.Log("Successfully synced VPlus configuration from server.");
                        }
                    }
                }
            }
        }