mirror of
https://github.com/Outer-Wilds-New-Horizons/new-horizons.git
synced 2025-12-11 20:15:44 +01:00
Don't impulsively change namespace brackets
This commit is contained in:
parent
1a56fe1ab2
commit
f11099d62f
@ -26,314 +26,315 @@ using UnityEngine;
|
|||||||
using UnityEngine.Events;
|
using UnityEngine.Events;
|
||||||
using static NewHorizons.External.Modules.ShipLogModule;
|
using static NewHorizons.External.Modules.ShipLogModule;
|
||||||
|
|
||||||
namespace NewHorizons;
|
namespace NewHorizons
|
||||||
|
|
||||||
public class NewHorizonsApi : INewHorizons
|
|
||||||
{
|
{
|
||||||
[Obsolete("Create(Dictionary<string, object> config) is deprecated, please use LoadConfigs(IModBehaviour mod) instead")]
|
public class NewHorizonsApi : INewHorizons
|
||||||
public void Create(Dictionary<string, object> config)
|
|
||||||
{
|
{
|
||||||
Create(config, null);
|
[Obsolete("Create(Dictionary<string, object> config) is deprecated, please use LoadConfigs(IModBehaviour mod) instead")]
|
||||||
}
|
public void Create(Dictionary<string, object> config)
|
||||||
|
|
||||||
[Obsolete("Create(Dictionary<string, object> config) is deprecated, please use LoadConfigs(IModBehaviour mod) instead")]
|
|
||||||
public void Create(Dictionary<string, object> config, IModBehaviour mod)
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
{
|
||||||
var name = (string)config["Name"];
|
Create(config, null);
|
||||||
|
}
|
||||||
|
|
||||||
NHLogger.LogWarning($"Recieved API request to create planet [{name}]");
|
[Obsolete("Create(Dictionary<string, object> config) is deprecated, please use LoadConfigs(IModBehaviour mod) instead")]
|
||||||
|
public void Create(Dictionary<string, object> config, IModBehaviour mod)
|
||||||
if (name == null) return;
|
{
|
||||||
|
try
|
||||||
var relativePath = $"temp/{name}.json";
|
|
||||||
var fullPath = Path.Combine(Main.Instance.ModHelper.Manifest.ModFolderPath, relativePath);
|
|
||||||
if (!Directory.Exists(Path.Combine(Main.Instance.ModHelper.Manifest.ModFolderPath, "temp")))
|
|
||||||
{
|
{
|
||||||
Directory.CreateDirectory(Path.Combine(Main.Instance.ModHelper.Manifest.ModFolderPath, "temp"));
|
var name = (string)config["Name"];
|
||||||
|
|
||||||
|
NHLogger.LogWarning($"Recieved API request to create planet [{name}]");
|
||||||
|
|
||||||
|
if (name == null) return;
|
||||||
|
|
||||||
|
var relativePath = $"temp/{name}.json";
|
||||||
|
var fullPath = Path.Combine(Main.Instance.ModHelper.Manifest.ModFolderPath, relativePath);
|
||||||
|
if (!Directory.Exists(Path.Combine(Main.Instance.ModHelper.Manifest.ModFolderPath, "temp")))
|
||||||
|
{
|
||||||
|
Directory.CreateDirectory(Path.Combine(Main.Instance.ModHelper.Manifest.ModFolderPath, "temp"));
|
||||||
|
}
|
||||||
|
JsonHelper.SaveJsonObject(fullPath, config);
|
||||||
|
var body = Main.Instance.LoadConfig(Main.Instance, relativePath);
|
||||||
|
File.Delete(fullPath);
|
||||||
|
|
||||||
|
// Update it to point to their mod for textures and stuff
|
||||||
|
body.Mod = mod ?? Main.Instance;
|
||||||
|
|
||||||
|
if (!Main.BodyDict.ContainsKey(body.Config.starSystem)) Main.BodyDict.Add(body.Config.starSystem, new List<NewHorizonsBody>());
|
||||||
|
Main.BodyDict[body.Config.starSystem].Add(body);
|
||||||
}
|
}
|
||||||
JsonHelper.SaveJsonObject(fullPath, config);
|
catch (Exception ex)
|
||||||
var body = Main.Instance.LoadConfig(Main.Instance, relativePath);
|
|
||||||
File.Delete(fullPath);
|
|
||||||
|
|
||||||
// Update it to point to their mod for textures and stuff
|
|
||||||
body.Mod = mod ?? Main.Instance;
|
|
||||||
|
|
||||||
if (!Main.BodyDict.ContainsKey(body.Config.starSystem)) Main.BodyDict.Add(body.Config.starSystem, new List<NewHorizonsBody>());
|
|
||||||
Main.BodyDict[body.Config.starSystem].Add(body);
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
NHLogger.LogError($"Error in Create API:\n{ex}");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
[Obsolete("SpawnObject(GameObject planet, Sector sector, string propToCopyPath, Vector3 position, Vector3 eulerAngles, float scale, bool alignRadial) is deprecated, please use SpawnObject(IModBehaviour mod, GameObject planet, Sector sector, string propToCopyPath, Vector3 position, Vector3 eulerAngles, float scale, bool alignRadial) instead")]
|
|
||||||
public GameObject SpawnObject(GameObject planet, Sector sector, string propToCopyPath, Vector3 position, Vector3 eulerAngles,
|
|
||||||
float scale, bool alignRadial)
|
|
||||||
{
|
|
||||||
return SpawnObject(null, planet, sector, propToCopyPath, position, eulerAngles, scale, alignRadial);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void LoadConfigs(IModBehaviour mod)
|
|
||||||
{
|
|
||||||
Main.Instance.LoadConfigs(mod);
|
|
||||||
}
|
|
||||||
|
|
||||||
public GameObject GetPlanet(string name)
|
|
||||||
{
|
|
||||||
return Main.BodyDict.Values.SelectMany(x => x)?.ToList()?.FirstOrDefault(x => x.Config.name == name)?.Object;
|
|
||||||
}
|
|
||||||
|
|
||||||
public string GetCurrentStarSystem() => Main.Instance.CurrentStarSystem;
|
|
||||||
public UnityEvent<string> GetChangeStarSystemEvent() => Main.Instance.OnChangeStarSystem;
|
|
||||||
public UnityEvent<string> GetStarSystemLoadedEvent() => Main.Instance.OnStarSystemLoaded;
|
|
||||||
public UnityEvent<string> GetBodyLoadedEvent() => Main.Instance.OnPlanetLoaded;
|
|
||||||
|
|
||||||
public bool SetDefaultSystem(string name)
|
|
||||||
{
|
|
||||||
if (!Main.SystemDict.ContainsKey(name)) return false;
|
|
||||||
|
|
||||||
Main.Instance.SetDefaultSystem(name);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
public bool ChangeCurrentStarSystem(string name)
|
|
||||||
{
|
|
||||||
if (!Main.SystemDict.ContainsKey(name)) return false;
|
|
||||||
|
|
||||||
Main.Instance.ChangeCurrentStarSystem(name);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
public string[] GetInstalledAddons()
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
return Main.MountedAddons.Select(x => x?.ModHelper?.Manifest?.UniqueName).ToArray();
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
NHLogger.LogError($"Couldn't get installed addons:\n{ex}");
|
|
||||||
return new string[] { };
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private static object QueryJson(Type outType, string filePath, string jsonPath)
|
|
||||||
{
|
|
||||||
if (filePath == "") return null;
|
|
||||||
try
|
|
||||||
{
|
|
||||||
var jsonText = File.ReadAllText(filePath);
|
|
||||||
var jsonData = JObject.Parse(jsonText);
|
|
||||||
return jsonData.SelectToken(jsonPath)?.ToObject(outType);
|
|
||||||
}
|
|
||||||
catch (FileNotFoundException)
|
|
||||||
{
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
catch (JsonException e)
|
|
||||||
{
|
|
||||||
NHLogger.LogError(e.ToString());
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public object QueryBody(Type outType, string bodyName, string jsonPath)
|
|
||||||
{
|
|
||||||
var planet = Main.BodyDict[Main.Instance.CurrentStarSystem].Find((b) => b.Config.name == bodyName);
|
|
||||||
return planet == null
|
|
||||||
? null
|
|
||||||
: QueryJson(outType, Path.Combine(planet.Mod.ModHelper.Manifest.ModFolderPath, planet.RelativePath), jsonPath);
|
|
||||||
}
|
|
||||||
|
|
||||||
public T QueryBody<T>(string bodyName, string jsonPath)
|
|
||||||
{
|
|
||||||
var data = QueryBody(typeof(T), bodyName, jsonPath);
|
|
||||||
if (data is T result)
|
|
||||||
{
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
return default;
|
|
||||||
}
|
|
||||||
|
|
||||||
public object QuerySystem(Type outType, string jsonPath)
|
|
||||||
{
|
|
||||||
var system = Main.SystemDict[Main.Instance.CurrentStarSystem];
|
|
||||||
return system == null
|
|
||||||
? null
|
|
||||||
: QueryJson(outType, Path.Combine(system.Mod.ModHelper.Manifest.ModFolderPath, system.RelativePath), jsonPath);
|
|
||||||
}
|
|
||||||
|
|
||||||
public T QuerySystem<T>(string jsonPath)
|
|
||||||
{
|
|
||||||
var data = QuerySystem(typeof(T), jsonPath);
|
|
||||||
if (data is T result)
|
|
||||||
{
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
return default;
|
|
||||||
}
|
|
||||||
|
|
||||||
public GameObject SpawnObject(IModBehaviour mod, GameObject planet, Sector sector, string propToCopyPath, Vector3 position, Vector3 eulerAngles,
|
|
||||||
float scale, bool alignRadial)
|
|
||||||
{
|
|
||||||
var prefab = SearchUtilities.Find(propToCopyPath);
|
|
||||||
var detailInfo = new DetailInfo()
|
|
||||||
{
|
|
||||||
position = position,
|
|
||||||
rotation = eulerAngles,
|
|
||||||
scale = scale,
|
|
||||||
alignRadial = alignRadial
|
|
||||||
};
|
|
||||||
return DetailBuilder.Make(planet, sector, mod, prefab, detailInfo);
|
|
||||||
}
|
|
||||||
|
|
||||||
public AudioSignal SpawnSignal(IModBehaviour mod, GameObject root, string audio, string name, string frequency,
|
|
||||||
float sourceRadius = 1f, float detectionRadius = 20f, float identificationRadius = 10f, bool insideCloak = false,
|
|
||||||
bool onlyAudibleToScope = true, string reveals = "")
|
|
||||||
{
|
|
||||||
var info = new SignalInfo()
|
|
||||||
{
|
|
||||||
audio = audio,
|
|
||||||
detectionRadius = detectionRadius,
|
|
||||||
frequency = frequency,
|
|
||||||
identificationRadius = identificationRadius,
|
|
||||||
insideCloak = insideCloak,
|
|
||||||
name = name,
|
|
||||||
onlyAudibleToScope = onlyAudibleToScope,
|
|
||||||
position = Vector3.zero,
|
|
||||||
reveals = reveals,
|
|
||||||
sourceRadius = sourceRadius
|
|
||||||
};
|
|
||||||
|
|
||||||
return SignalBuilder.Make(root, null, info, mod).GetComponent<AudioSignal>();
|
|
||||||
}
|
|
||||||
|
|
||||||
public (CharacterDialogueTree, RemoteDialogueTrigger) SpawnDialogue(IModBehaviour mod, GameObject root, string xmlFile, float radius = 1f,
|
|
||||||
float range = 1f, string blockAfterPersistentCondition = null, float lookAtRadius = 1f, string pathToAnimController = null,
|
|
||||||
float remoteTriggerRadius = 0f)
|
|
||||||
{
|
|
||||||
var info = new DialogueInfo()
|
|
||||||
{
|
|
||||||
blockAfterPersistentCondition = blockAfterPersistentCondition,
|
|
||||||
lookAtRadius = lookAtRadius,
|
|
||||||
pathToAnimController = pathToAnimController,
|
|
||||||
position = Vector3.zero,
|
|
||||||
radius = radius,
|
|
||||||
range = range,
|
|
||||||
xmlFile = xmlFile,
|
|
||||||
remoteTrigger = remoteTriggerRadius > 0f ? new RemoteTriggerInfo()
|
|
||||||
{
|
{
|
||||||
position = null,
|
NHLogger.LogError($"Error in Create API:\n{ex}");
|
||||||
radius = remoteTriggerRadius,
|
|
||||||
} : null,
|
|
||||||
};
|
|
||||||
|
|
||||||
return DialogueBuilder.Make(root, null, info, mod);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void CreatePlanet(string config, IModBehaviour mod)
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
var planet = JsonConvert.DeserializeObject<PlanetConfig>(config);
|
|
||||||
if (planet == null)
|
|
||||||
{
|
|
||||||
NHLogger.LogError($"Couldn't load planet via API. Is your Json formatted correctly? {config}");
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var body = Main.Instance.RegisterPlanetConfig(planet, mod, null);
|
|
||||||
|
|
||||||
if (!Main.BodyDict.ContainsKey(body.Config.starSystem)) Main.BodyDict.Add(body.Config.starSystem, new List<NewHorizonsBody>());
|
|
||||||
Main.BodyDict[body.Config.starSystem].Add(body);
|
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
|
||||||
|
[Obsolete("SpawnObject(GameObject planet, Sector sector, string propToCopyPath, Vector3 position, Vector3 eulerAngles, float scale, bool alignRadial) is deprecated, please use SpawnObject(IModBehaviour mod, GameObject planet, Sector sector, string propToCopyPath, Vector3 position, Vector3 eulerAngles, float scale, bool alignRadial) instead")]
|
||||||
|
public GameObject SpawnObject(GameObject planet, Sector sector, string propToCopyPath, Vector3 position, Vector3 eulerAngles,
|
||||||
|
float scale, bool alignRadial)
|
||||||
{
|
{
|
||||||
NHLogger.LogError($"Error in CreatePlanet API:\n{ex}");
|
return SpawnObject(null, planet, sector, propToCopyPath, position, eulerAngles, scale, alignRadial);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public void DefineStarSystem(string name, string config, IModBehaviour mod)
|
public void LoadConfigs(IModBehaviour mod)
|
||||||
{
|
|
||||||
var starSystemConfig = JsonConvert.DeserializeObject<StarSystemConfig>(config);
|
|
||||||
Main.Instance.LoadStarSystemConfig(name, starSystemConfig, null, mod);
|
|
||||||
}
|
|
||||||
|
|
||||||
public (CharacterDialogueTree, RemoteDialogueTrigger) CreateDialogueFromXML(string textAssetID, string xml, string dialogueInfo, GameObject planetGO)
|
|
||||||
{
|
|
||||||
var info = JsonConvert.DeserializeObject<DialogueInfo>(dialogueInfo);
|
|
||||||
return DialogueBuilder.Make(planetGO, null, info, xml, textAssetID);
|
|
||||||
}
|
|
||||||
|
|
||||||
public GameObject CreateNomaiText(string xml, string textInfo, GameObject planetGO)
|
|
||||||
{
|
|
||||||
var info = JsonConvert.DeserializeObject<TranslatorTextInfo>(textInfo);
|
|
||||||
return TranslatorTextBuilder.Make(planetGO, null, info, null, xml);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void AddShipLogXML(IModBehaviour mod, XElement xml, string planetName, string imageFolder, Dictionary<string, Vector2> entryPositions, Dictionary<string, (Color colour, Color highlight)> curiousityColours)
|
|
||||||
{
|
|
||||||
// This method has to be called each time the ship log manager is created, i.e. each time a system loads so it will only ever be relevant to the current one.
|
|
||||||
var starSystem = Main.Instance.CurrentStarSystem;
|
|
||||||
|
|
||||||
var body = new NewHorizonsBody(new PlanetConfig()
|
|
||||||
{
|
{
|
||||||
name = planetName,
|
Main.Instance.LoadConfigs(mod);
|
||||||
starSystem = starSystem,
|
}
|
||||||
ShipLog = new ShipLogModule()
|
|
||||||
|
public GameObject GetPlanet(string name)
|
||||||
|
{
|
||||||
|
return Main.BodyDict.Values.SelectMany(x => x)?.ToList()?.FirstOrDefault(x => x.Config.name == name)?.Object;
|
||||||
|
}
|
||||||
|
|
||||||
|
public string GetCurrentStarSystem() => Main.Instance.CurrentStarSystem;
|
||||||
|
public UnityEvent<string> GetChangeStarSystemEvent() => Main.Instance.OnChangeStarSystem;
|
||||||
|
public UnityEvent<string> GetStarSystemLoadedEvent() => Main.Instance.OnStarSystemLoaded;
|
||||||
|
public UnityEvent<string> GetBodyLoadedEvent() => Main.Instance.OnPlanetLoaded;
|
||||||
|
|
||||||
|
public bool SetDefaultSystem(string name)
|
||||||
|
{
|
||||||
|
if (!Main.SystemDict.ContainsKey(name)) return false;
|
||||||
|
|
||||||
|
Main.Instance.SetDefaultSystem(name);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool ChangeCurrentStarSystem(string name)
|
||||||
|
{
|
||||||
|
if (!Main.SystemDict.ContainsKey(name)) return false;
|
||||||
|
|
||||||
|
Main.Instance.ChangeCurrentStarSystem(name);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public string[] GetInstalledAddons()
|
||||||
|
{
|
||||||
|
try
|
||||||
{
|
{
|
||||||
spriteFolder = imageFolder
|
return Main.MountedAddons.Select(x => x?.ModHelper?.Manifest?.UniqueName).ToArray();
|
||||||
}
|
}
|
||||||
}, mod);
|
catch (Exception ex)
|
||||||
|
|
||||||
if (!Main.BodyDict.ContainsKey(starSystem))
|
|
||||||
{
|
|
||||||
Main.BodyDict.Add(starSystem, new List<NewHorizonsBody>());
|
|
||||||
Main.BodyDict[starSystem].Add(body);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
var existingBody = Main.BodyDict[starSystem]
|
|
||||||
.FirstOrDefault(x => x.Config.name == planetName && x.Mod.ModHelper.Manifest.UniqueName == mod.ModHelper.Manifest.UniqueName);
|
|
||||||
if (existingBody != null)
|
|
||||||
{
|
{
|
||||||
body = existingBody;
|
NHLogger.LogError($"Couldn't get installed addons:\n{ex}");
|
||||||
|
return new string[] { };
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static object QueryJson(Type outType, string filePath, string jsonPath)
|
||||||
|
{
|
||||||
|
if (filePath == "") return null;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var jsonText = File.ReadAllText(filePath);
|
||||||
|
var jsonData = JObject.Parse(jsonText);
|
||||||
|
return jsonData.SelectToken(jsonPath)?.ToObject(outType);
|
||||||
|
}
|
||||||
|
catch (FileNotFoundException)
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
catch (JsonException e)
|
||||||
|
{
|
||||||
|
NHLogger.LogError(e.ToString());
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public object QueryBody(Type outType, string bodyName, string jsonPath)
|
||||||
|
{
|
||||||
|
var planet = Main.BodyDict[Main.Instance.CurrentStarSystem].Find((b) => b.Config.name == bodyName);
|
||||||
|
return planet == null
|
||||||
|
? null
|
||||||
|
: QueryJson(outType, Path.Combine(planet.Mod.ModHelper.Manifest.ModFolderPath, planet.RelativePath), jsonPath);
|
||||||
|
}
|
||||||
|
|
||||||
|
public T QueryBody<T>(string bodyName, string jsonPath)
|
||||||
|
{
|
||||||
|
var data = QueryBody(typeof(T), bodyName, jsonPath);
|
||||||
|
if (data is T result)
|
||||||
|
{
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
return default;
|
||||||
|
}
|
||||||
|
|
||||||
|
public object QuerySystem(Type outType, string jsonPath)
|
||||||
|
{
|
||||||
|
var system = Main.SystemDict[Main.Instance.CurrentStarSystem];
|
||||||
|
return system == null
|
||||||
|
? null
|
||||||
|
: QueryJson(outType, Path.Combine(system.Mod.ModHelper.Manifest.ModFolderPath, system.RelativePath), jsonPath);
|
||||||
|
}
|
||||||
|
|
||||||
|
public T QuerySystem<T>(string jsonPath)
|
||||||
|
{
|
||||||
|
var data = QuerySystem(typeof(T), jsonPath);
|
||||||
|
if (data is T result)
|
||||||
|
{
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
return default;
|
||||||
|
}
|
||||||
|
|
||||||
|
public GameObject SpawnObject(IModBehaviour mod, GameObject planet, Sector sector, string propToCopyPath, Vector3 position, Vector3 eulerAngles,
|
||||||
|
float scale, bool alignRadial)
|
||||||
|
{
|
||||||
|
var prefab = SearchUtilities.Find(propToCopyPath);
|
||||||
|
var detailInfo = new DetailInfo()
|
||||||
|
{
|
||||||
|
position = position,
|
||||||
|
rotation = eulerAngles,
|
||||||
|
scale = scale,
|
||||||
|
alignRadial = alignRadial
|
||||||
|
};
|
||||||
|
return DetailBuilder.Make(planet, sector, mod, prefab, detailInfo);
|
||||||
|
}
|
||||||
|
|
||||||
|
public AudioSignal SpawnSignal(IModBehaviour mod, GameObject root, string audio, string name, string frequency,
|
||||||
|
float sourceRadius = 1f, float detectionRadius = 20f, float identificationRadius = 10f, bool insideCloak = false,
|
||||||
|
bool onlyAudibleToScope = true, string reveals = "")
|
||||||
|
{
|
||||||
|
var info = new SignalInfo()
|
||||||
|
{
|
||||||
|
audio = audio,
|
||||||
|
detectionRadius = detectionRadius,
|
||||||
|
frequency = frequency,
|
||||||
|
identificationRadius = identificationRadius,
|
||||||
|
insideCloak = insideCloak,
|
||||||
|
name = name,
|
||||||
|
onlyAudibleToScope = onlyAudibleToScope,
|
||||||
|
position = Vector3.zero,
|
||||||
|
reveals = reveals,
|
||||||
|
sourceRadius = sourceRadius
|
||||||
|
};
|
||||||
|
|
||||||
|
return SignalBuilder.Make(root, null, info, mod).GetComponent<AudioSignal>();
|
||||||
|
}
|
||||||
|
|
||||||
|
public (CharacterDialogueTree, RemoteDialogueTrigger) SpawnDialogue(IModBehaviour mod, GameObject root, string xmlFile, float radius = 1f,
|
||||||
|
float range = 1f, string blockAfterPersistentCondition = null, float lookAtRadius = 1f, string pathToAnimController = null,
|
||||||
|
float remoteTriggerRadius = 0f)
|
||||||
|
{
|
||||||
|
var info = new DialogueInfo()
|
||||||
|
{
|
||||||
|
blockAfterPersistentCondition = blockAfterPersistentCondition,
|
||||||
|
lookAtRadius = lookAtRadius,
|
||||||
|
pathToAnimController = pathToAnimController,
|
||||||
|
position = Vector3.zero,
|
||||||
|
radius = radius,
|
||||||
|
range = range,
|
||||||
|
xmlFile = xmlFile,
|
||||||
|
remoteTrigger = remoteTriggerRadius > 0f ? new RemoteTriggerInfo()
|
||||||
|
{
|
||||||
|
position = null,
|
||||||
|
radius = remoteTriggerRadius,
|
||||||
|
} : null,
|
||||||
|
};
|
||||||
|
|
||||||
|
return DialogueBuilder.Make(root, null, info, mod);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void CreatePlanet(string config, IModBehaviour mod)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var planet = JsonConvert.DeserializeObject<PlanetConfig>(config);
|
||||||
|
if (planet == null)
|
||||||
|
{
|
||||||
|
NHLogger.LogError($"Couldn't load planet via API. Is your Json formatted correctly? {config}");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
var body = Main.Instance.RegisterPlanetConfig(planet, mod, null);
|
||||||
|
|
||||||
|
if (!Main.BodyDict.ContainsKey(body.Config.starSystem)) Main.BodyDict.Add(body.Config.starSystem, new List<NewHorizonsBody>());
|
||||||
|
Main.BodyDict[body.Config.starSystem].Add(body);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
NHLogger.LogError($"Error in CreatePlanet API:\n{ex}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void DefineStarSystem(string name, string config, IModBehaviour mod)
|
||||||
|
{
|
||||||
|
var starSystemConfig = JsonConvert.DeserializeObject<StarSystemConfig>(config);
|
||||||
|
Main.Instance.LoadStarSystemConfig(name, starSystemConfig, null, mod);
|
||||||
|
}
|
||||||
|
|
||||||
|
public (CharacterDialogueTree, RemoteDialogueTrigger) CreateDialogueFromXML(string textAssetID, string xml, string dialogueInfo, GameObject planetGO)
|
||||||
|
{
|
||||||
|
var info = JsonConvert.DeserializeObject<DialogueInfo>(dialogueInfo);
|
||||||
|
return DialogueBuilder.Make(planetGO, null, info, xml, textAssetID);
|
||||||
|
}
|
||||||
|
|
||||||
|
public GameObject CreateNomaiText(string xml, string textInfo, GameObject planetGO)
|
||||||
|
{
|
||||||
|
var info = JsonConvert.DeserializeObject<TranslatorTextInfo>(textInfo);
|
||||||
|
return TranslatorTextBuilder.Make(planetGO, null, info, null, xml);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void AddShipLogXML(IModBehaviour mod, XElement xml, string planetName, string imageFolder, Dictionary<string, Vector2> entryPositions, Dictionary<string, (Color colour, Color highlight)> curiousityColours)
|
||||||
|
{
|
||||||
|
// This method has to be called each time the ship log manager is created, i.e. each time a system loads so it will only ever be relevant to the current one.
|
||||||
|
var starSystem = Main.Instance.CurrentStarSystem;
|
||||||
|
|
||||||
|
var body = new NewHorizonsBody(new PlanetConfig()
|
||||||
|
{
|
||||||
|
name = planetName,
|
||||||
|
starSystem = starSystem,
|
||||||
|
ShipLog = new ShipLogModule()
|
||||||
|
{
|
||||||
|
spriteFolder = imageFolder
|
||||||
|
}
|
||||||
|
}, mod);
|
||||||
|
|
||||||
|
if (!Main.BodyDict.ContainsKey(starSystem))
|
||||||
|
{
|
||||||
|
Main.BodyDict.Add(starSystem, new List<NewHorizonsBody>());
|
||||||
|
Main.BodyDict[starSystem].Add(body);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
Main.BodyDict[starSystem].Add(body);
|
var existingBody = Main.BodyDict[starSystem]
|
||||||
|
.FirstOrDefault(x => x.Config.name == planetName && x.Mod.ModHelper.Manifest.UniqueName == mod.ModHelper.Manifest.UniqueName);
|
||||||
|
if (existingBody != null)
|
||||||
|
{
|
||||||
|
body = existingBody;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Main.BodyDict[starSystem].Add(body);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var system = new StarSystemConfig()
|
||||||
|
{
|
||||||
|
entryPositions = entryPositions?
|
||||||
|
.Select((pair) => new EntryPositionInfo() { id = pair.Key, position = pair.Value })
|
||||||
|
.ToArray(),
|
||||||
|
curiosities = curiousityColours?
|
||||||
|
.Select((pair) => new CuriosityColorInfo() { id = pair.Key, color = MColor.FromColor(pair.Value.colour), highlightColor = MColor.FromColor(pair.Value.highlight) })
|
||||||
|
.ToArray()
|
||||||
|
};
|
||||||
|
|
||||||
|
Main.Instance.LoadStarSystemConfig(starSystem, system, null, mod);
|
||||||
|
|
||||||
|
RumorModeBuilder.AddShipLogXML(GameObject.FindObjectOfType<ShipLogManager>(), xml, body);
|
||||||
}
|
}
|
||||||
|
|
||||||
var system = new StarSystemConfig()
|
/// <summary>
|
||||||
{
|
/// Register your own builder that will act on the given GameObject by reading its raw json string
|
||||||
entryPositions = entryPositions?
|
/// </summary>
|
||||||
.Select((pair) => new EntryPositionInfo() { id = pair.Key, position = pair.Value })
|
/// <param name="builder"></param>
|
||||||
.ToArray(),
|
public void RegisterCustomBuilder(Action<GameObject, string> builder) => PlanetCreationHandler.CustomBuilders.Add(builder);
|
||||||
curiosities = curiousityColours?
|
|
||||||
.Select((pair) => new CuriosityColorInfo() { id = pair.Key, color = MColor.FromColor(pair.Value.colour), highlightColor = MColor.FromColor(pair.Value.highlight) })
|
|
||||||
.ToArray()
|
|
||||||
};
|
|
||||||
|
|
||||||
Main.Instance.LoadStarSystemConfig(starSystem, system, null, mod);
|
public string GetTranslationForShipLog(string text) => TranslationHandler.GetTranslation(text, TranslationHandler.TextType.SHIPLOG);
|
||||||
|
|
||||||
RumorModeBuilder.AddShipLogXML(GameObject.FindObjectOfType<ShipLogManager>(), xml, body);
|
public string GetTranslationForDialogue(string text) => TranslationHandler.GetTranslation(text, TranslationHandler.TextType.DIALOGUE);
|
||||||
|
|
||||||
|
public string GetTranslationForUI(string text) => TranslationHandler.GetTranslation(text, TranslationHandler.TextType.UI);
|
||||||
|
|
||||||
|
public string GetTranslationForOtherText(string text) => TranslationHandler.GetTranslation(text, TranslationHandler.TextType.OTHER);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Register your own builder that will act on the given GameObject by reading its raw json string
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="builder"></param>
|
|
||||||
public void RegisterCustomBuilder(Action<GameObject, string> builder) => PlanetCreationHandler.CustomBuilders.Add(builder);
|
|
||||||
|
|
||||||
public string GetTranslationForShipLog(string text) => TranslationHandler.GetTranslation(text, TranslationHandler.TextType.SHIPLOG);
|
|
||||||
|
|
||||||
public string GetTranslationForDialogue(string text) => TranslationHandler.GetTranslation(text, TranslationHandler.TextType.DIALOGUE);
|
|
||||||
|
|
||||||
public string GetTranslationForUI(string text) => TranslationHandler.GetTranslation(text, TranslationHandler.TextType.UI);
|
|
||||||
|
|
||||||
public string GetTranslationForOtherText(string text) => TranslationHandler.GetTranslation(text, TranslationHandler.TextType.OTHER);
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -6,142 +6,143 @@ using System.IO;
|
|||||||
using System.Linq;
|
using System.Linq;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
|
|
||||||
namespace NewHorizons.Utility.Files;
|
namespace NewHorizons.Utility.Files
|
||||||
|
|
||||||
public static class AssetBundleUtilities
|
|
||||||
{
|
{
|
||||||
public static Dictionary<string, (AssetBundle bundle, bool keepLoaded)> AssetBundles = new Dictionary<string, (AssetBundle, bool)>();
|
public static class AssetBundleUtilities
|
||||||
|
|
||||||
public static void ClearCache()
|
|
||||||
{
|
{
|
||||||
foreach (var pair in AssetBundles)
|
public static Dictionary<string, (AssetBundle bundle, bool keepLoaded)> AssetBundles = new Dictionary<string, (AssetBundle, bool)>();
|
||||||
|
|
||||||
|
public static void ClearCache()
|
||||||
{
|
{
|
||||||
if (!pair.Value.keepLoaded)
|
foreach (var pair in AssetBundles)
|
||||||
{
|
{
|
||||||
if (pair.Value.bundle == null)
|
if (!pair.Value.keepLoaded)
|
||||||
{
|
{
|
||||||
NHLogger.LogError($"The asset bundle for {pair.Key} was null when trying to unload");
|
if (pair.Value.bundle == null)
|
||||||
|
{
|
||||||
|
NHLogger.LogError($"The asset bundle for {pair.Key} was null when trying to unload");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
pair.Value.bundle.Unload(true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
AssetBundles = AssetBundles.Where(x => x.Value.keepLoaded).ToDictionary(x => x.Key, x => x.Value);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void PreloadBundle(string assetBundleRelativeDir, IModBehaviour mod)
|
||||||
|
{
|
||||||
|
string key = Path.GetFileName(assetBundleRelativeDir);
|
||||||
|
var completePath = Path.Combine(mod.ModHelper.Manifest.ModFolderPath, assetBundleRelativeDir);
|
||||||
|
var request = AssetBundle.LoadFromFileAsync(completePath);
|
||||||
|
request.completed += _ =>
|
||||||
|
{
|
||||||
|
NHLogger.Log($"Finished loading async bundle {assetBundleRelativeDir}");
|
||||||
|
AssetBundles[key] = (request.assetBundle, true);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
public static T Load<T>(string assetBundleRelativeDir, string pathInBundle, IModBehaviour mod) where T : UnityEngine.Object
|
||||||
|
{
|
||||||
|
string key = Path.GetFileName(assetBundleRelativeDir);
|
||||||
|
T obj;
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
AssetBundle bundle;
|
||||||
|
|
||||||
|
if (AssetBundles.ContainsKey(key))
|
||||||
|
{
|
||||||
|
bundle = AssetBundles[key].bundle;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
pair.Value.bundle.Unload(true);
|
var completePath = Path.Combine(mod.ModHelper.Manifest.ModFolderPath, assetBundleRelativeDir);
|
||||||
|
bundle = AssetBundle.LoadFromFile(completePath);
|
||||||
|
if (bundle == null)
|
||||||
|
{
|
||||||
|
NHLogger.LogError($"Couldn't load AssetBundle at [{completePath}] for [{mod.ModHelper.Manifest.Name}]");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
AssetBundles[key] = (bundle, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
obj = bundle.LoadAsset<T>(pathInBundle);
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
NHLogger.LogError($"Couldn't load asset {pathInBundle} from AssetBundle {assetBundleRelativeDir}:\n{e}");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return obj;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static GameObject LoadPrefab(string assetBundleRelativeDir, string pathInBundle, IModBehaviour mod)
|
||||||
|
{
|
||||||
|
var prefab = Load<GameObject>(assetBundleRelativeDir, pathInBundle, mod);
|
||||||
|
|
||||||
|
prefab.SetActive(false);
|
||||||
|
|
||||||
|
ReplaceShaders(prefab);
|
||||||
|
|
||||||
|
return prefab;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void ReplaceShaders(GameObject prefab)
|
||||||
|
{
|
||||||
|
foreach (var renderer in prefab.GetComponentsInChildren<Renderer>(true))
|
||||||
|
{
|
||||||
|
foreach (var material in renderer.sharedMaterials)
|
||||||
|
{
|
||||||
|
if (material == null) continue;
|
||||||
|
|
||||||
|
var replacementShader = Shader.Find(material.shader.name);
|
||||||
|
if (replacementShader == null) continue;
|
||||||
|
|
||||||
|
// preserve override tag and render queue (for Standard shader)
|
||||||
|
// keywords and properties are already preserved
|
||||||
|
if (material.renderQueue != material.shader.renderQueue)
|
||||||
|
{
|
||||||
|
var renderType = material.GetTag("RenderType", false);
|
||||||
|
var renderQueue = material.renderQueue;
|
||||||
|
material.shader = replacementShader;
|
||||||
|
material.SetOverrideTag("RenderType", renderType);
|
||||||
|
material.renderQueue = renderQueue;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
material.shader = replacementShader;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
foreach (var trenderer in prefab.GetComponentsInChildren<TessellatedRenderer>(true))
|
||||||
AssetBundles = AssetBundles.Where(x => x.Value.keepLoaded).ToDictionary(x => x.Key, x => x.Value);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void PreloadBundle(string assetBundleRelativeDir, IModBehaviour mod)
|
|
||||||
{
|
|
||||||
string key = Path.GetFileName(assetBundleRelativeDir);
|
|
||||||
var completePath = Path.Combine(mod.ModHelper.Manifest.ModFolderPath, assetBundleRelativeDir);
|
|
||||||
var request = AssetBundle.LoadFromFileAsync(completePath);
|
|
||||||
request.completed += _ =>
|
|
||||||
{
|
|
||||||
NHLogger.Log($"Finished loading async bundle {assetBundleRelativeDir}");
|
|
||||||
AssetBundles[key] = (request.assetBundle, true);
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
public static T Load<T>(string assetBundleRelativeDir, string pathInBundle, IModBehaviour mod) where T : UnityEngine.Object
|
|
||||||
{
|
|
||||||
string key = Path.GetFileName(assetBundleRelativeDir);
|
|
||||||
T obj;
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
AssetBundle bundle;
|
|
||||||
|
|
||||||
if (AssetBundles.ContainsKey(key))
|
|
||||||
{
|
{
|
||||||
bundle = AssetBundles[key].bundle;
|
foreach (var material in trenderer.sharedMaterials)
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
var completePath = Path.Combine(mod.ModHelper.Manifest.ModFolderPath, assetBundleRelativeDir);
|
|
||||||
bundle = AssetBundle.LoadFromFile(completePath);
|
|
||||||
if (bundle == null)
|
|
||||||
{
|
{
|
||||||
NHLogger.LogError($"Couldn't load AssetBundle at [{completePath}] for [{mod.ModHelper.Manifest.Name}]");
|
if (material == null) continue;
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
AssetBundles[key] = (bundle, false);
|
var replacementShader = Shader.Find(material.shader.name);
|
||||||
}
|
if (replacementShader == null) continue;
|
||||||
|
|
||||||
obj = bundle.LoadAsset<T>(pathInBundle);
|
// preserve override tag and render queue (for Standard shader)
|
||||||
}
|
// keywords and properties are already preserved
|
||||||
catch (Exception e)
|
if (material.renderQueue != material.shader.renderQueue)
|
||||||
{
|
{
|
||||||
NHLogger.LogError($"Couldn't load asset {pathInBundle} from AssetBundle {assetBundleRelativeDir}:\n{e}");
|
var renderType = material.GetTag("RenderType", false);
|
||||||
return null;
|
var renderQueue = material.renderQueue;
|
||||||
}
|
material.shader = replacementShader;
|
||||||
|
material.SetOverrideTag("RenderType", renderType);
|
||||||
return obj;
|
material.renderQueue = renderQueue;
|
||||||
}
|
}
|
||||||
|
else
|
||||||
public static GameObject LoadPrefab(string assetBundleRelativeDir, string pathInBundle, IModBehaviour mod)
|
{
|
||||||
{
|
material.shader = replacementShader;
|
||||||
var prefab = Load<GameObject>(assetBundleRelativeDir, pathInBundle, mod);
|
}
|
||||||
|
|
||||||
prefab.SetActive(false);
|
|
||||||
|
|
||||||
ReplaceShaders(prefab);
|
|
||||||
|
|
||||||
return prefab;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void ReplaceShaders(GameObject prefab)
|
|
||||||
{
|
|
||||||
foreach (var renderer in prefab.GetComponentsInChildren<Renderer>(true))
|
|
||||||
{
|
|
||||||
foreach (var material in renderer.sharedMaterials)
|
|
||||||
{
|
|
||||||
if (material == null) continue;
|
|
||||||
|
|
||||||
var replacementShader = Shader.Find(material.shader.name);
|
|
||||||
if (replacementShader == null) continue;
|
|
||||||
|
|
||||||
// preserve override tag and render queue (for Standard shader)
|
|
||||||
// keywords and properties are already preserved
|
|
||||||
if (material.renderQueue != material.shader.renderQueue)
|
|
||||||
{
|
|
||||||
var renderType = material.GetTag("RenderType", false);
|
|
||||||
var renderQueue = material.renderQueue;
|
|
||||||
material.shader = replacementShader;
|
|
||||||
material.SetOverrideTag("RenderType", renderType);
|
|
||||||
material.renderQueue = renderQueue;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
material.shader = replacementShader;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach (var trenderer in prefab.GetComponentsInChildren<TessellatedRenderer>(true))
|
|
||||||
{
|
|
||||||
foreach (var material in trenderer.sharedMaterials)
|
|
||||||
{
|
|
||||||
if (material == null) continue;
|
|
||||||
|
|
||||||
var replacementShader = Shader.Find(material.shader.name);
|
|
||||||
if (replacementShader == null) continue;
|
|
||||||
|
|
||||||
// preserve override tag and render queue (for Standard shader)
|
|
||||||
// keywords and properties are already preserved
|
|
||||||
if (material.renderQueue != material.shader.renderQueue)
|
|
||||||
{
|
|
||||||
var renderType = material.GetTag("RenderType", false);
|
|
||||||
var renderQueue = material.renderQueue;
|
|
||||||
material.shader = replacementShader;
|
|
||||||
material.SetOverrideTag("RenderType", renderType);
|
|
||||||
material.renderQueue = renderQueue;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
material.shader = replacementShader;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user