Skip to content
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

Add Music functionality #598

Merged
merged 37 commits into from
May 7, 2017
Merged
Show file tree
Hide file tree
Changes from 28 commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
9626a95
Create config.cpp
fishykins Feb 22, 2017
c1967a7
Create CfgMusic.hpp
fishykins Feb 22, 2017
0bd367b
Create CfgFunctions.hpp
fishykins Feb 22, 2017
410492c
Create script_component.hpp
fishykins Feb 22, 2017
9ab739b
Create fnc_compileMusic.sqf
fishykins Feb 22, 2017
a67a6e0
Create fnc_getMusicData.sqf
fishykins Feb 22, 2017
c35da7d
Create fnc_getMusicPath.sqf
fishykins Feb 22, 2017
3819c2b
Create fnc_getMusicPlaying
fishykins Feb 22, 2017
06d55d2
Rename fnc_getMusicPlaying to fnc_getMusicPlaying.sqf
fishykins Feb 22, 2017
097c336
Create fnc_playMusic.sqf
fishykins Feb 22, 2017
25c8fef
Create fnc_searchMusic.sqf
fishykins Feb 22, 2017
aaaeed6
Create fnc_stopMusic.sqf
fishykins Feb 22, 2017
7aa2ddb
Update script_component.hpp
fishykins Feb 22, 2017
e3b5447
Update fnc_compileMusic.sqf
fishykins Feb 22, 2017
3957e01
Update config.cpp
fishykins Feb 23, 2017
3c2b703
Update CfgFunctions.hpp
fishykins Feb 23, 2017
5ae06cd
Update fnc_getMusicData.sqf
fishykins Feb 23, 2017
e66ad53
Update fnc_getMusicPath.sqf
fishykins Feb 23, 2017
fb596b6
Update fnc_getMusicPlaying.sqf
fishykins Feb 23, 2017
9615d2b
Update fnc_playMusic.sqf
fishykins Feb 23, 2017
924d38c
Update fnc_searchMusic.sqf
fishykins Feb 23, 2017
6258a15
Update fnc_stopMusic.sqf
fishykins Feb 23, 2017
e22e983
Update CfgFunctions.hpp
fishykins Feb 23, 2017
befaa7d
Update fnc_searchMusic.sqf
fishykins Feb 23, 2017
45bded6
Update CfgMusic.hpp
fishykins Feb 23, 2017
4bcec6c
Update fnc_searchMusic.sqf
fishykins Mar 5, 2017
f527625
Updated to camelCase
fishykins Mar 5, 2017
75b2f06
Lots of formatting and edits
fishykins Mar 8, 2017
cf6bc98
Create fnc_findMusic.sqf
fishykins Mar 9, 2017
f0f5f71
More fixes for the approval of the mob
fishykins Mar 9, 2017
d437beb
"forEach" not working with configs for some unknown reason...
fishykins Mar 9, 2017
c4c2add
Tweaked as per comments
fishykins Mar 9, 2017
2f42698
Fixed tabs in CfgMusic
fishykins Mar 9, 2017
d87b29f
Fixed some typos
fishykins Mar 20, 2017
29c0d88
support for APEX music+ minor improvements
fishykins Apr 10, 2017
309d43d
Improved getMusicData, tidied up headers
fishykins Apr 10, 2017
de10876
tabs fix
fishykins Apr 10, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions addons/music/CfgFunctions.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
class CfgFunctions
{
class CBA
{
class Music
{
// CBA_fnc_getMusicPath
class getMusicPath
{
description = "Get the config path of given music class";
file = "\x\cba\addons\music\fnc_getMusicPath.sqf";
};
// CBA_fnc_getMusicData
class getMusicData
{
description = "Get data attached to music class";
file = "\x\cba\addons\music\fnc_getMusicData.sqf";
};
// CBA_fnc_findMusic
class findMusic
{
description = "Find music of spesified type";
Copy link
Contributor

@Killswitch00 Killswitch00 Mar 18, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"specific" (or "specified")

file = "\x\cba\addons\music\fnc_findMusic.sqf";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file is missing. Add the file, or remove the class.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added

};
// CBA_fnc_compileMusic
class compileMusic
{
description = "Find all music in game";
file = "\x\cba\addons\music\fnc_compileMusic.sqf";
};
// CBA_fnc_getMusicPlaying
class getMusicPlaying
{
description = "Returns the data on currently playing music";
file = "\x\cba\addons\music\fnc_getMusicPlaying.sqf";
};
// CBA_fnc_playMusic
class playMusic
{
description = "Play a given track";
file = "\x\cba\addons\music\fnc_playMusic.sqf";
};
// CBA_fnc_stopMusic
class stopMusic
{
description = "Stops the music playing";
file = "\x\cba\addons\music\fnc_stopMusic.sqf";
};
};
};
};
3 changes: 3 additions & 0 deletions addons/music/CfgMusic.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class CfgMusic
{
};
17 changes: 17 additions & 0 deletions addons/music/config.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include "script_component.hpp"

class CfgPatches {
class ADDON {
author = "$STR_CBA_Author";
name = CSTRING(component);
url = "$STR_CBA_URL";
units[] = {};
requiredVersion = REQUIRED_VERSION;
requiredAddons[] = {"CBA_common"};
version = VERSION;
authors[] = {"Fishy"};
};
};

#include "CfgFunctions.hpp"
#include "CfgMusic.hpp"
54 changes: 54 additions & 0 deletions addons/music/fnc_compileMusic.sqf
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/* ----------------------------------------------------------------------------
Function: CBA_fnc_compileMusic

Description:
A function used to gather a list of all usable music.

Parameters:
none

Returns:
Array of compiled music

Example:
(begin example)
_allMusic = [] call CBA_fnc_compileMusic
(end example)

Author:
Fishy
---------------------------------------------------------------------------- */
#include "script_component.hpp"

if (isNil QGVARMAIN(compiledMusic)) then {
private "_config";
_config = configFile >> 'CfgMusic';
private "_missionConfig";
_missionConfig = missionConfigFile >> 'CfgMusic';
private "_unsortedSongs";
_unsortedSongs = [];

for [{_i=0}, {_i < (count _config)}, {INC(_i)}] do {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you are only using _i to select from _missionConfig. So a forEach should work here as well shouldn't it?
Also please don't private "_var";_var = 1; do private _var = 1; instead

private "_song";
_song = _config select _i;
private "_songDuration";
_songDuration = getNumber (_song >> 'duration');
if (_songDuration > 0) then {
_unsortedSongs pushBack (configName _song);
};
};

for [{_i=0}, {_i < (count _missionConfig)}, {INC(_i)}] do {
Copy link
Contributor

@Dorbedo Dorbedo Mar 9, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

filtering via configProperties could be suitable too
e.g.:

private _allMusic = configProperties [MissionConfigFile >> "CfgMusic", "getNumber(_x>>'duration')>0",true];
_allMusic append configProperties [configFile >> "CfgMusic", "getNumber(_x>>'duration')>0",true];
private _unsortedSongs = [];
{_unsortedSongs pushBackUnique _x;} count _allMusic;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's better by a mile

private "_song";
_song = _missionConfig select _i;
private "_songDuration";
_songDuration = getNumber (_song >> 'duration');
if (_songDuration > 0) then {
_unsortedSongs pushBack (configName _song);
};
};

GVARMAIN(compiledMusic) = _unsortedSongs;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this value can contain double entries. (same name inside missionconfig and config)
I think, this should not be the case.

};

GVARMAIN(compiledMusic)
38 changes: 38 additions & 0 deletions addons/music/fnc_getMusicData.sqf
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/* ----------------------------------------------------------------------------
Function: CBA_fnc_getMusicData

Description:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Header is missing some line-breaks see this as example

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In general this file could use some more line-breaks

A function used to return data from the given music class

Parameters:
one string OR one config path, and a data type (eg 'name')

Returns:
data entry for requested music class

Example:
(begin example)
_musicName = ["LeadTrack01_F_Bootcamp","Duration"] call CBA_fnc_getMusicData;
(end example)

Author:
Fishy
---------------------------------------------------------------------------- */
#include "script_component.hpp"

params [["_className",""],["_dataType","name"]];
private "_config";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can change this to private _config = _classname and then remove the else from the if statement. If _className is not a config the IS_CONFIG check from line 35 will fail.
That way you can theoretically also omit the isNil check in Line 35. But I'm not sure if one could just pass Nil as _className.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think Nil would get past IS_CONFIG so I'll leave it be.


if (IS_STRING(_classname)) then {
//Is a string, get config
_config = [_className] call CBA_fnc_getMusicPath;
} else {
if (IS_CONFIG(_className)) then {
_config = _className;
};
};

if (!IS_CONFIG(_config)) exitWith {ERROR_1("Config not found",_className);};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_config can be nil if CBA_fnc_getMusicPath did not find a path

If ((isNil "_config")||{!IS_CONFIG(_config)}) exitWith


//Now we have a config, grab the data
[_config,_dataType,nil] call BIS_fnc_returnConfigEntry
36 changes: 36 additions & 0 deletions addons/music/fnc_getMusicPath.sqf
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/* ----------------------------------------------------------------------------
Function: CBA_fnc_getMusicPath

Description:
A function used to return the config path of a music file

Parameters:
STRING- name of track

Returns:
config entry of given track

Example:
(begin example)
_configPath = ["LeadTrack01_F_Bootcamp"] call CBA_fnc_getMusicPath
(end example)

Authors:
Fishy, Dorbedo
---------------------------------------------------------------------------- */
#include "script_component.hpp"

if (!params [["_className","",[""]]]) exitWith {WARNING('No classname was provided');};

private "_config";
_config = configFile >> 'CfgMusic' >> _className;
private "_duration";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This use of private is even worse than private ARRAY :D

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll get it right eventually ;)

_duration = getNumber (_config >> "duration");

if (_duration == 0) then {
_config = missionConfigFile >> "CfgMusic" >> _className;
_duration = getNumber (_config >> "name");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(_config >> "duration")

if (_duration == 0) exitWith {WARNING_1("No path found for class",_className);nil};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the nil has no purpose

};

_config
44 changes: 44 additions & 0 deletions addons/music/fnc_getMusicPlaying.sqf
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/* ----------------------------------------------------------------------------
Function: CBA_fnc_getMusicPlaying

Description:
A function used to return the current time on playing music. Must have been started with CBA_fnc_playMusic

Parameters:
BOOL: if true, return bool (default: false)

Returns:
array [class, music time, time left] OR bool (true for playing music)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd personally prefer if there was CBA_fnc_getMusicPlaying reporting the array and CBA_fnc_isMusicPlaying always returning the boolean.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes that makes more sense


Example:
(begin example)
_musicTime = [] call CBA_fnc_getMusicPlaying;
(end example)

Author:
Fishy
---------------------------------------------------------------------------- */
#include "script_component.hpp"

if (!params [["_bool",false,[false]]]) exitWith {WARNING('Incorrect param');};

if (isNil QGVAR(track)) exitWith {
if (_bool) then {false} else {['', 0, 0, 0]}
};

if (_bool) exitWith {true};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be done without _return
e.g.:

if (isNil QGVAR(track)) exitWith {
    if (_bool) then {
        false;
    }else{
        ['', 0, 0, 0]
    };
};
if (_bool) exitWith {true};

and then without the return in L36/L37


GVAR(track) params ["_class","_startTime","_playPos","_duration"];
private "_trackTime";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again that weird private thingy. I won't mention it anymore for the rest of the files.

_trackTime = (CBA_missionTime - _startTime) + _playPos;
private "_remainingTime";
_remainingTime = _duration - _trackTime;
if (_remainingTime <=0) then {
_remainingTime = 0;
_trackTime = 0;
_class = "";
GVAR(track) = nil;
};

[_class, _trackTime, _remainingTime]

53 changes: 53 additions & 0 deletions addons/music/fnc_playMusic.sqf
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/* ----------------------------------------------------------------------------
Function: CBA_fnc_playMusic

Description:
A function used to play music.

Parameters:
1: CLASS Class to play
2: INT Position to play from (default: 0)
3: BOOL allowed to interupt already playing music (default: true)

Returns:
BOOL -true if started playing music

Example:
(begin example)
_bool = ["LeadTrack01_F_Bootcamp", 30, true] call CBA_fnc_playMusic;
(end example)


Author:
Fishy
---------------------------------------------------------------------------- */
#include "script_component.hpp"

params [["_className","",[""]],["_time",0,[0]],["_overWrite", true,[true]]];

if (_className isEqualTo "") exitWith {
WARNING("No class given");
false
};

If ((!_overWrite)&&{[true] call CBA_fnc_getMusicPlaying}) exitWith {false};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think !_overWrite needs parentheses but doesn't matter probably.
A few more spaces to both sides of the && would make this a little more readable


private "_return";
_return = false;

private "_duration";
_duration = [_className,"duration"] call CBA_fnc_getMusicData;

if (!isNil "_duration") then {
if (_time < _duration) then {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you could make this an exitWith instead of a then. That way you could completly remove the _return variable and just return false in line 51.
But readability may suffer if you do this. not sure.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style question. Both methods have their downsides. This is fine too.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or just use try {} catch {}; :D

playMusic [_className, _time];
GVAR(track) = [_className,CBA_missionTime,_time,_duration];
_return = true;
} else {
WARNING("Time is greater than song length");
};
} else {
WARNING("Music not found");
};

_return
27 changes: 27 additions & 0 deletions addons/music/fnc_stopMusic.sqf
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/* ----------------------------------------------------------------------------
Function: CBA_fnc_stopMusic

Description:
A function used to stop any music playing. Must have been started with CBA_fnc_playMusic, otherwise it is ignored.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the header needs an update due to changed behavior.


Parameters:
none

Returns:
BOOL: true if music was stopped, false if already stopped
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there is no need to return anymore

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This return value is useless as it doesn't return false anymore.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Functions that "do stuff" as opposed to setter functions report usually nothing / nil in SQF, but true is ok too.


Example:
(begin example)
_bool = call CBA_fnc_stopMusic;
(end example)

Author:
Fishy
---------------------------------------------------------------------------- */
#include "script_component.hpp"

if (!([true] call CBA_fnc_getMusicPlaying)) exitWith {false};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you could leave out this line.
It would result in the benefit, that this function stops music, which was not called via cba, too.


playMusic ["",0];
GVAR(track) = nil;
true
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

true can be left out

Copy link
Contributor

@commy2 commy2 Mar 9, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Disagree. No function should end with an assignment operator. At least return nil. Returning the assignment operator has a bug in SQF.


_a = call {
    _b = 1;
};

>generic error in expression

_a = call {
    _b = 1;
    nil
};

>no error, _a is undefined.

7 changes: 7 additions & 0 deletions addons/music/script_component.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#define COMPONENT music
#include "\x\cba\addons\main\script_mod.hpp"


#define DEBUG_MODE_FULL
#define DEFAULT_SONG_TYPE "song"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this macro isn't used

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's used once in fnc_findMusic.sqf which I missed out when I uploaded!

#include "\x\cba\addons\main\script_macros.hpp"