Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void ParseConfigFiles()
- {
- string configPath = Application.streamingAssetsPath;
- if(Directory.Exists(configPath))
- {
- foreach(string file in Directory.EnumerateFiles(configPath, @"*.txt", SearchOption.AllDirectories))
- {
- StreamReader sr = new StreamReader(file);
- string line = "";
- int active_line_index = 1;
- while(sr.EndOfStream == false)
- {
- stored_identifier = "";
- line = sr.ReadLine();
- line = line.TrimStart(' ', '\t'); //trim tabs and spaces
- line = line.TrimEnd(' ', '\t');
- int remainingChars = line.Length;
- bool already_added_tweak = false;
- while(remainingChars > 0)
- {
- string token;
- int tokenEnd = line.IndexOf(' ');
- if(tokenEnd == -1)
- {
- token = line;
- tokenEnd = line.Length;
- }
- else token = line.Remove(line.IndexOf(' '));
- remainingChars -= token.Length;
- if(tokenEnd+1 < line.Length)
- {
- line = line.Substring(tokenEnd+1);
- remainingChars = line.Length;
- }
- else remainingChars = 0;
- // Debug.Log("Parsing token: "+token);
- switch(token)
- {
- case "#":
- remainingChars = 0;
- break;
- case "zone":
- state = states.SET_ZONE;
- break;
- case "layer":
- state = states.SET_LAYER;
- break;
- case "add":
- state = states.ADD_LAYER;
- break;
- case "tweak":
- state = states.TWEAK;
- break;
- default: //identifier, action depends on current state
- switch(state)
- {
- case states.SET_ZONE:
- MusicZone targetZone = FindMusicZone(token);
- bool created_new_music_zone = false;
- if(targetZone == null)
- {
- targetZone = new MusicZone();
- targetZone.name = token;
- created_new_music_zone = true;
- }
- if(created_new_music_zone) musicZones.Add(targetZone);
- else
- {
- targetZone.tweaks = new List<Tweak>();
- targetZone.layers = new List<MusicLayer>();
- }
- c_ActiveMusicZone = targetZone;
- break;
- case states.SET_LAYER:
- MusicLayer targetLayer = FindMusicLayer(token);
- if(targetLayer == null)
- {
- Debug.LogError("File: "+file+" Line: "+active_line_index+" Attempted to set a Music Layer active that does not exist! Please verify that the names match.");
- }
- else c_ActiveMusicLayer = targetLayer;
- break;
- case states.ADD_LAYER:
- MusicLayer targetLayer2 = FindMusicLayer(token);
- if(targetLayer2 == null)
- {
- Debug.LogError("File: "+file+" Line: "+active_line_index+" Attempted to add a music layer that does not exist! Please verify that the names match.");
- }
- if(c_ActiveMusicZone != null) c_ActiveMusicZone.layers.Add(targetLayer2);
- break;
- case states.TWEAK:
- if(already_added_tweak == false)
- {
- c_ActiveTweak = new Tweak();
- c_ActiveTweak.target = FindMusicLayer(token);
- if(c_ActiveTweak.target == null)
- {
- Debug.Log("File: "+file+" Line: "+active_line_index+" Unable to find target for tweaking \""+token+"\", perhaps it is misspelled?");
- }
- already_added_tweak = true;
- }
- else
- {
- if(c_ActiveTweak != null) //we only parse the rest of the line if we successfully found a tweak target
- {
- float value1;
- if(float.TryParse(token, out value1))
- {
- c_ActiveTweak.value = value1;
- switch(stored_identifier)
- {
- case "osc_high_volume":
- c_ActiveTweak.type = tweakType.OSC_HIGH_VOLUME;
- break;
- case "osc_low_volume":
- c_ActiveTweak.type = tweakType.OSC_LOW_VOLUME;;
- break;
- case "offset_volume":
- c_ActiveTweak.type = tweakType.OFFSET_VOLUME;
- break;
- case "blend_duration":
- c_ActiveTweak.type = tweakType.BLEND_DURATION;
- break;
- case "wind_edge_influence":
- c_ActiveTweak.type = tweakType.WIND_EDGE_INFLUENCE;
- break;
- }
- if(c_ActiveMusicZone != null) c_ActiveMusicZone.tweaks.Add(c_ActiveTweak);
- }
- else stored_identifier = token;
- }
- }
- break;
- default:
- //we must assume that the token is intended to identify a variable or a value, so we will check against a list of known variable names
- float value;
- if(float.TryParse(token, out value))
- {
- switch(stored_identifier)
- {
- case "osc_frequency_low":
- if (c_ActiveMusicZone != null) c_ActiveMusicZone.osc_frequency_low = value;
- break;
- case "osc_frequency_high":
- if (c_ActiveMusicZone != null) c_ActiveMusicZone.osc_frequency_high = value;
- break;
- case "osc_transition_interval":
- if (c_ActiveMusicZone != null) c_ActiveMusicZone.osc_transition_interval = value;
- break;
- case "osc_transition_duration":
- if (c_ActiveMusicZone != null) c_ActiveMusicZone.osc_transition_duration = value;
- break;
- case "osc_low_volume":
- if(c_ActiveMusicLayer != null) c_ActiveMusicLayer.osc_low_volume = value;
- break;
- case "osc_high_volume":
- if(c_ActiveMusicLayer != null) c_ActiveMusicLayer.osc_high_volume = value;
- break;
- case "offset_volume":
- if(c_ActiveMusicLayer != null) c_ActiveMusicLayer.offset_volume = value;
- break;
- case "blend_duration":
- if(c_ActiveMusicLayer != null) c_ActiveMusicLayer.blend_duration = value;
- break;
- case "wind_edge_influence":
- if(c_ActiveMusicLayer != null) c_ActiveMusicLayer.wind_edge_influence = value;
- break;
- default:
- Debug.LogError("File: "+file+" Line: "+active_line_index+" Attempting to modify an unknown variable, please verify the name!");
- break;
- }
- }
- else stored_identifier = token;
- break;
- }
- break;
- }
- }
- state = states.DEFAULT;
- active_line_index++;
- }
- sr.Close();
- }
- }
- foreach(MusicLayer ml in musicLayers) //We mirror all the default settings so that we can restore from tweaks easily
- {
- ml.__offset_volume = ml.offset_volume;
- ml.__osc_high_volume = ml.osc_high_volume;
- ml.__osc_low_volume = ml.osc_low_volume;
- ml.__blend_duration = ml.blend_duration;
- ml.___wind_edge_influence = ml.wind_edge_influence;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement