Skip to content
This repository has been archived by the owner on Nov 10, 2021. It is now read-only.

Commit

Permalink
Updated project
Browse files Browse the repository at this point in the history
  • Loading branch information
erri120 committed May 17, 2020
1 parent 9ba85b0 commit 2f5fd3a
Show file tree
Hide file tree
Showing 8 changed files with 181 additions and 51 deletions.
46 changes: 5 additions & 41 deletions Plugin.cs
Original file line number Diff line number Diff line change
@@ -1,61 +1,25 @@
using NetScriptFramework.SkyrimSE;
using NetScriptFramework.Tools;

namespace PluginTemplate
namespace UtilityLibrary
{
public class Plugin : NetScriptFramework.Plugin
{
public override string Key => "plugin.template";
public static string PluginName => "Plugin Template";
public override string Key => "utility.library";
public static string PluginName => "Utility Library";
public override string Name => PluginName;
public override int Version => 1;

public override string Author => "erri120";
public override string Website => "https://github.com/erri120/";
public override string Website => "https://github.com/erri120/netscriptframework-utility";

public override int RequiredFrameworkVersion => 9;
public override int RequiredLibraryVersion => 13;

private bool _inMainMenu;
private Timer _timer;
private readonly object _lockObject = new object();
private long _lastTime;

protected override bool Initialize(bool loadedAny)
{
_timer = new Timer();

Events.OnMainMenu.Register(e =>
{
_inMainMenu = e.Entering;
if(e.Entering && _timer.IsRunning)
_timer.Stop();
else if(!e.Entering && !_timer.IsRunning)
_timer.Start();
});

Events.OnFrame.Register(e =>
{
var now = _timer.Time;
if (now - _lastTime < 1000)
return;
_lastTime = now;
if (Main.Instance == null)
return;
if (_inMainMenu || Main.Instance.IsGamePaused)
return;
var player = PlayerCharacter.Instance;
if (player == null)
return;
lock (_lockObject)
{
//do something
}
UtilityLibrary.IsInMainMenu = e.Entering;
});

return true;
Expand Down
6 changes: 3 additions & 3 deletions Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("PluginTemplate")]
[assembly: AssemblyDescription("A Template for NetScriptFramework Plugins")]
[assembly: AssemblyTitle("Utility Library")]
[assembly: AssemblyDescription("A Library with utility functions for NetScriptFramework Plugins")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("erri120")]
[assembly: AssemblyProduct("PluginTemplate")]
[assembly: AssemblyProduct("UtilityLibrary")]
[assembly: AssemblyCopyright("Copyright © erri120 2020")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
Expand Down
2 changes: 1 addition & 1 deletion PluginTemplate.sln → Utility Library.sln
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.30104.148
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PluginTemplate", "PluginTemplate.csproj", "{D76AB2C4-39E1-4097-9C95-B41EEF91BE3C}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UtilityLibrary", "UtilityLibrary.csproj", "{D76AB2C4-39E1-4097-9C95-B41EEF91BE3C}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down
19 changes: 19 additions & 0 deletions UtilityLibrary.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using NetScriptFramework.SkyrimSE;

namespace UtilityLibrary
{
public static partial class UtilityLibrary
{
/// <summary>
/// Whether or not the game is in the main menu
/// </summary>
public static bool IsInMainMenu { get; internal set; }

/// <summary>
/// Whether or not the player is in game. This means the player is not
/// in the main menu, <see cref="Main.Instance"/> is not <c>null</c> and
/// the game is not paused.
/// </summary>
public static bool IsInGame => !IsInMainMenu && Main.Instance != null && !Main.Instance.IsGamePaused;
}
}
12 changes: 10 additions & 2 deletions PluginTemplate.csproj → UtilityLibrary.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
<ProjectGuid>{D76AB2C4-39E1-4097-9C95-B41EEF91BE3C}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>PluginTemplate</RootNamespace>
<AssemblyName>PluginTemplate</AssemblyName>
<RootNamespace>UtilityLibrary</RootNamespace>
<AssemblyName>UtilityLibrary</AssemblyName>
<TargetFrameworkVersion>v4.8</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<Deterministic>true</Deterministic>
Expand Down Expand Up @@ -51,6 +51,9 @@
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>
<ItemGroup>
<Reference Include="JetBrains.Annotations, Version=2020.1.0.0, Culture=neutral, PublicKeyToken=1010a0d8d6380325, processorArchitecture=MSIL">
<HintPath>packages\JetBrains.Annotations.2020.1.0\lib\net20\JetBrains.Annotations.dll</HintPath>
</Reference>
<Reference Include="NetScriptFramework">
<HintPath>..\NetScriptFramework\NetScriptFramework.dll</HintPath>
</Reference>
Expand All @@ -69,7 +72,12 @@
<ItemGroup>
<Compile Include="Plugin.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="UtilityLibrary.cs" />
<Compile Include="UtilityLibrary\ActorExtensions.cs" />
<Compile Include="Utils.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
135 changes: 135 additions & 0 deletions UtilityLibrary/ActorExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
using JetBrains.Annotations;
using NetScriptFramework.SkyrimSE;

namespace UtilityLibrary
{
public static partial class UtilityLibrary
{
private const uint CreatureKeywordID = 0x13795;
private static BGSKeyword _creatureKeyword;

private const uint UndeadKeywordID = 0x13796;
private static BGSKeyword _undeadKeyword;

private const uint AnimalKeywordID = 0x13798;
private static BGSKeyword _animalKeyword;

/// <summary>
/// Checks if the given actor is valid by doing null checks and checking
/// if the base from of the actor is either NPC or Leveled Character.
/// </summary>
/// <param name="actor">The Actor</param>
/// <returns><c>true</c> if valid actor, <c>false</c> if not</returns>
public static bool IsValidActor(this Actor actor)
{
if (actor == null)
return false;

if (actor.IsPlayer)
return true;

if (actor.BaseForm.FormType != FormTypes.NPC || actor.BaseForm.FormType != FormTypes.LeveledCharacter)
return false;

return true;
}

/// <summary>
/// Returns the name of an actor. This goes beyond the normal
/// <see cref="TESForm.Name"/> and checks the <see cref="Actor.BaseActor"/>
/// and <see cref="TESObjectREFR.BaseForm"/> as well.
/// </summary>
/// <param name="actor">The Actor</param>
/// <returns>Either the name of an actor or null if the actor has no name</returns>
[CanBeNull]
public static string GetName([NotNull] this Actor actor)
{
if (string.IsNullOrWhiteSpace(actor.Name))
{
return string.IsNullOrWhiteSpace(actor.BaseActor.Name)
? actor.BaseForm.Name
: null;
}

return actor.Name;
}

/// <summary>
/// Checks if the given actor is Female.
/// </summary>
/// <param name="actor">The Actor</param>
/// <returns><c>true</c> if female, <c>false</c> if not</returns>
public static bool IsFemale([NotNull] this Actor actor)
{
return (actor.BaseActor.ActorData.ActorBaseFlags & TESActorBaseData.DataFlags.Female) !=
TESActorBaseData.DataFlags.None;
}

/// <summary>
/// Checks if the given actor is Male.
/// </summary>
/// <param name="actor">The Actor</param>
/// <returns><c>true</c> if male, <c>false</c> if not</returns>
public static bool IsMale([NotNull] this Actor actor)
{
return !actor.IsFemale();
}

/// <summary>
/// Checks if the given actor is a creature by checking if it
/// has the creature keyword.
/// </summary>
/// <param name="actor">The Actor</param>
/// <returns><c>true</c> if creature, <c>false</c> if not</returns>
public static bool IsCreature([NotNull] this Actor actor)
{
if (_creatureKeyword != null) return actor.Race.HasKeyword(_creatureKeyword);

var keywordForm = TESForm.LookupFormById(CreatureKeywordID);
if (keywordForm is BGSKeyword keyword)
{
_creatureKeyword = keyword;
}

return actor.Race.HasKeyword(_creatureKeyword);
}

/// <summary>
/// Checks if the given actor is an animal by checking if it
/// has the animal keyword.
/// </summary>
/// <param name="actor">The Actor</param>
/// <returns><c>true</c> if animal, <c>false</c> if not</returns>
public static bool IsAnimal([NotNull] this Actor actor)
{
if (_animalKeyword != null) return actor.Race.HasKeyword(_animalKeyword);

var keywordForm = TESForm.LookupFormById(AnimalKeywordID);
if (keywordForm is BGSKeyword keyword)
{
_animalKeyword = keyword;
}

return actor.Race.HasKeyword(_animalKeyword);
}

/// <summary>
/// Checks if the given actor is an undead by checking if it
/// has the undead keyword.
/// </summary>
/// <param name="actor">The Actor</param>
/// <returns><c>true</c> if undead, <c>false</c> if not</returns>
public static bool IsUndead([NotNull] this Actor actor)
{
if (_undeadKeyword != null) return actor.Race.HasKeyword(_undeadKeyword);

var keywordForm = TESForm.LookupFormById(UndeadKeywordID);
if (keywordForm is BGSKeyword keyword)
{
_undeadKeyword = keyword;
}

return actor.Race.HasKeyword(_undeadKeyword);
}
}
}
8 changes: 4 additions & 4 deletions Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
using System.Collections.Generic;
using NetScriptFramework.Tools;

namespace PluginTemplate
namespace UtilityLibrary
{
public static class Utils
internal static class Utils
{
private static readonly LogFile LogFile;

Expand All @@ -14,14 +14,14 @@ static Utils()
LogFile = new LogFile(Plugin.PluginName, LogFileFlags.AppendFile | LogFileFlags.AutoFlush | LogFileFlags.IncludeTimestampInLine);
}

public static void Log(string s)
internal static void Log(string s)
{
LogFile.AppendLine(s);
//uncomment if you want to display the log message on the HUD, useful when for debugging
//NetScriptFramework.SkyrimSE.MenuManager.ShowHUDMessage(s, null, true);
}

public static void Do<T>(this IEnumerable<T> enumerable, Action<T> action)
internal static void Do<T>(this IEnumerable<T> enumerable, Action<T> action)
{
foreach (var item in enumerable) action(item);
}
Expand Down
4 changes: 4 additions & 0 deletions packages.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="JetBrains.Annotations" version="2020.1.0" targetFramework="net48" />
</packages>

0 comments on commit 2f5fd3a

Please sign in to comment.