private void LoadColorPresets(string colorPresetFolderPath) { if (!Directory.Exists(colorPresetFolderPath)) { Directory.CreateDirectory(colorPresetFolderPath); } string[] colorPresetFilePaths = Directory.GetFiles(colorPresetFolderPath); foreach (string colorPresetFilePath in colorPresetFilePaths) { string name = Path.GetFileName(colorPresetFilePath); if (!name.EndsWith(".json")) { continue; } name = name.Substring(0, name.Length - 5); string json = File.ReadAllText(colorPresetFilePath); JSONNode rootJSON; try { rootJSON = JSON.Parse(json); } catch (Exception e) { Logger.log.Error("Failed loading color preset: " + name); Logger.log.Error("\t" + e.Message); continue; } ColorPreset colorPreset; try { colorPreset = ColorPreset.LoadColorPresetFromJSON(rootJSON); } catch (InvalidJSONException e) { Logger.log.Error("Failed loading color preset: " + name); Logger.log.Error("\tInvalid JSON data: " + e.errorMessage); continue; } ColorPreset.SetColorPreset(name, colorPreset); } Logger.log.Info(ColorPreset.GetColorPresetCount() + " color presets loaded from " + colorPresetFolderPath); }
public static CustomLightshowData LoadLightshowDataFromJSON(JSONNode rootJSON, string name) { //just use the SimpleJSON library available in ChatCore if (!rootJSON.IsObject) { throw new InvalidJSONException("Root is not a JSON object"); } float bpm = 120f; if ( rootJSON.TryGetKey("_bpm", out JSONNode bpmJSON) && bpmJSON is JSONNumber bpmJSONNumber && bpmJSONNumber.AsFloat > 0f ) { bpmJSON = bpmJSONNumber.AsFloat; } ColorPreset colorPreset = null; if (rootJSON.TryGetKey("colorPreset", out JSONNode colorPresetJSON)) { if (colorPresetJSON is JSONString colorPresetJSONString) { colorPreset = ColorPreset.GetColorPreset(colorPresetJSONString.Value); if (colorPreset == null) { Logger.log.Warn("Couldn't find color preset for lightshow: " + name); } } else { try { colorPreset = ColorPreset.LoadColorPresetFromJSON(colorPresetJSON); } catch (InvalidJSONException e) { Logger.log.Warn("Failed loading color preset for lightshow: " + name); Logger.log.Warn("\tInvalid JSON data: " + e.errorMessage); } } } bool eventsExists = rootJSON.TryGetKey("_events", out JSONNode eventsNodeJSON); if (!eventsExists || !eventsNodeJSON.IsArray) { throw new InvalidJSONException("No events JSON array found in root"); } JSONArray eventsJSON = eventsNodeJSON.AsArray; List <BeatmapEventData> eventsList = new List <BeatmapEventData>(); foreach (JSONNode eventJSON in eventsJSON) { bool timeExists = eventJSON.TryGetKey("_time", out JSONNode timeJSON); bool typeExists = eventJSON.TryGetKey("_type", out JSONNode typeJSON); bool valueExists = eventJSON.TryGetKey("_value", out JSONNode valueJSON); if (!timeExists || !typeExists || !valueExists) { continue; } if (!timeJSON.IsNumber || !typeJSON.IsNumber || !valueJSON.IsNumber) { continue; } float time = timeJSON.AsFloat; int type = typeJSON.AsInt; int value = valueJSON.AsInt; if (type < -1 || type > 15) { continue; } if ( eventJSON.TryGetKey("_customData", out JSONNode customDataJSON) && customDataJSON is JSONObject customDataJSONObject ) { Color? color = null; ColorGradient?colorGradient = null; float? direction = null; float? rotationSpeed = null; bool? rotationLockPosition = null; float? rotationStartPosition = null; string ringsNameFilter = null; bool? ringsCounterSpin = null; bool? ringsReset = null; float? ringsStep = null; float? ringsPropagationSpeed = null; float? ringsFlexySpeed = null; float? ringsStepMultiplier = null; float? ringsPropagationSpeedMultiplier = null; float? ringsFlexySpeedMultiplier = null; if ( customDataJSONObject.TryGetKey("_color", out JSONNode colorJSON) && TryParseColor(colorJSON, out Color col) ) { color = col; } if ( customDataJSON.TryGetKey("_lightGradient", out JSONNode colorGradientJSON) && colorGradientJSON is JSONObject colorGradientJSONObject && colorGradientJSONObject.TryGetKey("_startColor", out JSONNode startColorJSON) && colorGradientJSONObject.TryGetKey("_endColor", out JSONNode endColorJSON) && colorGradientJSONObject.TryGetKey("_duration", out JSONNode durationJSON) && TryParseColor(startColorJSON, out Color startColor) && TryParseColor(endColorJSON, out Color endColor) && durationJSON is JSONNumber durationJSONNumber && durationJSONNumber.AsFloat > 0f ) { float currentBPM = bpm; if ( colorGradientJSONObject.TryGetKey("_bpm", out JSONNode currentBPMJSON) && currentBPMJSON is JSONNumber currentBPMJSONNumber && currentBPMJSONNumber.AsFloat > 0f ) { currentBPM = currentBPMJSONNumber.AsFloat; } EasingFunction.Ease?easing = null; if ( colorGradientJSONObject.TryGetKey("_easing", out JSONNode easingJSON) && easingJSON is JSONString easingJSONString && Enum.TryParse(easingJSONString.Value, out EasingFunction.Ease ease) ) { easing = ease; } colorGradient = new ColorGradient( startColor, endColor, durationJSONNumber.AsFloat * 60f / currentBPM, easing ?? EasingFunction.Ease.Linear ); } if ( customDataJSONObject.TryGetKey("_direction", out JSONNode directionJSON) && directionJSON is JSONNumber directionJSONNumber && (directionJSONNumber.AsInt == 0 || directionJSONNumber.AsInt == 1) ) { direction = directionJSONNumber.AsInt == 0 ? -1f : 1f; } if ( customDataJSONObject.TryGetKey("_preciseSpeed", out JSONNode rotationSpeedJSON) && rotationSpeedJSON is JSONNumber rotationSpeedJSONNumber ) { rotationSpeed = rotationSpeedJSONNumber.AsFloat; } if ( customDataJSONObject.TryGetKey("_lockPosition", out JSONNode rotationLockPositionJSON) && rotationLockPositionJSON is JSONBool rotationLockPositionJSONBool ) { rotationLockPosition = rotationLockPositionJSONBool.AsBool; } if ( customDataJSONObject.TryGetKey("_startPosition", out JSONNode rotationStartPositionJSON) && rotationStartPositionJSON is JSONNumber rotationStartPositionJSONNumber ) { rotationStartPosition = rotationStartPositionJSONNumber.AsFloat; } if ( customDataJSONObject.TryGetKey("_nameFilter", out JSONNode ringsNameFilterJSON) && ringsNameFilterJSON is JSONString ringsNameFilterJSONString ) { ringsNameFilter = ringsNameFilterJSONString.Value; } if ( customDataJSONObject.TryGetKey("_counterSpin", out JSONNode ringsCounterSpinJSON) && ringsCounterSpinJSON is JSONBool ringsCounterSpinJSONBool ) { ringsCounterSpin = ringsCounterSpinJSONBool.AsBool; } if ( customDataJSONObject.TryGetKey("_reset", out JSONNode ringsResetJSON) && ringsResetJSON is JSONBool ringsResetJSONBool ) { ringsReset = ringsResetJSONBool.AsBool; } if ( customDataJSONObject.TryGetKey("_step", out JSONNode ringsStepJSON) && ringsStepJSON is JSONNumber ringsStepJSONNumber ) { ringsStep = ringsStepJSONNumber.AsFloat; } if ( customDataJSONObject.TryGetKey("_prop", out JSONNode ringsPropagationSpeedJSON) && ringsPropagationSpeedJSON is JSONNumber ringsPropagationSpeedJSONNumber ) { ringsPropagationSpeed = ringsPropagationSpeedJSONNumber.AsFloat; } if ( customDataJSONObject.TryGetKey("_speed", out JSONNode ringsFlexySpeedJSON) && ringsFlexySpeedJSON is JSONNumber ringsFlexySpeedJSONNumber ) { ringsFlexySpeed = ringsFlexySpeedJSONNumber.AsFloat; } if ( customDataJSONObject.TryGetKey("_stepMult", out JSONNode ringsStepMultiplierJSON) && ringsStepMultiplierJSON is JSONNumber ringsStepMultiplierJSONNumber ) { ringsStepMultiplier = ringsStepMultiplierJSONNumber.AsFloat; } if ( customDataJSONObject.TryGetKey("_propMult", out JSONNode ringsPropagationSpeedMultiplierJSON) && ringsPropagationSpeedMultiplierJSON is JSONNumber ringsPropagationSpeedMultiplierJSONNumber ) { ringsPropagationSpeedMultiplier = ringsPropagationSpeedMultiplierJSONNumber.AsFloat; } if ( customDataJSONObject.TryGetKey("_speedMult", out JSONNode ringsFlexySpeedMultiplierJSON) && ringsFlexySpeedMultiplierJSON is JSONNumber ringsFlexySpeedMultiplierJSONNumber ) { ringsFlexySpeedMultiplier = ringsFlexySpeedMultiplierJSONNumber.AsFloat; } CustomBeatmapEventData customEventData = new CustomBeatmapEventData( time, (BeatmapEventType)type, value, color, colorGradient, direction, rotationSpeed, rotationLockPosition, rotationStartPosition, ringsNameFilter, ringsCounterSpin, ringsReset, ringsStep, ringsPropagationSpeed, ringsFlexySpeed, ringsStepMultiplier, ringsPropagationSpeedMultiplier, ringsFlexySpeedMultiplier ); eventsList.Add(customEventData); continue; } eventsList.Add(new BeatmapEventData(time, (BeatmapEventType)type, value)); } if (eventsList.Count == 0) { throw new InvalidJSONException("No valid events found in events JSON array"); } BeatmapEventData[] events = eventsList.OrderBy(beatmapEvent => beatmapEvent.time).ToArray(); return(new CustomLightshowData(events, colorPreset)); }