-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7c33eb3
commit eb27808
Showing
77 changed files
with
1,262 additions
and
224 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"Values": {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
using Newtonsoft.Json; | ||
using System; | ||
using System.Diagnostics; | ||
using System.IO; | ||
using System.Text; | ||
|
||
namespace Aequus.Common.DataSets; | ||
|
||
public sealed class EmbeddedJsonFile { | ||
private readonly IJsonHolder _dataSet; | ||
public readonly string FileName; | ||
public readonly string FullFilePath; | ||
public readonly string? FileData; | ||
|
||
internal EmbeddedJsonFile(IJsonHolder holder) { | ||
_dataSet = holder; | ||
FileName = holder.FilePath; | ||
FullFilePath = $"Assets/Metadata/{FileName}.json"; | ||
|
||
if (Aequus.Instance.FileExists(FullFilePath)) { | ||
using var stream = Aequus.Instance.GetFileStream(FullFilePath, newFileStream: true); | ||
using var streamReader = new StreamReader(stream); | ||
FileData = streamReader.ReadToEnd(); | ||
} | ||
else { | ||
FileData = null; | ||
} | ||
} | ||
|
||
public void Apply() { | ||
try { | ||
if (!string.IsNullOrEmpty(FileData)) { | ||
JsonConvert.DeserializeObject(FileData, _dataSet.GetType()); | ||
} | ||
} | ||
catch (Exception ex) { | ||
Aequus.Instance.Logger.Error(ex); | ||
} | ||
} | ||
|
||
[Conditional("GEN")] | ||
internal void GenerateEmbeddedFiles() { | ||
string assetLocation = Path.Join(Aequus.ModSources, "Assets", "Metadata"); | ||
string fileLocation = Path.Join(assetLocation, $"{FileName}.json"); | ||
Aequus.Instance.Logger.Debug(fileLocation); | ||
try { | ||
// Only attempt to create the file if the metadata directory even exists. | ||
if (!Directory.Exists(assetLocation)) { | ||
return; | ||
} | ||
|
||
// Create subdirectories for the full file location. | ||
string? fileSubLocation = Path.GetDirectoryName(fileLocation); | ||
if (!Directory.Exists(fileSubLocation)) { | ||
Directory.CreateDirectory(fileSubLocation!); | ||
} | ||
|
||
JsonSerializerSettings settings = new JsonSerializerSettings { | ||
Formatting = Formatting.Indented, | ||
}; | ||
string jsonData = JsonConvert.SerializeObject(_dataSet, settings); | ||
if (jsonData != null) { | ||
byte[] buffer = Encoding.UTF8.GetBytes(jsonData); | ||
using FileStream file = File.Create(fileLocation, buffer.Length); | ||
file.Write(buffer, 0, buffer.Length); | ||
} | ||
} | ||
catch (Exception ex) { | ||
Aequus.Instance.Logger.Error(ex); | ||
} | ||
} | ||
} |
Oops, something went wrong.