Advertisement
Pro_Unit

ScriptGenerator

Mar 2nd, 2019
457
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.56 KB | None | 0 0
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. namespace GameCore.CodeGeneration
  4. {
  5.     using System.Linq;
  6.     using System.Text.RegularExpressions;
  7.     using System;
  8.  
  9.     using UnityEngine;
  10.  
  11.     [CreateAssetMenu (fileName = "ScriptGenerator", menuName = "GameCore/CodeGeneration/ScriptGenerator", order = 0)]
  12. public class ScriptGenerator : ScriptableObject
  13.     {
  14.         [SerializeField] TextAsset template;
  15.         [Multiline]
  16.         [Tooltip ("Example: \"/Scripts/$NAMESPACE_NAME$/$SCRIPT_NAME$/$SCRIPT_NAME$$TYPE_NAME$.cs\"")]
  17.         [SerializeField] string pathTemplate = "/Scripts/$NAMESPACE_NAME$/$SCRIPT_NAME$/$SCRIPT_NAME$$TYPE_NAME$.cs";
  18.         [SerializeField] List<ScriptGenerator> dependies;
  19.         public List<ScriptGenerator> Dependies => dependies.FindAll (d => d != this);
  20.         [SerializeField] List<string> keys;
  21.  
  22.         public List<string> Keys => keys;
  23.  
  24.         [ContextMenu ("Parse Keys")]
  25.         private void parseKeys ()
  26.         {
  27.             var matchs = Regex.Matches (template.text, @"\$[A-Z]*_[A-Z]*\$");
  28.  
  29.             keys = matchs.Cast<Match> ().Select (m => m.Value).Distinct ().ToList ();
  30.         }
  31.         public static List<ScriptGenerator> generatedScripts = new List<ScriptGenerator> ();
  32.         public Dictionary<string, string> Generate (Dictionary<string, string> keySelector)
  33.         {
  34.             if (generatedScripts != null && generatedScripts.Contains (this))
  35.                 return null;
  36.             else
  37.                 generatedScripts.Add (this);
  38.             string filePath = Application.dataPath + pathTemplate;
  39.             foreach (var keyValue in keySelector)
  40.                 filePath = filePath.Replace (keyValue.Key, keyValue.Value.Replace('.','/'));
  41.  
  42.             filePath = filePath.Replace ("$SCRIPT_NAME$", name);
  43.  
  44.             // Debug.Log ($"{filePath}");
  45.  
  46.             // bool isExist = File.Exists (filePath);
  47.  
  48.             // Debug.Log ($"{filePath} Exist {isExist}");
  49.  
  50.             // if (isExist) return null;
  51.  
  52.             Dictionary<string, string> pathTextResult = new Dictionary<string, string> ();
  53.             string ScriptText = template.text;
  54.  
  55.             foreach (var kvp in keySelector)
  56.                 ScriptText = ScriptText.Replace (kvp.Key, kvp.Value);
  57.  
  58.             pathTextResult.Add (filePath, ScriptText);
  59.  
  60.             foreach (var scriptGenerator in Dependies)
  61.             {
  62.                 var dict = scriptGenerator.Generate (keySelector);
  63.                 if(dict == null) continue;
  64.                 foreach (var pair in dict)
  65.                     if (!pathTextResult.ContainsKey (pair.Key))
  66.                         pathTextResult.Append (pair);
  67.             }
  68.  
  69.             return pathTextResult;
  70.  
  71.             // File.WriteAllText (filePath, ScriptText);
  72.  
  73.         }
  74.         // public List<ScriptGenerator> GetDependiosWithout (ScriptGenerator scriptGenerator)
  75.         // {
  76.         //     List<ScriptGenerator> result = new List<ScriptGenerator> (dependies);
  77.  
  78.         //     if (result.Contains (scriptGenerator))
  79.         //         result.Remove (scriptGenerator);
  80.  
  81.         //     return result;
  82.         // }
  83.  
  84.         public IEnumerable<string> GetKeysWithdependies ()
  85.         {
  86.             var keysofDependies = Dependies
  87.                 .SelectMany (sg => sg.Keys);
  88.             return keys.Concat (keysofDependies).Distinct ();
  89.         }
  90.  
  91.         [Sirenix.OdinInspector.Button] public void parseKeysByButton () => parseKeys ();
  92.         [Sirenix.OdinInspector.Button]
  93.         [ContextMenu ("PrintAllKeys")] void PrintAllKeys () => Debug.Log (Keys.Log ());
  94.     }
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement