Skip to content

Commit

Permalink
Fix merge regressions
Browse files Browse the repository at this point in the history
  • Loading branch information
FlorianGlawogger committed Jan 15, 2025
1 parent 1205fb2 commit a67a6fb
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ public class ParameterLoader : MonoBehaviour
/// </summary>
public UnityEvent<ExperimentParameters> parametersLoaded = new UnityEvent<ExperimentParameters>();

/// <summary>
/// Invoked when ExperimentParameters have been loaded, which are custom (e.g. received from Javascript WebGL).
/// </summary>
public UnityEvent CustomParametersLoaded = new UnityEvent();

/// <summary>
/// Invoked when the JSON files have been initialized.
/// </summary>
Expand All @@ -48,6 +53,11 @@ public ExperimentParameters MostRecentParameters
private set;
}

/// <summary>
/// Used to know whether there was already an experiment which potentially used the URL fragment parameters on WebGL already
/// </summary>
private static bool firstExperimentDefaultParametersLoaded = false;

#region Singleton
private static ParameterLoader _instance;
public static ParameterLoader Instance
Expand All @@ -70,9 +80,10 @@ private void Start()
// Listener for external json data (sent e.g. via a Javascript button from a website where Maroon is embedded)
WebGlReceiver.Instance.OnIncomingData.AddListener((string jsonData) => {
LoadJsonFromString(jsonData);
CustomParametersLoaded?.Invoke();
});
#endif

if (_automaticiallyDetectJsonFiles)
{
#if UNITY_WEBGL && !UNITY_EDITOR
Expand Down Expand Up @@ -142,8 +153,22 @@ public void InitJsonFiles(List<TextAsset> jsonFiles)
/// </summary>
/// <param name="file">File to load</param>
/// <returns>The loaded ExperimentParameters</returns>
public ExperimentParameters LoadJsonFromFileIndex(int index)
public ExperimentParameters LoadJsonFromFileIndex(int index, bool firstDefaultParametersLoad = false)
{
#if UNITY_WEBGL
/*
* Initial config received from Javascript (originating from the URL Fragment config) will be received before the requested experiment is loaded,
* thus if on WebGL and the WebGlReceiver.Instance.MostRecentData is not null and this it the first experiment, load instead the WebGlReceiver.Instance.MostRecentData instead of the requested default file.
*/
if (firstDefaultParametersLoad && !firstExperimentDefaultParametersLoaded && !string.IsNullOrWhiteSpace(WebGlReceiver.Instance.MostRecentData))
{
firstExperimentDefaultParametersLoaded = true;
CustomParametersLoaded?.Invoke();
return LoadJsonFromString(WebGlReceiver.Instance.MostRecentData);
}
#endif


if (index >= _jsonFile.Count)
{
Debug.LogError("Index " + index + " is greater or equal the number of files " + _jsonFile.Count);
Expand All @@ -160,7 +185,7 @@ public ExperimentParameters LoadJsonFromFileIndex(int index)
/// </summary>
/// <param name="name">Name of the file to load</param>
/// <returns>The loaded ExperimentParameters</returns>
public ExperimentParameters LoadJsonFromFileName(string name)
public ExperimentParameters LoadJsonFromFileName(string name, bool firstDefaultParametersLoad = false)
{
int index = IndexOfJson(name);
if (index == -1)
Expand All @@ -169,7 +194,7 @@ public ExperimentParameters LoadJsonFromFileName(string name)
return null;
}

return LoadJsonFromFileIndex(index);
return LoadJsonFromFileIndex(index, firstDefaultParametersLoad);
}

/// <summary>
Expand All @@ -179,7 +204,16 @@ public ExperimentParameters LoadJsonFromFileName(string name)
/// <returns>The loaded ExperimentParameters</returns>
public ExperimentParameters LoadJsonFromString(string data)
{
Debug.Log("Trying to load ExperimentParameters from JSON String.");
MostRecentParameters = ConvertJsonToExperimentParameters(data);
if (MostRecentParameters == null)
{
Debug.LogError("Loaded ExperimentParameters are null.");
}
else
{
Debug.Log("Successfully parsed ExperimentParameters: " + MostRecentParameters.GetType());
}
parametersLoaded?.Invoke(MostRecentParameters);
return MostRecentParameters;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,22 +126,22 @@ public void OnFilesLoadedInital()
#if UNITY_WEBGL && !UNITY_EDITOR
if (BootstrappingManager.Instance.UrlParameters.TryGetValue(WebGlUrlParameter.Config, out string config))
{
if (!ApplyConfig(config)) ApplyConfig("Default");
if (!ApplyConfig(config)) ApplyConfig("Default", true);
}
else
#endif
{
ApplyConfig("Default");
ApplyConfig("Default", true);
}

ParameterLoader.Instance.OnFilesInitialized.RemoveListener(OnFilesLoadedInital);
}

public bool ApplyConfig(string configName)
public bool ApplyConfig(string configName, bool firstDefaultParametersLoad = false)
{
_currentConfigIndex = ParameterLoader.Instance.IndexOfJson(configName);
dropdown.SetValueWithoutNotify(_currentConfigIndex);
var parameters = ParameterLoader.Instance.LoadJsonFromFileName(configName);
var parameters = ParameterLoader.Instance.LoadJsonFromFileName(configName, firstDefaultParametersLoad);

return parameters != null;
}
Expand Down

0 comments on commit a67a6fb

Please sign in to comment.