29 using System.Collections.Generic;
32 using Microsoft.CSharp;
33 using OpenSim.Region.ScriptEngine.Shared.CodeTools;
34 using System.CodeDom.Compiler;
42 private static CSharpCodeProvider CScodeProvider =
new CSharpCodeProvider();
44 static void Main(
string[] args)
50 Console.WriteLine(
"No input file specified");
54 if (!File.Exists(args[0]))
56 Console.WriteLine(
"Input file does not exist");
63 source = cvt.Convert(File.ReadAllText(args[0]));
67 Console.WriteLine(
"Conversion failed:\n"+e.Message);
71 source = CreateCSCompilerScript(source);
75 Console.WriteLine(CompileFromDotNetText(source,
"a.out"));
79 Console.WriteLine(
"Conversion failed: "+e.Message);
86 private static string CreateCSCompilerScript(
string compileScript)
88 compileScript = String.Empty +
89 "using OpenSim.Region.ScriptEngine.Shared; using System.Collections.Generic;\r\n" +
90 String.Empty +
"namespace SecondLife { " +
91 String.Empty +
"public class Script : OpenSim.Region.ScriptEngine.Shared.ScriptBase.ScriptBaseClass { \r\n" +
92 @"public Script() { } " +
98 private static string CompileFromDotNetText(
string Script,
string asset)
101 string OutFile = asset;
106 File.Delete(OutFile);
110 throw new Exception(
"Unable to delete old existing "+
111 "script-file before writing new. Compile aborted: " +
116 CompilerParameters parameters =
new CompilerParameters();
118 parameters.IncludeDebugInformation =
true;
121 Path.GetDirectoryName(AppDomain.CurrentDomain.BaseDirectory);
123 parameters.ReferencedAssemblies.Add(Path.Combine(rootPath,
124 "OpenSim.Region.ScriptEngine.Shared.dll"));
125 parameters.ReferencedAssemblies.Add(Path.Combine(rootPath,
126 "OpenSim.Region.ScriptEngine.Shared.Api.Runtime.dll"));
128 parameters.GenerateExecutable =
false;
129 parameters.OutputAssembly = OutFile;
130 parameters.IncludeDebugInformation =
true;
131 parameters.WarningLevel = 1;
132 parameters.TreatWarningsAsErrors =
false;
134 CompilerResults results = CScodeProvider.CompileAssemblyFromSource(parameters, Script);
136 if (results.Errors.Count > 0)
138 string errtext = String.Empty;
139 foreach (CompilerError CompErr
in results.Errors)
141 string severity = CompErr.IsWarning ?
"Warning" :
"Error";
143 KeyValuePair<int, int> lslPos;
145 lslPos = FindErrorPosition(CompErr.Line, CompErr.Column);
147 string text = CompErr.ErrorText;
149 text = ReplaceTypes(CompErr.ErrorText);
153 errtext += String.Format(
"Line ({0},{1}): {4} {2}: {3}\n",
154 lslPos.Key - 1, lslPos.Value - 1,
155 CompErr.ErrorNumber, text, severity);
158 disp =
"Completed with errors";
160 if (!File.Exists(OutFile))
162 throw new Exception(errtext);
166 if (!File.Exists(OutFile))
168 string errtext = String.Empty;
169 errtext +=
"No compile error. But not able to locate compiled file.";
170 throw new Exception(errtext);
178 FileInfo fi =
new FileInfo(OutFile);
182 string errtext = String.Empty;
183 errtext +=
"No compile error. But not able to stat file.";
184 throw new Exception(errtext);
187 Byte[] data =
new Byte[fi.Length];
191 FileStream fs = File.Open(OutFile, FileMode.Open, FileAccess.Read);
192 fs.Read(data, 0, data.Length);
197 string errtext = String.Empty;
198 errtext +=
"No compile error. But not able to open file.";
199 throw new Exception(errtext);
204 string filetext = System.Convert.ToBase64String(data);
205 Byte[] buf = Encoding.ASCII.GetBytes(filetext);
206 FileStream sfs = File.Create(OutFile +
".text");
207 sfs.Write(buf, 0, buf.Length);
210 string posmap = String.Empty;
222 buf = Encoding.ASCII.GetBytes(posmap);
224 FileStream mfs = File.Create(OutFile +
".map");
225 mfs.Write(buf, 0, buf.Length);
231 private static string ReplaceTypes(
string message)
233 message = message.Replace(
234 "OpenSim.Region.ScriptEngine.Shared.LSL_Types.LSLString",
237 message = message.Replace(
238 "OpenSim.Region.ScriptEngine.Shared.LSL_Types.LSLInteger",
241 message = message.Replace(
242 "OpenSim.Region.ScriptEngine.Shared.LSL_Types.LSLFloat",
245 message = message.Replace(
246 "OpenSim.Region.ScriptEngine.Shared.LSL_Types.list",
252 private static KeyValuePair<int, int> FindErrorPosition(
int line,
int col)
255 return FindErrorPosition(line, col, null);
258 private class kvpSorter : IComparer<KeyValuePair<KeyValuePair<int, int>, KeyValuePair<int, int>>>
260 public int Compare(KeyValuePair<KeyValuePair<int, int>, KeyValuePair<int, int>> a,
261 KeyValuePair<KeyValuePair<int, int>, KeyValuePair<int, int>> b)
263 int kc = a.Key.Key.CompareTo(b.Key.Key);
264 return (kc != 0) ? kc : a.Key.Value.CompareTo(b.Key.Value);
269 int col, Dictionary<KeyValuePair<int, int>,
270 KeyValuePair<int, int>> positionMap)
272 if (positionMap == null || positionMap.Count == 0)
273 return new KeyValuePair<int, int>(line, col);
275 KeyValuePair<int, int> ret =
new KeyValuePair<int, int>();
277 if (positionMap.TryGetValue(
new KeyValuePair<int, int>(line, col),
281 var sorted =
new List<KeyValuePair<KeyValuePair<int, int>, KeyValuePair<int, int>>>(positionMap);
283 sorted.Sort(
new kvpSorter());
289 foreach (KeyValuePair<KeyValuePair<int, int>, KeyValuePair<int, int>> posmap
in sorted)
292 int nl = posmap.Value.Key + line - posmap.Key.Key;
293 int nc = posmap.Value.Value + col - posmap.Key.Value;
295 if (posmap.Key.Key > line)
305 if (posmap.Key.Key == line && posmap.Key.Value > col)
308 if (nl > l || (nl == l && nc > c))
319 l = posmap.Value.Key;
320 c = posmap.Value.Value;
322 return new KeyValuePair<int, int>(l, c);
Interactive OpenSim region server