migrate to new data API

SMAPI 3.0 no longer allows access to arbitrary file paths through its APIs, so this commit refactors affected code to use relative paths instead.
This commit is contained in:
Jesse Plamondon-Willard 2019-01-06 00:31:40 -05:00
parent cfe5cdc49e
commit 7ba11188bc
No known key found for this signature in database
GPG Key ID: 7D7C8097B62033CE
17 changed files with 106 additions and 176 deletions

View File

@ -14,7 +14,7 @@ namespace Omegasis.BuildEndurance
** Properties ** Properties
*********/ *********/
/// <summary>The relative path for the current player's data file.</summary> /// <summary>The relative path for the current player's data file.</summary>
private string DataFilePath => Path.Combine("data", $"{Constants.SaveFolderName}.json"); private string RelativeDataPath => Path.Combine("data", $"{Constants.SaveFolderName}.json");
/// <summary>The mod settings.</summary> /// <summary>The mod settings.</summary>
private ModConfig Config; private ModConfig Config;
@ -124,7 +124,7 @@ namespace Omegasis.BuildEndurance
this.WasEating = false; this.WasEating = false;
// load player data // load player data
this.PlayerData = this.Helper.ReadJsonFile<PlayerData>(this.DataFilePath) ?? new PlayerData(); this.PlayerData = this.Helper.Data.ReadJsonFile<PlayerData>(this.RelativeDataPath) ?? new PlayerData();
if (this.PlayerData.OriginalMaxStamina == 0) if (this.PlayerData.OriginalMaxStamina == 0)
this.PlayerData.OriginalMaxStamina = Game1.player.MaxStamina; this.PlayerData.OriginalMaxStamina = Game1.player.MaxStamina;
@ -179,7 +179,7 @@ namespace Omegasis.BuildEndurance
this.PlayerData.NightlyStamina = Game1.player.MaxStamina; this.PlayerData.NightlyStamina = Game1.player.MaxStamina;
// save data // save data
this.Helper.WriteJsonFile(this.DataFilePath, this.PlayerData); this.Helper.Data.WriteJsonFile(this.RelativeDataPath, this.PlayerData);
} }
/// <summary>Try and emulate the old Game1.shouldFarmerPassout logic.</summary> /// <summary>Try and emulate the old Game1.shouldFarmerPassout logic.</summary>

View File

@ -14,7 +14,7 @@ namespace Omegasis.BuildHealth
** Properties ** Properties
*********/ *********/
/// <summary>The relative path for the current player's data file.</summary> /// <summary>The relative path for the current player's data file.</summary>
private string DataFilePath => Path.Combine("data", $"{Constants.SaveFolderName}.json"); private string RelativeDataPath => Path.Combine("data", $"{Constants.SaveFolderName}.json");
/// <summary>The mod settings and player data.</summary> /// <summary>The mod settings and player data.</summary>
private ModConfig Config; private ModConfig Config;
@ -118,7 +118,7 @@ namespace Omegasis.BuildHealth
this.WasCollapsed = false; this.WasCollapsed = false;
// load player data // load player data
this.PlayerData = this.Helper.ReadJsonFile<PlayerData>(this.DataFilePath) ?? new PlayerData(); this.PlayerData = this.Helper.Data.ReadJsonFile<PlayerData>(this.RelativeDataPath) ?? new PlayerData();
if (this.PlayerData.OriginalMaxHealth == 0) if (this.PlayerData.OriginalMaxHealth == 0)
this.PlayerData.OriginalMaxHealth = Game1.player.maxHealth; this.PlayerData.OriginalMaxHealth = Game1.player.maxHealth;
@ -169,7 +169,7 @@ namespace Omegasis.BuildHealth
} }
// save data // save data
this.Helper.WriteJsonFile(this.DataFilePath, this.PlayerData); this.Helper.Data.WriteJsonFile(this.RelativeDataPath, this.PlayerData);
} }
public bool shouldFarmerPassout() public bool shouldFarmerPassout()

View File

@ -1,6 +1,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq;
using CustomNPCFramework.Framework.Enums; using CustomNPCFramework.Framework.Enums;
using CustomNPCFramework.Framework.Graphics; using CustomNPCFramework.Framework.Graphics;
using CustomNPCFramework.Framework.ModularNpcs.ColorCollections; using CustomNPCFramework.Framework.ModularNpcs.ColorCollections;
@ -82,8 +83,8 @@ namespace CustomNPCFramework
/// <summary>Initialize the asset pool with some test variables.</summary> /// <summary>Initialize the asset pool with some test variables.</summary>
public void initializeAssetPool() public void initializeAssetPool()
{ {
string path = Path.Combine(ModHelper.DirectoryPath, "Content", "Graphics", "NPCS"); string relativePath = Path.Combine("Content", "Graphics", "NPCS");
assetPool.getAssetManager("testNPC").addPathCreateDirectory(new KeyValuePair<string, string>("characters", path)); assetPool.getAssetManager("testNPC").addPathCreateDirectory(new KeyValuePair<string, string>("characters", relativePath));
} }
/// <summary>A function that is called when the game finishes saving.</summary> /// <summary>A function that is called when the game finishes saving.</summary>
@ -145,52 +146,40 @@ namespace CustomNPCFramework
public void initializeExamples() public void initializeExamples()
{ {
return; return;
string dirPath = Path.Combine(ModHelper.DirectoryPath, "Content", "Templates"); string relativeDirPath = Path.Combine("Content", "Templates");
var aManager = assetPool.getAssetManager("testNPC"); var aManager = assetPool.getAssetManager("testNPC");
aManager.addPathCreateDirectory(new KeyValuePair<string, string>("templates", dirPath)); aManager.addPathCreateDirectory(new KeyValuePair<string, string>("templates", relativeDirPath));
string filePath = Path.Combine(dirPath, "Example.json");
if (!File.Exists(filePath)) // write example
{ {
string getRelativePath = getShortenedDirectory(filePath); string relativeFilePath = Path.Combine(relativeDirPath, "Example.json");
ModMonitor.Log("THIS IS THE PATH::: " + getRelativePath); if (!File.Exists(Path.Combine(this.Helper.DirectoryPath, relativeFilePath)))
{
ModMonitor.Log("THIS IS THE PATH::: " + relativeFilePath);
AssetInfo info = new AssetInfo("MyExample", new NamePairings("StandingExampleL", "StandingExampleR", "StandingExampleU", "StandingExampleD"), new NamePairings("MovingExampleL", "MovingExampleR", "MovingExampleU", "MovingExampleD"), new NamePairings("SwimmingExampleL", "SwimmingExampleR", "SwimmingExampleU", "SwimmingExampleD"), new NamePairings("SittingExampleL", "SittingExampleR", "SittingExampleU", "SittingExampleD"), new Vector2(16, 16), false); AssetInfo info = new AssetInfo("MyExample", new NamePairings("StandingExampleL", "StandingExampleR", "StandingExampleU", "StandingExampleD"), new NamePairings("MovingExampleL", "MovingExampleR", "MovingExampleU", "MovingExampleD"), new NamePairings("SwimmingExampleL", "SwimmingExampleR", "SwimmingExampleU", "SwimmingExampleD"), new NamePairings("SittingExampleL", "SittingExampleR", "SittingExampleU", "SittingExampleD"), new Vector2(16, 16), false);
info.writeToJson(filePath); info.writeToJson(relativeFilePath);
} }
string filePath2 = Path.Combine(dirPath, "AdvancedExample.json");
if (!File.Exists(filePath2))
{
ExtendedAssetInfo info2 = new ExtendedAssetInfo("AdvancedExample", new NamePairings("AdvancedStandingExampleL", "AdvancedStandingExampleR", "AdvancedStandingExampleU", "AdvancedStandingExampleD"), new NamePairings("AdvancedMovingExampleL", "AdvancedMovingExampleR", "AdvancedMovingExampleU", "AdvancedMovingExampleD"), new NamePairings("AdvancedSwimmingExampleL", "AdvancedSwimmingExampleR", "AdvancedSwimmingExampleU", "AdvancedSwimmingExampleD"), new NamePairings("AdvancedSittingExampleL", "AdvancedSittingExampleR", "AdvancedSittingExampleU", "AdvancedSittingExampleD"), new Vector2(16, 16), false, Genders.female, new List<Seasons>()
{
Seasons.spring,
Seasons.summer
}, PartType.hair
);
info2.writeToJson(filePath2);
}
} }
/// <summary>Used to splice the mod directory to get relative paths.</summary> // write advanced example
public static string getShortenedDirectory(string path)
{ {
string lol = (string)path.Clone(); string relativeFilePath = Path.Combine(relativeDirPath, "AdvancedExample.json");
string[] spliter = lol.Split(new string[] { ModHelper.DirectoryPath }, StringSplitOptions.None); if (!File.Exists(Path.Combine(this.Helper.DirectoryPath, relativeFilePath)))
try
{ {
return spliter[1]; ExtendedAssetInfo info2 = new ExtendedAssetInfo("AdvancedExample", new NamePairings("AdvancedStandingExampleL", "AdvancedStandingExampleR", "AdvancedStandingExampleU", "AdvancedStandingExampleD"), new NamePairings("AdvancedMovingExampleL", "AdvancedMovingExampleR", "AdvancedMovingExampleU", "AdvancedMovingExampleD"), new NamePairings("AdvancedSwimmingExampleL", "AdvancedSwimmingExampleR", "AdvancedSwimmingExampleU", "AdvancedSwimmingExampleD"), new NamePairings("AdvancedSittingExampleL", "AdvancedSittingExampleR", "AdvancedSittingExampleU", "AdvancedSittingExampleD"), new Vector2(16, 16), false, Genders.female, new List<Seasons>() { Seasons.spring, Seasons.summer }, PartType.hair);
info2.writeToJson(relativeFilePath);
} }
catch
{
return spliter[0];
} }
} }
/// <summary>Used to finish cleaning up absolute asset paths into a shortened relative path.</summary> /// <summary>Used to finish cleaning up absolute asset paths into a shortened relative path.</summary>
public static string getRelativeDirectory(string path) public static string getRelativeDirectory(string path)
{ {
string s = getShortenedDirectory(path); return path
return s.Remove(0, 1); .Split(new[] { ModHelper.DirectoryPath }, 2, StringSplitOptions.None)
.Last()
.TrimStart(Path.DirectorySeparatorChar);
} }
} }
} }

View File

@ -49,17 +49,17 @@ namespace CustomNPCFramework.Framework.Graphics
} }
/// <summary>Save the json to a certain location.</summary> /// <summary>Save the json to a certain location.</summary>
/// <param name="path">The absolute path to save.</param> /// <param name="relativeFilePath">The relative path to save.</param>
public void writeToJson(string path) public void writeToJson(string relativeFilePath)
{ {
Class1.ModHelper.WriteJsonFile<AssetInfo>(path, this); Class1.ModHelper.Data.WriteJsonFile(relativeFilePath, this);
} }
/// <summary>Read the json from a certain location.</summary> /// <summary>Read the json from a certain location.</summary>
/// <param name="path">The absolute path to save.</param> /// <param name="relativePath">The relative path to save.</param>
public static AssetInfo readFromJson(string path) public static AssetInfo readFromJson(string relativePath)
{ {
return Class1.ModHelper.ReadJsonFile<AssetInfo>(path); return Class1.ModHelper.Data.ReadJsonFile<AssetInfo>(relativePath);
} }
} }
} }

View File

@ -1,4 +1,3 @@
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using CustomNPCFramework.Framework.Enums; using CustomNPCFramework.Framework.Enums;
@ -9,65 +8,48 @@ namespace CustomNPCFramework.Framework.Graphics
public class AssetManager public class AssetManager
{ {
/// <summary>A list of all of the assets held by this asset manager.</summary> /// <summary>A list of all of the assets held by this asset manager.</summary>
public List<AssetSheet> assets; public List<AssetSheet> assets { get; } = new List<AssetSheet>();
/// <summary>A list of all of the directories managed by this asset manager.</summary> /// <summary>A list of directories managed by this asset manager, relative to the mod folder.</summary>
public Dictionary<string, string> paths; public Dictionary<string, string> relativePaths { get; } = new Dictionary<string, string>();
/// <summary>Construct an instance.</summary>
public AssetManager()
{
this.assets = new List<AssetSheet>();
this.paths = new Dictionary<string, string>();
}
/// <summary>Construct an instance.</summary>
/// <param name="assetsPathsToLoadFrom">A list of all directories to be managed by the asset manager. Name, path is the key pair value.</param>
public AssetManager(Dictionary<string, string> assetsPathsToLoadFrom)
{
this.assets = new List<AssetSheet>();
this.paths = assetsPathsToLoadFrom;
}
/// <summary>Default loading function from hardcoded paths.</summary> /// <summary>Default loading function from hardcoded paths.</summary>
public void loadAssets() public void loadAssets()
{ {
foreach (var path in this.paths) foreach (var relativePath in this.relativePaths)
this.ProcessDirectory(path.Value); this.ProcessDirectory(relativePath.Value);
} }
/// <summary>Process all .json files in the given directory. If there are more nested directories, keep digging to find more .json files. Also allows us to specify a broader directory like Content/Grahphics/ModularNPC/Hair to have multiple hair styles.</summary> /// <summary>Process all .json files in the given directory. If there are more nested directories, keep digging to find more .json files. Also allows us to specify a broader directory like Content/Grahphics/ModularNPC/Hair to have multiple hair styles.</summary>
/// <param name="targetDirectory">The absolute directory path to process.</param> /// <param name="relativeDirPath">The relative directory path to process.</param>
/// <remarks>Taken from Microsoft c# documented webpages.</remarks> /// <remarks>Taken from Microsoft c# documented webpages.</remarks>
private void ProcessDirectory(string targetDirectory) private void ProcessDirectory(string relativeDirPath)
{ {
// Process the list of files found in the directory. DirectoryInfo root = new DirectoryInfo(Path.Combine(Class1.ModHelper.DirectoryPath, relativeDirPath));
string[] files = Directory.GetFiles(targetDirectory, "*.json"); foreach (FileInfo file in root.GetFiles("*.json"))
foreach (string file in files) this.ProcessFile(Path.Combine(relativeDirPath, file.Name), relativeDirPath);
this.ProcessFile(file, targetDirectory);
// Recurse into subdirectories of this directory. // Recurse into subdirectories of this directory.
string[] subdirectoryEntries = Directory.GetDirectories(targetDirectory); foreach (DirectoryInfo subdir in root.GetDirectories())
foreach (string subdirectory in subdirectoryEntries) this.ProcessDirectory(Path.Combine(relativeDirPath, subdir.Name));
this.ProcessDirectory(subdirectory);
} }
/// <summary>Actually load in the asset information.</summary> /// <summary>Actually load in the asset information.</summary>
/// <param name="file">The absolute file path to process.</param> /// <param name="relativeFilePath">The relative path to the file to process.</param>
/// <param name="path">The absolute directory path containing the file.</param> /// <param name="relativeDirPath">The relative path containing the file.</param>
private void ProcessFile(string file, string path) private void ProcessFile(string relativeFilePath, string relativeDirPath)
{ {
try try
{ {
ExtendedAssetInfo info = ExtendedAssetInfo.readFromJson(file); ExtendedAssetInfo info = ExtendedAssetInfo.readFromJson(relativeFilePath);
AssetSheet sheet = new AssetSheet(info, path); AssetSheet sheet = new AssetSheet(info, relativeDirPath);
this.addAsset(sheet); this.addAsset(sheet);
Class1.ModMonitor.Log("Loaded in new modular asset: " + info.assetName + " asset type: " + info.type); Class1.ModMonitor.Log("Loaded in new modular asset: " + info.assetName + " asset type: " + info.type);
} }
catch catch
{ {
AssetInfo info = AssetInfo.readFromJson(file); AssetInfo info = AssetInfo.readFromJson(relativeFilePath);
AssetSheet sheet = new AssetSheet(info, path); AssetSheet sheet = new AssetSheet(info, relativeDirPath);
this.addAsset(sheet); this.addAsset(sheet);
} }
} }
@ -102,16 +84,16 @@ namespace CustomNPCFramework.Framework.Graphics
} }
/// <summary>Add a path to the dictionary.</summary> /// <summary>Add a path to the dictionary.</summary>
/// <param name="path">The absolute path to add.</param> /// <param name="path">The relative path to add.</param>
private void addPath(KeyValuePair<string, string> path) private void addPath(KeyValuePair<string, string> path)
{ {
this.paths.Add(path.Key, path.Value); this.relativePaths.Add(path.Key, path.Value);
} }
/// <summary>Create appropriate directories for the path.</summary> /// <summary>Create appropriate directories for the path.</summary>
private void createDirectoriesFromPaths() private void createDirectoriesFromPaths()
{ {
foreach (var v in this.paths) foreach (var v in this.relativePaths)
Directory.CreateDirectory(Path.Combine(Class1.ModHelper.DirectoryPath, v.Value)); Directory.CreateDirectory(Path.Combine(Class1.ModHelper.DirectoryPath, v.Value));
} }

View File

@ -351,7 +351,7 @@ namespace CustomNPCFramework.Framework.Graphics
return new StandardCharacterAnimation(bodySprite, eyesSprite, hairSprite, shirtSprite, pantsSprite, shoesSprite, accessoryCollection, drawColors); return new StandardCharacterAnimation(bodySprite, eyesSprite, hairSprite, shirtSprite, pantsSprite, shoesSprite, accessoryCollection, drawColors);
} }
/// <summary>Get the string for the standard character sprite to be used from this asset sheet.</summary> /// <summary>Get the relative path for the standard character sprite to be used from this asset sheet.</summary>
/// <param name="imageGraphics">The standard asset sheet to be used.</param> /// <param name="imageGraphics">The standard asset sheet to be used.</param>
public virtual string getDefaultSpriteImage(AssetSheet imageGraphics) public virtual string getDefaultSpriteImage(AssetSheet imageGraphics)
{ {

View File

@ -25,21 +25,13 @@ namespace CustomNPCFramework.Framework.Graphics
/// <summary>Construct an instance.</summary> /// <summary>Construct an instance.</summary>
/// <param name="info">The asset info file to be read in or created. Holds path information.</param> /// <param name="info">The asset info file to be read in or created. Holds path information.</param>
/// <param name="path">The path to the assetinfo file.</param> /// <param name="relativeDirPath">The relative path to the assetinfo file.</param>
/// <param name="direction">The direction to set the animation.</param> /// <param name="direction">The direction to set the animation.</param>
public AssetSheet(AssetInfo info, string path, Direction direction = Direction.down) public AssetSheet(AssetInfo info, string relativeDirPath, Direction direction = Direction.down)
{ {
this.assetInfo = info; this.assetInfo = info;
this.textures = new TextureGroup(info, path, direction); this.textures = new TextureGroup(info, relativeDirPath, direction);
try this.path = relativeDirPath;
{
this.path = Class1.getShortenedDirectory(path);
}
catch
{
this.path = path;
}
this.index = 0; this.index = 0;
} }

View File

@ -56,17 +56,12 @@ namespace CustomNPCFramework.Framework.Graphics
} }
} }
public DirectionalTexture(IModHelper helper, NamePairings info, string path, Direction direction = Direction.down) public DirectionalTexture(IModHelper helper, NamePairings info, string relativePath, Direction direction = Direction.down)
{ {
string leftString = Class1.getShortenedDirectory(Path.Combine(path, info.leftString + ".png")).Remove(0, 1); this.leftTexture = new Texture2DExtended(helper, Path.Combine(relativePath, $"{info.leftString}.png"));
string rightString = Class1.getShortenedDirectory(Path.Combine(path, info.rightString + ".png")).Remove(0, 1); this.rightTexture = new Texture2DExtended(helper, Path.Combine(relativePath, $"{info.rightString}.png"));
string upString = Class1.getShortenedDirectory(Path.Combine(path, info.upString + ".png")).Remove(0, 1); this.upTexture = new Texture2DExtended(helper, Path.Combine(relativePath, $"{info.upString}.png"));
string downString = Class1.getShortenedDirectory(Path.Combine(path, info.downString + ".png")).Remove(0, 1); this.downTexture = new Texture2DExtended(helper, Path.Combine(relativePath, $"{info.downString}.png"));
this.leftTexture = new Texture2DExtended(helper, leftString);
this.rightTexture = new Texture2DExtended(helper, rightString);
this.upTexture = new Texture2DExtended(helper, upString);
this.downTexture = new Texture2DExtended(helper, downString);
switch (direction) switch (direction)
{ {

View File

@ -39,17 +39,17 @@ namespace CustomNPCFramework.Framework.Graphics
} }
/// <summary>Save the json to a certain location.</summary> /// <summary>Save the json to a certain location.</summary>
/// <param name="path">The absolute path to write.</param> /// <param name="relativePath">The relative path to write.</param>
public new void writeToJson(string path) public new void writeToJson(string relativePath)
{ {
Class1.ModHelper.WriteJsonFile<ExtendedAssetInfo>(path, this); Class1.ModHelper.Data.WriteJsonFile<ExtendedAssetInfo>(relativePath, this);
} }
/// <summary>Read the json from a certain location.</summary> /// <summary>Read the json from a certain location.</summary>
/// <param name="path">The absolute path to read.</param> /// <param name="relativePath">The relative path to read.</param>
public new static ExtendedAssetInfo readFromJson(string path) public new static ExtendedAssetInfo readFromJson(string relativePath)
{ {
return Class1.ModHelper.ReadJsonFile<ExtendedAssetInfo>(path); return Class1.ModHelper.Data.ReadJsonFile<ExtendedAssetInfo>(relativePath);
} }
} }
} }

View File

@ -14,17 +14,10 @@ namespace CustomNPCFramework.Framework.ModularNpcs
public string relativePath; public string relativePath;
/// <summary>Construct an instance.</summary> /// <summary>Construct an instance.</summary>
/// <param name="path">The full path to the file.</param> /// <param name="relativePath">The relative path to the file.</param>
public Sprite(string path) public Sprite(string relativePath)
{ {
try this.relativePath = relativePath;
{
this.relativePath = Class1.getShortenedDirectory(path);
}
catch
{
this.relativePath = path;
}
try try
{ {
this.sprite = new AnimatedSprite(); this.sprite = new AnimatedSprite();

View File

@ -353,8 +353,7 @@ namespace Omegasis.NightOwl
this.PreCollapseStamina, this.PreCollapseStamina,
this.PreCollapseHealth this.PreCollapseHealth
}; };
string path = Path.Combine(this.Helper.DirectoryPath, "Error_Logs", "Mod_State.json"); this.Helper.Data.WriteJsonFile("Error_Logs/Mod_State.json", state);
this.Helper.WriteJsonFile(path, state);
} }
/// <summary>Try and emulate the old Game1.shouldFarmerPassout logic.</summary> /// <summary>Try and emulate the old Game1.shouldFarmerPassout logic.</summary>

View File

@ -26,8 +26,8 @@ namespace Omegasis.SaveAnywhere.Framework
/// <summary>SMAPI's APIs for this mod.</summary> /// <summary>SMAPI's APIs for this mod.</summary>
private readonly IModHelper Helper; private readonly IModHelper Helper;
/// <summary>The full path to the player data file.</summary> /// <summary>The relative path to the player data file.</summary>
private string SavePath => Path.Combine(this.Helper.DirectoryPath, "data", $"{Constants.SaveFolderName}.json"); private string RelativeDataPath => Path.Combine("data", $"{Constants.SaveFolderName}.json");
/// <summary>Whether we should save at the next opportunity.</summary> /// <summary>Whether we should save at the next opportunity.</summary>
private bool WaitingToSave; private bool WaitingToSave;
@ -80,7 +80,7 @@ namespace Omegasis.SaveAnywhere.Framework
/// <summary>Clear saved data.</summary> /// <summary>Clear saved data.</summary>
public void ClearData() public void ClearData()
{ {
File.Delete(this.SavePath); File.Delete(Path.Combine(this.Helper.DirectoryPath, this.RelativeDataPath));
this.RemoveLegacyDataForThisPlayer(); this.RemoveLegacyDataForThisPlayer();
} }
@ -105,20 +105,16 @@ namespace Omegasis.SaveAnywhere.Framework
} }
// get data // save data to disk
PlayerData data = new PlayerData PlayerData data = new PlayerData
{ {
Time = Game1.timeOfDay, Time = Game1.timeOfDay,
Characters = this.GetPositions().ToArray(), Characters = this.GetPositions().ToArray(),
IsCharacterSwimming = Game1.player.swimming.Value IsCharacterSwimming = Game1.player.swimming.Value
}; };
this.Helper.Data.WriteJsonFile(this.RelativeDataPath, data);
// save to disk // clear any legacy data (no longer needed as backup)
// ReSharper disable once PossibleNullReferenceException -- not applicable
Directory.CreateDirectory(new FileInfo(this.SavePath).Directory.FullName);
this.Helper.WriteJsonFile(this.SavePath, data);
// clear any legacy data (no longer needed as backup)1
this.RemoveLegacyDataForThisPlayer(); this.RemoveLegacyDataForThisPlayer();
} }
@ -126,7 +122,7 @@ namespace Omegasis.SaveAnywhere.Framework
public void LoadData() public void LoadData()
{ {
// get data // get data
PlayerData data = this.Helper.ReadJsonFile<PlayerData>(this.SavePath); PlayerData data = this.Helper.Data.ReadJsonFile<PlayerData>(this.RelativeDataPath);
if (data == null) if (data == null)
return; return;

View File

@ -36,9 +36,9 @@ namespace SimpleSoundManager.Framework
} }
/// <summary>Constructor for wav files.</summary> /// <summary>Constructor for wav files.</summary>
public void loadWavFile(IModHelper helper, string soundName, string pathToWav) public void loadWavFile(IModHelper helper, string soundName, string relativePath)
{ {
WavSound wav = new WavSound(helper, soundName, pathToWav); WavSound wav = new WavSound(helper, soundName, relativePath);
SimpleSoundManagerMod.ModMonitor.Log("Getting sound file:" + soundName); SimpleSoundManagerMod.ModMonitor.Log("Getting sound file:" + soundName);
try try
{ {

View File

@ -37,9 +37,9 @@ namespace SimpleSoundManager
} }
/// <summary>A constructor that takes a mod helper and a relative path to a wav file.</summary> /// <summary>A constructor that takes a mod helper and a relative path to a wav file.</summary>
public WavSound(IModHelper modHelper, string name, string pathInModDirectory, bool loop = false) public WavSound(IModHelper modHelper, string name, string relativePath, bool loop = false)
{ {
string path = Path.Combine(modHelper.DirectoryPath, pathInModDirectory); string path = Path.Combine(modHelper.DirectoryPath, relativePath);
this.path = path; this.path = path;
this.soundName = name; this.soundName = name;
this.loop = loop; this.loop = loop;

View File

@ -26,17 +26,5 @@ namespace StardustCore.UIUtilities.SpriteFonts.Components
this.TopPadding = top; this.TopPadding = top;
this.BottomPadding = bottom; this.BottomPadding = bottom;
} }
/// <summary>Save this to a .json file.</summary>
public void WriteToJson(string path)
{
StardustCore.ModCore.ModHelper.WriteJsonFile(path, this);
}
/// <summary>Read the data from the .json file.</summary>
public static CharacterSpacing ReadFromJson(string path)
{
return StardustCore.ModCore.ModHelper.ReadJsonFile<CharacterSpacing>(path);
}
} }
} }

View File

@ -44,8 +44,7 @@ namespace Vocalization.Framework
{ {
foreach (LanguageName language in Vocalization.config.translationInfo.LanguageNames) foreach (LanguageName language in Vocalization.config.translationInfo.LanguageNames)
{ {
var loaded = var loaded = Vocalization.ModHelper.Data.ReadJsonFile<SortedDictionary<string, VoiceAudioOptions>>($"AudioCues/AudioCues{Seperator}{language}.json")
Vocalization.ModHelper.ReadJsonFile<SortedDictionary<string, VoiceAudioOptions>>(Path.Combine(Vocalization.ModHelper.DirectoryPath, "AudioCues", "AudioCues" + Seperator + language + ".json"))
?? new SortedDictionary<string, VoiceAudioOptions>(); ?? new SortedDictionary<string, VoiceAudioOptions>();
DictionaryReferences.Add(Vocalization.config.translationInfo.getTranslationName(language), loaded); DictionaryReferences.Add(Vocalization.config.translationInfo.getTranslationName(language), loaded);
} }
@ -54,7 +53,7 @@ namespace Vocalization.Framework
public static void saveAudioCues() public static void saveAudioCues()
{ {
foreach (var v in DictionaryReferences) foreach (var v in DictionaryReferences)
Vocalization.ModHelper.WriteJsonFile<SortedDictionary<string, VoiceAudioOptions>>(Path.Combine(Vocalization.ModHelper.DirectoryPath, "AudioCues", "AudioCues" + Seperator + v.Key + ".json"), v.Value); Vocalization.ModHelper.Data.WriteJsonFile($"AudioCues/AudioCues{Seperator}{v.Key}.json", v.Value);
} }
} }
} }

View File

@ -609,38 +609,35 @@ namespace Vocalization
{ {
foreach (LanguageName language in config.translationInfo.LanguageNames) foreach (LanguageName language in config.translationInfo.LanguageNames)
{ {
string relativeLanguagePath = Path.Combine(RelativeVoicePath, language.ToString());
// check if mod supports language // check if mod supports language
DirectoryInfo languageDir = new DirectoryInfo(Path.Combine(ModHelper.DirectoryPath, RelativeVoicePath, language.ToString())); DirectoryInfo languageDir = new DirectoryInfo(Path.Combine(ModHelper.DirectoryPath, relativeLanguagePath));
if (!languageDir.Exists) if (!languageDir.Exists)
return; return;
// get characters supported in this translation and load their voice cue file // get characters supported in this translation and load their voice cue file
foreach (string dir in Directory.GetDirectories(languageDir.FullName)) foreach (DirectoryInfo subdir in languageDir.GetDirectories())
{ {
ModMonitor.Log(dir); string characterName = subdir.Name;
string relativeCuePath = Path.Combine(relativeLanguagePath, subdir.Name, "VoiceCues.json"); // dialogue cues (aka when the character should "speak")
string[] clips = Directory.GetFiles(dir, "*.wav");
//For every .wav file in every character voice clip directory load in the voice clip. //For every .wav file in every character voice clip directory load in the voice clip.
foreach (string file in clips) foreach (FileInfo file in subdir.GetFiles("*.wav"))
{ {
string fileName = Path.GetFileNameWithoutExtension(file); string fileName = Path.GetFileNameWithoutExtension(file.Name);
soundManager.loadWavFile(ModHelper, fileName, file); soundManager.loadWavFile(ModHelper, fileName, file.FullName);
ModMonitor.Log("Loaded sound file: " + fileName + " from: " + file); ModMonitor.Log($"Loaded sound file: {fileName} from: {file.FullName}");
} }
//Get the character dialogue cues (aka when the character should "speak") from the .json file.
string voiceCueFile = Path.Combine(dir, "VoiceCues.json");
string characterName = Path.GetFileName(dir);
//If a file was not found, create one and add it to the list of character voice cues. //If a file was not found, create one and add it to the list of character voice cues.
//I have to scrape all files if they don't exist so that way all options are available for release. //I have to scrape all files if they don't exist so that way all options are available for release.
if (!File.Exists(voiceCueFile)) if (!File.Exists(Path.Combine(ModHelper.DirectoryPath, relativeCuePath)))
{ {
CharacterVoiceCue cue = new CharacterVoiceCue(characterName); CharacterVoiceCue cue = new CharacterVoiceCue(characterName);
cue.initializeEnglishScrape(); cue.initializeEnglishScrape();
cue.initializeForTranslation(language); cue.initializeForTranslation(language);
scrapeDictionaries(voiceCueFile, cue, language, languageDir); scrapeDictionaries(relativeCuePath, cue, language, languageDir);
try try
{ {
if (language == config.translationInfo.CurrentTranslation) if (language == config.translationInfo.CurrentTranslation)
@ -655,7 +652,7 @@ namespace Vocalization
//Only load in the cues for the current translation. //Only load in the cues for the current translation.
if (language == config.translationInfo.CurrentTranslation) if (language == config.translationInfo.CurrentTranslation)
{ {
CharacterVoiceCue cue = ModHelper.ReadJsonFile<CharacterVoiceCue>(voiceCueFile); CharacterVoiceCue cue = ModHelper.Data.ReadJsonFile<CharacterVoiceCue>(relativeCuePath);
//scrapeDictionaries(voiceCueFile,cue); //scrapeDictionaries(voiceCueFile,cue);
DialogueCues.Add(characterName, cue); DialogueCues.Add(characterName, cue);
} }
@ -670,7 +667,7 @@ namespace Vocalization
} }
/// <summary>Used to obtain all strings for almost all possible dialogue in the game.</summary> /// <summary>Used to obtain all strings for almost all possible dialogue in the game.</summary>
public static void scrapeDictionaries(string cuePath, CharacterVoiceCue cue, LanguageName language, DirectoryInfo languageDir) public static void scrapeDictionaries(string relativeCuePath, CharacterVoiceCue cue, LanguageName language, DirectoryInfo languageDir)
{ {
string dialoguePath = Path.Combine("Characters", "Dialogue"); string dialoguePath = Path.Combine("Characters", "Dialogue");
string stringsPath = Path.Combine("Strings"); //Used for all sorts of extra strings and stuff for like StringsFromCS string stringsPath = Path.Combine("Strings"); //Used for all sorts of extra strings and stuff for like StringsFromCS
@ -2860,7 +2857,7 @@ namespace Vocalization
} }
} }
ModHelper.WriteJsonFile<CharacterVoiceCue>(cuePath, cue); ModHelper.Data.WriteJsonFile(relativeCuePath, cue);
//DialogueCues.Add(cue.name, cue); //DialogueCues.Add(cue.name, cue);
} }