Repository navigation
Lua (& more) #264
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Lua (& more) #264
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
bf6f2da
LuaUserMessages
callumok2004 8d46367
Fixes
callumok2004 11ae91b
Some ent/player methods
callumok2004 dc9de41
CreateMove, more methods
callumok2004 92e894a
More methods
callumok2004 8c95478
Bass, GModAudioChannel stuff
callumok2004 41bfbac
Some fixes
callumok2004 955ee2d
Add missing debugoverlay's
callumok2004 0c489d9
Fix
callumok2004 ff569d8
Fix dedicated
callumok2004 ae8b551
Pixel visibility
callumok2004 cbe3207
client bonemanipulate
callumok2004 b980c6b
Fix audiochannel filename
callumok2004 233c0b4
more lua methods
callumok2004 ea0b0a0
SENT ClientThink
callumok2004 89b736c
Weapons in thirdperson, fix head pos+ang, blinking
callumok2004 ae4a3f2
Fix tp using wrong weapon models
callumok2004 7bfce08
Remove redundant check
callumok2004 adac6f5
LuaMesh & related studiorender/shaderapi impl
callumok2004 105f291
Suppress analyzer release tracking warnings
callumok2004 6fcb35e
Add LuaJIT OSX binaries
marchc1 62c9ab6
Fix my remaining concerns
marchc1 daaa2ff
Merge branch 'main' into lua
marchc1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Submodule LuaJIT
updated
from 3546db to d8b530
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| using ManagedBass; | ||
|
|
||
| using Source.Common.Audio; | ||
| using Source.Common.GarrysMod; | ||
|
|
||
| using System.Runtime.InteropServices; | ||
|
|
||
| namespace Game.BASS; | ||
|
|
||
| public class BassAudioStream : IBassAudioStream, IDisposable | ||
| { | ||
| int Handle; | ||
| IAudioStreamEvent? Event; | ||
| uint Offset; | ||
| GCHandle Self; | ||
|
|
||
| static readonly FileProcedures StreamProcs = new() { | ||
| Close = static user => { }, | ||
| Length = static user => 0, | ||
| Read = StreamRead, | ||
| Seek = StreamSeek, | ||
| }; | ||
|
|
||
| static BassAudioStream Get(nint user) => (BassAudioStream)GCHandle.FromIntPtr(user).Target!; | ||
|
|
||
| static unsafe int StreamRead(nint buffer, int length, nint user) { | ||
| BassAudioStream stream = Get(user); | ||
| int read = stream.Event!.StreamRequestData(new Span<byte>((void*)buffer, length), length, (int)stream.Offset); | ||
| stream.Offset += (uint)read; | ||
| return read; | ||
| } | ||
|
|
||
| static bool StreamSeek(long offset, nint user) { | ||
| if ((ulong)offset > 0x4000000) | ||
| return false; | ||
| Get(user).Offset = (uint)offset; | ||
| return true; | ||
| } | ||
|
|
||
| public bool Init(IAudioStreamEvent? eventHandler) { | ||
| Event = eventHandler; | ||
| Self = GCHandle.Alloc(this); | ||
| Handle = Bass.CreateStream(StreamSystem.NoBuffer, BassFlags.Decode | BassFlags.StreamDownloadBlocks | BassFlags.RestrictDownloadRate, StreamProcs, GCHandle.ToIntPtr(Self)); | ||
| if (Handle == 0) { | ||
| Warning($"Couldn't create BASS audio stream ({GMod_Audio.BassErrorDescription(Bass.LastError)})\n"); | ||
| return false; | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| public void Dispose() { | ||
| if (Handle != 0) | ||
| Bass.StreamFree(Handle); | ||
| if (Self.IsAllocated) | ||
| Self.Free(); | ||
| } | ||
|
|
||
| public unsafe int Decode(Span<byte> buffer) { | ||
| if (Handle == 0) | ||
| return 0; | ||
| int ret; | ||
| fixed (byte* ptr = buffer) | ||
| ret = Bass.ChannelGetData(Handle, (nint)ptr, buffer.Length); | ||
| return ret < 0 ? 0 : ret; | ||
| } | ||
|
|
||
| public int GetOutputBits() { | ||
| Error("Not used"); | ||
| return 0; | ||
| } | ||
|
|
||
| public int GetOutputRate() { | ||
| Bass.ChannelGetInfo(Handle, out ChannelInfo info); | ||
| return info.Frequency == 0 ? 44100 : info.Frequency; | ||
| } | ||
|
|
||
| public int GetOutputChannels() { | ||
| Bass.ChannelGetInfo(Handle, out ChannelInfo info); | ||
| return info.Channels == 0 ? 2 : info.Channels; | ||
| } | ||
|
|
||
| public uint GetPosition() { | ||
| if (Handle == 0) | ||
| return 0; | ||
| return (uint)Bass.StreamGetFilePosition(Handle, FileStreamPosition.Current); | ||
| } | ||
|
|
||
| public void SetPosition(uint distance) { } | ||
|
|
||
| public uint GetHandle() => (uint)Handle; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,226 @@ | ||
| using ManagedBass; | ||
|
|
||
| using Source.Common.GarrysMod; | ||
|
|
||
| using System.Numerics; | ||
| using System.Runtime.CompilerServices; | ||
| using System.Runtime.InteropServices; | ||
|
|
||
| namespace Game.BASS; | ||
|
|
||
| public class GModAudioChannel(int handle, bool isFile, int type, string? fileName) : IGModAudioChannel | ||
| { | ||
| int Handle = handle; | ||
| readonly bool IsFile = isFile; | ||
| readonly int Type = type; | ||
| readonly string? FileName = fileName; | ||
|
|
||
| public void Destroy() { | ||
| if (Bass.ChannelIsActive(Handle) == PlaybackState.Playing && !IsLooping() && Bass.ChannelGetLength(Handle, PositionFlags.Bytes) >= 0) { | ||
| Bass.ChannelFlags(Handle, BassFlags.AutoFree, BassFlags.AutoFree); | ||
| Handle = 0; | ||
| return; | ||
| } | ||
|
|
||
| if (Type == 0) | ||
| Bass.StreamFree(Handle); | ||
| else if (Type == 1) | ||
| Bass.MusicFree(Handle); | ||
|
|
||
| Handle = 0; | ||
| } | ||
|
|
||
| public void Stop() => Bass.ChannelStop(Handle); | ||
| public void Pause() => Bass.ChannelPause(Handle); | ||
| public void Play() => Bass.ChannelPlay(Handle, false); | ||
|
|
||
| public void SetVolume(float volume) => Bass.ChannelSetAttribute(Handle, ChannelAttribute.Volume, volume); | ||
|
|
||
| public float GetVolume() { | ||
| Bass.ChannelGetAttribute(Handle, ChannelAttribute.Volume, out float volume); | ||
| return volume; | ||
| } | ||
|
|
||
| public void SetPlaybackRate(float rate) => Bass.ChannelSetAttribute(Handle, ChannelAttribute.Frequency, rate * GetSamplingRate()); | ||
|
|
||
| public float GetPlaybackRate() { | ||
| Bass.ChannelGetAttribute(Handle, ChannelAttribute.Frequency, out float frequency); | ||
| if (frequency <= 0.0f) | ||
| return 1.0f; | ||
| return frequency / GetSamplingRate(); | ||
| } | ||
|
|
||
| public void SetPos(in Vector3 position, in Vector3 direction, in Vector3 velocity) { | ||
| Bass.ChannelSet3DPosition(Handle, GMod_Audio.ToBass(position), GMod_Audio.ToBass(direction), GMod_Audio.ToBass(velocity)); | ||
| Bass.Apply3D(); | ||
| } | ||
|
|
||
| public void GetPos(out Vector3 position, out Vector3 direction, out Vector3 velocity) { | ||
| Vector3D pos = new(), dir = new(), vel = new(); | ||
| Bass.ChannelGet3DPosition(Handle, ref pos, ref dir, ref vel); | ||
| position = GMod_Audio.FromBass(pos); | ||
| direction = GMod_Audio.FromBass(dir); | ||
| velocity = GMod_Audio.FromBass(vel); | ||
| } | ||
|
|
||
| public void SetTime(TimeUnit_t time, bool dontDecode) { | ||
| long target = Bass.ChannelSeconds2Bytes(Handle, time); | ||
| long current = Bass.ChannelGetPosition(Handle, PositionFlags.Bytes); | ||
| PositionFlags flags = PositionFlags.Bytes; | ||
| if (current < target && !dontDecode) | ||
| flags = PositionFlags.DecodeTo; | ||
| Bass.ChannelSetPosition(Handle, target, flags); | ||
| } | ||
|
|
||
| public TimeUnit_t GetTime() => Bass.ChannelBytes2Seconds(Handle, Bass.ChannelGetPosition(Handle, PositionFlags.Bytes)); | ||
|
|
||
| public TimeUnit_t GetBufferedTime() { | ||
| if (IsFile) | ||
| return GetLength(); | ||
|
|
||
| long length = Bass.ChannelGetLength(Handle, PositionFlags.Bytes); | ||
| long download = Bass.StreamGetFilePosition(Handle, FileStreamPosition.Download); | ||
| long start = Bass.StreamGetFilePosition(Handle, FileStreamPosition.Start); | ||
| long size = Bass.StreamGetFilePosition(Handle, (FileStreamPosition)8); | ||
| double buffered = (double)(ulong)download / (double)(ulong)(size - start) * (double)(ulong)length; | ||
| return Bass.ChannelBytes2Seconds(Handle, (long)(ulong)buffered); | ||
| } | ||
|
|
||
| public void Set3DFadeDistance(float min, float max) { | ||
| Bass.ChannelSet3DAttributes(Handle, Mode3D.LeaveCurrent, min, max, -1, -1, -1.0f); | ||
| Bass.Apply3D(); | ||
| } | ||
|
|
||
| public void Get3DFadeDistance(out float min, out float max) { | ||
| Mode3D mode = 0; | ||
| int iangle = 0, oangle = 0; | ||
| float outvol = 0; | ||
| min = 0; | ||
| max = 0; | ||
| Bass.ChannelGet3DAttributes(Handle, ref mode, ref min, ref max, ref iangle, ref oangle, ref outvol); | ||
| } | ||
|
|
||
| public void Set3DCone(int innerAngle, int outerAngle, float outerVolume) { | ||
| if (innerAngle < 0 || outerAngle < 0) { | ||
| innerAngle = -1; | ||
| outerAngle = -1; | ||
| } | ||
| Bass.ChannelSet3DAttributes(Handle, Mode3D.LeaveCurrent, -1.0f, -1.0f, innerAngle, outerAngle, outerVolume); | ||
| Bass.Apply3D(); | ||
| } | ||
|
|
||
| public void Get3DCone(out int innerAngle, out int outerAngle, out float outerVolume) { | ||
| Mode3D mode = 0; | ||
| float min = 0, max = 0; | ||
| innerAngle = 0; | ||
| outerAngle = 0; | ||
| outerVolume = 0; | ||
| Bass.ChannelGet3DAttributes(Handle, ref mode, ref min, ref max, ref innerAngle, ref outerAngle, ref outerVolume); | ||
| } | ||
|
|
||
| public int GetState() => Bass.ChannelIsActive(Handle) switch { | ||
| PlaybackState.Playing => 1, | ||
| PlaybackState.Stalled => 3, | ||
| PlaybackState.Paused => 2, | ||
| _ => 0, | ||
| }; | ||
|
|
||
| public void SetLooping(bool looping) => Bass.ChannelFlags(Handle, looping ? BassFlags.Loop : BassFlags.Default, BassFlags.Loop); | ||
| public bool IsLooping() => (Bass.ChannelFlags(Handle, BassFlags.Default, BassFlags.Default) & BassFlags.Loop) != 0; | ||
| public bool IsOnline() => !IsFile; | ||
| public bool Is3D() => (Bass.ChannelFlags(Handle, BassFlags.Default, BassFlags.Default) & BassFlags.Bass3D) != 0; | ||
| public bool IsBlockStreamed() => (Bass.ChannelFlags(Handle, BassFlags.Default, BassFlags.Default) & BassFlags.StreamDownloadBlocks) != 0; | ||
| public bool IsValid() => Handle != 0; | ||
|
|
||
| public double GetLength() => Bass.ChannelBytes2Seconds(Handle, Bass.ChannelGetLength(Handle, PositionFlags.Bytes)); | ||
|
|
||
| public ReadOnlySpan<char> GetFileName() { | ||
| if (FileName != null) | ||
| return FileName; | ||
| Bass.ChannelGetInfo(Handle, out ChannelInfo info); | ||
| ChannelInfoRaw raw = Unsafe.As<ChannelInfo, ChannelInfoRaw>(ref info); | ||
| if (raw.FileName == 0) | ||
| return ""; | ||
| return ((raw.Flags & BassFlags.Unicode) != 0 ? Marshal.PtrToStringUni(raw.FileName) : Marshal.PtrToStringAnsi(raw.FileName)) ?? ""; | ||
| } | ||
|
|
||
| [StructLayout(LayoutKind.Sequential)] | ||
| struct ChannelInfoRaw | ||
| { | ||
| public int Frequency; | ||
| public int Channels; | ||
| public BassFlags Flags; | ||
| public ChannelType ChannelType; | ||
| public int OriginalResolution; | ||
| public int Plugin; | ||
| public int Sample; | ||
| public nint FileName; | ||
| } | ||
|
|
||
| public int GetSamplingRate() { | ||
| Bass.ChannelGetInfo(Handle, out ChannelInfo info); | ||
| return info.Frequency; | ||
| } | ||
|
|
||
| public int GetBitsPerSample() { | ||
| Bass.ChannelGetInfo(Handle, out ChannelInfo info); | ||
| return info.OriginalResolution; | ||
| } | ||
|
|
||
| public float GetAverageBitRate() { | ||
| Bass.ChannelGetAttribute(Handle, ChannelAttribute.Bitrate, out float bitrate); | ||
| return bitrate; | ||
| } | ||
|
|
||
| public void GetLevel(out float left, out float right) { | ||
| int level = Bass.ChannelIsActive(Handle) == PlaybackState.Playing ? Bass.ChannelGetLevel(Handle) : 0; | ||
| left = ((uint)level >> 16) * 3.0517578e-05f; | ||
| right = (level & 0xffff) * 3.0517578e-05f; | ||
| } | ||
|
|
||
| public int FFT(float[] data, GModChannelFFT size) { | ||
| if (Bass.ChannelIsActive(Handle) != PlaybackState.Playing) | ||
| return 0; | ||
|
|
||
| (int count, DataFlags flags) = size switch { | ||
| GModChannelFFT.FFT_256 => (128, DataFlags.FFT256), | ||
| GModChannelFFT.FFT_512 => (256, DataFlags.FFT512), | ||
| GModChannelFFT.FFT_2048 => (1024, DataFlags.FFT2048), | ||
| GModChannelFFT.FFT_4096 => (2048, DataFlags.FFT4096), | ||
| GModChannelFFT.FFT_8192 => (4096, DataFlags.FFT8192), | ||
| GModChannelFFT.FFT_16384 => (8192, DataFlags.FFT16384), | ||
| GModChannelFFT.FFT_32768 => (16384, DataFlags.FFT32768), | ||
| _ => (512, DataFlags.FFT1024), | ||
| }; | ||
| Bass.ChannelGetData(Handle, data, (int)(flags | DataFlags.Float | DataFlags.FFTRemoveDC)); | ||
| return count; | ||
| } | ||
|
|
||
| public void SetChannelPan(float pan) => Bass.ChannelSetAttribute(Handle, ChannelAttribute.Pan, pan); | ||
|
|
||
| public float GetChannelPan() { | ||
| Bass.ChannelGetAttribute(Handle, ChannelAttribute.Pan, out float pan); | ||
| return pan; | ||
| } | ||
|
|
||
| public nint GetTags(int type) => Bass.ChannelGetTags(Handle, (TagType)type); | ||
|
|
||
| public void Set3DEnabled(bool enabled) { | ||
| if (!Is3D()) | ||
| return; | ||
| Bass.ChannelSet3DAttributes(Handle, enabled ? Mode3D.Normal : Mode3D.Off, -1.0f, -1.0f, -1, -1, -1.0f); | ||
| Bass.Apply3D(); | ||
| } | ||
|
|
||
| public bool Get3DEnabled() { | ||
| if (!Is3D()) | ||
| return false; | ||
| Mode3D mode = 0; | ||
| float min = 0, max = 0, outvol = 0; | ||
| int iangle = 0, oangle = 0; | ||
| Bass.ChannelGet3DAttributes(Handle, ref mode, ref min, ref max, ref iangle, ref oangle, ref outvol); | ||
| return mode != Mode3D.Off; | ||
| } | ||
|
|
||
| public void Restart() => Bass.ChannelPlay(Handle, true); | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this get called through a garbage collector method in glua? Haven't checked but I want to make sure just in case