| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431 |
- using System.Collections.Generic;
- using UnityEngine;
- using System.IO;
- using System.Text;
- using System;
- public class UniWebViewGradleConfig
- {
- private readonly string m_filePath;
- public UniWebViewGradleNode Root { get; }
- public UniWebViewGradleConfig(string filePath)
- {
- var file = File.ReadAllText(filePath);
- TextReader reader = new StringReader(file);
- m_filePath = filePath;
- Root = new UniWebViewGradleNode("root");
- var curNode = Root;
- var str = new StringBuilder();
- var indentationBuffer = new StringBuilder();
- var inDoubleQuote = false;
- var inSingleQuote = false;
- var inDollarVariable = false;
- var atLineStart = true;
- while (reader.Peek() > 0)
- {
- char c = (char)reader.Read();
- switch (c)
- {
- case '/':
- {
- if (!inDoubleQuote && !inSingleQuote && !inDollarVariable && reader.Peek() == '/')
- {
- var raw = str.ToString();
- var buffered = FormatStr(str);
- str = new StringBuilder();
- reader.Read();
- string comment = reader.ReadLine() ?? string.Empty;
- if (!string.IsNullOrEmpty(buffered))
- {
- curNode.AppendChildNode(new UniWebViewGradleContentNode(buffered + " //" + comment, curNode));
- }
- else
- {
- var commentNode = new UniWebViewGradleCommentNode(comment, curNode)
- {
- LeadingWhitespace = indentationBuffer.Length > 0 ? indentationBuffer.ToString() : ExtractLeadingWhitespace(raw)
- };
- curNode.AppendChildNode(commentNode);
- }
- indentationBuffer = new StringBuilder();
- inDollarVariable = false;
- atLineStart = false;
- break;
- }
- str.Append('/');
- atLineStart = false;
- break;
- }
- case '\n':
- case '\r':
- {
- var strf = FormatStr(str);
- if (!string.IsNullOrEmpty(strf))
- {
- curNode.AppendChildNode(new UniWebViewGradleContentNode(strf, curNode));
- }
- }
- inDollarVariable = false;
- str = new StringBuilder();
- indentationBuffer = new StringBuilder();
- atLineStart = true;
- break;
- case '\t':
- {
- if (atLineStart)
- {
- indentationBuffer.Append(" ");
- }
- else
- {
- str.Append(" ");
- atLineStart = false;
- }
- break;
- }
- case ' ':
- {
- if (atLineStart)
- {
- indentationBuffer.Append(' ');
- }
- else
- {
- str.Append(c);
- }
- break;
- }
- case '{':
- {
- if (inDoubleQuote || inSingleQuote) {
- str.Append(c);
- break;
- }
- var raw = str.ToString();
- var n = FormatStr(str);
- if (curNode != null) {
- // Create a node even when n is empty to preserve brace balance (e.g., nested blocks inside closures).
- var nodeName = n ?? string.Empty;
- UniWebViewGradleNode node = new UniWebViewGradleNode(nodeName, curNode);
- var leading = indentationBuffer.Length > 0 ? indentationBuffer.ToString() : ExtractLeadingWhitespace(raw);
- node.LeadingWhitespace = leading;
- curNode.AppendChildNode(node);
- curNode = node;
- }
- }
- str = new StringBuilder();
- indentationBuffer = new StringBuilder();
- atLineStart = false;
- break;
- case '}':
- {
- if (inDoubleQuote || inSingleQuote) {
- str.Append(c);
- break;
- }
- var strf = FormatStr(str);
- if (!string.IsNullOrEmpty(strf))
- {
- curNode.AppendChildNode(new UniWebViewGradleContentNode(strf, curNode));
- }
- if (indentationBuffer.Length > 0)
- {
- curNode.ClosingWhitespace = indentationBuffer.ToString();
- }
- if (curNode.Parent != null) {
- curNode = curNode.Parent;
- }
- }
- str = new StringBuilder();
- indentationBuffer = new StringBuilder();
- atLineStart = false;
- break;
- case '\"':
- if (inDollarVariable) {
- str.Append(c);
- break;
- }
- inDoubleQuote = !inDoubleQuote;
- str.Append(c);
- atLineStart = false;
- break;
- case '\'':
- if (inDollarVariable) {
- str.Append(c);
- break;
- }
- inSingleQuote = !inSingleQuote;
- str.Append(c);
- atLineStart = false;
- break;
- case '$':
- {
- if (inDoubleQuote || inSingleQuote) {
- str.Append(c);
- break;
- }
- inDollarVariable = true;
- str.Append(c);
- atLineStart = false;
- break;
- }
- default:
- str.Append(c);
- if (!char.IsWhiteSpace(c))
- {
- atLineStart = false;
- }
- break;
- }
- }
- // End of file.
- var endline = FormatStr(str);
- if (!string.IsNullOrEmpty(endline))
- {
- if (curNode != null) {
- curNode.AppendChildNode(new UniWebViewGradleContentNode(endline, curNode));
- }
- }
- //Debug.Log("Gradle parse done!");
- }
- public void Save(string path = null)
- {
- if (path == null) {
- path = m_filePath;
- }
-
- File.WriteAllText(path, Print());
- }
- private static string FormatStr(StringBuilder sb)
- {
- var str = sb.ToString();
- str = str.Trim();
- return str;
- }
- private static string ExtractLeadingWhitespace(string raw)
- {
- if (string.IsNullOrEmpty(raw)) return string.Empty;
- var sb = new StringBuilder();
- foreach (var ch in raw)
- {
- if (ch == '\t')
- {
- sb.Append(" ");
- }
- else if (ch == ' ')
- {
- sb.Append(' ');
- }
- else if (!char.IsWhiteSpace(ch))
- {
- break;
- }
- }
- return sb.ToString();
- }
- public string Print()
- {
- StringBuilder sb = new StringBuilder();
- PrintNode(sb, Root, -1);
- // Remove the first empty line.
- sb.Remove(0, 1);
- if (sb.Length > 0 && sb[sb.Length - 1] == '\n')
- {
- sb.Remove(sb.Length - 1, 1);
- }
- return sb.ToString();
- }
- private string GetLevelIndent(int level)
- {
- if (level <= 0) return "";
- return new string(' ', level * 4);
- }
- private void PrintNode(StringBuilder stringBuilder, UniWebViewGradleNode node, int level)
- {
- if (node.Parent != null) {
- var indent = node.LeadingWhitespace ?? GetLevelIndent(level);
- if (node is UniWebViewGradleCommentNode)
- {
- stringBuilder.Append("\n" + indent + @"//" + node.Name);
- }
- else
- {
- stringBuilder.Append("\n" + indent + node.Name);
- }
- }
- if (node is UniWebViewGradleContentNode || node is UniWebViewGradleCommentNode) return;
- if (node.Parent != null) {
- stringBuilder.Append(" {");
- }
- foreach (var c in node.Children) {
- PrintNode(stringBuilder, c, level + 1);
- }
- if (node.Parent != null) {
- var indent = node.ClosingWhitespace ?? GetLevelIndent(level);
- stringBuilder.Append("\n" + indent + "}");
- }
- }
- }
- public class UniWebViewGradleNode
- {
- protected string m_name;
- public UniWebViewGradleNode Parent { get; private set; }
- public string Name => m_name;
- public string LeadingWhitespace { get; set; }
- public string ClosingWhitespace { get; set; }
- public List<UniWebViewGradleNode> Children { get; private set; } = new List<UniWebViewGradleNode>();
- public UniWebViewGradleNode(string name, UniWebViewGradleNode parent = null)
- {
- Parent = parent;
- m_name = name;
- }
- public void Each(Action<UniWebViewGradleNode> f)
- {
- f(this);
- foreach (var n in Children)
- {
- n.Each(f);
- }
- }
- public void AppendChildNode(UniWebViewGradleNode node)
- {
- if (Children == null) Children = new List<UniWebViewGradleNode>();
- Children.Add(node);
- node.Parent = this;
- }
- public UniWebViewGradleNode TryGetNode(string path)
- {
- var subpath = path.Split('/');
- var cnode = this;
- foreach (var p in subpath)
- {
- if (string.IsNullOrEmpty(p)) continue;
- var tnode = cnode.FindChildNodeByName(p);
- if (tnode == null)
- {
- Debug.Log("Can't find Node:" + p);
- return null;
- }
- cnode = tnode;
- }
- return cnode;
- }
- public UniWebViewGradleNode FindChildNodeByName(string name)
- {
- foreach (var n in Children)
- {
- if (n is UniWebViewGradleCommentNode || n is UniWebViewGradleContentNode)
- continue;
- if (n.Name == name)
- return n;
- }
- return null;
- }
- public bool ReplaceContentStartsWith(string pattern, string value)
- {
- foreach (var n in Children)
- {
- if (!(n is UniWebViewGradleContentNode)) continue;
- if (n.m_name.StartsWith(pattern))
- {
- n.m_name = value;
- return true;
- }
- }
- return false;
- }
- public UniWebViewGradleContentNode ReplaceContentOrAddStartsWith(string pattern, string value)
- {
- foreach (var n in Children)
- {
- if (!(n is UniWebViewGradleContentNode)) continue;
- if (n.m_name.StartsWith(pattern))
- {
- n.m_name = value;
- return (UniWebViewGradleContentNode)n;
- }
- }
- return AppendContentNode(value);
- }
-
- public UniWebViewGradleContentNode AppendContentNode(string content)
- {
- foreach (var n in Children)
- {
- if (!(n is UniWebViewGradleContentNode)) continue;
- if (n.m_name == content)
- {
- Debug.Log("UniWebViewGradleContentNode with " + content + " already exists!");
- return null;
- }
- }
- UniWebViewGradleContentNode cnode = new UniWebViewGradleContentNode(content, this);
- AppendChildNode(cnode);
- return cnode;
- }
- public bool RemoveContentNode(string contentPattern)
- {
- for(int i=0;i<Children.Count;i++)
- {
- if (!(Children[i] is UniWebViewGradleContentNode)) continue;
- if(Children[i].m_name.Contains(contentPattern))
- {
- Children.RemoveAt(i);
- return true;
- }
- }
- return false;
- }
- }
- public sealed class UniWebViewGradleContentNode : UniWebViewGradleNode
- {
- public UniWebViewGradleContentNode(String content, UniWebViewGradleNode parent) : base(content, parent)
- {
- }
- public void SetContent(string content)
- {
- m_name = content;
- }
- }
- public sealed class UniWebViewGradleCommentNode : UniWebViewGradleNode
- {
- public UniWebViewGradleCommentNode(String content, UniWebViewGradleNode parent) : base(content, parent)
- {
- }
- public string GetComment()
- {
- return m_name;
- }
- }
|