Skip to content

Commit 86de47f

Browse files
committed
bass: try a mix of a previous unfucked version..
1 parent 5cfa9c3 commit 86de47f

File tree

6 files changed

+179
-61
lines changed

6 files changed

+179
-61
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3327,6 +3327,11 @@ Valid encoders are:<br>
33273327
> This function requires the `BASSENC` plugin to work at all!<br>
33283328
> You can find all the plugins at https://www.un4seen.com/ drop them into the `bin/` folder besides `libbass.so`<br>
33293329
3330+
> [!WARNING]
3331+
> This function is a fucking gamble, I do not know why BASS decides this, though it behaves sometimes randomly.<br>
3332+
> the current code for it seems to be somewhat stable, though any change like shifting it, adding arguments will cause issues<br>
3333+
> until this is figured out this won't be changed in any way further (This could all point back to BASSENC itself having a bug specific to my usecase)<br>
3334+
33303335
#### IGModAudioChannel:Update(number time)
33313336
Updates the channel for the given time in seconds<br>
33323337
See: https://www.un4seen.com/doc/#bass/BASS_ChannelUpdate.html<br>

source/modules/bass.cpp

Lines changed: 64 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -334,58 +334,85 @@ LUA_FUNCTION_STATIC(IGModAudioChannel_Restart)
334334
return 0;
335335
}
336336

337-
class BassEncoderCallback : public IGModEncoderCallback
337+
338+
339+
class BassEncoderCallback : public IGModEncoderCallback, public GarrysMod::Lua::ILuaThreadedCall
338340
{
339341
public:
340-
BassEncoderCallback(GarrysMod::Lua::ILuaInterface* pLua)
342+
BassEncoderCallback(int nReference)
341343
{
342-
m_pLua = pLua;
343-
m_nCallbackReference = Util::ReferenceCreate(pLua, "BassEncoderCallback - callback reference");
344+
m_nCallbackReference = nReference;
344345
}
345346

346347
virtual ~BassEncoderCallback() {
347-
if (m_pLua && m_nCallbackReference != -1)
348+
if (m_nCallbackReference != -1)
348349
{
349-
Util::ReferenceFree(m_pLua, m_nCallbackReference, "BassEncoderCallback - callback deletion");
350+
Msg("BassEncoderCallback deleted while still holding a reference!\n");
350351
m_nCallbackReference = -1;
351-
m_pLua = NULL;
352352
}
353353
};
354354

355-
virtual bool ShouldFinish(IGModAudioChannelEncoder* pEncoder, void* nSignalData)
355+
virtual bool ShouldForceFinish(IGModAudioChannelEncoder* pEncoder, void* nSignalData)
356356
{
357-
if (m_pLua == nSignalData)
358-
return true; // Force finish as this is our signal that our interface is shutting down!
357+
//if (m_pLua == nSignalData)
358+
// return true; // Force finish as this is our signal that our interface is shutting down!
359359

360360
return false;
361361
};
362362

363363
virtual void OnFinish(IGModAudioChannelEncoder* pEncoder, GModEncoderStatus nStatus)
364+
{
365+
m_bIsBassDone = true;
366+
367+
if (m_bForceDelete)
368+
delete this; // We serve no purpose... Lua is gone :NOOOOO:
369+
};
370+
371+
bool IsDone()
372+
{
373+
return m_bIsBassDone;
374+
}
375+
376+
void Done(GarrysMod::Lua::ILuaInterface* LUA)
364377
{
365378
if (m_nCallbackReference == -1)
366379
return;
367380

368-
Util::ReferencePush(m_pLua, m_nCallbackReference);
369-
if (nStatus == GModEncoderStatus::FINISHED)
370-
{
371-
m_pLua->PushBool(true);
372-
m_pLua->PushNil();
381+
Util::ReferencePush(LUA, m_nCallbackReference);
382+
if (m_nBassStatus == GModEncoderStatus::FINISHED) {
383+
LUA->PushBool(true);
384+
LUA->PushNil();
373385
} else {
374-
m_pLua->PushBool(false);
375-
m_pLua->PushString("Encoder was interrupted by Lua shutdown!");
386+
LUA->PushBool(false);
387+
LUA->PushString("Encoder was interrupted by Lua shutdown!");
376388
}
377-
m_pLua->CallFunctionProtected(2, 0, true);
389+
LUA->CallFunctionProtected(2, 0, true);
378390

379-
Util::ReferenceFree(m_pLua, m_nCallbackReference, "BassEncoderCallback - callback deletion OnFinish");
391+
Util::ReferenceFree(LUA, m_nCallbackReference, "BassEncoderCallback - callback deletion OnFinish");
380392
m_nCallbackReference = -1;
381-
m_pLua = NULL;
382-
};
393+
}
394+
395+
void OnShutdown()
396+
{
397+
m_nCallbackReference = -1;
398+
if (!m_bIsBassDone)
399+
{
400+
// Bass still uses us, so we gotta delay this
401+
m_bForceDelete = true;
402+
return;
403+
}
404+
405+
delete this;
406+
}
383407

384408
private:
385-
GarrysMod::Lua::ILuaInterface* m_pLua = nullptr;
409+
bool m_bIsBassDone = false;
410+
bool m_bForceDelete = false;
386411
int m_nCallbackReference = -1;
412+
int m_nBassStatus = GModEncoderStatus::DIED;
387413
};
388414

415+
#if ENABLE_UTTERLY_BROKEN_ENCODER_SHIT // https://github.com/RaphaelIT7/gmod-holylib/commit/48bc854f48aa26ec6539eabd01b66d87110b4598#diff-82711262b6e5109632243c62ed0ab8cba506cdf021817af4af807da268297bce
389416
LUA_FUNCTION_STATIC(IGModAudioChannel_EncodeToDisk)
390417
{
391418
IGModAudioChannel* channel = Get_IGModAudioChannel(LUA, 1, true);
@@ -419,6 +446,7 @@ LUA_FUNCTION_STATIC(IGModAudioChannel_EncodeToDisk)
419446
LUA->PushNil();
420447
return 2;
421448
}
449+
#endif
422450

423451
LUA_FUNCTION_STATIC(IGModAudioChannel_Update)
424452
{
@@ -460,23 +488,31 @@ LUA_FUNCTION_STATIC(IGModAudioChannel_DestroyLink)
460488
return 2;
461489
}
462490

463-
LUA_FUNCTION_STATIC(IGModAudioChannel_EncodeToDisk2)
491+
LUA_FUNCTION_STATIC(IGModAudioChannel_EncodeToDisk)
464492
{
465493
IGModAudioChannel* channel = Get_IGModAudioChannel(LUA, 1, true);
466494

467495
const char* pFileName = LUA->CheckString(2);
468496
unsigned long nFlags = (unsigned long)LUA->CheckString(3);
469-
LUA->CheckType(4, GarrysMod::Lua::Type::Function);
470497

471-
const char* pErrorMsg;
472-
channel->EncodeToDisk2(pFileName, nFlags, NULL, &pErrorMsg);
498+
/*BassEncoderCallback* pCallback = nullptr;
499+
if (LUA->IsType(4, GarrysMod::Lua::Type::Function))
500+
{
501+
LUA->Push(4);
502+
pCallback = new BassEncoderCallback(Util::ReferenceCreate(LUA, "BassEncoderCallback - callback reference"));
503+
}*/
504+
505+
const char* pErrorMsg = channel->EncodeToDisk(pFileName, nFlags/*, pCallback*/);
473506
if (!pErrorMsg)
474507
{ // Success
475508
LUA->PushBool(true);
476509
LUA->PushNil();
477510
return 2;
478511
}
479512

513+
// if (pCallback)
514+
// LUA->AddThreadedCall(pCallback);
515+
480516
LUA->PushBool(false);
481517
LUA->PushString(pErrorMsg);
482518
return 2;
@@ -642,7 +678,6 @@ void CBassModule::LuaInit(GarrysMod::Lua::ILuaInterface* pLua, bool bServerInit)
642678

643679
// HolyLib specific
644680
Util::AddFunc(pLua, IGModAudioChannel_EncodeToDisk, "EncodeToDisk");
645-
Util::AddFunc(pLua, IGModAudioChannel_EncodeToDisk2, "EncodeToDisk2");
646681
Util::AddFunc(pLua, IGModAudioChannel_Update, "Update");
647682
Util::AddFunc(pLua, IGModAudioChannel_CreateLink, "CreateLink");
648683
Util::AddFunc(pLua, IGModAudioChannel_DestroyLink, "DestroyLink");
@@ -661,7 +696,9 @@ void CBassModule::LuaShutdown(GarrysMod::Lua::ILuaInterface* pLua)
661696
{
662697
// Finish all callbacks
663698
// We pass pLua so that our BassEncoderCallback can check if its their state and force a finish
699+
#if ENABLE_UTTERLY_BROKEN_ENCODER_SHIT
664700
gGModAudio->FinishAllAsync(pLua);
701+
#endif
665702

666703
Util::NukeTable(pLua, "bass");
667704
}

source/plugin.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ void CServerPlugin::OnEdictFreed(const edict_t *edict)
356356
g_pModuleManager.OnEdictFreed(edict);
357357
}
358358

359-
class HolyLib_PluginThink : GarrysMod::Lua::ILuaThreadedCall
359+
class HolyLib_PluginThink : public GarrysMod::Lua::ILuaThreadedCall
360360
{
361361
public:
362362
void SetLua(GarrysMod::Lua::ILuaInterface* pLua)
@@ -432,7 +432,7 @@ GMOD_MODULE_OPEN()
432432
pPluginThink.SetLua(LUA);
433433

434434
// Add our Think hook.
435-
LUA->AddThreadedCall((GarrysMod::Lua::ILuaThreadedCall*)&pPluginThink);
435+
LUA->AddThreadedCall(&pPluginThink);
436436

437437
return 0;
438438
}

source/sourcesdk/IGmod_Audio.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ enum GModEncoderStatus {
2323
DIED = 3,
2424
};
2525

26+
#if ENABLE_UTTERLY_BROKEN_ENCODER_SHIT
2627
// HolyLib specific
2728
// NOTE: Always call GetLastError after any function call to check for errors!
2829
class IGModAudioChannelEncoder
@@ -39,13 +40,14 @@ class IGModAudioChannelEncoder
3940
// Wasn't exposed since CreateEncoder already calls it so it has no real use
4041
// virtual void InitEncoder(unsigned long nEncoderFlags) = 0;
4142
};
43+
#endif
4244

4345
class IGModAudioChannelEncoder;
4446
class IGModEncoderCallback // Callback struct
4547
{
4648
public:
4749
virtual ~IGModEncoderCallback() {};
48-
virtual bool ShouldFinish(IGModAudioChannelEncoder* pEncoder, void* nSignalData) = 0;
50+
virtual bool ShouldForceFinish(IGModAudioChannelEncoder* pEncoder, void* nSignalData) = 0;
4951
virtual void OnFinish(IGModAudioChannelEncoder* pEncoder, GModEncoderStatus nStatus) = 0;
5052
};
5153

@@ -93,8 +95,10 @@ class IGModAudioChannel
9395
// Uses the "DATA" path for writes! Returns NULL on success, else the error message
9496
// Does NOT require the channel to be a decoder channel!
9597
// Call IGModAudioChannelEncoder->GetLastError and check if its even valid! (Else it will be invalidated/freed on the next tick)
96-
virtual const char* EncodeToDisk2( const char* pFileName, unsigned long nFlags, IGModEncoderCallback* pCallback, const char** pErrorOut ) = 0;
98+
virtual const char* EncodeToDisk( const char* pFileName, unsigned long nFlags ) = 0;
99+
#if ENABLE_UTTERLY_BROKEN_ENCODER_SHIT
97100
virtual IGModAudioChannelEncoder* CreateEncoder( const char* pFileName, unsigned long nFlags, IGModEncoderCallback* pCallback, const char** pErrorOut ) = 0;
101+
#endif
98102
virtual void Update( unsigned long length ) = 0; // Updates the playback buffer
99103
virtual bool CreateLink( IGModAudioChannel* pChannel, const char** pErrorOut ) = 0;
100104
virtual bool DestroyLink( IGModAudioChannel* pChannel, const char** pErrorOut ) = 0;
@@ -139,7 +143,9 @@ abstract_class IGMod_Audio
139143
// HolyLib specific ones
140144
virtual unsigned long GetVersion() = 0; // Returns bass version
141145
virtual bool LoadPlugin(const char* pluginName, const char** pErrorOut) = 0;
146+
#if ENABLE_UTTERLY_BROKEN_ENCODER_SHIT
142147
virtual void FinishAllAsync(void* nSignalData) = 0; // Called on Lua shutdown to finish all callbacks/async tasks for that interface
148+
#endif
143149
};
144150

145151
#undef CALLBACK // Solves another error with minwindef.h

0 commit comments

Comments
 (0)