Skip to content

Commit 76dca08

Browse files
committed
Add project files.
1 parent e84f27d commit 76dca08

20 files changed

+1261
-0
lines changed

Common/Animation/AnimationFuncs.cs

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
namespace Common.Animation;
2+
3+
using UnityEngine;
4+
using System;
5+
6+
public static class AnimationFuncs
7+
{
8+
public static Func<Color, Color> SinusoidalColor(Color min, Color max, float frequency)
9+
{
10+
return new Func<Color, Color>(x =>
11+
{
12+
var lerp = MathUtil.Sin01(Time.time * frequency);
13+
return Color.Lerp(min, max, lerp);
14+
});
15+
}
16+
}
+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
namespace Common.Animation;
2+
3+
using UnityEngine;
4+
using System;
5+
6+
public sealed class ShaderColorPropertyAnimation : ObjectAnimation<Material>
7+
{
8+
private readonly int _propertyID;
9+
private readonly Func<Color, Color> _func;
10+
11+
public ShaderColorPropertyAnimation(int propertyID, Func<Color, Color> func)
12+
{
13+
_propertyID = propertyID;
14+
_func = func ?? throw new ArgumentNullException("func is null");
15+
}
16+
17+
protected override bool OnUpdate(Material actor)
18+
{
19+
if (!actor) return false;
20+
21+
var color = actor.GetColor(_propertyID);
22+
actor.SetColor(_propertyID, _func(color));
23+
return true;
24+
}
25+
26+
protected override void OnStart(Material actor) { }
27+
protected override void OnStop(Material actor) { }
28+
}

Common/Animation/ObjectAnimation.cs

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
namespace Common.Animation;
2+
3+
using System.Collections.Generic;
4+
using System;
5+
using BepInEx.Logging;
6+
using Common;
7+
8+
public abstract class ObjectAnimation<TActor>
9+
where TActor : UnityEngine.Object
10+
{
11+
private static readonly Dictionary<TActor, ObjectAnimation<TActor>> animations = new();
12+
private static readonly List<TActor> toRemove = new();
13+
14+
static ObjectAnimation()
15+
{
16+
Updater.updated += OnAnimationsUpdate;
17+
}
18+
19+
private static void OnAnimationsUpdate()
20+
{
21+
foreach(var pair in animations)
22+
{
23+
var actor = pair.Key;
24+
var anim = pair.Value;
25+
try
26+
{
27+
var playing = anim.OnUpdate(actor);
28+
if (!playing) toRemove.Add(actor);
29+
}
30+
catch(Exception e)
31+
{
32+
Logging.Logger.Log(LogLevel.Error, $"{anim}.OnUpdate({actor}).\n"+ e);
33+
toRemove.Add(actor);
34+
}
35+
}
36+
toRemove.ForEach(x => animations.Remove(x));
37+
toRemove.Clear();
38+
}
39+
40+
public void Play(TActor actor)
41+
{
42+
if (!actor) throw new ArgumentException("actor is null or destroyed");
43+
44+
if (animations.ContainsKey(actor))
45+
{
46+
try { animations[actor].OnStop(actor); }
47+
catch (Exception e)
48+
{
49+
Logging.Logger.Log(LogLevel.Error, $"{animations[actor]}.OnStop({actor}).\n"+ e);
50+
}
51+
}
52+
53+
try { OnStart(actor); }
54+
catch (Exception e)
55+
{
56+
Logging.Logger.Log(LogLevel.Error, $"{this}.OnStart({actor}).\n" + e);
57+
return;
58+
}
59+
animations[actor] = this;
60+
}
61+
62+
protected abstract void OnStart(TActor actor);
63+
protected abstract bool OnUpdate(TActor actor);
64+
protected abstract void OnStop(TActor actor);
65+
}

Common/Common.projitems

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<PropertyGroup>
4+
<MSBuildAllProjects>$(MSBuildAllProjects);$(MSBuildThisFileFullPath)</MSBuildAllProjects>
5+
<HasSharedItems>true</HasSharedItems>
6+
<SharedGUID>91d8d243-2590-4911-a827-c4c929e0ab69</SharedGUID>
7+
</PropertyGroup>
8+
<PropertyGroup Label="Configuration">
9+
<Import_RootNamespace>Common</Import_RootNamespace>
10+
</PropertyGroup>
11+
<ItemGroup>
12+
<Compile Include="$(MSBuildThisFileDirectory)Extensions.cs" />
13+
<Compile Include="$(MSBuildThisFileDirectory)NumberFormatter.cs" />
14+
<Compile Include="$(MSBuildThisFileDirectory)QuickLogger.cs" />
15+
<Compile Include="$(MSBuildThisFileDirectory)Animation\AnimationFuncs.cs" />
16+
<Compile Include="$(MSBuildThisFileDirectory)Animation\MaterialColorAnimation.cs" />
17+
<Compile Include="$(MSBuildThisFileDirectory)Animation\ObjectAnimation.cs" />
18+
<Compile Include="$(MSBuildThisFileDirectory)Logging.cs" />
19+
<Compile Include="$(MSBuildThisFileDirectory)Updater.cs" />
20+
<Compile Include="$(MSBuildThisFileDirectory)Util\ConfigUtil.cs" />
21+
<Compile Include="$(MSBuildThisFileDirectory)Util\JsonUtil.cs" />
22+
<Compile Include="$(MSBuildThisFileDirectory)Util\MathUtil.cs" />
23+
</ItemGroup>
24+
</Project>

Common/Common.shproj

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<PropertyGroup Label="Globals">
4+
<ProjectGuid>91d8d243-2590-4911-a827-c4c929e0ab69</ProjectGuid>
5+
<MinimumVisualStudioVersion>14.0</MinimumVisualStudioVersion>
6+
</PropertyGroup>
7+
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
8+
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\CodeSharing\Microsoft.CodeSharing.Common.Default.props" />
9+
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\CodeSharing\Microsoft.CodeSharing.Common.props" />
10+
<ItemGroup>
11+
<Compile Include="Animation\AnimationFuncs.cs" />
12+
<Compile Include="Animation\MaterialColorAnimation.cs" />
13+
<Compile Include="Animation\ObjectAnimation.cs" />
14+
<Compile Include="Util\ConfigUtil.cs" />
15+
<Compile Include="Util\JsonUtil.cs" />
16+
<Compile Include="Util\MathUtil.cs" />
17+
</ItemGroup>
18+
<ItemGroup>
19+
<None Include="Common.projitems" />
20+
</ItemGroup>
21+
<Import Project="Common.projitems" Label="Shared" />
22+
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\CodeSharing\Microsoft.CodeSharing.CSharp.targets" />
23+
</Project>

Common/Extensions.cs

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
namespace Common;
2+
3+
using System.Collections;
4+
using System.Threading.Tasks;
5+
using UnityEngine;
6+
using UWE;
7+
8+
public static class Extensions
9+
{
10+
public static TechType GetTechType(this string techTypeName)
11+
{
12+
if (TechTypeExtensions.FromString(techTypeName, out TechType techType, true))
13+
return techType;
14+
15+
QuickLogger.Error($"Failed to parse TechType from string: {techTypeName}");
16+
return TechType.None;
17+
}
18+
19+
public static T SafeParseEnum<T>(this string value)
20+
{
21+
try
22+
{
23+
return (T)System.Enum.Parse(typeof(T), value, true);
24+
}
25+
catch
26+
{
27+
QuickLogger.Error($"Failed to parse enum {typeof(T).Name} from string: {value}");
28+
return default;
29+
}
30+
}
31+
32+
public static string GetDisplayName(this TechType techType)
33+
{
34+
return Language.main.Get(techType);
35+
}
36+
37+
public static string GetClassId(this TechType techType)
38+
{
39+
return CraftData.GetClassIdForTechType(techType);
40+
}
41+
42+
public static IEnumerator GetPrefabAsync(this TechType techType, IOut<GameObject> @out)
43+
{
44+
return CraftData.GetPrefabForTechTypeAsync(techType, false, @out);
45+
}
46+
47+
48+
49+
}

Common/Logging.cs

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
namespace Common;
2+
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Text;
6+
using BepInEx.Logging;
7+
8+
public class Logging
9+
{
10+
internal static ManualLogSource Logger;
11+
12+
public static void Initialize(ManualLogSource logSource)
13+
{
14+
Logger = logSource;
15+
}
16+
}

Common/NumberFormatter.cs

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
namespace Common
2+
{
3+
using System.Collections.Generic;
4+
using UnityEngine;
5+
6+
internal class NumberFormatter
7+
{
8+
private static readonly IDictionary<int, string> _formattedValueCache = new Dictionary<int, string>();
9+
10+
internal static string FormatValue(float value)
11+
{
12+
int intCastValue = Mathf.CeilToInt(value);
13+
return FormatValue(intCastValue);
14+
}
15+
16+
internal static string FormatValue(int value)
17+
{
18+
if (!_formattedValueCache.TryGetValue(value, out string amountString))
19+
{
20+
amountString = $"{HandleLargeNumbers(value)}";
21+
_formattedValueCache.Add(value, amountString);
22+
}
23+
return amountString;
24+
}
25+
26+
private static string HandleLargeNumbers(float possiblyLargeValue)
27+
{
28+
if (possiblyLargeValue > 9999999f)
29+
{
30+
return $"{possiblyLargeValue / 1000000f:F1}M";
31+
}
32+
33+
if (possiblyLargeValue > 9999f)
34+
{
35+
return $"{possiblyLargeValue / 1000f:F1}K";
36+
}
37+
38+
return $"{possiblyLargeValue:F0}";
39+
}
40+
41+
/// <summary>
42+
/// Goes from Red at 0% to Green at 100%, passing through Yellow at 50%.
43+
/// </summary>
44+
/// <param name="value">The current value</param>
45+
/// <param name="max">The 100% value.</param>
46+
/// <param name="min">The 0% value.</param>
47+
/// <returns>The calculated color.</returns>
48+
internal static Color GetNumberColor(float value, float max, float min)
49+
{
50+
float mid = (min + max) / 2;
51+
52+
if (value < min)
53+
return Color.white;
54+
55+
if (value <= mid)
56+
return Color.Lerp(Color.red, Color.yellow, (value - min) / (mid - min));
57+
58+
if (value <= max)
59+
return Color.Lerp(Color.yellow, Color.green, (value - mid) / (max - mid));
60+
61+
return Color.white;
62+
}
63+
}
64+
}

Common/QuickLogger.cs

+95
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
namespace Common
2+
{
3+
using System;
4+
using System.Reflection;
5+
using BepInEx.Logging;
6+
7+
public static class QuickLogger
8+
{
9+
private static readonly AssemblyName ModName = Assembly.GetExecutingAssembly().GetName();
10+
private static readonly ManualLogSource _manualLogSource;
11+
12+
public static bool DebugLogsEnabled = false;
13+
14+
static QuickLogger()
15+
{
16+
_manualLogSource = Logger.CreateLogSource(ModName.Name);
17+
}
18+
19+
public static void Info(string msg, bool showOnScreen = false)
20+
{
21+
_manualLogSource.LogInfo(msg);
22+
if (showOnScreen)
23+
ErrorMessage.AddMessage(msg);
24+
}
25+
26+
public static void Debug(string msg, bool showOnScreen = false)
27+
{
28+
_manualLogSource.LogDebug(msg);
29+
30+
if (DebugLogsEnabled && showOnScreen)
31+
ErrorMessage.AddDebug(msg);
32+
}
33+
34+
public static void Error(string msg, bool showOnScreen = false)
35+
{
36+
_manualLogSource.LogError(msg);
37+
38+
if (showOnScreen)
39+
ErrorMessage.AddError(msg);
40+
}
41+
42+
public static void Error(string msg, Exception ex)
43+
{
44+
_manualLogSource.LogError($"{msg}{Environment.NewLine}{ex}");
45+
}
46+
47+
public static void Error(Exception ex)
48+
{
49+
_manualLogSource.LogError(ex);
50+
}
51+
52+
public static void Warning(string msg, bool showOnScreen = false)
53+
{
54+
_manualLogSource.LogWarning(msg);
55+
56+
if (showOnScreen)
57+
ErrorMessage.AddWarning(msg);
58+
}
59+
60+
/// <summary>
61+
/// Creates the version string in format "#.#.#" or "#.#.# rev:#"
62+
/// </summary>
63+
public static string GetAssemblyVersion()
64+
{
65+
Version version = ModName.Version;
66+
67+
// Major Version
68+
// Minor Version
69+
// Build Number
70+
// Revision
71+
72+
if (version.Revision > 0)
73+
{
74+
return $"{version.Major}.{version.Minor}.{version.Build} rev:{version.Revision}";
75+
}
76+
77+
if (version.Build > 0)
78+
{
79+
return $"{version.Major}.{version.Minor}.{version.Build}";
80+
}
81+
82+
if (version.Minor > 0)
83+
{
84+
return $"{version.Major}.{version.Minor}.0";
85+
}
86+
87+
return $"{version.Major}.0.0";
88+
}
89+
90+
public static string GetAssemblyName()
91+
{
92+
return ModName.Name;
93+
}
94+
}
95+
}

0 commit comments

Comments
 (0)