mirror of
https://github.com/soarqin/DSP_Mods_TO.git
synced 2025-12-15 19:13:27 +08:00
Added CruiseAssist and AutoPilot
This commit is contained in:
157
CruiseAssist/Commons/ConfigManager.cs
Normal file
157
CruiseAssist/Commons/ConfigManager.cs
Normal file
@@ -0,0 +1,157 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using BepInEx.Configuration;
|
||||
using HarmonyLib;
|
||||
|
||||
namespace CruiseAssist.Commons;
|
||||
|
||||
internal abstract class ConfigManager
|
||||
{
|
||||
public static ConfigFile Config { get; private set; }
|
||||
|
||||
protected ConfigManager(ConfigFile config)
|
||||
{
|
||||
_instance = this;
|
||||
Config = config;
|
||||
Config.SaveOnConfigSet = false;
|
||||
}
|
||||
|
||||
public static void CheckConfig(Step step)
|
||||
{
|
||||
_instance.CheckConfigImplements(step);
|
||||
}
|
||||
|
||||
protected abstract void CheckConfigImplements(Step step);
|
||||
|
||||
public static ConfigEntry<T> Bind<T>(ConfigDefinition configDefinition, T defaultValue, ConfigDescription configDescription = null)
|
||||
{
|
||||
return Config.Bind(configDefinition, defaultValue, configDescription);
|
||||
}
|
||||
|
||||
public static ConfigEntry<T> Bind<T>(string section, string key, T defaultValue, ConfigDescription configDescription = null)
|
||||
{
|
||||
return Config.Bind(section, key, defaultValue, configDescription);
|
||||
}
|
||||
|
||||
public static ConfigEntry<T> Bind<T>(string section, string key, T defaultValue, string description)
|
||||
{
|
||||
return Config.Bind(section, key, defaultValue, description);
|
||||
}
|
||||
|
||||
public static ConfigEntry<T> GetEntry<T>(ConfigDefinition configDefinition)
|
||||
{
|
||||
ConfigEntry<T> configEntry;
|
||||
try
|
||||
{
|
||||
configEntry = (ConfigEntry<T>)Config[configDefinition];
|
||||
}
|
||||
catch (KeyNotFoundException ex)
|
||||
{
|
||||
LogManager.LogError($"{ex.GetType()}: configDefinition={configDefinition}");
|
||||
throw;
|
||||
}
|
||||
return configEntry;
|
||||
}
|
||||
|
||||
public static ConfigEntry<T> GetEntry<T>(string section, string key)
|
||||
{
|
||||
return GetEntry<T>(new ConfigDefinition(section, key));
|
||||
}
|
||||
|
||||
public static T GetValue<T>(ConfigDefinition configDefinition)
|
||||
{
|
||||
return GetEntry<T>(configDefinition).Value;
|
||||
}
|
||||
|
||||
public static T GetValue<T>(string section, string key)
|
||||
{
|
||||
return GetEntry<T>(section, key).Value;
|
||||
}
|
||||
|
||||
public static bool ContainsKey(ConfigDefinition configDefinition)
|
||||
{
|
||||
return Config.ContainsKey(configDefinition);
|
||||
}
|
||||
|
||||
public static bool ContainsKey(string section, string key)
|
||||
{
|
||||
return Config.ContainsKey(new ConfigDefinition(section, key));
|
||||
}
|
||||
|
||||
public static bool UpdateEntry<T>(string section, string key, T value) where T : IComparable
|
||||
{
|
||||
var entry = GetEntry<T>(section, key);
|
||||
var value2 = entry.Value;
|
||||
var flag = value2.CompareTo(value) == 0;
|
||||
bool flag2;
|
||||
if (flag)
|
||||
{
|
||||
flag2 = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
entry.Value = value;
|
||||
flag2 = true;
|
||||
}
|
||||
return flag2;
|
||||
}
|
||||
|
||||
public static bool RemoveEntry(ConfigDefinition key)
|
||||
{
|
||||
return Config.Remove(key);
|
||||
}
|
||||
|
||||
public static Dictionary<ConfigDefinition, string> GetOrphanedEntries()
|
||||
{
|
||||
var flag = _orphanedEntries == null;
|
||||
if (flag)
|
||||
{
|
||||
_orphanedEntries = Traverse.Create(Config).Property<Dictionary<ConfigDefinition, string>>("OrphanedEntries").Value;
|
||||
}
|
||||
return _orphanedEntries;
|
||||
}
|
||||
|
||||
public static void Migration<T>(string newSection, string newKey, T defaultValue, string oldSection, string oldKey)
|
||||
{
|
||||
GetOrphanedEntries();
|
||||
var configDefinition = new ConfigDefinition(oldSection, oldKey);
|
||||
var flag = _orphanedEntries.TryGetValue(configDefinition, out var text);
|
||||
if (!flag) return;
|
||||
Bind(newSection, newKey, defaultValue).SetSerializedValue(text);
|
||||
_orphanedEntries.Remove(configDefinition);
|
||||
LogManager.LogInfo(string.Concat("migration ", oldSection, ".", oldKey, "(", text, ") => ", newSection, ".", newKey));
|
||||
}
|
||||
|
||||
public static void Save(bool clearOrphanedEntries = false)
|
||||
{
|
||||
if (clearOrphanedEntries)
|
||||
{
|
||||
GetOrphanedEntries().Clear();
|
||||
}
|
||||
Config.Save();
|
||||
LogManager.LogInfo("save config.");
|
||||
}
|
||||
|
||||
public static void Clear()
|
||||
{
|
||||
Config.Clear();
|
||||
}
|
||||
|
||||
public static void Reload()
|
||||
{
|
||||
Config.Reload();
|
||||
}
|
||||
|
||||
private static ConfigManager _instance;
|
||||
|
||||
private static Dictionary<ConfigDefinition, string> _orphanedEntries;
|
||||
|
||||
public enum Step
|
||||
{
|
||||
Awake,
|
||||
GameMainBegin,
|
||||
UniverseGenCreateGalaxy,
|
||||
State,
|
||||
ChangeSeed
|
||||
}
|
||||
}
|
||||
17
CruiseAssist/Commons/EnumUtils.cs
Normal file
17
CruiseAssist/Commons/EnumUtils.cs
Normal file
@@ -0,0 +1,17 @@
|
||||
using System;
|
||||
|
||||
namespace CruiseAssist.Commons;
|
||||
|
||||
internal static class EnumUtils
|
||||
{
|
||||
public static bool TryParse<TEnum>(string value, out TEnum result) where TEnum : struct
|
||||
{
|
||||
if (value == null || !Enum.IsDefined(typeof(TEnum), value))
|
||||
{
|
||||
result = (TEnum)Enum.GetValues(typeof(TEnum)).GetValue(0);
|
||||
return false;
|
||||
}
|
||||
result = (TEnum)Enum.Parse(typeof(TEnum), value);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
29
CruiseAssist/Commons/ListUtils.cs
Normal file
29
CruiseAssist/Commons/ListUtils.cs
Normal file
@@ -0,0 +1,29 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace CruiseAssist.Commons;
|
||||
|
||||
internal static class ListUtils
|
||||
{
|
||||
public static string ToString(List<int> list)
|
||||
{
|
||||
return list == null || list.Count == 0 ? "" : list.Select(id => id.ToString()).Aggregate((a, b) => a + "," + b);
|
||||
}
|
||||
|
||||
public static string ToString(List<string> list)
|
||||
{
|
||||
return list == null || list.Count == 0 ? "" : list.Aggregate((a, b) => a + "," + b);
|
||||
}
|
||||
|
||||
public static List<int> ParseToIntList(string str)
|
||||
{
|
||||
return string.IsNullOrEmpty(str)
|
||||
? []
|
||||
: str.Split(',').Where(s => int.TryParse(s, out _)).Select(int.Parse).ToList();
|
||||
}
|
||||
|
||||
public static List<string> ParseToStringList(string str)
|
||||
{
|
||||
return string.IsNullOrEmpty(str) ? [] : str.Split(',').ToList();
|
||||
}
|
||||
}
|
||||
34
CruiseAssist/Commons/LogManager.cs
Normal file
34
CruiseAssist/Commons/LogManager.cs
Normal file
@@ -0,0 +1,34 @@
|
||||
using System.Reflection;
|
||||
using BepInEx.Logging;
|
||||
|
||||
namespace CruiseAssist.Commons;
|
||||
|
||||
internal static class LogManager
|
||||
{
|
||||
public static ManualLogSource Logger { private get; set; }
|
||||
|
||||
public static void LogInfo(object data)
|
||||
{
|
||||
Logger.LogInfo(data);
|
||||
}
|
||||
|
||||
public static void LogInfo(MethodBase method)
|
||||
{
|
||||
Logger.LogInfo(method.DeclaringType.Name + "." + method.Name);
|
||||
}
|
||||
|
||||
public static void LogInfo(MethodBase method, object data)
|
||||
{
|
||||
Logger.LogInfo(string.Concat(method.DeclaringType.Name, ".", method.Name, ": ", data?.ToString()));
|
||||
}
|
||||
|
||||
public static void LogError(object data)
|
||||
{
|
||||
Logger.LogError(data);
|
||||
}
|
||||
|
||||
public static void LogError(MethodBase method, object data)
|
||||
{
|
||||
Logger.LogError(string.Concat(method.DeclaringType.Name, ".", method.Name, ": ", data?.ToString()));
|
||||
}
|
||||
}
|
||||
8
CruiseAssist/Commons/Tuple.cs
Normal file
8
CruiseAssist/Commons/Tuple.cs
Normal file
@@ -0,0 +1,8 @@
|
||||
namespace CruiseAssist.Commons;
|
||||
|
||||
internal struct Tuple<T1, T2>(T1 v1, T2 v2)
|
||||
{
|
||||
public T1 Item1 { get; set; } = v1;
|
||||
|
||||
public T2 Item2 { get; set; } = v2;
|
||||
}
|
||||
Reference in New Issue
Block a user