|
| 1 | +using OpenMod.Installer.RocketMod.Helpers; |
| 2 | +using Rocket.Core.Logging; |
| 3 | +using System.IO; |
| 4 | + |
| 5 | +namespace OpenMod.Installer.RocketMod.Jobs |
| 6 | +{ |
| 7 | + public abstract class OpenModUnturnedConfigJob : IJob |
| 8 | + { |
| 9 | + public void ExecuteMigration(string[] args) |
| 10 | + { |
| 11 | + Logger.Log($"Updating OpenMod Unturned {ConfigType} config..."); |
| 12 | + |
| 13 | + var openmodDirectory = OpenModInstallerPlugin.Instance.OpenModManager.WorkingDirectory; |
| 14 | + var openmodUnturnedYaml = Path.Combine(openmodDirectory, "openmod.unturned.yaml"); |
| 15 | + |
| 16 | + if (!Directory.Exists(openmodDirectory)) |
| 17 | + { |
| 18 | + Directory.CreateDirectory(openmodDirectory); |
| 19 | + } |
| 20 | + |
| 21 | + string configText; |
| 22 | + |
| 23 | + if (File.Exists(openmodUnturnedYaml)) |
| 24 | + { |
| 25 | + configText = File.ReadAllText(openmodUnturnedYaml); |
| 26 | + } |
| 27 | + else |
| 28 | + { |
| 29 | + var openmodAssembly = AssemblyHelper.GetAssembly("OpenMod.Unturned"); |
| 30 | + using var configFileStream = openmodAssembly.GetManifestResourceStream("OpenMod.Unturned.openmod.unturned.yaml"); |
| 31 | + using var ms = new MemoryStream(); |
| 32 | + configFileStream!.CopyTo(ms); |
| 33 | + |
| 34 | + ms.Seek(0, SeekOrigin.Begin); |
| 35 | + |
| 36 | + using var reader = new StreamReader(ms); |
| 37 | + configText = reader.ReadToEnd(); |
| 38 | + } |
| 39 | + |
| 40 | + ModifyConfig(ref configText); |
| 41 | + |
| 42 | + File.WriteAllText(openmodUnturnedYaml, configText); |
| 43 | + |
| 44 | + Logger.Log($"Finished updating OpenMod Unturned {ConfigType} config."); |
| 45 | + } |
| 46 | + |
| 47 | + protected abstract string ConfigType { get; } |
| 48 | + |
| 49 | + protected abstract void ModifyConfig(ref string configText); |
| 50 | + } |
| 51 | +} |
0 commit comments