public static RadioContextCondition LoadFromJsonUsingType(JsonData json) { RadioContextCondition context = null; switch ((String)json["type"]) { case "time": context = TimeContextCondition.LoadFromJson(json); break; case "weather": context = WeatherContextCondition.LoadFromJson(json); break; case "mood": context = MoodContextCondition.LoadFromJson(json); break; case "disaster": context = DisasterContextCondition.LoadFromJson(json); break; default: Debug.LogError("[CSLMusic] Error: Unknown context type!"); break; } return(context); }
public static UserRadioChannel LoadFromJson(String filename) { try { string data = File.ReadAllText(filename); JsonData json = JsonMapper.ToObject(data); UserRadioChannel channel = new UserRadioChannel(); channel.m_Name = (String)json["name"]; if (json.Keys.Contains("collections")) { List <String> collections = new List <string>(); foreach (JsonData v in json["collections"]) { collections.Add((String)v); } channel.m_Collections = new HashSet <string>(collections); } else { channel.m_Collections = new HashSet <string>(new string[] { channel.m_Name }); } if (json.Keys.Contains("thumbnail")) { channel.m_ThumbnailFile = Path.Combine(Path.GetDirectoryName(filename), (String)json["thumbnail"]); } if (json.Keys.Contains("schedule")) { List <RadioChannelInfo.State> states = new List <RadioChannelInfo.State>(); foreach (JsonData entry in json["schedule"]) { RadioChannelInfo.State state = new RadioChannelInfo.State(); state.m_contentType = (RadioContentInfo.ContentType)Enum.Parse(typeof(RadioContentInfo.ContentType), (String)entry["type"], true); state.m_minCount = (int)entry["min"]; state.m_maxCount = (int)entry["max"]; states.Add(state); } channel.m_StateChain = states.ToArray(); } // Named conditions var namedConditions = new Dictionary <string, RadioContextCondition>(); if (json.Keys.Contains("filters") && json["filters"].IsObject) { foreach (var name in json["filters"].Keys) { var entry = json["filters"][name]; var cond = RadioContextCondition.LoadFromJsonUsingType(entry); if (cond != null) { namedConditions[name] = cond; } } } if (json.Keys.Contains("contexts")) { foreach (JsonData entry in json["contexts"]) { var context = RadioContext.LoadFromJson(entry, namedConditions); if (context != null) { context.m_RadioChannel = channel; channel.m_Contexts.Add(context); // Auto-load collections that are defined in contexts foreach (var coll in context.m_Collections) { channel.m_Collections.Add(coll); } } } } return(channel); } catch (Exception ex) { Debug.LogError(ex); return(null); } }