Skip to content

Commit

Permalink
OOP rewrite
Browse files Browse the repository at this point in the history
  • Loading branch information
iBowie committed Jun 10, 2020
1 parent fd3b8b6 commit 115be85
Show file tree
Hide file tree
Showing 8 changed files with 246 additions and 69 deletions.
5 changes: 4 additions & 1 deletion BowieD.Unturned.AssetExpander.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,11 @@
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="CustomFields\Items\StarveCustomField.cs" />
<Compile Include="CustomFields\Items\ThirstCustomField.cs" />
<Compile Include="CustomFields\Items\TireCustomField.cs" />
<Compile Include="ESearchMode.cs" />
<Compile Include="EventManager.cs" />
<Compile Include="Models\CustomField.cs" />
<Compile Include="Plugin.cs" />
<Compile Include="PluginConfiguration.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
Expand Down
45 changes: 45 additions & 0 deletions CustomFields/Items/StarveCustomField.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using BowieD.Unturned.AssetExpander.Models;
using SDG.Unturned;

namespace BowieD.Unturned.AssetExpander.CustomFields.Items
{
public class StarveCustomField : ICustomField
{
public string Name => "Starve";
public EAssetType Type => EAssetType.ITEM;
public void Init()
{
UseableConsumeable.onConsumePerformed += UseableConsumeable_onConsumePerformed;
UseableConsumeable.onPerformedAid += UseableConsumeable_onPerformedAid;
}

private void UseableConsumeable_onPerformedAid(Player instigator, Player target)
{
var asset = instigator.equipment.asset;
if (asset is ItemConsumeableAsset ica)
consume(target, ica);
}

private void UseableConsumeable_onConsumePerformed(Player instigatingPlayer, ItemConsumeableAsset consumeableAsset)
{
consume(instigatingPlayer, consumeableAsset);
}

private void consume(Player player, ItemConsumeableAsset asset)
{
if (Plugin.CustomData.TryGetValue(asset.GUID, out var cData))
{
if (cData.TryGetValue(Name, out var raw) && byte.TryParse(raw, out byte parsed))
{
player.life.serverModifyFood(-parsed);
}
}
}

public void Stop()
{
UseableConsumeable.onConsumePerformed -= UseableConsumeable_onConsumePerformed;
UseableConsumeable.onPerformedAid -= UseableConsumeable_onPerformedAid;
}
}
}
45 changes: 45 additions & 0 deletions CustomFields/Items/ThirstCustomField.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using BowieD.Unturned.AssetExpander.Models;
using SDG.Unturned;

namespace BowieD.Unturned.AssetExpander.CustomFields.Items
{
public class ThirstCustomField : ICustomField
{
public string Name => "Thirst";
public EAssetType Type => EAssetType.ITEM;
public void Init()
{
UseableConsumeable.onConsumePerformed += UseableConsumeable_onConsumePerformed;
UseableConsumeable.onPerformedAid += UseableConsumeable_onPerformedAid;
}

private void UseableConsumeable_onPerformedAid(Player instigator, Player target)
{
var asset = instigator.equipment.asset;
if (asset is ItemConsumeableAsset ica)
consume(target, ica);
}

private void UseableConsumeable_onConsumePerformed(Player instigatingPlayer, ItemConsumeableAsset consumeableAsset)
{
consume(instigatingPlayer, consumeableAsset);
}

private void consume(Player player, ItemConsumeableAsset asset)
{
if (Plugin.CustomData.TryGetValue(asset.GUID, out var cData))
{
if (cData.TryGetValue(Name, out var raw) && byte.TryParse(raw, out byte parsed))
{
player.life.serverModifyWater(-parsed);
}
}
}

public void Stop()
{
UseableConsumeable.onConsumePerformed -= UseableConsumeable_onConsumePerformed;
UseableConsumeable.onPerformedAid -= UseableConsumeable_onPerformedAid;
}
}
}
45 changes: 45 additions & 0 deletions CustomFields/Items/TireCustomField.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using BowieD.Unturned.AssetExpander.Models;
using SDG.Unturned;

namespace BowieD.Unturned.AssetExpander.CustomFields.Items
{
public class TireCustomField : ICustomField
{
public string Name => "Tire";
public EAssetType Type => EAssetType.ITEM;
public void Init()
{
UseableConsumeable.onConsumePerformed += UseableConsumeable_onConsumePerformed;
UseableConsumeable.onPerformedAid += UseableConsumeable_onPerformedAid;
}

private void UseableConsumeable_onPerformedAid(Player instigator, Player target)
{
var asset = instigator.equipment.asset;
if (asset is ItemConsumeableAsset ica)
consume(target, ica);
}

private void UseableConsumeable_onConsumePerformed(Player instigatingPlayer, ItemConsumeableAsset consumeableAsset)
{
consume(instigatingPlayer, consumeableAsset);
}

private void consume(Player player, ItemConsumeableAsset asset)
{
if (Plugin.CustomData.TryGetValue(asset.GUID, out var cData))
{
if (cData.TryGetValue(Name, out var raw) && byte.TryParse(raw, out byte parsed))
{
player.life.serverModifyStamina(-parsed);
}
}
}

public void Stop()
{
UseableConsumeable.onConsumePerformed -= UseableConsumeable_onConsumePerformed;
UseableConsumeable.onPerformedAid -= UseableConsumeable_onPerformedAid;
}
}
}
50 changes: 0 additions & 50 deletions EventManager.cs

This file was deleted.

24 changes: 24 additions & 0 deletions Models/CustomField.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using SDG.Unturned;

namespace BowieD.Unturned.AssetExpander.Models
{
public interface ICustomField
{
/// <summary>
/// Field Name
/// </summary>
string Name { get; }
/// <summary>
/// Asset Type
/// </summary>
EAssetType Type { get; }
/// <summary>
/// Used on start of the server to init all the custom fields
/// </summary>
void Init();
/// <summary>
/// Used to dispose of any custom fields
/// </summary>
void Stop();
}
}
97 changes: 81 additions & 16 deletions Plugin.cs
Original file line number Diff line number Diff line change
@@ -1,38 +1,99 @@
using Rocket.Core.Plugins;
using BowieD.Unturned.AssetExpander.Models;
using Rocket.Core.Plugins;
using SDG.Unturned;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;

namespace BowieD.Unturned.AssetExpander
{
public sealed class Plugin : RocketPlugin<PluginConfiguration>
{
internal static Plugin Instance { get; private set; }
internal static Dictionary<Guid, Dictionary<string, string>> CustomData { get; } = new Dictionary<Guid, Dictionary<string, string>>();
internal static HashSet<ICustomField> Fields { get; } = new HashSet<ICustomField>();

protected override void Load()
{
CustomData.Clear();
RegisterCustomFields(Assembly);
LoadCustomData();
EventManager.Start();

Rocket.Core.Logging.Logger.Log("Plugin created by BowieD");
Rocket.Core.Logging.Logger.Log(@"https://github.com/iBowie/BowieD.Unturned.AssetExpander");
}
protected override void Unload()
{
EventManager.Stop();
CustomData.Clear();
}

void LoadCustomData()
public void RegisterCustomField<T>() where T : class, ICustomField, new()
{
RegisterCustomField(new T());
}
public void RegisterCustomField<T>(T instance) where T : class, ICustomField
{
RegisterCustomField(instance);
}
public void RegisterCustomFields(Assembly assembly)
{
foreach (var t in assembly.GetTypes())
{
if (t.IsClass && typeof(ICustomField).IsAssignableFrom(t))
{
try
{
ICustomField instance = (ICustomField)Activator.CreateInstance(t);
if (instance == null)
continue;

RegisterCustomField(instance);
}
catch { }
}
}
}

private void RegisterCustomField(ICustomField instance)
{
Fields.Add(instance);
instance.Init();
}
private void LoadCustomData()
{
if (Configuration.Instance.SearchMode == ESearchMode.OFF)
return;

if (Configuration.Instance.SearchMode.HasFlag(ESearchMode.ASSET))
{
Rocket.Core.Logging.Logger.Log("ASSET search is disabled");
List<Asset> assets = new List<Asset>();
Assets.find(assets);

foreach (var asset in assets)
{
var assetPath = asset.absoluteOriginFilePath;
var fields = Fields.Where(d => d.Type == asset.assetCategory);
if (fields.Any())
{
using (StreamReader sr = new StreamReader(assetPath))
{
while (!sr.EndOfStream)
{
string line = sr.ReadLine();
string[] split = line.Split(' ');
string name = split[0];
string value = string.Join(" ", split.Skip(1));

if (fields.Any(d => d.Name == name))
{
addOrOverrideCustomData(asset.GUID, name, value);
}
}
}
}
}
}

if (Configuration.Instance.SearchMode.HasFlag(ESearchMode.CONFIG))
Expand All @@ -44,22 +105,26 @@ void LoadCustomData()
if (asset is null)
continue;

if (!CustomData.ContainsKey(asset.GUID))
{
CustomData.Add(asset.GUID, new Dictionary<string, string>());
}

var dict = CustomData[asset.GUID];

foreach (var cf in kv.Value)
{
if (dict.ContainsKey(cf.Key))
dict[cf.Key] = cf.Value;
else
dict.Add(cf.Key, cf.Value);
addOrOverrideCustomData(g, cf.Key, cf.Value);
}
}
}
}
void addOrOverrideCustomData(Guid g, string name, string value)
{
if (!CustomData.ContainsKey(g))
{
CustomData.Add(g, new Dictionary<string, string>());
}

var dict = CustomData[g];

if (dict.ContainsKey(name))
dict[name] = value;
else
dict.Add(name, value);
}
}
}
4 changes: 2 additions & 2 deletions Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@
// 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.1.0")]
[assembly: AssemblyFileVersion("1.0.1.0")]
[assembly: AssemblyVersion("1.1.0.0")]
[assembly: AssemblyFileVersion("1.1.0.0")]

0 comments on commit 115be85

Please sign in to comment.