summaryrefslogtreecommitdiff
path: root/Client/Assets/Scripts/XUtliPoolLib/XSkillScriptGen.cs
blob: cf1d4f3461f15701cbecd882809f63ac91c6e6dc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Text;
using System.Xml;

namespace XUtliPoolLib
{
	public class XSkillScriptGen : XSingleton<XSkillScriptGen>
	{
		private string _template;

		public readonly string ScriptPath = "..\\..\\src\\client\\XMainClient\\XMainClient\\Script\\XSkillGen\\XScriptCode\\";

		private readonly string ProjectFile = "..\\..\\src\\client\\XMainClient\\XMainClient\\XMainClient.csproj";

		private readonly string NameSpace = "http://schemas.microsoft.com/developer/msbuild/2003";

		public XSkillScriptGen()
		{
			this._template = this.LoadTemplate();
		}

		public bool ScriptGen(string skill, string scriptname)
		{
			Dictionary<string, string> dictionary = new Dictionary<string, string>();
			dictionary.Add("class_name", skill);
			dictionary.Add("method_name", scriptname);
			string text = this.TemplateFormat(this._template, dictionary);
			bool flag = text != null && text != "";
			if (flag)
			{
				string path = string.Concat(new string[]
				{
					this.ScriptPath,
					skill,
					"_",
					scriptname,
					".cs"
				});
				using (FileStream fileStream = new FileStream(path, FileMode.Create))
				{
					StreamWriter streamWriter = new StreamWriter(fileStream, Encoding.UTF8);
					streamWriter.Write(text);
					streamWriter.Close();
				}
				this.AddToProject(skill + "_" + scriptname + ".cs");
			}
			return text != null && text.Length > 0;
		}

		public bool ScriptDel(string skill, string scriptname)
		{
			return this.DelFromProject(skill + "_" + scriptname + ".cs");
		}

		private void AddToProject(string addname)
		{
			XmlDocument xmlDocument = new XmlDocument();
			xmlDocument.Load(this.ProjectFile);
			XmlNamespaceManager xmlNamespaceManager = new XmlNamespaceManager(xmlDocument.NameTable);
			xmlNamespaceManager.AddNamespace("ms", this.NameSpace);
			XmlNode documentElement = xmlDocument.DocumentElement;
			XmlNode xmlNode = documentElement.SelectSingleNode("ms:ItemGroup/ms:Compile", xmlNamespaceManager);
			XmlElement xmlElement = xmlDocument.CreateElement("Compile", this.NameSpace);
			xmlElement.SetAttribute("Include", "Script\\XSkillGen\\XScriptCode\\" + addname);
			xmlNode.ParentNode.AppendChild(xmlElement);
			xmlDocument.Save(this.ProjectFile);
		}

		private bool DelFromProject(string name)
		{
			string str = "Script\\XSkillGen\\XScriptCode\\" + name;
			XmlDocument xmlDocument = new XmlDocument();
			xmlDocument.Load(this.ProjectFile);
			XmlNamespaceManager xmlNamespaceManager = new XmlNamespaceManager(xmlDocument.NameTable);
			xmlNamespaceManager.AddNamespace("ms", this.NameSpace);
			XmlNode documentElement = xmlDocument.DocumentElement;
			XmlNode xmlNode = documentElement.SelectSingleNode("ms:ItemGroup/ms:Compile[@Include='" + str + "']", xmlNamespaceManager);
			bool flag = xmlNode != null;
			if (flag)
			{
				xmlNode.ParentNode.RemoveChild(xmlNode);
			}
			File.Delete(this.ScriptPath + name);
			xmlDocument.Save(this.ProjectFile);
			return xmlNode != null;
		}

		private string LoadTemplate()
		{
			//!
			Assembly assembly = Assembly.Load("XMainClient");
			Stream manifestResourceStream = assembly.GetManifestResourceStream("XMainClient.Script.XSkillGen.SkillGenTemplate.txt");
			byte[] array = new byte[5120];
			int count = (int)manifestResourceStream.Length;
			manifestResourceStream.Read(array, 0, count);
			manifestResourceStream.Close();
			UTF8Encoding utf8Encoding = new UTF8Encoding();
			return utf8Encoding.GetString(array, 0, count);
		}

		private string TemplateFormat(string template, Dictionary<string, string> dicts)
		{
			StringBuilder stringBuilder = new StringBuilder();
			int num = 0;
			int num2;
			for (;;)
			{
				num2 = template.IndexOf("%(", num);
				bool flag = num2 != -1;
				if (!flag)
				{
					goto IL_AE;
				}
				stringBuilder.Append(template.Substring(num, num2 - num));
				num = template.IndexOf(")s", num2);
				bool flag2 = num != -1;
				if (!flag2)
				{
					goto IL_9A;
				}
				string key = template.Substring(num2 + 2, num - num2 - 2);
				bool flag3 = dicts.ContainsKey(key);
				if (!flag3)
				{
					break;
				}
				stringBuilder.Append(dicts[key]);
				num += 2;
			}
			return "";
			IL_9A:
			stringBuilder.Append(template.Substring(num2));
			goto IL_C8;
			IL_AE:
			stringBuilder.Append(template.Substring(num));
			IL_C8:
			return stringBuilder.ToString();
		}

		public override bool Init()
		{
			return true;
		}

		public override void Uninit()
		{
		}
	}
}