Skip to content

Commit

Permalink
快速使用音频
Browse files Browse the repository at this point in the history
  • Loading branch information
DiaoDiaoCN committed Jul 25, 2023
1 parent ca14468 commit b507110
Show file tree
Hide file tree
Showing 21 changed files with 474 additions and 0 deletions.
220 changes: 220 additions & 0 deletions DiaoDiaoAudio/AudioHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,220 @@
using PluginAPI.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using SCPSLAudioApi.AudioCore;
using System.IO;
using PluginAPI.Helpers;
using Mirror;
using CommandSystem;
using System.Xml.Linq;
using UnityEngine;

namespace PluginApiAudio
{
[CommandHandler(typeof(RemoteAdminCommandHandler))]
///音频
public class SCP_Audio : ParentCommand
{
public SCP_Audio() => LoadGeneratedCommands();

public override string Command { get; } = "audio";

public override string[] Aliases { get; } = new string[] { "audio" };

public override string Description { get; } = "播放阴乐";

public override void LoadGeneratedCommands() { }

protected override bool ExecuteParent(ArraySegment<string> arguments, ICommandSender sender, out string response)
{
AudioHelper.Play(arguments.At(0));
response = "听";
return true;
}
}
/// <summary>
/// 音乐播放方法
/// 使用音频核心方案来源:https://github.com/CedModV2/SCPSLAudioApi
/// </summary>
public static class AudioHelper
{
/// <summary>
/// 播放文件夹
/// </summary>
public static readonly string AudioPath = Path.Combine($"{Paths.Plugins + "\\" + Server.Port}", "CustomSounds");

/// <summary>
/// 注册
/// </summary>
internal static void Register()
{
SCPSLAudioApi.Startup.SetupDependencies();
//创建文件夹
if (!Directory.Exists(AudioPath))
Directory.CreateDirectory(AudioPath);
}

/// <summary>
/// 播放音效
/// 仅本人收听
/// </summary>
/// <param name="AudioPath">文件路径</param>
/// <param name="hub">播放人</param>
/// <param name="To">目标,不设置就是全员收听</param>
public static AudioPlayerBase PlayAudio(this Player player, string _AudioFile, bool Loop = false, int Volume = 80)
{
//播放
return Play(_AudioFile, new List<int>() { player.PlayerId }, Loop, Volume);
}

/// <summary>
/// 播放音效
/// 附近玩家听到
/// </summary>
/// <param name="AudioPath">文件路径</param>
/// <param name="hub">播放人</param>
/// <param name="To">目标,不设置就是全员收听</param>
public static AudioPlayerBase PlayAudio(this Player player, string _AudioFile, float distance, bool Loop = false, int Volume = 80)
{
var playerids = new List<int>();
foreach (var p in Player.GetPlayers().Where(p => UnityEngine.Vector3.Distance(p.Position, player.Position) <= distance))
{
playerids.Add(p.PlayerId);
}
//播放
return Play(_AudioFile, playerids, Loop, Volume);
}

/// <summary>
/// 播放音效
/// </summary>
/// <param name="AudioPath">文件路径</param>
/// <param name="hub">播放人</param>
/// <param name="To">目标,不设置就是全员收听</param>
public static AudioPlayerBase Play(string _AudioFile, List<int> To = null, bool Loop = false, int Volume = 80)
{
//收听人
if (To == null)
To = Player.GetPlayers().Select(p => p.PlayerId).ToList();
//播放
return _Play(_AudioFile, To, Loop, Volume);
}

private static AudioPlayerBase _Play(string _AudioFile, List<int> To, bool Loop, int Volume = 80)
{
if (DiaoDiaoAudio.Singleton.DiaoDiaoConfig.EnableAudio)
{
var AudioFile = Path.Combine(AudioPath, _AudioFile + ".ogg");
if (!File.Exists(AudioFile))
{
Log.Error("音频文件不存在:" + AudioFile);
return null;
}

//播放
AudioPlayerBase audtioPlayer = null;
if (AudioRob.GetNoPlayRob(out var _ad))
{
audtioPlayer = _ad;//已有的播放器
}
else
{
//创建假人
var hubPlayer = AudioRob.RobPlayerSimple();
//播放器
audtioPlayer = AudioPlayerBase.Get(hubPlayer);
}
audtioPlayer.Enqueue(AudioFile, -1);//播放文件
if (To != null)
audtioPlayer.BroadcastTo.AddRange(To);//收听方,不设置就是全员收听
audtioPlayer.Volume = Volume;//音量
audtioPlayer.Loop = Loop;//循环
audtioPlayer.Play(0);
return audtioPlayer;
}
else
{
Log.Error("插件未启用");
}
return null;
}
}

/// <summary>
/// 作者:调调
/// 说明:
/// 假人
/// </summary>
public static class AudioRob
{
private static int id = 0;

/// <summary>
/// 是否假人
/// </summary>
public static bool IsRob(this ReferenceHub player)
{
return AudioPlayerBase.AudioPlayers.ContainsKey(player);
}

/// <summary>
/// 获取没有播放的假人
/// </summary>
public static bool GetNoPlayRob(out AudioPlayerBase _ad)
{
if (AudioPlayerBase.AudioPlayers.Count(p => p.Value.IsFinish ) > 0)
{
_ad = AudioPlayerBase.AudioPlayers.First(p => p.Value.IsFinish).Value;
return true;
}
_ad = null;
return false;
}

/// <summary>
/// 简单创建假人,用于播放音频
/// </summary>
public static ReferenceHub RobPlayerSimple()
{
if (DiaoDiaoAudio.Singleton.DiaoDiaoConfig.EnableAudio)
{
var newPlayer = UnityEngine.Object.Instantiate(NetworkManager.singleton.playerPrefab);
var fakeConnection = new FakeConnection(id);
var hubPlayer = newPlayer.GetComponent<ReferenceHub>();
NetworkServer.AddPlayerForConnection(fakeConnection, newPlayer);
return hubPlayer;
}
else
{
Log.Error("插件未启用");
}
return null;
}

public class FakeConnection : NetworkConnectionToClient
{
public FakeConnection(int connectionId) : base(connectionId)
{

}

public override string address
{
get
{
return "localhost";
}
}

public override void Send(ArraySegment<byte> segment, int channelId = 0)
{
}
public override void Disconnect()
{
}
}
}
}
46 changes: 46 additions & 0 deletions DiaoDiaoAudio/DiaoDiaoAudio.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using HarmonyLib;
using MEC;
using Microsoft.Win32;
using PluginAPI.Core;
using PluginAPI.Core.Attributes;
using PluginAPI.Enums;
using PluginAPI.Events;
using SCPSLAudioApi.AudioCore;
using System.Collections.Generic;
using System.Linq;
using static RoundSummary;

namespace PluginApiAudio
{
public class DiaoDiaoAudio
{
public static DiaoDiaoAudio Singleton { get; private set; }

[PluginEntryPoint("音频插件", "1.0.0.", "音频", "调调")]
void LoadPlugin()
{
Singleton = this;
AudioHelper.Register();
EventManager.RegisterEvents(this);
}

[PluginConfig]
public DiaoDiaoAudioConfig DiaoDiaoConfig;

[PluginEvent(ServerEventType.WaitingForPlayers)]
public void aa()
{
Timing.RunCoroutine(test());
}

IEnumerator<float> test()
{
while (true)
{
Log.Info(Player.GetPlayers().Count.ToString());
Log.Info(AudioPlayerBase.AudioPlayers.Count.ToString());
yield return Timing.WaitForSeconds(1f);
}
}
}
}
110 changes: 110 additions & 0 deletions DiaoDiaoAudio/DiaoDiaoAudio.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{B8BE22B2-E29B-43EF-88FC-05C1125DC169}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>DiaoDiaoAudio</RootNamespace>
<AssemblyName>DiaoDiaoAudio</AssemblyName>
<TargetFrameworkVersion>v4.8</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<Deterministic>true</Deterministic>
<TargetFrameworkProfile />
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="0Harmony">
<HintPath>..\Lib\0Harmony.dll</HintPath>
</Reference>
<Reference Include="Assembly-CSharp, Version=0.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\Lib\Assembly-CSharp.dll</HintPath>
</Reference>
<Reference Include="Assembly-CSharp-firstpass">
<HintPath>..\Lib\Assembly-CSharp-firstpass.dll</HintPath>
</Reference>
<Reference Include="CommandSystem.Core, Version=0.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\Lib\CommandSystem.Core.dll</HintPath>
</Reference>
<Reference Include="Mirror, Version=0.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\Lib\Mirror.dll</HintPath>
</Reference>
<Reference Include="Newtonsoft.Json, Version=13.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.13.0.1\lib\net45\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="NorthwoodLib, Version=1.2.1.0, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\Lib\NorthwoodLib.dll</HintPath>
</Reference>
<Reference Include="PluginAPI, Version=12.0.0.0, Culture=neutral, processorArchitecture=AMD64">
<HintPath>..\packages\Northwood.PluginAPI.12.0.0\lib\net48\PluginAPI.dll</HintPath>
</Reference>
<Reference Include="SCPSLAudioApi">
<HintPath>..\Lib\SCPSLAudioApi.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Buffers, Version=4.0.2.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\System.Buffers.4.4.0\lib\netstandard2.0\System.Buffers.dll</HintPath>
</Reference>
<Reference Include="System.Core" />
<Reference Include="System.Management" />
<Reference Include="System.Memory, Version=4.0.1.1, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\System.Memory.4.5.3\lib\netstandard2.0\System.Memory.dll</HintPath>
</Reference>
<Reference Include="System.Numerics" />
<Reference Include="System.Numerics.Vectors, Version=4.1.3.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Numerics.Vectors.4.4.0\lib\net46\System.Numerics.Vectors.dll</HintPath>
</Reference>
<Reference Include="System.Runtime.CompilerServices.Unsafe, Version=4.0.4.1, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Runtime.CompilerServices.Unsafe.4.5.2\lib\netstandard2.0\System.Runtime.CompilerServices.Unsafe.dll</HintPath>
</Reference>
<Reference Include="System.ValueTuple, Version=4.0.3.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\System.ValueTuple.4.5.0\lib\net47\System.ValueTuple.dll</HintPath>
</Reference>
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
<Reference Include="UnityEngine.CoreModule, Version=0.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\Lib\UnityEngine.CoreModule.dll</HintPath>
</Reference>
<Reference Include="YamlDotNet, Version=11.0.0.0, Culture=neutral, PublicKeyToken=ec19458f3c15af5e, processorArchitecture=MSIL">
<HintPath>..\packages\YamlDotNet.11.0.1\lib\net45\YamlDotNet.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="AudioHelper.cs" />
<Compile Include="DiaoDiaoAudio.cs" />
<Compile Include="DiaoDiaoAudioConfig.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<ItemGroup />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
6 changes: 6 additions & 0 deletions DiaoDiaoAudio/DiaoDiaoAudio.csproj.user
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<ProjectView>ProjectFiles</ProjectView>
</PropertyGroup>
</Project>
Loading

0 comments on commit b507110

Please sign in to comment.