Advertisement
Guest User

Taiji Music System Parser

a guest
May 17th, 2022
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 11.37 KB | None | 0 0
  1. void ParseConfigFiles()
  2. {
  3.     string configPath = Application.streamingAssetsPath;
  4.     if(Directory.Exists(configPath))
  5.     {
  6.         foreach(string file in Directory.EnumerateFiles(configPath, @"*.txt", SearchOption.AllDirectories))
  7.         {
  8.             StreamReader sr = new StreamReader(file);
  9.             string line = "";
  10.             int active_line_index = 1;
  11.             while(sr.EndOfStream == false)
  12.             {
  13.                 stored_identifier = "";
  14.                 line = sr.ReadLine();
  15.                 line = line.TrimStart(' ', '\t'); //trim tabs and spaces
  16.                 line = line.TrimEnd(' ', '\t');
  17.                 int remainingChars = line.Length;
  18.                 bool already_added_tweak = false;
  19.                 while(remainingChars > 0)
  20.                 {
  21.                     string token;
  22.                     int tokenEnd = line.IndexOf(' ');
  23.                     if(tokenEnd == -1)
  24.                     {
  25.                         token = line;
  26.                         tokenEnd = line.Length;
  27.                     }
  28.                     else token = line.Remove(line.IndexOf(' '));
  29.                     remainingChars -= token.Length;
  30.                     if(tokenEnd+1 < line.Length)
  31.                     {
  32.                         line = line.Substring(tokenEnd+1);
  33.                         remainingChars = line.Length;
  34.                     }
  35.                     else remainingChars = 0;
  36.                    
  37.                    
  38.                     // Debug.Log("Parsing token: "+token);
  39.                    
  40.                     switch(token)
  41.                     {
  42.                         case "#":
  43.                             remainingChars = 0;
  44.                         break;
  45.                         case "zone":
  46.                             state = states.SET_ZONE;
  47.                         break;
  48.                         case "layer":
  49.                             state = states.SET_LAYER;
  50.                         break;
  51.                         case "add":
  52.                             state = states.ADD_LAYER;
  53.                         break;
  54.                         case "tweak":
  55.                             state = states.TWEAK;
  56.                         break;
  57.                         default: //identifier, action depends on current state
  58.                             switch(state)
  59.                             {
  60.                                 case states.SET_ZONE:
  61.                                     MusicZone targetZone = FindMusicZone(token);
  62.                                     bool created_new_music_zone = false;
  63.                                     if(targetZone == null)
  64.                                     {
  65.                                         targetZone = new MusicZone();
  66.                                         targetZone.name = token;
  67.                                         created_new_music_zone = true;
  68.                                     }
  69.                                     if(created_new_music_zone) musicZones.Add(targetZone);
  70.                                     else
  71.                                     {
  72.                                         targetZone.tweaks = new List<Tweak>();
  73.                                         targetZone.layers = new List<MusicLayer>();
  74.                                     }
  75.                                     c_ActiveMusicZone = targetZone;
  76.                                 break;
  77.                                 case states.SET_LAYER:
  78.                                     MusicLayer targetLayer = FindMusicLayer(token);
  79.                                     if(targetLayer == null)
  80.                                     {
  81.                                         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.");
  82.                                     }
  83.                                     else c_ActiveMusicLayer = targetLayer;
  84.                                 break;
  85.                                 case states.ADD_LAYER:
  86.                                     MusicLayer targetLayer2 = FindMusicLayer(token);
  87.                                     if(targetLayer2 == null)
  88.                                     {
  89.                                         Debug.LogError("File: "+file+" Line: "+active_line_index+" Attempted to add a music layer that does not exist! Please verify that the names match.");
  90.                                     }
  91.                                     if(c_ActiveMusicZone != null) c_ActiveMusicZone.layers.Add(targetLayer2);
  92.                                 break;
  93.                                 case states.TWEAK:
  94.                                     if(already_added_tweak == false)
  95.                                     {
  96.                                         c_ActiveTweak = new Tweak();
  97.                                         c_ActiveTweak.target = FindMusicLayer(token);
  98.                                         if(c_ActiveTweak.target == null)
  99.                                         {
  100.                                             Debug.Log("File: "+file+" Line: "+active_line_index+" Unable to find target for tweaking \""+token+"\", perhaps it is misspelled?");
  101.                                         }
  102.                                         already_added_tweak = true;
  103.                                     }
  104.                                     else
  105.                                     {
  106.                                         if(c_ActiveTweak != null) //we only parse the rest of the line if we successfully found a tweak target
  107.                                         {
  108.                                                
  109.                                             float value1;
  110.                                             if(float.TryParse(token, out value1))
  111.                                             {
  112.                                                 c_ActiveTweak.value = value1;
  113.                                                 switch(stored_identifier)
  114.                                                 {
  115.                                                     case "osc_high_volume":
  116.                                                         c_ActiveTweak.type = tweakType.OSC_HIGH_VOLUME;
  117.                                                     break;
  118.                                                     case "osc_low_volume":
  119.                                                         c_ActiveTweak.type = tweakType.OSC_LOW_VOLUME;;
  120.                                                     break;
  121.                                                     case "offset_volume":
  122.                                                         c_ActiveTweak.type = tweakType.OFFSET_VOLUME;
  123.                                                     break;
  124.                                                     case "blend_duration":
  125.                                                         c_ActiveTweak.type = tweakType.BLEND_DURATION;
  126.                                                     break;
  127.                                                     case "wind_edge_influence":
  128.                                                         c_ActiveTweak.type = tweakType.WIND_EDGE_INFLUENCE;
  129.                                                     break;
  130.                                                 }
  131.                                                 if(c_ActiveMusicZone != null) c_ActiveMusicZone.tweaks.Add(c_ActiveTweak);
  132.                                             }
  133.                                             else stored_identifier = token;
  134.                                         }
  135.                                     }
  136.                                 break;
  137.                                 default:
  138.                                     //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
  139.                                     float value;
  140.                                     if(float.TryParse(token, out value))
  141.                                     {
  142.                                         switch(stored_identifier)
  143.                                         {
  144.                                             case "osc_frequency_low":
  145.                                                 if (c_ActiveMusicZone != null) c_ActiveMusicZone.osc_frequency_low = value;
  146.                                             break;
  147.                                             case "osc_frequency_high":
  148.                                                 if (c_ActiveMusicZone != null) c_ActiveMusicZone.osc_frequency_high = value;
  149.                                             break;
  150.                                             case "osc_transition_interval":
  151.                                                 if (c_ActiveMusicZone != null) c_ActiveMusicZone.osc_transition_interval = value;
  152.                                             break;
  153.                                             case "osc_transition_duration":
  154.                                                 if (c_ActiveMusicZone != null) c_ActiveMusicZone.osc_transition_duration = value;
  155.                                             break;
  156.                                             case "osc_low_volume":
  157.                                                 if(c_ActiveMusicLayer != null) c_ActiveMusicLayer.osc_low_volume = value;
  158.                                             break;
  159.                                             case "osc_high_volume":
  160.                                                 if(c_ActiveMusicLayer != null) c_ActiveMusicLayer.osc_high_volume = value;
  161.                                             break;
  162.                                             case "offset_volume":
  163.                                                 if(c_ActiveMusicLayer != null) c_ActiveMusicLayer.offset_volume = value;
  164.                                             break;
  165.                                             case "blend_duration":
  166.                                                 if(c_ActiveMusicLayer != null) c_ActiveMusicLayer.blend_duration = value;
  167.                                             break;
  168.                                             case "wind_edge_influence":
  169.                                                 if(c_ActiveMusicLayer != null) c_ActiveMusicLayer.wind_edge_influence = value;
  170.                                             break;
  171.                                             default:
  172.                                                 Debug.LogError("File: "+file+" Line: "+active_line_index+" Attempting to modify an unknown variable, please verify the name!");
  173.                                             break;
  174.                                         }
  175.                                     }
  176.                                     else stored_identifier = token;
  177.                                 break;
  178.                             }
  179.                         break;
  180.                     }
  181.                    
  182.                 }
  183.                 state = states.DEFAULT;
  184.                 active_line_index++;
  185.             }
  186.             sr.Close();
  187.         }
  188.     }
  189.    
  190.     foreach(MusicLayer ml in musicLayers) //We mirror all the default settings so that we can restore from tweaks easily
  191.     {
  192.         ml.__offset_volume = ml.offset_volume;
  193.         ml.__osc_high_volume = ml.osc_high_volume;
  194.         ml.__osc_low_volume = ml.osc_low_volume;
  195.         ml.__blend_duration = ml.blend_duration;
  196.         ml.___wind_edge_influence = ml.wind_edge_influence;
  197.     }
  198. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement