Skip to content

Commit

Permalink
Version up to 1.1 and code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
ManlyMarco committed May 15, 2018
1 parent 3910680 commit aa01979
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 73 deletions.
16 changes: 16 additions & 0 deletions FPSCounter/Extensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;

namespace FPSCounter
{
public static class Extensions
{
public static T Next<T>(this T src) where T : struct
{
if (!typeof(T).IsEnum) throw new ArgumentException(String.Format("Argumnent {0} is not an Enum", typeof(T).FullName));

T[] Arr = (T[])Enum.GetValues(src.GetType());
int j = Array.IndexOf<T>(Arr, src) + 1;
return (Arr.Length == j) ? Arr[0] : Arr[j];
}
}
}
129 changes: 57 additions & 72 deletions FPSCounter/FPSCounter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,61 @@

namespace FPSCounter
{
[BepInPlugin("B6C411AF-D2BA-4CAE-B5A9-94C9CC0BF9A1", "FPS Counter", "1.0")]
[BepInPlugin("FPSCounter", "FPS Counter", "1.1")]
public class FPSCounter : BaseUnityPlugin
{
private static readonly string ConfigFileLocation;
private readonly float avgFraction = 0.994f;
private readonly int screenOffset = 20;

private State currentState = State.Normal;
private float deltaTime;
private bool initial;
private float lastAvg;
private float minFps, maxFps;

private Rect screenRect;
private GUIStyle style = new GUIStyle();

static FPSCounter()
{
ConfigFileLocation = Assembly.GetExecutingAssembly().Location + ".config";
}

private enum State
{
Hidden,
Normal,
NormalWhite,
}

private System.Collections.IEnumerator HideKeybindInfo()
{
yield return new WaitForSecondsRealtime(15);

initial = false;
}

private void LoadConfig()
{
if (File.Exists(ConfigFileLocation))
{
initial = false;
try
{
currentState = (State)int.Parse(File.ReadAllText(ConfigFileLocation));
}
catch (Exception ex)
{
BepInLogger.Log("Failed to load FPSCounter config: " + ex.Message);
}
}
else
{
initial = true;
}
}

private void OnGUI()
{
if (currentState == State.Hidden) return;
Expand All @@ -40,6 +82,11 @@ private void OnGUI()
GUI.Label(screenRect, text, style);
}

private void OnLevelFinishedLoading(Scene sc, LoadSceneMode mode)
{
ResetValues();
}

private void ResetValues()
{
minFps = float.MaxValue;
Expand All @@ -57,11 +104,16 @@ private void ResetValues()
style.normal.textColor = Color.white;
}

private static readonly string ConfigFileLocation;

static FPSCounter()
private void SaveConfig()
{
ConfigFileLocation = Assembly.GetExecutingAssembly().Location + ".config";
try
{
File.WriteAllText(ConfigFileLocation, ((int)currentState).ToString());
}
catch (Exception ex)
{
BepInLogger.Log("Failed to save FPSCounter config: " + ex.Message);
}
}

private void Start()
Expand All @@ -78,49 +130,6 @@ private void Start()
SceneManager.sceneLoaded += OnLevelFinishedLoading;
}

private void OnLevelFinishedLoading(Scene sc, LoadSceneMode mode)
{
ResetValues();
}

private bool initial;

private System.Collections.IEnumerator HideKeybindInfo()
{
yield return new WaitForSecondsRealtime(15);

initial = false;
}

private void LoadConfig()
{
if (File.Exists(ConfigFileLocation))
{
initial = false;
try
{
currentState = (State)int.Parse(File.ReadAllText(ConfigFileLocation));
}
catch (Exception ex)
{
BepInLogger.Log("Failed to load FPSCounter config: " + ex.Message);
}
}
else
{
initial = true;
}
}

State currentState = State.Normal;

private enum State
{
Hidden,
Normal,
NormalWhite,
}

private void Update()
{
if (Input.GetKeyDown(KeyCode.U))
Expand All @@ -137,29 +146,5 @@ private void Update()

deltaTime += (Time.unscaledDeltaTime - deltaTime) * 0.1f;
}

private void SaveConfig()
{
try
{
File.WriteAllText(ConfigFileLocation, ((int)currentState).ToString());
}
catch (Exception ex)
{
BepInLogger.Log("Failed to save FPSCounter config: " + ex.Message);
}
}
}

public static class Extensions
{
public static T Next<T>(this T src) where T : struct
{
if (!typeof(T).IsEnum) throw new ArgumentException(String.Format("Argumnent {0} is not an Enum", typeof(T).FullName));

T[] Arr = (T[])Enum.GetValues(src.GetType());
int j = Array.IndexOf<T>(Arr, src) + 1;
return (Arr.Length == j) ? Arr[0] : Arr[j];
}
}
}
1 change: 1 addition & 0 deletions FPSCounter/FPSCounter.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
<CodeAnalysisRuleSet>FPSCounter.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>
<ItemGroup>
<Compile Include="Extensions.cs" />
<Compile Include="FPSCounter.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion FPSCounter/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,4 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0")]
[assembly: AssemblyVersion("1.1.*")]

0 comments on commit aa01979

Please sign in to comment.