Skip to content

Commit

Permalink
begin porting keychain
Browse files Browse the repository at this point in the history
  • Loading branch information
UriBuilder committed Oct 3, 2024
1 parent 7c33eb3 commit eb27808
Show file tree
Hide file tree
Showing 77 changed files with 1,262 additions and 224 deletions.
2 changes: 2 additions & 0 deletions Aequus.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ namespace Aequus;
public class Aequus : Mod {
public const char MOD_NAME_SEPERATOR = '/';

public static readonly string ModSources = Path.Join(Main.SavePath, "ModSources", nameof(Aequus));

internal delegate void LegacyDrawMethod(Texture2D texture, Vector2 position, Rectangle? frame, Color color, float scale, Vector2 origin, float rotation, SpriteEffects effects, float layerDepth);

#if DEBUG
Expand Down
6 changes: 5 additions & 1 deletion Aequus.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,13 @@
<Compile Remove="SourceGenerators\**" />
<Compile Remove="$(CompilerGeneratedFilesOutputPath)/**/*.cs" Condition="'$(Configuration)' == 'Debug'" />
</ItemGroup>

<ItemGroup>
<Folder Include="Assets\Metadata\" />
</ItemGroup>

<!-- Custom compiler flags for loading unfinished content. -->
<PropertyGroup Condition="'$(Configuration)' == 'Debug'">
<DefineConstants>POLLUTED_OCEAN;CUSTOM_RESOURCE_UI;CRAB_CREVICE_DISABLE</DefineConstants>
<DefineConstants>POLLUTED_OCEAN;CUSTOM_RESOURCE_UI;CRAB_CREVICE_DISABLE;GEN;KEYCHAIN</DefineConstants>
</PropertyGroup>
</Project>
210 changes: 122 additions & 88 deletions Aequus.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Assets/Metadata/Items/Keychain.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"Values": {}
}
72 changes: 72 additions & 0 deletions Common/DataSets/EmbeddedJsonFile.cs
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);
}
}
}
Loading

0 comments on commit eb27808

Please sign in to comment.