using System.Linq;
using System.IO;
using System.Reflection;
+using System.Text;
+using System.Diagnostics;
namespace Tests
}
void Tv_SelectedItemChanged (object sender, SelectionChangeEventArgs e)
{
-// FileInfo fi = e.NewValue as FileInfo;
-// if (fi == null)
-// return;
-// if (fi.Extension == ".crow" || fi.Extension == ".goml") {
-// IMLStream imls = new IMLStream (fi.FullName);
-// lock (CrowInterface.UpdateMutex) {
-// (CrowInterface.FindByName ("crowContainer") as Container).SetChild
-// (imls.Instance);
-// CurSources = imls.Source;
-// }
-// }
+ FileInfo fi = e.NewValue as FileInfo;
+ if (fi == null)
+ return;
+ if (fi.Extension == ".crow" || fi.Extension == ".goml") {
+ Instantiator i = new Instantiator(fi.FullName);
+ lock (CrowInterface.UpdateMutex) {
+ (CrowInterface.FindByName ("crowContainer") as Container).SetChild
+ (i.CreateInstance());
+ CurSources = i.GetImlSourcesCode();
+ }
+ }
+ }
+ void onImlSourceChanged(Object sender, TextChangeEventArgs e){
+ Instantiator i;
+ try {
+ i = Instantiator.CreateFromImlFragment (e.Text);
+ } catch (Exception ex) {
+ Debug.WriteLine (ex);
+ return;
+ }
+ lock (CrowInterface.UpdateMutex) {
+ (CrowInterface.FindByName ("crowContainer") as Container).SetChild
+ (i.CreateInstance());
+ }
}
void onButClick(object send, MouseButtonEventArgs e)
{
<TextBox Background="White" Height="Fit"
VerticalAlignment="Top"
TextAlignment="TopLeft" Font="mono, 10"
+ TextChanged="onImlSourceChanged"
Text="{CurSources}" Multiline="true"/>
</Scroller>
<ScrollBar Name="scrollbar1" Value="{../scroller1.ScrollY}"
{
Interface.LoaderInvoker loader = null;
- public string ImlPath;
- public Stream ImlStream;
- public Type RootType = null;
+ string ImlPath;
+ Stream ImlStream;
DynamicMethod dm = null;
+
public ILGenerator il = null;
+ public Type RootType = null;
+
+ /// <summary>
+ /// Finalize instatiator MSIL and return LoaderInvoker delegate
+ /// </summary>
+ public Interface.LoaderInvoker GetLoader(){
+ if (loader != null)
+ return loader;
+
+ il.Emit(OpCodes.Ret);
+ loader = (Interface.LoaderInvoker)dm.CreateDelegate (typeof(Interface.LoaderInvoker));
+ return loader;
+ }
#region CTOR
public IMLReader (string path)
Read();//close tag
}
/// <summary>
- /// Finalize instatiator MSIL and return LoaderInvoker delegate
- /// </summary>
- public Interface.LoaderInvoker GetLoader(){
- if (loader != null)
- return loader;
-
- il.Emit(OpCodes.Ret);
- loader = (Interface.LoaderInvoker)dm.CreateDelegate (typeof(Interface.LoaderInvoker));
- return loader;
- }
- /// <summary>
/// Inits il generator, RootType must have been read first
/// </summary>
void InitEmitter(){
List<string[]> itemTemplateIds = new List<string[]> ();
bool inlineTemplate = false;
reader.Read ();
-
+ int depth = reader.Depth + 1;
while (reader.Read ()) {
- if (!reader.IsStartElement ())
+ if (!reader.IsStartElement () || reader.Depth > depth)
continue;
if (reader.Name == "Template") {
inlineTemplate = true;
reader.Read ();
readChildren (reader, crowType,true);
- continue;
} else if (reader.Name == "ItemTemplate") {
string dataType = "default", datas = "", path = "";
while (reader.MoveToNextAttribute ()) {
itemTemplateIds.Add (new string[] { dataType, uid, datas });
}
-
- continue;
}
}
// along with this program. If not, see <http://www.gnu.org/licenses/>.
using System;
using System.Threading;
+using System.IO;
+using System.Text;
namespace Crow
{
{
public Type RootType;
Interface.LoaderInvoker loader;
+ string imlPath;
+
#region CTOR
public Instantiator (string path){
- System.Globalization.CultureInfo savedCulture = Thread.CurrentThread.CurrentCulture;
- Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.InvariantCulture;
+ imlPath = path;
#if DEBUG_LOAD
Stopwatch loadingTime = new Stopwatch ();
RootType = itr.RootType;
}
} catch (Exception ex) {
- throw new Exception ("Error loading <" + path + ">:", ex);
+ throw new Exception ("Error loading <" + path + ">:\n", ex);
}
#if DEBUG_LOAD
Debug.WriteLine ("IML Instantiator creation '{2}' : {0} ticks, {1} ms",
loadingTime.ElapsedTicks, loadingTime.ElapsedMilliseconds, path);
#endif
-
- Thread.CurrentThread.CurrentCulture = savedCulture;
+ }
+ public static Instantiator CreateFromImlFragment(string fragment){
+ try {
+ using (Stream s = new MemoryStream(Encoding.UTF8.GetBytes(fragment))){
+ return new Instantiator(s);
+ }
+ } catch (Exception ex) {
+ throw new Exception ("Error loading fragment:\n" + fragment + "\n", ex);
+ }
+ }
+ public Instantiator (Stream stream){
+ #if DEBUG_LOAD
+ Stopwatch loadingTime = new Stopwatch ();
+ loadingTime.Start ();
+ #endif
+ using (IMLReader itr = new IMLReader (stream)){
+ loader = itr.GetLoader ();
+ RootType = itr.RootType;
+ }
+ #if DEBUG_LOAD
+ loadingTime.Stop ();
+ Debug.WriteLine ("IML Instantiator creation '{2}' : {0} ticks, {1} ms",
+ loadingTime.ElapsedTicks, loadingTime.ElapsedMilliseconds, path);
+ #endif
}
public Instantiator (Type _root, Interface.LoaderInvoker _loader)
{
loader (tmp);
return tmp;
}
+ public string GetImlSourcesCode(){
+ try {
+ using (StreamReader sr = new StreamReader (imlPath))
+ return sr.ReadToEnd();
+ } catch (Exception ex) {
+ throw new Exception ("Error getting sources for <" + imlPath + ">:", ex);
+ }
+ }
}
}