Skip to content

Commit

Permalink
Merge pull request #263 from SpaceWarpDev/dev
Browse files Browse the repository at this point in the history
Dev --> Main for 1.5 release
  • Loading branch information
cheese3660 authored Oct 10, 2023
2 parents 17ed1af + 1693054 commit a103931
Show file tree
Hide file tree
Showing 152 changed files with 7,084 additions and 3,418 deletions.
22 changes: 21 additions & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<Project>
<PropertyGroup>
<SpaceWarpVersion>1.4.3</SpaceWarpVersion>
<SpaceWarpVersion>1.5.0</SpaceWarpVersion>
<TargetFramework>netstandard2.0</TargetFramework>
<RootNamespace>SpaceWarp</RootNamespace>
<LangVersion>11</LangVersion>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
Expand All @@ -28,4 +29,23 @@
System.Diagnostics.CodeAnalysis.DoesNotReturnAttribute;
</PolySharpIncludeGeneratedTypes>
</PropertyGroup>

<!-- <ItemGroup Label="Dependencies for build targets">-->
<!-- <PackageReference Include="JsonPeek" Version="1.2.0" PrivateAssets="all"/>-->
<!-- </ItemGroup>-->

<!-- &lt;!&ndash; Define the main target &ndash;&gt;-->
<!-- <Target Label="Reading properties from swinfo.json" Name="ReadPropertiesFromJson" BeforeTargets="PreBuildEvent">-->
<!-- <JsonPeek ContentPath="$(SolutionDir)/SpaceWarpBuildTemplate/swinfo.json" Query="$">-->
<!-- <Output TaskParameter="Result" ItemName="Swinfo"/>-->
<!-- </JsonPeek>-->
<!-- <JsonPeek ContentPath="$(SolutionDir)/SpaceWarpBuildTemplate/swinfo.json" Query="$.dependencies">-->
<!-- <Output TaskParameter="Result" ItemName="Dependencies"/>-->
<!-- </JsonPeek>-->

<!-- &lt;!&ndash; Extract properties from the JSON &ndash;&gt;-->
<!-- <PropertyGroup>-->
<!-- <SpaceWarpVersion>@(Swinfo -> '%(version)')</SpaceWarpVersion>-->
<!-- </PropertyGroup>-->
<!-- </Target>-->
</Project>
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using JetBrains.Annotations;
using SpaceWarp.API.Lua;
using UnityEngine;
using Logger = BepInEx.Logging.Logger;

namespace SpaceWarp.API.Assets;

[SpaceWarpLuaAPI("Assets")]
[PublicAPI]
public static class AssetManager
{
private static readonly Dictionary<string, UnityObject> AllAssets = new();
Expand Down Expand Up @@ -135,7 +137,7 @@ public static bool TryGetAsset<T>(string path, out T asset) where T : UnityObjec

return true;
}

/// <summary>
/// Gets an asset from the specified asset path
/// </summary>
Expand Down
45 changes: 45 additions & 0 deletions SpaceWarp.Core/API/Configuration/BepInExConfigEntry.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using System;
using BepInEx.Configuration;
using JetBrains.Annotations;

namespace SpaceWarp.API.Configuration;

[PublicAPI]
public class BepInExConfigEntry : IConfigEntry
{
public readonly ConfigEntryBase EntryBase;

public BepInExConfigEntry(ConfigEntryBase entryBase)
{
EntryBase = entryBase;
}

public object Value
{
get => EntryBase.BoxedValue;
set => EntryBase.BoxedValue = value;
}
public Type ValueType => EntryBase.SettingType;

public T Get<T>() where T : class
{
if (!typeof(T).IsAssignableFrom(ValueType))
{
throw new InvalidCastException($"Cannot cast {ValueType} to {typeof(T)}");
}

return Value as T;
}

public void Set<T>(T value)
{
if (!ValueType.IsAssignableFrom(typeof(T)))
{
throw new InvalidCastException($"Cannot cast {ValueType} to {typeof(T)}");
}

EntryBase.BoxedValue = Convert.ChangeType(value, ValueType);
}

public string Description => EntryBase.Description.Description;
}
34 changes: 34 additions & 0 deletions SpaceWarp.Core/API/Configuration/BepInExConfigFile.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System.Collections.Generic;
using BepInEx.Configuration;
using JetBrains.Annotations;

namespace SpaceWarp.API.Configuration;

[PublicAPI]
public class BepInExConfigFile : IConfigFile
{

public readonly ConfigFile AdaptedConfigFile;

public BepInExConfigFile(ConfigFile adaptedConfigFile)
{
AdaptedConfigFile = adaptedConfigFile;
}

public void Save()
{
AdaptedConfigFile.Save();
}

public IConfigEntry this[string section, string key] => new BepInExConfigEntry(AdaptedConfigFile[section, key]);

public IConfigEntry Bind<T>(string section, string key, T defaultValue = default, string description = "")
{
return new BepInExConfigEntry(AdaptedConfigFile.Bind(section, key, defaultValue, description));
}

public IReadOnlyList<string> Sections => AdaptedConfigFile.Keys.Select(x => x.Section).Distinct().ToList();

public IReadOnlyList<string> this[string section] => AdaptedConfigFile.Keys.Where(x => x.Section == section)
.Select(x => x.Key).ToList();
}
25 changes: 25 additions & 0 deletions SpaceWarp.Core/API/Configuration/ConfigValue.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System;
using JetBrains.Annotations;

namespace SpaceWarp.API.Configuration;

[PublicAPI]
public class ConfigValue<T>
{
public IConfigEntry Entry;

public ConfigValue(IConfigEntry entry)
{
Entry = entry;
if (typeof(T) != entry.ValueType)
{
throw new ArgumentException(nameof(entry));
}
}

public T Value
{
get => (T)Entry.Value;
set => Entry.Value = value;
}
}
23 changes: 23 additions & 0 deletions SpaceWarp.Core/API/Configuration/EmptyConfigFile.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System.Collections.Generic;
using JetBrains.Annotations;

namespace SpaceWarp.API.Configuration;

[PublicAPI]
public class EmptyConfigFile : IConfigFile
{
public void Save()
{
}

public IConfigEntry this[string section, string key] => throw new KeyNotFoundException($"{section}/{key}");

public IConfigEntry Bind<T>(string section, string key, T defaultValue = default, string description = "")
{
throw new System.NotImplementedException();
}

public IReadOnlyList<string> Sections => new List<string>();

public IReadOnlyList<string> this[string section] => throw new KeyNotFoundException($"{section}");
}
15 changes: 15 additions & 0 deletions SpaceWarp.Core/API/Configuration/IConfigEntry.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System;
using JetBrains.Annotations;

namespace SpaceWarp.API.Configuration;

[PublicAPI]
public interface IConfigEntry
{
public object Value { get; set; }
public Type ValueType { get; }
public T Get<T>() where T : class;
public void Set<T>(T value);

public string Description { get; }
}
17 changes: 17 additions & 0 deletions SpaceWarp.Core/API/Configuration/IConfigFile.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System.Collections.Generic;
using JetBrains.Annotations;

namespace SpaceWarp.API.Configuration;

[PublicAPI]
public interface IConfigFile
{
public void Save();

public IConfigEntry this[string section, string key] { get; }

public IConfigEntry Bind<T>(string section, string key, T defaultValue = default, string description = "");

public IReadOnlyList<string> Sections { get; }
public IReadOnlyList<string> this[string section] { get; }
}
53 changes: 53 additions & 0 deletions SpaceWarp.Core/API/Configuration/JsonConfigEntry.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using System;
using JetBrains.Annotations;

namespace SpaceWarp.API.Configuration;

[PublicAPI]
public class JsonConfigEntry : IConfigEntry
{
private readonly JsonConfigFile _configFile;
private object _value;

public JsonConfigEntry(JsonConfigFile configFile, Type type, string description, object value)
{
_configFile = configFile;
_value = value;
Description = description;
ValueType = type;
}


public object Value
{
get => _value;
set
{
_value = value;
_configFile.Save();
}
}

public Type ValueType { get; }
public T Get<T>() where T : class
{
if (!typeof(T).IsAssignableFrom(ValueType))
{
throw new InvalidCastException($"Cannot cast {ValueType} to {typeof(T)}");
}

return Value as T;
}

public void Set<T>(T value)
{
if (!ValueType.IsAssignableFrom(typeof(T)))
{
throw new InvalidCastException($"Cannot cast {ValueType} to {typeof(T)}");
}

Value = Convert.ChangeType(value, ValueType);
}

public string Description { get; }
}
Loading

0 comments on commit a103931

Please sign in to comment.